var recentHash = "";

   window.onload = function() {
     
     setInterval(initialiseStateFromURL, 500);
   }
 
   function initialiseStateFromURL() {
     
     recentHash = pollHash(recentHash);
  
   }

   function pollHash(recentHash) {
  
     if (window.location.hash==recentHash)
     {
     
       return recentHash;
     }
     else
     {
     
     recentHash = window.location.hash;

     var temp = new Array();
     temp = recentHash.split('/');
     
     var menu = temp[0];
     var id = temp[1];
     
     update(menu, id);

     return recentHash;
     }
   }  

   function update(menu, id)
   {
 
     if (menu == "#category")
     {
       new function() {
       new Ajax.Updater('categories', '/products/prodlist/id/' + id, {asynchronous:true, evalScripts:true});; return false;}
     }
     else if (menu == "#subcategory")
     {
       new function() {
       new Ajax.Updater('categories', '/products/prodlist/subid/' + id, {asynchronous:true, evalScripts:true}); return false;}     
     }
     else if (menu == "#product")
     {
       new function() {
       new Ajax.Updater('categories', '/products/proddetails/id/' + id, {asynchronous:true, evalScripts:true}); return false;}        
     }
     else if (menu == "#aboutus")
     {
       new function() {
       new Ajax.Updater('categories', '/aboutus/details/id/' + id, {asynchronous:true, evalScripts:true}); return false;}
     }
   }
   
   function updateItem(inputid, id) {
       var value = document.getElementById(inputid).value;
       location.replace("/cart/update/id/" + id + "/value/" + value);
   }
   
   function addItem(inputid, id) {
       var value = document.getElementById(inputid).value;
       location.replace("/cart/add/id/" + id + "/value/" + value);
   }

   function updatebasket(inputid, id, priceid, sumprice2, oldvalue2) {
			var value = document.getElementById(inputid).value;
			var sumprice = document.getElementById('sumpricehidden').value;
			var oldvalueinp = document.getElementById('oldprod'+id);
			var oldvalue = oldvalueinp.value;
			oldvalueinp.value = value;
			document.getElementById("updated"+id).style.display = "block";
			retadpuxaja("updatebasket", "/cart/updateitem/id/" + id + "/value/" + value);
			retadpuxaja(priceid, "/cart/updateprice/id/" + id + "/value/" + value);
			retadpuxaja("pricesummary", "/cart/updatepricesummary/id/" + id + "/value/" + value + "/sumprice/" + sumprice + "/oldvalue/" + oldvalue);
			setTimeout ( "setupdatedHide("+id+")", 3000 );
   }
   
   function addtobasket(inputid, id) {
       var value = document.getElementById(inputid).value;
       retadpuxaja("updatebasket", "/cart/additem/id/" + id + "/value/" + value);
       document.getElementById("added"+id).style.display = "block";
       setTimeout ( "setaddedHide("+id+")", 3000 );
   }
   
   function retadpuxaja(divid, url) {
       new Ajax.Updater(divid, url, {asynchronous:true, evalScripts:true});; return false;
   }

   function retadpuxaja3(divid, url, value) 
   {
     if (value == "")
     {
       $('telepules_result').hide();
     }
     else
     {
     $('telepules_result').show();
     new Ajax.Updater(divid, url, {asynchronous:true, evalScripts:true});; return false;
     }
   }

   function setaddedHide (id)
   {
       document.getElementById("added"+id).style.display = "none";
   }
   
   function setupdatedHide (id)
   {
       document.getElementById("updated"+id).style.display = "none";
   }

   function setcouponokHide (id)
   {
       document.getElementById("couponok"+id).style.display = "none";
   }

   function setcouponerrorHide (id)
   {
       document.getElementById("couponerror"+id).style.display = "none";
   }
   
   function submitForm (id)
   {
       document.forms[id].submit();
       return false;
   }

function jumpToProduct()
{
  var productid = document.getElementById("jump_to_product").value;

  if (productid != 0)
  {
	location.href = "/products/details/id/" + productid;
  }
}

function simulateClick() {
  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window,
    0, 0, 0, 0, 0, false, false, false, false, 0, null);
  var cb = document.getElementById("video_link"); 
  var canceled = !cb.dispatchEvent(evt);
	return false;
}

function validate_coupon(id) {
  var value = document.getElementById('ccode'+id).value;
	var number = document.getElementById('prod'+id).value;
	var proddiscountinput = document.getElementById('prod'+id+'discount');
	var productdiscount = proddiscountinput.value;
	var sumprice = document.getElementById('sumpricehidden').value;
	new Ajax.Request("/cart/checkCoupon/id/"+id+"/value/"+value, {
	  method: 'get',
	  onSuccess: function(transport) {
	    if (transport.responseText>0) {
				proddiscountinput.value = transport.responseText;
				document.getElementById("couponerror"+id).style.display = "none";
				document.getElementById("couponok"+id).style.display = "block";
				
				retadpuxaja("pieceprice"+id, "/cart/updatepieceprice/id/" + id + "/value/" + value);
			  retadpuxaja("sumprice"+id, "/cart/updatesumprice/id/" + id + "/value/" + value + "/number/" + number);
			  retadpuxaja("pricesummary", "/cart/updatesummarydiscount/id/" + id + "/value/" + value + "/number/" + number + "/sumprice/" + sumprice + "/discount/"+ productdiscount);
				
				setTimeout ( "setcouponokHide("+id+")", 3000 );
			} else {
				document.getElementById("couponok"+id).style.display = "none";
				document.getElementById("couponerror"+id).style.display = "block";
				
				setTimeout ( "setcouponerrorHide("+id+")", 3000 );
			}
		}
	});
}
