function confirmOrder(){
	var varResult = true; //by default, everything is ok
	//check if credit card has been entered
	if(document.cart_detail.OrderPaymentMethod.value == "3" || document.cart_detail.OrderPaymentMethod.value == "4"){
		varResult = validatecc();
	}
	//check delivery details
	if (varResult == true){
	varResult = validatedeliv();
	}
	if (varResult == true){
	document.getElementById('btnCartConfirmOrder').innerHTML = "<span class='cbutton'><b>Processing... Please wait...</b></span>";
	document.cart_detail.submit();
	}
}

function updateCartQty(){

	document.cart.action = "cart.asp?action=update";
	document.cart.submit();

}

function UpdateOrderTotalFromDeliv(){
	try{
		varTaxRate = document.cart_detail.taxRate.value; //0.10; //default tax rate. Must also be declared in shopping.asp (varDefaultTaxRate)
	}
	catch(err){
		varTaxRate = 0;
	}
	varSubTotal = document.cart_detail.subTotal.value;
	try{
		varDeliv = document.cart_detail.delivfees.value;
	}
	catch(err){
		varDeliv = 0;
	}
	try{
		varDisc = document.cart_detail.orderDiscount.value;
	}
	catch(err){
		varDisc = 0;
	}
	varSubTotal = parseFloat(varSubTotal) - parseFloat(varDisc);
	varTax = parseFloat((parseFloat(varSubTotal,2) + parseFloat(varDeliv,2)) * varTaxRate,2); //calculate tax total
	try{
	document.getElementById('oTax').innerHTML = CurrencyFormatted(parseFloat(varTax,2));
	}
	catch(err){}
	document.getElementById('oTotal').innerHTML = CurrencyFormatted(parseFloat(varSubTotal,2) + parseFloat(varDeliv,2) + parseFloat(varTax,2));
	try{
	document.cart_detail.taxTotal.value = CurrencyFormatted(varTax,2);
	}
	catch(err){}
	document.cart_detail.grandTotal.value = parseFloat(CurrencyFormatted(varSubTotal,2)) + parseFloat(CurrencyFormatted(varDeliv,2)) + parseFloat(CurrencyFormatted(varTax,2));
	
}

function UpdateTaxTotalFromBilling(){
	varTax = 0;
	varBillingCountry = document.cart_detail.PostCountry.value;
	
	if (varBillingCountry == "Australia"){
		//tax applies
		try{
		varTax = document.cart.taxTotal.value;
		}
		catch(err){varTax = 0;}
	}
	else{
		varTax = 0;
	}
	varSubTotal = document.cart.subTotal.value;
	try{
	varDeliv = document.cart_detail.delivfees.value;
	}
	catch(err){varDeliv = 0;}
	try{
	document.getElementById('oTax').innerHTML = CurrencyFormatted(parseFloat(varTax,2));
	}
	catch(err){}
	document.getElementById('oTotal').innerHTML = CurrencyFormatted(parseFloat(varSubTotal,2) + parseFloat(varDeliv,2) + parseFloat(varTax,2));
}

function updateTaxByCountry(varCountryList, varTaxRateList){
//updates tax rate by country code

	var varRate = parseFloat(CurrencyFormatted(0)); //default rate
	var varCountry = document.cart_detail.DeliveryCountry.value;
	
	if(varCountryList != "" && varCountry != ""){
		//split country list into array
		var varCountryListTxt = varCountryList;
		
		var varCountryArr = new Array();
		varCountryArr = varCountryListTxt.split("|");
		
		var arLen=varCountryArr.length;
		for ( var z=0, len=arLen; z<len; ++z ){
			if (varCountry == varCountryArr[z]){
				break;
			}
		}
		
		//split rate list into array
		var varTaxRateListTxt = varTaxRateList;
		var varRateArr = new Array();
		varRateArr = varTaxRateListTxt.split("|");
		varRate = varRateArr[z];		
	}
	try{
	document.cart_detail.taxRate.value = parseFloat(CurrencyFormatted(varRate));
	}
	catch(err){}
	//update order totals
	UpdateOrderTotalFromDeliv();

	return varRate;
}

function SameAsBilling(){

	document.cart_detail.DeliveryAddress.value = document.cart_detail.PostAddress.value;
	document.cart_detail.DeliveryCity.value = document.cart_detail.PostCity.value;
	document.cart_detail.DeliveryStateOrProvince.value = document.cart_detail.PostStateOrProvince.value;
	document.cart_detail.DeliveryPostalCode.value = document.cart_detail.PostPostalCode.value;
	document.cart_detail.DeliveryCountry.value = document.cart_detail.PostCountry.value;
	document.cart_detail.DeliveryAddress.focus();

}

function validatedeliv(){
//delivery address
	
	if (document.cart_detail.DeliveryName.value=="") {
	alert("Oops! Please enter your DELIVERY CONTACT NAME.");
	document.cart_detail.DeliveryName.focus();
	return false;
	}
	
	if (document.cart_detail.DeliveryAddress.value=="") {
	alert("Oops! Please enter your DELIVERY ADDRESS.");
	document.cart_detail.DeliveryAddress.focus();
	return false;
	}

	if (document.cart_detail.DeliveryCity.value=="") {
	alert("Oops! Please select your DELIVERY SUBURB.");
	document.cart_detail.DeliveryCity.focus();
	return false;
	}
	
	if (document.cart_detail.DeliveryStateOrProvince.value=="") {
	alert("Oops! Please select your DELIVERY STATE.");
	document.cart_detail.DeliveryStateOrProvince.focus();
	return false;
	}
	
	if (document.cart_detail.DeliveryPostalCode.value=="") {
	alert("Oops! Please select your DELIVERY POSTCODE.");
	document.cart_detail.DeliveryPostalCode.focus();
	return false;
	}
	
	if (document.cart_detail.DeliveryCountry.value=="") {
	alert("Oops! Please select your DELIVERY COUNTRY.");
	document.cart_detail.DeliveryCountry.focus();
	return false;
	}
	
	if (document.cart_detail.delivfees.value=="") {
	alert("Oops! Please select the DELIVERY METHOD/FEE.");
	document.cart_detail.delivfees.focus();
	return false;
	}
	
	
//other fields


	return true
}

function validatecc(){
//credit card details
	try{
		if (document.cart_detail.CCType.value=="") {
		alert("Oops! Please select the CARD TYPE.");
		document.cart_detail.CCType.focus();
		return false;
		}
		
		if (document.cart_detail.CCNameOnCard.value=="") {
		alert("Oops! Please enter the NAME ON THE CARD.");
		document.cart_detail.CCNameOnCard.focus();
		return false;
		}
		
		if (document.cart_detail.CCNumber.value=="") {
		alert("Oops! Please enter the CARD NUMBER.");
		document.cart_detail.CCNumber.focus();
		return false;
		}
		
		varCardNumValid = Mod10(document.cart_detail.CCNumber.value)
		if (varCardNumValid == false){
		alert("Oops! The CREDIT CARD NUMBER entered is not valid.");
		document.cart_detail.CCNumber.focus();
		return false;
		}

		if (document.cart_detail.CCExpMonth.value=="") {
		alert("Oops! Please select the CARD EXPIRY MONTH.");
		document.cart_detail.CCExpMonth.focus();
		return false;
		}
		
		if (document.cart_detail.CCExpYear.value=="") {
		alert("Oops! Please select the CARD EXPIRY YEAR.");
		document.cart_detail.CCExpYear.focus();
		return false;
		}
		
		if (document.cart_detail.CCSecurityCode.value=="") {
		alert("Oops! Please enter the CARD SECURITY CODE.");
		document.cart_detail.CCSecurityCode.focus();
		return false;
		}
		
		
		return true
	}
	catch(err){return false}

}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}


function Mod10(ccNumb) {
/* 
the alorithum takes each digit, from right to left and muliplies each second 
digit by two. If the multiple is two-digits long (i.e.: 6 * 2 = 12) the two digits of 
the multiple are then added together for a new number (1 + 2 = 3). You then add up the 
string of numbers, both unaltered and new values and get a total sum. This sum is then 
divided by 10 and the remainder should be zero if it is a valid credit card. Hense the 
name Mod 10 or Modulus 10. */ 

	var valid = "0123456789" // Valid digits in a credit card number 
	var len = ccNumb.length; // The length of the submitted cc number 
	var iCCN = parseInt(ccNumb); // integer of ccNumb 
	var sCCN = ccNumb.toString(); // string of ccNumb 
	sCCN = sCCN.replace (/^\s+|\s+$/g,''); // strip spaces 
	var iTotal = 0; // integer total set at zero 
	var bNum = true; // by default assume it is a number 
	var bResult = false; // by default assume it is NOT a valid cc 
	var temp; // temp variable for parsing string 
	var calc; // used for calculation of each digit 

	// Determine if the ccNumb is in fact all numbers 
	for (var j=0; j<len; j++) { 
		temp = "" + sCCN.substring(j, j+1); 
		if (valid.indexOf(temp) == "-1"){bNum = false;} 
	} 

	// if it is NOT a number, you can either alert to the fact, or just pass a failure 
	if(!bNum){ 
		bResult = false; 
	} 

	// Determine if it is the proper length 
	if((len == 0)&&(bResult)){ // nothing, field is blank AND passed above # check 
		bResult = false; 
	}
	else{ // ccNumb is a number and the proper length - let's see if it is a valid card number 
		if(len >= 15){ // 15 or 16 for Amex or V/MC 
			for(var i=len;i>0;i--){ // LOOP throught the digits of the card 
				calc = parseInt(iCCN) % 10; // right most digit 
				calc = parseInt(calc); // assure it is an integer 
				iTotal += calc; // running total of the card number as we loop - Do Nothing to first digit 
				i--; // decrement the count - move to the next digit in the card 
				iCCN = iCCN / 10; // subtracts right most digit from ccNumb 
				calc = parseInt(iCCN) % 10 ; // NEXT right most digit 
				calc = calc *2; // multiply the digit by two 
				switch(calc){ 
					case 10: calc = 1; break; //5*2=10 & 1+0 = 1 
					case 12: calc = 3; break; //6*2=12 & 1+2 = 3 
					case 14: calc = 5; break; //7*2=14 & 1+4 = 5 
					case 16: calc = 7; break; //8*2=16 & 1+6 = 7 
					case 18: calc = 9; break; //9*2=18 & 1+8 = 9 
					default: calc = calc; //4*2= 8 & 8 = 8 -same for all lower numbers 
				} 
				iCCN = iCCN / 10; // subtracts right most digit from ccNum 
				iTotal += calc; // running total of the card number as we loop 
			} // END OF LOOP 
			if ((iTotal%10)==0){ // check to see if the sum Mod 10 is zero 
				bResult = true; // This could be a valid credit card number. 
			}
			else { 
				bResult = false; // This could NOT be a valid credit card number 
			} 
		} 
	} 

return bResult; // Return the results 
}
