// items array
var items = new Array()
items[ 0 ]= new Array("Johnny America, Issue One",2.50,"JA01",0,3);
items[ 1 ]= new Array("Johnny America, Issue Two",3.00,"JA02",0,3);
items[ 2 ]= new Array("Johnny America, Issue Three",4.00,"JA03",0,4);
items[ 3 ]= new Array("Johnny America, Issue Four",4.00,"JA04",0,4);
items[ 4 ]= new Array("Johnny America, Issue Five",8.00,"JA05",0,7);
items[ 5 ]= new Array("Johnny America, Issue Six",4.00,"JA06",0,4);
items[ 6 ]= new Array("Johnny America, Issue Seven",4.00,"JA07",0,4);
items[ 7 ]= new Array("Johnny America Intro Pack",12.00,"JA00",0,14);
items[ 8 ]= new Array("Johnny America One-Inch Pin",0.50,"MISC01",0,1); 
items[ 9 ]= new Array("Date with a Johnny America Representative",6.50,"MISC02",0,0); 
items[ 10 ]= new Array("Johnny America Twenty-Stick Matches",1.00,"MISC03",0,1);
items[ 11 ]= new Array("Johnny America Subscription: Issues 7 - 11",20.00,"SUB01",0,20);
items[ 12 ]= new Array("Johnny America Subscription: Issues 8 - 12",20.00,"SUB02",0,20);
items[ 13 ]= new Array("J.A. T-Shirt, Size YL",12.00,"T01-YL",0,12);
items[ 14 ]= new Array("J.A. T-Shirt, Size SS",12.00,"T01-SM",0,12);
items[ 15 ]= new Array("J.A. T-Shirt, Size MM",12.00,"T01-MM",0,12);
items[ 16 ]= new Array("J.A. T-Shirt, Size LL",12.00,"T01-LL",0,12);
items[ 17 ]= new Array("J.A. T-Shirt, Size XL",12.00,"T01-XL",0,12);

// accept item number and new quantity, check that input is a non-negative integer before changing array
function UpdateQty(item,newqty) {
 
	var oldstring = newqty; 
	var newstring = parseFloat(oldstring).toString(); 
	var InpValid=1; 
	
	if (oldstring.length == newstring.length && newstring != "NaN" && newstring > 0) { 
		items[item][3] = newqty;
	} else if (newstring == 0) {
		items[item][3] = 0;
	} else { alert("The Moon Rabbit Drinking Club & Benevolence Society Automaton finds non-negative integers insufficiently erotic to process.\n\nIf you're going to modify your quantities please remember your partner's needs.\n\nSometimes when I, the Moon Rabbit Drinking Club & Benevolence Society Web Automaton, think of negative numbers -- or alpha characters accidentally typed into fields clearly intended for numeric input -- I find myself overwhelmed with a nausea cured only by hard liquor and unprotected sex with partners of ambiguous quality."); 
	}
		WriteCart(); // either way, update quantity in array
}

// write shopping cart
function WriteCart() {

	var txt = "";
	txt += "<p><hr size=\"1\" /><div class=\"space\"></div><h1>Shopping Cart</h1><p>";
	
	txt += "<table cellpadding=\"5\" border=\"0\" cellspacing=\"5\" class=\"store-table\"><tr><td>"; // envelope table
	
	txt += "<form>";
	txt += "<table border=\"0\">";
	txt += "<tr><td align=\"left\"><font size=\"-2\">Qty.</font></td>" + 
			"<td align=\"left\"><font size=\"-2\">Description</font></td>" + 
			"<td align=\"right\"><font size=\"-2\">Item cost</font></td>" + 
			"<td align=\"right\"><font size=\"-2\">Subtotal</font></td></tr>";

	var total = 0;
	var weight = 0;
	var shipping = 0;
		
	var rowcheck = 0; // var to store even/odd for row color change
		
		// loop through ALL items
		for (i=0;i<items.length;i++) {
						
			if (items[i][3] > 0) { // if item is in cart
			
				if (rowcheck % 2) {
					txt += "<tr>"; 
				} else {
					txt += "<tr bgcolor=\"#FAF3FA\">";
				}
			
				rowcheck++;
			
				var subtotal = items[i][1] * items[i][3];
				total = total + subtotal;
			
				var subweight = items[i][4] * items[i][3];
				weight = weight + subweight;
	
				txt += "<td align=\"left\"><input type=text size=2 maxlength=2 value=" + items[i][3] + " onChange=\"UpdateQty(" 
			 		 + i + ",this.value);\"></input></td>";
			 	txt += "<td align=\"left\">" + items[i][0] + "</td>"; 
			 	txt += "<td align=\"right\"><i>$ " + items[i][1] + "</i></td>";			 
			 	txt += "<td align=\"right\">$ " + subtotal + "</td>";
			 	txt += "</tr>";
			 
			 } // item in cart
			
			// if item is in web page, create button
			if (document.getElementById(items[i][2])) {
				var blurb = "";
				
				if (items[i][3] == 0) {
					blurb += "<i>$ " +  items[i][1] + "</i><br />&nbsp;<br />";
					blurb += "<input type=button value=\"Add to Cart\" onClick=\"AddToCart('" + items[i][2] + "');\">";
				} else { 
					blurb += "<i>$ " +  items[i][1] + " ( x " + items[i][3] + " = $" + (items[i][1] * items[i][3]) + " )</i><br />&nbsp;<br />";
					blurb += "<input type=button value=\"Currently in cart &mdash; " + items[i][3] + ". Click to add another.\"" + 
					" onClick=\"AddToCart('" + items[i][2] + "');\">";
				}				
				document.getElementById(items[i][2]).innerHTML = blurb; 
			} // end create button
			
		} // end items loop
		
	if (document.forms.country.radio[1].checked) {
		shipping = weight * .30;
		shipping = Math.round(shipping*100)/100; 
	 	txt += "<tr><td colspan=3 align=\"right\"><br /><i>Additional postage to Mexico or Canada</i></td><td align=\"right\"><br />$ " 
	 	+ shipping + "</td></tr>";
	} else if (document.forms.country.radio[2].checked) {
		shipping = weight * .60;
		shipping = Math.round(shipping*100)/100; 
	 	txt += "<tr><td colspan=3 align=\"right\"><br /><i>Additional postage to places far away</i></td><td align=\"right\"><br />$ " 
	 	+ shipping + "</td></tr>";
	}
	
	total = total + shipping;
		
	txt += "<tr><td colspan=3 align=\"right\"><br /><i>Total</i></td><td align=\"right\"><br />$ " + total + "</td></tr></table>";
	txt += "</form>";
	
	txt += "</td></tr><tr><td align=\"right\"><br />"; // envelope table
	
	txt += checkout(shipping);
	
	txt += "</td></tr></table>"; // envelope table

	txt += "</p><hr size=\"1\" />";	
	
		
	if ( total > 0 ) { 
		document.getElementById('dyncarttop').innerHTML = txt;
		document.getElementById('dyncartbottom').innerHTML = txt;
	} else {
		document.getElementById('dyncarttop').innerHTML = "";
		document.getElementById('dyncartbottom').innerHTML = "";
	}
}

// creates code for paypal submit button, using items in global cart. shipping variable required.
function checkout(shipping) {

 	var formmethod = "";
	var paypal_num = 0;
	formmethod +="<form action=\"https://www.paypal.com/us/cgi-bin/webscr\" method=\"post\">";
	formmethod +="<input type=\"hidden\" name=\"cmd\" value=\"_cart\">"; 
 	formmethod +="<input type=\"hidden\" name=\"upload\" value=\"1\">";
	formmethod +="<input type=\"hidden\" name=\"business\" value=\"orders@johnnyamerica.net\">";
	formmethod +="<input type=\"hidden\" name=\"cancel_return\" value=\"http://www.johnnyamerica.net/store/\">";
	formmethod +="<input type=\"hidden\" name=\"currency_code\" value=\"USD\">";

	for (i=0;i<items.length;i++) {
		if (items[i][3] > 0) { // if item is in cart
			paypal_num++;
			formmethod += "<input type=\"hidden\" name=\"item_name_" + paypal_num + "\" value=\"" + items[i][0] + "\">";
			formmethod += "<input type=\"hidden\" name=\"amount_"   + paypal_num  + "\" value=\"" + items[i][1] + "\">";
			formmethod += "<input type=\"hidden\" name=\"quantity_" + paypal_num  + "\" value=\"" + items[i][3] + "\">";
		}
	}
	
	if ( shipping > 0 ) { // adds cart-wide handling charge if applicable
		formmethod += "<input type=\"hidden\" name=\"item_name_" + (paypal_num + 1)  + "\" value=\"" + "Shipping Surcharge" + "\">";
		formmethod += "<input type=\"hidden\" name=\"handling_cart\" value=\"" + shipping + "\">";
	}

	formmethod += "<input type=\"image\" src=\"http://www.paypal.com/en_US/i/btn/x-click-but01.gif\"" + 
				  " name=\"submit\" alt=\"Make payments with PayPal - it's electric!\">";
	formmethod += "</form><div class=\"space\"></div>";
	return formmethod;
}


// takes item code, returns real position in array
function RealProd( item ) {
	for (i=0;i<items.length;i++)
		if ( item == items[i][2] )
			return i;
}

// accepts item code, increments item in cart
function AddToCart( item ) {
	items[RealProd(item)][3]++;
	WriteCart();	
}