//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','Piccolo - Key of E ($20ea.)','Piccolo - Key of D ($22ea.)','Flute (Key of C - Fife)','Flute (Key of Bb - Fife)','Flute (Key of A)','Flute (Key of G)','Flute (Key of F)','Flute (Key of E)','Flute (Key of Eb)','Flute (Key of D)','Flute (Key of C#)','Flute (Key of C)','Flute (Key of B)','Flute (Key of 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','Piccolo - Key of E ($20 ea.)','Piccolo - Key of D ($22 ea.)','Piccolo - Key of C ($24 ea.)','Fife - Key of Bb ($26 ea.)','Flute - Key of A ($28 ea.)','Flute - Key of G ($30 ea.)','Flute - Key of F ($32 ea)','Flute - Key of E ($34 ea.)','Flute - Key of Eb ($36 ea.)','Flute - Key of D ($38 ea.)','Flute - Key of C# ($40 ea.)','Flute - Key of C ($40)','Flute - Key of B ($42 ea.)','Flute - Key of Bb ($44 ea.)','Shakuhachi - Student Model ($80 ea.)','Shakuhachi - Professional Model ($120 ea.)','Shakuhachi - Deluxe Model ($150 ea.)','Quena - Key of high C ($24 ea.)','Quena - Key of G ($30 ea.)','Quena - Key of low D ($38 ea.)','Lakota Flute - Key of high C ($34 ea.)','Lakota Flute - Key of G ($40 ea.)','Lakota Flute - Key of F ($42 ea.)','Lakota Flute - Key of low D ($48 ea.)','Lakota Flute - Key of low C ($50 ea.)','Pan Pipe - Pentatonic ($50+) {Call First}','Pan Pipe - Diatonic ($70+) {Call First}','Dijeridoo - Small ($45 ea.)','Dijeridoo - Medium ($65 ea.)','Dijeridoo - Large ($85 ea.)','Wind Chimes - Small {specify bead color} ($35 ea.)','Wind Chimes - Medium {specify bead color} ($55 ea.)','Wind Chimes - Large {specify bead color} ($75 ea.)','Bamboo Drinking Mug - Small ($20 ea.)','Bamboo Drinking Mug - Large ($25 ea.)','Corked Drinking Mug - Small ($25 ea.)','Corked Drinking Mug - Large ($30 ea.)','Corked Cannister - Small ($10 ea.)','Corked Cannister - Medium ($20 ea.)','Corked Cannister - Large ($30 ea.)','Double-Ended Large Corked Cannister ($45 ea.)');


//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] + ' of each ' + writeProdJim[i] + ' and<br>');
			}else howMany = (howMany + writeCost[i] + ' of each ' + 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 Woodsong Instruments</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 Instruments 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 Instruments <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();

}

//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 > 5) pah = 5;
			}
		}else break;
	}
	var pahAdd = new Array(0, 4, 5.5, 7, 8.5, 10);
	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 * .045);
	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();
}


