/*
mailorder.js

the JS functions on this page are included on mailorder.htm
*/

//this function checks the order form to count how many bagels were ordered so far
function getCountBagelsOrdered () {
	var numBagelsSoFar=0;
	for(i=0; i<document.form1.elements.length; i++) 	{
		if (document.form1.elements[i].name.match(/^qty/) &&  isNaN(eval(document.form1.elements[i].value))==false) {
			numBagelsSoFar += eval(document.form1.elements[i].value);
		}
	}
	return numBagelsSoFar;
}

//this function is called when the form is submitted.  
//it checks that the user doesn't still need to add more bagels to the order (in order to complete the baker's dozens)
function checkTotals () {

	//Get the total number of bagels ordered so far
	var numBagelsSoFar=getCountBagelsOrdered();
	
	//figure out totals and pricing
	var remainder = numBagelsSoFar % 13;
	if (remainder) 
		var numBakersDozens = ((numBagelsSoFar - remainder ) / 13) + 1;
	else var numBakersDozens = ((numBagelsSoFar - remainder ) / 13);
	
	var needmore = (numBakersDozens * 13) - numBagelsSoFar;
	
	if (numBakersDozens < 2) {
		alert ("The minimum for mail orders is 2 baker's dozens (26 bagels).");//\n\nPlease add "+needmore+" bagels to your order.");
		return false;
	}
	
	//if need to add more, alert message and don't allow form to submit.
	if (needmore > 0) {		
		if (needmore==1) numMoreBagels = "1 more bagel";
		else numMoreBagels = needmore+" more bagels";
		alert ("Please add "+numMoreBagels+" to your order \nin order to complete the "+numBakersDozens+" baker's dozens."); 
		return false;
	}
	else return true;
	
}

//this function updates the totals displayed on the bottom of the order form page
//the function is called every time the user fills in a qty for any bagel type.
function updateTotals() {
	
	//Get the total number of bagels ordered so far
	var numBagelsSoFar=getCountBagelsOrdered();
	
	//figure out totals and pricing
	var remainder = numBagelsSoFar % 13;
	if (remainder) 
		var numBakersDozens = ((numBagelsSoFar - remainder ) / 13) + 1;
	else var numBakersDozens = ((numBagelsSoFar - remainder ) / 13);
	
	var needmore = (numBakersDozens * 13) - numBagelsSoFar;
	
	var costPerBakersDozen = document.form1.costPerBakersDozen.value;
	var cost = "$"+CurrencyFormatted(costPerBakersDozen * numBakersDozens);
	
	//Display numBagelsSoFar in the "Total Bagels Ordered: " text display
	document.getElementById("numBagels").innerHTML = numBagelsSoFar; 
	
	//Display how many bakers dozens so far
	document.getElementById("numBakersDozens").innerHTML = numBakersDozens;

	//Display the cost so far
	document.getElementById("cost").innerHTML = cost;

	//Display a msg re how many more to add to order
	if (numBakersDozens==1) {
		var msg = "<marquee><blink>The minimum for mail orders is 2 baker's dozens (26 bagels).<br />Please add "+(needmore+13)+" bagels to your order to complete the 2 baker's dozens.</blink></marquee>"; 
	}
	else if (needmore > 0) {
		if (needmore==1) numMoreBagels = "1 more bagel";
		else numMoreBagels = needmore+" more bagels";
		var msg = "<marquee><blink>Please add "+numMoreBagels+" to your order to complete the "+numBakersDozens+" baker's dozens.</blink></marquee>"; 
	}
	else var msg=" "; 
	document.getElementById("msg").innerHTML = msg;
	
}

//function to format money amount to 2 decimal places.  (from http://www.web-source.net/web_development/currency_formatting.htm)
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;
}

//used for popup windows
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

//this function is called when the "sameasbilling" checkbox is checked.
//function fills in the shipping fields with the data from the billing fields.
function fillInFields() {
	//alert ("OK1..."+eval(document.checkoutForm.shipstate.options[document.checkoutForm.shipstate.selectedIndex])+"\n\nOK2..."+
			//eval(document.checkoutForm.state.options[document.checkoutForm.state.selectedIndex]));
	
	if (document.checkoutForm.sameasbilling.checked==true) {
		document.checkoutForm.shipname.value = document.checkoutForm.fname.value + " " + document.checkoutForm.lname.value;
		document.checkoutForm.shipaddress.value = document.checkoutForm.address.value;
		document.checkoutForm.shipaddress1.value = document.checkoutForm.address1.value;
		document.checkoutForm.shipcity.value = document.checkoutForm.city.value;
		
		var billingStateIndex = document.checkoutForm.state.options[document.checkoutForm.state.selectedIndex].index;
		document.checkoutForm.shipstate.options[billingStateIndex].selected=true;
		
		
		//var billingStateIndex = document.checkoutForm.state.options[document.checkoutForm.state.selectedIndex];
		//document.checkoutForm.shipstate.options[billingStateIndex].selected=true;
			
		document.checkoutForm.shipzip.value = document.checkoutForm.zip.value;
	}
}


//this functions makes sure that all form fields are filled in.
//function is called when form is submitted
function validateForm() {
 
 var errMsg="";
		
 if (document.checkoutForm.name.value=="")  {errMsg += "Please enter a valid billing billing name.\n";}
 if (document.checkoutForm.address.value=="")  {errMsg += "Please enter a valid billing address.\n";}
 if (document.checkoutForm.city.value=="")  {errMsg += "Please enter a valid billing city.\n";}
 //if (document.checkoutForm.zip.value=="")  {errMsg += "Please enter a valid billing zipcode.\n";}
 if (! isZip(document.checkoutForm.zip.value) )  {errMsg += "Please enter a valid billing zipcode.\n";}
 if (document.checkoutForm.phone.value=="")  {errMsg += "Please enter a valid phone number.\n";}
 if (document.checkoutForm.ccNum.value=="")  {errMsg += "Please enter a valid credit card number.\n";}
 if (document.checkoutForm.pass.value=="")  {errMsg += "Please enter a valid password.\n";}
 if (document.checkoutForm.pass1.value=="")  {errMsg += "Please fill in the password confirmation field.\n";}
 
 var emailreg = new RegExp("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$"); 
 if (! emailreg.test(document.checkoutForm.email.value)) {errMsg += "Please enter a valid email address.\n";}
 
 if ( document.checkoutForm.pass.value!=document.checkoutForm.pass1.value) {errMsg += "The password does not match the password confirmation.\n";}
 
 if (document.checkoutForm.sameasbilling.checked==false) {
		if (document.checkoutForm.shipname.value=="") { errMsg+= "Please enter a valid shipping name.\n";}
		if (document.checkoutForm.shipaddress.value=="") { errMsg+= "Please enter a valid shipping address.\n";}
		if (document.checkoutForm.shipcity.value=="") { errMsg+= "Please enter a valid shipping city.\n";}
		//if (document.checkoutForm.shipzip.value=="") { errMsg+= "Please enter a valid shipping zipcode.\n";}
		if (! isZip(document.checkoutForm.shipzip.value) )  {errMsg += "Please enter a valid shipping zipcode.\n";}
	}
 
 if (! errMsg=="") {
 		alert ("ERROR:  Unable to process form submission.\n\n"+errMsg);
		return false;
 }
 else return true;

}


//Validates a zip code based on both US zip code schemes: 12345-1234 or 12345
//copied from http://www.dreamincode.net/code/snippet70.htm
function isZip(s) 
{

     // Check for correct zip code
     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);

     if (!reZip.test(s)) {
          //alert("Zip Code Is Not Valid");
          return false;
     }

return true;
}



