var xmlHttp
var isGenQuotePg = false;

/**
 * Gets the shipping cost for a quote or order
 */
function getShippingCost(frm,baseUrl,genQuotePg) {
	var padPid, padQty, url;
	var lamPids;
	var mQty = 0;
	var zip = frm.zipCode.value;
	isGenQuotePg = genQuotePg;
	// CLEAR THE OLD SHIPPING CALCS
	eval("document.quoteWizard.shippingTotal_"+p1+".value = '0.00'");
	if(genQuotePg) {
		eval("document.quoteWizard.shippingTotal_"+p2+".value = '0.00'");
		eval("document.quoteWizard.shippingTotal_"+p3+".value = '0.00'");
		eval("document.quoteWizard.shippingTotal_"+p4+".value = '0.00'");
		eval("document.quoteWizard.shippingTotal_"+p5+".value = '0.00'");
		eval("document.quoteWizard.shippingTotal_"+p6+".value = '0.00'");
	}
	calculateGrandTotals(document.quoteWizard);
	// END CLEAR THE OLD SHIPPING CALCS
	// Set up the array of laminate pids to process
	if(genQuotePg) {
		lamPids = p1+","+p2+","+p3+","+p4+","+p5+","+p6
	} else {
		lamPids = p1;
	}
	// Check the zip code
	if(zip == "" || zip.length < 5 || !isNumeric(zip)) {
		alert("A valid zip code is required to calculate shipping.");
		return false;
	}
	// Get the pad info
	for(i=0;i<padArray.length;i++) {
		if(eval("document.quoteWizard.ckPad_"+padArray[i]+".checked")) {
			padPid = padArray[i];
			padQty = eval("document.quoteWizard.padRolls_"+padArray[i]+".value");
			break;
		}
	}
	// Get the moulding info
	for(i=0;i<mouldingArray.length;i++) {
		if(eval("document.quoteWizard.ckMoulding_"+mouldingArray[i]+".checked")) {
			mQty += parseInt(eval("document.quoteWizard.mouldingItems_"+mouldingArray[i]+".value"));
		}
	}
	if(padPid == null) {
		padPid = 0;
		padQty = 0;
	}
	// Check field #1 to see if user has quoted anything
	lamQty = eval("document.quoteWizard.boxTotal_"+p1+".value");
	accQty = eval("document.quoteWizard.accTotal_"+p1+".value");
	if(lamQty == 0 && padQty == 0 && mQty == 0 && accQty == 0) {
		alert("Cannot calculate shipping costs.  No products have been selected.");
	} else {
		// put up processing text
		document.getElementById('shipMsg').innerHTML = '<i>Please wait...getting shipping quote...</i>';
		if(genQuotePg) {
			lamQty = eval("document.quoteWizard.boxTotal_"+p1+".value")+","+
			         eval("document.quoteWizard.boxTotal_"+p2+".value")+","+
					 eval("document.quoteWizard.boxTotal_"+p3+".value")+","+
					 eval("document.quoteWizard.boxTotal_"+p4+".value")+","+
					 eval("document.quoteWizard.boxTotal_"+p5+".value")+","+
					 eval("document.quoteWizard.boxTotal_"+p6+".value");
		    grandTTL = eval("document.quoteWizard.grandTotal_"+p1+".value")+","+
			           eval("document.quoteWizard.grandTotal_"+p2+".value")+","+
					   eval("document.quoteWizard.grandTotal_"+p3+".value")+","+
					   eval("document.quoteWizard.grandTotal_"+p4+".value")+","+
					   eval("document.quoteWizard.grandTotal_"+p5+".value")+","+
					   eval("document.quoteWizard.grandTotal_"+p6+".value");
		} else {
			lamQty = eval("document.quoteWizard.boxTotal_"+p1+".value");
			grandTTL = eval("document.quoteWizard.grandTotal_"+p1+".value");
		}
		//build url
		url = baseUrl+"&zip="+zip+"&padPid="+padPid+"&padQty="+padQty+"&mQty="+mQty+"&lamPid="+lamPids+"&lamQty="+lamQty+"&grandTTL="+grandTTL;
		// make the request
		makeRequest(url);
	}
}

/**
 * Builds the xml ajax request
 */
function makeRequest(url) {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	xmlHttp.onreadystatechange=useContents;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

/**
 * Returns the ajax handler object
 */
function GetXmlHttpObject(handler) { 
	var objXMLHttp=null
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

/**
 * Handler function that recieves answer from ajax obj and utilizes it
 */
function useContents() {
	var discountFactor = 1;
	var free = false;
        var discount = false;
        var isKronoswiss = document.quoteWizard.ks.value;
        var isInterFlooring = document.quoteWizard.interflooring.value;
        var isFreeShipping = document.quoteWizard.isfreeshipping.value;
        if(isFreeShipping == "yes") {
        	free = true;
        }
		/*if(parseInt(eval("document.quoteWizard.grandTotal_"+p1+".value")) >= 2000 && isKronoswiss == "no" && isInterFlooring == "no") {
			free = true;
		}*/
        if(isKronoswiss == "no" && isInterFlooring == "no") {
        	discountFactor = 2;
            discount = true;
        }
	
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			shipping = xmlHttp.responseText;
			if(shipping.indexOf("ERROR") != -1) {
				alert(shipping);
				document.quoteWizard.ttlShip.value = '0.00'
				eval("document.quoteWizard.shippingTotal_"+p1+".value = '0'");
			} else {
				//alert(xmlHttp.responseText);
				amts = shipping.split(",");
				if(free) {
					document.quoteWizard.ttlShip.value = '0.00';
				} else {
					document.quoteWizard.ttlShip.value = formatCurrency(amts[0]/discountFactor);
				}
				if(amts.length > 1) { 
					eval("document.quoteWizard.shippingTotal_"+p1+".value = '"+formatCurrency(amts[0]/discountFactor)+"'");
					eval("document.quoteWizard.shippingTotal_"+p2+".value = '"+formatCurrency(amts[1]/discountFactor)+"'");
					eval("document.quoteWizard.shippingTotal_"+p3+".value = '"+formatCurrency(amts[2]/discountFactor)+"'");
					eval("document.quoteWizard.shippingTotal_"+p4+".value = '"+formatCurrency(amts[3]/discountFactor)+"'");
					eval("document.quoteWizard.shippingTotal_"+p5+".value = '"+formatCurrency(amts[4]/discountFactor)+"'");
					eval("document.quoteWizard.shippingTotal_"+p6+".value = '"+formatCurrency(amts[5]/discountFactor)+"'");
				} else {
					if(free) {
						eval("document.quoteWizard.shippingTotal_"+p1+".value = '0.00'");
					} else {
						eval("document.quoteWizard.shippingTotal_"+p1+".value = '"+formatCurrency(amts[0]/discountFactor)+"'");
					} 
				}
				calculateGrandTotals(document.quoteWizard);
				if(free) {
					document.getElementById('shipMsg').innerHTML = '<font color=red><b><i>Shipping is free on this item!</i></b></font>';
				} else {
					if(discount) {
						document.getElementById('shipMsg').innerHTML = '<font color=red><b><i>1/2 off Shipping Sale.  You saved $'+formatCurrency(amts[0]/discountFactor)+' on shipping!</i></b></font>';
                    } else {
					    document.getElementById('shipMsg').innerHTML = '<i>Shipping calculation done!</i>';
					}
				}
			}
        } else {
            alert('ERROR :: Unable to calculate shipping.');
        }
    }
}

