
// Search Through the dBase for the Name
function FindName(aName){
	for(n=0;n<dbase.length;n++){
		if(dbase[n][1]==aName)
			return ItemName = n;
	}
}

// ***** FORMAT DECIMAL *****
function formatdecimal(amtin){
   amtin+="";
   amtin=Math.round(parseFloat(amtin)*100);
   amtin/=100;
   amtin+="";

   if(amtin.indexOf(".")<0){
      amtin+=".00";
      return amtin;
   }

   if(amtin.indexOf(".")==amtin.length-2){amtin+="0";}
   return amtin;
}

	// ***** SHOW COOKIE *****
	function showcookie(){
		var newwin=open("", "displayWindow","width=400,height=800");
		with(newwin.document){
   			open();
   			write("<html><head>");
   			write("<font color='#0000FF' face='Times New Roman'>");
   			write("<title>Cookie Contents</title>");
   			write("</head>");
   			write("<body bgcolor='#FF99CC' background='Images/Parchment.jpg'>");
   			write("<div align='center'><h2>Cookie Contents</h2></div>");
   			getcookie();
   			for(var row=0;row<this.items.length;row++){
   				var itemscells = separatedata(this.items[row],"²");
				write( itemscells[0] + " " + itemscells[1] + " " + itemscells[2] + " " + itemscells[3] + " " + itemscells[4] + " " + itemscells[5] + " " + "<BR>");
			}
			write("</div>");
   			write("</body></html>");
   			close();
		}
	}

	
   	// ***** GET COOKIE *****
   	function getcookie () {
   		var cookiename="order=";
   		var doccookie=document.cookie;
   		if (document.cookie.length>0){
   			var start=doccookie.indexOf(cookiename);
   			if (start!=-1){
   			start+=cookiename.length;
   			var end=doccookie.indexOf(";", start);
   			if(end==-1){end=doccookie.length;}
   			this.items=separatedata(unescape(doccookie.substring(start,end)),"¹");
   			return items;
   			}
   		}
   		else{items=null;}
   		
	}
	

	// ***** MYCOOKIE *****
	function mycookie(namein){
   		this.name="order";
   		this.items=null;
   		this.orderdelimiter="¹";
   		this.itemdelimiter="²";
   		this.optiondelimiter="³";
   		this.getcookie=getcookie;
   		this.setcookie=setcookie;
   		this.delcookie=delcookie;
	}


	// ***** SET COOKIE *****	
	function setcookie(){
	var value=this.items.join("¹");
	document.cookie="order=" + escape(value) + "; expires=" + expdate.toGMTString() +  "; path=/";
	}

	// ***** DELETE COOKIE *****
	function delcookie () {
		var expireNow = new Date();
		document.cookie = name + "=" +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
	}
	
	// ***** SEPARATE DATA *****
	function separatedata(datain,delim){
		var xarray=datain.split(delim);
		return xarray;
	}

	
	// ***** QUANTITY OK *****
	function quantityok(valuein){

   		valuein+="";
   		if(valuein.length==0){return false};
   		for(var i=0;i<valuein.length;i++){
      		if(valuein.charAt(i)<"0" || valuein.charAt(i)>"9"){return false;}
   		}
   	return true;
	}
	
	// ***** QUANTITY POSITIVE *****
	function quantitypositive(valuein){
		
   		if(quantityok(valuein)){
      		if(valuein>0){return true;}
   		}
   	return false;
	}


	var expdate = new Date();
	expdate.setTime (expdate.getTime() +  (24 * 60 * 60 * 1000 * 365)); 

// ***** DISPLAY & STORE *****
function displayandstore(){
		// If Netscape V4 run this code 
		if(document.layers){
			var obj2 = document.layers.ShoppingCart.document.Page.elements["quantity"];
			var obj1 = document.layers.ShoppingCart.document.Page.elements["type"];
			if(obj2.value == 0)
			{
				alert("The quantity must be a number greater than zero.");
      			obj2.select();
      			obj2.focus();
				obj2.value = 1;
      			return;
   			}
			obj2.value*=1; // Removes Leading Zeros
		
			for(var i=0;i <obj1.length; i++)
			{	// Find which shipping method has been selected
				if (obj1.options[i].selected == true) 
				{
					var TypeValue = i;	// Place selected shipping method value into Variable MethodValue
				}
			}
			var ProdID=dbase[TypeValue + ItemName][0];
			var ShippingIndex="0";
			var Cost = dbase[TypeValue + ItemName][3];
			var Type=dbase[TypeValue + ItemName][2];			// Product Type
			var ShippingIndex = dbase[TypeValue + ItemName][4];	// Item weight in Grams
			var FullCost=formatdecimal(Cost*obj2.value);
		
			// Use Shipping Index to store the Items weight
			var myarray = new Array(ProdID, dbase[TypeValue + ItemName][1], Type, Cost, obj2.value, ShippingIndex);
			var neworderdata = myarray.join("²");
	
			getcookie();
			if(this.items==null){this.items=new Array(neworderdata);}
			else{
				var itemcount=this.items.length;
				this.items[itemcount]=neworderdata;
			}
			setcookie();
			alert(obj2.value+" - "+dbase[TypeValue+ItemName][1]+" products at $"+Cost+" each\nfor a total cost of $"+FullCost+"\nhave been added to your shopping cart.");
			
		// If Internet Explorer V4 or Netscape V6 and above run this code
		}else{
			with(document){
				if(Page.quantity.value == 0){
					alert("The quantity must be a number greater than zero.");
      				Page.quantity.select();
      				Page.quantity.focus();
					Page.quantity.value = 1;
      				return;
   				}
				Page.quantity.value*=1; // Removes Leading Zeros
		
				for(var i=0;i <Page.type.length; i++)
				{	// Find which shipping method has been selected
					if (Page.type.options[i].selected == true) 
					{
						var TypeValue = i;	// Place selected shipping method value into Variable MethodValue
					}
				}
		
		
				//alert(dbase[TypeValue][2]);
				var ProdID=dbase[TypeValue + ItemName][0];
				var ShippingIndex="0";
				var Cost = dbase[TypeValue + ItemName][3];
				var Type=dbase[TypeValue + ItemName][2];			// Product Type
				var ShippingIndex = dbase[TypeValue + ItemName][4];	// Item weight in Grams
				var FullCost=formatdecimal(Cost*document.Page.quantity.value);
		
		
			
				// Use Shipping Index to store the Items weight
				var myarray = new Array(ProdID, dbase[TypeValue + ItemName][1], Type, Page.type.value, Page.quantity.value, ShippingIndex);
				var neworderdata = myarray.join("²");
	
				getcookie();
				if(this.items==null){this.items=new Array(neworderdata);}
				else{
					var itemcount=this.items.length;
					this.items[itemcount]=neworderdata;
				}
				setcookie();
				alert(Page.quantity.value+" - "+dbase[TypeValue+ItemName][1]+" products at $"+Cost+" each\nfor a total cost of $"+FullCost+"\nhave been added to your shopping cart.");
			}
		}
		
}
// If the view cart button is pressed and no items are in the shopping cart
// the alert message is displayed.
function ViewCartContents(){
	getcookie();
	if (this.items!=null){
		window.location.replace("ShoppingCart.htm");
	}else{
		alert("You have no items in your shopping cart!");
	}
}
	
	function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);