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_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// for shopping basket
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 promo_array = new makeArray(no_of_order_pages);  // on promotion or not
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);

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 (i < clen)
 {
  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; k <= n; k++)
 {
  this[k] = 0;
 }
 return this;
}

function addtoCookieArray(cookieName, info)
{
    var tmpString=GetCookie(cookieName);
    if(GetCookie(cookieName) !=null)
    {
      tmpString = GetCookie(cookieName) + escape(info) + ",";
      //append to the current cookie values
      document.cookie = cookieName + "=" + tmpString;
    }
    else
    {
      tmpString = escape(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 */

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; i < elmstr.length; i++)
    {
        if (elmstr.charAt(i) < "0" || 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; i < elmstr.length; i++)
    {
        if (elmstr.charAt(i) < "0" || elmstr.charAt(i) > "9") {
            if ((elmstr.charAt(i) == ".")&&(DotExist == 0))
                DotExist = 1;
            else
                return 0;
        }
        else if (DotExist == 1){
            if (DigitAfterDecimal < 2)
                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 (sTmp.length < 2)
   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 calcNow()
{
 //update information from the ordering cookies
 parseIt("productID", itemid_array);
 parseIt("product", itemname_array);
 parseIt("description", descript_array);
 parseIt("sku", sku_array);
 parseIt("promotion", promo_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);

 //clear global calculation variables
 subtotal = 0;
 order = '';
 currency = "$";

 for (x=1; eval(price_array[x]) != 0; x++)
 {
//  order +=  quantity_array[x] + " of " + itemname_array[x] +
//   "(" + descript_array[x] + "), price=" + price_array[x] + " " + unit_array[x] +
//   "; promo=" + promo_array[x] +
//   "; taxable=" + taxable_array[x] +
//   "; shipping=" + shipping_array[x] +
//   "; weight=" + weight_array[x];

if (quantity_array[x]!=0)
{
order += '<TR>' +
 '<TD VALIGN="TOP"><FONT SIZE=3 CLASS=headline12><B>' + sku_array[x] + '</B></FONT>&nbsp;</TD>' +
 '<TD VALIGN="TOP"><FONT SIZE=3 CLASS=headline12><B>' + unescape(itemname_array[x]) + ' ' + unescape(model_array[x]) + '</B></FONT>' +
 '<BR><FONT SIZE=2 CLASS=headline10><B>Unit Price: ' + unit_array[x] + ' @ ' + currency + '' + fmtPrice(price_array[x]) + '</B></FONT></TD>' +
 '<TD VALIGN="TOP"><FONT SIZE=3 CLASS=headline12><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"></FONT></TD>' +
 '<TD VALIGN="TOP" NOWRAP><FONT SIZE=3 CLASS=headline12><B>' +
 '<INPUT TYPE="TEXT" NAME="sum' + x + '" SIZE="5" VALUE="' +
fmtPrice(price_array[x] * quantity_array[x]) + '" ONCHANGE="displaySum('+
x + ')"></B></FONT></TD>' +
 '<TD VALIGN="TOP"><FONT SIZE=2 CLASS=headline10><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="#134E7D">' +
 '<TD COLSPAN="3" ALIGN="RIGHT"><FONT SIZE="3" class="headline14" COLOR="white"><B>SUBTOTAL '+ currency + '&nbsp;</FONT></TD>' +
 '<TD COLSPAN="2" ALIGN="LEFT" NOWRAP><FONT class="total" COLOR="black">'+
'<INPUT TYPE="TEXT" SIZE="5" NAME="formsubtotal" VALUE="' +
fmtPrice(subtotal) +
 '" ONCHANGE="document.basket.formsubtotal.value=fmtPrice(subtotal)"></FONT></TD></TR>';
}

function clearOrder()
{
 //  ----------- EXPIRES THE COOKIES ( which makes them read null ) ----------
 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 = "promotion" + 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;
 openOrderForm();
}

function initHiddens()
{// Set hidden variables with values of cookies
 document.basket.Order_ItemID.value = '' + GetCookie("productID");
 document.basket.Order_ItemName.value = '' + GetCookie("product");
 document.basket.Order_ItemDescription.value = '' + GetCookie("description");
 document.basket.Order_ItemCode.value = '' + GetCookie("sku");
 document.basket.Order_ItemPromotion.value = '' + GetCookie("promotion");
 document.basket.Order_ItemListPrice.value = '' + GetCookie("price");
 document.basket.Order_ItemUnit.value = '' + GetCookie("unit");
 document.basket.Order_ItemQuantity.value = '' + GetCookie("quantity");
 document.basket.Order_ItemTaxable.value = '' + GetCookie("taxable");
 document.basket.Order_ItemShipping.value = '' + GetCookie("shipping");
 document.basket.Order_ItemWeight.value = '' + GetCookie("weight");
 document.basket.Order_ItemModel.value = '' + GetCookie("model");
}

// common items for shopping basket
item_Description=" ";
item_SKU="";
item_Promo="0";
item_Taxable="1";
item_Shipping="0%200%200%200%200%200"
item_Weight="";
item_Model="";

//VARIABLES FOR GLOBAL USE
no_of_order_pages = 200; //the number of possible product pages selected for ordering
//====== Cookie functions =================================
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 (i < clen) 
	{
		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;
}

//-------- Array function compatible with NS 2.0x, 3.0x and MSIE
function makeArray(n) 
{
	this.length = n;
	for (var i = 1; i <= n; i++) 
	{
		this[i] = 0;
	}
	return this;
}

function addtoCookieArray(cookieName, info) 
{
	var tmpString=GetCookie(cookieName);
	if(GetCookie(cookieName) !=null) 
	{
		tmpString = GetCookie(cookieName) + info + ",";
		//append to the current cookie values
		document.cookie = cookieName + "=" + tmpString;
	} else 
	{
		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 do not 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 was not 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;
}

// return -1 if total # of elements in array exceed max limit
// return 0 if element is not found in the array
// return array index if element is found in the array
function posInArray(cookieName, element)
{
 	var tmp_array = new makeArray(no_of_order_pages);
 	total_count=parseIt(cookieName, tmp_array);

 	if (total_count >= no_of_order_pages)
		return -1;

 	for (i=1; i<=total_count; i++)
 	{
		if (tmp_array[i]==element)
			return i;
 	}
	return 0;
}

// This modifies the value of the n-th element in the array stored in cookie.
// For integer arrays, it will perform addition to existing value.
// For string arrays, it will perform concatenation.
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; i<=total_count; i++)
		addtoCookieArray(cookieName, tmp_array[i]);
		return tmp_array[index];
}

// check if input is an integer and it is greater than 0
function isInt(elm) 
{
	var elmstr = "" + elm;
	if (elmstr == "")
		return false;
	for (var i = 0; i < elmstr.length; i++) 
	{
		if (elmstr.charAt(i) < "0" || 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; i < elmstr.length; i++) 
	{
		if (elmstr.charAt(i) < "0" || elmstr.charAt(i) > "9") 
		{
			if ((elmstr.charAt(i) == ".")&&(DotExist == 0))
				DotExist = 1;
			else
				return 0;
		}
		else if (DotExist == 1) 
		{
			if (DigitAfterDecimal < 2)
				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 (sTmp.length < 2)
						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 addtoBasket(productID, productName, productDescription, productSKU, productPromo, productPrice, productUnit, productQuantity, productTaxable, productShipping, productWeight, productModel) 
{
	// test if browser supports cookie
	document.cookie = "testcookie=mytestvalue";
	if (GetCookie("testcookie")!="mytestvalue")
	{
		alert("\nNO COOKIE SUPPORT\n \nYou will not be able to use the shopping basket without cookie support.\n \nFor more information on Cookie Support and turning it on please see your Browsers help section.\n \nYou can purchase this item without cookie support by selecting the Express Lane button ");
		return;
	}
	// if product already in shopping basket
	search_index = posInArray("productID", productID);
	if (search_index==-1)
	{
		alert("The shopping basket is full. No more items can be added.");
		return;
	}
	if (search_index!=0)
	{
		total_quantity=addValueInArray("quantity", search_index, productQuantity);
	}
	else
	{
		addtoCookieArray("productID", productID);
		addtoCookieArray("product", productName);
		addtoCookieArray("description", productDescription);
		addtoCookieArray("sku", productSKU);
		addtoCookieArray("promotion", productPromo);
		addtoCookieArray("price", productPrice);
		addtoCookieArray("unit", productUnit);
		addtoCookieArray("quantity", productQuantity);
		addtoCookieArray("taxable", productTaxable);
		addtoCookieArray("shipping", productShipping);
		addtoCookieArray("weight", productWeight);
		addtoCookieArray("model", productModel);
		total_quantity=productQuantity;
	}
	if (total_quantity == 0)
	{
		alert(productQuantity + " " + unescape(productName) + "s " + "cannot be added to your shopping basket.\n\n REASON:\n The maximium number of these items that can be added to your basket is 9999.99");
	}
	else if(productQuantity == 1)
	{
		alert(productQuantity + " " + unescape(productName) + " " + "has been added to your shopping basket.\n\nAltogether, you have "+
		total_quantity + " of these items in your basket.");
	}
	else
	{
		alert(productQuantity + " " + unescape(productName) + "s " + "have been added to your shopping basket.\n\nAltogether, you have "+
		total_quantity + " of these items in your basket.");
	}
}
