// JavaScript Document

function basketFunction(){
	var thisfun=document.forms[0].basketFunctions.value;
	if(thisfun=="NONE"){
		return;
	}
	else{
		if(thisfun=="qnt"){
			updateBasket();
			return;
		}
				if(thisfun=="rem"){
			removeBasket();
			return;
		}
						if(thisfun=="del"){
			emptyBasket();
			return;
		}		
	}
}

   //delete from basket
   
    function removeBasket() { 
 if (confirm('Are you sure you want to REMOVE the item(s) ticked?\n\nThis action cannot be undone!')) {
	for (i=0; i<document.forms[0].elements.length; i++) { 
		el = document.forms[0].elements[i];
		if (el.type=="checkbox" && el.name.indexOf("checkBox")!=-1){
			if (el.checked) {
			var id = el.name.replace(/checkBox/, "");
			deleteme(id);
}
		} 
	}
	returnBasket();
	}
}

function deleteme(id){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="/resource/working/commerce/deleteItem.asp";
url=url+"?id="+id;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function deleteme2(id){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="/resource/working/commerce/deleteBasket.asp"
url=url+"?id="+id;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

    function emptyBasket() { 
 if (confirm('Are you sure you want to EMPTY the entire basket?\n\nThis action cannot be undone!')) {
	for (i=0; i<document.forms[0].elements.length; i++) { 
		el = document.forms[0].elements[i];
		if (el.type=="checkbox" && el.name.indexOf("checkBox")!=-1){
			var id = el.name.replace(/checkBox/, "");
			deleteme2(id);
		} 
	}
	returnBasket2();
	}
}

function updateMe(id,qnt){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="/resource/working/commerce/updateItem.asp"
url=url+"?id="+id;
url=url+"&qnt="+qnt;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

    function updateBasket() { 
 if (confirm('Are you sure you want to UPDATE the selection(s) ticked?')) {
 var qnt;
 var x;
 var id;
	for (i=0; i<document.forms[0].elements.length; i++) { 
		el = document.forms[0].elements[i];
		if (el.type=="checkbox" && el.name.indexOf("checkBox")!=-1){
			if (el.checked) {
			id = el.name.replace(/checkBox/, "");
			x = "qnt" + id;
			qnt = document.getElementById(x).value;
			updateMe(id,qnt);
}
		} 
	}
	returnBasket();
	}
}

function returnBasket(){
	mydiv=getQueryVariable('div');
	mycat=getQueryVariable('cat');
window.location.href="/commerce-basket.asp?div=" + mydiv + "&cat=" + mycat;
	}
	
	function returnBasket2(){
var kill_time = new Date("January 1, 1970");
var kill_string = "buttonjewellery=;expires=" + kill_time.toGMTString();
document.cookie = kill_string;
	mydiv=getQueryVariable('div');
	mycat=getQueryVariable('cat');
window.location.href="/shop-online.asp?div=" + mydiv + "&cat=" + mycat;
	}
	
	function getQueryVariable(variable) { 
	var query = window.location.search.substring(1); 
	var vars = query.split("&"); 
	for (var i=0;i<vars.length;i++) {   
	var pair = vars[i].split("=");   
	if (pair[0] == variable) {     
	return pair[1];   
	} 
	} 
	alert('Query Variable ' + variable + ' not found');
	}
	
	function selDes(obj){
		var x = "checkBox"+obj;
		if(document.getElementById(x).checked==true){
			document.getElementById(x).checked=false;
		}
		else{
			document.getElementById(x).checked=true
		}
	}

function makePayment() {

//error checking........................................................................
var ititle = document.forms[0].ititle.value;
var fname = document.forms[0].fname.value;
var sname = document.forms[0].sname.value;
var ad1 = document.forms[0].ad1.value;
var ad2 = document.forms[0].ad2.value;
var ad3 = document.forms[0].ad3.value;
var ad4 = document.forms[0].ad4.value;
var town = document.forms[0].town.value;
var county = document.forms[0].county.value;
var postcode = document.forms[0].postcode.value;
var country = document.forms[0].country.value;
var telephone = document.forms[0].telephone.value;
var emailaddress = document.forms[0].emailaddress.value;

// reset fields before alert.......................................................
document.getElementById("a1").innerHTML = "&nbsp;";
document.getElementById("a2").innerHTML = "&nbsp;";
document.getElementById("a3").innerHTML = "&nbsp;";
document.getElementById("a4").innerHTML = "&nbsp;";
document.getElementById("a5").innerHTML = "&nbsp;";
document.getElementById("a6").innerHTML = "&nbsp;";
document.getElementById("a7").innerHTML = "&nbsp;";
document.getElementById("a8").innerHTML = "&nbsp;";
document.getElementById("a9").innerHTML = "&nbsp;";
document.getElementById("a10").innerHTML = "&nbsp;";
document.getElementById("a12").innerHTML = "&nbsp;";
document.getElementById("a13").innerHTML = "&nbsp;";
//..........................................................................................

var thisAlert="<a name=alert></a>&nbsp;Required&nbsp;";

if(ititle==""){
document.getElementById("a1").innerHTML = thisAlert;
document.location.href="#alert";

}
else if (fname==""){
document.getElementById("a2").innerHTML = thisAlert;
document.location.href="#alert";
}
else if (sname==""){
document.getElementById("a3").innerHTML = thisAlert;
document.location.href="#alert";
}
else if (ad1==""){
document.getElementById("a4").innerHTML = thisAlert;
document.location.href="#alert";
}
else if (ad2==""){
document.getElementById("a5").innerHTML = thisAlert;
document.location.href="#alert";
}
else if (ad3==""){
document.getElementById("a6").innerHTML = thisAlert;
document.location.href="#alert";
}
else if (ad4==""){
document.getElementById("a7").innerHTML = thisAlert;
document.location.href="#alert";
}
else if (town==""){
document.getElementById("a8").innerHTML = thisAlert;
document.location.href="#alert";
}
else if (county==""){
document.getElementById("a9").innerHTML = thisAlert;
document.location.href="#alert";
}
else if (postcode==""){
document.getElementById("a10").innerHTML = thisAlert;
document.location.href="#alert";
}
else if (telephone==""){
document.getElementById("a12").innerHTML = thisAlert;
document.location.href="#alert";
}
else if (emailaddress==""){
document.getElementById("a13").innerHTML = thisAlert;
document.location.href="#alert";
}

else {

var thisLot;
thisLot = "ititle=" + encodeURIComponent(document.forms[0].ititle.value);
thisLot = thisLot + "&fname=" + encodeURIComponent(document.forms[0].fname.value);
thisLot = thisLot + "&sname=" + encodeURIComponent(document.forms[0].sname.value);
thisLot = thisLot + "&ad1=" + encodeURIComponent(document.forms[0].ad1.value);
thisLot = thisLot + "&ad2=" + encodeURIComponent(document.forms[0].ad2.value);
thisLot = thisLot + "&ad3=" + encodeURIComponent(document.forms[0].ad3.value);
thisLot = thisLot + "&ad4=" + encodeURIComponent(document.forms[0].ad4.value);
thisLot = thisLot + "&town=" + encodeURIComponent(document.forms[0].town.value);
thisLot = thisLot + "&postcode=" + encodeURIComponent(document.forms[0].postcode.value);
thisLot = thisLot + "&county=" + encodeURIComponent(document.forms[0].county.value);
thisLot = thisLot + "&country=" + encodeURIComponent(document.forms[0].country.value);
thisLot = thisLot + "&emailaddress=" + encodeURIComponent(document.forms[0].emailaddress.value);
thisLot = thisLot + "&telephone=" + encodeURIComponent(document.forms[0].telephone.value);
thisLot = thisLot + "&cancontact=" + encodeURIComponent(document.forms[0].cancontact.value);

var url="/sendToPayment.asp"

document.location.href=url+"?"+thisLot;
}
}
