//resets form boxes  and variables onLoad
function resetForm(){
totalPrice = 0;
totalTemp = 0;
document.forms[0].total.value = '0';
document.forms[0].pah.value = '0';
var pah = 0;
var addPah = 0;
document.forms[0].gTotal.value = '0';
	for (i=0; i<100; i++)
		if (eval('document.forms[0].noof' + i)){
		eval('document.forms[0].noof' + i + '.value = ""');
	}
	for (i=0; i<100; i++){
		if (eval('document.forms[0].check' + i )){
		eval('document.forms[0].check' + i + '.checked = false')
		}
	}

for (i=0; i<100; i++)
	{writeCost[i] = 0;}


for (i=0; i<100; i++)
	{prod[i] = 0;}

totalTemp = 0;
}

 //variable for round
var totalPrice = 0;

// given a numeral, makes a money-formatted figure:
function round(Number,Decimals,Separator)
{
 // Placed in the public domain by Affordable Production Tools
 // March 21, 1998
 // Web site: http://www.apt.simplenet.com
 Number += ""          // Force argument to string.
 Decimals += ""        // Force argument to string.
 Separator += ""       // Force argument to string.
 if((Separator == "") || (Separator.length > 1))
  Separator = "."
 if(Number.length == 0)
  Number = "0"
 var OriginalNumber = Number  // Save for number too large.
 var Sign = 1
 var Pad = ""
 var Count = 0
 if(parseFloat(Number)){
  Number = parseFloat(Number)} else {
  Number = 0}
 if((parseInt(Decimals,10)) || (parseInt(Decimals,10) == 0)){
  Decimals = parseInt(Decimals,10)} else {
  Decimals = 2}
 if(Number < 0)
 {
  Sign = -1         // Remember sign of Number.
  Number *= Sign    // Force absolute value of Number.
 }
 if(Decimals < 0)
  Decimals *= -1    // Force absolute value of Decimals.
 Number = "" + Math.floor(Number * Math.pow(10,Decimals + 1) + 5)
 if((Number.substring(1,2) == '.')||((Number + '')=='NaN'))
  return(OriginalNumber) // Number too large to format as specified.
 if(Number.length < Decimals +1) // Construct pad string.
 {
  for(Count = Number.length; Count <= Decimals; Count++)
   Pad += "0"
 }
 Number = Pad + Number // Pad number as needed.
 if(Decimals == 0){
  Number = Number.substring(0, Number.length -1)} else {
 Number = Number.substring(0,Number.length - Decimals -1) +
          Separator +
          Number.substring(Number.length - Decimals -1,
          Number.length -1)}
 if(Sign == -1)
  Number = "-" + Number  // Set sign of number.
 if(Number.length == 0)
  Number="0"
totalPrice = Number
 return(totalPrice)
}

//Checks if entry is a numeral:
function isNumber(inputStr){
	for (var i = 0; i < inputStr.length; i++){
		var oneChar = inputStr.substring(i, i + 1)
		if (oneChar < "0" || oneChar > "9") {
			return false;
		}
	}
	return true
}

//working with "isNumber" this changes a string to a numeral to see if it's a number:
//Is this neccesary???
function checkIt(formNum){
	inputStr = eval('document.forms[0].' + formNum + '.value');
	if (isNumber(inputStr)) {
		}
	else{
	alert("Please enter a numeral here");
	eval('document.forms[0].' + formNum + '.focus()');
	}
}

//Validates zip Code if it is an "ADD4" zip code:
function validateZip(t){
	t += "";
		if (t.substring(5,6)=="-" && t.length==10){
		}
		else if (t.length ==5){
		}
		else alert("You did not enter a proper zip code. Please enter a 5 digit zip code or a proper zip with 4 digit extension with a dash between the 5th and 6th numerals");
	}

//Checks 5-digit zip code:
function checkZip(formNum){
	inputStr = eval('document.forms[0].' + formNum + '.value');
	if (isNumber(inputStr)) {
		validateZip(eval('document.forms[0].' + formNum + '.value'));
		}
	else{
		var alertdone = 0;
		inputStr += "";
		if (inputStr.substring(5,6)=='-'){
			firstFive = parseFloat(inputStr.substring(0,5))
			secondFour = parseFloat(inputStr.substring(6,11))
				if ( (isNumber(inputStr.substring(0,5))) && (firstFive < 100000)){
					}
					else{
						alert("You did not enter a proper zip code.");
						alertdone = 1;
					}
				 if (alertdone==0){
			       if ((isNumber((inputStr.substring(6,11)))) && (secondFour < 10000) && (secondFour > 999)){
					}
	       else if ((isNumber(inputStr.substring(6,11))) && (secondFour < 1000) && (inputStr.substring(6,7)=='0')){
		}
			else alert("You did not enter a proper zip code. Please re-enter your 4 digit extension.");
				}
		}
			else alert("You did not enter a proper zip code. Please enter a 5 digit zip code or a proper zip with 4 digit extension");
			eval('document.forms[0].' + formNum + '.focus()');
		}
}

//variables for "assignResults":
var writeProd = new Array('0','Flute (Key of high E)','Flute (Key of high D)','Flute (Key of high C)','Flute (Key of high Bb)','Flute (Key of A)','Flute (Key of G)','Flute (Key of F)','Flute (Key of low E)','Flute (Key of low Eb)','Flute (Key of low D)','Flute (Key of low C#)','Flute (Key of low C)','Flute (Key of low B)','Flute (Key of low Bb)','Shakuhachi (Call First)','Quena (Call First)','Lakota Flutes (Call First)','Pan Pipe - Pentatonic (Call First)','Pan Pipe - Diatonic (Call First)','Dijeridoo (Small)','Dijeridoo (Medium)','Dijeridoo (Large)','Wind Chimes (specify bead color)','Bamboo Drinking Mugs (Small)','Bamboo Drinking Mugs (Large)','Corked Drinking Mugs (Small)','Corked Drinking Mugs (Large)','Corked Cannisters (Small)','Corked Cannisters (Medium)','Corked Cannisters (Large)','Double-Ended Large Corked Cannisters');

var writeCost = new Array(100);
for (i=0; i<100; i++)
	{writeCost[i] = 0;}

var prod = new Array(100);
for (i=0; i<100; i++)
	{prod[i] = 0;}

//Creates file in preview window with print button:
function assignResults(){
	var whereResult = 'null';

	//variable for string
	howMany = '';
	//writes lines for orderng all the products
	for (i=0; i<100; i++){
			if (prod[i]){
			writeCost[i] = eval('document.forms[0].noof' + i + '.value');
		//checking to see if they are checking products non-sequentially
		// this should be looped someow if you had a lot of products
			if (prod[i+1] || prod[i+2] || prod[i+3]){
			howMany = (howMany + writeCost[i] + ' items ' + writeProd[i] + ' and<br>');
			}else howMany = (howMany + writeCost[i] + ' items ' + writeProd[i] + ' <br>');
			}
	}
	//variable for price
	varHowMuch = document.forms[0].gTotal.value;
	//setting address field according to in US or not:
	if (document.forms[0].foreign.checked){
		var addr = document.forms[0].foradd.value;
	}
		else addr = (document.forms[0].address.value) + '<br>' + (document.forms[0].city.value) + ', ' + (document.forms[0].state.options[document.forms[0].state.selectedIndex].value) + ', ' + (document.forms[0].zip.value);
	//concatenates variable string
	result = ((document.forms[0].name.value) + '<br>' + addr + ' <BR><BR>Please send me<BR>' + (howMany) + '<BR><BR>I have enclosed ' + (varHowMuch) + '<BR>');
	//opens window:
	var resultw = open ('', 'resultWindow', 'width=440px,height=540px,scrollbars=1,left=25px,screenX=25px');
  	resultw.document.open();
	//writes constants of document plus previously constructed string:
 	resultw.document.write('<HTML><HEAD><TITLE>Order Form for Woodsong Cottage</TITLE>' + '<script language="javascript"> var da = (document.all) ? 1:0;' + 'var pr = (window.print) ? 1 : 0;' + 'var mac = (navigator.userAgent.indexOf("Mac") != -1); function printThis()' + '{if (pr) {window.print();}' + 'else if (da && !mac) {vbPrintPage()' + ';' + '}' + '}' + '<' + '/' + 'script>' + '</HEAD><BODY onLoad="window.focus()"><font face="times new roman, times" size="4" color="#000000"><BR><BR>' + result + '</font><form>' + '<input type = "button"  value = "print" onClick="printThis();">' + '</form><BR><FONT SIZE="2"><I>Please review this information and be sure the total is correct.</I><BR>Woodsong Cottage is not responsible for incorrect calculations and will return checks written for less than the proper amount.</FONT><BR><BR>mail form with check or money order to: <BR><i><FONT SIZE=+1>Woodsong Cottage <BR>1098 Dobbins Farm Road <BR>Floyd, VA 24091<BR>please include your phone number<BR>THANK YOU FOR YOUR BUSINESS!</FONT></i><BR></BODY></HTML>');
  	resultw.document.close();

}

//variables for "assignResJim":
var writeProdJim = new Array('0','Songs Of Love (CD)','Rolling Home (CD)','Sing We Enchanted (CD)','Burly Minstrels (tape)','Burly Minstrels (CD)','Bedlam Boys (CD)','Good Companions (tape)','Good Companions (CD)','World Turned Upside Down (tape)','World Turned Upside Down (CD)');


//Creates file in preview window with print button:
function assignResJim(){
	var whereResult = 'null';

	//variable for string
	howMany = '';
	//writes lines for orderng all the products
	for (i=0; i<100; i++){
			if (prod[i]){
			writeCost[i] = eval('document.forms[0].noof' + i + '.value');
		//checking to see if they are checking products non-sequentially
		// this should be looped someow if you had a lot of products
			if (prod[i+1] || prod[i+2] || prod[i+3] || prod[i+4] || prod[i+5] || prod[i+6] || prod[i+7]|| prod[i+8]){
			howMany = (howMany + writeCost[i] + ' copies of ' + writeProdJim[i] + ' and<br>');
			}else howMany = (howMany + writeCost[i] + ' copies of ' + writeProdJim[i] + ' <br>');
			}
	}
	//variable for price
	varHowMuch = document.forms[0].gTotal.value;
	//setting address field according to in US or not:
	if (document.forms[0].foreign.checked){
		var addr = document.forms[0].foradd.value;
	}
		else addr = (document.forms[0].address.value) + '<br>' + (document.forms[0].city.value) + ', ' + (document.forms[0].state.options[document.forms[0].state.selectedIndex].value) + ', ' + (document.forms[0].zip.value);
	//concatenates variable string
	result = ((document.forms[0].name.value) + '<br>' + addr + ' <BR><BR>Please send me<BR>' + (howMany) + '<BR><BR>I have enclosed ' + (varHowMuch) + '<BR>');
	//opens window:
	var resultw = open ('', 'resultWindow', 'width=440px,height=540px,scrollbars=1,left=25px,screenX=25px');
  	resultw.document.open();
	//writes constants of document plus previously constructed string:
 	resultw.document.write('<HTML><HEAD><TITLE>Order Form for Jim Hancock</TITLE>' + '<script language="javascript"> var da = (document.all) ? 1:0;' + 'var pr = (window.print) ? 1 : 0;' + 'var mac = (navigator.userAgent.indexOf("Mac") != -1); function printThis()' + '{if (pr) {window.print();}' + 'else if (da && !mac) {vbPrintPage()' + ';' + '}' + '}' + '<' + '/' + 'script>' + '</HEAD><BODY onLoad="window.focus()"><font face="times new roman, times" size="4" color="#000000"><BR><BR>' + result + '</font><form>' + '<input type = "button"  value = "print" onClick="printThis();">' + '</form><BR><FONT SIZE="2"><I>Please review this information and be sure the total is correct.</I><BR>Jim Hancock is not responsible for incorrect calculations and will return checks written for less than the proper amount.</FONT><BR><BR>mail form with check or money order to: <BR><i><FONT SIZE=+1>PO Box 4694 <BR>Austin, TX 78765</FONT></i><BR></BODY></HTML>');
  	resultw.document.close();

}

//sums up all products indicated:
function totalUp(){
	//initializes postage and handling:
	pah = 0
	//loops thru each field and adds in p&h:
	for (i=0; i<100; i++){
		if (eval('document.forms[0].noof' + (i+1))){
			if (eval('document.forms[0].noof' + (i+1) + '.value')){
			pah =  parseFloat(pah) + parseFloat(eval('document.forms[0].noof' + (i+1) + '.value'))
			//set to maximum charge:
			if (pah > 3) pah = 3;
			}
		}else break;
	}
	var pahAdd = new Array(0,2, 3.5, 5);
	addPah = round(pahAdd[pah]);
	document.forms[0].pah.value = "$" + addPah;

	//loops thru each field and adds up prices:
	totalProd = 0;
	for (i=0; i<100; i++){
			if (prod[i]){
			totalTemp = prod[i];
			totalProd = totalProd + totalTemp;
			}
		}
	//sets total product price:
	document.forms[0].total.value = "$" + round(totalProd) ;
	//acounts for sales tax:
	if (document.forms[0].tax.checked) {
	totalToTax = totalProd;
	taxOnPrice = (totalToTax * .085);
	taxedTotal=parseFloat(totalToTax) + parseFloat(taxOnPrice);
	//sets grand total if there is sales tax:
	document.forms[0].gTotal.value="$" + round(taxedTotal + parseFloat(addPah));
	}
	//sets grand total without salse tax:
else document.forms[0].gTotal.value= "$" + round(parseFloat(totalProd) + parseFloat(addPah));
}

//takes addIn (form field),
//sets it to array index (i) and sets value by price (n):
function addUp(addIn, i, n){
prod[i] = eval((addIn).value * n);
totalUp();
}


