
var appName = navigator.appName.toLowerCase();
var appVer = parseInt(navigator.appVersion);
var userAgent=navigator.userAgent.toLowerCase();

/*
if ((appName=='microsoft internet explorer' || appName=='netscape')  &&  appVer < 4)
 alert('This site will not work with IE or Netscape browsers below version 4 \n'  + 
'Your current browser version is: ' + navigator.appVersion + '. \n'  +
'Please update your browser, free from Netscape or Microsoft sites.' )
*/

/*
if (!document.getElementById || !document.all)
{
	alert('This site may not work properly on your browser. The site uses \n'  +  
	'Dynamic HTML and your browser does not appear to support it. \n\n'  + 

	'If you are using Internet Explorer or Netscape, please note that \n'  + 
	'IE or Netscape browsers below version 4 do not support DHTML.')
}
*/

if (!document.getElementById || !document.all)
{
	alert('This site may not work properly on your browser. The site uses \nDynamic HTML and your browser does not appear to support it. \n\nIf you are using Internet Explorer or Netscape, please note that \nIE or Netscape browsers below version 4 do not support DHTML.');
}



showVAT = true;
var CurrFormat = "US";
var currDecimal = 2;

var theId;
var theQuantity;
var thePrice;
var theTotal;
var theProduct;

var theCurrSign = "\u0024";


function addToBasket(theID, theqty)
{
	theId = theID;
	theQuantity = theqty;
	thePrice = price[theId];
	theTotal = theQuantity * thePrice;
	theProduct = product[theId];


  if(isNaN(theqty))
  {
    alert(theqty + " is not a proper quantity. \nPlease enter a proper number.")
    return;
  }
  else if(theqty < 1)
  {
    alert("You cannot order a quantity less than 1.")
    return;
  }
  else if (document.cookie.indexOf('cookies') == -1) //cookies doesn't exist!
  {
		if (cookiesEnabled())
			getCookieInfo();
		else
			location.href = "no_cookies.html";
  }
  else
  	getCookieInfo();
}




function cookiesEnabled() 
{ 
	 //half and hour expiry.
	 setCookie("cookies", "cookies", 1); 

	 if (document.cookie.indexOf('cookies') == -1)
	   return false
	 else
		return true;
 }


  function setCookie(name, value, expires, path, domain, secure)
  { 
     var exp = new Date(); 
     exp.setTime(exp.getTime() + (3600000 * expires)); //hrs

    
    var ecommCookie = name + "=" + escape(value) + 
    //var ecommCookie = name + "=" + value + 
      "; expires=" + exp.toGMTString() + 
      ((path) ? "; path=" + path : "") + 
      ((domain) ? "; domain=" + domain : "") + 
      ((secure) ? "; secure" : ""); 
    document.cookie = ecommCookie; 
  }







showVAT = true;
vatNum =  "14567002752";
VAT = 0.145;


price = new Array();
product = new Array();

price['rootWireless_Merchant_ServicesMobilescape_5000'] = 1200.00;
price['rootWireless_Merchant_ServicesMobilescape_3000'] = 800.00;
price['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_AV100'] = 998.00;
price['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_OA180_NEW_PRODUCT'] = 799.00;
price['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_OA2000'] = 2100.00;
price['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_AV300'] = 2800.00;
price['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_AV200'] = 998.00;
price['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_FE900'] = 2998.00;
price['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_FE800'] = 3198.00;
price['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_OA100'] = 895.00;
price['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_READ_ME_FIRSTBIOMETRIC_INFORMATION'] = 0.00;
price['rootProximity_Card_ReaderAC100'] = 698.00;
price['rootGPS_Vehicle_Tracking_SystemsWireless_Vehicle_Tracking_Device'] = 495.00;
price['rootGPS_Vehicle_Tracking_SystemsG4_Echo_Wireless_Passive_GPS_System'] = 695.00;
price['rootGPS_Vehicle_Tracking_SystemsTM_GPS_ModuleHYPHENPassive_System'] = 395.00;

product['rootWireless_Merchant_ServicesMobilescape_5000'] = 'Mobilescape 5000';
product['rootWireless_Merchant_ServicesMobilescape_3000'] = 'Mobilescape 3000';
product['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_AV100'] = 'AV100';
product['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_OA180_NEW_PRODUCT'] = 'OA180 NEW PRODUCT';
product['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_OA2000'] = 'OA2000';
product['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_AV300'] = 'AV300';
product['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_AV200'] = 'AV200';
product['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_FE900'] = 'FE900';
product['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_FE800'] = 'FE800';
product['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_OA100'] = 'OA100';
product['rootBiometricsHYPHENTrident_Industries_LLcPERIOD_READ_ME_FIRSTBIOMETRIC_INFORMATION'] = 'BIOMETRIC INFORMATION';
product['rootProximity_Card_ReaderAC100'] = 'AC100';
product['rootGPS_Vehicle_Tracking_SystemsWireless_Vehicle_Tracking_Device'] = 'Wireless Vehicle Tracking Device';
product['rootGPS_Vehicle_Tracking_SystemsG4_Echo_Wireless_Passive_GPS_System'] = 'G4 Echo Wireless Passive GPS System';
product['rootGPS_Vehicle_Tracking_SystemsTM_GPS_ModuleHYPHENPassive_System'] = 'TM GPS Module-Passive System';



var VATPrice;
var VATTotal;



var cookieinfo;

function getCookieInfo()
{
	if(document.cookie.indexOf("tempbasket") == -1)
	{
	  cookieinfo = "";
	}
	else
	{
	  cInfoStart = document.cookie.indexOf("tempbasket=") + 11;
	  cInfoEnd = document.cookie.indexOf(";", cInfoStart);

	  if (cInfoEnd == -1)
	    cInfoEnd = document.cookie.length

	  cookieinfo = document.cookie.substring(cInfoStart, cInfoEnd);
	  cookieinfo = unescape(cookieinfo);
	}
	//alert("cookieinfo  = " + cookieinfo)	
	saveToBasket();
}



var theOldValue;

function saveToBasket()
{
	//if this product does not exist in the basket already, just append it directly to the list.
	if(cookieinfo.indexOf("#" + theId + "@") == -1 ) // have to put the # and @ to make it absolutely unique, otherwise if CameraB2 exists, it will erroneously say CameraB exists!!!!!
	{
	    //alert("PRODUCT DOES NOT exists!!!!")

	    theValue = theProduct + "#" + theQuantity + "#" + thePrice + "#" + theTotal + "#" + theId + "@";
	    theValue = theValue + cookieinfo;

	    //alert("theValue = " + theValue)
	    setCookie("tempbasket", theValue, 24, "/")  // expiry in 24hrs
	}
	else
	{
		theIdPos = cookieinfo.indexOf(theId)
		prodEnd = cookieinfo.indexOf("@", theIdPos)

		//prodEnd--; // remove the  "@"
		theExtract = cookieinfo.substring(0, prodEnd);

		prodStart = theExtract.lastIndexOf("@")
		if (prodStart != -1)
		{
		  theExtract = theExtract.substring(prodStart+1, theExtract.length);
		}
	
		//alert("theExtract = " + theExtract)
		theOldValue = theExtract;


        //theProduct
        cutOff = theExtract.indexOf("#")
        extrProd = theExtract.substring(0, cutOff)
        theExtract = theExtract.substring(cutOff+1, theExtract.length);

        //theQuantity
        cutOff = theExtract.indexOf("#")
        extrQty = theExtract.substring(0, cutOff)
        //alert("extrQty = " + extrQty)
        theExtract = theExtract.substring(cutOff+1, theExtract.length);

        //thePrice
        cutOff = theExtract.indexOf("#")
        extrPrice = theExtract.substring(0, cutOff)
        //alert("extrPrice = " + extrPrice)
        theExtract = theExtract.substring(cutOff+1, theExtract.length);

        //theTotal
        cutOff = theExtract.indexOf("#")
        extrTotal = theExtract.substring(0, cutOff)
        theExtract = theExtract.substring(cutOff+1, theExtract.length);
        
        extrQtyInt = formatCurrency(extrQty);
        extrQtyInt += formatCurrency(theQuantity);

        //extrPriceInt = formatCurrency(extrPrice);
        // In the case the price has changed in the middle of ordering????
        extrPriceInt =  thePrice

        extrTotalInt = extrQtyInt * extrPriceInt;

        NewValue = extrProd + "#" + extrQtyInt + "#" + extrPriceInt + "#" + extrTotalInt + "#" + theId //+ "@";

		// alert("NewValue = " + NewValue + "\n" + "theOldValue = " + theOldValue)

	    //replace old value with new value...
	    startReplaceint = cookieinfo.indexOf(theOldValue);
	    endReplaceint = startReplaceint + theOldValue.length;

	    cookieinfo = cookieinfo.substring(0, startReplaceint) + NewValue + cookieinfo.substring(endReplaceint, cookieinfo.length);

	    setCookie("tempbasket", cookieinfo, 24, "/") // exp in 24hrs

	   // alert("product 2 = " + extrProd)
	}
	
	showAdded()
}
          

  
  function showAdded()
  {
    theTotal = formatCurrency(theTotal);
    
    VATPrice = thePrice + (thePrice * VAT); 
    VATTotal = theQuantity * VATPrice; 
    VATTotal = formatCurrency(VATTotal);

    var theDisplay = 
    "The following product has been added to your shopping cart: \n\n" +
    "Product: " + theProduct + "\n" +
    "Quantity: " + theQuantity + "\n" +
    "Price: " + theCurrSign + doDecimalPos(thePrice) + "\n" +
    "Total: " + theCurrSign + doDecimalPos(theTotal) + "\n";

    var theDisplay2 = "";
    
    if (showVAT)
    {
    	theDisplay2 =
	    "\nTotal incl Tax: " + theCurrSign + doDecimalPos(VATTotal) + "\n" +
	    "(NOTE: Customers not required to pay sales tax may remove \n" +
	    "it from the shopping cart.)\n";
    }
    //alert(theDisplay + theDisplay2);
    
    //problems with Dollar sign.
    theDisplay = totalReplace(theDisplay, "&#36;", "$");
    theDisplay2 = totalReplace(theDisplay2, "&#36;", "$");
    
    var theDisplay3 =  
    "\nWould you like to checkout now? Press 'OK' to checkout \n" +
    "and 'Cancel' to continue shopping. \n "
    
    if (confirm(theDisplay + theDisplay2 + theDisplay3))
      location.href="cgi/basket_vat.html";
  }





	function totalReplace(mainStr, searchStr, resStr)
	{
		while (mainStr.indexOf(searchStr) > -1 )
		{
			mainStr = mainStr.replace(searchStr, resStr);
		}
		return mainStr;
	}



  function formatCurrency(oldValue)
  {
     //alert("oldValue = " + oldValue);
     oldValue = "" + oldValue + "";
     point = oldValue.indexOf(".")
     
     // if there is a decimal point and it is longer than 3 characters...
     if (point > -1 && point < oldValue.length-3) 
     {
		 wholeNo = oldValue.substring(0,point)
		 decNo = oldValue.substring(point+1, oldValue.length)
	 
     	    firstDec = decNo.substring(0,1);
     	    secondDec = decNo.substring(1,2);
     	    thirdDec = decNo.substring(2,3)
     	     if (thirdDec > 4 && secondDec < 9)
     	       secondDec++
     	    else if (thirdDec > 4 && secondDec == 9)
     	    {
     	      secondDec = 0
     	      if (firstDec < 9)
     	       firstDec++
     	      else if (firstDec == 9)
     	      {
     	        firstDec = 0;
     	        wholeNo++
     	      }
     	    }
     	     newValue = wholeNo + "." + firstDec + "" + secondDec;
	 
	 		return parseFloat(newValue)
	 }
     else
 		return parseFloat(oldValue)
  }




  function doDecimalPos(oldValue)
  {
     oldValue = "" + oldValue + "";
     point = oldValue.indexOf(".")
     if (point == -1)
       newValue = oldValue + ".00";
     else if (point == oldValue.length-2)
		newValue = oldValue + "0";
	else
		newValue = oldValue;
     
     	return displayFrFormat(newValue);
  }
  
  
  function displayFrFormat(theFrPrice)
  {
     if(CurrFormat == "FR")
     {
  		theFrPriceStr = theFrPrice + ""; //change to string.
  		theFrPriceStr = theFrPriceStr.replace(".", ",");
  		return theFrPriceStr;
  	}
  	else
  	 return theFrPrice;
  }

  