var siteHost = window.location.hostname
if(siteHost=='lsnetbook' || siteHost=='devbox.pg54.com')siteHost=siteHost+'/cav'
siteHost = 'http://'+siteHost+'/'
var itemSKUCode
var itemQTY
var updateTimerID


function GetXmlHttpObject(){
	var objXMLHttp=null
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest()
	}else if (window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

function nowTime(){
	var date = new Date();
	var nowTime = date.getTime();
	return nowTime
}

function updateBasketValues(newQTY,newCode){
	clearTimeout(updateTimerID)
	itemSKUCode = newCode
	itemQTY = newQTY
	if(itemQTY!='')updateTimerID = setTimeout('refreshBasket()',500)
}

function refreshBasket(){
	document.getElementById('BasketUpdater').style.display='block'
	var url=siteHost+"ecom/ajax_basket_logic.php?update="+itemSKUCode+"&qty="+itemQTY+"&time="+nowTime()
	xmlHttp=GetXmlHttpObject()
	xmlHttp.onreadystatechange=displayNewBasket
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function displayNewBasket(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		document.getElementById('BasketWrapper').innerHTML=xmlHttp.responseText
		document.getElementById('BasketUpdater').style.display='none'
	}
}

function validate() {

	if (document.customer.firstname.value=="" || document.customer.lastname.value=="") {
		alert("Please Enter Your Full Name");
		return false;
	}
	if (document.customer.addr1.value=="" || document.customer.town.value=="" || document.customer.post.value=="") {
		alert("Missing Address Information!");
		return false;
	}
	if (document.customer.country.value=="") {
		alert("Country?");
		return false;
	}
	if (document.customer.phone.value=="") {
		alert("Missing Telephone Number!");
		return false;
	}

	var tstring = "";
	string = '' + document.customer.phone.value;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];

	if (isNaN(tstring)) {
		alert('Telephone must be numbers only.');
		document.customer.phone.focus();
		document.customer.phone.select();
		return false;
	}


	var strg = tstring;
	var first_two_digits = strg.substring(0,2);
	if (first_two_digits=="07") {
		alert("Please enter the Card Holder's Landline Phone Number. No mobile numbers allowed in this field, Sorry!");
		document.customer.phone.focus();
		document.customer.phone.select();
		return false;
	}

	if (document.customer.email.value=="") {
		alert("e-mail Address?");
		return false;
	}



	return emailValidator();
}

function emailValidator()
{

	if (document.customer.email.value == "")
	{
		alert("Please enter your EMAIL Address.");
		return (false);
	}

	if (!isEmailAddr(document.customer.email.value))
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		return (false);
	}

	if (document.customer.email.value.length < 3)
	{
		alert("Please enter at least 3 characters in the \"email\" field.");
		return (false);
	}
}
function isEmailAddr(email)
{
	var result = false
	var theStr = new String(email)
	var index = theStr.indexOf("@");
	if (index > 0)
	{
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
		result = true;
	}
	return result;
}


function checkOrderValue(){
	var currentOrderValue = parseInt(document.getElementById('exVatOrderTotal').innerHTML)
	if(currentOrderValue<35){
		alert("Sorry... We have a minimum order value of \xA335.00 (excluding VAT)")
		return false
	}
}

function updateCountrySession(newCountry){
	document.getElementById('BasketUpdater').style.display='block'
	var url=siteHost+"ecom/setCountrySession.php?country="+newCountry+"&time="+nowTime()
	xmlHttp=GetXmlHttpObject()
	xmlHttp.onreadystatechange=displayNewBasket
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


