// <!--//

	currency="USD";

var appversion=parseInt(navigator.appVersion); 
no_of_order_pages = 200; //the number of possible product pages selected for ordering  
var itemid_array = new makeArray(no_of_order_pages);
var itemname_array = new makeArray(no_of_order_pages);
var descript_array = new makeArray(no_of_order_pages);
var sku_array = new makeArray(no_of_order_pages);
var price_array = new makeArray(no_of_order_pages);
var unit_array = new makeArray(no_of_order_pages);  // measurement unit
var quantity_array = new makeArray(no_of_order_pages);
var taxable_array = new makeArray(no_of_order_pages);        // a bitmap that tells whether this item is taxable
var shipping_array = new makeArray(no_of_order_pages);  // shipping cost per item
var weight_array = new makeArray(no_of_order_pages);  // weight for calculating shipping cost
var subtotal = 0; //subtotal or order
var order = ''; //the printed result to show the customer's order
var model_array = new makeArray(no_of_order_pages);
var options_array = new makeArray(no_of_order_pages);

/////////////////////////////////////////////////////////////////////////////////////////////////////

minOrder = 75.00;		// Mimimum order charge
processFee=25;
MinFound="N";
MinID="MinOrd";
MinIDDesc="Minimum Order Processing Fee";

//function CheckMinOrder()
function mytest2()
{
checkMin()
if(subtotal<minOrder && (MinFound=='N'))
	{
	MinFound="Y";
	alert("Minimum Order of $"+minOrder+" is required. A Processing Fee will be added to your order");
	addtoBasket(MinID, MinIDDesc, '', MinID, processFee, '', '1', '', '0.00 0 0 0 0 0', '0', '', '');
	calcNow();
	openOrderForm();
	return -1;
	}
else
	{
	document.basket.submit();
	}
}


function checkMin()
{
MinFound="N";
var MinIndex=0;

//clear global calculation variables
subtotal = 0;
order = '';

for (x=1; eval(price_array[x]) != 0; x++)
	{
	for (x=1; eval(price_array[x]) != 0; x++)
		{
		if(itemid_array[x]==MinID && (quantity_array[x]>0))
			{
			MinFound="Y";
			MinIndex=x;
			}
		if (quantity_array[x]!=0)
			{
			//calculate the subtotal for the whole lot of products
			if (price_array[x] == "")
			        price_array[x] = "0";
			tmpamount  = Math.round(eval(price_array[x]) * eval(quantity_array[x])*Math.pow(10,2))/Math.pow(10,2);
			tmpamount -= 0;
			subtotal  += tmpamount;
			}
		}
	}
if(MinFound=="Y")
	{
	test2=subtotal-(quantity_array[MinIndex]*price_array[MinIndex]);
	if(test2 >= minOrder)
		{
		quantity_array[MinIndex]=0;
		document.cookie = "quantity=; expires=Thu, 01-Jan-70 00:00:01 GMT";
		// convert from array to cookie
		for (i=1; eval(price_array[i])!= 0; i++)
			{
		         addtoCookieArray("quantity", quantity_array[i]);
			}
		document.basket.Order_ItemQuantity.value=GetCookie("quantity");
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////

function mytest()
{
	var newShipAmt = 0;  // Amount to hold total shipping
	var arrayElm = 0; // Number of Items Purchased (Elements)
	var tempArray="";
	var loop=0;
	var loop2=0;
	var pos=0;
	
	tempArray = unescape(document.basket.Order_ItemQuantity.value); // hidden qty ordered
	qtyArray = tempArray.split(","); // put into array
	arrayElm = qtyArray.length - 1; // number of items ordered (elements)
	tempArray = unescape(document.basket.Order_ItemShipping.value); // hidden shipping amount
	shipArray = tempArray.split(","); // put into array
	while (loop<arrayElm) // loop as many times as there are elements
	{
	  	shipItemArray=shipArray[loop].split(" "); // put all shipping amounts into an array for each item
		newShipAmt = newShipAmt + (qtyArray[loop] * shipItemArray[0]);
		shipItemArray[0]=0;
		loop2=0;
		tempArray="";
		while (loop2<shipItemArray.length)
		{
			tempArray=tempArray + shipItemArray[loop2];
			loop2 ++;
			if (loop2 < shipItemArray.length)
			{
				tempArray=tempArray + " ";
			}
		}
		shipArray[loop]=escape(tempArray);
		loop ++;
	}
	document.basket.ShippingBase1.value=fmtPrice(newShipAmt);
	document.basket.Order_ItemShipping.value=shipArray;
//	alert("End");
}

/////////////////////////////////////////////////////////////////////////
// return true if add is ok
// return false if add fails
function addtoBasket(productID, productName, productDescription, productSKU, productPrice,
	 productUnit, productQuantity, productTaxable, productShipping,productWeight,productModel,productOptions)
{
	// if product already in shopping basket
	var tmp_array_ID = new makeArray(no_of_order_pages);
	var tmp_array_Options = new makeArray(no_of_order_pages);
	var found=0;

	total_count=parseIt("productID", tmp_array_ID);
	parseIt("productOptions", tmp_array_Options);

	if (total_count >= no_of_order_pages)
	{
   		alert("The shopping basket is full. No more items can be added.");
   		return false;
	}

	for (i=1; total_count>=i; i++)
	{
//  		if (tmp_array_ID[i]==productID && escape(tmp_array_Options[i])==productOptions)
  		if (tmp_array_ID[i]==productID && tmp_array_Options[i]==productOptions)
		{
   			found=1;
			break;
		}
 	}

  	if (found!=0)
	{
 		total_quantity=addValueInArray("quantity", i, productQuantity);
  	}
  	else
	{
   		addtoCookieArray("productID", productID);
   		addtoCookieArray("product", productName);
//   		addtoCookieArray("description", productDescription);
   		addtoCookieArray("sku", productSKU);
   		addtoCookieArray("price", productPrice);
   		addtoCookieArray("unit", productUnit);
   		addtoCookieArray("quantity", productQuantity);
   		addtoCookieArray("taxable", productTaxable);
   		addtoCookieArray("shipping", productShipping);
   		addtoCookieArray("weight", productWeight);
   		addtoCookieArray("model", productModel);
		addtoCookieArray("productOptions", productOptions); 
		total_quantity=productQuantity;
	}
	if (total_quantity == 0)
	{
   		alert(productQuantity + " " + unescape(productName) + "s " + "cannot be added to your shopping basket.\n\nREASON:\nThe maximium number of these items that can be added to your basket is 9999.99.");
		return false;
  	}

	window.location.href="basketminord.htm";
	return true;
}


function getCookieVal (offset)
{
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return document.cookie.substring(offset, endstr);
}
	
function GetCookie (name)
{
	var arg = name + "=";
  	var alen = arg.length;
  	var clen = document.cookie.length;
  	var i = 0;
  	while (clen>i)
	{
    		var j = i + alen;
    		if (document.cookie.substring(i, j) == arg)
   			return getCookieVal (j);
   		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
			break; 
  	}
  	return null;
}

function addValueInArray(cookieName, index, element)
{
	var tmp_array = new makeArray(no_of_order_pages);
 	total_count=parseIt(cookieName, tmp_array);
	tmp_array[index] = eval(tmp_array[index]) + eval(element);
	tmp_array[index] = RoundQty( tmp_array[index] );
	if (tmp_array[index] > 9999.99)
		return 0;
 	// remove existing cookie
 	document.cookie = cookieName + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";

	// convert from array to cookie
	for (i=1; total_count>=i; i++)
     		addtoCookieArray(cookieName, tmp_array[i]);
 	return tmp_array[index];
}
	

////////////////////////////////////////////////////////////////////////



function ConvertHTMLEncodeString(szArgumentValue)
{
	var strTmp="";
	var x=0;

	for (x=0; x<szArgumentValue.length; x++)
	{
		switch ( szArgumentValue.charAt(x) )
		{
		case '"':
			strTmp += "&quot;";
			break;
		case '&':
			strTmp += "&amp;";
			break;
		case '<':
			strTmp += "&lt;";
			break;
		case '>':
			strTmp += "&gt;";
			break;
		default:
			strTmp += szArgumentValue.charAt(x);
			break;
		}
	}
	return strTmp;
}

function openOrderForm()  
{
  var cart =window.open(document.location,"_self");
  if(navigator.appName == "Netscape")
  {
    cart.focus();
  }
}
function getCookieVal (offset)
{
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return document.cookie.substring(offset, endstr);
}
function GetCookie (name)
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (clen>1) 
  {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function SetCookie (name,value,expires,path,domain,secure) 
{
 document.cookie = name + "=" + escape (value) +
  ((expires) ? "; expires=" + expires : '') +
  ((path) ? "; path=" + path : '') +
  ((domain) ? "; domain=" + domain : '') +
  ((secure) ? "; secure" : '');
}

function makeArray(n) 
{
  this.length = n;
  for (var k = 1; n>=k; k++) 
  {
    this[k] = 0;
  }
  return this;
} 

function addtoCookieArray(cookieName, info)
{
  var tmpString=GetCookie(cookieName);
  if(GetCookie(cookieName) !=null)
  {
    tmpString = GetCookie(cookieName) + escape(info) + ",";
//    tmpString = GetCookie(cookieName) + info + ",";
    //append to the current cookie values
    document.cookie = cookieName + "=" + tmpString;
  }
  else
  {
    tmpString = escape(info) + ",";
//    tmpString = info + ",";
    document.cookie = cookieName + "=" + tmpString;
  }
}

// this defines an array for holding the user configuration
// cookie contents when parsed out. Increase the count if you need to store more
// but don't forget that the limitation is 4096 bytes
//
// this parses the info stored in the config cookie into the array
function parseIt(cookieName, array) 
{
 config=GetCookie(cookieName);
 config+='';           // make it a string if it wasn't before
 var y = 1;
 while(config.indexOf(",") >= 0) 
 {
   var pos = config.indexOf(",");
   if (pos==0)
     array[y]="";
   else
     array[y]=unescape(config.substring(0,pos));
   y++;
   config=config.substring(pos+1,config.length);
 }
 return y-1;
}

function fmtPrice(value)
{   
  amount = Math.round(value*Math.pow(10,2))/Math.pow(10,2);
  amount -= 0;
  return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}

function displaySum(index)
{
  eval("document.basket.sum"+index+".value=fmtPrice("+quantity_array[index]*price_array[index]+")");
  document.basket.formsubtotal.value=fmtPrice(subtotal);
}

function isInt(elm) 
{
    var elmstr = '' + elm; 
    if (elmstr == '')
        return false;
    for (var i = 0; elmstr.length>i; i++)
    {
        if ( "0">elmstr.charAt(i) || elmstr.charAt(i) > "9")
            { return false; }
    }
    return true;
}
function RoundQty(elm)
{
    var elmstr = "" + elm;
    if (elmstr == "")
        return 0;
    DotExist=0;
    DigitAfterDecimal=0;
    for (var i = 0; elmstr.length>i; i++)
    {
        if ( "0">elmstr.charAt(i) || elmstr.charAt(i) > "9")
	{ 
            if ((elmstr.charAt(i) == ".") && (DotExist == 0))
                DotExist = 1;
            else
                return 0; 
        }
        else if (DotExist == 1)
	{
            if (2>DigitAfterDecimal )
                DigitAfterDecimal = DigitAfterDecimal + 1 ;
            else
	    {
                if (elmstr.charAt(i)>="5")
		{
                   var sTmp100 = elmstr.substring(0, i-3) + elmstr.substring(i-2, i);
		   nTmp = Number( sTmp100 ) ;
		   nTmp = nTmp + 1;
	           var sTmp = "" + nTmp;
		   if (2>sTmp.length)
			   sTmp = sTmp.substring(0, sTmp.length - 2) + ".0" + sTmp.substring(sTmp.length - 2, sTmp.length );
		   else
			   sTmp = sTmp.substring(0, sTmp.length - 2) + "." + sTmp.substring(sTmp.length - 2, sTmp.length );
                   return sTmp ;
                }
                else               
                   return Number( elmstr.substring(0, i) );
            }
        }       
    }
    return elm;
}

function updateQuantity(Qty, index, acceptZero)
{
    if (!acceptZero && (Qty>=10000 || Qty==0))
	{
		alert ("Please enter a valid number that is greater than 0.01 and less than 9999.99!");
	    eval("document.basket.quan"+index+".value=quantity_array[index]");
        }
        else
	{
	    if(itemid_array[index]!=MinID || acceptZero)
	    {
	     quantity_array[index]=Qty;
	     // remove existing cookie
	     document.cookie = "quantity=; expires=Thu, 01-Jan-70 00:00:01 GMT";

	     // convert from array to cookie
	     for (i=1; eval(price_array[i])!= 0; i++)
//	     for (i=1; eval(quantity_array[i])!= 0; i++)
		{
	         addtoCookieArray("quantity", quantity_array[i]);
		}

	     // update hidden variable as well
	     document.basket.Order_ItemQuantity.value=GetCookie("quantity");
	     calcNow();
	     displaySum(index);
	     openOrderForm();
	     }
	     else
             {
        	    eval("document.basket.quan"+index+".value=quantity_array[index]");
             }
        }
}

function calcNow()
{
 //update information from the ordering cookies
 parseIt("productID", itemid_array);
 parseIt("product", itemname_array);
// parseIt("description", descript_array);
 parseIt("sku", sku_array);
 parseIt("price", price_array);
 parseIt("unit", unit_array);
 parseIt("quantity", quantity_array);
 parseIt("taxable", taxable_array);
 parseIt("shipping", shipping_array);
 parseIt("weight", weight_array);
 parseIt("model", model_array);
 parseIt("productOptions", options_array);


checkMin();
 //clear global calculation variables
 subtotal = 0;
 order = '';

 for (x=1; eval(price_array[x]) != 0; x++)
// for (x=1; eval(quantity_array[x]) != 0; x++)
 {
	if (quantity_array[x]!=0)
	{
		order += '<tr>' +
		 '<td valign="top"><font class="normaltext"><b>' + ConvertHTMLEncodeString(unescape(sku_array[x])) + '</b></font>&nbsp;</td>' +

		 '<td valign="top"><font class="normaltext"><b>' + ConvertHTMLEncodeString(unescape(itemname_array[x])) + ' ' + ConvertHTMLEncodeString(unescape(model_array[x])) + '</b></font>' +
		 '    <br><font class="normaltext"><b> Unit Price: ' + unit_array[x] + ' @ ' + currency + '' + fmtPrice(price_array[x]) + '</b></font>' +
		 (options_array[x]==""?"":('    <br><font class="normaltext"><b>(' + options_array[x] + ')</b></font>')) +
		 '</td>' +

		 '<td valign="top"><font class="normaltext"><b><input type="TEXT" name="quan' + x + '" size="3" value="' + quantity_array[x] +
		 '" onchange="this.value=RoundQty(this.value),updateQuantity(this.value,' + x + ',false)" maxlength="7"/></b>' +
		 '</font></td>' +

		 '<td valign="top"><font class="normaltext"><b>' +
		 '	<input type="text" name="sum' + x + '" size="8" value="' +
			 fmtPrice(price_array[x] * quantity_array[x]) + '" onchange="displaySum('+
			 x + ')"/></b></font>'+
		 '</td>' +


		 '<td valign="top"><font class="normaltext"><b><input type="button" onclick="updateQuantity(0,' + x +
		 ',true); openOrderForm()" value="Remove"   /></b></font></td>' +


		 '</tr>'+
		 '<tr>'+
		 '    <td colspan="5"><hr size="1" width="100%" color="black" noshade>'+
		 '    </td>'+
		 '</tr>';

		//calculate the subtotal for the whole lot of products
		if (price_array[x] == "")
		        price_array[x] = "0";
 
		tmpamount  = Math.round(eval(price_array[x]) * eval(quantity_array[x])*Math.pow(10,2))/Math.pow(10,2);
		tmpamount -= 0;
		subtotal  += tmpamount;
	}
 }
 order += '<tr bgcolor="black">' +
	 '<td colspan="3" align="right"><font face="arial" size="4" color="white"><b>SUBTOTAL</b></font> '+ currency + '&nbsp;</td>' +
	 '<td colspan="2" align="left"><font class="title">'+
	 '<input type="text" size="8" name="formsubtotal" value="' +
	 fmtPrice(subtotal) +
	 '" onchange="document.basket.formsubtotal.value=fmtPrice(subtotal)"/></font></td></tr>';
}

function clearOrder() 
{
 emptyStr = "=; expires=Thu, 01-Jan-70 00:00:01 GMT";

 document.cookie = "productID" + emptyStr;
 document.cookie = "product" + emptyStr;
 document.cookie = "description" + emptyStr;
 document.cookie = "sku" + emptyStr;
 document.cookie = "price" + emptyStr;
 document.cookie = "unit" + emptyStr;
 document.cookie = "quantity" + emptyStr;
 document.cookie = "taxable" + emptyStr;
 document.cookie = "shipping" + emptyStr;
 document.cookie = "weight" + emptyStr;
 document.cookie = "model" + emptyStr;
 document.cookie = "productOptions" + emptyStr;
 openOrderForm();
}

// -->
// </script>

