
var appName = navigator.appName.toLowerCase();
var appVer = parseInt(navigator.appVersion);
var userAgent=navigator.userAgent.toLowerCase();


//If your browser does not support DHTML ....
if (!document.getElementById)
{
	alert('Tato stránka nemusí ve va?em prohlí?eči fungovat správně. Stránka \nvyu?ívá dynamický HTML, který vá? prohlí?eč pravděpodobně nepodporuje. \n\nPokud pou?íváte prohlí?eč Internet Explorer nebo Netscape, upozorňujeme, \n?e IE nebo Netscape ni??ích verzí ne? verze 4 kód DHTML nepodporují.');
}



showVAT = false;
var CurrFormat = "US";
var currDecimal = 2;

var theId;
var theQuantity;
var thePrice;
var theTotal;
var theProduct;

var theCurrSign = "\u20AC";


function addToBasket(theID, theqty)
{
	theId = theID;
	theQuantity = theqty;
	thePrice = price[theId];
	theTotal = theQuantity * thePrice;
	theProduct = product[theId];


  if(isNaN(theqty))
  {
    alert(theqty + " není správné mno\u017Eství. \nZadejte správné číslo.")
    return;
  }
  else if(theqty < 1)
  {
    alert("Nemů\u017Eete si objednat méně ne\u017E 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 = false;
vatNum =  "";
VAT = 0.175;


price = new Array();
product = new Array();

price['krby1'] = 120.00;
price['krb_2'] = 100.00;
price['krb_3'] = 290.00;

product['krby1'] = 'krby1';
product['krb_2'] = 'krb 2';
product['krb_3'] = 'krb 3';



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 = 
    "Do Va\u0161eho nákupního ko\u0161íku byl přidán následující produkt: \n\n" +
    "Produkt: " + theProduct + "\n" +
    "Mno\u017Eství: " + theQuantity + "\n" +
    "Cena: " + theCurrSign + doDecimalPos(thePrice) + "\n" +
    "Celkem: " + theCurrSign + doDecimalPos(theTotal) + "\n";

    var theDisplay2 = "";
    
    if (showVAT)
    {
    	theDisplay2 =
	    "\nSoučet včetně DPH/daně: " + theCurrSign + doDecimalPos(VATTotal) + "\n" +
	    "(Zákazníci, kteří nemusí platit DPH/daň, \n" +
	    "ji mohou vyjmout z ko\u0161íku)\n";
    }
    //alert(theDisplay + theDisplay2);
    
    //problems with Dollar sign.
    theDisplay = totalReplace(theDisplay, "&#36;", "$");
    theDisplay2 = totalReplace(theDisplay2, "&#36;", "$");
    
    var theDisplay3 =  
    "\nChcete se teď odhlásit? Odhlaste se stisknutím tlačítka 'OK' \n" +
    "a po stisknutí tlačítka 'Storno' mů\u017Eete pokračovat v nákupech. \n "
    
    if (confirm(theDisplay + theDisplay2 + theDisplay3))
      location.href="cgi/basket_novat.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;
  }

  