
// Global variables
theProducts   = new Array ();
theDisplay    = new Array ();
theQty        = new Array ();
thePrice      = new Array ();
theShipping      = new Array ();
theName       = new Array ();
theCatNo      = new Array ();
//deliveryCharges = new Array ("9.00","12.00","15.00","20.00","25.00");

totalItemsInArray = 0;

function Monify(value)
{
  var str = "" + Math.round(value*100);
  var len = str.length;
  return (str=="0")?"0.00":(str.substring(0,len-2)+"."+str.substring(len-2,len)); }

function InsertInArray (i, qty, price, shipping, name, catNo) {
   if (qty > 0)
   {
      theQty[i]       = qty;
      thePrice[i]     = price;
      theShipping[i]     = shipping;
      theName[i]      = name;
      theCatNo[i]     = catNo;
      theProducts[i]  = theQty[i]+","+thePrice[i]+","+theShipping[i]+","+theName[i]+","+theCatNo[i];
      theDisplay[i]   = theName[i]+" - "+theQty[i]+" x "+thePrice[i]+"\r\n";
   }
   else
   {
      theCatNo[i]     = catNo;
      theProducts[i]  = "";
   }
}

function CheckTheProduct (qty, price, shipping, name, catNo) {
   price = Monify (price);
   var i = 0;
   while (i < totalItemsInArray)
   {
      // Two items with the same or no catalog number are allowed, if they have different names
      if (theCatNo[i] == catNo && theName[i] == name)
      {
         InsertInArray (i, qty, price, shipping, name, catNo);
         return;
      }
      i++;
   }

   // If adding a new item, its qty must be more than 0
   if (qty > 0)
   {
      InsertInArray (i, qty, price, shipping, name, catNo);
      totalItemsInArray++;
   }
}

function PrepareProductsList (form)
{
   var products = "";
   var display  = "";
   var shippingDisplay  = "";
   var shippingSub  = 0;
   var i = 0;
   var sub = 0;
   while (i < totalItemsInArray)
   {
      // CONSTRUCT THE PRODUCTS LIST
      if (theProducts[i])
      {
         products += theProducts[i];
         display  += theDisplay[i];
         shippingSub  += theQty[i]*theShipping[i];
         sub += (theQty[i]*thePrice[i]);
      }
      i++;
     }
     
   shippingDisplay  = Monify(shippingSub);
   products += "|";
   form.orderproducts.value    = products;
   form.displayProducts.value = display;
   form.shipping.value = shippingDisplay;
   form.subTotal.value    = Monify(sub);
	
//TAX ADDED HERE IF FROM CALIFORNIA
   if(form.State.value == "XX"){
   		tax = 8.250;
   } else {
   		tax = 0;
   }
   
   
   if (form.subTotal.value > 0 && tax > 0) {
   		form.tax.value    = Monify((sub + shippingSub) * tax/100);
   		
   		form.total.value    = Monify(+form.subTotal.value + +form.tax.value + +form.shipping.value);
   	}
   else { 
   	form.tax.value    = "0.00";
   		form.total.value    = Monify(+form.subTotal.value +  +form.shipping.value);
   	} 
  }
  
function D_M_Table(form, form_element, priceMenu, priceBase, shipping, name, catNo) {
   var qty = Math.abs(form_element.value);
   //DISCOUNT CALCULATED HERE BY SUBTRACTING FROM THE ITEMS BASE PRICE
   if(form_element.name == "RomanImperators"){
	   if (qty >= 1000){
		  priceMenu.value=7;
	   	}
	   else if(qty >= 999){
		  priceMenu.value=2;
	   	} else {
		  priceMenu.value="";
	   	}
   }
   //DISCOUNT CALCULATED HERE BY SUBTRACTING FROM THE ITEMS BASE PRICE
   else if(form_element.name == "GreekV1"){
	   if (qty >= 1000){
		  priceMenu.value=2;
	   	}
	   else if(qty >= 999){
		  priceMenu.value=1;
	   	} else {
		  priceMenu.value="";
	   	}
   }
   
   price = priceBase-priceMenu.value;
   CheckTheProduct (qty, price, shipping, name, catNo);
   PrepareProductsList (form);
}

function ClearAndReset (form)
{
   totalItemsInArray = 0;
}

//ORDER NUMBER CREATED BY MONTH DAY YEAR PLUS RANDOM 4 DIGITS
function OrderNumber (form){
		Date = new Date();
		Month = Date.getMonth()+1;
		Day = Date.getDay();
		Year = Date.getYear()
		Random = Math.floor((Math.random()*8999)+1000);
		
		form.ordernumber.value = Month+""+Day+""+Year+""+Random;
		
		/* random()*8999 returns random number between 0 and 8999 */
		/* adding 1000 makes it a number between 1000 and 9999 */
		/* floor just chops off the decimal stuff and return an integer that has */
		/* 4 digits */
	}


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_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 popUp(url) {
sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
self.name = "mainWin";
}
