﻿// JScript File
function AddToShoppingCart_GiftCards(BrokerID, SessionGUID, decAmount, strTo, strFrom, strComments)
{
    try
    {
        if (validateCurrency("txtAmount","divGCVAmount"))
        {
            decAmount = trim(decAmount); 
            strTo = trim(strTo).replace("'", ""); 
            strFrom = trim(strFrom).replace("'", "");
            strComments = trim(strComments).replace("'", "");
            
            var strParams = "BrokerID="+BrokerID+"&SessionGUID="+SessionGUID+"&decAmount="+decAmount+"&strTo="+strTo+"&strFrom="+strFrom+"&strComments="+strComments;
            var strServiceURL =  "http://"+location.hostname+"/ws/tcws.asmx/AddGiftCardToCart";
            ajaxPost(strParams,strServiceURL,"Redirect_Update_Or_Error_GiftCards");
        }
    }
   catch(ex)
   {
    var strParams = "strErrorMsg="+ex+"&strSubject=JS_Exception_addToShoppingCart_GiftCards&bIsHtmlMsg=true";  
    ajaxPost(strParams,location.protocol+"//"+location.hostname+"/ws/tcws.asmx/LogError", null);
   }            
}
function Redirect_Update_Or_Error_GiftCards(xml)
{
    try
    {
        var update = false;
        if(document.all)
        {
            if (xml.indexOf("true") != -1)
            {
                update = true;
            }
        }
        else
        {
            if (xml.indexOf("true") != -1)
            {
                update = true;
            }    
        }
        
        if (update == true)
        {
            if(window.location.href==document.referrer)
            {
                window.location= "/Checkout.html";
            }
            else
            {
                returnObj('divGiftCardUpdate').obj.innerHTML = "Gift Card Added to Cart!"; 
                returnObj('divGiftCardUpdate').style.display = "";
                openCart();
            }
        }
        if (update == false)
        {
            returnObj('divGiftCardUpdate').style = "color:red;";
            returnObj('divGiftCardUpdate').obj.innerHTML = "The gift card could not be added to your shopping cart!<br/>Please verify the amount and quantity and try again, or call us at 1-800-521-9616.<br />"; 
        }
    }
   catch(ex)
   {
    var strParams = "strErrorMsg="+ex+"&strSubject=JS_Exception_RedirectOrError&bIsHtmlMsg=true";  
    ajaxPost(strParams,location.protocol+"//"+location.hostname+"/ws/tcws.asmx/LogError", null);
   }            
}
function CheckBalance_GiftCards(BrokerID, SessionGUID, GCNumber)
{
    try
    {
        var strParams = "BrokerID="+BrokerID+"&SessionID="+SessionGUID+"&ShippingType=0&CustomerID=0&DiscountCode=0&GiftCardNum="+GCNumber+"&Transform=false&TransformType=0&Apply=false"; 
        var strServiceURL =  "http://"+location.hostname+"/ws/tcws.asmx/GetGiftCardBalance";
        ajaxPost(strParams,strServiceURL,"RenderGCBalance");
    }
   catch(ex)
   {
    var strParams = "strErrorMsg="+ex+"&strSubject=JS_Exception_CheckBalanceGiftCards&bIsHtmlMsg=true";  
    ajaxPost(strParams,location.protocol+"//"+location.hostname+"/ws/tcws.asmx/LogError", null);
   }            
}

function getBalanceFromXML(xml)
{
    try
    {    
        if (xml.indexOf("<Balance>") != -1)
        {
            txt=xml.substring(xml.indexOf("<Balance>") + 9); 
            txt = txt.replace("</Balance>", "");
            txt = trim(txt);
            txt = formatAsMoney(txt); 

            returnObj('txtGCBalance').style.display="";
            returnObj('txtGCBalance').obj.innerHTML = "Gift Card Balance: <span style=\"color:green\">$" + txt + "</span>";
        }
        else
        {
            returnObj('txtGCBalance').style.display="";
            returnObj('txtGCBalance').style = "color:red;";
            returnObj('txtGCBalance').obj.innerHTML = "<span style=\"color:red\">There was a problem with this gift card! <br />Verify the card number and try again or call 1-800-521-9616</span>";
        }
    }
   catch(ex)
   {
    var strParams = "strErrorMsg="+ex+"&strSubject=JS_Exception_getBalanceFromXML&bIsHtmlMsg=true";  
    ajaxPost(strParams,location.protocol+"//"+location.hostname+"/ws/tcws.asmx/LogError", null);
   }            
}
function RenderGCBalance(xml)
{
    getBalanceFromXML(xml); 
}
function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}
