var totalweight;
var runcost;
var vvalue;
var dvalue;
var pickups = 0;
var prangearr = new Array();
var noposts = 0;
var reject;

//checkbox
function checkBox(boxname){
	
	if (boxname == "pay_cc"){
		document.getElementById("pay_bd").checked = false;
		document.getElementById("pay_pal").checked = false;
	}
	if (boxname == "pay_bd"){
		document.getElementById("pay_cc").checked = false;
		document.getElementById("pay_pal").checked = false;
	}
	if (boxname == "pay_pal"){
		document.getElementById("pay_bd").checked = false;
		document.getElementById("pay_cc").checked = false;
	}
	//document.getElementById("pay_cc").checked
	//document.getElementById("pay_bd").checked
	//document.getElementById("pay_pal").checked
}

function noPost(){
	alert('Your postcode could not be found. Please check it and try again.');
}

function valform(){
	if ((document.getElementById("pay_cc").checked == false) && (document.getElementById("pay_bd").checked == false) && (document.getElementById("pay_pal").checked == false) && (document.getElementById("noch").value < '1')){
		alert("Please select a payment method.");
	} else {
		document.getElementById("payforms").submit();
	}
	
}

function clearField(fields){
	document.getElementById(fields).value = "";
}
pic1= new Image(); 
pic1.src="images/img1B.jpg";

pic2= new Image(); 
pic2.src="images/img2B.jpg";

pic3= new Image(); 
pic3.src="images/img3B.jpg";

pic4= new Image(); 
pic4.src="images/img4B.jpg";

pic5= new Image(); 
pic5.src="images/img5B.jpg";


//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!");
	}
}


function validateNo(){
		document.userformx.submit();
}

//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 {
		
			if (document.getElementById("pickupx").checked == true){
			document.userformx.submit();
			} else {
			getPostageCheck();
			}
		
		}
	}
}


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

function dupAddressb(){
	//document.getElementById("cust_billname").value = document.getElementById("cust_name").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 slideSwitch() {
	
	
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
     //var $sibs  = $active.siblings();
     //var rndNum = Math.floor(Math.random() * $sibs.length );
     //var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
	
	
	
}



$(function() {
    setInterval( "slideSwitch()", 5000 );
});

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)
{ 

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+"&volume="+document.getElementById("volume").value
url=url+"&delete="+itemdel
url=url+"&qty="+document.getElementById("Quantity").value

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+"&poststat="+noposts
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=showPostage
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function getPostageCheck()
{ 

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

var url="includes/postagecheck.php"
url=url+"?pcode="+document.getElementById("cust_postcode").value
url=url+"&weight=85"
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=showCheck
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

var reject;
function showCheck(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
 	eval(xmlHttp.responseText);
		if (reject == 1){
			alert('The postcode that you have entered is invalid.');
		} else {

		document.getElementById("userformx").submit();
		
		}
	}
}

//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)
}


function giftVoucher(){

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+"&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 {

		getPostage(setpost);
		
		}
	}
}

var pstage;
var totcosts;
var fincost;
var subtrpost;
var reject;
function showPostage() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
 	eval(xmlHttp.responseText);
	
	//if (reject == 1){
		//window.location = "https://aromatherapyandnaturalskincare.com.au/index.php?essential_oils=125&trigs=0&nopost=1";
	//}
	
	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;
	var getperc = eval((runcost / 100) * dvalue);
	var costnopost = eval(runcost - getperc);
	var costnopcl = eval(costnopost);
	var pstagecl = eval(pstage);
	
	
			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);
	if (reject == 1){
		alert('The postcode that you have entered is invalid.');
	} else {
	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;
}

