﻿// JScript File

function DeleteCartItem(invID,Element1Id,Element2Id)
{
    if(confirm("Are you sure you want to remove this item?"))
    {
        var res = PurpleParrot.Storefront.WebUI.Cart_WebUserControls_ShoppingCart.DeleteCartItem(invID, Element1Id, Element2Id);
        if(res.value[0]!="")
        {
            alert(res.value[0]);
        }
        else
        {
            if(res.value[1]>0)
            {
                if(document.getElementById("ctl00_lblCartCount")!=null)
                {
                    document.getElementById("ctl00_lblCartCount").innerHTML="("+res.value[1]+")";
                }
                DisplayCart();
               
            }
            else
            {
                
                if(res.value[2]=="checkout")
                {
                    location.href="../ShoppingCart.aspx";
                }
                else
                {
                    HideCart();
                }
               
            }
            
        }
    }
    return false;
}

function DisplayCart()
{
    var cartList=PurpleParrot.Storefront.WebUI.Cart_WebUserControls_ShoppingCart.GetShoppingCart();
    document.getElementById("ctl00_Main_wucShoppingCartList_cart").style.display="block";
    document.getElementById("ctl00_Main_wucShoppingCartList_cart").innerHTML=cartList.value[0];
    document.getElementById("ctl00_Main_wucShoppingCartList_totalsLeft").style.display="block";
    document.getElementById("ctl00_Main_wucShoppingCartList_lblSubTotal").innerHTML=cartList.value[1];
    document.getElementById("ctl00_Main_wucShoppingCartList_totalsRight").style.display="block";
    document.getElementById("ctl00_Main_wucShoppingCartList_lblHandlingTotal").innerHTML=cartList.value[2];
    //document.getElementById("ctl00_Main_wucShoppingCartList_lblSalesTax").innerHTML=cartList.value[3];
    document.getElementById("ctl00_Main_wucShoppingCartList_lblTotalCost").innerHTML=cartList.value[3];
    document.getElementById("ctl00_Main_wucShoppingCartList_lblItems").style.visibility="hidden";
    document.getElementById("ctl00_Main_wucShoppingCartList_btnTopUpdateCart").style.visibility="visible";
    document.getElementById("ctl00_Main_wucShoppingCartList_btnTopEmptyCart").style.visibility="visible";
    document.getElementById("ctl00_Main_wucShoppingCartList_btnUpdateCart").style.visibility="visible";
    document.getElementById("ctl00_Main_wucShoppingCartList_btnEmptyCart").style.visibility="visible";
    document.getElementById("ctl00_Main_wucShoppingCartList_btnCheckout").style.visibility="visible";
    document.getElementById("ctl00_Main_wucShoppingCartList_btnContinueShopping").value="Continue Shopping";
    if(document.getElementById("ctl00_Main_wucShoppingCartList_ExactOrderCost")!=null)
    {
      document.getElementById("ctl00_Main_wucShoppingCartList_ExactOrderCost").style.display="block";
      var stateCode="";
      var zipCode="";
      if(document.getElementById("ctl00_Main_wucShoppingCartList_drpState")!=null)
      {
        stateCode=document.getElementById("ctl00_Main_wucShoppingCartList_drpState").value;
        zipCode=trim(document.getElementById("ctl00_Main_wucShoppingCartList_txtZipCode5").value);
     
        if(stateCode!="NS" && zipCode!="")
        {
           GetSalesTaxAndShipping(stateCode,zipCode); 
        }
        else
        {
            ClearSalesTaxAndShipping();
        }
      
      }
           
    }
    
    if(document.getElementById("ctl00_Main_wucShippingAddress_drpState")!=null)
    {
       var stateCode="";
       var zipCode="";
       stateCode=document.getElementById("ctl00_Main_wucShippingAddress_drpState").value;
       zipCode=trim(document.getElementById("ctl00_Main_wucShippingAddress_txtZipCode5").value);
       if(stateCode!="NS" && zipCode!="")
       {
           GetSalesTaxAndShipping(stateCode,zipCode); 
       }
       else
       {
            ClearSalesTaxAndShipping();
       }
       
    }
}

function HideCart()
{
    document.getElementById("ctl00_Main_wucShoppingCartList_cart").style.display="none";
    document.getElementById("ctl00_Main_wucShoppingCartList_totalsLeft").style.display="none";
    document.getElementById("ctl00_Main_wucShoppingCartList_totalsRight").style.display="none";
    document.getElementById("ctl00_Main_wucShoppingCartList_lblItems").style.visibility="visible";
    document.getElementById("ctl00_Main_wucShoppingCartList_lblItems").innerHTML="You have 0 item(s) in your shopping cart."
    document.getElementById("ctl00_Main_wucShoppingCartList_btnTopUpdateCart").style.visibility="hidden";
    document.getElementById("ctl00_Main_wucShoppingCartList_btnTopEmptyCart").style.visibility="hidden";
    document.getElementById("ctl00_Main_wucShoppingCartList_btnUpdateCart").style.visibility="hidden";
    document.getElementById("ctl00_Main_wucShoppingCartList_btnEmptyCart").style.visibility="hidden";
    document.getElementById("ctl00_Main_wucShoppingCartList_btnCheckout").style.visibility="hidden";
    document.getElementById("ctl00_Main_wucShoppingCartList_btnContinueShopping").value="Back to Product Catalog";
    if(document.getElementById("ctl00_Main_wucShoppingCartList_ExactOrderCost")!=null)
    {
        document.getElementById("ctl00_Main_wucShoppingCartList_ExactOrderCost").style.display="none";
    }
}

function EmptyCart()
{
    if(confirm("Are you sure you want to empty your cart?"))
    {
        var res=PurpleParrot.Storefront.WebUI.Cart_WebUserControls_ShoppingCart.EmptyCart();
        if(res.value[0]=="")
        {
            if(res.value[1]=="checkout")
            {
                location.href="../ShoppingCart.aspx";
            }
            else
            {
                HideCart();
            }
        }
        else
        {
            alert(res.value);
        }
       
    }
     return false;
}

function UpdateCartItems()
{
    var i="01";
    var j=1;
    var err=false;
    var prefix="";
    if(document.getElementById("ctl00_Main_wucShoppingCartList_rptShoppingCart_ctl"+i+"_txtQuantity")!=null)
    {
        prefix="ctl00_Main_wucShoppingCartList_";
    }   
    else
    {
        prefix="";
    }
    
    while(document.getElementById(prefix+"rptShoppingCart_ctl"+i+"_txtQuantity")!=null)
    {
        var quantity=document.getElementById(prefix+"rptShoppingCart_ctl"+i+"_txtQuantity").value;
        var invID=document.getElementById(prefix+"rptShoppingCart_ctl"+i+"_invId").value;
        var element1Id = document.getElementById(prefix + "rptShoppingCart_ctl" + i + "_Element1Id").value;
        var element2Id = document.getElementById(prefix + "rptShoppingCart_ctl" + i + "_Element2Id").value;
        var userInput = document.getElementById(prefix + "rptShoppingCart_ctl" + i + "_txtUserInput").value;
        if(CheckIfNumber(quantity))
        {
            if(quantity<=0)
            {
                var res=PurpleParrot.Storefront.WebUI.Cart_WebUserControls_ShoppingCart.DeleteCartItem(invID,element1Id,element2Id);
                if(res.value[0]!="")
                {
                   alert(res.value[0]);
                }
            }
            else
            {
                var res2=PurpleParrot.Storefront.WebUI.Cart_WebUserControls_ShoppingCart.UpdateQuantity(invID,quantity,element1Id,element2Id,userInput);
                if(res2.value[0]!="")
                {
                   alert(res2.value[0]);
                   if(confirm("Do you wish to buy "+res2.value[1]+" "+res2.value[2]+"(s)?"))
                    {
                        document.getElementById(prefix+"rptShoppingCart_ctl"+i+"_txtQuantity").value=res2.value[1];
                        var res3 = PurpleParrot.Storefront.WebUI.Cart_WebUserControls_ShoppingCart.UpdateQuantity(invID, res2.value[1], element1Id, element2Id,userInput);
                        if(res3.value[0]!="")
                        {
                            alert(res3.value[0]);
                        }
                    }
                }
            }
                   
           
          }
          else
          {
            err=true;
            alert("Please enter a number in the quantity field");
            document.getElementById(prefix+"rptShoppingCart_ctl"+i+"_txtQuantity").style.backgroundColor = "Yellow";           
          }
          
            j=j+1;
            if(j<9)
            {   
                i="0"+j;            
            }
            else
            {
                i=j;
            }
    }
    
    if(!err)
    {
        var cartItemsCount=PurpleParrot.Storefront.WebUI.Cart_WebUserControls_ShoppingCart.GetCartCount();
        if(document.getElementById("ctl00_lblCartCount")!=null)
        {
            document.getElementById("ctl00_lblCartCount").innerHTML="("+cartItemsCount.value[0]+")";
        }
        if(cartItemsCount.value[0]<=0)
        {
           if(cartItemsCount.value[1]=="checkout")
            {
                location.href="../ShoppingCart.aspx";
            }
            else
            {
                HideCart();
            }
        }
        else
        {
            DisplayCart();
        }
    }
    return false;
}


function GetCostInformation()
{
    var valid=true;
    var stateCode="";
    var zipCode="";
    if(document.getElementById("ctl00_Main_wucShoppingCartList_drpState")!=null)
    {
        stateCode=document.getElementById("ctl00_Main_wucShoppingCartList_drpState").value;
        if(stateCode=="NS")
        {
            valid=false;
        }
        else
        {
            if(document.getElementById("ctl00_Main_wucShoppingCartList_txtZipCode5")!=null)
            {
                zipCode=trim(document.getElementById("ctl00_Main_wucShoppingCartList_txtZipCode5").value);
                if(zipCode=="")
                {
                    valid=false;
                }
            }
        }
    }
    
    if(valid)
    {
        GetSalesTaxAndShipping(stateCode,zipCode);
    }
    else
    {
        ClearSalesTaxAndShipping();
        alert("Please enter the state and zip code of the shipping address to get the exact order cost.");
    }
    return false;
}

function GetSalesTaxAndShipping(stateCode,zipCode)
{
    var stateTax=PurpleParrot.Storefront.WebUI.Cart_WebUserControls_ShoppingCart.GetTaxTotal(stateCode);
    document.getElementById("ctl00_Main_wucShoppingCartList_lblSalesTax").innerHTML=stateTax.value;
    if(zipCode.length!=5)
    {
        alert("To calculate shipping cost please enter the 5 digit zip code.");
    }
    else
    {
        var shippingCost=PurpleParrot.Storefront.WebUI.Cart_WebUserControls_ShoppingCart.GetShippingTotal(zipCode);
        document.getElementById("ctl00_Main_wucShoppingCartList_lblHandlingTotal").innerHTML=shippingCost.value;
    }
    var orderTotal=PurpleParrot.Storefront.WebUI.Cart_WebUserControls_ShoppingCart.GetOrderTotal();
    document.getElementById("ctl00_Main_wucShoppingCartList_lblTotalCost").innerHTML=orderTotal.value;
}

function GetExactCostInformation()
{
    var valid=true;
    var stateCode="";
    var zipCode="";
    if(document.getElementById("ctl00_Main_wucShippingAddress_drpState")!=null)
    {
        stateCode=document.getElementById("ctl00_Main_wucShippingAddress_drpState").value;
        if(stateCode=="NS")
        {
            valid=false;
        }
        else
        {
            if(document.getElementById("ctl00_Main_wucShippingAddress_txtZipCode5")!=null)
            {
                zipCode=trim(document.getElementById("ctl00_Main_wucShippingAddress_txtZipCode5").value);
                if(zipCode=="")
                {
                    valid=false;
                }
            }
        }
    }
    
    if(valid)
    {
        GetSalesTaxAndShipping(stateCode,zipCode);
    }
    else
    {
        ClearSalesTaxAndShipping();
        alert("Please enter the state and zip code of the shipping address to get the exact order cost.");
    }
    return false;
}


function ClearSalesTaxAndShipping()
{
    var stateTax=PurpleParrot.Storefront.WebUI.Cart_WebUserControls_ShoppingCart.SetTaxTotal();
    document.getElementById("ctl00_Main_wucShoppingCartList_lblSalesTax").innerHTML=stateTax.value;
    var shippingCost=PurpleParrot.Storefront.WebUI.Cart_WebUserControls_ShoppingCart.SetShippingTotal();
    document.getElementById("ctl00_Main_wucShoppingCartList_lblHandlingTotal").innerHTML=shippingCost.value;
    var orderTotal=PurpleParrot.Storefront.WebUI.Cart_WebUserControls_ShoppingCart.GetOrderTotal();
    document.getElementById("ctl00_Main_wucShoppingCartList_lblTotalCost").innerHTML=orderTotal.value;
}

function UpdateItemCount() {
    var count = PurpleParrot.Storefront.WebUI.Cart_WebUserControls_ShoppingCart.GetItemCount();
    document.getElementById("ctl00_storeShoppingCart_lblCartCountSt").innerHTML = "(" + count.value + ")";
    return true;
}