var RowsInForm = 2 //5          //How many line items will be in the order form?
var ProductsInList = 2 //10     //How many products in your product list? "0 is the first item in list"
//var SalesTaxRate = 0.0600   //Set to sales tax rate in decimal. e.g. 0.0775 is 6.00%.
//var TaxableState = "FL"     //Set to name of state you charge sales tax in.
var ProdSubscript = 1       //Identifies subscript of selected product in current row.

function MakeArray(n) {
   this.length = n
   for (var i=1;i<=n;i++) {this[i]=0}
   return this
}

function BuildZeroArray(n) {
   this.length = n
   for (var i=0;i<=n;i++) {this[i]=0}
   return this
}

function prodobj(name, unitprice) {
   this.name = name
   this.unitprice = unitprice
}

function ordobj(prodsub, qty, unitprice, extprice) {
   this.prodsub = prodsub
   this.qty = qty
   this.unitprice = unitprice
   this.extprice = extprice
}

function updateRow(rownum){ 
   var exeLine='ProdSubscript=document.ordform.prodchosen'+rownum+'.selectedIndex'
   eval(exeLine) 
   ordData[rownum].prodsub=ProdSubscript 
   var exeLine='tempqty=document.ordform.qty'+rownum+'.value'
   eval(exeLine)
   ordData[rownum].qty=tempqty-0   //-- Gets unit price from the product price list.
   ordData[rownum].unitprice=prodlist[ProdSubscript].unitprice
   ordData[rownum].extprice=(ordData[rownum].qty)*ordData[rownum].unitprice
   var exeLine='document.ordform.unitprice'+rownum+'.value=currency(ordData['+rownum+'].unitprice,10)'
   eval (exeLine)
   var exeLine='document.ordform.extprice'+rownum+'.value=currency(ordData['+rownum+'].extprice,10)'
   eval(exeLine)
   updateTotals()

}

function updateTotals() {
   var subtotal = 0
   for (var i=1;i<=RowsInForm;i++) {
      subtotal=subtotal+ordData[i].extprice
   }
   document.ordform.subtotal.value = currency(subtotal,10)
   //salestax = 0
   //if (document.ordform.Taxable.checked) {  //No sales tax
   //   salestax = SalesTaxRate*subtotal
   //}
   //document.ordform.salestax.value = currency(salestax,10)
   //document.ordform.grandtotal.value = currency(subtotal+salestax,10)
}

function copyAddress() {
   document.ordform.shipName.value=document.ordform.billName.value
   document.ordform.shipCompany.value=document.ordform.billCompany.value
   document.ordform.shipAdd1.value=document.ordform.billAdd1.value
   document.ordform.shipAdd2.value=document.ordform.billAdd2.value
   document.ordform.shipCSZ.value=document.ordform.billCSZ.value
   document.ordform.shipPhone.value=document.ordform.billPhone.value
   document.ordform.shipEmail.value=document.ordform.billEmail.value
}

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"}

   if (dNum>=1000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
   }

   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
}

function isEmpty(){ 
var myQty = document.getElementById('qty');
	if(myQty.value.length == 0){
		alert("Please choose a Qty");
		myQty.style.backgroundColor='YELLOW'; 
		myQty.focus();
	return true;
	}
var myItem2 = document.ordform.items[0].options[document.ordform.items[1].selectedIndex].text;
	if(myItem2 == "Choose") {
	} else {
	var myQty2 = document.ordform.qty[1];
		if(myQty2.value <= "0") {
			alert("Please choose a Qty");
			myQty2.style.backgroundColor = 'YELLOW';
			myQty2.focus();
	return true;
		}
	}

var myPayment = document.getElementById('cb1').checked;
var myPayment1 = document.getElementById('cb2').checked;
	if (myPayment == false && myPayment1 == false) {
		alert("Please choose Payment method");
	return true;
	}
	if (myPayment1 == true) {
	} else {
var myName = document.getElementById('shipName');
var myAdd1 = document.getElementById('shipAdd1');
var myCSZ = document.getElementById('shipCSZ');
var myEmail = document.getElementById('shipEmail');
	if (myName.value == "") {
		alert("You must enter a Name!");
		myName.style.backgroundColor='YELLOW';
		myName.focus();
	return true;
	}
	if (myAdd1.value == "") {
		alert("You must enter and Address");
		myAdd1.style.backgroundColor='YELLOW';
		myAdd1.focus();
	return true;
	}
	if (myCSZ.value == "") {
		alert("You must enter your City, State, Zip");
		myCSZ.style.backgroundColor='YELLOW';
		myCSZ.focus();
	return true;
	}
		//alert(myEmail.value);
	if (myEmail.value == "") {
		alert("Please enter a valid EMail Address \r\n This is so I can notify you \r\n when your item has shipped");
		myEmail.style.backgroundColor='YELLOW';
		myEmail.focus();
	return true;
	}
	}
	if (myPayment == true) {
		Inv();
	}
	else {
		PayPalInv();
	}
	return false;
}	

function PayPalInv() {
		//setup each lines information
var myItem1 = document.ordform.items[0].options[document.ordform.items[0].selectedIndex].text;
	var myQty1 = document.ordform.qty[0].value;
	var myPrice1 = document.ordform.unitprice[0].value;
	var myExt1 = document.ordform.extprice[0].value;
	if (myItem1 == "Choose") {
		myItem1 = ""; myQty1 = ""; myPrice1 = ""; myExt1 = "";
	}
		//Second Items line
var myItem2 = document.ordform.items[0].options[document.ordform.items[1].selectedIndex].text;
	var myQty2 = document.ordform.qty[1].value;
	var myPrice2 = document.ordform.unitprice[1].value;
	var myExt2 = document.ordform.extprice[1].value;
	if (myItem2 == "Choose") {
		myItem2 = ""; myQty2 = ""; myPrice2 = ""; myExt2 = "";
	}
	var myTotal = document.getElementById('subtotal').value;

top.consoleRef=window.open('','myconsole',
   +',width=800'
   +',height=600' 
   +',menubar=yes'
   +',toolbar=yes'
   +',status=yes'
   +',scrollbars=yes'
   +',location=yes'
   +',directories=yes'
//   +',copyhistory=yes'
   +',resizable=yes')
 top.consoleRef.document.writeln(
   '<html><head><title>Invoice</title></head>'
   +'<body bgcolor=white onLoad="self.focus()">'
   +'<center><H1> Invoice </H1>'
   +'<H3>MAHALO FOR YOUR SUPPORT</H3></center>'
   +'<table align="center" border="1"><tr>'
   +'<th><b>Product<br>'+myItem1+'</th><th><b>Qty<br>'+myQty1+'</th></b>'
   +'<th><b>Price<br>'+myPrice1+'</th><th>Ext Price<br>'+myExt1+'</th></b>'
   +'<tr><td><b><center>'+myItem2+'</center></b></td><td><b><center>'+myQty2+'</center></b></td>'
   +'<td><b><center>'+myPrice2+'</center></b></td><td><b><center>'+myExt2+'</center></td></b></tr>'
//   +'<th><b>Product<br>'+myItem1+'<br>'+myItem2+'</b></th><th><b>Qty<br>'+myQty1+'<br>'+myQty2+'</b></th>'
//   +'<th><b>Price<br>'+myPrice1+'<br>'+myPrice2+'</b></th><th><b>Ext Price<br>'+myExt1+'<br>'+myExt2+'</b></th>'
   +'<tr><td colspan="3" align="right"><b>Total: &nbsp</b></td><td align="center"><b>'+myTotal+'</b></td></tr>'
   +'</tr></table>'
   +'<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">'
   +'<input type="hidden" name="lc" value="US">'
   +'<input type="hidden" name="currency_code" value="USD">'
   +'<input type="hidden" name="business" value="jrmint@gmail.com">'
   +'<input type="hidden" name="cmd" value="_cart">'
   +'<input type="hidden" name="upload" value="1">'
   +'<input type="hidden" name="shipping_1" value="0.00">'
   +'<input type="hidden" name="item_name_1" value="'+myItem1+'">'
   +'<input type="hidden" name="amount_1" value="'+myPrice1+'">'
   +'<input type="hidden" name="quantity_1" value="'+myQty1+'">'
   +'<!-- 2nd Item -->'
   +'<input type="hidden" name="shipping_2" value="0.00">'
   +'<input type="hidden" name="item_name_2" value="'+myItem2+'">'
   +'<input type="hidden" name="amount_2" value="'+myPrice2+'">'
   +'<input type="hidden" name="quantity_2" value="'+myQty2+'">'
   +'<p align="center">'
   +'<input type="image" src="https://www.paypal.com//en_US/i/bnr/bnr_paymentsBy_150x40.gif">'
   +'</form></p>'
   +'<p> </p>'
   +'</body></html>'
 )
 top.consoleRef.document.close()

}

function Inv() {
		//setup each lines information
var myItem1 = document.ordform.items[0].options[document.ordform.items[0].selectedIndex].text;
	var myQty1 = document.ordform.qty[0].value;
	var myPrice1 = document.ordform.unitprice[0].value;
	var myExt1 = document.ordform.extprice[0].value;
	if (myItem1 == "Choose") {
		myItem1 = ""; myQty1 = ""; myPrice1 = ""; myExt1 = "";
	}
		//Second Items line
var myItem2 = document.ordform.items[0].options[document.ordform.items[1].selectedIndex].text;
	var myQty2 = document.ordform.qty[1].value;
	var myPrice2 = document.ordform.unitprice[1].value;
	var myExt2 = document.ordform.extprice[1].value;
	if (myItem2 == "Choose") {
		myItem2 = ""; myQty2 = ""; myPrice2 = ""; myExt2 = "";
	} 

	var myTotal = document.getElementById('subtotal').value;
	var myName = document.getElementById('shipName').value;
	var myCompany = document.getElementById('shipCompany').value;
	var myAdd1 = document.getElementById('shipAdd1').value;
	var myAdd2 = document.getElementById('shipAdd2').value;
	var myCSZ = document.getElementById('shipCSZ').value;
	var myPhone = document.getElementById('shipPhone').value;
	var myEmail = document.getElementById('shipEmail').value; 
	//alert (myQty);

top.consoleRef=window.open('','myconsole',
   +',width=800'
   +',height=600' 
   +',menubar=yes'
   +',toolbar=yes'
   +',status=yes'
   +',scrollbars=yes'
   +',location=yes'
   +',directories=yes'
   +',copyhistory=yes'
   +',resizable=yes')
 top.consoleRef.document.writeln(
   '<html><head><title>Invoice</title>'
   +'<script language="JavaScript"> function printWindow(){ window.print() }</script></head>'
   +'<body bgcolor=white onLoad="self.focus()">'
   +'<center><H1> Invoice </H1><H3>MAHALO FOR YOUR SUPPORT</H3>'
   +'<p><b><a href="javascript:printWindow()">Please Print this Page.</a></b>'
   +'<br> Make Check Payable to: &nbsp&nbsp David Pitsford<br>'
   +'Mail this Invoice with your check to:</p>'
   +'<p>David Pitsford<br>10717 Fairbanks Rd<br>Albuquerque, NM 87112</p></center>'
   +'<table align="center" border="1"><tr>'
   +'<th><b>Product<br>'+myItem1+'</th><th><b>Qty<br>'+myQty1+'</th></b>'
   +'<th><b>Price<br>'+myPrice1+'</th><th>Ext Price<br>'+myExt1+'</th></b>'
   +'<tr><td><b><center>'+myItem2+'</center></b></td><td><b><center>'+myQty2+'</center></b></td>'
   +'<td><b><center>'+myPrice2+'</center></b></td><td><b><center>'+myExt2+'</center></td></b></tr>'
//   +'<th><b>Product<br>'+myItem1+'<br>'+myItem2+'</b></th><th><b>Qty<br>'+myQty1+'<br>'+myQty2+'</b></th>'
//   +'<th><b>Price<br>'+myPrice1+'<br>'+myPrice2+'</b></th><th><b>Ext Price<br>'+myExt1+'<br>'+myExt2+'</b></th>'
   +'<tr><td colspan="3" align="right"><b>Total: &nbsp</b></td><td align="center"><b>'+myTotal+'</b></td></tr>'
   +'</tr></table>'
   +'<p> </p>'
   +'<table border="1" align="center"><tr>'
   +'<td width="528" colspan="2"><center><b>Ship To</b></center></td></tr>'
   +'<tr><td width="120" align="center">Name:</td><td width="408">'+myName+'</td></tr>'
   +'<tr><td width="120" align="center">Company:</td><td width="408">'+myCompany+'</td></tr>'
   +'<tr><td width="120" align="center">Address1:</td><td width="408">'+myAdd1+'</td></tr>'
   +'<tr><td width="120" align="center">Address2:</td><td width="408">'+myAdd2+'</td></tr>'
   +'<tr><td width="120" align="center">City, State, Zip:</td><td width="408">'+myCSZ+'</td></tr>'
   +'<tr><td width="120" align="center">Phone:</td><td width="408">'+myPhone+'</td></tr>'
   +'<tr><td width="120" align="center">Email address:</td><td width="408">'+myEmail+'</td></tr>'
   +'</table>'
   +'<p> </p>'
   +'</body></html>'
 )
 top.consoleRef.document.close()
	
}

function checkBoxValidate(id){
var myBox = document.getElementById(id);
var myBox1 = document.ordform.cb1;
var myBox2 = document.ordform.cb2;
	if(myBox.name == "cb1") {
		myBox2.checked = false;
		var Payment = myBox1.value;
	document.getElementById('ShipTo').style.display='block';
	}
	else if(myBox.name == "cb2") {
		myBox1.checked = false;
		var Payment = myBox2.value;
	document.getElementById('ShipTo').style.display='none';
	}
	else{}
	return Payment; 
}
