//
// check1(theForm) will validate certain fields in theForm and will
// popup a text box informing the user what values are not filled in.
//
// <input type="button" name="Submit" value="Send" onClick="check(this.Form)">
//
function check1(theForm) {
	var NotFilledIn = "";
	var count = 0;
	if  (theForm.BillingFullName.value =="") {
		NotFilledIn += "\n" + ++count + ". Billing Full Name";
	}
	if (theForm.BillingAddress1.value =="") {
		NotFilledIn += "\n" + ++count + ". Billing Address 1";
	}
	if (theForm.BillingCity.value =="") {
		NotFilledIn += "\n" + ++count + ". Billing City";
	}
	if (theForm.BillingState.selectedIndex == 0) {
		NotFilledIn += "\n" + ++count + ". Billing State";
	}
	if (theForm.BillingZip.selectedIndex == 0) {
		NotFilledIn += "\n" + ++count + ". Billing Zip";
	}
	if (theForm.BillingEmail.selectedIndex == 0) {
		NotFilledIn += "\n" + ++count + ". Billing E-Mail";
	}
	if  (theForm.ShippingFullName.value =="") {
		NotFilledIn += "\n" + ++count + ". Shipping Full Name";
	}
	if (theForm.ShippingAddress1.value =="") {
		NotFilledIn += "\n" + ++count + ". Shipping Address 1";
	}
	if (theForm.BillingCity.value =="") {
		NotFilledIn += "\n" + ++count + ". Shipping City";
	}
	if (theForm.ShippingState.selectedIndex == 0) {
		NotFilledIn += "\n" + ++count + ". Shipping State";
	}
	if (theForm.ShippingZip.selectedIndex == 0) {
		NotFilledIn += "\n" + ++count + ". Shipping Zip";
	}
	if (theForm.ShippingEmail.selectedIndex == 0) {
		NotFilledIn += "\n" + ++count + ". Shipping E-Mail";
	}

	if (count == 0) {
		theForm.submit()
	}
	else if (count == 1) {
			alert("\n\nPlease fill in the following item:\t\n" + NotFilledIn + "\n\n\nFields marked with * are required.")
	}
	else {
			alert("\n\nPlease fill in the following " + count + " items:\t\n" + NotFilledIn + "\n\n\nFields marked with * are required.")
	}
	return true;
}


//
// check2(theForm) will validate certain fields in theForm and will
// popup a text box informing the user the first field that is not filled in
// and will put focus on that field.
//
// <FORM NAME="form1" onSubmit="return check2(this)" METHOD="POST" ACTION="/cgi-bin/">
//
function check2(theForm) {
	if  (theForm.fullName.value =="") {
		alert("Please enter your Full Name.");
		theForm.fullName.focus();
		return false;
	}
	if (!isEmail(theForm.email)) {
		alert("Please enter a correct E-mail Address.");
		theForm.email.focus();
		return false;
	}
	if (theForm.phone.value =="") {
		NotFilledIn += "\n" + ++count + ". Phone Number";
	}
	if (theForm.location.selectedIndex == 0) {
		NotFilledIn += "\n" + ++count + ". Location";
	}
	if (theForm.cubicle.value =="") {
		NotFilledIn += "\n" + ++count + ". Cubicle";
	}
	if (theForm.status_open.selectedIndex == 0) {
		NotFilledIn += "\n" + ++count + ". Status";
	}
	if (theForm.problem_description.value =="" ) {
		NotFilledIn += "\n" + ++count + ". Problem Description";
	}
	if (count == 0) {
		theForm.submit()
	}
	else if (count == 1) {
			alert("\n\nPlease fill in the following item:\t\n" + NotFilledIn + "\n\n\nFields marked with * are required.")
	}
	else {
			alert("\n\nPlease fill in the following " + count + " items:\t\n" + NotFilledIn + "\n\n\nFields marked with * are required.")
	}
	return true;
}


//
// highlight(state) Highlights a form element and puts focus to it when the mouse is moved over it.
//
// <FORM NAME="mainform" onmouseover="highlight(0);" onmouseout="highlight(1);">
//
clr=new Array('thistle','white','silver');
function highlight(state) {
	element=event.srcElement;
	etag=element.tagName;
	if (etag=='INPUT' || etag=='SELECT' || etag=='TEXTAREA' ) {
		etype=element.type;
		if ((etype=='submit' || etype=='reset' || etype=='button') && state==1) state=2;
		element.style.backgroundColor=clr[state];
		if (!element.disabled && etag!='SELECT') {element.focus();}
	}
}



/****************************************************************************/
/************************* UTILITY FUNCTIONS ********************************/
/****************************************************************************/

//
// isEmail(elm) validates elm to be a pseudo correct e-mail address.
// It makes sure elm isn't blank, contains a "@" and a "."
//
function isEmail(elm) {
	if (elm.value.indexOf("@") != "-1" &&
		elm.value.indexOf(">") != "-1" &&
		elm.value != "")
		{return true;}
	else {return false;}
}


//-- JavaScript code written by Alan Simpson - www.coolnerds.com
//-- Global Variables
var RowsInForm = 17          //How many line items will be in the order form?
var ProductsInList = 17     //How many products in your product list?
var SalesTaxRate = 0.0525   //Set to sales tax rate in decimal. e.g. 0.0775 is 7.75%.
var TaxableState = "WI"     //Set to name of state you charge sales tax in.
var ProdSubscript = 0       //Identifies subscript of selected product in current row.

//-- Function to create a new empty array that starts at 1.
function MakeArray(n) {
   this.length = n
   for (var i=1;i<=n;i++) {this[i]=0}
   return this
}

//-- Function to create a new, empty array that starts at zero.
function BuildZeroArray(n) {
   this.length = n
   for (var i=0;i<=n;i++) {this[i]=0}
   return this
}

//-- Defines a custom object named prodobj (Product Object).
//-- An array of these objects will act as our product/price list.
function prodobj(name, unitprice) {
   this.name = name
   this.unitprice = unitprice
}

//-- Defines a new custom object named ordobj (Order Object).
//-- Will house data displayed in order form line items.
function ordobj(prodsub, qty, unitprice, extprice) {
   this.prodsub = prodsub
   this.qty = qty
   this.unitprice = unitprice
   this.extprice = extprice
}

//-- Updates current row in order array and form.
function updateRow(rownum){
   PrintSubscript=rownum
   ordData[rownum].prodsub=PrintSubscript
   var exeLine='tempqty=document.stockings.qty'+rownum+'.value'
   eval(exeLine)
   ordData[rownum].qty=tempqty
   ordData[rownum].unitprice=prodlist[PrintSubscript].unitprice
   ordData[rownum].extprice=(ordData[rownum].qty)*ordData[rownum].unitprice
   var exeLine='document.stockings.extprice'+rownum+'.value=currency(ordData['+rownum+'].extprice,10)'
   eval(exeLine)
   updateTotals()
}

//-- Updates the totals in the lower part of order details.
function updateTotals() {
   var subqty = 0
   var subtotal = 0
   for (var i=1;i<=RowsInForm;i++) {
	  subqty=subqty+parseInt(ordData[i].qty)
   	  if (subqty == null || subqty == "")
	    subqty = 0
// 10.20.05 added to change the shipping to $4 for the first stocking, 2 each additional
		shipping = 2+(subqty*2)
//	  shipping = subqty*2
	  subtotal=subtotal+ordData[i].extprice
// alert(subqty)
   }
   document.stockings.subtotal.value = currency(subtotal,10)
   salestax=0
   if (document.stockings.Taxable.checked) {
		salestax = SalesTaxRate*subtotal

//      salestax = SalesTaxRate*subtotal
   }
   document.stockings.salestax.value = currency(salestax,10)
   document.stockings.shipping.value = currency(shipping,10)

   document.stockings.grandtotal.value = currency(subtotal+salestax+shipping,10)
}

//-- Copies the "Bill To" information to the "Ship To" information.
function copyAddress() {
   document.stockings.ShippingFullName.value=document.stockings.BillingFullName.value
   document.stockings.ShippingAddress1.value=document.stockings.BillingAddress1.value
   document.stockings.ShippingAddress2.value=document.stockings.BillingAddress2.value
   document.stockings.ShippingCity.value=document.stockings.BillingCity.value
   document.stockings.ShippingState.value=document.stockings.BillingState.value
   document.stockings.ShippingZip.value=document.stockings.BillingZip.value
   document.stockings.ShippingEmail.value=document.stockings.BillingEmail.value
   document.stockings.ShippingPhone.value=document.stockings.BillingPhone.value
}

//-- Shows number in $$xxx,xxx.xx format and pads left side with blanks.
function currency(anynum,width) {
   anynum=eval(anynum)
   workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
   if (workStr.indexOf(".")==-1){workStr+=".00"}
   dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
   pStr=workStr.substr(workStr.indexOf("."))
   while (pStr.length<3){pStr+="0"}

   //--- Adds comma in thousands place.
   if (dNum>=1000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
   }

   //-- Adds comma in millions place.
   if (dNum>=1000000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
   }
   retval=dStr+pStr 
   if (anynum < 0) {
      retval=retval.substring(1,retval.length)
      retval="("+retval+")"        
   }
   retval = "$"+retval
   //--Pad with leading blanks to better align numbers.
   while (retval.length<width){retval=" "+retval}

   return retval
}


//Create a new array named prodlist with six elements.
prodlist = new BuildZeroArray(ProductsInList)   //Create empty product list array.
//-- JavaScript programmers: The array below defines products and unit prices.
//-- The only comma allowed in each line is the one that separates the product 
//-- name from its unit price.  Do not change first item (prodobj[0]).
prodlist[0] = new prodobj('-none-',0)
prodlist[1] = new prodobj('3 Color-Santa-Red',25)
prodlist[2] = new prodobj('3 Color-Angel-White',25)
prodlist[3] = new prodobj('3 Color-Gingerbread Boy-Red',25)
prodlist[4] = new prodobj('3 Color-Gingerbread Boy-Green',25)
prodlist[5] = new prodobj('3 Color-Reindeer-Green',25)
prodlist[6] = new prodobj('3 Color-Reindeer-Red',25)
prodlist[7] = new prodobj('3 Color-Teddy Bear-Green',25)
prodlist[8] = new prodobj('3 Color-Teddy Bear-Red',25)
prodlist[9] = new prodobj('3 Color-All Over Cow',25)
prodlist[10] = new prodobj('4 Color-Angel-White',30)
prodlist[11] = new prodobj('4 Color-Santa-Red',30)
prodlist[12] = new prodobj('4 Color-Reindeer-Green',30)
prodlist[13] = new prodobj('4 Color-Teddy Bear--White',30)
prodlist[14] = new prodobj('4 Color-Gingerbread Boy-Red',30)
prodlist[15] = new prodobj('4 Color-Cow and Barn-Red',30)
prodlist[16] = new prodobj('4 Color-Cow and Barn-Burgandy',30)
prodlist[17] = new prodobj('4 Color-Cow and Barn-Red with Green',30)

//-- JavaScript programmers- The ProductsInList variable defined in the head of 
//-- this page must match the highest-numbered item in this array. In this sample
//-- page you can see that the ProductsInList variable is initially set to 10, which
//-- matches the last subscript in the array above.

//-- Creates a new array named ordData, which will stores order form line items.
ordData = new MakeArray(RowsInForm)
for (var i=1; i<= RowsInForm; i++) {
   ordData[i] = new ordobj(0,0,0,0)
}
