var Product_Product = new Array();
var Product_Description = new Array();
var Product_Price = new Array();
var Product_Weight = new Array();
var Product_BoxSize = new Array();
var Product_PartNumber = new Array();
var Product_Size = new Array();
var Product_Category = new Array();
var Product_DimA = new Array();
var Product_DimB = new Array();
var Product_DimC = new Array();
var Product_DimD = new Array();
var Product_DimE = new Array();
var Product_DimF = new Array();

var SC_Product = new Array();
var SC_Qty = new Array();

var cookieName = "KNCHomeShopCookie";
var TotalLines = 0;
var msg;


/*  var SC_Description = new Array();
    var SC_Price = new Array();
    var SC_Weight = new Array();
    var SC_BoxSize = new Array();
    var SC_PartNumber = new Array();
    var SC_Size = new Array();
    var SC_Category = new Array();
*/

function SaveItemToShoppingCartCookie(theProduct, theQty , ShowMessage){
    var found = "N";
    cookieName = "KNCHomeShopCookie";
    var myarray = init_array();
    get_array(cookieName,myarray);
    var L = 0;
    for(i=1;i<=next_entry(myarray) -1;i = i + 2){
            SC_Product[L] = myarray[i];
            SC_Qty[L] = myarray[i + 1];
            if(myarray[i] == theProduct){
                if(theQty == 20){
                    SC_Qty[L] = 20;
                }
                else {
                    theQty = parseInt(theQty);
                    SC_Qty[L] = parseInt(SC_Qty[L]) + theQty;
                }
                found = "Y";
            }
            SC_Qty[L] = parseInt(SC_Qty[L]);
//            alert(SC_Qty[L]);
            L = L + 1;
    }

    var timeToKeep = 3600000;
    var expires = new Date();
    expires.setTime(expires.getTime() + timeToKeep);
    var myarray = init_array();
    for(L=0; L < SC_Product.length; L++){
        myarray[myarray.length] = SC_Product[L];
        myarray[myarray.length] = SC_Qty[L];
    }
    if(found == "N"){
        myarray[myarray.length] = theProduct;
        myarray[myarray.length] = theQty;
    }
    set_array(cookieName, myarray, expires);
    if(ShowMessage == "Y"){
     // "https://www.knchomeshop.com/test/KNCHomeShopShoppingCart.asp"
    //    alert("Item has been added to shopping cart");
    //	window.location.href = "KNCHomeShopShoppingCart.asp";
    	window.location.href = "https://www.knchomeshop.com/KNCHomeShopShoppingCart.asp";
    }
}

function SaveItemToShoppingCartCookieSaved(theProduct, theQty , ShowMessage){

//    alert("NO");
    var timeToKeep = 3600000;
    var expires = new Date();
    var myarray = init_array();
    expires.setTime(expires.getTime() + timeToKeep);
    var myarray = init_array();
    get_array(cookieName,myarray);
    myarray[myarray.length] = theProduct;
    myarray[myarray.length] = theQty;
    set_array(cookieName, myarray, expires);
    if(ShowMessage == "Y"){
        alert("Item has been added to shopping cart");
    }
}


function AddToShoppingCart(){
    SaveItemToShoppingCartCookie(Product.lstProduct.value,Product.lstQty.value , "Y");
//    dump_cookies();
}

function ShoppingCartOnBottomOfPage(){
    document.write("<table width = 800 border = 0>");
    document.write("<tr><td>");
    document.write("<Center>");
    document.write("    <select name=\"lstProduct\">");
    for(P=0; P < Product_Product.length; P++){
        document.write("<option value=\"" + Product_Product[P] + "\"><font size=4 face=\"Arial\">" + Product_Product[P] + "</font></option>");
    }
    document.write("    </select>");
    document.write("    <font size=4 face=\"Arial\">Quanity to Add to Order </font>");
    document.write("    <select name=\"lstQty\">");
    for(X=1;X < 51; X++){
        document.write("<Option value = " + X + "><font size=\"4\" face=\"Arial\">" + X + "</Option>");
    }
    document.write("    </select>");
    document.write("  <input type=\"button\" name=\"Add\" value=\"Add to Cart\" onClick='AddToShoppingCart();'>");
    
    document.write("&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#TopOfPage\">    <font size=4 face=\"Arial\" color=\"blue\">Top of Page </font></a>");
    
    document.write("</Center>");

    document.write("  </td></tr>");
    document.write("  </table>");
    
    document.write("<table width = 800 border = 0>");
    document.write("<tr><td>");
    document.write("<Center>");
    document.write("<font color=\"red\">");
    document.write("Shopping cart only works with Internet Explorer 5.5 of above");
    document.write("</font>");
    document.write("  </td></tr>");
    document.write("  </table>");
    
 }
 
function CreateItemList(){
    SC_Product.length = 0;
    SC_Qty.length = 0;
    cookieName = "KNCHomeShopCookie";
    var myarray = init_array();
    get_array(cookieName,myarray);
    var L = 0;
    for(i=1;i<=next_entry(myarray) -1;i = i + 2){
            SC_Product[L] = myarray[i];
            SC_Qty[L] = myarray[i + 1];
            L = L + 1;
    }
    TotalLines = L;
}

function ChangeItemQuantity(R){
    if(TotalLines > 1){
        Pr = Product.SCUnitPrice(R).value;
        Pr = Pr.slice(1,Pr.length);
        IPrice = parseFloat(Pr);
        IQty = Product.SCQty(R).value;
        IQty = parseInt(IQty);
        Product.SCQuantityPrice(R).value = "$" + FormatNumber(IQty * IPrice, 2, true, false);
        TPrice = 0;
        for(X=0; X < Product.SCQuantityPrice.length; X++){
            Pr = Product.SCQuantityPrice(X).value;
            Pr = Pr.slice(1,Pr.length);
            IPrice = parseFloat(Pr);
            TPrice = TPrice + IPrice;
        }
        Product.subtotal.value = "$" + FormatNumber(TPrice, 2, true, false);
    }
    if(TotalLines == 1){
        Pr = Product.SCUnitPrice.value;
        Pr = Pr.slice(1,Pr.length);
        IPrice = parseFloat(Pr);
        IQty = Product.SCQty.value;
        IQty = parseInt(IQty);
        Product.SCQuantityPrice.value = "$" + FormatNumber(IQty * IPrice, 2, true, false);
        Product.subtotal.value = Product.SCQuantityPrice.value;
    }
    ChangeTax();
}

function ResetQtys(){
    var TPrice = 0;
    if(TotalLines > 1){
        CreateItemList();
        for(X=0; X < Product.SCQuantityPrice.length; X++){
            Product.SCQty(X).value = SC_Qty[X];
            IPrice =  Product.SCUnitPrice(X).value;
            IPrice = IPrice.slice(1,IPrice.length);
            IPrice = parseFloat(IPrice);
            Product.SCQuantityPrice(X).value = "$" + FormatNumber(SC_Qty[X] * IPrice, 2, true, false);
        }
        for(X=0; X < Product.SCQuantityPrice.length; X++){
            Pr = Product.SCQuantityPrice(X).value;
            Pr = Pr.slice(1,Pr.length);
            IPrice = parseFloat(Pr);
            TPrice = TPrice + IPrice;
        }
        
    
    }
    if(TotalLines == 1){
        Product.SCQty.value = SC_Qty[0];
        IPrice =  Product.SCUnitPrice.value;
        IPrice = IPrice.slice(1,IPrice.length);
        IPrice = parseFloat(IPrice);
        Product.SCQuantityPrice.value = "$" + FormatNumber(SC_Qty[0] * IPrice, 2, true, false);
        TPrice = SC_Qty[0] * IPrice;
    }
    Product.subtotal.value = "$" + FormatNumber(TPrice, 2, true, false);

}

function RemoveItemFromCart(R){
    var msg = "\nDo you want to delete item?\n";
    if (confirm(msg)){
        cookieName = "KNCHomeShopCookie";
        
        var expires = new Date();
        var timeToKeep = 3600000;
        expires.setTime(expires.getTime() + timeToKeep);
        var myarray = init_array();
        get_array(cookieName,myarray);
    del_entry(cookieName, myarray, parseInt(R) * 2 + 1, expires);
        location.replace("KNCHomeShopShoppingCart.asp");
    }
    else{
        if(TotalLines == 1){
            Product.RemoveItem.checked = false;
        }
        else{
            Product.RemoveItem(R).checked = false;
        
        }
    }}


function SaveChangesToCart(){
    cookieName = "KNCHomeShopCookie";
    var expires = new Date();
    var timeToKeep = 3600000;
    expires.setTime(expires.getTime() + timeToKeep);
    var myarray = init_array();
    var value = '';
    if(TotalLines > 1){
        for(L=0; L < Product.SCProduct.length ; L++){
            value += Product.SCProduct(L).value + '^';
            value += Product.SCQty(L).value + '^';
        }
    }
    else{
        value += Product.SCProduct.value + '^';
        value += Product.SCQty.value + '^';
    }
    

    set_cookie(cookieName, value, expires);
}


function SaveChangesToCartOLD(){
        cookieName = "KNCHomeShopCookie";
        var expires = new Date();
        var timeToKeep = 3600000;
        expires.setTime(expires.getTime() + timeToKeep);
        del_cookie(cookieName);
        var myarray = init_array();
        for(L=0; L < Product.SCProduct.length -1; L++){
            SaveItemToShoppingCartCookie(Product.SCProduct(L).value, Product.SCQty(L).value, "N")   
        }
    
}

function ChangeTax(){
    var taxTotal;
    subtotal = Product.subtotal.value;
    subtotal = subtotal.slice(1, subtotal.length)
    subtotal = parseFloat(subtotal);
    if(Product.chkTax.checked == false){
        taxTotal = 0;
    }
    else{
        taxTotal = subtotal * .0675;
        taxTotal = taxTotal * 10000;
        taxTotal = Math.round(taxTotal);
        taxTotal = taxTotal / 10000;
    }
    taxTotal = FormatNumber(taxTotal, 2, true, false);

    Product.tax.value = "$" + taxTotal;
    Shipping = Product.ship.value;
    Shipping = Shipping.slice(1, Shipping.length);
    theTotal = subtotal + parseFloat(taxTotal) + parseFloat(Shipping);
    theTotal = FormatNumber(theTotal, 2, true, false);
    Product.total.value = "$" + theTotal;
        
    
}

function checkIfOrder(){
    Order = Product.total.value;
    Order = Order.slice(1, Order.length);
    Order = parseFloat(Order);
    if(Order > 0){
        Product.submit();
    }
    else{
        alert("Nothing has been ordered.");
    }
}

function ValidateCreditInfo_C2(){
    msg = "";
    msg += HasQuotesNotNull(checkout.fname, "First name");
    msg += HasQuotesNotNull(checkout.lname, "Last name");
    msg += HasQuotes(checkout.company, "Company");
    msg += HasQuotesNotNull(checkout.billing_address1, "Billing Address");
    msg += HasQuotesNotNull(checkout.billing_city, "Billing City");
    msg += HasQuotesNotNull(checkout.billing_state, "Billing State");
    msg += HasQuotesNotNull(checkout.country, "Country");
    msg += HasQuotesNotNull(checkout.billing_zip, "Billing Zip");
    msg += HasQuotesNotNull(checkout.email_Address, "Email Address");

    msg += HasQuotes(checkout.shipping_address1, "Shipping Address");
    msg += HasQuotes(checkout.shipping_city, "Shipping City");
    msg += HasQuotes(checkout.shipping_state, "Shipping State");
    msg += HasQuotes(checkout.shipping_zip, "Shipping Zip");  
}

function ValidateCreditInfo_C3(){
    msg = "";    
    msg += IsCreditCardNumber(checkout.Credit_Card_Number, "Credit Card Number");
    msg += IsADate(checkout.Credit_Card_Expiration_Date, "Credit Card Expiration_Date");
}

function ProceedOnCreditInfo(){
    ValidateCreditInfo();
    if(msg == ""){
        checkout.submit();
    }
    else{
        alert(msg);
    }
}

function ProceedOnCreditInfo_C2(){
    ValidateCreditInfo_C2();
    if(msg == ""){
        checkout.submit();
    }
    else{
        alert(msg);
    }
}

function ProceedOnCreditInfo_C3(){
    ValidateCreditInfo_C3();
    if(msg == ""){
        checkout.submit();
    }
    else{
        alert(msg);
    }
}