var totalweight;
var runcost;
var vvalue;
var dvalue;
var pickups = 0;
var prangearr = new Array();
var flash = 2;
var specx;
var postremove;

//lock discount field
function lockdisc(loc){
	if (loc == 1){
	document.getElementById("disccontent").innerHTML = 'Please note: Your order includes Gift Certificates or Hampers, discounts cannot be applied to this order.';
	document.getElementById("rebttt").style.display = "none";
	}
}

//wrapping
function giftWrap(){
	var pretot = eval(document.getElementById("totcost").innerHTML);
	if (document.getElementById("wrap").value != 0){
		if (flash == 2){
		var newx = eval(pretot+eval(2));
		document.getElementById("totcost").innerHTML = newx.toFixed(2);
		document.getElementById("fintot").value = newx.toFixed(2);
		}
		flash = 1;
	} else {
		var newx = eval(pretot-eval(2));
		document.getElementById("totcost").innerHTML = newx.toFixed(2);
		document.getElementById("fintot").value = newx.toFixed(2);
		flash = 2;
	}

}

function formSub(forms){
	
		document.getElementById(forms).submit();
	
	
}


function valform(){
		
		if ((document.getElementById("pay_bd").checked == true) || (document.getElementById("pay_pal").checked == true)){
		document.getElementById("payforms").submit();
		} else {
		alert('Please select a payment option');	
		}
	
	
}

function clearField(fields){
	document.getElementById(fields).value = "";
}


//submit customer update form
function submitForm(){
	if (document.getElementById("cust_pass").value == document.getElementById("cust_passconf").value){
		document.userform.submit();
	} else {
		alert("You have entered your new password incorrectly!");
	}
}

//submit search
function searchsub(){
	if (document.getElementById("psearch").value != ""){
	document.getElementById("search").submit();
	} else {
		alert("You cannot leave the search field blank");
	}
}

//validate order form
function validatef(){
	if ((document.getElementById("cust_namey").value == "") || (document.getElementById("cust_surname").value == "") || (document.getElementById("cust_phone").value == "") || (document.getElementById("cust_email").value == "") || (document.getElementById("cust_addrbill").value == "") || (document.getElementById("cust_suburbbill").value == "") || (document.getElementById("cust_statebill").value == "") || (document.getElementById("cust_postcodebill").value == "") || (document.getElementById("cust_countrybill").value == "") || (document.getElementById("cust_addr").value == "") || (document.getElementById("cust_suburb").value == "") || (document.getElementById("cust_state").value == "") || (document.getElementById("cust_postcode").value == "") || (document.getElementById("cust_country").value == "")){
		alert("Please ensure all form fields are complete!");
	
	
	} else {
		if (document.getElementById("cust_country").value != '181'){
		
		alert("We can only process online orders for delivery to Australian destinations!");
		
		} else {
			
		document.userformx.submit();
		
		}
	}
}


//duplicate address for del details
function dupAddress(){
	document.getElementById("cust_billname").value = document.getElementById("cust_namey").value+" "+document.getElementById("cust_surname").value;
	document.getElementById("cust_addr").value = document.getElementById("cust_addrbill").value;
	document.getElementById("cust_suburb").value = document.getElementById("cust_suburbbill").value;
	document.getElementById("cust_state").value = document.getElementById("cust_statebill").value;
	document.getElementById("cust_country").value = document.getElementById("cust_countrybill").value;
	document.getElementById("cust_postcode").value = document.getElementById("cust_postcodebill").value;
}

//no delivery
function pickup(){
	if (pickups == 0){
		alert("No shipping charges are incurred for pick up orders. Pick up is only available from our Mornington Showroom during opening hours. Please check our location. Are you sure you want to pick up?");
		pickups = 1;
	} else {
		pickups = 0;
	}
	showPostage();
}

//alert for non aus selections
function checkCountry(country){
	if (country != '181'){
		alert("Please note, we do not provide online delivery costs for overseas orders. Please contact us to arrange overseas orders!");
	}
}


//hide pops
function closeAlert(alerts){
	document.getElementById(alerts).style.display = "none";
}
//set count for headers
var typecount = 1;
var type;
//slideshow


function menuSub(menub){
	document.getElementById(menub).style.display = "inline";
}

function hideSub(menub){
	var countb = 0;
	var modcountb;
	do {
	countb ++;
	modcountb = "submentop"+countb;
	
		if (countb != menub){
		document.getElementById(modcountb).style.display = "none";
		}
	
	} while (countb < 50);
	var countb = 0;
	var modcountb = 0;
	
	//if (menub == 'all'){
	//	menuHide('all');
	//}
}



function menuSubx(menubb){
	//document.getElementById("tester").innerHTML = 'test';
	document.getElementById(menubb).style.display = "inline";
}

function hideSubb(menubb){
	var countbb = 0;
	var modcountbb;
	do {
	countbb ++;
	modcountbb = "submentopb"+countbb;
	
		if (countbb != menubb){
		document.getElementById(modcountbb).style.display = "none";
		}
	
	} while (countbb < 30);
	var countbb = 0;
	var modcountbb = 0;
}

function hideMe(ccbx){
		
		var xcount = 0;
		var gfld;
		
		do {
			
		xcount ++;
		gfld = "submentopx"+xcount;
			
			if (xcount != ccbx){
			document.getElementById(gfld).style.display = "none";
			}
		
		} while (xcount);
		
		xcount = 0;
		gfld = 0;
		
}

//hide regular top subs
var removex;
function showSubbx(menubbx){
	
		
		document.getElementById(menubbx).style.display = "inline";
		
		removex = menubbx;
}

function hideSubbxx(menubbx){
		//if (countbbx != menubbx){
		document.getElementById(menubbx).style.display = "none";
		//}

}

function hideSubbx(menubbx){
	
		//if (countbbx != menubbx){
		document.getElementById(menubbx).style.display = "none";
		//}

}



function menuShow(menu){
	document.getElementById(menu).style.display = "inline";
}


function menuHide(menu){
	
	var count = 0;
	var modcount;
	do {
	count ++;
	modcount = "drop"+count;
	
		if (count != menu){
		document.getElementById(modcount).style.display = "none";
		}
	
	} while (count < 4);
	var count = 0;
	
	if (menu == 'all'){
		var countb = 0;
		var modcountb;
		do {
		countb ++;
		modcountb = "submentop"+countb;
	
			if (countb != menub){
			document.getElementById(modcountb).style.display = "none";
			}
	
		} while (countb < 50);
		var countb = 0;
		var modcountb = 0;
	}
}

function priceChange(price){
	
	$expcount = 0;
	do { 
		
		
	
		if (price == prangearr[$expcount][0]){
			document.getElementById("pchangex").innerHTML = prangearr[$expcount][1];
		}
		$expcount ++;
		
	} while ($expcount < mcounts);

	document.getElementById("pchange").innerHTML = price;
	//document.getElementById("pchangex").innerHTML = prangearr[0][0];

}


var xmlHttp
var displaycart

function addToCart(itemid,showcart,itemdel,itemqty)
{ 

if (itemqty != 'x'){
var qtys = itemqty;
} else {
var qtys = document.getElementById("Quantity").value;
}

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
 //if (showcart == "x"){
	 displaycart = showCartTotals;
 //} else {
 	 //displaycart = showCart;
 //}
//var qfield = "qty"+itemqty
var url="includes/utilities_cart.php"
url=url+"?itemid="+itemid
url=url+"&showcart="+showcart
url=url+"&delete="+itemdel
url=url+"&qty="+qtys

url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=displaycart
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function showCartTotals() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
	document.getElementById("cartalert").style.display = "inline";
 	document.getElementById("runcart").innerHTML=xmlHttp.responseText;
}
}

function showCart() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
 	document.getElementById("showcart").innerHTML=xmlHttp.responseText;
	cartAdjust("x", "x");
}
}


//adjust cart values etc here
var totalsid
var updateqty
function cartAdjust(prodid,divid)
{ 

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }

totalsid = "totals"+divid;
updateqty = "qty"+divid;
var url="includes/utilities_cart.php"
url=url+"?prodid="+prodid
if (divid != "x"){
url=url+"&newtot="+document.getElementById(updateqty).value
} else {
url=url+"&newtot=x"
}
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=adjustcart
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

var newtotals;
var agtotals;
var righttotals;
function adjustcart() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
 	//document.getElementById(totalsid).innerHTML=xmlHttp.responseText;
	eval(xmlHttp.responseText);
		if (totalsid != "totalsx"){
		document.getElementById(totalsid).innerHTML = newtotals;
		}
	document.getElementById("carttext").innerHTML = agtotals;
	document.getElementById("cart").innerHTML= righttotals;
}
}

 //get postage
function getPostage(pcodesz)
{ 

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }

var url="includes/postage.php"
url=url+"?pcode="+pcodesz
url=url+"&weight="+totalweight
url=url+"&prodtot="+runcost
url=url+"&bulky="+bulky
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=showPostage
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}


//this one gets the postage for the shopping cart
function getPostagecart()
{ 

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }

var url="includes/postage.php"
url=url+"?pcode="+document.getElementById("cpcode").value;
url=url+"&weight="+totalweight;
url=url+"&prodtot="+runcost;
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=showPostagec
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

var special;

function giftVoucher(){
	
	document.getElementById("gcertx").value = document.getElementById("gcert").value;

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }

var url="includes/gvoucher.php"
url=url+"?code="+document.getElementById("gcert").value;
url=url+"&spec="+special;
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=showVouch
xmlHttp.open("GET",url,true)
xmlHttp.send(null)


}

var vvalue;
var dvalue;
function showVouch(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
 	eval(xmlHttp.responseText);
		if ((vvalue == 0) && (dvalue == 0)){
			alert('The voucher number or discount code that you have entered is not registered in our database.');
		} else if (dvalue == 'xx'){
			alert('You can not use a discount code on orders containing sale items.');
		} else {
		
		getPostage(setpost);
		
		}
	}
}

var pstage;
var totcosts;
var fincost;
var subtrpost;
function showPostage() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
 	eval(xmlHttp.responseText);
	
	//if using redemption with free shipping, set it here.
	if (postremove == 1){
		pstage = 0;
	}
	
	if (pickups == 1){
	subtrpost = pstage;
	} else {
	subtrpost = 0;
	}
	
	document.getElementById("postrow").style.display = "";
	document.getElementById("totrow").style.display = "";
	if (pickups == 1){
	document.getElementById("post").innerHTML = 0;
	document.getElementById("posttot").value = 0;
	} else {
	document.getElementById("post").innerHTML = pstage;
	document.getElementById("posttot").value = pstage;
		
	}
	
	//calc gift certs
	if (vvalue > 0){
	document.getElementById("giftrow").style.display = "";
	document.getElementById("giftred").innerHTML = "<b>$ -"+vvalue+"</b>";
	//document.getElementById("discount").value = vvalue;
	document.getElementById("disctype").value = 1;
	var checkvals = eval(totcosts - vvalue);
	
		if (checkvals < 0){
		document.getElementById("discount").value = totcosts;
		fincost = '0.00';
		} else {
		document.getElementById("discount").value = vvalue;
		fincost = checkvals;
		}
	//calc discount
	} else if (dvalue > 0){
	document.getElementById("giftrow").style.display = "";
	document.getElementById("giftred").innerHTML = "<b>-"+dvalue+"%</b>";
	document.getElementById("discount").value = dvalue;
	document.getElementById("disctype").value = 2;
	if (specx > 0){
		//document.getElementById("testspec").innerHTML = eval(specx);
		var setrun = eval(runcost - eval(specx));
	} else {
		var setrun = runcost;
	}
	var getperc = eval((setrun / 100) * dvalue);
	var costnopost = eval(setrun - getperc);
	var costnopcl = eval(costnopost);
	var pstagecl = eval(pstage);
	
			if (specx > 0){
			fincost = eval((pstagecl + costnopcl + eval(specx))-subtrpost);
			} else {
			fincost = eval((pstagecl + costnopcl)-subtrpost);
			}
			
		
	} else {
		fincost = eval(totcosts-subtrpost);
	}
		var finals = Math.round(fincost*100)/100;
	
	document.getElementById("totcost").innerHTML = finals.toFixed(2);
	document.getElementById("fintot").value = finals.toFixed(2);
	
	if (finals < 0.01){
		document.getElementById("r1").style.display = "none";
		document.getElementById("r2").style.display = "none";
		document.getElementById("r3").style.display = "none";
		document.getElementById("r4").style.display = "none";
		document.getElementById("r5").style.display = "none";
		document.getElementById("noch").value = "1";
	}
	
	
}
}

var pstage;
var totcosts;
var fincost;
var subtrpost;
function showPostagec() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
 	eval(xmlHttp.responseText);
	
	document.getElementById("cpost").innerHTML = "<br/><b>Delivery Cost: $"+pstage+"</b>";
	
}
}


//change paytype
function payType(ptype){
	if (ptype == 1){
		document.getElementById("cc").style.display = "inline"; 
	} else {
		document.getElementById("cc").style.display = "none";
	}
}

 
 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;
}


