﻿var nTimeout;
function ChangeQTY(obj) {
    var objs = obj.name.split('_');
   
    if (obj.value != "" && objs.length > 2 && objs[2] == "1") {
        $("#CountryDiv" + objs[1]).css("display", "block");
        $("#addInfo" + objs[1]).css("display", "none");
    }
    else {        
        if (obj.value != "" && Number(obj.value) > Number($("#stock" + objs[1]).val())) {
            SetBoxStock(objs[1], false);
        }
        else {
            SetBoxStock(objs[1], true);
        }        
    }
    
    if (obj.value == "") {
        $("#CountryDiv" + objs[1]).css("display", "none");
    }

}

function CountryChange(mpid) {
    var ProductS = $("#Country_" + mpid).val().split('_');
    if (ProductS.length > 1 && (Number(ProductS[1]) == 0 || Number(ProductS[1]) < Number($("#QTY_" + mpid + "_1").val()))) {
        //   alert('Out Of Stock');
        SetBoxStock(mpid, false);
        return;
    }
    else {
        SetBoxStock(mpid, true);
        return;
    }
}

function AffiliateAddCart(objname, nProductID, nStoreID, szProductType) {
    var names = objname.split('_');
    if ($("#" + objname).val() == "") {
        alert("Please select the quantity.");
        $("#" + objname).focus();
        return;
    }

    if (names.length > 2 && names[2] == "1" && $("#Country_" + names[1]).val() == "") {
        alert("Please select the Country.");
        $("#Country_" + names[1]).focus();
        return;
    }

    var ProductS = $("#Country_" + names[1]).val().split('_');

    if (ProductS.length > 1 && (Number(ProductS[1]) == 0 || Number(ProductS[1]) < Number($("#" + objname).val()))) {
        SetBoxStock(names[1], false);
        return;
    }
    if (nProductID == 0) {
        nProductID = ProductS[0];
        if (Number($("#" + objname).val()) > Number($("#stock" + names[1]).val())) {
            SetBoxStock(names[1], false);
        }
        else {
            SetBoxStock(names[1], true);
        }
     }
    
    addProduct(names[1],nProductID, nStoreID, 0, szProductType, $("#" + objname).val());
}



function addProduct(nMasterProductID,nProductID, nStoreID, nAttachToID, szProductType,nQty) {
    //   alert(nProductID + "," + nStoreID + "," + nAttachToID + "," + szProductType);
        $("#btn_AddCart" + nMasterProductID).active(false);
        $("#Loading" + nMasterProductID).css("display", "block");
        $.getJSON("/Accessory/Creative.aspx", "productType=" + szProductType + "&add=" + nProductID + "," + nStoreID + "," + nAttachToID + "," + nQty + "&rnd=" + Math.random(), function(data) {

            if (data.szStatus == "OK") {
                $("#CartText").html("Cart " + data.ItemCount + " items:" + data.Subtotal);
                SetBoxCssClass(nMasterProductID, nQty, false);
                $("#btn_AddCart" + nMasterProductID).active(true);            
            }
            else {                
                $("#btn_AddCart" + nMasterProductID).active(true);
            }

            $("#Loading" + nMasterProductID).css("display", "none");
        });        
}

function SetBoxStock(nMasterProductID, IsStock) {
    if (IsStock) {
        $("#btn_AddCart" + nMasterProductID).active(true);
        $("#StockOf" + nMasterProductID).css("display", "none");
    }
    else {
        $("#btn_AddCart" + nMasterProductID).active(false);
        $("#StockOf" + nMasterProductID).css("display", "");
    }
    
}

function SetBoxCssClass(nMasterProductID, nQty, IsRecover) {    
    if (IsRecover) {
        $("#ProductBox" + nMasterProductID).removeClass("affiProductBoxSelect");
        $("#ProductBox" + nMasterProductID).addClass("affiProductBox");      
        $("#addInfo" + nMasterProductID).css("display", "none");
        $("#CountryDiv" + nMasterProductID).css("display", "block");
        $("#QTY_" + nMasterProductID + "_0").attr("value", "");     
        $("#QTY_" + nMasterProductID + "_1").attr("value", "");
        $("#Top" + nMasterProductID).css("display", "none");
        $("#Bottom" + nMasterProductID).css("display", "none");                 
    }
    else {
        $("#ProductBox" + nMasterProductID).removeClass("affiProductBox");
        $("#ProductBox" + nMasterProductID).addClass("affiProductBoxSelect");
        $("#addInfo" + nMasterProductID).css("display", "block");
        $("#CountryDiv" + nMasterProductID).css("display", "none");
        $("#QTY_" + nMasterProductID + "_0").attr("value", nQty);
        $("#QTY_" + nMasterProductID + "_1").attr("value", nQty);
        $("#Top" + nMasterProductID).css("display", "block");
        $("#Bottom" + nMasterProductID).css("display", "block");

        setTimeout(function() {            
            $("#Top" + nMasterProductID).fadeOut();
            $("#Bottom" + nMasterProductID).fadeOut();
            $("#ProductBox" + nMasterProductID).removeClass("affiProductBoxSelect");
            $("#ProductBox" + nMasterProductID).addClass("affiProductBox");
            $("#addInfo" + nMasterProductID).fadeOut();
            $("#CountryDiv" + nMasterProductID).fadeOut();
            $("#QTY_" + nMasterProductID + "_0").attr("value", "");
            $("#QTY_" + nMasterProductID + "_1").attr("value", "");
        }, 1000);
    }
}

function openTabManually(id) {
    $("#" + id.replace("_Title", "_Content")).slideDown("fast", openTab(id));
}

function closeTab(id) {
    $("#" + id).addClass("TabTitleClosed");
    $("#" + id).removeClass("TabTitleOpened");
    $("#" + id.replace("_Title", "_Content")).addClass("TabContentAreaClosed");
    $("#" + id.replace("_Title", "_Content")).removeClass("TabContentAreaOpened");
}

function openTab(id) {
    $("#" + id).addClass("TabTitleOpened");
    $("#" + id).removeClass("TabTitleClosed");
    $("#" + id.replace("_Title", "_Content")).addClass("TabContentAreaOpened");
    $("#" + id.replace("_Title", "_Content")).removeClass("TabContentAreaClosed");

}


function newWindow2(url, width, height, resize) {
    var x = (screen.width - width) / 2;
    var y = (screen.height - height) / 2 - 70;
    //var win = window.open(url, "newwindow", "top=" + x + ",left=" + y + ",width=" + width + ",height=" + height + ",toolbar=0,scroll=0,resize=" + resize);
    var win = window.open(url, 'newwindow', 'top=' + y + ',left=' + x + ',status=yes,toolbar=no,location=no,scrollbars=no,width=' + width + ',height=' + height)
    win.focus();
} 
