﻿function RemoveProduct(Vid)
{
   var result=Pronutrition.Controls.Sidebar.RemoveProduct(Vid);
   var cant=document.getElementById("divCant"+Vid).innerHTML;
   var pret=document.getElementById("divPret"+Vid).innerHTML;
   if(document.getElementById("item"+Vid))
   { 
     document.getElementById("item"+Vid).parentNode.removeChild(document.getElementById("item"+Vid));
   }
    if(result!=null && result.value!=null && result.value==0)
   {  

       document.getElementById("NoItems").style.display="block";
       document.getElementById("shoppingCartBottom").style.display="none";
   }
   else
   {
      var total=parseFloat(document.getElementById("divTotal").innerHTML.toString().replace(",","."))-parseFloat(cant)*parseFloat(pret.toString().replace(",","."));
      document.getElementById("divTotal").innerHTML=parseFloat(total).toFixed(2);
   }
   
}

function AddToChart(Vid,callback)
{
    
     
     if(document.getElementById("txCantitate"+Vid)!=null && document.getElementById("txCantitate"+Vid).value!="" && isPozNum(document.getElementById("txCantitate"+Vid).value) && document.getElementById("txCantitate"+Vid).value<=99 && document.getElementById("txCantitate"+Vid).value.indexOf(".")<0)
     {
     var cant=document.getElementById("txCantitate"+Vid).value;
     var total=0;     
     
     var resultAdd=Pronutrition.Controls.Sidebar.AddToChart(Vid,cant);

     if(resultAdd!=null && resultAdd.value!=null && resultAdd.value!="")
     {
        if(resultAdd.value==1)
         {
          var result=Pronutrition.Controls.Sidebar.RealAdd(Vid,cant);
          if(result!=null && result.value!=null)
            {
               
               var continut='<div class=\"item\" id=\"item'+result.value["VariantID"]+'\">'+
		      '<div class=\"col1\">'+
		   	  '<div class=\"name\"><strong>'+result.value["ProductName"]+" - " +result.value["VariantName"]+'</strong></div>'+
			   '<div id=\"divCant'+result.value["VariantID"]+'\" class=\"fl\">'+cant+'</div><div class=\"fl\">&nbsp;X&nbsp;</div><div id=\"divPret'+result.value["VariantID"]+'\" class=\"price fl\">'+result.value["VariantReducePrice"].toFixed(2).toString().replace(".",",")+'</div><div class=\"fl\">&nbsp;RON</div>'+
			  '</div>'+
			  '<div class=\"col2\"><a href="javascript:RemoveProduct('+result.value["VariantID"]+')"><img src="/i/iconDeleteItem.gif" alt=\"\" /></a></div>'+
			  '<div class="clr"><img src="/i/spacer.gif" alt="" /></div><div class=\"clr itemSeparator\"><img src=\"/i/spacer.gif\" alt=\"\" /></div></div>';
			  if(document.getElementById("shoppingCartItems"))
			  {
			     document.getElementById("shoppingCartItems").innerHTML+=continut;
			  }
			  if(document.getElementById("NoItems").style.display=="block" || document.getElementById("NoItems").style.display=="")
			  {
			    document.getElementById("NoItems").style.display="none";
			    document.getElementById("shoppingCartBottom").style.display="block";
			  }
			  else
			  {
			    total=parseFloat(document.getElementById("divTotal").innerHTML.toString().replace(",","."));
			  }
	            total+=parseFloat(cant)*result.value["VariantReducePrice"];
	            document.getElementById("divTotal").innerHTML=parseFloat(total).toFixed(2).toString().replace(".",",");
            }
         }
         else
         {
           if(resultAdd.value==2)
           {
              if(document.getElementById("divCant"+Vid))
              { 
                 cant1= parseInt(document.getElementById("divCant"+Vid).innerHTML)+parseInt(cant);
                 document.getElementById("divCant"+Vid).innerHTML=cant1;
                 
              }
               total=parseFloat(document.getElementById("divTotal").innerHTML.toString().replace(",","."));
              total+=parseInt(cant)*parseFloat(document.getElementById("divPret"+Vid).innerHTML);
	          document.getElementById("divTotal").innerHTML=parseFloat(total).toFixed(2).toString().replace(".",",");
           }
         }
         document.getElementById("txCantitate"+Vid).value='1';
     }
     if(callback != undefined)
     {
        if(callback != '')
        {
            eval(callback);
        }
     }
     document.location = "#pageTop";
  }
  else
  {
     alert("Va rugam introduceti cantitati valide pentru produse (numere naturale intre 1 si 99)");
  }
}

function changeOption(cid)
{
//    if ($('ddBrands').value != "0")
//    {
        document.location.href = 'produse.aspx?bID=' + $('ddBrands').value + '&cID=' + cid;    
//    }
}