﻿//js To calculate the Amout for buy + the delevery amount

var TotalShipment = 0;
var TotalToPay = 0;

function getValues(vShop)
{
        //var xPath
        //if(vShop!="" && vShop!=null)
        //{
        //  xPath = "https://www." + vShop + "/emall/Order.asp";
        //}
        //else
        //{

        if(vShop != 1122)
        {
          xPath = "https://www.ecomtrade.co.il/eMall/Order.asp";
        }
        else
        {
          xPath = "https://www.traklin.co.il/Order.asp";
          //xPath = "https://www.ecomtrade.co.il/eMall/Order.asp";
        }

        try
        {
        //var x = document.getElementById("companyClubID").value;
        //if(x!=null && x!='' && x!='undefiend')
        //{
        // xPath = "https://www.ecomtrade.co.il/emall/Order.asp?club=" + x ;
        //}
        }catch(e){};
        //}
             
	document.all['shopping_cart_form'].action = xPath;
	updateCart(2);
	document.all['shopping_cart_form'].submit()
}
function getValues_Test()
{
	document.all['shopping_cart_form'].action = 'https://www.ecomtrade.co.il/emall/OrderTest.asp';
	updateCart(2);
	document.all['shopping_cart_form'].submit();
}
function getShopOrder(sc)
{
        try{
            document.all['TotalPrice'].value =  TotalToPay;
        }catch(e){};
	document.all['shopping_cart_form'].action = 'https://www.ecomtrade.co.il/emall/shopOrder.asp?sc='+sc;
	updateCart(2);
	document.all['shopping_cart_form'].submit()
}
function getShopOrderNew(sc)
{
        try{
            document.all['TotalPrice'].value =  TotalToPay;
        }catch(e){};
	//document.all['shopping_cart_form'].action = 'https://www.ecomtrade.co.il/emall/shopOrder.asp?sc='+sc;
	updateCart(2);
	window.open('https://www.ecomtrade.co.il/emall/shopOrder.asp?sc='+sc);
}
function ChangePrice(vObj,vID)
{
  var i,z
  for(i=0; i<vObj.options.length ;i++)
  {
    if(vObj.options(i).value == vObj.value)
    {
        z=vObj.options(i).text.split("-")
        document.getElementById("ID4_"+ vID).innerHTML = "&#8362; " + formatCurrency(z[z.length-1]);
        document.getElementById("price_"+ vID).value = z[z.length-1];
        try
        {
	if(z.length==3)
	         document.getElementById("tTotal").innerHTML = "&#8362; " +  formatCurrency(z[z.length-1]) + ": סה\"כ לתשלום"	
	else
	        document.getElementById("tTotal").innerHTML = " סה\"כ כללי להזמנה :"   + formatCurrency(z[z.length-1]) + " &#8362;" + "."

        }catch(e){};
    }
  }
}
function GetTatal()
{
  
   return (formatCurrency(TotalToPay+TotalShipment));
}
function GetTatalTax()
{
   return (formatCurrency((TotalToPay*1.155)+TotalShipment));
}
function CalcTotalShipment()
{       
	var i = 0;
	var otbl = document.getElementById("resTbl").rows;
	// ispaimary == 1-primary // 0-secondary
	var shipmentIsPrimary = 0;  // collect primary amount
	var shipmentNonPrimary = 0; // collect secondary amount
	var ExistPrimaryItem = 0;
	var productisprimary = "" ;
	var productQty = 0 ;
	var productPrice = 0 ;
	var shipmentPrice = 0 ;
	var ID=0;
	for(i; i<otbl.length; i++)
	{
              ID=0;

		for(var j=0 ;j<otbl[i].cells.length;j++)
		{
                  if(otbl[i].cells[j].id.indexOf("ID0")>-1)
                  {
                     try
                     {
                        if(document.all)
                        {
                           shipmentPrice =  otbl[i].cells[j].innerText;
                        }
                        else
                        {
                           shipmentPrice =  otbl[i].cells[j].textContent;
                        }

                        if(document.all)
                        {
                            productisprimary = otbl[i].cells[j].isprimary;
                        }else
                        {
                          productisprimary = otbl[i].cells[j].isprimary.getAttribute('isprimary');
                        }

                     }
                     catch(e){}
                  }
                  if(otbl[i].cells[j].id.indexOf("ID4")>-1)
                  {
                      if(document.all)
                        {
                            productPrice =  otbl[i].cells[j].innerText;
                        }
                        else
                        {
                           productPrice =  otbl[i].cells[j].textContent;
                        }


                  }
                  if(otbl[i].cells[j].id.indexOf("ID5")>-1)
                  {
                    ID =  otbl[i].cells[j].id.substr(4);

                    //productQty = document.getElementById("Inupt" + otbl[i].cells[j].id).value;
                    try
                    {
                      var xSplit =  otbl[i].cells[j].innerHTML.split(" ")
                      var l
                      for(l=0 ; l<xSplit.length ;l++)
                      {
                        if(xSplit[l].split("=")[0]=="value")
                        {
                            productQty = xSplit[l].split("=")[1].replace(/"/g, "");
                        }
                      }
                    }
                    catch(e)
                    {
                    }         
                  }

                  if(shipmentPrice==0)
                  {
                    var pID = document.getElementById("prod_id").value;
                    try
                     {
                       shipmentPrice =  document.getElementById("ID0_"+pID).value;
                       
                       if(document.all)
                       {

                           productisprimary = document.getElementById("ID0_"+pID).isprimary;
                       }
                       else
                       {

                          productisprimary = document.getElementById("ID0_"+pID).getAttribute('isprimary');
                          //alert(productisprimary);
                       }

                     }
                     catch(e){} ;
                  }
                }

           if(ID>0)
             {   

		if(productisprimary=="1")
		{
			var sPrice = shipmentPrice.split(" ");
			var S_Price;
			
			//alert(sPrice);
			if(sPrice.length==2)
			{
				S_Price=sPrice[0]
			}
			else
			{
				S_Price=sPrice[0]
			}

			shipmentIsPrimary += parseFloat(S_Price) * parseFloat(productQty) ;
			//alert(shipmentIsPrimary)
			//shipmentIsPrimary += parseFloat(otbl[i].cells[1].innerText) * parseFloat(productQty) ;
			ExistPrimaryItem = 1 ;
		}
		else
		{
			if(shipmentNonPrimary==0)
			{
				//if(otbl[i].cells[1].innerText!="")
				if(shipmentPrice!="")
				{
					//shipmentNonPrimary = parseFloat(shipmentPrice) * parseFloat(productQty);
					shipmentNonPrimary = parseFloat(shipmentPrice) ;

				}
			}
			else
			{
				//if(otbl[i].cells[1].innerText!="")
				if(shipmentPrice!="")
				{
					var nextShipment = parseFloat(shipmentPrice);
					if(shipmentNonPrimary < nextShipment)
					{
						shipmentNonPrimary = parseFloat(shipmentPrice);
					}
				}
			}
		}
	   }
	}
	if(ExistPrimaryItem==1)
	{
		TotalShipment = shipmentIsPrimary;
	}
	else
	{
		TotalShipment = shipmentNonPrimary;
	}



	if(String(TotalShipment).indexOf('.')==-1)
	{

           var TShipment = String(TotalShipment)+ ".00";

      	   return(TShipment);
        }
    	else
    	{
           return(TotalShipment);
        }
}
function CalcTotalPrice(vNoC)
{  
	var TotalPrice=0;
	var i = 0;
	var j = 0;
	var X;
	var SelValue = "";
	var Sel = "";
	var otbl = document.getElementById("resTbl").rows;
	var productQty = 0 ;
	var productDiscount = 0 ;
	var productPrice = 0 ;
        var ID=0;

	for(i; i<otbl.length ; i++)
	{
                ID=0;
                for(var j=0 ;j<otbl[i].cells.length;j++)
        	    {
                  if(otbl[i].cells[j].id.indexOf("ID4")>-1)
                  {
                      if(document.all)
                        {
                             productPrice =  otbl[i].cells[j].innerText;
                        }
                        else
                        {
                           productPrice =  otbl[i].cells[j].textContent;
                        }


                  }
                  if(otbl[i].cells[j].id.indexOf("ID5")>-1)
                  {
                    ID =  otbl[i].cells[j].id.substr(4);
                    //productQty = document.getElementById("Inupt" + otbl[i].cells[j].id).value;
                    try
                    {
                      var xSplit =  otbl[i].cells[j].innerHTML.split(" ");

                      var l;

                      for(l=0 ; l<xSplit.length;l++)
                      {    

                        if(xSplit[l].split("=")[0]=="value")
                        {
                            productQty = xSplit[l].split("=")[1].replace(/"/g, "");

                        }
                      }
                    }
                    catch(e)
                    {
                    }                    
                  }
                }
		//productQty = document.getElementById("Inupt" + otbl[i].cells[5].id).value;

          if(ID>0)
            {
		SelValue = "";
		X = "";
		X = "Sel" + otbl[i].cells[2].id
		if(document.getElementById(X)!=null)
		{
			j=0;
			Sel = document.getElementById(X).options;
			for(j ; j<Sel.length ; j++)
			{
				if(Sel[j].selected==true)
				{
					X = Sel[j].title.split(' ')
					SelValue = trim(X[0]);
					break;
				}
			}
		}
		if(SelValue!="")
		{
			var SplitCell = productPrice.split(' '); //otbl[i].cells[4].innerText.split(' ');
			if(isNum(SplitCell[0]))
			{
                                if(document.all)
                                {
                                     otbl[i].cells[4].innerText = SelValue + " " + SplitCell[1];
                                }
                                else
                                {
                                   otbl[i].cells[4].textContent = SelValue + " " + SplitCell[1];
                                }


				SplitCell[0] = SelValue;
			}
			else
			{
                                
                                if(document.all)
                                {
                                    otbl[i].cells[4].innerText = SelValue + " " + SplitCell[0];
                                }
                                else
                                {
                                   otbl[i].cells[4].textContent = SelValue + " " + SplitCell[0];
                                }


				SplitCell[1] = SelValue;
			}

			X = otbl[i].cells[2].id;
			X = X.split('_');
			X = "price_" + X[1];
			
			 if(document.all)
                                {
                                    	document.getElementById(X).value = otbl[i].cells[4].innerText;
                                }
                                else
                                {
                                  	document.getElementById(X).value = otbl[i].cells[4].textContent;
                                }
                                


		}
		else
			var SplitCell = productPrice.split(' '); //otbl[i].cells[4].innerText.split(' ')
                       
		SplitCell[0] = SplitCell[0].replace(/,/ig, "");





		if(isNum(SplitCell[0]))
		{
                    //alert(parseFloat(productQty));

                    if(isNaN(parseFloat(productQty)))
                    {
                       if(isNaN(TotalPrice))
                       {
                         TotalPrice = 0;
                       }
                       TotalPrice +=  parseFloat(SplitCell[0]);

                    }
                    else
                    {
                       if(isNaN(TotalPrice))
                       {
                         TotalPrice = 0;
                       }
                       TotalPrice +=  parseFloat(SplitCell[0]) * parseFloat(productQty) ;
                    }


                }
		else if(isNum(SplitCell[1]))
			TotalPrice +=  parseFloat(SplitCell[1]) * parseFloat(productQty) ;
                  


                  


		if(SplitCell.length>2)
		{
			for(j=2; j<SplitCell.length ; j++)
			{
				if(isNum(trim(SplitCell[j])))
				{
					productDiscount = parseFloat(SplitCell[j]) * parseFloat(productQty) ;
					break;
				}
			}
		}
		 if(isNaN(productDiscount))
                       {
                         productDiscount = 0;
                       }

		TotalPrice =  TotalPrice - productDiscount;
	   }
	}
         
	TotalToPay = TotalPrice;
	if(document.getElementById("TotalPrice")!=null)
	{
	    document.getElementById("TotalPrice").value = TotalToPay;
	}

	var tPrice = String(TotalToPay)
	if(tPrice.indexOf('.')==-1)
	{
		tPrice = tPrice + ".00";
		//return(tPrice);
	}
	else
	{
          //return(Math.round(TotalToPay*100)/100);
		//return(TotalToPay);
        } 

        return (formatCurrency(TotalToPay));
}
function isNum(sStr)
{
  if(!sStr) return false;
  for(var i=0; i<sStr.length-1; i++){
    var ch=sStr.charAt(i);
    if ("0123456789.-".indexOf(ch) ==-1) return false;
  }
  return true;
}
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}
function ContinueShopping()
{
   var i = history.length
   history.go(-1);
}
function formatCurrency(num)
{

  if(isNaN(num))
  num = "0";
  num = Math.floor(num*100+0.50000000001);
  var cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
  cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
  num = num.substring(0,num.length-(4*i+3))+','+
  num.substring(num.length-(4*i+3));
  return num+ '.' + cents;
  
}
