//-----------------------------------------------------------------------------------------
// --------------------------------private functions ----------------------------------------
//-----------------------------------------------------------------------------------------

function getGoodNum(nLocalNumb)
{
	var strTemp;
	nLocalNumb = Math.round(nLocalNumb * 100)/100;
	if (Math.round(nLocalNumb) == nLocalNumb) {
	      strTemp = nLocalNumb + ".00";
	} else if (Math.round(nLocalNumb * 10) == (nLocalNumb * 10)) {
		strTemp = nLocalNumb + "0";
	} else {
	   	strTemp = nLocalNumb + "";
    	}
    	
    	return strTemp;
}

function calcTotal()
{
   // calculate total
    var nLocalTotal = 0;
    for (nCountTotal = 0; nCountTotal < nMaxItems; nCountTotal++) {
    	nLocalTotal += parseFloat(document.all["sum" + nCountTotal].innerHTML);
    }
    
    return nLocalTotal;    
}

function Calc(nNum)
{
    nMaxItems = document.forms["orderform"].elements["max_items"].value;
    
    // change the text box
    var nTemp;
    nTemp = document.forms["orderform"].elements["hid_price_" + nNum].value * document.forms["orderform"].elements["quant" + nNum].value
    document.all["sum" + nNum].innerHTML = getGoodNum(nTemp);

    var nTotal = calcTotal();
    document.all["dyntotal"].innerHTML = "\$" + getGoodNum(nTotal);
    
    return 0;
}

function changeLinksText()
{
  nTotalNum = calcTotal();
  if (nTotalNum == 0) {
  	document.all["add_link"].innerHTML = "Add to shopping bag";
  	document.all["add_link2"].innerHTML = "Check out";
  } else {
  	document.all["add_link"].innerHTML = "Update shopping bag";
  	document.all["add_link2"].innerHTML = "Check out";
  }
}

function CookieFound(strLocCookieName)
{
   var arrCookie = document.cookie.split(";");
   
   for (Count1 = 0; Count1 < arrCookie.length; Count1++){
       	arrName = arrCookie[Count1].split("=");
 	if (arrName[0].indexOf(strLocCookieName) != -1) {
            // we have found a cookie
            return true;
        }
   }
   
   return false;
}

function setCookie(nLocalNum)
{
    var strCookieName = "__id" + document.forms["orderform"].elements["hid_quantid_" + nLocalNum].value;
    var strNewCookieValue = document.forms["orderform"].elements["quant" + nLocalNum].value;
    
    // we should not set cookie if it was not there, and the new value is 0
    if ((!CookieFound(strCookieName)) && (parseInt(strNewCookieValue) == 0)) {
    	// alert("we are not setting the cookie " + strCookieName + " !");
    	return false;
    }
    
    document.cookie = strCookieName + "=" + strNewCookieValue;
    // alert("SetCookie " + document.cookie);
    
    return true;
}

function setSourceCookie(mySource)
{
	if (mySource)
		document.cookie = "__source" + "=" + mySource;
		
	// alert("SetCookie " + document.cookie);
}

//-----------------------------------------------------------------------------------------
// --------------------------------public functions ----------------------------------------
//-----------------------------------------------------------------------------------------

// sets the elements data
function loadData()
{
  if (document.forms["orderform"] == null) return 1;
  if (document.cookie == "") return 1;
  
  nMaxItems = document.forms["orderform"].elements["max_items"].value;
  if (nMaxItems == 0) return 1;
  
  var arrCookie = document.cookie.split(";");
  for (i = 0; i < arrCookie.length; i++){
  	strName = arrCookie[i].split("=");
  	// alert("cookie " + i + " = " + strName);
  	
  	// if strName is "id" + number, then its the product
  	for (j = 0; j < nMaxItems; j++) {
  	  if (strName[0].indexOf("__id" + document.forms["orderform"].elements["hid_quantid_" + j].value) != -1) {
  	     document.forms["orderform"].elements["quant" + j].value = strName[1];
  	     Calc(j);
  	  }
  	}
  }
  
  changeLinksText();
  
  return 0;
}

function updateShoppingBag(nParam, source)
{
    nMaxItems = document.forms["orderform"].elements["max_items"].value;
    for (i = 0; i < nMaxItems; i++) {
       setCookie(i);
    }
    
    if (nParam == 1) {
    	document.location = "https://www.farleaves.com/shop/order.php3";
    } else {
    	// alert("Shopping bag was successfully updated!");
    	changeLinksText();
    }
    
    return;
}