//Link to West Marine model number pop-up if they click on logo.
//var x=null, y=null, z=null;
//var pd = new Array();		

//function win(x,y) {PDesc=pd[x]; MNum=y;
	//z=window.open('wmpPop.htm','Popup',' menubar=no,width=550,height=250'); //return true;
//	};
//----------------------------------------------------------------------------------------------------------------------------------------
//Declare global variables
OrderList = new Object;
var total, grandtotal, shipweight, postage, howship, shiprate, errorFlag, FormData, Tax, description, POA; 
var JustEmail=false; 
var SalesNumber=0; 
var InCheckout;	//Flag to limit form updates if not in checkout.
var Country; //Numerical country - 0=USA
var SCV;	//Shipping Country Value is COUNTRYn  where n is shipping rate table number
var SCN;	//Shipping Country Name is the COUNTRY part of above
var SCR;	//Shipping Country Rate is the n part of above.
//Orders[1] through Orders[N} are quantity ordered per Catalog file items stored in cookie elements 2-N
//Orders[0] contains customer information below
//Define FormData which is unescaped from orders[0] which is the first element in the cookies. 
//These items must match the sequence in the checkout form to allow FillForm() etc., to operate 
//The FormData only contains the form from FullName to the end of the form.  To get the index of elements
//  of FormData you have to subtract FullName from the index in the form
//   CUSTOMER INFORMATION
var count=0; ShipToZip = count;			//Entry box for shipping zip to calculate rates.  Auto fills shipping zip below
count++; var Commercial=count;			//Checkbox for Commercial discount if UPS
count++;  // dummy to keep radio boxes on an increment of 2 for StoreForm, below
count++; var Priority =count;			//Radiobox for selecting priority mail
count++; var QuotePriority=count;		//Read Only text box to display priority mail cost
count++; var Express = count;			//Radiobox for express mail
count++; var QuoteExpress=count;		//
count++; var UPSg=count;				//Radiobox UPS ground
count++; var QuoteUPSg=count;			//
count++; var UPS3d=count;
count++; var QuoteUPS3d=count;
count++; var UPS2d=count;
count++; var QuoteUPS2d=count;
count++; var UPSnd=count;
count++; var QuoteUPSnd=count;
count++; //VoidShip=count;
count++; //OrderDetails = count;
count++; var FullName = count;
count++; var DayPhone = count;
count++; //NightPhone = count;
count++; //BillAddress = count;
count++; //BillCity = count;
count++; //email = count;
count++; var BillState =count;
count++; //BillZip = count;
count++; //BillCountry = count;
count++; var ShipName = count;
count++; //ShipDayPhone = count;
count++; //ShipNightPhone = count;
count++; //ShipAddress = count;
count++; //ShipCity = count;
count++; //Shipemail = count;
count++; //ShipState = count;
count++; //ShipZip = count;
count++; //ShipCountry = count;
count++; InvoiceNumber=count;
var EndOfForm = count;

function StoreForm(f)	{			//Save form entries in FormData string in case they wander and come back
	if(!InCheckout) return 0;  // Omit refresh data if not in correct document.
	FormData=this.document.forms[f].elements[ShipToZip].value+"^";  
	for( i=Commercial; i<FullName; i++) FormData += (this.document.forms[ f ].elements[i].checked + "^");
	for( i=FullName; i<=EndOfForm; i++) FormData += (this.document.forms[ f ].elements[i].value + "^"); 
		//alert("made FormData =   "+FormData);
	orders[0]=escape(FormData);  	// The Name, address stuff, separated by ^
};

function FillForm(f) {						//Replaces name  address stuff if previously eneterd
	if(FormData[DayPhone] >"") {              // populate form only after valid data stored at least once - phone always has something.
		this.document.forms[f].elements[ShipToZip].value=FormData[0]; 
		for( i=Commercial; i<FullName; i++) { 				//>  
			 self.document.forms[f].elements[i].checked = false;  // restore radio shipping buttons
			if(FormData[i]=="true") self.document.forms[f].elements[i].checked = true; 
		};
		for( i=FullName; i<=EndOfForm; i++) { //alert(i+"  "+FormData[i]);
			self.document.forms[f].elements[i].value = FormData[i]; //>
		};
	}; 
};	
		
function RefreshCookie(f) {
	StoreForm(f);  // Assemble name and address stuff if in checkout
	PutCookie(); 
};		
		
function fix2(X) {
	if(isNaN(X)==false) {				//Don't add .00 to text entries
		X=Math.round(X*100)/100;
		X=""+X;
		if(X.search(/\./)==-1) X+=".";
		while (X.search(/\./)>X.length-3) 
			{X+="0";}; 
	}
	return X
};

function Warning(X) {
	alert(X);
	errorFlag=true;
};

function populateForm(y,x,z) {		// Put data from y into x and z 
errorFlag=false;
if (shipweight>0&&shiprate==99&&orders[N]==0&&SCN!="USA") { 
	
	if(confirm(ShippingErrorMessage)) 	errorFlag=true;
	};
document.forms[x].AMOUNT.value=fix2(grandtotal);
document.forms[x].TAX.value=Tax; 
document.forms[x].DESCRIPTION.value=description;   //orders.slice(1); 
document.forms[x].USER1.value="POA = $"+ orders[N];
document.forms[x].USER2.value="Total = $" +fix2(total);
document.forms[x].USER3.value="Weight = " + shipweight;
document.forms[x].USER4.value="Ship = $" + postage; 
if(postage==0 && shipweight>0){ //Shipweight is zero for PayOnAccount
	alert("You have not yet selected a shipping method.");
	errorFlag=true;
}
document.forms[x].USER5.value=Version;
document.forms[x].USER6.value="Ship via " + howship;
document.forms[x].USER7.value="Residential"; if(document.forms[y].Commercial.checked)document.forms[x].USER7.value="Commercial" ;
document.forms[x].USER8.value="Retail"; 
if (wholesale) document.forms[x].USER8.value="WHOLESALE " + vendor;
document.forms[x].USER9.value=description;			//to copy to email submit
document.forms[x].USER10.value=cookieCounter;
document.forms[x].INVOICE.value=SalesNumber;
document.forms[y].INVOICE.value=SalesNumber;		
document.forms[z].INVOICE.value=SalesNumber        //Fill in for email part too.
document.forms[x].NAME.value=document.forms[y].FullName.value;
document.forms[z].NAME.value=document.forms[y].FullName.value;		//Fill in email too
document.forms[x].NAMETOSHIP.value=document.forms[y].ShipName.value;

document.forms[x].ADDRESS.value=document.forms[y].BillAddress.value;
document.forms[x].ADDRESSTOSHIP.value=document.forms[y].ShipAddress.value;

document.forms[x].CITY.value=document.forms[y].BillCity.value;
document.forms[x].CITYTOSHIP.value=document.forms[y].ShipCity.value;
 
document.forms[x].STATE.value=document.forms[y].BillState.value;
document.forms[x].STATETOSHIP.value=document.forms[y].ShipState.value;
document.forms[x].ZIP.value=document.forms[y].BillZip.value;
document.forms[x].ZIPTOSHIP.value=document.forms[y].ShipZip.value; 
document.forms[x].COUNTRY.value=document.forms[y].BillCountry.value;

document.forms[x].COUNTRYTOSHIP.value = SCN;
document.forms[x].PHONE.value=document.forms[y].DayPhone.value;
if (document.forms[x].PHONE.value=='### ### ####') document.forms[x].PHONE.value='';
document.forms[x].PHONETOSHIP.value=document.forms[y].ShipDayPhone.value;
if (document.forms[x].PHONETOSHIP.value=='### ### ####') document.forms[x].PHONETOSHIP.value='';

document.forms[x].EMAIL.value=document.forms[y].email.value;
document.forms[x].EMAILTOSHIP.value = document.forms[y].Shipemail.value;

if (grandtotal<1&&orders[N]<1&&JustEmail!=true)  Warning('->Sorry, your order does not meet the $1.00 minimum. \n If items are entered, click JUST EMAIL ORDER without processing credit card, \n and we will call you for the credit card data.');  
if (document.forms[x].NAME.value.length == 0) Warning("You must enter your name as shown on your credit card");
if (errorFlag) return false;	// Don't report remainder if missed filling form
if (document.forms[x].ADDRESS.value.length == 0) Warning("We need your street address entered");
if (document.forms[x].CITY.value.length == 0) Warning("Your address CITY is missing");
if (errorFlag) return false;
if (document.forms[x].EMAIL.value.search(/\@/)==-1) Warning("Your email address is missing or appears faulty");
if (document.forms[x].PHONE.value.length <10 ) Warning("->We must have your phone number including area code in case we need to contact you about this order");
if (document.forms[x].COUNTRY.value.length < 2 ) Warning("->Please enter a Country for your billing address");
if (document.forms[x].COUNTRY.value=="USA") {		 //Skip BILLING state & zip check if foreign.
	if (document.forms[x].STATE.value.length <2) Warning("Your STATE information is not correct");   //>
	if (isNaN(document.forms[x].ZIP.value) || document.forms[x].ZIP.value.length !=5) Warning("Please enter a 5 digit BILLING zip code in USA, or correct COUNTRY in billing address");  //>
	};
if (document.forms[y].ShipName.value=='') Warning("Ship to NAME is missing");
if (document.forms[y].ShipAddress.value=='') Warning("Ship to ADDRESS is missing");
if (document.forms[y].ShipCity.value=='') Warning("Ship to CITY is missing");
if (document.forms[y].ShipState.value=='') Warning("Ship to STATE is missing");
if (SCN=="USA") {		 //Skip SHIPPING state & zip check if foreign.
	if (isNaN(Strip(document.forms[y].ShipToZip.value)) || document.forms[y].ShipToZip.value=="" || Strip(document.forms[y].ShipToZip.value) > 99999) {
		Warning("*****************************************************\nPlease go to the top of the page and enter the SHIP TO zip code.\n*****************************************************\n or correct COUNTRY in shipping address.");
		};
	};
if (errorFlag) return false;

document.forms[z].submitteremail.value=document.forms[y].email.value;		
document.forms[z].USER1.value=document.forms[x].USER1.value;   
document.forms[z].USER2.value=document.forms[x].USER2.value; 
document.forms[z].USER3.value=document.forms[x].USER3.value; 
document.forms[z].USER4.value=document.forms[x].USER4.value; 
document.forms[z].USER5.value=document.forms[x].USER5.value; 
document.forms[z].USER6.value=document.forms[x].USER6.value; 
document.forms[z].USER7.value=document.forms[x].USER7.value; 
document.forms[z].USER8.value=document.forms[x].USER8.value; 
document.forms[z].USER9.value=document.forms[x].USER9.value; 
document.forms[z].USER10.value=document.forms[x].USER10.value; 
//alert("filled z");
};

var submitEnable=false;		//Locks out sending email on ENTER key while filling form.
if (parseInt(navigator.appVersion)<4) submitEnable=true;        //>


function CheckEmail(f)  { 			//Used for email  order submission
	errorFlag=false; 
	if (document.forms[f].FullName.value.length == 0) Warning("You forgot to enter your name");
	if (errorFlag) return false;	// Don't report remainder if missed filling form
	if (document.forms[f].email.value.search(/\@/)==-1) Warning("Your email address is missing or appears faulty");
	if (document.forms[f].DayPhone.value=='### ### ####') document.forms[f].DayPhone.value='';
	if (document.forms[f].DayPhone.value.length <10) Warning("We must have your phone number including area code in case we need to contact you about this order");  //>
	if (errorFlag) return false;
	document.forms[f].INVOICE.value=SalesNumber;  
	document.forms[f].submitteremail.value=document.forms[f].email.value;   //Add reply email address
	populateForm(0,1,4);
	if (errorFlag) return false;
	w=window.open("EmailTellum.htm", "tellum", "width=600,height=400");
	return submitEnable;
};

function EmptyCart(f) {
	for(var i=1; i<=N; i++) { 			//Leave orders[0] with name/address infro
			orders[i]=0;
			};
	orders[N+1]="ENDYANDINA";
	document.forms[f].PaidOnAccount.value="";
	PutCookie();
	MakeList(f); 
};

function UpdateCheckout() { 
	QuoteShipping();
	MakeList(0);
	RefreshCookie(0);
};

function AddShippingLine(s) {
	v="";postage=0;
	if(s!=99) v=document.forms[0].elements[s].value;
	if(SCN!="USA") OrderList += SCN + " Global ";
	if(v!="") {OrderList += howship + " at cost = " + v + "\r";
		postage=v.slice(1); 
	};
	if(v=="") {	if(shipweight>0 && !document.getElementById("ShipOnAccount").checked && MakeNumb(document.getElementById("PaidOnAccount").value)==0) OrderList +="\r* * * SHIPPING METHOD HAS NOT BEEN SELECTED * * *\rIf in USA, enter zip above and select a shipping method.\r";
	};
};

function CalculateShipping(){ 
	shiprate=99;	//A value to indicate if anything entered yet
	QuoteShipping();	 // make sure boxes are filled
	if(this.document.forms[0].elements[Priority].checked == true) { howship = "Priority mail"; shiprate=QuotePriority;};	
	if(this.document.forms[0].elements[Express].checked == true) { howship = "Express mail"; shiprate=QuoteExpress;};	
	if(this.document.forms[0].elements[UPSg].checked == true) { howship = "UPS Ground"; shiprate=QuoteUPSg;};
	if(this.document.forms[0].elements[UPS3d].checked == true) { howship = "UPS 3 Day Select"; shiprate=QuoteUPS3d;};
	if(this.document.forms[0].elements[UPS2d].checked == true) { howship = "UPS Second Day Air"; shiprate=QuoteUPS2d;};
	if(this.document.forms[0].elements[UPSnd].checked == true) { howship = "UPS Next Day Air"; shiprate=QuoteUPSnd;}; 
	AddShippingLine(shiprate); 
 };
	
function EditItem(x,f){ 
	var i = (window.document.forms[ f ].Edit.value); 	//var i is catalog number
	if (i<1) return;			//>Can't edit customer data
	if((orders[i] != null) && (orders[i] > 0) && i<N ) {     //> N is "paid on account", adjusted in the POA box.
		orders[i] = orders[i] * 1 + x;
		if (i == 2) orders[i] = 0;      // Can't adjust discount - has to be re-entered.  Regenerates if POA box > 1K
		PutCookie(); 
		MakeList(f);}
	else	alert("Enter an EXISTING CATALOG number from the shopping cart list.\r\rOR\r\rClick BUY on catalog item to create a fresh entry.");
};

function AddSalesTax(f){ 
	Tax="";
	if (InSC(f)) {
		Tax=fix2(total*0.06);
		OrderList += "\rSouth Carolina Sales Tax = $" + Tax;
		};
	
	};
	
function InSC(f){ 
	if ((orders[13]&1)==1) return 0;		//bit 1 of orders[13] suppresses sales tax.
	if(!InCheckout) return 0;  // Omit test if not in correct document.
	if(document.forms[f].ShipState.value=="SC") return 1;
	if(document.forms[f].ShipState.value=="SOUTHCAROLINA") return 1;
	if(document.forms[f].ShipState.value=="SCAROLINA") return 1;
	
	};
	
function Strip(x){
	var Stripx = "" ;
	stripcharacters = ". '=*_:/;,@#&\n";
	for (i=0; i<x.length;i++)	{ 		//>
		var c = x.charAt(i);
		if(c=="\r") c="+";
		if(stripcharacters.indexOf(c)== -1) Stripx +=c.toUpperCase();
		};
		return Stripx;
	};

++Javacount;
if(JavaCheck==true)alert("OrderJS add 1="+Javacount);



