//Ajax Functions

function LoadBasket()
{ 

	xmlHttp_100=GetXmlHttpObject();
	if (xmlHttp_100==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	 }

	var url="ajax/loadbasket.php";
	url=url+"?sid="+Math.random();
	xmlHttp_100.onreadystatechange = function() {
		
		if ((xmlHttp_100.readyState==1) || (xmlHttp_100.readyState==2) || (xmlHttp_100.readyState==3)) {
			document.getElementById("basket").innerHTML="Loading Basket...";
		}
		
		if (xmlHttp_100.readyState==4) {
			document.getElementById("basket").innerHTML=xmlHttp_100.responseText;
		}
	
	}
	xmlHttp_100.open("GET",url,true);
	xmlHttp_100.send(null);

}

function LoadSignIn()
{ 

	if (document.getElementById("signinbox").style.display=="block") {
		
		document.getElementById("signinbox").style.display="none";
		
	} else {

		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null) {
		  alert ("Your browser does not support AJAX!");
		  return;
		 }
	
		var url="ajax/loadsignin.php";
		url=url+"?sid="+Math.random();
		xmlHttp.onreadystatechange = function() {
			
			if ((xmlHttp.readyState==1) || (xmlHttp.readyState==2) || (xmlHttp.readyState==3)) {
				document.getElementById("signinbox").style.display="block";
				document.getElementById("signinbox").innerHTML="Loading Sign In...";
			}
			
			if (xmlHttp.readyState==4) {
				document.getElementById("signinbox").style.display="block";
				document.getElementById("signinbox").innerHTML=xmlHttp.responseText;
			}
		
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
		
	}
	
}

function SelectColor(prodid)
{ 

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	 }

	

	var url="ajax/loadcolors.php";
	url=url+"?sid="+Math.random();
	url=url+"&prodid="+prodid;
	xmlHttp.onreadystatechange = function() {
		
		if ((xmlHttp.readyState==1) || (xmlHttp.readyState==2) || (xmlHttp.readyState==3)) {
			document.getElementById("colorbox").style.display="block";
			document.getElementById("colorbox").innerHTML="Loading Colors...";
		}
		
		if (xmlHttp.readyState==4) {
			document.getElementById("sizebox").style.display="none";
			document.getElementById("colorbox").style.display="block";
			document.getElementById("colorbox").innerHTML=xmlHttp.responseText;
		}
	
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function SelectSize(prodid)
{ 

	if (document.getElementById("colorid").value=="") {
		alert('Choose Color First');
		return false;
	}

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	 }

	var clid = document.getElementById("colorid").value;

	var url="ajax/loadsizes.php";
	url=url+"?sid="+Math.random();
	url=url+"&prodid="+prodid;
	url=url+"&clid="+clid;
	xmlHttp.onreadystatechange = function() {
		
		if ((xmlHttp.readyState==1) || (xmlHttp.readyState==2) || (xmlHttp.readyState==3)) {
			document.getElementById("sizebox").style.display="block";
			document.getElementById("sizebox").innerHTML="Loading Sizes...";
		}
		
		if (xmlHttp.readyState==4) {
			document.getElementById("colorbox").style.display="none";
			document.getElementById("sizebox").style.display="block";
			document.getElementById("sizebox").innerHTML=xmlHttp.responseText;
		}
	
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function LoadPhotos(prodid,prodimage) {

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	}

	var url="ajax/loadphotos.php";
	url=url+"?sid="+Math.random();
	if (prodid) { url=url+"&prodid="+prodid; }
	if (prodimage) { url=url+"&prodimage="+prodimage; }
	
	xmlHttp.onreadystatechange = function() {
		
		if ((xmlHttp.readyState==1) || (xmlHttp.readyState==2) || (xmlHttp.readyState==3)) {
			document.getElementById("black_overlay").style.display="block";
			document.getElementById("lightbox").style.display="block";
			document.getElementById("lightbox_body").innerHTML="Loading, please wait...";
		}
		
		if (xmlHttp.readyState==4) {
			document.getElementById("black_overlay").style.display="block";
			document.getElementById("lightbox").style.display="block";
			document.getElementById("lightbox_body").innerHTML=xmlHttp.responseText;
		}
	
	}
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function AddToBasket() {

	if (document.getElementById("colorid").value=="") {
		alert ('Select Color');
		return false;
	} else {
		var colorid = document.getElementById("colorid").value;	
	}
	
	if (document.getElementById("sizeid")) {
		if (document.getElementById("sizeid").value=="") {
			alert ('Select Size');
			return false;
		} else {
			var sizeid = document.getElementById("sizeid").value;
		}
	}
	
	if (document.getElementById("qty").value=="") {
		alert ('Select Quantity');
		return false;
	} else {
		var qty = document.getElementById("qty").value;	
	}
	
	if (document.getElementById("stockid").value=="") {
		alert ('Select Color and Size');
		return false;
	} else {
		var stockid = document.getElementById("stockid").value;	
	}

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	}

	var url="ajax/addtobasket.php";
	url=url+"?sid="+Math.random();
	if (stockid) { url=url+"&stockid="+stockid; }
	if (qty) { url=url+"&qty="+qty; }
	
	xmlHttp.onreadystatechange = function() {
		
		if ((xmlHttp.readyState==1) || (xmlHttp.readyState==2) || (xmlHttp.readyState==3)) {
			document.getElementById("black_overlay").style.display="block";
			document.getElementById("addtobasketbox").style.display="block";
			document.getElementById("addtobasketbox").innerHTML="Loading, please wait...";
		}
		
		if (xmlHttp.readyState==4) {
			document.getElementById("black_overlay").style.display="block";
			document.getElementById("addtobasketbox").style.display="block";
			document.getElementById("addtobasketbox").innerHTML=xmlHttp.responseText;
			LoadBasket();
			//setTimeout("CloseAddToBasket()",6000);
			
			
		}
	
	}
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function CloseAddToBasket() {
	document.getElementById("black_overlay").style.display="none";
	document.getElementById("addtobasketbox").style.display="none";
}


function LoadFlashPreview(prod_id,prod_image,screentype) {

	xmlHttp2=GetXmlHttpObject();
	if (xmlHttp2==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	}

	var url="ajax/loadflashpreview.php";
	url=url+"?sid="+Math.random();
	if (prod_id) { url=url+"&prod_id="+prod_id; }
	if (prod_image) { url=url+"&prod_image="+prod_image; }
	if (screentype) { url=url+"&screentype="+screentype; }
	
	xmlHttp2.onreadystatechange = function() {
		
		if (screentype=="normal") {
		
			if ((xmlHttp2.readyState==1) || (xmlHttp2.readyState==2) || (xmlHttp2.readyState==3)) {
				document.getElementById("loadedflashbox").innerHTML="<div style='text-align:center; padding-top:50px; text-weight:bold; '>Loading, please wait...</div>";
			}
			
			if (xmlHttp2.readyState==4) {
				document.getElementById("loadedflashbox").innerHTML=xmlHttp2.responseText;
			}
		
		} else if (screentype=="fullscreen") {
			
			if ((xmlHttp2.readyState==1) || (xmlHttp2.readyState==2) || (xmlHttp2.readyState==3)) {
				document.getElementById("black_overlay").style.display="block";
				document.getElementById("lightbox").style.display="block";
				document.getElementById("lightbox_body").innerHTML="Loading, please wait...";
			}
			
			if (xmlHttp2.readyState==4) {
				document.getElementById("black_overlay").style.display="block";
				document.getElementById("lightbox").style.display="block";
				document.getElementById("lightbox_body").innerHTML=xmlHttp2.responseText;
			}
			
		}
	
	}
	
	xmlHttp2.open("GET",url,true);
	xmlHttp2.send(null);

}

function LoadAddresses(locationtype) {
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	}

	var url="ajax/loadaddresses.php";
	url=url+"?sid="+Math.random();
	if (locationtype) { url=url+"&locationtype="+locationtype; }
	
	xmlHttp.onreadystatechange = function() {
		
		if ((xmlHttp.readyState==1) || (xmlHttp.readyState==2) || (xmlHttp.readyState==3)) {
			document.getElementById("addressesbox").style.display="block";
			document.getElementById("addressesbox").innerHTML="Loading, please wait...";
		}
		
		if (xmlHttp.readyState==4) {
			document.getElementById("addressesbox").style.display="block";
			document.getElementById("addressesbox").innerHTML=xmlHttp.responseText;
		}
	
	}
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function AddEditAddress(type,contactid,addrid,count_addr,locationtype) {
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	}

	var url="ajax/addeditaddress.php";
	url=url+"?sid="+Math.random();
	if (type) { url=url+"&type="+type; }
	if (contactid) { url=url+"&contactid="+contactid; }
	if (addrid) { url=url+"&addrid="+addrid; }
	if (count_addr) { url=url+"&count_addr="+count_addr; }
	if (locationtype) { url=url+"&locationtype="+locationtype; }
	
	xmlHttp.onreadystatechange = function() {
		
		if ((xmlHttp.readyState==1) || (xmlHttp.readyState==2) || (xmlHttp.readyState==3)) {
			document.getElementById("black_overlay").style.display="block";
			document.getElementById("addtobasketbox").style.display="block";
			document.getElementById("addtobasketbox").innerHTML="Loading, please wait...";
		}
		
		if (xmlHttp.readyState==4) {
			document.getElementById("black_overlay").style.display="block";
			document.getElementById("addtobasketbox").style.display="block";
			document.getElementById("addtobasketbox").innerHTML=xmlHttp.responseText;
		}
	
	}
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

	
}

function SaveAddEditAddress() {
	
	if (document.getElementById("type")) {
		var type = document.getElementById("type").value;
	}
	if (document.getElementById("addrid")) {
		var addrid = document.getElementById("addrid").value;
	}
	if (document.getElementById("locationtype")) {
		var locationtype = document.getElementById("locationtype").value;
	}
	
	var namesurname = document.getElementById("namesurname").value;
	if (namesurname=="") {
		alert("Please fill in your Name Surname");
		return false;
	}

	var company = document.getElementById("company").value;
	
	var address1 = document.getElementById("address1").value;
	if (address1=="") {
		alert("Please fill in Address Line 1");
		return false;
	}
	
	var address2 = document.getElementById("address2").value;
	
	var city = document.getElementById("city").value;
	if (city=="") {
		alert("Please fill in City");
		return false;
	}
	
	var county = document.getElementById("county").value;

	var postcode = document.getElementById("postcode").value;
	if (postcode=="") {
		alert("Please fill in Postcode");
		return false;
	}
	
	var pagesObj = document.getElementById("country"); 
	var country = pagesObj.options[pagesObj.selectedIndex].value;
	
	if (country=="") {
		alert("Please select Country");
		return false;
	}
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	}
	/*
	if (document.getElementById("defbilling").checked) {
		var defbilling = 1;
	} else {
		var defbilling = 0;
	}
	*/
	
	var url="ajax/saveaddeditaddress.php";
	url=url+"?sid="+Math.random();
	if (type) { url=url+"&type="+type; }
	if (addrid) { url=url+"&addrid="+addrid; }
	if (namesurname) { url=url+"&namesurname="+namesurname; }
	if (company) { url=url+"&company="+company; }
	if (address1) { url=url+"&address1="+address1; }
	if (address2) { url=url+"&address2="+address2; }
	if (city) { url=url+"&city="+city; }
	if (county) { url=url+"&county="+county; }
	if (postcode) { url=url+"&postcode="+postcode; }
	if (country) { url=url+"&country="+country; }
	//if (defbilling) { url=url+"&defbilling="+defbilling; }

	xmlHttp.onreadystatechange = function() {
		
		if ((xmlHttp.readyState==1) || (xmlHttp.readyState==2) || (xmlHttp.readyState==3)) {
			document.getElementById("black_overlay").style.display="block";
			document.getElementById("addtobasketbox").style.display="block";
			document.getElementById("addtobasketbox").innerHTML="Loading, please wait...";
		}
		
		if (xmlHttp.readyState==4) {
			document.getElementById("black_overlay").style.display="block";
			document.getElementById("addtobasketbox").style.display="block";
			document.getElementById("addtobasketbox").innerHTML=xmlHttp.responseText;
			LoadAddresses(locationtype);
		}
	
	}
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function SetDefaultBilling(addrid,locationtype) {
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	}
	
	var url="ajax/setdefaultbilling.php";
	url=url+"?sid="+Math.random();
	if (addrid) { url=url+"&addrid="+addrid; }

	xmlHttp.onreadystatechange = function() {
		
		if ((xmlHttp.readyState==1) || (xmlHttp.readyState==2) || (xmlHttp.readyState==3)) {
			document.getElementById("black_overlay").style.display="block";
			document.getElementById("addtobasketbox").style.display="block";
			document.getElementById("addtobasketbox").innerHTML="Loading, please wait...";
		}
		
		if (xmlHttp.readyState==4) {
			document.getElementById("black_overlay").style.display="block";
			document.getElementById("addtobasketbox").style.display="block";
			document.getElementById("addtobasketbox").innerHTML=xmlHttp.responseText;
			LoadAddresses(locationtype);
		}
	
	}
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

function CloseAddEditAddress() {
	document.getElementById("black_overlay").style.display="none";
	document.getElementById("addtobasketbox").style.display="none";
	document.getElementById("addtobasketbox").innerHTML="";
}

function DeleteAddress(addrid,locationtype) 
{

	var agree=confirm("Are you sure you want to delete the address?");
	if (agree) {
		
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null) {
		  alert ("Your browser does not support AJAX!");
		  return;
		}
	
		var url="ajax/deleteaddress.php";
		url=url+"?sid="+Math.random();
		if (addrid) { url=url+"&addrid="+addrid; }
	
		xmlHttp.onreadystatechange = function() {
			
			if ((xmlHttp.readyState==1) || (xmlHttp.readyState==2) || (xmlHttp.readyState==3)) {
				document.getElementById("black_overlay").style.display="block";
				document.getElementById("addtobasketbox").style.display="block";
				document.getElementById("addtobasketbox").innerHTML="Loading, please wait...";
			}
			
			if (xmlHttp.readyState==4) {
				document.getElementById("black_overlay").style.display="block";
				document.getElementById("addtobasketbox").style.display="block";
				document.getElementById("addtobasketbox").innerHTML=xmlHttp.responseText;
				LoadAddresses(locationtype);
			}
		
		}
		
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
		
	}

}

function LoadMeasurements(catid,subcatid)
{ 

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	 }

	

	var url="ajax/loadmeassurements.php";
	url=url+"?sid="+Math.random();
	url=url+"&catid="+catid;
	url=url+"&subcatid="+subcatid;
	xmlHttp.onreadystatechange = function() {
		
		if ((xmlHttp.readyState==1) || (xmlHttp.readyState==2) || (xmlHttp.readyState==3)) {
			document.getElementById("measurements").style.display="block";
			document.getElementById("measurements").innerHTML="Loading Meassurements...";
		}
		
		if (xmlHttp.readyState==4) {
			document.getElementById("measurements").style.display="block";
			document.getElementById("measurements").innerHTML=xmlHttp.responseText;
		}
	
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function PaymentMessage() {

	document.getElementById("black_overlay").style.display="block";
	document.getElementById("addtobasketbox").style.display="block";
	document.getElementById("addtobasketbox").innerHTML="<div style='padding:20px; font-size:16px;'><img src=\"include/icon_wait.png\" align=\"left\" alt=\"Please Wait\" />&nbsp;&nbsp;We are processing your payment, please be patient it could take few minutes...</div>";	
	document.getElementById("confirmbutton").disabled = true;

}

function ChangeCarType() {

	var pagesObj = document.getElementById("cardtype"); 
	var cardtype = pagesObj.options[pagesObj.selectedIndex].value;	
	
	if ((cardtype=="Maestro") || (cardtype=="Solo")) {
		document.getElementById("startdatetr").style.display = "table-row";
		document.getElementById("issuenumbertr").style.display = "table-row";
		
		document.getElementById("startmonth").options[0].selected = true;
		document.getElementById("startyear").options[0].selected = true;
		document.getElementById("issuenumber").value = "";
		
	} else {
		document.getElementById("startdatetr").style.display = "none";
		document.getElementById("issuenumbertr").style.display = "none";
	}

}

function PrintEmail() {
	var name = 'info';
	var at = '&#64;';
	var domain = 'viktory';
	var ext = '.co.uk';
	document.write('<a href=\"mailto:'+name+at+domain+ext+'\">'+name+at+domain+ext+'</a>');
}

//Ajax Main Function
function GetXmlHttpObject() {
	var xmlHttp=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	  catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	return xmlHttp;
}
