// JavaScript Document
function ajaxFunction(getVals, targetId)
{
	//alert("in");
	var browserType;
	var xmlHttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4) 
		{	
			if(targetId == "checkout")			
			{
				//alert("test\\" +  xmlHttp.responseText);
				return true;
			}
			else
				document.getElementById(targetId).innerHTML = xmlHttp.responseText;	
		}
	}

	xmlHttp.open("GET", "functions/ajaxWork.php?"+getVals, true);
	xmlHttp.send(null);
}



function checkout(orderId)
{
	document.getElementById('processing').innerHTML = "processing order...";
	ajaxFunction('orderID='+orderId, 'checkout');
	setTimeout("document.paypalForm.submit()",500);
}

function emailOrder()
{
	document.getElementById('emailOrderForm').style.display = "block";
}

function addToCart(id)
{	
	qty = document.getElementById(id).value;	
	if(qty != "" && qty != null && IsNumeric(qty))
	{
		if(validateQty(id))
			ajaxFunction("qty="+qty+"&id="+id, 'confirm_add');
		else
			return;
	}
	else
		alert("Please enter a valid quantity!");
	
	//setTimeout("clearConfirm()",5000);
}



function validateQty(id)
{
	var qty = document.getElementById(id).value;
	var minQty = eval("document.getElementById('minQty_"+id+"').value;");
	if(qty == "")
	{
		document.getElementById(id).value = 0;
		return true;
	}
	//alert("sdf: "+minQty);
	if(qty%minQty != 0)	
	{
		document.getElementById(id).value = "";
		document.getElementById(id).focus();
		alert("Quantities need to be multiples of "+minQty);
		return;
	}
	return true;
}



function clearConfirm() {document.getElementById('confirm_add').innerHTML = "";}







function updateCart(id)
{		
	var qty = document.getElementById(id).value;		

	if(qty == null || qty == "")
		qty = 0;
		
	if(IsNumeric(qty))
	{				
		ajaxFunction("qty="+qty+"&id="+id, "none");		
	}
	else
	{
		alert("Please enter a valid quantity!");
		document.getElementById(id).value = "";
	}
}





/*function saveFreightRate(obj)

{

	var rate = obj.options[obj.selectedIndex].value;

	var loc = obj.options[obj.selectedIndex].innerHTML;	

	var oldRate = document.getElementById('fr').value;

	if(rate != "none")

	{

		rate = parseFloat(rate);

		document.getElementById('showFreightRate').innerHTML = "$"+rate.toFixed(2);

		document.getElementById('fr').value = rate;

		document.getElementById('showFreightLoc').innerHTML = loc;

		document.getElementById('frl').value = loc; 

	}

	else

	{

		document.getElementById('showFreightRate').innerHTML = "";

		document.getElementById('showFreightLoc').innerHTML = "";

		document.getElementById('frl').value = "";

		document.getElementById('fr').value = "";

	}

	if(!IsNumeric(rate))

		rate = 0;

	if(!IsNumeric(oldRate))

		oldRate = 0;

	var subTotal = document.getElementById('subTotal').value;

	//alert("before: "+subTotal+" - "+oldRate+" + "+rate);

	subTotal = subTotal - oldRate + rate;

	//alert(" = "+subTotal);

	var GST = subTotal * 0.1;

	subTotal = parseFloat(subTotal);

	var total = subTotal + GST;

	GST = parseFloat(GST);

	//alert(GST);

	//alert("a: "+subTotal+" "+document.getElementById('subTotalText').innerHTML);

	document.getElementById('GST').innerHTML = "$"+GST.toFixed(2);

	document.getElementById('subTotal').value = subTotal.toFixed(2);

	document.getElementById('subTotalText').innerHTML = "$"+subTotal.toFixed(2);

	document.getElementById('totalText').innerHTML = "$"+total.toFixed(2);



	ajaxFunction("freight="+rate+"&location="+loc, "none");



}*/

function saveFreightRate(obj)

{

	var rate = obj.options[obj.selectedIndex].value;

	var loc = obj.options[obj.selectedIndex].innerHTML;

	if(rate != "none")

	{

		ajaxFunction("freight="+rate+"&loc="+loc, "showFreightRate");	

	}

	else

	{

		document.getElementById('showFreightRate').innerHTML = "";

		document.getElementById('showFreightLoc').innerHTML = "";

	}

	if(!IsNumeric(rate))

		rate = 0;

	//ajaxFunction("freight="+rate+"&location="+loc, "none");	

	setTimeout("window.location = 'cart.php'", 60);

}







function IsNumeric(sText)

{	

	var ValidChars = "0123456789.";

	var IsNumber=true;

	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) 

	{ 

		Char = sText.charAt(i); 

		if (ValidChars.indexOf(Char) == -1) 

		{

			IsNumber = false;

		}

	}

	return IsNumber;

}





function validateCart(type)
{
	if(document.getElementById('postcode').value == "" )
	{
		alert("Please enter your postcode!");
		return false;
	}
	var text;
	for(i=0; i<document.cartForm.elements.length; i++)
	{
		text = document.cartForm.elements[i].name;		
		if(text.match("itemQty") == "itemQty")
		{			
				if(!validateQty(text.substr(8, text.length)))
				return false;		
		}
	}
		alert(ocument.getElementById('postcode').value); 
	if(type == "out" && (document.getElementById('postcode').value == "" ))
	{
		alert("Please seletct your freight location!")
		return false;
	}
	return true;
}



function postcodeEntered(obj)

	{

		if(isNum(obj.value))

		{

			//if(obj.value.length == 4)

			ajaxFunction("postcode="+obj.value, 'none');

			return true;

		}

		

		alert("Please enter a valid number!");

		obj.value = "";

		obj.focus();

		

	}

	

	function isNum(sText)

	{

		var ValidChars = "0123456789.";

		var IsNumber=true;

		var Char;

		for (i = 0; i < sText.length && IsNumber == true; i++) 

		{ 

			Char = sText.charAt(i); 

			if (ValidChars.indexOf(Char) == -1) 

			{

				IsNumber = false;

			}

		}

		return IsNumber;

	}