
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 = "FR";
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 + " nije tocna kolicina. \nMolimo vas da unesete odgovarajuci broj.")
    return;
  }
  else if(theqty < 1)
  {
    alert("Ne mo\u017Eete naruciti kolicinu manju od 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 = 17.5;


price = new Array();
product = new Array();

price['rootprodajaKnjiga_1'] = 10.00;
price['rootprodajaKnjiga_2'] = 8.00;

product['rootprodajaKnjiga_1'] = 'Knjiga 1';
product['rootprodajaKnjiga_2'] = 'Knjiga 2';



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 = 
    "Sljedeci proizvod je dodan va\u0161oj ko\u0161arici: \n\n" +
    "Proizvod: " + theProduct + "\n" +
    "Kolicina: " + theQuantity + "\n" +
    "Cijena: " + theCurrSign + doDecimalPos(thePrice) + "\n" +
    "Ukupno: " + theCurrSign + doDecimalPos(theTotal) + "\n";

    var theDisplay2 = "";
    
    if (showVAT)
    {
    	theDisplay2 =
	    "\nUkupno ukljucujuci PDV/porez: " + theCurrSign + doDecimalPos(VATTotal) + "\n" +
	    "(Kupci koji ne trebaju platiti PDV/porez \n" +
	    "mogu ga izvaditi iz ko\u0161arice)\n";
    }
    //alert(theDisplay + theDisplay2);
    
    //problems with Dollar sign.
    theDisplay = totalReplace(theDisplay, "&#36;", "$");
    theDisplay2 = totalReplace(theDisplay2, "&#36;", "$");
    
    var theDisplay3 =  
    "\n\u017Delite li sada platiti? Pritisnite 'OK' za placanje \n" +
    "i 'Odustani' za nastavak kupovanja. \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;
  }

  