var ModalDialogWindow;
var ModalDialogInterval;
var ModalDialog = new Object;

ModalDialog.value = '';
ModalDialog.eventhandler = '';

var displayed_reviews = 0;

function refresh_page()
{
	document.location.replace(document.location.href);
}

function ModalDialogMaintainFocus()
{
	try
	{
		if (ModalDialogWindow.closed)
		{
			window.clearInterval(ModalDialogInterval);
			eval(ModalDialog.eventhandler);
			return;
		}

//			ModalDialogWindow.focus();
	}
	catch (everything)
	{

	}
}

function ModalDialogRemoveWatch()
{
	ModalDialog.value = '';
	ModalDialog.eventhandler = '';
}

function ModalDialogShow(page_to_show, do_refresh)
{
	var do_refresh = (do_refresh == null) ? true : do_refresh;

	ModalDialogRemoveWatch();

	if(do_refresh === true)
	{
		ModalDialog.eventhandler = "refresh_page()";
	}

	var args='width=800,height=610,left=325,top=300,toolbar=0,';
	args += 'location=0,status=0,menubar=0,scrollbars=1,resizable=0'; 

	ModalDialogWindow=window.open("","",args);
	ModalDialogWindow.document.open('');
	ModalDialogWindow.document.location = page_to_show;
	ModalDialogWindow.document.close();
	ModalDialogWindow.focus();
	ModalDialogInterval = window.setInterval("ModalDialogMaintainFocus()", 5);
}


	
	function show_popup(id){
		if (document.getElementById(id)) {
			div_popup = document.getElementById(id);
			div_popup.style.display = "";
		}
	}
	
	function hide_popup(id){
		div_popup = document.getElementById(id);
		div_popup.style.display = "none";
		
	}
	



function vital_image(url, img_id){
	params = "img_id=" + img_id;
	
	alert(url);
	
	url += "&force_vital=1";
	send_ajax(params, url, 'POST', true, 'vital_image_callback');
}

function vital_image_callback(ajax_id){
	img_id = ajax_array[ajax_id].responseText;
	
	
	if (document.getElementById(img_id)) {
		img = document.getElementById(img_id);
		img.src = img.src + "&test=1";
	}
}



var file_upload_elements = new Array();
var file_ids = 0;

function quickset(element){
	elements = element.elements
	
	var params = "";
	
	for (var i = 0; i < elements.length; i++) {
		if (elements[i].name && elements[i].value) {
			var value = elements[i].value;
			value = str_replace('&amp;','@amp;', value);
			value = str_replace('&','@amp;', value);
			params += elements[i].name + "=" + value + "&";
		}
	}
	send_ajax(params, '/ajax/quickset.php', 'POST', true, 'quickset_callback', element);
		
	element.style.backgroundImage = "url('/images/blue.gif')";
	element.style.backgroundRepeat = "no-repeat";
	element.style.backgroundPosition = "bottom right"
	element.style.border = "2px solid #CCE0EB";
	
	return false
}


function quickset_callback(ajax_id, element){
	//element passed through automatically
	element = element_controller[ajax_id];

	var doc = ajax_array[ajax_id].responseXML;
	
	if (doc == null) {
		alert("There has been an error getting suggestions.");
		alert(ajax_array[ajax_id].responseText);
	}
	else {
		//get XML values		
		outcome = doc.getElementsByTagName('outcome')[0].firstChild.nodeValue;
		details = doc.getElementsByTagName('details')[0].firstChild.nodeValue;
		target_div = doc.getElementsByTagName('target_div')[0].firstChild.nodeValue;
		target_content = doc.getElementsByTagName('target_content')[0].firstChild.nodeValue;
		
		target_content = str_replace("\n", "<br />", target_content);
		
		if (outcome == "success") {
			element.style.backgroundImage = "url('/images/green.gif')";
			element.style.border = "3px solid #ACDEC2";
			if (target_content == "false") {
				target_content = "";
			}
			document.getElementById(target_div).innerHTML = target_content;
			
		}
		else {
			alert("error:\n" + responce)
			element.style.backgroundImage = "url('/images/red.gif')";
			element.style.border = "3px solid #F0989A";
			element.style.backgroundColor = "#F0989A";
		}
	}
}

function quickset_file(element){
	//add the element to the element handler
	file_id = file_ids;
	file_ids++;
	file_upload_elements[file_id] = element;
	
	//assign the form to the element handler
	element.file_id.value = file_id;
	
	element.style.backgroundImage = "url('/images/blue.gif')";
	element.style.backgroundRepeat = "no-repeat";
	element.style.backgroundPosition = "right"
	
	return true;
}

function quickset_file_callback(responce, file_id){
	//get the element form the element handler
	element = file_upload_elements[file_id];
	
	if(responce.indexOf("success") != -1){
		element.style.backgroundImage = "url('/images/green.gif')";
	}
	else{
		alert("error:\n" + responce)
		element.style.backgroundImage = "url('/images/red.gif')";
	}
}



function str_replace(search, replace, string){
	var idx = string.indexOf(search);
	
	while (idx > -1) {
		string = string.replace(search, replace);
		idx = string.indexOf(search);
	}
	
	return string;
}

function check_order_items(){
	send_ajax('', '/check_cart.php', 'POST', true, 'check_order_items_response');	
}
function check_order_items_response(ajax_id){
	var response = ajax_array[ajax_id].responseText;
}
/*used for the reviw ajax*/
function get_full_review(id, return_id, type){	
	var params = "id=" + id + "&return=" + return_id + '&type=' + type + '&';	
	send_ajax(params, '/ajax/review.php', 'POST', true, 'display_review');	
	return false
}

function display_review(ajax_id){
	//element passed through automatically
	var doc = ajax_array[ajax_id].responseXML;
	
	if (doc == null) {
		alert("There has been an error getting suggestions.");
		alert(ajax_array[ajax_id].responseText);
	}
	else {
		//get XML values		
		outcome = doc.getElementsByTagName('outcome')[0].firstChild.nodeValue;
		target_div = doc.getElementsByTagName('target_div')[0].firstChild.nodeValue;
		target_content = doc.getElementsByTagName('target_content')[0].firstChild.nodeValue;
		target_content = str_replace("\\n", "<br />", target_content);
		
		if (outcome == "success") {
			if (target_content == "false") {
				target_content = "";
			}
			document.getElementById(target_div).innerHTML = target_content;
			
		}
		else {
			alert("error:\n" + responce);
		}
	}
}

function get_more_reviews(pid, return_id, limit){
	if(displayed_reviews == 0)
	{
		displayed_reviews = '3';
	}
		
	var params = "pid=" + pid + "&return=" + return_id + '&type=more&limit=' + limit + '&current=' + displayed_reviews;	
	send_ajax(params, '/ajax/review.php', 'POST', true, 'display_more_reviews');	
	return false	
}

function display_more_reviews(ajax_id){
	//element passed through automatically
	var doc = ajax_array[ajax_id].responseXML;

	if (doc == null) {
		alert("There has been an error getting suggestions.");
		alert(ajax_array[ajax_id].responseText);
	}
	else {
		//get XML values		
		outcome = doc.getElementsByTagName('outcome')[0].firstChild.nodeValue;
		target_div = doc.getElementsByTagName('target_div')[0].firstChild.nodeValue;
		results = doc.getElementsByTagName('results');
		var div_contnet = document.getElementById(target_div).innerHTML;
		var star_img = '';
		
		for (i = 0; i < results.length; i++) {
			id = results[i].getElementsByTagName('id')[0].firstChild.nodeValue;
			stars = results[i].getElementsByTagName('stars')[0].firstChild.nodeValue;
			title = results[i].getElementsByTagName('title')[0].firstChild.nodeValue;
			date = results[i].getElementsByTagName('date')[0].firstChild.nodeValue;
			name = results[i].getElementsByTagName('name')[0].firstChild.nodeValue;
			review = results[i].getElementsByTagName('review')[0].firstChild.nodeValue;	
			
			for($i = 0; $i < Math.round(stars); $i++)
			{
				star_img += '<img src="/images/star.gif" alt="'+stars+' star" />';
			}
			if(review.length>200){
				review = substr(review,0,197)+'...';
				review += '<br /><a href="#" onclick="get_full_review(\''+id+'\', \'review_content_'+id+'\', \'full\'); return false;">See full review</a>';
			}
			review = str_replace("\\n", "<br />", review);
			
			div_contnet += '<hr style="width:80%;" /><p>';
			div_contnet += star_img+' <span style="font-weight:bold;">'+title+'</span> on '+date;
			div_contnet += '<br />review by <span style="font-style:italic;">'+name+'</span></p>';
			div_contnet += '<p><span id="review_content_'+id+'">'+review+'</span></p>';			
		}
		
		if (outcome == "success") {
			document.getElementById(target_div).innerHTML = div_contnet;			
		}
		else {
			alert("error:\n" + responce)
		}
	}
}
