/////////////////////////////////////
//    parseDec(val,places,sep)     //
//---------------------------------//
// Function taken from :  http://www.taylor.org/~patrick/javascript/lib/decimal_parse/
// Basic decimal conversion etc.   //
/////////////////////////////////////
function parseDec(val,places,sep)
{

	// This function takes two arguments:
	//   (string || number)  val
	//            (integer)  places
	//             (string)  sep
	// val is the numeric string or number to parse
	// places represents the number of decimal
	// places to return at the end of the parse.
	// sep is an optional string to be used to separate
	// the whole units from the decimal units (default: '.')

	val = '' + val;
		// Implicitly cast val to (string)

	if (!sep) {
		sep = '.';
		// If separator isn't specified, then use a decimal point '.'
	}

	if (!places) { places = 0; }
	places = parseInt(places);
		// Make sure places is an integer

	if (!parseInt(val)) {
		// If val is null, zero, NaN, or not specified, then
		// assume val to be zero.  Add 'places' number of zeros after
		// the separator 'sep', and then return the value.  We're done here.
		val = '0';
		if (places > 0) {
			val += sep;
			while (val.substring((val.indexOf(sep))).length <= places) {
				val += '0';
			}
		}
		return val;
	}

	if ((val.indexOf('.') > -1) && (sep != '.')) {
		val = val.substring(0,val.indexOf('.')) + sep + val.substring(val.indexOf('.')+1);
			// If we're using a separator other than '.' then convert now.
	}

	if (val.indexOf(sep) > -1) {
		// If our val has a separator, then cut our value
		// into pre and post 'decimal' based upon the separator.
		pre = val.substring(0,val.indexOf(sep));
		post = val.substring(val.indexOf(sep)+1);
	} else {
		// Otherwise pre gets everything and post gets nothing.
		pre = val;
		post = '';
	}

	if (places > 0) {
		// If we're dealing with a decimal then...

		post = post.substring(0,(places+1));
			// We care most about the digit after 'places'

		if (post.length > places) {
			// If we have trailing decimal places then...

			//alert (parseInt(post.substring(post.length - 1)));

			if ( parseInt(post.substring(post.length - 1)) > 4 ) {
				post = '' + Math.round(parseInt(post) / 10);
				//post = '' + post.substring(0,post.length - 2) + (1/Math.pow(10,places));
				//post = ('' + post.substring(0,post.length - 2)) + (parseInt(post.substring(post.length - 1)) + 1);
			} else {
				post = '' + Math.round(parseInt(post));
			}
		}

		if (post.length > places) {
			post = '' + Math.round(parseInt(post.substring(0,places)));
		} else if (post.length < places) {
			while (post.length < places) {
				post += '0';
			}
		}

	} else {

		if (parseInt((post.substring(0,1))) > 4) {
			pre = '' + (parseInt(pre) + 1);
		} else {
			pre = '' + (parseInt(pre));
		}
		post = '';
	}

	sep = (post.length > 0) ? sep : '';
		// Should we use a separator?

	val = pre + sep + post;
		// Rebuild val

	return val;
}


function isInt(num)
{
   if (isNaN(num))
      return false;
   if (num == "")
      return false;
	else
	   return (num.indexOf(".") == -1);
}

function validate(wch)
{
	var msg = "";
	if(document.orderForm.BillName.value == ""){msg+="Billing Name \n";}
	if(document.orderForm.BillAddress.value == ""){msg+="Billing Address \n";}
	if(document.orderForm.BillCity.value == ""){msg+="Billing City \n";}
	if(document.orderForm.BillState.value == ""){msg+="Billing State \n";}
	if( (document.orderForm.BillZip5.value.toString().length < "5") || (isNaN(document.orderForm.BillZip5.value)) ){msg+="Billing Zip-code \n";}
	if( (document.orderForm.BillZip4.value != "") && (  (document.orderForm.BillZip4.value.toString().length < "4") || (isNaN(document.orderForm.BillZip4.value)) ) )
	   {msg+="Extended Billing Zip-code \n";}
	if(document.orderForm.BillPhone.value == ""){msg+="Billing Phone \n";}
	if(document.orderForm.DelivName.value != "")
		{
			if(document.orderForm.DelivAddress.value == ""){msg+="Delivery Address \n";}
			if(document.orderForm.DelivCity.value == ""){msg+="Delivery City \n";}
			if(document.orderForm.DelivState.value == ""){msg+="Delivery State \n";}
			if( (document.orderForm.DelivZip5.value.toString().length < "5") || (isNaN(document.orderForm.DelivZip5.value)) ){msg+="Delivery Zip-code \n";}
			if( (document.orderForm.DelivZip4.value != "") && (  (document.orderForm.DelivZip4.value.toString().length < "4") || (isNaN(document.orderForm.DelivZip4.value)) ) )
	         {msg+="Extended Delivery Zip-code \n";}

		}
		else
		{
			document.orderForm.DelivAddress.value = "";
			document.orderForm.DelivCity.value = "";
			document.orderForm.DelivState.value = "";
			document.orderForm.DelivZip5.value = "";
		}

	//Videos
	for (i = 1; i <= 5; i++)
	{
		var holder = eval ("document.orderForm.VideoQty" + i + ".value");
		var holder1 = eval ("document.orderForm.Video" + i + ".value");
		if (holder1 == "" && holder != "")
		   msg+='Video ' + i + ' Type Number \n';
		if ( (holder1 != "")  && (!isInt(holder) || (holder <= 0) ) )
		   msg+='Video ' + i + ' Qty \n';
   }

	//Brochures
	for (i = 1; i <= 10; i++)
	{
		var holder = eval ("document.orderForm.Brochure" + i + ".value");
		if ( (holder != '') && (!isInt(holder) || (holder <= 0) ) )
		   msg+='Brochure ' + i + ' Qty \n';
   }

	//Bookmarks
	if ( document.orderForm.Bookmark1.value != "" && ( !isInt(document.orderForm.Bookmark1.value) || (document.orderForm.Bookmark1.value <= 0) ) )
	  {msg+="Bookmark 1 Qty \n";}
	if ( document.orderForm.Bookmark2.value != "" && ( !isInt(document.orderForm.Bookmark2.value) || (document.orderForm.Bookmark2.value <= 0) ) )
	  {msg+="Bookmark 2 Qty \n";}


	//Discount Packages
	for (i = 1; i <= 6; i++)
	{
		var holder = eval("document.orderForm.DiscQty"+i+".value");
		if ( (holder != '')  && ( !isInt(holder) || (holder <= 0) ) )
	   msg+='Discount Packages ' + i + ' Qty \n';
	}


	if(msg != "")
	{
		alert("Please fix the following errors:\n"+msg);
	}
	else
	{
		calcTotals();
		if (document.orderForm.GrandTotal.value == "0")
		   alert ("Please enter an order.");
		else
		{
	      if (wch==2)
	         {document.orderForm.submit();}
		   if (wch==3)
	         {printOrderForm();}
	   }
	}
}

function calcTotals ()
{
   var grandtotal = 0;
   var tmp = 0;
	var tmpTotal = 0;

   //Calc Video Totals
	for (i = 1; i <= 5; i++)
	{
		eval("tmp=parseFloat(document.orderForm.VideoQty" + i + ".value)*50;");
		if (isNaN(tmp))
		   tmp = 0;
		if (tmp == 0)
		   eval("document.orderForm.VideoTotal" + i + ".value='';");
		else
		   eval ("document.orderForm.VideoTotal" + i + ".value=tmp;");
		tmpTotal+=tmp;
	}
   document.orderForm.VideoSubTotal.value=parseDec(tmpTotal,2);
   document.orderForm.VideoGrandTotal.value=parseDec(tmpTotal,2);
   grandtotal += tmpTotal;



   //Brochures
   tmpTotal = 0;
   for (i = 1; i <= 10; i++)
   {
   	eval("tmp=parseFloat(document.orderForm.Brochure" + i + ".value)*30;");
   	if (isNaN(tmp))
   	   tmp = 0;
		tmpTotal+=tmp;
	}

   document.orderForm.BrochureSubTotal.value=parseDec(tmpTotal,2);
   document.orderForm.BrochureGrandTotal.value=parseDec(tmpTotal,2);
	grandtotal += tmpTotal;

	//Bookmarks
	tmpTotal = 0;
	var t1;
	var t2;

	if (document.orderForm.Bookmark1.value == "" || isNaN(document.orderForm.Bookmark1.value))
	   t1 = 0;
	else
	   t1 = parseFloat(document.orderForm.Bookmark1.value);

	if (document.orderForm.Bookmark2.value == "" || isNaN(document.orderForm.Bookmark2.value))
	   t2 = 0;
	else
	   t2 = parseFloat(document.orderForm.Bookmark2.value);

	tmpTotal+=(t1 + t2) * 15 ;
	document.orderForm.BookmarkSubTotal.value = parseDec(tmpTotal,2);
	document.orderForm.BookmarkGrandTotal.value = parseDec(tmpTotal,2);
	grandtotal += tmpTotal;


	//Middle Links
	tmpTotal = 0;
	if (document.orderForm.MiddleLink1.checked)
	   tmpTotal+=15;
	if (document.orderForm.MiddleLink2.checked)
	   tmpTotal+=40;
	document.orderForm.MiddleLinkGrandTotal.value = parseDec(tmpTotal,2);
	grandtotal += tmpTotal;


	//Discount Packages
	tmpTotal = 0;

	for (i = 1; i <= 6; i++)
	{
	   if (i == 1)
	      eval ("tmp=document.orderForm.DiscQty"+i+".value * 375;");
	   if (i == 2)
	      eval ("tmp=document.orderForm.DiscQty"+i+".value * 280;");
	   if (i == 3)
	      eval ("tmp=document.orderForm.DiscQty"+i+".value * 135;");
	   if (i == 4)
	      eval ("tmp=document.orderForm.DiscQty"+i+".value * 225;");
	   if (i == 5)
	      eval ("tmp=document.orderForm.DiscQty"+i+".value * 315;");
	   if (i == 6)
	   	eval ("tmp=document.orderForm.DiscQty"+i+".value * 50;");

	   if (isNaN(tmp))
	      tmp=0;
	   if (tmp == 0)
	      eval ("document.orderForm.DiscTotal"+i+".value = '';");
	   else
	      eval ("document.orderForm.DiscTotal"+i+".value = tmp;");
	   tmpTotal+=tmp;
	}

	document.orderForm.DiscSubTotal.value = parseDec(tmpTotal,2);
	document.orderForm.DiscGrandTotal.value = parseDec(tmpTotal,2);
	grandtotal += tmpTotal;

	//Grand Total
	document.orderForm.GrandTotal.value = parseDec(grandtotal,2);


}

function printOrderForm()
{
	var tablewindow = window.open();
	tablewindow.document.open();
	tablewindow.document.write(
	"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"
	+"<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><title>CEYA: Preview Order Form</title>"
	+"<link rel='stylesheet' type='text/css' href='http://www.uwplatt.edu/template_data/style.css'></link>"
	+"</head><body onload='javascript:window.print();'>"
	+"<table border='0' cellpadding='1' cellspacing='5' width='625'><tr><td width='50%' align='left' valign='top'>"
	+"<table border='0' cellpadding='0' cellspacing='1' width='100%'><tr><td>"
	+"<table border='0' cellpadding='1' cellspacing='5' width='100%' >"
	+"<tr><td class='contactTitle'>Billing Address</td></tr><tr><td>"
	+document.orderForm.BillName.value+"<br />"
	+document.orderForm.BillSchool.value+"<br />"
	+document.orderForm.BillAddress.value+"<br />"
	+document.orderForm.BillCity.value+", "+document.orderForm.BillState.value + " "
	+document.orderForm.BillZip5.value);

	if (document.orderForm.BillZip4.value != "")
	   tablewindow.document.write(" - " + document.orderForm.BillZip4.value);

	tablewindow.document.write(
	"<br /><br />"+document.orderForm.BillPhone.value+"<br /><br />"
	+"PO Number: "+document.orderForm.BillPONumber.value+"<br />"
	+"Tax Exempt Number: "+document.orderForm.BillTaxExempt.value
	+"</td></tr></table></td></tr></table></td><td width='50%' align='left' valign='top'>"
	+"<table border='0' cellpadding='0' cellspacing='1' width='100%'><tr><td>"
	+"<table border='0' cellpadding='1' cellspacing='5' width='100%'>"
	+"<tr><td class='contactTitle'>Delivery Address (if different)</td></tr><tr><td>");

	if (document.orderForm.DelivName.value != "")
	{
		tablewindow.document.write (
		   document.orderForm.DelivName.value+"<br />"
	      +document.orderForm.DelivSchool.value+"<br />"
	  		+document.orderForm.DelivAddress.value+"<br />"
			+document.orderForm.DelivCity.value+", "+document.orderForm.DelivState.value + " "
			+document.orderForm.DelivZip5.value);
			if (!document.orderForm.DelivZip4.value == "")
			   tablewindow.document.write(" - "+document.orderForm.DelivZip4.value);
	}
   else
   {
		tablewindow.document.write (
		   document.orderForm.BillName.value+"<br />"
	      +document.orderForm.BillSchool.value+"<br />"
	  		+document.orderForm.BillAddress.value+"<br />"
			+document.orderForm.BillCity.value+", "+document.orderForm.BillState.value + " "
			+document.orderForm.BillZip5.value);
			if (!document.orderForm.BillZip4.value == "")
			   tablewindow.document.write(" - "+document.orderForm.BillZip4.value);
	}
	tablewindow.document.write(
	"</td></tr></table></td></tr></table><br />"
	+"<table border='0' cellpadding='0' cellspacing='1' width='100%'><tr><td>"
	+"<table border='0' cellpadding='1' cellspacing='5' width='100%' >"
	+"<tr><td class='contactTitle'>Additional Information</td></tr><tr><td>"
	+"Date Order Placed: "+document.orderForm.DatePlaced.value+"<br />"
	+"Order Needed By: "+document.orderForm.DateNeeded.value+"<br />"
	+"Email Address: "+document.orderForm.email.value+"<br />"
	+"</td></tr></table></td></tr></table></td></tr><tr><td width='50%' align='left' valign='top'>"
	+"<table border='0' cellpadding='0' cellspacing='1' width='100%'><tr><td>"
	+"<table border='0' cellpadding='0' cellspacing='1' width='100%' >"
	+"<tr><td colspan='4' class='contactTitle'>Videos</td></tr>"
	+"<tr align='center'><td class='contactTitle'>Tape Number</td><td class='contactTitle'>Quantity</td><td class='contactTitle'>Price</td><td class='contactTitle'>Total</td></tr>"
	+"<tr align='center'><td>"+document.orderForm.Video1.value+"</td><td>"+document.orderForm.VideoQty1.value+"</td><td>$50.00</td><td>"+document.orderForm.VideoTotal1.value+"</td></tr>"
	+"<tr align='center'><td>"+document.orderForm.Video2.value+"</td><td>"+document.orderForm.VideoQty2.value+"</td><td>$50.00</td><td>"+document.orderForm.VideoTotal2.value+"</td></tr>"
	+"<tr align='center'><td>"+document.orderForm.Video3.value+"</td><td>"+document.orderForm.VideoQty3.value+"</td><td>$50.00</td><td>"+document.orderForm.VideoTotal3.value+"</td></tr>"
	+"<tr align='center'><td>"+document.orderForm.Video4.value+"</td><td>"+document.orderForm.VideoQty4.value+"</td><td>$50.00</td><td>"+document.orderForm.VideoTotal4.value+"</td></tr>"
	+"<tr align='center'><td>"+document.orderForm.Video5.value+"</td><td>"+document.orderForm.VideoQty5.value+"</td><td>$50.00</td><td>"+document.orderForm.VideoTotal5.value+"</td></tr>"
	+"<tr align='center'><td colspan='3' align='right'><i>Total Videotape Order $</i></td><td>"+document.orderForm.VideoSubTotal.value+"</td></tr></table></td></tr></table>"
	+"<table border='0' cellpadding='0' cellspacing='1'  width='100%'><tr><td>"
	+"<table border='0' cellpadding='0' cellspacing='1' width='100%' >"
	+"<tr><td colspan='2' class='contactTitle'>Brochures</td></tr>"
	+"<tr><td colspan='2' class='footerBlack'><i>Minimum order of one unit (50 brochures at $30 per unit.)</i></td></tr>"
	+"<tr><td class='contactTitle' align='center'>Brochure Title</td><td class='contactTitle' align='center'>Units</td></tr>"
	+"<tr><td>What Are Adolescents</td><td>"+document.orderForm.Brochure1.value+"</td></tr>"
	+"<tr><td>How Parents Can Survive</td><td>"+document.orderForm.Brochure2.value+"</td></tr>"
	+"<tr><td>26 Tips For Parents</td><td>"+document.orderForm.Brochure3.value+"</td></tr>"
	+"<tr><td>What Could A Middle School Be</td><td>"+document.orderForm.Brochure4.value+"</td></tr>"
	+"<tr><td>Peer Pressure</td><td>"+document.orderForm.Brochure5.value+"</td></tr>"
	+"<tr><td>Self-Esteem</td><td>"+document.orderForm.Brochure6.value+"</td></tr>"
	+"<tr><td align='right'><i>Total Brochures Order $</i></td><td>"+document.orderForm.BrochureSubTotal.value+"</td></tr></table></td></tr></table>"
	+"</td><td width='50%' align='left' valign='top'><table border='0' cellpadding='0' cellspacing='1'  width='100%'><tr><td>"
	+"<table border='0' cellpadding='0' cellspacing='1' width='100%' >"
	+"<tr><td colspan='2' class='contactTitle'>Bookmarks</td></tr>"
	+"<tr><td colspan='2' class='footerBlack'><i>Minimum order of one unit (100 bookmarks at $15 per unit.)</i></td></tr>"
	+"<tr><td class='contactTitle' align='center'>Bookmark Title</td><td class='contactTitle' align='center'>Units</td></tr>"
	+"<tr><td align='center'>'I'</td><td>"+document.orderForm.Bookmark1.value+"</td></tr>"
	+"<tr><td align='center'>TRANSESCENT</td><td>"+document.orderForm.Bookmark2.value+"</td></tr>"
	+"<tr><td align='right'><i>Total Bookmark Order $</i></td><td>"+document.orderForm.BookmarkSubTotal.value+"</td></tr></table></td></tr></table>"
	+"<table border='0' cellpadding='0' cellspacing='1' width='100%' ><tr><td><table border='0' cellpadding='0' cellspacing='1' width='100%'>"
	+"<tr><td colspan='2' class='contactTitle'>Middle Link</td></tr>"
	+"<tr><td colspan='2' class='footerBlack'><i>Three issues annually. Subscriptions run from September through August.</i></td></tr>"
	+"<tr><td>"
	);
	var check1 = document.orderForm.MiddleLink1.checked
	if (check1)
	{
		tablewindow.document.write("<b><i> X</i></b> ");
	}
	tablewindow.document.write("</td><td>Single subscription $15.00 <span class='footerBlack'>(1 copy each issue)</span></td></tr><tr><td>");
	var check2 = document.orderForm.MiddleLink2.checked
	if (check2)
	{
		tablewindow.document.write("<b><i>X</i></b> ");
	}
	tablewindow.document.write(
	"</td><td>Institutional subscription $40.00 <span class='footerBlack'>(5 copies each issue)</span></td></tr></table></td></tr></table>"
	+"<table border='0' cellpadding='0' cellspacing='1' width='100%'><tr><td><table border='0' cellpadding='0' cellspacing='1' width='100%'>"
	+"<tr><td colspan='4' class='contactTitle'>Discount Packages</td></tr>"
	+"<tr align='center'><td class='contactTitle'>Package Title</td><td class='contactTitle'>Quantity</td><td class='contactTitle'>Price</td><td class='contactTitle'>Total</td></tr>"
	+"<tr align='center'><td align='left'>Parents Package</td><td>"+document.orderForm.DiscQty1.value+"</td><td>$375.00</td><td>"+document.orderForm.DiscTotal1.value+"</td></tr>"
	+"<tr align='center'><td align='left'>Student Package</td><td>"+document.orderForm.DiscQty2.value+"</td><td>$280.00</td><td>"+document.orderForm.DiscTotal2.value+"</td></tr>"
	+"<tr align='center'><td align='left'>Judith Baenen Package</td><td>"+document.orderForm.DiscQty3.value+"</td><td>$135.00</td><td>"+document.orderForm.DiscTotal3.value+"</td></tr>"
	+"<tr align='center'><td align='left'>Motivation Package</td><td>"+document.orderForm.DiscQty4.value+"</td><td>$225.00</td><td>"+document.orderForm.DiscTotal4.value+"</td></tr>"
	+"<tr align='center'><td align='left' style='font-size:12px;'>Professional Development</td><td>"+document.orderForm.DiscQty5.value+"</td><td>$315.00</td><td>"+document.orderForm.DiscTotal5.value+"</td></tr>"
	+"<tr align='center'><td colspan='3' align='right'><i>Total Discount Packages $</i></td><td>"+document.orderForm.DiscSubTotal.value+"</td></tr></table></td></tr></table>"
	+"</td></tr><tr><td colspan='2' align='center'><table border='0' cellpadding='0' cellspacing='1'><tr><td>"
	+"<table border='0' cellpadding='0' cellspacing='1' width='100%'><tr><td colspan='2' class='contactTitle'>Order Summary</td></tr>"
	+"<tr><td align='left'>Total Videotape Order</td><td>"+document.orderForm.VideoGrandTotal.value+"</td></tr>"
	+"<tr><td align='left'>Total Brochure Order</td><td>"+document.orderForm.BrochureGrandTotal.value+"</td></tr>"
	+"<tr><td align='left'>Total Bookmark Order</td><td>"+document.orderForm.BookmarkGrandTotal.value+"</td></tr>"
	+"<tr><td align='left'>Total Middle Link Order</td><td>"+document.orderForm.MiddleLinkGrandTotal.value+"</td></tr>"
	+"<tr><td align='left'>Total Discount Package Order</td><td>"+document.orderForm.DiscGrandTotal.value+"</td></tr>"
	+"<tr><td align='right'><b>TOTAL ORDER $ </b></td><td>"+document.orderForm.GrandTotal.value+"</td></tr>"
	+"<tr><td colspan='2' align='center' class='footerBlack'>Please make all checks or money orders payable to UW-Platteville</td></tr>"
	+"<tr><td align='left' class='footerBlack'>TERMS OF INVOICE:  NET 30 DAYS <br />ALL SALES ARE FINAL <br />NO RETURNS WILL BE ACCEPTED</td>"
	+"<td align='left' class='footerBlack'>Mail To: <br />University of Wisconsin - Platteville <br /> 1 University Plaza <br />Platteville, WI 53818-3099</td></tr>"
	+"<tr><td align='center' colspan='2'><table border='0' cellpadding='0' cellspacing='1' class='tableLight'><tr><td>"
	+"<table border='0' cellpadding='0' cellspacing='1' class='tableLight'><tr><td class='contactTitle'>Comments</td></tr>"
	+"<tr><td align='center' width='90%'><textarea cols='35' rows='4' name='comments'>"+document.orderForm.comments.value+"</textarea></td></tr></table></td></tr></table></td></tr>"
	+"</table></td></tr></table></td></tr></table></body></html>"
	);
	tablewindow.document.close();
}