var mssgHeader = "YOUR ORDER STATUS: \n";
var mssgGiftQty = " - Please select at least one Gift Pack \n";
var mssgGiftTotal = " - Gift Total not yet calculated \n";
var mssgShipTotal = " - Shipping Total not yet calculated \n";
var mssgOrderTotal = " - Your Total Order not yet calculated \n"; 
var mssgZone = " - Please select a Delivery State \n";

var zone;
var giftCount, giftCountRow, giftCost, giftTotal, giftQty, giftShip, shipTotal, certPrice, certRow; 

function giftCert()   {
	/*  IE browser has a problem with this approch. I implemented a work around as seen below
	in the calculator loop/

	certPrice = document.orderinfo.Price_giftCert.options[document.orderinfo.Price_giftCert.selectedIndex].value;
	//certLocation = "Price_gft" + document.orderinfo.certRowNumb.value;
		//alert("certLocation: " + certLocation + "   gift price: " + certPrice);
	document.orderinfo.eval("Price_gft" + document.orderinfo.certRowNumb.value).value = certPrice;
	var giftCertLoc = eval("Price_gft" + document.orderinfo.certRowNumb.value);
		//alert("giftCertLoc: " + giftCertLoc);
	giftLoc = eval("Price_gft" + document.orderinfo.certRowNumb.value);
		//alert("giftLoc: " + giftLoc);
	document.orderinfo.giftLoc.value = certPrice;
		//alert("certPrice: " + certPrice);
	*/
	calcOrder();
}

function calcOrder()  {
		//alert("got here");
	giftTotal = 0;
	giftQty = 0;
	giftCost = 0;
	giftShip = 0;
	giftCount = 0;
	giftCountRow = 0;
	shipTotal = 0;
	var numberRecords = document.orderinfo.numRecords.value;
			//alert("numrecords=" + numberRecords);
	for (iCounter=1; iCounter <= numberRecords; iCounter++)  {
			//alert("rowcount=" + iCounter);
		giftCountRow = "GiftQty"+iCounter;
			//alert("giftCountRow= " + giftCountRow);
		
			//alert("giftCountRow = " + giftCountRow);
		giftCount = parseFloat(eval("document.orderinfo." + giftCountRow).value);
		if (giftCount > 0)  { 
			giftQty = giftQty + giftCount 
			
			//alert("giftCount= " + giftCount);
			zone = findZone();
			
			//  fake around for IE browsers and the gift card ...
			if (parseFloat(eval("document.orderinfo.Price_gft" + iCounter).value) == 999)  {
			giftCost = document.orderinfo.Price_giftCert.options[document.orderinfo.Price_giftCert.selectedIndex].value;
			}
			else  {
			giftCost = parseFloat(eval("document.orderinfo.Price_gft" + iCounter).value); 
			}
			
			if (zone == "East")  {
				giftShip = parseFloat(eval("document.orderinfo.East_Zone_shp" + iCounter).value); 
			}
			if (zone == "West") {
				giftShip = parseFloat(eval("document.orderinfo.West_Zone_shp" + iCounter).value);
			}
				//alert("threedayship: " + document.orderinfo.threedayship.checked);
			if (zone == "West" & document.orderinfo.threedayship.checked == true) {
				giftShip = parseFloat(eval("document.orderinfo.West_Zone_3day_shp" + iCounter).value);
			}
			giftTotal = giftTotal + giftCount * giftCost;
			shipTotal = shipTotal + giftCount * giftShip;
		}
		else  {
			zone = findZone();
		}
	}
	
		//alert("giftTotal= " + giftTotal);
	if (giftQty > 0)  {
		if (giftQty == 1) {  gWord = "Pack"; 
		}
		else  { gWord = "Packs";
		}
		mssgGiftQty = "You have selected " + giftQty + " Gift " + gWord  + "\n";
	}
	else { mssgGiftQty = " - Please select at least one Gift Pack \n";
	}
	
	if (giftTotal > 0)  {
			mssgGiftTotal = "Your Gift total will be $" + formatCurrancy(giftTotal) + "\n";
	}
	else {  
		mssgGiftTotal = " - Gift total not yet calculated \n";
	}
	
	if (shipTotal > 0)  {
			mssgShipTotal = "Your Shipping total will be $" + formatCurrancy(shipTotal) + "\n";
	}
	else {  
		mssgShipTotal = " - Shipping total not yet calculated \n";  
	}
	
	if (giftTotal > 0 & shipTotal > 0)  {
		orderTotal = giftTotal + shipTotal;
		mssgOrderTotal = "Your TOTAL ORDER will be $" + formatCurrancy(orderTotal) + "\n";
	}
	else  {
		mssgOrderTotal = " - Your Total Order not yet calculated \n"; 
	}
	displayMessage();	
}

function findZone()  {
	if (document.orderinfo.deltobill.checked == true)  {
		if(document.orderinfo.skipBilling.value == "yes")  {
			gState = document.orderinfo.zRegion.value;
		}
		else {
			gState = document.orderinfo.zRegion.options[document.orderinfo.zRegion.selectedIndex].value;
		}
	}
	else  {
		gState = document.orderinfo.yRegion.options[document.orderinfo.yRegion.selectedIndex].value;
	}
	
	if (gState == "ME" || gState == "NH" || gState == "VT" || gState == "MA" || gState == "CT" || gState == "RI" || gState == "NY" || gState == "PA" || gState == "MD" || gState == "DE" || gState == "NJ" || gState == "VA" || gState == "NC" || gState == "SC" || gState == "GA" || gState == "DC" || gState == "FL" || gState == "TN" || gState == "WV" || gState == "KY" || gState == "OH" || gState == "IN" || gState == "MS" || gState == "WI" || gState == "AL" || gState == "MI" || gState == "IL")  { zone = "East";
	}
	else { zone = "West";
	}
	mssgZone = "Delivery will be to the " + zone + " Zone \n";
	
	if (gState  == "none")  {
		zone = "none";
		mssgZone = " - Please select a Delivery State \n";
	}
	return zone;
}

function displayMessage()  {
	document.orderinfo.ordersummry.disabled=false
	mssgToPost = mssgHeader + mssgGiftQty  + mssgGiftTotal + mssgZone + mssgShipTotal  + "\n" + mssgOrderTotal;
		//alert(mssgToPost);
	document.orderinfo.ordersummry.value = mssgToPost;
	document.orderinfo.ordersummry.disabled=true
}

function formatCurrancy(value)  {
	/* Generic routine for formatting numbers. See page 569 of
	the JavaScript Bible */ 
	var decplaces = 2;
	var strValue = "" + Math.round(eval(value)*Math.pow(10, decplaces));
	while(strValue.length <= decplaces)  {
		strValue = "0" + strValue;
	}
	var decpoint = strValue.length - decplaces;
	value = strValue.substring(0, decpoint) + "." + strValue.substring(decpoint, strValue.length);
	return value;  //return the value to the calling routine
}