// The following function have been designed and implemented by Marc Cabot under the 
// employ of JVH Market Design for the SentierNBTrail council on August 1st, 2002.

function jvhCalculate() {

	varTotal = 0;
	varTotalDiscounted = 0;
	varDiscount = 0;
	varTotalItems = 0;
	
	for(i=0; i < document.varPrice.length; i++){
		if(i!==1){
		// calculate all the subtotals.
		varCurrentPriceOfQty = (parseFloat(document.varPrice[i]) * parseFloat(document.varQtyRef[i].value));
		if(document.varPhysical[i]){
			varTotalItems = varTotalItems + parseInt(document.varQtyRef[i].value);
		}
		document.varTotalRef[i].value = validateToDecimal( varCurrentPriceOfQty );
		varTotal = varTotal + varCurrentPriceOfQty;
		if(document.varDiscount[i]) {
			varDiscount = varDiscount - (varCurrentPriceOfQty * parseFloat(document.varDiscountPrice));
		}}
	}
	
	varShipping = getShippingCost();
	if(varTotalItems == 0) varShipping = 0;
	document.forms['OrderForm'].elements['shippingCost'].value = validateToDecimal(varShipping);
	varTotal = varTotal + varShipping;
	
	if(!hasCardNumber()) varDiscount = 0;
		
	document.forms['OrderForm'].elements['subTotal'].value = validateToDecimal(varTotal);
//	document.forms['OrderForm'].elements['discount'].value = validateToDecimal(varDiscount);
	varFinalTotal=0;
	if(varDiscount==0){
		varFinalTotal = varTotal;
	} else {
		varFinalTotal = varTotal + varDiscount + .01;
	}
//	document.forms['OrderForm'].elements['finalTotal'].value = validateToDecimal(varFinalTotal);

}

function getShippingCost(){
	varReturnValue = 0;


	if( (document.forms['OrderForm'].elements['qtyTrailBuildingSpecHardCopy'].value > 0)||(document.forms['OrderForm'].elements['qtySignManualHardCopy'].value  >0)  	|| (document.forms['OrderForm'].elements['qtySignManualWinCD'].value  >0)|| (document.forms['OrderForm'].elements['qtySentierNBTrailLogoPin'].value >0) || (document.forms['OrderForm'].elements['qtyRangerRobin'].value  > 0)){
	
	
		for(i=0; i<document.varCountryCode.length; i++){
		if(document.varCountryCode[i].checked){
			varReturnValue = document.varShip[i];
			}
		}
	}//document.forms['OrderForm'].elements[''].value 
	return varReturnValue;
}

function validateToDecimal(n) {
	n = parseFloat(n);
	n = n * 100;
	n = parseInt(n);
	n = parseFloat(n);
	n = n / 100;
	ns = n + '';
	de = ns.indexOf('.');
	le = ns.length;
	if (n==0) {
		return '0.00';
	} else {
		if (de == -1) {
			return n + '.00';
		} else {
			if (de == le - 3) {
				return n + '';
			} else {
				if (de == le - 2){
					return n + '0';
				} else {
					return n + '.00';
				}
			}
		}
	}
}

function hasCardNumber() {
	//if (document.forms['OrderForm'].elements['cardNumber'].value.length == 0) {
	//	return false;
	//} else { 
		return true;
//	}
}

function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}