var totalbottles = 0;
var totaldollars = 0;
var totalmales = 0;
var totalfemales = 0;
var gclub = 0;
var tax = 0;
var grat = 0;
var service = 0;
var limo = 0;
var limoset = 0;
var limoadded = 0;
var malecost = 0;
var femalecost = 0;

function getHTTPObject()
{
	var xmlhttp;
	/*@cc_on
	@if(@_jscript_version >=5)
	try
	{
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E)
		{
			xmlhttp=false;
		}
	}
	@else
		xmlhttp=false;
	@end @*/
	if(!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)
		{
			xmlhttp=false;
		}
	}
	return xmlhttp;
}
function handlePhotoResponse()
{
	if(http.readyState == 4)
	{
		//alert(http.responseText);
		document.getElementById('currentimage').innerHTML=http.responseText;	
	}
}
function showPhoto(id) 
{
	var url = 'showphoto.php?id='+id;
	//alert(url);
	http.open("GET",url, true);
	http.onreadystatechange = handlePhotoResponse;
	http.send(null);
}
function handlePDFResponse()
{
   if(http.readyState == 4)
   {
   // alert(http.responseText);
      document.getElementById('pdfmsg').innerHTML=http.responseText;
   }
}
function getPDF(id)
{
   var url = 'genpdf.php?item=' + id;
   //alert(url);
   document.getElementById('pdfmsg').innerHTML='<div class="tabletext" style="width: 400px; margin-bottom: 10px;"><img src="images/msc_loading.gif" /> Your PDF is being created, please wait...</div>';
   http.open("GET",url, true);
   http.onreadystatechange = handlePDFResponse;
   http.send(null);
}
function openForm()
{
	document.getElementById('nlet').style.display='';
}
function closeForm()
{
	document.getElementById('nlet').style.display='none';
}

function showNLResponse()
{
	if(http.readyState == 4)
	{
		//alert(http.responseText);
		var clink = '<br/>Thank you for requesting a Thermosense product guide.<br /><br />You should receive a copy ';
		clink += ' by post in the next few days.';
		clink += '<br /><br />&nbsp;&nbsp;<a style="text-decoration:none;" href="javascript:void(0)" onclick="closeForm()">Close</a>';
		document.getElementById('nlet').innerHTML = clink;
	}
}

function subscribeNL()
{
	var message = '';
	if(!document.getElementById("nlname").value)
	{
		message += '* Name is required.'+"\n";
	}
	if(!document.getElementById("company").value)
	{
		message += '* Company is required.'+"\n";
	}
	if(!document.getElementById("addr1").value)
	{
		message += '* Address 1 is required.'+"\n";
	}
	if(!document.getElementById("town").value)
	{
		message += '* Town is required.'+"\n";
	}
	if(!document.getElementById("postcode").value)
	{
		message += '* Postcode is required.'+"\n";
	}
	if(!document.getElementById("nltel").value)
	{
		message += '* Telephone Phone is required.'+"\n";
	}
	if(!document.getElementById("email").value)
	{
		message += '* Email Address is required.'+"\n";
	}
	if(message)
	{
		alert(message);
		return false;
	}
	var poststr = "name=" + encodeURI(document.getElementById("nlname").value) +
			"&position=" + encodeURI(document.getElementById("position").value) +
			"&company=" + encodeURI(document.getElementById("company").value) +
			"&addr1=" + encodeURI(document.getElementById("addr1").value) +
			"&addr2=" + encodeURI(document.getElementById("addr2").value) +
			"&town=" + encodeURI(document.getElementById("town").value) +
			"&county=" + encodeURI(document.getElementById("county").value) +
			"&postcode=" + encodeURI(document.getElementById("postcode").value) +
			"&tel=" + encodeURI(document.getElementById("nltel").value) +
			"&email=" + encodeURI(document.getElementById("email").value);
	var url = 'http://www.thermosense.co.uk/addinfo.php';
	document.getElementById('nlet').innerHTML='<div style="margin: 10px;"><img src="/images/msc_loading.gif" />Saving details. Please wait...</div>';
	http.open("POST",url, true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", poststr.length);
	http.setRequestHeader("Connection", "close");
	http.onreadystatechange = showNLResponse;
	http.send(poststr);
}
function showSubResponse()
{
   if(http.readyState == 4)
   {
   //alert(http.responseText);
      document.getElementById('subscribe').innerHTML=http.responseText;
   }
}
function subscribe()
{
	var sname = document.getElementById('sname').value;
	var semail = document.getElementById('semail').value;
	//var scomments = document.getElementById('scomments').value;
	var url = 'http://www.thermosense.co.uk/addsub.php?name=' + sname + '&email=' + semail ;
	//alert(url);
	document.getElementById('subscribe').innerHTML='<div style="margin-bottom: 10px;"><img src="/images/msc_loading.gif" /> Your Subscription is being registered, please wait...</div>';
	http.open("GET",url, true);
	http.onreadystatechange = showSubResponse;
	http.send(null);
}
function handleRatingResults()
{
        if(http.readyState == 4)
        {
//alert(http.responseText);
                //eval(http.responseText);
                document.getElementById('ratresult').innerHTML = 'Thankyou for your review';
        }
}
function updateRating(post,user,rating,question)
{
        if(post == 0) return false;
        var ratpct = rating *20;
        ratstr = ratpct + '%';
        //alert(arguments.length);
        if(arguments.length == 4)
        {
                if(question == 1) { ratq1 = rating; document.getElementById('rat1').style.width = ratstr; return false; }
                if(question == 2) { ratq2 = rating; document.getElementById('rat2').style.width = ratstr; return false; }
                if(question == 3) { ratq3 = rating; document.getElementById('rat3').style.width = ratstr; return false; }
                if(question == 4) { ratq4 = rating; document.getElementById('rat4').style.width = ratstr; return false; }
        }
		var cm = document.getElementById('review').value;
		var rcm = document.getElementById('recommend').value;
		var nme = document.getElementById('revname').value;
		var frm = document.getElementById('revfrom').value;
		var eml = document.getElementById('revemail').value;
		var url="/updateratings.php?user=" + user + "&post=" + post + "&r1=" + ratq1 + "&r2=" + ratq2 + "&r3=" + ratq3 + "&r4=" + ratq4 + "&rcm=" + rcm + "&c=" + cm + "&nme=" + nme + "&frm=" + frm + "&eml=" + eml;
//alert(url);
		http.open("GET",url, true);
		http.onreadystatechange=handleRatingResults;
		http.send(null);
}
function updateFoodRating(post,user,rating,question)
{
        if(post == 0) return false;
        var ratpct = rating *20;
        ratstr = ratpct + '%';
        //alert(arguments.length);
        if(arguments.length == 4)
        {
                if(question == 1) { ratq1 = rating; document.getElementById('rat1').style.width = ratstr; return false; }
                if(question == 2) { ratq2 = rating; document.getElementById('rat2').style.width = ratstr; return false; }
                if(question == 3) { ratq3 = rating; document.getElementById('rat3').style.width = ratstr; return false; }
                if(question == 4) { ratq4 = rating; document.getElementById('rat4').style.width = ratstr; return false; }
        }
		var cm = document.getElementById('review').value;
		var rcm = document.getElementById('recommend').value;
		var nme = document.getElementById('revname').value;
		var frm = document.getElementById('revfrom').value;
		var eml = document.getElementById('revemail').value;
		var url="/updatefoodratings.php?user=" + user + "&post=" + post + "&r1=" + ratq1 + "&r2=" + ratq2 + "&r3=" + ratq3 + "&r4=" + ratq4 + "&rcm=" + rcm + "&c=" + cm + "&nme=" + nme + "&frm=" + frm + "&eml=" + eml;
//alert(url);
		http.open("GET",url, true);
		http.onreadystatechange=handleRatingResults;
		http.send(null);
}


function revert()
{
return;
		document.getElementById('adminoverlay').style.display='none';	
		document.getElementById('admindiv').style.display='';	
}
function handleTextResponse()
{
	if(http.readyState == 4)
	{
//		alert(http.responseText);
		document.getElementById('adminoverlay').innerHTML=http.responseText;	
		document.getElementById('adminoverlay').style.display='';	
		document.getElementById('admindiv').style.display='none';	
	}
}
function handlePageResponse()
{
	if(http.readyState == 4)
	{
//		alert(http.responseText);
		document.getElementById('admindiv').innerHTML=http.responseText;	
		document.getElementById('admindiv').style.display='';	
		document.getElementById('adminoverlay').style.display='none';	
	}
}
function shTxt(textid)  
{
return;
	var url = 'getadmintext.php?id=' + textid;
	//alert(url);
	http.open("GET",url, true);
	http.onreadystatechange = handleTextResponse;
	http.send(null);
}
function loadPage(selelm)  
{
	var id = selelm.options[selelm.selectedIndex].value;
	var url = 'pageform.php?page=' + id;
	document.location.replace(url);
}
function zzloadItem(selelm)  
{
	var id = selelm.options[selelm.selectedIndex].value;
	var url = 'adminform.php?form=2&item=' + id;
	//alert(url);
	http.open("GET",url, true);
	http.onreadystatechange = handlePageResponse;
	http.send(null);
}
function loadClient(selelm)  
{
	var id = selelm.options[selelm.selectedIndex].value;
	var url = 'clientform.php?client=' + id;
	document.location.replace(url);
}
function loadItem(selelm)
{
	var id = selelm.options[selelm.selectedIndex].value;
	document.location.replace('uploader/ubr_file_upload.php?item='+id);
}
function delItem(selelm)
{
	var id = selelm.options[selelm.selectedIndex].value;
	document.location.replace('index.php?del='+id);
}

function delCat(cat)
{
	if(confirm('Are you sure you want to delete this category?'))
		document.location.replace('prodcats.php?del='+cat);
}

//
// AJAX/JAVASCRIPT FUNCTIONS WRITTEN BY JUSTIN BELOW
//

// function validate_db_field (field_in, placeholder_in, php_in) {
// AJAX function to validate form field against database.
//   field_in = form field to be checked (eg. html_page_name).
//   placeholder_in = placeholder on form that will show result of this routine.
//   php_in = php script to run (to do database check and return result etc.).
function validate_db_field (field_in, placeholder_in, php_in) {
	if (field_in.length==0) { 
		// Field to be validated is empty. Show error.
		document.getElementById(placeholder_in).innerHTML="<div><img style=\"margin-right: 5px; vertical-align: middle;\" src=\"/images/msc_icon_error.gif\"><span class=\"red\">Sort it out muppet boy. I can't validate an empty field!</span></div>";
		return;
	}
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else {
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			// show result of php program in placeholder.
			document.getElementById(placeholder_in).innerHTML=xmlhttp.responseText;
		}
	}
	// call php program, passing field to be checked as a parameter.
	// alert("Debug :: field_in = " + field_in);
	xmlhttp.open("GET",php_in + "?field_in=" + field_in, true);
	xmlhttp.send();
}

// function ajax_update(table, field, field_value, key, key_value) {
// AJAX function to update database with supplied parameters.
//   table = database table to update (e.g. update <table>...)
//   field = database table field to update (e.g. update <table> set <field> = ...).
//   field_value = value to update database table field with (e.g. update <table> set <field> = <field_value>...).
//   key = database table key field (e.g. update <table> set <field> = <field_value> where <key> = ...).
//   key_value = key value for database table update (e.g. update <table> set <field> = <field_value> where <key> = <key_value>...).
function ajax_update(table, field, field_value, key, key_value) {
	// if field_value is a boolean (e.g. checkbox result) set to 1 or 0 accordingly.
	if (field_value == true) {
		field_value = 1; // e.g. checkbox = checked
	} else {
		field_value = 0;
	}

	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else {
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	// pass parameters to php program for database update.
	xmlhttp.open("GET","update_subscribers.php" + "?table=" + table + "&field=" + field + "&field_value=" + field_value + "&key=" + key + "&key_value=" + key_value, true);
	xmlhttp.send();
}
