/***************************
 * JavaScript based shopping trolley for the Shadowforge Website
 *
 * Pre-requisites:
 * 		JavaScript 1.2
 *	 	toolkit.js
 *  	products.js
 * 		shadowforge.js
 *
 ********** TO DO **********
 *
 * Handle PnP and GST
 *
 ***************************/

var TROLLEY_URL = "show_trolley.html";

function addAllItems( args ) {
	// Delete the submit button args
	delete args["buyall.x"];
	delete args["buyall.y"];

	// Enumerate all items and add them to the trolley
	for( var code in args ) {
		if( args[code] && parseInt( args[code] ) > 0 ) { // Don't try to add null or < 1 qty
			if( trolley[code] )
				trolley[code] = parseInt( trolley[code] ) + parseInt( args[code] );
			else
				trolley[code] = parseInt( args[code] );
		}
	}
}

function addSingleItem( args ) {
	var code = null;

	// Find the submit button arg
	for( var submitCode in args ) {
		if( submitCode.substr( 0, 3 ) == "buy" ) {
			// Extract the product code from the submit button arg ( buy<code>.x ), ignore the buy<code>.y arg
			var dotpos = submitCode.indexOf( "." );
			code = submitCode.slice( 3, dotpos );
		}
	}
	// If we found a product code then get the value of the <code> arg and add to trolly
	if( code && args[code] && parseInt( args[code] ) > 0 ) { // Make sure we found a product code and the qty is not null or < 1
		if( trolley[code] )
			trolley[code] = parseInt( trolley[code] ) + parseInt( args[code] );
		else
			trolley[code] = parseInt( args[code] );
	}
}

function list_items_in_trolley( updateable ) {
	document.write( "  <TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0 WIDTH=500>\n" );
	document.write( "    <TR>\n" );
	document.write( "      <TD width=50 align=center><B>CODE</B></TD>\n" );
	document.write( "      <TD width=270><B>DESCRIPTION</B></TD>\n" );
	document.write( "      <TD width=100 align=center><B>RRP ($AU)</B></TD>\n" );
	document.write( "      <TD width=30 align=center><B>QTY</B></TD>\n" );
	document.write( "      <TD width=50 align=center><B>TOTAL</B></TD>\n" );
	document.write( "    </TR>\n" );
	var total = 0;
	for( var item in trolley ) {
		if( ( typeof trolley[item] == 'function' ) || ( item.charAt( 0 ) == "$" ) ) continue; // Don't enumerate functions or "hidden" properties
		document.write( "  <TR>\n" );
		document.write( "      <TD align=center>" + item + "</TD>" );
		document.write( "<TD>" + products[item].description + "</TD>" );
		document.write( "<TD align=center>" + formatCurrency( products[item].price ) + "</TD>" );
		if( updateable )
			document.write( "<TD align=center><INPUT TYPE=text NAME='" + item + "' VALUE=" + trolley[item] + " MAXLENGTH=3 SIZE=3></TD>" );
		else
			document.write( "<TD align=center>" + trolley[item] + "</TD>" );
		document.write( "<TD align=center>" + formatCurrency( trolley[item] * products[item].price ) + "</TD>\n" );
		document.write( "    </TR>\n" );
		total += trolley[item] * products[item].price;
	}
	document.write( "    <TR><TD COLSPAN=4 ALIGN=RIGHT><B>TOTAL</B></TD><TD align=center>" + formatCurrency( total ) + "</TD></TR>\n" );
	document.write( "  </TABLE>\n" );
}

function show_trolley() {
	// Show contents of trolly in a form that allows user to change qty, delete items or produce an order form.
	document.write( "<HTML>\n" );
	document.write( "<HEAD>\n" );
	document.write( "  <LINK REL='stylesheet' HREF='shadowforge.css' TYPE='text/css'>\n" );
	document.write( "</HEAD>\n" );
	document.write( "<BODY>\n" );
	document.write( "<CENTER>\n" );
	document.write( "<FORM ACTION='" + TROLLEY_URL + "' METHOD=GET>\n" );
	list_items_in_trolley( true );
	document.write( "  <P>\n" );
	document.write( "  <INPUT TYPE=submit NAME=submit VALUE='Update Order'>\n" );
	document.write( "  <INPUT TYPE=submit NAME=submit VALUE='Empty Trolley'>\n" );
	document.write( "  <INPUT TYPE=submit NAME=submit VALUE='Generate Order Form'>\n" );
	document.write( "</FORM>\n" );
	document.write( "</CENTER>\n" );
	document.write( "</BODY>\n" );
}

function show_deatils_form( user_details ) {
	document.write( "<HTML>\n" );
	document.write( "<HEAD>\n" );
	document.write( "  <LINK REL='stylesheet' HREF='shadowforge.css' TYPE='text/css'>\n" );
	document.write( "</HEAD>\n" );
	document.write( "<BODY>\n" );
	document.write( "<CENTER>\n" );
	document.write( "Please enter in delivery details.\n" );
	document.write( "<P>" );
	document.write( "<FORM ACTION='" + TROLLEY_URL +"' METHOD=GET>\n" );
	document.write( "<TABLE>\n" );
	document.write( "  <TR>\n" );
	document.write( "    <TD ALIGN=right><B>Name</B></TD>\n" );
	document.write( "    <TD><INPUT TYPE=text NAME='name' VALUE='" + ( ( user_details.name ) ? user_details.name : "" ) + "' SIZE=40></TD>\n" );
	document.write( "  </TR>\n" );
	document.write( "  <TR>\n" );
	document.write( "    <TD ALIGN=right><B>Address</B></TD>\n" );
	document.write( "    <TD><INPUT TYPE=text NAME='address' VALUE='" + ( ( user_details.address ) ? user_details.address : "" ) + "' SIZE=40></TD>\n" );
	document.write( "  </TR>\n" );
	document.write( "  <TR>\n" );
	document.write( "    <TD ALIGN=right><b>Suburb</b></TD>\n" );
	document.write( "    <TD><INPUT TYPE=text NAME='suburb' VALUE='" + ( ( user_details.suburb ) ? user_details.suburb : "" ) + "' SIZE=25></TD>\n" );
	document.write( "  </TR>\n" );
	document.write( "  <TR>\n" );
	document.write( "    <TD ALIGN=right><b>State</b></TD>\n" );
	document.write( "    <TD><INPUT TYPE=text NAME='state' VALUE='" + ( ( user_details.state ) ? user_details.state : "" ) + "' SIZE=10></TD>\n" );
	document.write( "  </TR>\n" );
	document.write( "  <TR>\n" );
	document.write( "    <TD ALIGN=right><b>Postcode</b></TD>\n" );
	document.write( "    <TD><INPUT TYPE=text NAME='postcode' VALUE='" + ( ( user_details.postcode ) ? user_details.postcode : "" ) + "' SIZE=6></TD>\n" );
	document.write( "  </TR>\n" );
	document.write( "  <TR>\n" );
	document.write( "    <TD ALIGN=right><b>Country</b></TD>\n" );
	document.write( "    <TD><INPUT TYPE=text NAME='country' VALUE='" + ( ( user_details.country ) ? user_details.country : "" ) + "' SIZE=25></TD>\n" );
	document.write( "  </TR>\n" );
	document.write( "  <TR>\n" );
	document.write( "    <TD ALIGN=right><b>Contact phone number<BR>or email address</b></TD>\n" );
	document.write( "    <TD><INPUT TYPE=text NAME='contact' VALUE='" + ( ( user_details.contact ) ? user_details.contact : "" ) + "' SIZE=40></TD>\n" );
	document.write( "  </TR>\n" );
	document.write( "  <TR>\n" );
	document.write( "    <TD ALIGN=right><b>Payment method</b></TD>\n" );
	document.write( "    <TD><SELECT NAME='method'>\n" );
	if( user_details.method == "Credit Card" )
		document.write( "      <OPTION SELECTED VALUE='Credit Card'>Credit Card\n" );
	else
		document.write( "      <OPTION          VALUE='Credit Card'>Credit Card\n" );
	if( user_details.method == "Cheque" )
		document.write( "      <OPTION SELECTED VALUE='Cheque'>Cheque / Money Order ( $AU Only )\n" );
	else
		document.write( "      <OPTION          VALUE='Cheque'>Cheque / Money Order ( $AU Only )\n" );
	if( user_details.method == "COD" )
		document.write( "      <OPTION SELECTED VALUE='COD'>Cash On Delivery ( Australia Only )\n" );
	else
		document.write( "      <OPTION          VALUE='COD'>Cash On Delivery ( Australia Only )\n" );
	document.write( "    </TD>\n" );
	document.write( "  </TR>\n" );
	document.write( "  <TR>\n" );
	document.write( "    <TD ALIGN=right><b>Postage</b></TD>\n" );
	document.write( "    <TD><SELECT NAME='postage'>\n" );
	if( user_details.postage == "AU" )
		document.write( "      <OPTION SELECTED VALUE='AU'>+$6 ( Australia )\n" );
	else
		document.write( "      <OPTION          VALUE='AU'>+$7.5 ( Australia )\n" );
	if( user_details.postage == "NZ" )
		document.write( "      <OPTION SELECTED VALUE='NZ'>+10% ( New Zealand )\n" );
	else
		document.write( "      <OPTION          VALUE='NZ'>+10% ( New Zealand )\n" );
	if( user_details.postage == "UK" )
		document.write( "      <OPTION SELECTED VALUE='UK'>+20% ( UK / Europe )\n" );
	else
		document.write( "      <OPTION          VALUE='UK'>+20% ( UK / Europe )\n" );
	if( user_details.postage == "Other" )
		document.write( "      <OPTION SELECTED VALUE='Other'>+15% ( All Other Countries )\n" );
	else
		document.write( "      <OPTION          VALUE='Other'>+15% ( All Other Countries )\n" );
	document.write( "    </TD>\n" );
	document.write( "  </TR>\n" );
	document.write( "</TABLE>\n" );
	document.write( "<P>\n" );
	document.write( "<INPUT TYPE=submit NAME=submit VALUE='Generate Form'>\n" );
	document.write( "</FORM>\n" );
	document.write( "</CENTER>\n" );
	document.write( "</BODY>\n" );
}

function generate_orderform( user_details ) {
	document.write( "<CENTER>" );
	document.write( "<BIG><B>SHADOWFORGE MINIATURES ORDER FORM</B></BIG><BR>" );
	document.write( "P.O.Box 277 Gymea NSW 2227 Australia<BR>\n" );
	document.write( "PH/FAX +61 2 9522 0764 &nbsp; email: nth@shadowforge.com.au\n" );
	document.write( "<P>\n" );
	document.write( "</CENTER>\n" );
	document.write( "<B>PLACING AN ORDER:</B> Please refer to our ORDERING PAGE for full details.<BR>\n" );
	document.write( "<b>POSTAL CHARGES (IN AUSTRALIAN DOLLARS):</b><br>\n" );
	document.write( "AUSTRALIA: ADD $6 &nbsp;<br>NZ: ADD 10% <b>(MINIMUM $7.50)</b> &nbsp;<br> UK/EUROPE: ADD 20% <b>(MINIMUM $11.00)</b> &nbsp;<br> ALL OTHERS: ADD 15% <b>(MINIMUM $9.50)</b>.<br> FOR ORDERS OVER $200 WE WILL EMAIL YOU BACK WITH AN EXACT COST \n" );
	document.write( "<P>\n" );
	document.write( "<CENTER>\n" );
	list_items_in_trolley( false );
	document.write( "<BR>\n" );
	document.write( "<TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0 WIDTH=500>\n" );
	// Calculate total amount of order for working out % postages
	var total = 0;
	for( var item in trolley ) {
		if( ( typeof trolley[item] == 'function' ) || ( item.charAt( 0 ) == "$" ) ) continue; // Don't enumerate functions or "hidden" properties
		total += trolley[item] * products[item].price;
	}

	// Calculate GST and pnp
	var pnp =  0;
	var GST = 0;
	switch( user_details.postage ) {
		case 'AU':
			pnp = 5;
			GST = ( total + 5 ) * 0.1;
			break;

		case 'NZ':
			pnp = total * 0.1;
			GST = 0;
			break;

		case 'UK':
			pnp = total * 0.2;
			GST = 0;
			break;

		case 'Other':
			pnp = total * 0.15;
			GST = 0;
			break;
}
	document.write( "  <TR>\n" );
	document.write( "    <TD WIDTH=445 ALIGN=right><B>POSTAGE</B></TD>\n" );
	document.write( "    <TD WIDTH=55 ALIGN=center>" + formatCurrency( pnp ) + "\n" );
	document.write( "	 </TR>\n" );
	if( GST > 0 ) {
		document.write( "	 <TR>\n" );
		document.write( "	   <TD WIDTH=445 ALIGN=right><B>GST</B></TD>\n" );
		document.write( "	   <TD WIDTH=55 ALIGN=center>" + formatCurrency( GST ) + "</TD>\n" );
		document.write( "	   </TD>\n" );
		document.write( "	 </TR>\n" );
	}
	document.write( "	 <TR>\n" );
	document.write( "	   <TD WIDTH=445 ALIGN=right><B>GRAND TOTAL</B></TD>\n" );
	document.write( "	   <TD WIDTH=55 ALIGN=center>" + formatCurrency( total + GST + pnp ) + "</TD>\n" );
	document.write( "	   </TD>\n" );
	document.write( "	 </TR>\n" );
	document.write( "</TABLE>\n" );
	document.write( "</CENTER>\n" );
	document.write( "<P>\n" );
	document.write( "<TABLE WIDTH=100%>\n" );
	document.write( "  <TR>\n" );
	document.write( " 	 <TD ALIGN=right><B>Name:</B></TD>\n" );
	document.write( "    <TD COLSPAN=7>" + user_details.name + "</TD>\n" );
	document.write( "    </TD>\n" );
	document.write( "  </TR>\n" );
	document.write( "  <TR>\n" );
	document.write( " 	 <TD ALIGN=right><B>Address:</B></TD>\n" );
	document.write( "    <TD COLSPAN=7>" + user_details.address + "</TD>\n" );
	document.write( "    </TD>\n" );
	document.write( "  </TR>\n" );
	document.write( "  <TR>\n" );
	document.write( " 	 <TD ALIGN=right><B>Suburb:</B></TD>\n" );
	document.write( "    <TD>" + user_details.suburb + "</TD>\n" );
	document.write( "    </TD>\n" );
	document.write( " 	 <TD ALIGN=right><B>State:</B></TD>\n" );
	document.write( "    <TD>" + user_details.state + "</TD>\n" );
	document.write( "    </TD>\n" );
	document.write( " 	 <TD ALIGN=right><B>Postcode:</B></TD>\n" );
	document.write( "    <TD>" + user_details.postcode + "</TD>\n" );
	document.write( "    </TD>\n" );
	document.write( " 	 <TD ALIGN=right><B>Country:</B></TD>\n" );
	document.write( "    <TD>" + user_details.country + "</TD>\n" );
	document.write( "    </TD>\n" );
	document.write( "  </TR>\n" );
	document.write( "  <TR>\n" );
	document.write( " 	 <TD COLSPAN=3 ALIGN=right><B>Contact phone number or email address:</B></TD>\n" );
	document.write( "    <TD COLSPAN=5>" + user_details.contact + "</TD>\n" );
	document.write( "    </TD>\n" );
	document.write( "  </TR>\n" );

	switch( user_details.method ) {
		case "Credit Card":
			document.write( "  <TR>\n" );
			document.write( "    <TD ALIGN=right VALIGN=top><B>Payment method:</B></TD>\n" );
			document.write( "    <TD COLSPAN=7 VALIGN=top>\n" );
			document.write( "      <TABLE CELLSPACING=4>\n" );
			document.write( "        <TR>\n" );
			document.write( "          <TD><TABLE CELLSPACING=0 BORDER=1><TR><TD WIDTH=20>&nbsp;</TD><TD>MasterCard</TD></TR></TABLE></TD>\n" );
			document.write( "          <TD><TABLE CELLSPACING=0 BORDER=1><TR><TD WIDTH=20>&nbsp;</TD><TD>VisaCard</TD></TR></TABLE></TD>\n" );
			document.write( "          <TD><TABLE CELLSPACING=0 BORDER=1><TR><TD WIDTH=20>&nbsp;</TD><TD>BankCard</TD></TR></TABLE></TD>\n" );
			document.write( "          <TD>&nbsp;</TD>\n" );
			document.write( "        </TR>\n" );
			document.write( "        <TR>\n" );
			document.write( "          <TD><TABLE CELLSPACING=0 BORDER=1><TR><TD WIDTH=20>&nbsp;</TD><TD WIDTH=20>&nbsp;</TD><TD WIDTH=20>&nbsp;</TD><TD WIDTH=20>&nbsp;</TD></TR></TABLE></TD>\n" );
			document.write( "          <TD><TABLE CELLSPACING=0 BORDER=1><TR><TD WIDTH=20>&nbsp;</TD><TD WIDTH=20>&nbsp;</TD><TD WIDTH=20>&nbsp;</TD><TD WIDTH=20>&nbsp;</TD></TR></TABLE></TD>\n" );
			document.write( "          <TD><TABLE CELLSPACING=0 BORDER=1><TR><TD WIDTH=20>&nbsp;</TD><TD WIDTH=20>&nbsp;</TD><TD WIDTH=20>&nbsp;</TD><TD WIDTH=20>&nbsp;</TD></TR></TABLE></TD>\n" );
			document.write( "          <TD><TABLE CELLSPACING=0 BORDER=1><TR><TD WIDTH=20>&nbsp;</TD><TD WIDTH=20>&nbsp;</TD><TD WIDTH=20>&nbsp;</TD><TD WIDTH=20>&nbsp;</TD></TR></TABLE></TD>\n" );
			document.write( "        </TR>\n" );
			document.write( "      </TABLE>\n" );
			document.write( "    	 <BR>" );
			document.write( "    	 <B>Expiry Date:</B>___________ &nbsp;&nbsp;&nbsp;<B>Signature:</B>_______________________" );
			document.write( "    </TD>" );
			document.write( "  </TR>" );
			break;

 		case "Cheque":
			document.write( "  <TR>\n" );
			document.write( "    <TD ALIGN=right><B>Payment method:</B></TD>\n" );
			document.write( "    <TD COLSPAN=7>Cheque / Money Order ( $AU Only )</TD>" );
			document.write( "  </TR>\n" );
			break;

	  case "COD":
			document.write( "  <TR>\n" );
			document.write( "    <TD ALIGN=right><B>Payment method:</B></TD>\n" );
			document.write( "    <TD COLSPAN=7>Cash On Delivery ( Australia Only )</TD>" );
			document.write( "  </TR>\n" );
			break;
	}
	document.write( "  </TABLE>\n" );
}

var trolley = new Cookie( document, "trolley", 24, "/" );
trolley.load(); // Get existing trolley contents

var args = getArgs();
var submit = unescape( args.submit ); // Get the action type
submit = submit.replace( /\+/g, " " );
delete args.submit; 		// Then delete it so it isn't picked up as a trolley item
switch( submit ) {
	case 'add':
		if( args["buyall.x"] ) { // User wants to add all items
			addAllItems( args );
		} else { // User only wants to add one item
			addSingleItem( args );
		}
		trolley.store(); // Store updated trolley contents
		break;

//	case 'Update+Order':
	case 'Update Order':
		// Empty the trolley then add all items in the arg list
		trolley.remove();
		addAllItems( args );
		trolley.store(); // Store updated trolley contents
		show_trolley();
		break;

//	case 'Empty+Trolley':
	case 'Empty Trolley':
		trolley.remove();
		trolley.store(); // Store updated trolley contents
		show_trolley();
		break;

//	case 'Generate+Order+Form':
	case 'Generate Order Form':
		var user_details = new Cookie( document, "details" ); // Get users details from cookie to auto fill in form.
		user_details.load();
		// Show user details form
		show_deatils_form( user_details );
		break;

//	case 'Generate+Form':
	case 'Generate Form':
		// Set user details cookie
		var user_details = new Cookie( document, "details" );
		user_details.name = unescape( args.name ).replace( /\+/g, " " );
		user_details.address = unescape( args.address ).replace( /\+/g, " " );
		user_details.suburb = unescape( args.suburb ).replace( /\+/g, " " );
		user_details.state = unescape( args.state ).replace( /\+/g, " " );
		user_details.postcode = unescape( args.postcode ).replace( /\+/g, " " );
		user_details.country = unescape( args.country ).replace( /\+/g, " " );
		user_details.contact = unescape( args.contact ).replace( /\+/g, " " );
		user_details.method = unescape( args.method ).replace( /\+/g, " " );
		user_details.postage = unescape( args.postage ).replace( /\+/g, " " );
		user_details.store();

		generate_orderform( user_details );

		// Empty the trolley
		var trolley = new Cookie( document, "trolley", 24, "." );
		trolley.load();
		trolley.remove();
		break;

	case 'View Trolley':
//	case 'View+Trolley':
		show_trolley();
		break;
}
