item_Currency="&pound;";

	


//VARIABLES FOR GLOBAL USE
no_of_order_pages = 200; //the number of possible product pages selected for ordering  


function getListValue(list) 
{ 
   var listValue = ""; 

   if ( list.selectedIndex != -1 )
   { 
      listValue = list.options[ list.selectedIndex ].value; 
   } 

   return( listValue ); 
} 


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 makeArray(n)
{
 	this.length = n;
	for (var i = 0; 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;
}

	

// 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; total_count>=i; 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; elmstr.length>i; 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; elmstr.length>i; 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 (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;
}


		
// check for valid quantity and prompt user if there is an error
// if valid is okay, update the hidden variable used by the basket buttons.
//
// "formObj" should equal to something like "document.buynow"
// This function affects the following fields:
//	Order_ItemQuantity	=> the text input box, this is the field that's used to submit to CGI
//	LastGoodQuan		=> the field is used to store previous value in case of mistake
function updateQuantity(formObj)
{
	// If quantity is valid, then go on to add stuff to cookie
	Qty=RoundQty(formObj.Order_ItemQuantity.value);
	if (Qty >=10000 || Qty==0 || formObj.Order_ItemQuantity.value==0)
	{
    		alert ("Please enter a valid number that is greater than 0.01 and less than 9999.99!");
		formObj.Order_ItemQuantity.value = formObj.LastGoodQuan.value;
    		return;
  	}
  	formObj.LastGoodQuan.value = Qty;
 	formObj.Order_ItemQuantity.value = Qty;
}
	

// 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)
{
	// test if browser supports cookie
  	document.cookie = "testcookie=mytestvalue";
  	if (GetCookie("testcookie")!="mytestvalue")
	{
   		alert("Your browser does not appear to support cookies.\nPlease either turn the feature on or use to a different browser.");
		return false;
	}


	// 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)
		{
   			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="basket.htm";
	return true;
}


// This function must be called by the inquiry form as an onsubmit action
//  formObj would be something like document.forms.inquiry, document.forms.selectoptions
function inquiryNow(inquiryFormObj, selectOptionsFormObj)
{
	// find the first ItemOption hidden var index on the inquiry form
	for (j = 0; j < inquiryFormObj.elements.length; j++ )
	{
		if ( inquiryFormObj.elements[j].name == "Order_ItemOptions" )
			break;
	}
	if ( j >= inquiryFormObj.elements.length)
	{
		return 1;
	}
	// copy all itemOption hidden var from the buynow's selectoptions form
	for ( i = 0; i < selectOptionsFormObj.elements.length; i++ )
	{
		if ( selectOptionsFormObj.elements[i].name == "Order_ItemOptions" )
		{
			if ( selectOptionsFormObj.elements[i].type=="select-one" )
				inquiryFormObj.elements[j].value = getListValue( selectOptionsFormObj.elements[i] );
			else
				inquiryFormObj.elements[j].value = selectOptionsFormObj.elements[i].value;
			j++;
		}
	}
	return 1;
}



//For order form (not inquiry), reconstruct all the options to a single variable
//  formObj would be something like document.forms.selectoptions
function reconstructOptions(formObj)
{
	var strOptions="";
	var hasComma=0;

	for ( i = 0; i < formObj.elements.length; i++ )
	{
		if ( formObj.elements[i].name == "Order_ItemOptions" )
		{
			var	strTmp="";
			if ( formObj.elements[i].type == "select-one" )
				strTmp = getListValue(formObj.elements[i]);
			else
				strTmp = formObj.elements[i].value;
			nLen=strTmp.length;

			if (nLen > 2 && strTmp.charAt(nLen-1)=='&' && strTmp.charAt(nLen-2)==':')
				strOptions += strTmp.substring(0,nLen-1);		// strip the & at the end
			else
			{
				strOptions += strTmp + ",";
				hasComma=1;
			}
		}
	}
	if (hasComma==1)
		strOptions=strOptions.substring(0, strOptions.length-1);

	return escape(strOptions);
}




function modify_field(inputfield)
{
	var returnString = "";

	for ( i = 0; i < inputfield.value.length; i++ )
	{
		var c = inputfield.value.charAt(i);
		if (c!="," && c!=":")
			returnString += c;
	}

	if ( returnString != inputfield.value )
	{
		inputfield.value = returnString;
		alert ( "The characters ',' and ':' are not allowed in this field, and therefore are being removed." );
	}
}



