//Shoping Cart BEGIN

var divsCartCountIds;
var divCartUpdatingId;
var labelTotalNumber;
var productId;
var updatingProduct = false;

function AddToShoppingCart(currentProductId,divUpdatingId, updatePictureUrl)
{
   if(!updatingProduct)
   {
      divCartUpdatingId = divUpdatingId;
      productId = currentProductId;
      
      updatingProduct = true;

      DisplayProductProgress(divCartUpdatingId, updatePictureUrl);   
      
      retVal = ServiceMethods.AddProduct(productId,OnAddComplete,OnAddTimeOut,OnAddError);
   }
}

function OnAddComplete(value)
{
   if (value != null) 
   {
      divsCartCountIds = value;
      GetShoppingCartCount(productId);
   }
   else alert('can\'t add');
   updatingProduct = false;
   
   RemoveProductProgress(divCartUpdatingId);
}

function OnAddTimeOut(value)
{
   alert('Server is busy. Try again later.');
   updatingProduct = false;
   
   RemoveProductProgress(divCartUpdatingId);
}

function OnAddError(value)
{
   alert('Add Error');
   updatingProduct = false;
   
   RemoveProductProgress(divCartUpdatingId);
}

function GetShoppingCartCount(productId)
{
   retVal = ServiceMethods.GetProductCount(productId,OnCartCountComplete,OnCartCountTimeOut,OnCartCountError);   
}

function OnCartCountComplete(value)
{   
   if(value == 0)
   {
      for(index in divsCartCountIds)
      {
         document.getElementById(divsCartCountIds[index]).style.display = "none";
         document.getElementById(divsCartCountIds[index]).innerHTML = "";
      }
   }
   else
   {  
      for(index in divsCartCountIds)
      {
         document.getElementById(divsCartCountIds[index]).style.display = "block";
         document.getElementById(divsCartCountIds[index]).innerHTML = ValueString(value);
         document.getElementById(divsCartCountIds[index]).style.cursor = "pointer";  
      }    
   }
   updatingProduct = false;
}

function OnCartCountTimeOut(value)
{
   alert('Server is busy. Try again later.');
   updatingProduct = false;
}

function OnCartCountError(value)
{
   alert('Count Error');
   updatingProduct = false;
}

function ValueString(value)
{
   
   paragraph = "<br/>"+ value;
   return paragraph;
}

function RemoveFromCart(currentProductId, divUpdatingId, updatePictureUrl)
{
   if(!updatingProduct)
   {
      divCartUpdatingId = divUpdatingId;
      productId = currentProductId;
      updatingProduct = true;
   
      DisplayProductProgress(divCartUpdatingId, updatePictureUrl);
      
      retVal = ServiceMethods.RemoveProduct(productId,OnRemoveComplete,OnRemoveTimeOut,OnRemoveError);
   }
}

function OnRemoveComplete(value)
{   
   if(value != null) 
   {
      divsCartCountIds = value;
      GetShoppingCartCount(productId);
   }
   updatingProduct = false;
   
   RemoveProductProgress(divCartUpdatingId);
}

function OnRemoveTimeOut(value)
{
   alert('Server is busy. Try again later.');
   updatingProduct = false;
  
   RemoveProductProgress(divCartUpdatingId);
   
}

function OnRemoveError(value)
{
   alert('Remove Error');
   updatingProduct = false;
  
   RemoveProductProgress(divCartUpdatingId);
}


function DisplayProductProgress(divUpdateId, updatePictureUrl)
{
   document.getElementById(divUpdateId).style.display = "block";
   document.getElementById(divUpdateId).innerHTML = "<img src='" + updatePictureUrl + "' alt='Updating' />";
}

function RemoveProductProgress(divUpdateId)
{
   document.getElementById(divUpdateId).style.display = "none";
   document.getElementById(divUpdateId).innerHTML = "";
}

function UpdateCountOnClientClick(buttonClientId,textBoxClientId,msgNaN,msgNSZ)
{
   var textBoxProductCount=document.getElementById(textBoxClientId);
   var countStr=textBoxProductCount.value;
   //TODO: if value is in farsi then convert numbers from farsi to arabic.
    difference=1584;
   var transformedCountStr="";
   for(i=0;i<countStr.length;i++)
   {
      if(countStr.charCodeAt(i)>(difference))
         {
            var currentChar=countStr.charCodeAt(i);
            transformedCountStr+=String.fromCharCode(currentChar-difference);
         }
         else
         {
            transformedCountStr+=countStr.charAt(i);
         }
   }
   if(isNaN(Number(transformedCountStr))||(transformedCountStr!=parseInt(transformedCountStr)))
   {
      alert(msgNaN);
      return false;
   }
   else
   {   
      var countInt=parseInt(transformedCountStr);
      
      if(countInt<0)
      {
         alert(msgNSZ);
         return false;
      }
      else
      {
         return true;
      }
   }
}
//Shoping Cart END
