function isset(varname){
  return(typeof(window[varname])!='undefined');
}

function cartfunction(tact,cartid){
	if(tact != '' && cartid != ''){
		tURL = "shoppingcartajax.php?id="+cartid+"&act="+tact+"&qty="+document.getElementById("qty"+cartid).value+"&sid="+document.getElementById("sessionid").value;
		if(isset('isshopping')){
			tURL = tURL + "&isshopping="+isshopping;
		}
		if(isset('isdelivery')){
			tURL = tURL + "&isdelivery="+isdelivery;
			if(document.getElementById("isrep").value == "1"){
				tURL = tURL + "&No_of_Recipients="+document.getElementById("No_of_Recipients").value;
				tURL = tURL + "&totrep="+document.getElementById("totrep").value;
				tURL = tURL + "&totprod="+document.getElementById("totprod").value;
				for(i=1;i<=parseInt(document.getElementById("totrep").value);i++){
					tURL = tURL + "&fullname"+i+"="+document.getElementById("fullname"+i).value;
					tURL = tURL + "&address"+i+"="+document.getElementById("address"+i).value;
					tURL = tURL + "&message"+i+"="+document.getElementById("message"+i).value;
					for(j=0;j<parseInt(document.getElementById("totprod").value);j++){
						if(document.getElementById("purchased_items_"+i+"_"+j).checked == true){
							tURL = tURL + "&purchased_items_"+i+"_"+j+"="+document.getElementById("purchased_items_"+i+"_"+j).value;
						}
					}
				}
			}
		}
		if(isset('ischeckout')){
			tURL = tURL + "&ischeckout="+ischeckout;
			tURL = tURL + "&Loyalty_Program_Name="+document.getElementById("Loyalty_Program_Name").value;
			tURL = tURL + "&Loyalty_Membership_No="+document.getElementById("Loyalty_Membership_No").value;
			tURL = tURL + "&FirstName="+document.getElementById("FirstName").value;
			tURL = tURL + "&Surname="+document.getElementById("Surname").value;
			tURL = tURL + "&Phone="+document.getElementById("Phone").value;
			tURL = tURL + "&Mobile="+document.getElementById("Mobile").value;
			tURL = tURL + "&Email="+document.getElementById("Email").value;
			tURL = tURL + "&Address1="+document.getElementById("Address1").value;
			tURL = tURL + "&Address2="+document.getElementById("Address2").value;
			tURL = tURL + "&Suburb="+document.getElementById("Suburb").value;
			tURL = tURL + "&Postcode="+document.getElementById("Postcode").value;
			tURL = tURL + "&State="+document.getElementById("State").value;
			tURL = tURL + "&Country="+document.getElementById("Country").value;
			tURL = tURL + "&DelFirstName="+document.getElementById("DelFirstName").value;
			tURL = tURL + "&DelSurname="+document.getElementById("DelSurname").value;
			tURL = tURL + "&DelPhone="+document.getElementById("DelPhone").value;
			tURL = tURL + "&DelMobile="+document.getElementById("DelMobile").value;
			tURL = tURL + "&DelEmail="+document.getElementById("DelEmail").value;
			tURL = tURL + "&DelAddress1="+document.getElementById("DelAddress1").value;
			tURL = tURL + "&DelAddress2="+document.getElementById("DelAddress2").value;
			tURL = tURL + "&DelSuburb="+document.getElementById("DelSuburb").value;
			tURL = tURL + "&DelPostcode="+document.getElementById("DelPostcode").value;
			tURL = tURL + "&DelState="+document.getElementById("DelState").value;
			tURL = tURL + "&DelCountry="+document.getElementById("DelCountry").value;
		}
		if(isset('ischeckoutconfirm')){
			tURL = tURL + "&ischeckoutconfirm="+ischeckoutconfirm;
		}
		//alert(tURL);
		var xmlHttp1; try{xmlHttp1=new XMLHttpRequest();}catch(e){try{xmlHttp1=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser does not support AJAX!");return false;}}}
		xmlHttp1.onreadystatechange=function(){
		if(xmlHttp1.readyState==4){
			if(xmlHttp1.status == 200){
		  	if(xmlHttp1.responseText != "NOTOK"){
					document.getElementById('cartdetail').innerHTML=xmlHttp1.responseText;
					getminicart();
		  	}}}}
	  xmlHttp1.open("GET",tURL,true);	
	  xmlHttp1.send(null);
	}
}

function getminicart(){
	document.getElementById("ajaxminicart").innerHTML = "<img src=images/lightbox/loading.gif>";
	tURL = "_inc-mini-cart.php?isajax=true&sessionid="+document.getElementById("sessionid").value;
	var xmlHttp2; try{xmlHttp2=new XMLHttpRequest();}catch(e){try{xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser does not support AJAX!");return false;}}}
	xmlHttp2.onreadystatechange=function(){
	if(xmlHttp2.readyState==4){
		if(xmlHttp2.status == 200){
	  	if(xmlHttp2.responseText != "NOTOK"){
				document.getElementById('ajaxminicart').innerHTML=xmlHttp2.responseText;
	  	}}}}
  xmlHttp2.open("GET",tURL,true);	
  xmlHttp2.send(null);
}

function viewrecipientlist(){
	if(document.getElementById("isrep").value == "1"){
		if(document.getElementById("No_of_Recipients").value == ""){
			document.getElementById("totrep").value = 0;
			for(i=1;i<=10;i++)
					document.getElementById("recipientlist"+i).style.display = "none";
		}else{
			NoofRecipients = parseInt(document.getElementById("No_of_Recipients").value);
			document.getElementById("totrep").value = NoofRecipients;
			if(NoofRecipients != 0){
				for(i=1;i<=NoofRecipients;i++){
					document.getElementById("recipientlist"+i).style.display = "";
				}
				NoofRecipients = NoofRecipients + 1;
				for(i=NoofRecipients;i<=10;i++){
					document.getElementById("recipientlist"+i).style.display = "none";
				}
			}else{
				for(i=1;i<=10;i++)
					document.getElementById("recipientlist"+i).style.display = "none";
			}
		}
	}
}

function validatedeliveryform(frm){
	if(frm.No_of_Recipients.selectedIndex == 0){
		alert("Please select No of Recipients.");
		frm.No_of_Recipients.focus();
		return false;
	}
	if(frm.No_of_Recipients.selectedIndex != 0 && frm.No_of_Recipients.selectedIndex != 1){
		totrep = parseInt(document.getElementById("totrep").value);
		totprod = parseInt(document.getElementById("totprod").value);
		for(i=1;i<=totrep;i++){
			if(document.getElementById("fullname"+i).value == ""){
				alert("Please enter recipient name.");
				document.getElementById("fullname"+i).focus();
				return false;
			}
			if(document.getElementById("address"+i).value == ""){
				alert("Please enter recipient address/postal.");
				document.getElementById("address"+i).focus();
				return false;
			}
			isProdSelected = 0;
			for(j=0;j<totprod;j++){
				if(document.getElementById("purchased_items_"+i+"_"+j).checked == true) isProdSelected = isProdSelected + 1;
			}
			if(isProdSelected == 0){
				alert("Please select atleast one item.");
				document.getElementById("purchased_items_"+i+"_0").focus();
				return false;
			}
		}
	}
	return true;
}

function validateme(val){
	cartid = document.getElementById("purchased_items_"+val).value;
	qty = document.getElementById("ty"+cartid).value;
	totrep = parseInt(document.getElementById("totrep").value);
	totprod = parseInt(document.getElementById("totprod").value);
	totrep = 10;
	tqty = 0;
	tmpids = "";
	tmpadd = "";
	for(i=1;i<=totrep;i++){
		for(j=0;j<totprod;j++){
			if(document.getElementById("purchased_items_"+i+"_"+j).value == cartid){
				if(document.getElementById("purchased_items_"+i+"_"+j).checked == true){
					tqty = tqty + 1;
				}else{
					tmpids = tmpids + tmpadd + "purchased_items_"+i+"_"+j;
					tmpadd = ",";
				}
			}
		}
	}
	if(document.getElementById("purchased_items_"+val).checked == true){
		if(tqty == qty){
			tmparray = tmpids.split(",");
			for(x in tmparray){
				if(tmparray[x] != ""){
				document.getElementById(tmparray[x]).checked = false;
				document.getElementById(tmparray[x]).disabled = true;
			}
		}
		}
		if(tqty > qty){
			document.getElementById("purchased_items_"+val).checked = false;
			document.getElementById("purchased_items_"+val).disabled = true;
		}
	}else{
		if(tqty < qty){
			tmparray = tmpids.split(",");
			for(x in tmparray){
				if(tmparray[x] != ""){
				document.getElementById(tmparray[x]).disabled = false;
				}
			}
		}
	}
}