
//--------------------------------------------- CATALOG FUNCTIONS ------------------------------------------------------------------------
var orders = new Array();				// number of each item ordered  NOTE orders[0] stores the name and address information for refreshing the form.
var redirect	 = "";				// Flag to test if we should go to the Paradise OrderThanks.
var Thanks;
var emailSent=true;				// Flag for special instructions.  Set to false if any entered.
var cookieCounter;
function CurrentIndex(X) {			// searches for ID in current catalog and returns index number.
	if (X!=null) {
		for(n=1;n<=N+1;n++) { 		//>
			if (X==ID[n]) return (n);
			};
		};
	};

function ZeroCookie() { 
	for(n=0;n<=N;n++) orders[n]="0";			//>
	orders[N+1]="ENDYANDINA"
	PutCookie() ;
//alert("Zeroed Cookie");
};

function RemoveCookie() {
	document.cookie = "yandina"+cookieCounter+"=" + 3 + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

function PutCookie() {
	var unlimit=1;
//	if(wholesale) unlimit=1000;
	document.cookie = "yandina"+cookieCounter+"="+orders.join("*") +"; expires=" + new Date((new Date()).getTime() + Life * unlimit * 24 * 3600000).toGMTString() + "; path = /";
//alert(" GetCookie PUT "+unescape(orders));
};

function GetCookie() {
	var allcookies = document.cookie;  
	var pos = allcookies.indexOf("yandina"+cookieCounter+"="); 
	var end = allcookies.indexOf("ENDYANDINA" , pos); 
	if ((pos==-1)||(end==-1)) {						// If no cookie, create one.
		ZeroCookie()
			} 
	else {
		var start = pos + 11; end = end + 10;  			//lengths of "yandina###=" and ENDYANDINA
		var mycookie = allcookies.substring(start, end); 
		orders = mycookie.split('*');					// fills the order array
		}; 
	if(orders[N+1]!="ENDYANDINA") ZeroCookie();			//Destroy obsolete cookies.
	FormData=unescape(orders[0]).split("^");				//Replaces name  address stuff if previously eneterd
//alert(" GetCookie GOT "+unescape(orders));
};
		
function Buy(thing) {					//  X is index number in catalog
	GetCookie();						// In case back door entry. 
	++orders[thing]; 		
	PutCookie();
	alert("One " + stock[thing] + " has been added to the order.\r\rClick PREVIEW ORDER if you wish to see shopping cart.");
};

function PutPrice(X) {			//  X is the ID[ ] of the product to put.
	X=CurrentIndex(X);			//
  	if (wholesale) { 
		var q=CurrentIndex("DS");		//Because can't include in the doc write with  either quotes
		document.write( '<CENTER><FONT SIZE="+2"COLOR="#FF00CC">Our discount $' + DropShip[X] + '</FONT></CENTER><A HREF="#ShowCart" ' )
//Drop ship charge removed for wholesale in next line
		document.write('onClick= "GetCookie();++orders[' + X + '];orders[' + q + ']=0;PutCookie();StoreForm(0);MakeList(0);"><IMG SRC="../graphics/Add2Order.jpg" WIDTH="124" HEIGHT="47" BORDER="0" ALT="Add one to order" VSPACE="10"></A>' );
		return;
	};
	if (srp[X]==null) srp[X]=""; 
	if (srp[X] >0 )
		document.write( '<CENTER><FONT SIZE="+2"COLOR="#FF00CC">$' + srp[X] + ' List </FONT></CENTER><BR>' );
		else document.write( '<CENTER><FONT SIZE="+2"COLOR="#FF00CC"> ' + srp[X] + ' </FONT></CENTER><BR>' );	
	document.write( '<CENTER><FONT SIZE="+2"COLOR="#FF0000">');
	if ( ! isNaN(price[X])) 
		document.write('$' + price[X] +' Sale Price</FONT><BR><A HREF="javascript:void(0)"onClick= "Buy(' + X + ')"><IMG SRC="graphics/Add2Order.jpg" WIDTH="124" HEIGHT="47" BORDER="0" ALT="Add one to order" VSPACE="10"></A>'+ 
		'<A HREF="ReviewOrder.htm"><BR><IMG SRC="graphics/ReviewOrder.jpg" WIDTH="124" HEIGHT="47" BORDER="0" ALT="Check the current status of your order."></A>');
	else document.write(price[X]);
	if (note[X].length>2) document.write('<BR>'+note[X]+'</CENTER>');
};

function SavePOA(x,f) 	{	//enters Paid On Account as a quantity of $1.00s so cookies will save it.
	GetCookie();
	if(isNaN(x-0)) x=0;		//>
	orders[N]=fix2(x); 
	PutCookie();
	MakeList(f); 
};

function MakeNumb(x){
	var Numbx = "" ;
	goodcharacters = ".1234567890";
	for (i=0; i<x.length;i++)	{ 		//>
		var c = x.charAt(i); 
		if(goodcharacters.indexOf(c)>=0) Numbx +=c; 
		};
	return Numbx;
	};
		
function MakeList(f)	{ 	// Reconstruct order list.
	if(Javacount!=JavaCorrect) alert(" *********** ERROR " +Javacount+ "/"+JavaCorrect+" *********** \n There has been a communications problem loading the shopping cart program. \nAfter clicking OK, try refreshing the window. \n If this message repeats the shopping cart may not work. \n You may still be able to place the order by clicking JUST EMAIL ORDER at the bottom of Checkout and we will phone you for the credit card information."); //This had to be put here instead of in JSpackRetail.js to give IE time to parse the script.	
	total=0; shipweight=0; postage=0;	
	grandtotal=0; OrderList = ""; 
	GetCookie();
	 var LastItem = N;			//orders[N] is PayOnAccount 
	if(orders[N]>1000) {		//Process discount.
		orders[2]=fix2(orders[N]-1000); 
		if(orders[2]<1) orders[2]=0;		//Minimum discount $1		
		PutCookie();
		LastItem = N-1;
	};
	for(var i=0; i<=LastItem; i++) { 				 //>
	amount=price[i]; 
	if(!isNaN(amount)) {  //In case went out of stock and using old cookie
		if(wholesale) amount = DropShip[i];  //Drop ship price is retailer discount.
		if(orders[i]>0 && i!=13) {   		//13 is a control entry that doesn't print
			amount = fix2(amount);	//In case entered as integer
			subtotal=fix2(orders[i]*amount);   	        //Math.round(orders[i]*price[i]*100)/100
			total+=fix2(subtotal)*1;  //*1 to keep it numeric.
			if(weight[i]>0) shipweight+= orders[i]*weight[i]; 
			var line = ""+i ;
			while (line.length<3 ) line = " " + line;						//>Add leading spaces.
			if ( i != 2) line += "   " + orders[i];
			while (line.length<10 ) line =  line + " " ;                         //>
			line += stock[i]; 
			if (isNaN(amount)==false) {
				while (line.length + amount.search(/\./)<47) line += " ";						//> 
				if ( i !=2) line +="$" + amount +" ea "; 
				while ((line.length+subtotal.search(/\./)) < 60) line += " ";		//>
				line += "$"+subtotal;}
			else line += "   " + amount;
			OrderList += (line + "\n");
			}; 
		};
	};

	if (total>0){ 
		shipweight = Math.round(shipweight*10)/10;
		OrderList += "\rTOTAL BEFORE SHIPPING AND TAXES $"+fix2(total);
		AddSalesTax(f); 
		if(shipweight>0) {
			shipweight=fix2(shipweight*1.05+0.25);			//allow 4oz + 5% for packaging
			OrderList += " \rTotal  Shipping Weight "+fix2(shipweight)+" lbs.\r";
			}; 
		if (InCheckout) { 
			if(shipweight>0) CalculateShipping();
			 grandtotal=fix2(total*1+Tax*1+postage*1);
			if(grandtotal > total) {
				if(postage>0) OrderList += "\r                  TOTAL INCLUDING SHIPPING and TAXES = $" + grandtotal; 
				else OrderList += "\r                  TOTAL INCLUDING TAXES = $" + grandtotal;
				};
			};
		};
	
	if(OrderList=='') {
		OrderList="                   SHOPPING CART IS EMPTY";
	};
		
	this.document.forms[f].OrderDetails.value = OrderList; 
	description="Cat# ";
	for(n=1;n<=N+1;n++) { ;			//>
		if (orders[n]>0) description += n + "=" + orders[n] + "; "
	}; 
};

function DoCatalogLine(Cat,item) {  
	if ((Catalog[item]&Cat)==0) return; 
	if (stock[item] == null) return;
	if (!stock[item].search(/^~/)) return;
	if (!note[item]) note[item]="";
	 	else (note[item] = "<BR>" + note[item]);
	if (!srp[item]) srp[item]="";
	if (srp[item]>0) srp[0]="$"+srp[item]; else srp[0]=srp[item];
	if (price[item]>0) price[0]="$"+price[item]; else price[0]=price[item];
	document.write('<TR>');
	if (Cat==1) {
		document.write('<TD>');
		if ( ! isNaN(price[item]))  
			document.write('<A HREF="javascript:void(0)"  onClick= Buy(' + item + ')> B<BR>U<BR>Y </A>' );
			else document.write("O<BR>U<BR>T");
		document.write("</TD>");
	} ;
	if(!href[item]) href[item]="";
	if(href[item]!="") {
		document.write('<TD><CENTER><A HREF=' + href[item] + ' TARGET="_self">' + stock[item] + '</A>' + note[item] + '</CENTER></TD>')} 
		else{document.write('<TD><CENTER>' + stock[item] + '<BR>' + note[item] + '</CENTER></TD>')
	};
	if(Cat==1) 
		document.write('<TD><CENTER><FONT SIZE="-1" COLOR="#CCFFFF">' + srp[0] + '</FONT><BR><FONT SIZE="+1" COLOR="#FF0000">' + price[0] + '</FONT></CENTER></TD>');
		else {
			document.write('<TD><CENTER>');
			PutPrice(ID[item]);
			document.write('</CENTER></TD>'); 
			};
	document.write(' <TD><CENTER>');
	if(photo[item]!=null) { 
		if(href[item]!="") 
			document.write('	<A HREF=' + href[item] + ' TARGET="_self">' + stock[item] + ' <IMG SRC=" ' + photo[item][0] + ' " WIDTH=" ' + photo[item][1] + '  " HEIGHT=" ' + photo[item][2] + ' " BORDER="2" HSPACE="10" VSPACE="10" ALIGN="RIGHT" ALT=" ' + desc[item] + ' "></A><BR>');
			else document.write(' <IMG SRC=" ' + photo[item][0] + ' " WIDTH=" ' + photo[item][1] + '  " HEIGHT=" ' + photo[item][2] + ' " BORDER="2" HSPACE="10" VSPACE="10" ALIGN="RIGHT" ALT=" ' + desc[item] + ' "   ><BR>');
	};
	document.write( '<FONT SIZE="+1" COLOR="#CC00CC">' + desc[item] + '</FONT></CENTER></TD></TR>' );
};

//This checks that they have cookies enabled and establishes a fresh cookie if none is present.
var NoCookies=0;		//permanent flag so we don't bug them	
					
if(NoCookies==0) {
	++NoCookies;
	for(cookieCounter=100;cookieCounter<110;cookieCounter++) { //Search for first working cookie
		GetCookie();    // If first entry this will create a zero cookie
		++orders[1];	//We've had trouble with multiple and stuck cookies
		zipBackup=orders[1];   // so this checks that we have a working one.
		PutCookie();
		GetCookie();
		zipCheck=orders[1];
		orders[1]=zipBackup-1;
		PutCookie();
		if(zipBackup==zipCheck) break;
		RemoveCookie();		//Eliminate non functioning cookie
	};
		if(zipBackup!=zipCheck) alert("     Your browser does not appear to have cookies enabled or memory is full.  The shopping cart feature will not function.  This means you will have to make a separate written order and email it to us or phone it in, toll free at 877 355 2184.  \n\n   If they are enabled you can try deleting cookies and restarting. \n\n    Cookies are small items of data that are stored on your local disk to retain the details of your order as you select items.  When you submit the order, this data is sent to us to place the order.  The browser only has access to those records that were created during the browser session while connected to our web site and they are retained for 72 hours to allow you to return to the order to review your selection or purchase.  Setting the security level under Tools/Internet Options/Security to maximum is one way cookies may be disabled and various CUSTOM cookie controls are set there.");
};
++Javacount;
if(JavaCheck==true)alert("CatFun.js add 1="+Javacount);