﻿function clearDefault(el) {
    if (el.defaultValue == el.value) el.value = ""
}
//THIS FUNCTION WILL FILL ALL THE VALUES IN THE OTHER FIELDS
function fillvalues(varCount, myObject) {
    if (myObject == '') {
        return;
    }

    var qryStr = "itemcode=" + myObject + "&clevel=" + document.getElementById("hdCLevel").value;
    //alert(qryStr); 
    $.get("Handlers/GetProductDetails.ashx?" + qryStr, function(result) {
        //alert(result);
        var strSplit = result.split("~~");
        if (strSplit[0] != ' ') {
            document.getElementById('price_' + varCount).innerHTML = strSplit[0];
        }
        if (strSplit[1] != ' ') {
            document.getElementById('spn_img_' + varCount).innerHTML = '<img src="' + strSplit[1] + '" style="border-width:0px;" width="75px" height="75" />';
        }
        if (strSplit[2] != ' ') {
            //              alert(strSplit[2] );
            document.getElementById('min_' + varCount).innerHTML = strSplit[2];
            document.getElementById('qty_' + varCount).value = strSplit[2];
            // var myNextCount = (varCount *1) + 1;     
            //                alert(myNextCount );
            //document.getElementById('item_' + myNextCount  ).focus();
        }
        else {
            //alert('Invalid SKU');
            //document.getElementById('item_' + varCount  ).value = '';
            //document.getElementById('item_' + varCount  ).focus();
        }
        if (strSplit[6] != ' ') {
            document.getElementById('desc_' + varCount).innerHTML = strSplit[6];
        }

        if (strSplit[12] != ' ') {
            document.getElementById('uom_' + varCount).innerHTML = strSplit[12];
        }


        if (strSplit[8] != ' ') {
            document.getElementById('aqty_' + varCount).innerHTML = strSplit[8];
        }

        //BUILD THE QUERYSTRING NOW.
        var QryStr = "sku=" + myObject + "&cid=" + WOWtrim(document.getElementById('hdCID').value) + "&clid=" + WOWtrim(document.getElementById('hdCLevel').value);
        QryStr = QryStr + "&min=" + WOWtrim(strSplit[2]) + "&incr=" + WOWtrim(strSplit[22]) + "&hotbuy=" + WOWtrim(strSplit[21]) + "&vid=" + WOWtrim(strSplit[4]);
        QryStr = QryStr + "&pid=" + WOWtrim(strSplit[3]);
        document.getElementById('hid_' + varCount).value = QryStr;
    });
}
 
//THIS FUNCTION IS FOR QUICK ORDERS PAGE.
function saveCart(myType) {
    $.blockUI({ css: {
        border: 'none',
        padding: '15px',
        backgroundColor: '#000',
        '-webkit-border-radius': '10px',
        '-moz-border-radius': '10px',
        opacity: .5,
        color: '#fff'
    }
    });

   
    var myControls;
    var ctrlName;
    myControls = document.getElementsByTagName("*");
    var ctrLength;
    ctrLength = myControls.length;
    var strQty = '';
    var myQty = '';
    var ctrlName1 = '';
    var items = new Array();
    var intItemCount = 0;
    var VariantIDS = "";
    //LOOPING THROUGH EACH CONTROL AND GETTING CORRECT FOOTER TEXTBOX
    for (var intCount = 0; intCount < ctrLength; intCount++) {
        ctrlName = '';
        ctrlName = myControls[intCount].id;
        if (ctrlName.indexOf('item_') != -1) {
            if (document.getElementById(ctrlName).value != "") {

                intItemCount = intItemCount + 1;
                ctrlName1 = ctrlName.replace('item_', 'hid_');
                var myQRY = document.getElementById(ctrlName1).value;
                var productProperties = myQRY.split("&");
                var VariantID = "";
                var ProductID = "";
                var ItemCode = "";
                var CartType = myType;
                ctrlName1 = ctrlName.replace('item_', 'qty_');
                var Quantity = document.getElementById(ctrlName1).value;
                var HotBuy = "";
                var Minimum = "";
                var Increment = "";
                var CustomerLevel = "";
                var CustomerID = "";
                //LOOPING THROUGH THE QUERY STRING AND ASSINGING THE VALUES
                for (var splitCounter = 0; splitCounter < productProperties.length; splitCounter++) {
                    var varValue = productProperties[splitCounter].split("=");
                    if (varValue[0] == "sku")
                        ItemCode = varValue[1];
                    else if (varValue[0] == "qty")
                        Quantity = varValue[1];
                    else if (varValue[0] == "cid")
                        CustomerID = varValue[1];
                    else if (varValue[0] == "hotbuy")
                        HotBuy = varValue[1];
                    else if (varValue[0] == "min")
                        Minimum = varValue[1];
                    else if (varValue[0] == "incr")
                        Increment = varValue[1];
                    else if (varValue[0] == "pid")
                        ProductID = varValue[1];
                    else if (varValue[0] == "vid")
                        VariantID = varValue[1];
                    else if (varValue[0] == "clid")
                        CustomerLevel = varValue[1];
                }
                VariantIDS = VariantIDS + "," + VariantID + "~" + Quantity;
                //                items.push({ ProductID: ProductID, VariantID: VariantID, ItemCode: ItemCode,
                //                    CartType: CartType, Quantity: Quantity, HotBuy: HotBuy,
                //                    Minimum: Minimum, Increment: Increment, CustomerLevel: CustomerLevel, CustomerID: CustomerID
                //                });
            }
        }
    }
    var strPQry = "?cartType=" + myType + "&variantIDS=" + VariantIDS + "&" + new Date();
    $.get("Handlers/AddToCartHandler.ashx" + strPQry, function(result) {
        var message = result;
        $.blockUI({ css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: .5,
            color: '#fff',
            cursor: 'wait'
        }
            , message: intItemCount + ' Product(s) added successfully to the ' + (myType == '0' ? 'cart' : 'portfolio') + '.'
    });
        
        setTimeout($.unblockUI, 10000);
        if (myType == "0") {
            window.location.href = "shoppingcart.aspx?errormsg=" + message + "&ReturnUrl=quickorder.aspx";
        }
        if (myType == "1") {
            window.location.href = "wishlist.aspx?ReturnUrl=quickorder.aspx";
        }
        if (myType == "2") {
            window.location.href = "shoppingcart.aspx?errormsg=" + message + "&ReturnUrl=quickorder.aspx";
        }

    });


}

//function saveCartPL(myType) {
//    $.blockUI({ css: {
//        border: 'none',
//        padding: '15px',
//        backgroundColor: '#000',
//        '-webkit-border-radius': '10px',
//        '-moz-border-radius': '10px',
//        opacity: .5,
//        color: '#fff'
//    }
//    });

//    var myControls;
//    var ctrlName;
//    var boolProcess = false;
//    myControls = document.getElementsByTagName("*");
//    var ctrLength;
//    ctrLength = myControls.length;
//    var getCID = '';
//    var intItemCount = 0;
//    var items = new Array();
//    //LOOP THROUGH THE ELEMENTS AND SEE WHETHER THE CHECKBOX IS CLICKED.    
//    for (var intCount = 0; intCount < ctrLength; intCount++) {
//        ctrlName = '';
//        ctrlName = myControls[intCount].id;
//        if (ctrlName.indexOf('WOWajaxChkBX~~') != -1) {
//            if (document.getElementById(ctrlName).checked == true) {
//                var myQRY = ctrlName.split("~~");
//                intItemCount = intItemCount + 1;
//                if (intItemCount == 0) {
//                    getCID = myQRY[1];
//                }
//                var productProperties = myQRY[1].split("&");
//                var VariantID = "";
//                var ProductID = "";
//                var ItemCode = "";
//                var CartType = myType;
//                var Quantity = "";
//                var HotBuy = "";
//                var Minimum = "";
//                var Increment = "";
//                var CustomerLevel = "";
//                var CustomerID = "";
//                //LOOPING THROUGH THE QUERY STRING AND ASSINGING THE VALUES
//                for (var splitCounter = 0; splitCounter < productProperties.length; splitCounter++) {
//                    var varValue = productProperties[splitCounter].split("=");
//                    if (varValue[0] == "sku")
//                        ItemCode = varValue[1];
//                    else if (varValue[0] == "qty")
//                        Quantity = varValue[1];
//                    else if (varValue[0] == "cid")
//                        CustomerID = varValue[1];
//                    else if (varValue[0] == "hotbuy")
//                        HotBuy = varValue[1];
//                    else if (varValue[0] == "min")
//                        Minimum = varValue[1];
//                    else if (varValue[0] == "incr")
//                        Increment = varValue[1];
//                    else if (varValue[0] == "pid")
//                        ProductID = varValue[1];
//                    else if (varValue[0] == "vid")
//                        VariantID = varValue[1];
//                    else if (varValue[0] == "clid")
//                        CustomerLevel = varValue[1];

//                }
//                items.push({ ProductID: ProductID, VariantID: VariantID, ItemCode: ItemCode,
//                    CartType: CartType, Quantity: Quantity, HotBuy: HotBuy,
//                    Minimum: Minimum, Increment: Increment, CustomerLevel: CustomerLevel, CustomerID: CustomerID
//                });
//                document.getElementById(ctrlName).checked = false;
//            }
//        }
//    }
//    //var options = { error: function(msg) { alert(msg); },
//    if (intItemCount == 0) {
//        $.blockUI({ css: {
//            border: 'none',
//            padding: '15px',
//            backgroundColor: '#000',
//            '-webkit-border-radius': '10px',
//            '-moz-border-radius': '10px',
//            opacity: .5,
//            color: '#fff',
//            cursor: 'wait'
//        }
//            , message: 'Please select Product(s) to be added to the ' + (myType == '0' ? 'cart' : 'portfolio') + '.'
//        });
//        setTimeout($.unblockUI, 1000);
//    }
//    else {
//        var options = {
//            type: "POST",
//            url: "WOWLayer/JSON/JSonAddToCart.aspx/AddProductsToCart",
//            data: "{'items':" + JSON.stringify(items) + "}",
//            contentType: "application/json; charset=utf-8",
//            dataType: "json",
//            async: true,
//            success: function(response) {
//                var op = response.d;
//                $.each(op, function(index, op) {
//                    //alert(op.CartTotal);
//                    //alert(op.CartTotalIncludesDiscount);
//                    //alert(op.TotalItems);
//                    $.blockUI({ css: {
//                        border: 'none',
//                        padding: '15px',
//                        backgroundColor: '#000',
//                        '-webkit-border-radius': '10px',
//                        '-moz-border-radius': '10px',
//                        opacity: .5,
//                        color: '#fff',
//                        cursor: 'wait'
//                    }
//            , message: 'Added ' + intItemCount + ' Product(s) to the ' + (myType == '0' ? 'cart' : 'portfolio') + '.'
//                    });
//                    setTimeout($.unblockUI, 1000);
//                });
//            },
//            failure: function(msg) { getError(msg); },
//            error: function(msg) { getError(msg); }
//        };
//        jQuery.ajax(options);
//    }
//}

//function saveCartAllPL(myType) {
//    $.ajaxSetup({ async: true });
//    $.blockUI({ css: {
//        border: 'none',
//        padding: '15px',
//        backgroundColor: '#000',
//        '-webkit-border-radius': '10px',
//        '-moz-border-radius': '10px',
//        opacity: .5,
//        color: '#fff'
//    }
//    });

//    var myControls;
//    var ctrlName;
//    var boolProcess = false;
//    myControls = document.getElementsByTagName("*");
//    var ctrLength;
//    ctrLength = myControls.length;
//    var getCID = '';
//    var intItemCount = 0;
//    var items = new Array();

//    for (var intCount = 0; intCount < ctrLength; intCount++) {
//        ctrlName = '';
//        ctrlName = myControls[intCount].id;
//        if (ctrlName.indexOf('WOWajaxChkBX~~') != -1) {

//            var myQRY = ctrlName.split("~~");
//            intItemCount = intItemCount + 1;
//            //            if (intItemCount == 0) {
//            //                getCID = myQRY[1];
//            //            }
//            var productProperties = myQRY[1].split("&");
//            var VariantID = "";
//            var ProductID = "";
//            var ItemCode = "";
//            var CartType = myType;
//            var Quantity = "";
//            var HotBuy = "";
//            var Minimum = "";
//            var Increment = "";
//            var CustomerLevel = "";
//            var CustomerID = "";
//            //LOOPING THROUGH THE QUERY STRING AND ASSINGING THE VALUES
//            for (var splitCounter = 0; splitCounter < productProperties.length; splitCounter++) {
//                var varValue = productProperties[splitCounter].split("=");
//                if (varValue[0] == "sku")
//                    ItemCode = varValue[1];
//                else if (varValue[0] == "qty")
//                    Quantity = varValue[1];
//                else if (varValue[0] == "cid")
//                    CustomerID = varValue[1];
//                else if (varValue[0] == "hotbuy")
//                    HotBuy = varValue[1];
//                else if (varValue[0] == "min")
//                    Minimum = varValue[1];
//                else if (varValue[0] == "incr")
//                    Increment = varValue[1];
//                else if (varValue[0] == "pid")
//                    ProductID = varValue[1];
//                else if (varValue[0] == "vid")
//                    VariantID = varValue[1];
//                else if (varValue[0] == "clid")
//                    CustomerLevel = varValue[1];

//            }
//            items.push({ ProductID: ProductID, VariantID: VariantID, ItemCode: ItemCode,
//                CartType: CartType, Quantity: Quantity, HotBuy: HotBuy,
//                Minimum: Minimum, Increment: Increment, CustomerLevel: CustomerLevel, CustomerID: CustomerID
//            });
//            document.getElementById(ctrlName).checked = false;

//        }
//    }
//    var options = {
//        type: "POST",
//        url: "WOWLayer/JSON/WSAddCart.asmx/AddProductsToCart",
//        data: "{'items':" + JSON.stringify(items) + "}",
//        contentType: "application/json; charset=utf-8",
//        dataType: "json",
//        async: true,
//        success: function(response) {
//            var op = response.d;
//            $.each(op, function(index, op) {
//                //alert(op.CartTotal);
//                //alert(op.CartTotalIncludesDiscount);
//                //alert(op.TotalItems);
//                $.blockUI({ css: {
//                    border: 'none',
//                    padding: '15px',
//                    backgroundColor: '#000',
//                    '-webkit-border-radius': '10px',
//                    '-moz-border-radius': '10px',
//                    opacity: .5,
//                    color: '#fff',
//                    cursor: 'wait'
//                }
//            , message: 'Added ' + intItemCount + ' Product(s) to the ' + (myType == '0' ? 'cart' : 'portfolio') + '.'
//                });
//                setTimeout($.unblockUI, 1000);
//            });
//        },
//        failure: function(msg) { getError(msg); },
//        error: function(msg) { getError(msg); }
//    };
//    jQuery.ajax(options);
//}
function saveCartPL(myType) {
    $.blockUI({ css: {
        border: 'none',
        padding: '15px',
        backgroundColor: '#000',
        '-webkit-border-radius': '10px',
        '-moz-border-radius': '10px',
        opacity: .5,
        color: '#fff'
    }
    });

    var myControls;
    var ctrlName;
    var boolProcess = false;
    myControls = document.getElementsByTagName("*");
    var ctrLength;
    ctrLength = myControls.length;
    var getCID = '';
    var intItemCount = 0;
    var items = new Array();
    var VariantIDS = "";
    //LOOP THROUGH THE ELEMENTS AND SEE WHETHER THE CHECKBOX IS CLICKED.    
    for (var intCount = 0; intCount < ctrLength; intCount++) {
        ctrlName = '';
        ctrlName = myControls[intCount].id;
        if (ctrlName.indexOf('WOWajaxChkBX~~') != -1) {
            if (document.getElementById(ctrlName).checked == true) {
                var myQRY = ctrlName.split("~~");
                intItemCount = intItemCount + 1;
                if (intItemCount == 0) {
                    getCID = myQRY[1];
                }
                var productProperties = myQRY[1].split("&");
                var VariantID = "";
                var ProductID = "";
                var ItemCode = "";
                var CartType = myType;
                var Quantity = "";
                var HotBuy = "";
                var Minimum = "";
                var Increment = "";
                var CustomerLevel = "";
                var CustomerID = "";
                //LOOPING THROUGH THE QUERY STRING AND ASSINGING THE VALUES
                for (var splitCounter = 0; splitCounter < productProperties.length; splitCounter++) {
                    var varValue = productProperties[splitCounter].split("=");
                    if (varValue[0] == "sku")
                        ItemCode = varValue[1];
                    else if (varValue[0] == "qty")
                        Quantity = varValue[1];
                    else if (varValue[0] == "cid")
                        CustomerID = varValue[1];
                    else if (varValue[0] == "hotbuy")
                        HotBuy = varValue[1];
                    else if (varValue[0] == "min")
                        Minimum = varValue[1];
                    else if (varValue[0] == "incr")
                        Increment = varValue[1];
                    else if (varValue[0] == "pid")
                        ProductID = varValue[1];
                    else if (varValue[0] == "vid")
                        VariantID = varValue[1];
                    else if (varValue[0] == "clid")
                        CustomerLevel = varValue[1];

                }
                VariantIDS = VariantIDS + "," + VariantID;     
                document.getElementById(ctrlName).checked = false;
            }
        }
    }
    //var options = { error: function(msg) { alert(msg); },
    if (intItemCount == 0) {
        $.blockUI({ css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: .5,
            color: '#fff',
            cursor: 'wait'
        }
            , message: 'Please select Product(s) to be added to the ' + (myType == '0' ? 'cart' : 'portfolio') + '.'
        });
        setTimeout($.unblockUI, 1000);
    }
    else {
        var strPQry = "?cartType=" + myType + "&variantIDS=" + VariantIDS + "&" + new Date();
        $.get("Handlers/AddToCartHandler.ashx" + strPQry, function(result) {

                $.blockUI({ css: {
                    border: 'none',
                    padding: '15px',
                    backgroundColor: '#000',
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: .5,
                    color: '#fff',
                    cursor: 'wait'
                }
            , message: intItemCount + ' Product(s) added successfully to the ' + (myType == '0' ? 'cart' : 'portfolio') + '.'
                });
                setTimeout($.unblockUI, 10000);
                setTimeout(window.location.href = window.location.href, 500);        

        });
    }
}
function saveCartAllPL(myType) {
    $.ajaxSetup({ async: true });
    $.blockUI({ css: {
        border: 'none',
        padding: '15px',
        backgroundColor: '#000',
        '-webkit-border-radius': '10px',
        '-moz-border-radius': '10px',
        opacity: .5,
        color: '#fff'
    }
    });

    var myControls;
    var ctrlName;
    var boolProcess = false;
    myControls = document.getElementsByTagName("*");
    var ctrLength;
    ctrLength = myControls.length;
    var getCID = '';
    var intItemCount = 0;
    var items = new Array();
    var VariantIDS = "";
    for (var intCount = 0; intCount < ctrLength; intCount++) {
        ctrlName = '';
        ctrlName = myControls[intCount].id;
        if (ctrlName.indexOf('WOWajaxChkBX~~') != -1) {

            var myQRY = ctrlName.split("~~");
            intItemCount = intItemCount + 1;
            //            if (intItemCount == 0) {
            //                getCID = myQRY[1];
            //            }
            var productProperties = myQRY[1].split("&");
            var VariantID = "";
            var ProductID = "";
            var ItemCode = "";
            var CartType = myType;
            var Quantity = "";
            var HotBuy = "";
            var Minimum = "";
            var Increment = "";
            var CustomerLevel = "";
            var CustomerID = "";
            //LOOPING THROUGH THE QUERY STRING AND ASSINGING THE VALUES
            for (var splitCounter = 0; splitCounter < productProperties.length; splitCounter++) {
                var varValue = productProperties[splitCounter].split("=");
                if (varValue[0] == "sku")
                    ItemCode = varValue[1];
                else if (varValue[0] == "qty")
                    Quantity = varValue[1];
                else if (varValue[0] == "cid")
                    CustomerID = varValue[1];
                else if (varValue[0] == "hotbuy")
                    HotBuy = varValue[1];
                else if (varValue[0] == "min")
                    Minimum = varValue[1];
                else if (varValue[0] == "incr")
                    Increment = varValue[1];
                else if (varValue[0] == "pid")
                    ProductID = varValue[1];
                else if (varValue[0] == "vid")
                    VariantID = varValue[1];
                else if (varValue[0] == "clid")
                    CustomerLevel = varValue[1];

            }
            VariantIDS = VariantIDS + "," + VariantID            
            document.getElementById(ctrlName).checked = false;

        }
    }
    var strPQry = "?cartType=" + myType + "&variantIDS=" + VariantIDS + "&" + new Date();
    $.get("Handlers/AddToCartHandler.ashx" + strPQry, function(result) {
            $.blockUI({ css: {
                border: 'none',
                padding: '15px',
                backgroundColor: '#000',
                '-webkit-border-radius': '10px',
                '-moz-border-radius': '10px',
                opacity: .5,
                color: '#fff',
                cursor: 'wait'
            }
            , message:  intItemCount + ' Product(s) added successfully to the ' + (myType == '0' ? 'cart' : 'portfolio') + '.'
            });
            setTimeout($.unblockUI, 10000);
            setTimeout(window.location.href = window.location.href, 500);

    });
   
}

function getError(msg) {

    $.blockUI({ css: {
        border: 'none',
        padding: '15px',
        backgroundColor: '#000',
        '-webkit-border-radius': '10px',
        '-moz-border-radius': '10px',
        opacity: .5,
        color: '#fff',
        cursor: 'wait'
    }
            , message: msg
    });
    setTimeout($.unblockUI, 1000);
}


//This is for the pop up to open -- customized for wow.
function MyOpenPopup(c) {

    window.open(c,
                    'window',
                    'width=480,height=480,scrollbars=yes,status=yes');
}

function checkNumeric(ip) {
    //alert('inside');
    var err = 0
    string = document.getElementById(ip).value;
    var valid = "0123456789"
    var ok = "yes";
    var temp;
    for (var i = 0; i < string.length; i++) {
        temp = "" + string.substring(i, i + 1);
        if (valid.indexOf(temp) == "-1") err = 1;
    }

    if (err == 1) {
        alert('Invalid SKU/Quantity');
        document.getElementById(ip).value = '';
        //document.getElementById(ip).focus();
    }
}
function WOWtrim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}
