
// javascript ordering.
/////
//x create arrays of titles, prices
//x create function that loops through array and writes table rows.
// qty and extended text boxes must be in array.
// create function on btnUpdate that extends qty and loads extended boxes, and gets total.
// create function that creates properly formatted money amounts.
// create function that loops thru array and writes print document.


var titles = new Array(
	"All The Clouds'll Roll Away (audio CD)",
	"All The Clouds'll Roll Away - Vol 1",
	"Appalachian Odyssey",
	"Blues For a Black Cat and Other Stories",
	"Celia Thaxter: Selected Writings",
	"City in the Sky",
	"Country Roads of Connecticut and Rhode Island",
	"Country Roads of New Hampshire",
	"Fleur Weymouth Photographs",
	"Grand Monadnock",
	"Hermaphroditus in America",
	"Highboy",
	"Higher Latitudes",
	"Maple Sugar Murders",
	"Message From the Sparrows",
	"Nature Walks Along the Seacoast", /* */
	"Nature Walks in Southern New Hampshire", /* */
	"Nature Walks in the New Hampshire Lakes Region", /* */
	"Primary Crime",
	"Rolling The Sun",
	"Tahirih Unveiled",
	"Tales of the Francois Vase",
	"The Island Queen",
	"The Ossabaw Book of Hours",
	"This Desired Place; The Isles of Shoals"
	);
var prices = new Array(
	15.00,		/* All The Clouds'll Roll Away */
	19.95,		/* All The Clouds'll Roll Away - Vol 1 */
	19.95,		/* Appalachian Odyssey */
	12.95, 		/* Blues For a Black Cat and Other Stories	*/
	14.50,		/* Celia Thaxter: Selected Writings	 */
	8.00, 		/* City in the Sky 	 */
	12.95, 		/* Country Roads of Connecticut and Rhode Island	*/	
	12.95, 		/* Country Roads of New Hampshire	 */
	25.00, 		/* Fleur Weymouth Photographs	 */
	20.00, 		/* Grand Monadnock	*/
	16.00, 		/* Hermaphroditus in America	 */
	5.99, 		/* Highboy	 */	
	8.00, 		/* Higher Latitudes	*/
	5.99, 		/* Maple Sugar Murders	*/
	21.99, 		/* Message From the Sparrows	 */
	15.95,		/* Nature Walks Along the Seacoast */
	10.95,		/* Nature Walks in Southern New Hampshire */
	12.95,		/* Nature Walks in the New Hampshire Lakes Region */
	5.99, 		/* Primary Crime	 */
	12.00,		/* Rolling The Sun	*/
	17.00,		/* Tahirih Unveiled	*/
	15.00, 		/* Tales of the Francois Vase	 */
	12.00, 		/* The Island Queen	*/
	8.00, 		/* The Ossabaw Book of Hours	 */
	26.00		/* This Desired Place; The Isles of Shoals	*/
	);



function createPage()
{
	arrsize = titles.length;
	
	document.write('<table border="1" width="80%">');
	document.write('<tr bgcolor="#aaaaaa">');
	document.write('<td width="10%" align="center"><b> Quantity </b></td>');
	document.write('<td align="center" ><b> Title </b> </td>');
	document.write('<td width="10%" align="center"><b> Price </b> </td>');
	document.write('<td width="10%" align="center"><b> Extended </b> </td>');
	document.write('</tr>');
	for (idx=0;idx<arrsize;idx++)
	{
		document.write('<tr>');
		document.write('<td> <input type="text" size="10" value="" > </td>');
		document.write('<td> ' + titles[idx] + '</td>');
		document.write('<td align="right"> $' + padNumber(prices[idx]) + '</td>');
		document.write('<td align="right"> <input type="text" size="10" value="" readonly > </td>');
		document.write('</tr>');
	}
	document.write('<tr>');
	document.write('<td colspan="3"><table width="100%"><tr><td>');
	document.write('<font size="-1">');
	document.write('Shipping and handling: $2.50 for the first book ordered.');
	document.write('<br />$.50 will be added for each additional book ordered.');
	document.write('</font></td>');
	document.write('</td>');
	document.write('<td align="right" > Shipping and handling:&nbsp; </td></tr></table>');
	
	document.write('<td align="right"> <input type="text" name="txtShip" size="10" value="" > </td>');
	document.write('</tr>');
	
	
	
	document.write('</table>');
}

function updateTotal()
{
	var total = 0;
	var count = 0;

	arrsize = titles.length;
	
	for (idx=0;idx<arrsize;idx++)
	{
		if (!isNaN(document.forms[0].elements[idx*2].value) )
		{
			if (document.forms[0].elements[idx*2].value > 0)
			{
				document.forms[0].elements[idx*2+1].value = padNumber(document.forms[0].elements[idx*2].value * prices[idx]);
				total = parseFloat(document.forms[0].elements[idx*2+1].value) + total;
				count = parseFloat(document.forms[0].elements[idx*2].value) + count;
			}
			else
				document.forms[0].elements[idx*2+1].value = "";
		}
	}

	//if (count <= 3)
	//	{shipping = 3;	}
	//else
	//	{shipping = 3 + ((count - 3)/2);}
	shipping = 2 + (count/2);

	document.forms[0].txtShip.value = padNumber(shipping);

	total+=shipping;
	total = roundNumber(total);
	total = padNumber(total);
	document.frmTotal.total.value = "$" + total;	
}

function roundNumber(num)
{
	num*=100;
	num = Math.round(num);
	var newnum = num/100;
	return newnum;
}

function padNumber(num)
{
	with (new Object(Math.round(100*num)+''))
	{
		 return substring(0,length-2)+'.'+substring(length-2,length) 
	} 
}

function startOver()
{
	arrsize = titles.length;
	
	for (idx=0;idx<arrsize;idx++)
	{
		document.forms[0].elements[idx*2].value = "";
		document.forms[0].elements[idx*2+1].value = "";
	}
	document.forms[0].txtShip.value = "";
	document.frmTotal.total.value = "";
}

function printOrderSheet()
{
	arrsize = titles.length;
	
	today = new Date();
	month = today.getMonth();
	date = today.getDate();
	year = today.getFullYear();
	showdate = (month+1) + '/' + date + '/' + year;

	var prevwnd=null;
    	prevWnd=window.open("","OrderForm","width=640,height=600,top=1,screeny=1,scrollbars=yes,resizable=yes,status=0");
    	prevWnd.document.open();
	prevWnd.document.writeln('<html><title> Appledore Books Order Form </title>');
	prevWnd.document.writeln('<body bgcolor=#ffffff>');
	prevWnd.document.writeln('<font size="+2" face="Arial,Helvetica" ><center><b><u> Appledore Books Order Form </u></b></center></font>');
	prevWnd.document.writeln('<br><font size="+0"><a href="javascript:window.print()">(click here to print this form)</a></font><p><hr noshade width="100%">');
	prevWnd.document.writeln('<u>Order date:  ' + showdate + '</u><br>');
	prevWnd.document.writeln('<p />Mail completed form with check or money order to: <p />');
	prevWnd.document.writeln('<center><font size=+3> Appledore Books </font><br />');
	prevWnd.document.writeln('<font size=+1>P.O. Box 174 <br>');
	prevWnd.document.writeln('Hancock, NH 03449-0174  </font></center><p>');
	prevWnd.document.writeln('<hr noshade width="100%">');
	prevWnd.document.writeln('<table border="0">');
	prevWnd.document.writeln('<tr><td> Ship to: </td><td> &nbsp;</td></tr>');
	prevWnd.document.writeln('<tr><td>&nbsp;</td><td><font size="+1">' + document.frmShipInfo.txtName.value + '</font></td></tr>' );
	prevWnd.document.writeln('<tr><td>&nbsp;</td><td><font size="+1">' + document.frmShipInfo.txtAddr1.value + '</font></td></tr>');
	if (document.frmShipInfo.txtAddr2.value)
		prevWnd.document.writeln('<tr><td>&nbsp;</td><td><font size="+1">' + document.frmShipInfo.txtAddr2.value + '</font></td></tr>');
	prevWnd.document.writeln('<tr><td>&nbsp;</td><td><font size="+1">' + document.frmShipInfo.txtCity.value + ', ');
	prevWnd.document.writeln('&nbsp;' + document.frmShipInfo.txtState.value + '  ' );
	prevWnd.document.writeln('&nbsp;' + document.frmShipInfo.txtZip.value + '</font></td></tr>');
	prevWnd.document.writeln('</table>');
	prevWnd.document.writeln('<hr width="100%">');
	prevWnd.document.writeln('&nbsp;&nbsp; Email:&nbsp;&nbsp;&nbsp;&nbsp;<i><b>' + document.frmShipInfo.txtEmail.value + '</b></i>');
	prevWnd.document.writeln('&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp; Phone:&nbsp;&nbsp;&nbsp;&nbsp;<i><b>' + document.frmShipInfo.txtPhone.value + '</b></i>');
	prevWnd.document.writeln('<hr width="100%">');
	prevWnd.document.writeln('<b>Please ship the following:</b><p />');
	prevWnd.document.writeln('<center>');
	prevWnd.document.writeln('<table width="80%" border="1">');
	prevWnd.document.writeln('<tr align="center"><td><b><font size="+1"> Quantity </font></b></td><td width="50%"><b><font size="+1"> Title </font></b></td><td><b><font size="+1"> Price </font></b></td><td><b><font size="+1"> Extended </font></b></td></tr>');

	for (idx=0;idx<arrsize;idx++)
	{
		if (!isNaN(document.forms[0].elements[idx*2].value) )
		{
			if (document.forms[0].elements[idx*2].value > 0)
			{
				prevWnd.document.writeln('<tr><td align="center">' + document.forms[0].elements[idx*2].value + '</td>');
				prevWnd.document.writeln('<td>' + titles[idx] + '</td><td align="center"> ' + padNumber(prices[idx]) + '</td>');
				prevWnd.document.writeln('<td align="right">' + document.forms[0].elements[idx*2+1].value + '</td></tr>');
			}
		}
	}		
	prevWnd.document.writeln('<tr><td colspan="3" align="right"> Shipping and Handling: </td><td align="right">' + document.forms[0].txtShip.value + '</td></tr>');
	prevWnd.document.writeln('<tr bgcolor="#eeeeee"><td colspan="3" align="right"><b> GRAND TOTAL: </b></td><td align="right"><b>' + document.frmTotal.total.value + '</b></td></tr>');
	prevWnd.document.writeln('</table>');
	prevWnd.document.writeln('<p /><hr width="50%">');
	prevWnd.document.writeln('<p />&nbsp;&nbsp;&nbsp;&nbsp; <i><b><font size="+1"> Thank-you for your order! </font></b></i>');
	prevWnd.document.writeln('<p /><hr width="50%">');
	prevWnd.document.writeln('</center>');

	prevWnd.document.writeln('</body></html>');
	prevWnd.document.close();
}



