﻿// JScript File
 
var prc = document.getElementById("ctl00_lblCartTotal");  
var newTypeInfoSet;
var priceUpdated = new Array();
var priceIndex = 0;
var priceUpdatedPostback = new Array();
var priceIndexPostback = 0;
var maxcpriceNum = 20;
var maxgcpriceNum = 200;
var maxRowNum = 10; 
var currencydisplay = false;  
 function setpriceUpdatedDdl()
{
     var tmp = document.getElementById("ddlistcomp");
     if (tmp) {
         for (var i = 0; i < tmp.length; i++) {
             tmp.options[i].innerHTML = findPricePart(tmp.options[i].text);

         }
     }
}
function setpriceUpdated()
{
    addpriceUpdated("ewebprice");
    addpriceUpdated("retailprice");
    addpriceUpdated("percsaving");
    addpriceUpdated("webprice");
    addpriceUpdated("surchrg");
    addpriceUpdated("exclvatprice");
    addpriceUpdated("inclvatprice");
    addpriceUpdated("exclvatsurchrg");
    addpriceUpdated("inclvatsurchrg");
    addpriceUpdated("rebateprice");
    addpriceUpdated("priceoverride");
    addpriceUpdated("pricematch");
    addpriceUpdated("clubprice");
    addpriceUpdated("clubpercsaving");
    addpriceUpdated("addph");
    addpriceUpdatedSet("multipricing");
    addpriceUpdatedSet("ekitprice");
    addpriceUpdatedSet("ekitpricesurchrg");
    addpriceUpdatedSet("ekitpriceexclvat");
    addpriceUpdatedSet("ekitpriceinclvat");
    addpriceUpdatedSet("ekitpriceexclvatsurchrg");
    addpriceUpdatedSet("ekitpriceinclvatsurchrg");
    
    
    addpriceUpdatedSet("cretailprice"); 
    addpriceUpdatedSet("cpercsaving");
    addpriceUpdatedSet("cwebprice");
    addpriceUpdatedSet("csurchrg");
    addpriceUpdatedSet("cexclvatprice");
    addpriceUpdatedSet("cinclvatprice");
    addpriceUpdatedSet("cexclvatsurchrg");
    addpriceUpdatedSet("cinclvatsurchrg");
    addpriceUpdatedSet("crebateprice");
    addpriceUpdatedSet("cpriceoverride");
    addpriceUpdatedSet("cpricematch");
    addpriceUpdatedSet("cclubprice");
    addpriceUpdatedSet("cclubpercsaving");
    addpriceUpdatedSet("caddph");
    addpriceUpdatedSet1("cmultipricing");
    
    addgridpriceUpdatedSet("gcretailprice"); 
    addgridpriceUpdatedSet("gcpercsaving");
    addgridpriceUpdatedSet("gcwebprice");
    addgridpriceUpdatedSet("gcsurchrg");
    addgridpriceUpdatedSet("gcexclvatprice");
    addgridpriceUpdatedSet("gcinclvatprice");
    addgridpriceUpdatedSet("gcexclvatsurchrg");
    addgridpriceUpdatedSet("gcinclvatsurchrg");
    addgridpriceUpdatedSet("gcrebateprice");
    addgridpriceUpdatedSet("gcpriceoverride");
    addgridpriceUpdatedSet("gcpricematch");
    addgridpriceUpdatedSet("gcclubprice");
    addgridpriceUpdatedSet("gcclubpercsaving");
    addgridpriceUpdatedSet("gcaddph");
    addgridpriceUpdatedSet1("gcmultipricing");
    
    addpriceUpdatedSet1("gwebprice");
    addpriceUpdatedSet1("gsurchrg");
    addpriceUpdatedSet1("ginclvatprice");
    addpriceUpdatedSet1("gexclvatprice");
    addpriceUpdatedSet1("ginclvatsurchrg");
    addpriceUpdatedSet1("gexclvatsurchrg");
    addpriceUpdatedSet1("grebateprice");
    addpriceUpdatedSet1("gclubprice");
    
    addpriceUpdatedSet1("rwebprice");
    addpriceUpdatedSet1("rsurchrg");
    addpriceUpdatedSet1("rwebpricemax");
    addpriceUpdatedSet1("rexclvatprice");
    addpriceUpdatedSet1("rinclvatprice");
    addpriceUpdatedSet1("rexclvatsurchrg");
    addpriceUpdatedSet1("rinclvatsurchrg");
    addpriceUpdatedSet1("rrebateprice");
    addpriceUpdatedSet1("rrebatepricemax");
    addpriceUpdatedSet1("rclubprice");
    addpriceUpdatedSet1("rpwebprice");
    addpriceUpdatedSet1("rpsurchrg");
    addpriceUpdatedSet1("rpwebpricemax");
    addpriceUpdatedSet1("rpexclvatprice");
    addpriceUpdatedSet1("rpinclvatprice");
    addpriceUpdatedSet1("rpexclvatsurchrg");
    addpriceUpdatedSet1("rpinclvatsurchrg");
    addpriceUpdatedSet1("rprebateprice");
    addpriceUpdatedSet1("rprebatepricemax");
    addpriceUpdatedSet1("rpclubprice");
    
    addpriceUpdatedSet1("twebprice");
    addpriceUpdatedSet1("tsurchrg");
    addpriceUpdatedSet1("twebpricemax");
    addpriceUpdatedSet1("texclvatprice");
    addpriceUpdatedSet1("tinclvatprice");
    addpriceUpdatedSet1("texclvatsurchrg");
    addpriceUpdatedSet1("tinclvatsurchrg");
    addpriceUpdatedSet1("trebateprice");
    addpriceUpdatedSet1("trebatepricemax");
    addpriceUpdatedSet1("tclubprice");
    addpriceUpdatedSet1("tpwebprice");
    addpriceUpdatedSet1("tpsurchrg");
    addpriceUpdatedSet1("tpwebpricemax");
    addpriceUpdatedSet1("tpexclvatprice");
    addpriceUpdatedSet1("tpinclvatprice");
    addpriceUpdatedSet1("tpexclvatsurchrg");
    addpriceUpdatedSet1("tpinclvatsurchrg");
    addpriceUpdatedSet1("tprebateprice");
    addpriceUpdatedSet1("tprebatepricemax");
    addpriceUpdatedSet1("tpclubprice");
    
    
}
function addpriceUpdatedSet1(priceid)
{
    var tmp;
    for(var i=1; i<maxcpriceNum; i++)
    {
        for(var j=0; j<maxRowNum; j++)
        {
            tmp = document.getElementById(priceid + j.toString() + "-" + i.toString());
            if (tmp)
            {
                priceUpdated[priceIndex] = tmp.id;
                priceIndex++;
            }
        }
    }

}
function addgridpriceUpdatedSet1(priceid)
{
    var tmp;
    for(var i=1; i<maxgcpriceNum; i++)
    {
        for(var j=0; j<maxRowNum; j++)
        {
            tmp = document.getElementById(priceid + j.toString() + "-" + i.toString());
            if (tmp)
            {
                priceUpdated[priceIndex] = tmp.id;
                priceIndex++;
            }
        }
    }

}

function addpriceUpdatedSet(priceid)
{
    var tmp;
    for(var i=1; i<maxcpriceNum; i++)
    {
        tmp = document.getElementById(priceid + i.toString());
        if (tmp)
        {
            priceUpdated[priceIndex] = priceid + i.toString();
            priceIndex++;
        }
    }

}
function addgridpriceUpdatedSet(priceid)
{
    var tmp;
    for(var i=1; i<maxgcpriceNum; i++)
    {
        tmp = document.getElementById(priceid + i.toString());
        if (tmp)
        {
            priceUpdated[priceIndex] = priceid + i.toString();
            priceIndex++;
        }
    }

}
function addpriceUpdated(priceid)
{
    var tmp = document.getElementById(priceid);
    if (tmp)
    {
        priceUpdated[priceIndex] = priceid;
        priceIndex++;
    }

}  
function findPricePart(word) {
     var tmp = word;
     var ndx = word.indexOf("$");
     if (ndx > -1) {
         tmp = word.substr(ndx, word.length - ndx);
         var str = tmp.split(/\s/g);
         tmp = getInitPrice(str[0]);
         tmp = word.replace(str[0], tmp);
     }

     return tmp;
 }
 
function getInitPriceMultipricing(price)
{
    var tmp = (((price.toLowerCase().replace("<br>", " <br>")).replace("</br>", " </br>")).replace("<br />", " <br />")).replace("<br/>", " <br/>");
    return getInitPriceSaving(tmp);
} 
function getInitPriceSaving(price) {
  var tmp = price.split(/\s/);
  var newprice= "";
  for (var i = 0; i < tmp.length; i++) {
    if (tmp[i].indexOf("$")>-1)
    {
        newprice += " " + getInitPrice(tmp[i]);
    }
    else
    {
        newprice += " " + tmp[i];
    }   
  }
  return newprice;
}

function getInitPriceByRate(price)
{
    price = price.replace(/,/g, "");
    price = (price*defaultRate/defaultTypeInfo.Rate).toFixed(2).toString();
    var  tmp = price.split(".");
    if (tmp[0].length>3)
    {
        price = tmp[0].substr(0, tmp[0].length-3) + "," + tmp[0].substr(tmp[0].length-3, 3);
        var tmp1 = "00";
        if (tmp.length > 1)
            tmp1 = tmp[1];
        price += "." + tmp1;
    }
    return price;
}
function getInitPrice(price) {
     var formatPrice;
     var negative = false;
     if (price.indexOf("$") > -1)
         price = price.replace("$", "");
         
     if (price.indexOf("-") > -1)
     {
         price = price.replace("-", "");
         negative = true;
     }
         
     price = getInitPriceByRate(price);
     var tmp = price.toString().split(".");
     var tmp1 = "00";
     if (tmp.length > 1)
         tmp1 = tmp[1];
     if (defaultTypeInfo.DecimalSeparator == "~")
         defaultTypeInfo.DecimalSeparator = ",";
     if (defaultTypeInfo.ThousandSeparator=="~")
            defaultTypeInfo.ThousandSeparator = ",";
     var tmp2;
     if (tmp[0].indexOf(",")>0)
     {
        tmp2 = tmp[0].replace(",", defaultTypeInfo.ThousandSeparator);
     }
     else
     {
        if (tmp[0].length>3)
        { 
            tmp2 = tmp[0].substr(0, tmp[0].length-3) + defaultTypeInfo.ThousandSeparator + tmp[0].substr(tmp[0].length-3);
        }
        else
            tmp2 =tmp[0];
     }
     
     //formatPrice = tmp2 + defaultTypeInfo.DecimalSeparator + tmp1;
     if(negative)
         formatPrice = '-' + tmp2 + defaultTypeInfo.DecimalSeparator + tmp1;
     else
         formatPrice = tmp2 + defaultTypeInfo.DecimalSeparator + tmp1;
     
     if (defaultTypeInfo.SymbolPrefix == true) {
         formatPrice =  defaultTypeInfo.Symbol + formatPrice;
     }
     else {
         formatPrice = formatPrice + defaultTypeInfo.Symbol;
     }
     return formatPrice;
 }
function setInitPrice()
{
    currencydisplay = true;
    setpriceUpdated();
    if (priceUpdated.length>0)
    {
    
        for(var i=0; i< priceUpdated.length; i++)
        {
            var priceid = document.getElementById(priceUpdated[i]);
         
            if (priceid.id.indexOf("saving") != -1 || priceid.id.indexOf("ekitprice") != -1 || priceid.id.indexOf("addph") != -1)
            {
                priceid.innerHTML = getInitPriceSaving(priceid.innerHTML);
            }
            else if (priceid.id.indexOf("multipricing") != -1)
            {
                priceid.innerHTML = getInitPriceMultipricing(priceid.innerHTML);
            }
            else if (priceid.id.indexOf("priceoverride") != -1) 
            {
                priceid.value = removeHTMLEntities(getInitPrice("0"));
            }
            else
            {  
                priceid.innerHTML = getInitPrice(priceid.innerHTML);
            }
        }  
    }
    setpriceUpdatedDdl();
}
     function onSuccess(result)//return culture
     { 
        if(result!="undefined")
        {          
            newTypeInfoSet = result.split("^");
            prc = document.getElementById("ctl00_lblCartTotal"); 
            if(prc) 
                prc.innerHTML = getUpdatedPrice(prc.innerHTML, result);
            prc = document.getElementById("ctl00_lblFreeShpMsg");
            if (prc)
                prc.innerHTML = getUpdatedPriceSaving(prc.innerHTML, result);
            prc = document.getElementById("ctl00_lblEasyPayTag");
            if (prc)
                prc.innerHTML = getUpdatedPriceSaving(prc.innerHTML, result); 
            for( var i=0; i<priceUpdated.length; i++)
            {
                 prc = document.getElementById(priceUpdated[i])
                 if(prc)
                 {
                     if (prc.id.indexOf("saving") != -1 || prc.id.indexOf("multipricing") != -1 || prc.id.indexOf("ekitprice") != -1 || prc.id.indexOf("addph") != -1) {
                         prc.innerHTML = getUpdatedPriceSaving(prc.innerHTML, result);
                     }
                     else if (prc.id.indexOf("priceoverride") != -1) 
                     {
                         var reg = new RegExp("[0-9]");
                         if (reg.test(prc.value))
                             prc.value = removeHTMLEntities(getUpdatedPrice(prc.value, result));
                         else
                             prc.value = removeHTMLEntities(getUpdatedPrice("0", result));
                     }
                     else if (prc.id.indexOf("lblShippingChrg") != -1) 
                     {
                         var reg = new RegExp("[0-9]");
                         if (reg.test(prc.innerHTML))
                             prc.innerHTML = getUpdatedPrice(prc.innerHTML, result);
                     }
                     
                     else
                         prc.innerHTML = getUpdatedPrice(prc.innerHTML, result);
                 }
            }
            defaultTypeInfo.Type = newTypeInfoSet[5];  
            defaultTypeInfo.Symbol = newTypeInfoSet[0];   
            defaultTypeInfo.SymbolPrefix = newTypeInfoSet[1]; 
            if (defaultTypeInfo.SymbolPrefix=="True")
               defaultTypeInfo.SymbolPrefix = true;
            else
               defaultTypeInfo.SymbolPrefix = false;        
            defaultTypeInfo.DecimalSeparator = newTypeInfoSet[2];  
            defaultTypeInfo.ThousandSeparator = newTypeInfoSet[3];  
            defaultTypeInfo.Rate = newTypeInfoSet[4];
        } 
     }
     function onError(error) {
            //$get("Status").innerHTML = error.get_message();
            alert(error.get_message());
    }
function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;    
    return true;
}

function removeHTMLEntities(inputString)
{
    var div = document.createElement('div');
    div.innerHTML = inputString;
    return div.innerHTML;
}


function getUpdatedPriceSaving(price, result) {

    var pattern = /\d+/i;
    var pattern1 = /-/i;
    var tmp = price.split(/\s/);
    var newprice = "";
    defaultTypeInfo.Symbol = removeHTMLEntities(defaultTypeInfo.Symbol);
    for (var i = 0; i < tmp.length; i++) {
        if ((tmp[i].indexOf(defaultTypeInfo.Symbol) > -1 && tmp[i - 1].search(pattern) > -1 && tmp[i - 1].search(pattern1) == -1) || (tmp[i].search(pattern) > -1 && tmp[i].search(pattern1) == -1 && tmp[i - 1].indexOf(defaultTypeInfo.Symbol) > -1)) {
            newprice += tmp[i];
        }
        else {
            newprice += " " + tmp[i];
        }   
    }
    tmp = newprice.split(/\s/);
    newprice = "";
    for (var i = 0; i < tmp.length; i++) {
        if (tmp[i].indexOf(defaultTypeInfo.Symbol) > -1) {
            newprice += " " + getUpdatedPrice(tmp[i], result);
        }
        else {
            newprice += " " + tmp[i];
        }
    }
    return newprice;
}
function getUpdatedPrice(orig, result) 
{
        var tt = defaultTypeInfo.Symbol;
        if (tt == orig && defaultTypeInfo.Type != 1)
             return "";
        var negative = false;
        
        if (orig.indexOf("-") > -1)
        {
             orig = orig.replace("-", "");
             negative = true;
        }
        
        var update = orig;
  
        var symbolLength = tt.length;
        if(tt.startsWith("&#")==true)
        {
           symbolLength = 1;
        }

        if (defaultTypeInfo.SymbolPrefix == true) {
            orig = orig.substr(symbolLength, orig.length);
        }
        else {
            orig = orig.substr(0, orig.length - symbolLength);
        }

        if (orig != "0" && orig != "") 
        {
            if (defaultTypeInfo.DecimalSeparator == "~")
                defaultTypeInfo.DecimalSeparator = ",";

            var updateset = orig.split(defaultTypeInfo.DecimalSeparator);
            if (defaultTypeInfo.ThousandSeparator == "~")
                defaultTypeInfo.ThousandSeparator = ",";

            if (updateset[0].indexOf(defaultTypeInfo.ThousandSeparator) > -1)
                updateset[0] = updateset[0].replace(defaultTypeInfo.ThousandSeparator, "");

            update = updateset[0] + "." + updateset[1];
        }
        else
            update = "0";
        
        var asFloat = defaultTypeInfo.Rate;
      
        update = (update * asFloat/newTypeInfoSet[4]).toFixed(2);
        
        updateset = update.split(".");
        if (updateset[0].length > 3)
        {
            updateset[0] = updateset[0].substr(0, updateset[0].length -3) +   newTypeInfoSet[3] + updateset[0].substr(updateset[0].length -3, 3);
        }
        
        update =  updateset[0] +  newTypeInfoSet[2] + updateset[1];
            
        if (newTypeInfoSet[1]=='True')
        {
           update = newTypeInfoSet[0] +  update;
        }
        else
        {
           update = update + newTypeInfoSet[0];
        }
        if (negative)
            update = '-' + update;
        
        return update;
    }      

function setPostBackPrice()
{
    if (!currencydisplay)
       return;

    priceUpdatedPostback = new Array();
    priceIndexPostback = 0;

    addpriceUpdatedPostback("retailprice");
    addpriceUpdatedPostback("percsaving");
    addpriceUpdatedPostback("webprice");
    addpriceUpdatedPostback("surchrg");
    addpriceUpdatedPostback("exclvatprice");
    addpriceUpdatedPostback("inclvatprice");
    addpriceUpdatedPostback("exclvatsurchrg");
    addpriceUpdatedPostback("inclvatsurchrg");
    addpriceUpdatedPostback("rebateprice");
    addpriceUpdatedPostback("priceoverride");
    addpriceUpdatedPostback("pricematch");
    addpriceUpdatedPostback("clubprice");
    addpriceUpdatedPostback("clubpercsaving");
    addpriceUpdatedPostback("addph");
    addpriceUpdatedSetPostback("multipricing");
    
    if (priceUpdatedPostback.length>0)
    {
    
        for(var i=0; i< priceUpdatedPostback.length; i++)
        {
            var priceid = document.getElementById(priceUpdatedPostback[i]);
         
            if (priceid.id.indexOf("saving") != -1 || priceid.id.indexOf("addph") != -1)
            {
                priceid.innerHTML = getInitPriceSaving(priceid.innerHTML);
            }
            else if (priceid.id.indexOf("multipricing") != -1)
            {
                priceid.innerHTML = getInitPriceMultipricing(priceid.innerHTML);
            }
            else if (priceid.id.indexOf("priceoverride") != -1) 
            {
                priceid.value = removeHTMLEntities(getInitPrice("0"));
            }
            else
            {  
                priceid.innerHTML = getInitPrice(priceid.innerHTML);
            }
        }  
    }
}

function addpriceUpdatedSetPostback(priceid)
{
    var tmp;
    for(var i=1; i<maxcpriceNum; i++)
    {
        tmp = document.getElementById(priceid + i.toString());
        if (tmp)
        {
            priceUpdatedPostback[priceIndexPostback] = priceid + i.toString();
            priceIndexPostback++;
        }
        else
        {
            break;
        }
    }

}
function addpriceUpdatedPostback(priceid)
{
    var tmp = document.getElementById(priceid);
    if (tmp)
    {
        priceUpdatedPostback[priceIndexPostback] = priceid;
        priceIndexPostback++;
    }

}  