/*
The following code is written by Nathan Foster.

NONE OF THE FOLLOWING CODE IS INTENDED AS INSTRUCTIONS ON HOW TO USE THE ENZYME PRODUCTIONS STORE! FOR INSTRUCTIONS ON HOW TO USE THE ENZYME PRODUCTIONS STORE, PLEASE VISIT: <http://enzyme-pro.com/store/instructions.html>
*/

var now=new Date();

var months=new Array('January','February','March','April','May','June','July','August','September','October','November','December');

var date=((now.getDate()<10)?"0":"")+now.getDate()

// Calculate four digit year.
function fourdigits(number) {
	return (number < 1000)?number+1900:number
}

// Join it all together
today=date+" "+months[now.getMonth()]+" "+(fourdigits(now.getYear()))

var maxquantity=10
var largestcost=0
var largestquantity=0
var idahosalestax="6%"

function isanumber(thestring) {
	if(thestring.length==0) { return 0 }

	for(var i=0;i<thestring.length;i++) {
		if((thestring.substring(i,i+1)<'0')||(thestring.substring(i,i+1)>'9')) { return 0 }
    	}
	return 1
}

function CheckQuantityFormat(quantitybox) {
	if(quantitybox.value=="") { quantitybox.value="0" }

	if(!isanumber(quantitybox.value)) {
		alert("You entered an invalid quantity:\n\n"+quantitybox.value+"\n\nQuantity changed to 0.")

		quantitybox.value="0"

		return 0
	}

	if(parseInt(quantitybox.value, 10)>10) {
		alert("You entered a quantity greater than the maximum allowed ("+maxquantity+"). You entered:\n\n"+quantitybox.value+"\n\nQuantity changed to "+maxquantity+".")

		quantitybox.value=maxquantity

		return 0
	}

	return 1
}

function CheckQuantity() {
	// this will return the largest cost in terms
	// of number of decimal places to the left.

	largestcost=0
	currentcost=0

	i=0

	do {
		tmpname=('product'+i)
		tmp=document.getElementById(tmpname)

		if(tmp) {
			if(!CheckQuantityFormat(tmp.quantity)) { return 0 }
			currentquantity=parseInt(tmp.quantity.value)
			currentcost=parseFloat(tmp.productcost.value)*currentquantity
			largestcost=(currentcost>largestcost?currentcost:largestcost)
			largestquantity=(currentquantity>largestquantity?currentquantity:largestquantity)
		}

		i++
	} while(tmp)

	if(largestcost>0) { 
		return largestcost
	} else { 
		alert('You have not ordered any products! Please change at least one "Order Quantity" box to some number between 1 and '+maxquantity+'.')

		return 0
	}
}

function UpdateOrder() {
	largestcostasstring=largestcost+""
	largestquantityasstring=largestquantity+""

	// make sure the user filled out the information
	if(!document.userdata.name.value||!document.userdata.address.value||!document.userdata.city.value||!document.userdata.state.value||!document.userdata.pcode.value||!document.userdata.country.value) {
		alert("Please enter all required information.")
		return 0
	}

	// initial formatting:
	TheWholeString=(today+"\n\n        Seller Address:\nEnzyme Productions\nP.O. Box 1172\nMoscow, ID 83843\nUSA\n\n        Buyer ShipTo Address:\n"+document.userdata.name.value+"\n"+document.userdata.address.value+"\n"+(document.userdata.address2.value?document.userdata.address2.value+"\n":"")+document.userdata.city.value+", "+document.userdata.state.value+" "+document.userdata.pcode.value+"\n"+document.userdata.country.value+"\n\n")

	i=0
	subtotal=0
	shipandh=0

	// In this document, there are more than one product.
	// These products are identified by a form with an id
	// named "product#" where # is the number, beginning
	// with 0, and progressing through to the highest
	// number.

	// add info for all products:
	do {
		tmpname=('product'+i)
		tmp=document.getElementById(tmpname)

		if(tmp&&parseInt(tmp.quantity.value)>0) {
			costofitems=parseFloat(tmp.productcost.value)*parseInt(tmp.quantity.value)
			costofitemsasstring=costofitems+""
			quantityofitemsasstring=parseInt(tmp.quantity.value)+""

			// code for right-aligning the quantities
			bufferspaces=""
			for(d=1;d<=largestquantityasstring.length-quantityofitemsasstring.length;d++) {
				bufferspaces=bufferspaces+" "
			}

			TheWholeString=(TheWholeString+bufferspaces+tmp.quantity.value)

			// code for right-aligning the costs
			bufferspaces=""
			for(d=1;d<=largestcostasstring.length-costofitemsasstring.length;d++) {
				bufferspaces=bufferspaces+" "
			}

			TheWholeString=(TheWholeString+" x "+tmp.productname.value+bufferspaces+costofitems.toFixed(2)+"\n")
			subtotal=subtotal+costofitems

			shipandh=shipandh+parseFloat(tmp.sandh.value)*parseInt(tmp.quantity.value)
		}

		i++
	} while(tmp)

	total=subtotal+shipandh

	// calculate right-align for subtotal and total
	var strings=new Array(subtotal.toFixed(2),shipandh.toFixed(2),total.toFixed(2))

	greaterofthree=0

	for (d=0;d<=2;d++) {
		greaterofthree=(greaterofthree>strings[d].length?greaterofthree:strings[d].length)
	}

	// subtotal
	bufferspaces=""
	for(d=1;d<=greaterofthree-strings[0].length;d++) {
		bufferspaces=bufferspaces+" "
	}
	TheWholeString=(TheWholeString+"\n        Subtotal:                               "+bufferspaces+strings[0])

	// shipping and handling
	bufferspaces=""
	for(d=1;d<=greaterofthree-strings[1].length;d++) {
		bufferspaces=bufferspaces+" "
	}
	TheWholeString=(TheWholeString+"\n        Shipping and handling:                  "+bufferspaces+strings[1])

	// total
	bufferspaces=""
	for(d=1;d==greaterofthree-strings[2].length;d++) {
		bufferspaces=bufferspaces+" "
	}
	TheWholeString=(TheWholeString+"\n\n        Total including\n        "+idahosalestax+" Idaho sales tax and\n        Google Checkout fee:                    "+bufferspaces+strings[2])

	TheWholeString=(TheWholeString+"\n\nThank you for ordering from Enzyme Productions!")

	document.orderform.ordercontents.value=TheWholeString

	return 1
}

function PageTurn(pagenumber, doupdate) {
	// in this document, there is more than one page. Each .maintext
	// div is a page, identified by an id page# where # is the page
	// number. The first two pages (page0 and page1) are the checkout
	// and the order review respectively. Any page greater than that
	// is one of the pages which displays the products.

	// if pagenumber equals zero, we must be proceeding to checkout.
	if(pagenumber==0) { 
		CheckQuantity()
		if (!largestcost) { return 0 }
	}
	if(doupdate) {
		if(!UpdateOrder()) { return 0 }
	}

	thepage=('page'+pagenumber)

	document.getElementById(thepage).style.display="block"

	i=0

	do {
		tmpname=('page'+i)
		tmp=document.getElementById(tmpname)

		if(tmp && i!=pagenumber) {
			tmp.style.display="none"
		}

		i++
	} while(tmp)
}