var total=0; 
var quant=0;
var f=0;

function checkout() {
	mycookies=new Array(20);
	var i=0;
	var startpoint=0;
	whole_len=document.cookie.length;
	while(whole_len!=startpoint && i< 20) {
    	if ((temlen=document.cookie.indexOf(";",startpoint))!=-1) {
			mycookies[i]=document.cookie.substring(startpoint,temlen);
			i++;
			startpoint=temlen+1;
     	} // end if
     	else if (startpoint != whole_len) {
	        mycookies[i]=document.cookie.substring(startpoint,whole_len);
	        i++;
	        startpoint=whole_len;
		} // end else
	} // end while
    printtitle();
    for (j=0;j<i;j++) { //filter out cookies that are not used by shopping cart
		if((mycookies[j].substring(0,6)=="reeski")||(mycookies[j].substring(0,7)==" reeski")) getCookie(mycookies[j]);
     } // end for
	 printtotal();
} // end view_cart()

function getCookie (acookie) {
	clen=acookie.length;
    nlen=acookie.indexOf("=",0);
    if (acookie.substring(0,1)==" ") cname=acookie.substring(7,nlen);
//    if (acookie.substring(0,1)==" ") cname=acookie.substring(5,nlen);
    else cname= acookie.substring(6,nlen); // cookie name should start with 'reeski'
//    else cname= acookie.substring(4,nlen);
    bname=acookie.substring(nlen+1,clen);
    sname_len=cname.indexOf("+",0);
//    store=cname.substring(0,sname_len);
    dlen=cname.indexOf("+",sname_len+1);
    product=cname.substring(sname_len+1,dlen)
//    descrip=cname.substring(sname_len+1,dlen)
    descrip=cname.substring(dlen+1,nlen);
//    product=cname.substring(dlen+1,nlen);
    qlen=bname.indexOf("+",0);
    quant=bname.substring(0,qlen);
    if(quant==0) document.cookie="reeski"+cname+"=bb; expires=Saturday, 16-Sep-90 23:59:59 GMT; path=/"+"";
//    if(quant==0) document.cookie="oops"+cname+"=bb; expires=Saturday, 16-Sep-90 23:59:59 GMT; path=/"+"";
//    rlen=bname.indexOf("+",qlen+1);
//    refer=bname.substring(qlen+1,rlen);
    price=bname.substring(qlen+1,clen-nlen);
//    price=bname.substring(rlen+1,clen-nlen);
    price=price_format(price);
    if(quant!=0)
	{
		subtotal=price*quant;
		total=total+subtotal;
		subtotal=price_format(subtotal);
	}
    printcontent();
}

function printtitle(){
tabletitle="<table width=100% border=0 cellspacing=0 cellpadding=20> \
	<tr> \
		<td> \
			<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#CCCCCC> \
				<tr> \
					<td> \
						<table width=100% align=center border=0 cellspacing=1 cellpadding=2> \
							<tr bgcolor=#FFCC00 align=center> \
								<td><img src=images/cart_product.gif width=41 height=20 border=0></td> \
								<td><img src=images/cart_description.gif width=59 height=20 border=0></td> \
								<td><img src=images/cart_each.gif width=24 height=20 border=0></td> \
								<td><img src=images/cart_quantity.gif width=47 height=20 border=0></td> \
								<td><img src=images/cart_subtotal.gif width=45 height=20 border=0></td> \
							</tr>";
document.write(tabletitle);
}

function printcontent(){
  if(quant!=0){
    document.write("<tr align=center bgcolor=#FFFFFF><td class=copy>"+product+"</td><td class=copy>");
    document.write(descrip+"&nbsp;</td><td class=copy>$"+price+"</td><td class=copy>");
    document.write(quant+"</td><td class=copy>$"+subtotal+"</td>");
	f++;
    document.write("<input type=hidden name=\"item#"+f+"\" value=\""+quant+" "+product+", "+descrip+", $"+subtotal+"\"></tr>");
   }
}

function printtotal(){
	if(quant!=0){
		bottomline1 = "<tr bgcolor=#EEEEEE> \
						<td colspan=4 align=right><img src=images/cart_total.gif width=34 height=20 \
						border=0></td> \
						<td align=center class=copy><b>$";
		document.write(bottomline1);

		total=price_format(total);
		document.write(total);

		bottomline2 = "</b><input type=hidden name=grandtotal value=\""+total+"\"></td></tr></table></td></tr></table></td></tr></table>";
		document.write(bottomline2);
	}
	else document.write("</table></td></tr></table></td></tr></table>");
}

function price_format(tmpprice){
// get rid off the extra digits behind "."
   dot = tmpprice.toString().indexOf(".");
   if(dot!=-1)
      tmpprice=tmpprice.toString().substr(0,dot+4);
   tmpprice=Math.round(tmpprice*100)/100;
// add "0" if necessary
   dot = tmpprice.toString().indexOf(".");
   if(dot ==-1)
      tmpprice=tmpprice.toString()+".00";
   else if((tmpprice.toString().length-dot)==2)
      tmpprice=tmpprice.toString()+"0";
   return tmpprice;
}

/********** FORM VALIDATION ********************/
// Global variables needed for the validateForm function.
// The data for these arrays is stored on each individual template. :ML:
var requiredFields=new Array();
var errorMessages=new Array();
var r=0;
var selectFieldArray=new Array();

function validateForm(theForm) {
	var finalError="We still need the following required field(s):\n\n";
	var errorFlag=false;
	var firstField=null;
	
	if (document.checkout.useshippingaddress.checked==false) {
		errorMessages[r] = "Purchaser's Name\n";
		requiredFields[r] = "billname"; r++;

		errorMessages[r] = "Purchaser's Address\n";
		requiredFields[r] = "billaddr1"; r++;

		errorMessages[r] = "Purchaser's City\n";
		requiredFields[r] = "billcity"; r++;

		errorMessages[r] = "Purchaser's State\n";
		requiredFields[r] = "billstate"; r++;

		errorMessages[r] = "Purchaser's Country\n";
		requiredFields[r] = "billcountry"; r++;

		errorMessages[r] = "Purchaser's Postal Code\n";
		requiredFields[r] = "billzip"; r++;
	}
	if (document.checkout.paymentmethod[0].checked==true) {

		errorMessages[r] = "Credit Card Type\n";
		requiredFields[r] = "creditcardtype"; r++;

		errorMessages[r] = "Credit Cardholder's Name\n";
		requiredFields[r] = "cardholdername"; r++;

		errorMessages[r] = "Credit Card Number\n";
		requiredFields[r] = "cardnumber"; r++;

		errorMessages[r] = "Credit Card Expiration Date\n";
		requiredFields[r] = "expires"; r++;

	}
	if (document.checkout.nomfrpref.checked==false) {
		errorMessages[r] = "Manufacturer Preferences\n";
		requiredFields[r] = "mfrpref1"; r++;
	}
	if (document.checkout.usemyboards.checked==true) {
		errorMessages[r] = "Details concerning the boards you will be recycling\n";
		requiredFields[r] = "customspecs"; r++;
	}
	
		
	for (var i=0;i<requiredFields.length;i++) { 	// Check all required fields for empty
		var theField=eval("document.forms[0]."+requiredFields[i]);
		if (theField.value=="") {
			finalError+=errorMessages[i]
			errorFlag=true;
			if (firstField==null) firstField=theField;
		}
	}
	if (errorFlag) {	// If there are missing fields, alert the user
		alert(finalError);
		firstField.focus();
		window.scrollBy(0, -40);
	} else theForm.submit(); // If all required information is on the page, submit the form
}


	// Since the text of select fields is not passed to the server,
	// this code takes the text, joins it with the value, and places the
	// complete string into a hidden field. :ML:
//		for (var i=0; i<theForm.speaker_assets.length; i++) {
//			var titleDesc = theForm.speaker_assets.options[i].text
//			var titleDescArray = titleDesc.split(" - ");
//			selectFieldArray[i] = titleDescArray[0] + "|" + titleDescArray[1] + "|" + theForm.speaker_assets.options[i].value;
//		}
//		theForm.speaker_assets_data.value = selectFieldArray;
		//alert(theForm.speaker_assets_data.value);
