
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 the 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.')
}




showVAT = false;
var CurrFormat = "US";
var currDecimal = 2;

var theId;
var theQuantity;
var thePrice;
var theTotal;
var theProduct;
var theProductID;
var sPoundCost;
var sDollarCost;
var nTheStore;
var nSOflag;
var sAssociateName;

var nLocation;
nCountrySelected =0;	// make sure to set this before using it

var theCurrSign = "\u00A3";


function addToBasket(theStore, theSOflag,  theID, theqty, theProdID,PoundCost,DollarCost)
{
	theId = theID;	// the short form text description
	theQuantity = theqty;

	nTheStore = theStore;	// the originating store - 0 is New Moon
	nSOflag = theSOflag;	// 1 if the item is special order
	//thePrice = price[theId];
	thePrice = PoundCost;
	theTotal = theQuantity * thePrice;
	//theProduct = product[theId];
	theProduct = theID;		// the short form text description
	theProductID = theProdID;	// the unique ID string for the product

	// we want to flag up if this happens to be a special order item
	if (nSOflag == 1)
	{
		theProductID = theProductID + " Special Order";
	}

	// see if we need to show an associate purchase

	if (nTheStore > 0)
	{
	sAssociateName = FindOriginatingSite(nTheStore);
	theProductID = theProductID + " , Ordered from " + sAssociateName;
	}

	sPoundCost = PoundCost;
	sDollarCost = DollarCost;

//alert("theStore = " + nTheStore + "SO = " + nSOflag + " the desc = " + theId + " ProductID = " + theProductID + " £ cost = " + sPoundCost + " $ cost = " + sDollarCost);

  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) + "; expires=" + exp.toGMTString() + 
      ((path) ? "; path=" + path : "") +  ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); 
    document.cookie = ecommCookie; 
  }

function FindOriginatingSite(nIndex)
{
Origin = new Array();
Origin[0] = "New Moon";
Origin[1] = "The first dummy associate";
Origin[2] = "The second dummy associate";
Origin[3] = "The third dummy associate";

return Origin[nIndex];

}



showVAT = false;
vatNum =  "";
VAT = 17.5;

var VATPrice;
var VATTotal;



var cookieinfo;


function getCookieInfo()
{
	// first we check to see if location is set so we can sort out currency and postage
	if(document.cookie.indexOf("location")==-1)
	{
		nLocation = -1;
		//alert("default location is the UK");
	}
	

	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);
	}
	saveToBasket();
}

function getCookieValue(cookieName)
{
	var cookieValue = document.cookie;
	var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
	if (cookieStartsAt == -1)
	{
		cookieStartsAt = cookieValue.indexOf(cookieName + "=");
	}
	if (cookieStartsAt == -1)
	{
		cookieValue = null;
	}
	else
	{
		cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
		var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
		if (cookieEndsAt == -1)
		{
			cookieEndsAt = cookieValue.length;
		}
		cookieValue = unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));
	}
	return cookieValue;
}
var theOldValue;

function saveToBasket()
{
if(document.cookie.indexOf("location")==-1)
{
	//alert("no location set, default location is the UK");
}
nCountrySelected = getCookieValue("location");

// we have to save the country in a cookie, we'll need it later on


	//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 EXIST")
	    

theValue = theProduct + "["+ theProductID +"]" + "#" + theQuantity + "#" + sPoundCost + "#" + sPoundCost + "#" + sDollarCost + "#" + theId + "@";

	    theValue = theValue + cookieinfo;
	       setCookie("tempbasket", theValue, 24, "/")  // expiry in 24hrs
	}
	else
	{
		// we already have at least one of these in the basket, so increment the number accordingly
		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);
		}
		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)
         theExtract = theExtract.substring(cutOff+1, theExtract.length);

	
	
        	//thePrice
        	cutOff = theExtract.indexOf("#")
        	extrPrice = theExtract.substring(0, cutOff)
	theExtract = theExtract.substring(cutOff+1, theExtract.length);
	
        	//theTotal
        	cutOff = theExtract.indexOf("#")
        	extrTotal = theExtract.substring(0, cutOff)
        	theExtract = theExtract.substring(cutOff+1, theExtract.length);
        	
	//theOtherCurrency
        	cutOff = theExtract.indexOf("#")
        	extrOtherCurrency = theExtract.substring(0, cutOff)
  	theExtract = theExtract.substring(cutOff+1, theExtract.length);

        extrQtyInt = formatCurrency(extrQty);
        extrQtyInt += formatCurrency(theQuantity);

        // In the case the price has changed in the middle of ordering
	
        		
	if(nCountrySelected < 2)
	{
		extrPriceInt = sPoundCost;	
	}
	else
	{
		extrPriceInt = sPoundCost;	
	}
	extrTotalInt = extrQtyInt * extrPriceInt;

        NewValue = extrProd + "#" + extrQtyInt + "#" + extrPriceInt + "#" + extrTotalInt + "#" + extrOtherCurrency +  "#" + theId //+ "@";

	    //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
	  }
	//location.href="cgi/basket_novat.html";
	//showAdded()
	location.href="cgi/basket_novat.html";
}
          

  
  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 basket: \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 VAT: " + theCurrSign + doDecimalPos(VATTotal) + "\n" +
	    "(NOTE: Customers not required to pay VAT may remove \n" +
	    "it from the shopping basket.)\n";
    }
       
    //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="basket_novat.html";
  }





	function totalReplace(mainStr, searchStr, resStr)
	{
		while (mainStr.indexOf(searchStr) > -1 )
		{
			mainStr = mainStr.replace(searchStr, resStr);
		}
		return mainStr;
	}



  function formatCurrency(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;
  }

  