var debugOn=0;
var imageindex = ""

function debug(message){
  if( debugOn == 1 ){
    alert(message);
  }
}

function findFreeIndex(){
  debug("findFreeIndex-1");
  var coo = document.cookie;
  var freeIndex = 0;
  var index = 0;
  debug("findFreeIndex-2");
  while ( index != -1 ){
    debug("findFreeIndex-3");
    freeIndex += 1;
    debug("findFreeIndex-4");
    index = coo.indexOf( "item"+freeIndex+"=" );
    debug("findFreeIndex-5");
  }
  debug("findFreeIndex-6");
  return freeIndex;
}

function myAddCookie(cookieName, cookieValue) {
  debug("myAddCookie-1");
  var newcookie = cookieName + "=" + cookieValue;
  debug("myAddCookie-2");
  document.cookie = newcookie;
  debug("myAddCookie-3");
}

function buynow(item,price){
  debug("buynow-1");
  var cookieindex = findFreeIndex();
  debug("buynow-2");
  myAddCookie("item" + cookieindex , item + "@" + price);
  alert( item + " Je sada u vasoj korpi.");
  debug("Cookies:  " + document.cookie);
}


function showCart() {
  var total = 0;
  var itemIndex = 0;
  document.write("<center><table width=100% border=0>");
  while ( true ){
    debug("showCart-1");
    itemIndex++;
    var searchingFor = "item" + itemIndex;
    var itemval = myReadCookie ( searchingFor );
    debug("showCart-2");
    if ( itemval == false ){
      // no more
      debug("showCart-3");
      break;
    }
    var index = itemval.indexOf("@");
    var itemname = itemval.substring(0,index);
    debug("showCart-4");
    var itemprice = itemval.substring(index+1);
    debug("showCart-5");
    total += Number(itemprice);
    debug("showCart-6");
    // write item info in two ways
    // First we need HTML so the items can be seen
    // Second we need to add them to the form
    document.write("<tr><td>"+itemname+" <input type=hidden name=\""+ searchingFor +"\" value=\""+ itemval +"\"></td>");
	document.write("<td valign=\"middle\"><input type=\"checkbox\" name=\"ne narucuje\" value=\"checkbox\"> Vrati iz korpe!</td>");
    document.write("<td><div align=right>Din. "+itemprice+"</div></td>");
    document.write("</tr>");
  }
  // total may have too many digits after . because of calculation imperfection
  var totalString = total.toString();
  var dotpos = totalString.indexOf(".");
  totalFixed = totalString.substring(0,dotpos+7);

  document.write("<tr bgcolor=#00CCFF ><td>Total:</td><td></td><td><div align=right>Din. "+totalFixed+"</div></td></tr>");
  document.write("</table></canter>");
}

function myReadCookie(name) {
  debug("myReadCookie-1");
  if (document.cookie == '') {
   // there's no cookie, so go no further
   debug("myReadCookie-2");
   return false;
  } else {
    debug("myReadCookie-3");
    // there is a cookie
    var firstChar, lastChar;
    var theBigCookie = document.cookie;
    debug("myReadCookie-4");
    firstChar = theBigCookie.indexOf(name);
    // find the start of 'name'
    debug("myReadCookie-5");
    if(firstChar != -1)  {
      // if you found the cookie
      firstChar += name.length + 1;
      // skip 'name' and '='
      lastChar = theBigCookie.indexOf(';', firstChar);
      debug("myReadCookie-6");
      // Find the end of the value string (i.e. the next ';').
      if(lastChar == -1) lastChar = theBigCookie.length;
      debug("myReadCookie-7");
      return theBigCookie.substring(firstChar, lastChar);
    } else {
      // If there was no cookie of that name, return false.
      debug("myReadCookie-8");
      return false;
    }
  }
}

function removeItem(itemToRemove){
  alert("Not implemented yet!");
}
