﻿




/*-------------- Step 3  Begin --------------*/

function bottleItemClick(sender, selectedBottles) {
    selectedBottles = selectedBottles || getSelectedBottles();
    toggleItemSelected(sender, selectedBottles);

    ShowPackageStatus(selectedBottles);
    setSelectedBottles(selectedBottles);
}

function toggleItemSelected(liItem, selectedBottles) {
    selectedBottles = selectedBottles || getSelectedBottles();
    var bottleId = $($(liItem).find("input[type='hidden']")).val();
    var itemIndex = -1;     // the array index of "bottleId", if it exists in the array "selectedBottles"
    for (var i = 0; i < selectedBottles.length; i++) {
        if (selectedBottles[i] == bottleId) {
            itemIndex = i;
            itemExists = true;
            break;
        }
    }
    if (itemIndex > -1)   // remove the "bottleId" from array
    {
        for (var j = itemIndex; j < selectedBottles.length - 1; j++) {
            selectedBottles[j] = selectedBottles[j + 1];
        }
        selectedBottles.pop();
    }
    else    // push the "bottleId" into array
    {
        selectedBottles.push(bottleId);
    }

    $(liItem).toggleClass("selected");
}

function ShowPackageStatus(selectedBottles) {
    var totalBottleCount = selectedBottles.length;
    if (totalBottleCount > 0) {
        // pack box type 1
        $("#spanCountOfBoxType1").html(totalBottleCount);
        $("span.doos1 img").attr("src", "/Images/doos1.jpg");

        // pack box type 2
        var boxCountOfType2 = parseInt(totalBottleCount / 4);
        $("#spanCountOfBoxType2").html(boxCountOfType2);
        
        var remainderOfBoxType2 = totalBottleCount % 4;
        remainderOfBoxType2 = (remainderOfBoxType2 == 0) ? 4 : remainderOfBoxType2;     // if the remainder is 0, set it as 4
        $("#spanLeftFlesOfBoxType2").html(4 - remainderOfBoxType2);
        var imagesOfBoxType2 = $("span.doos4 img");
        for (var j = 0; j < imagesOfBoxType2.length; j++) {
            if (j < remainderOfBoxType2) {
                $(imagesOfBoxType2.get(j)).attr("src", "/Images/doos1.jpg");
            }
            else {
                $(imagesOfBoxType2.get(j)).attr("src", "/Images/doos2.jpg");
            }
        }

        // pack box type 3
        var boxCountOfType3 = parseInt(totalBottleCount / 6);
        $("#spanCountOfBoxType3").html(boxCountOfType3);
        var remainderOfBoxType3 = totalBottleCount % 6;
        remainderOfBoxType3 = (remainderOfBoxType3 == 0) ? 6 : remainderOfBoxType3;     // if the remainder is 0, set it as 6
        $("#spanLeftFlesOfBoxType3").html(6 - remainderOfBoxType3);
        var imagesOfBoxType3 = $("span.doos6 img");
        for (var j = 0; j < imagesOfBoxType3.length; j++) {
            if (j < remainderOfBoxType3) {
                $(imagesOfBoxType3.get(j)).attr("src", "/Images/doos1.jpg");
            }
            else {
                $(imagesOfBoxType3.get(j)).attr("src", "/Images/doos2.jpg");
            }
        }
    }
    else    // set images' src to empty image
    {
        $("span.doos1 img").attr("src", "/Images/doos2.jpg");
        $("span.doos4 img").each(function() { $(this).attr("src", "/Images/doos2.jpg"); });
        $("span.doos6 img").each(function() { $(this).attr("src", "/Images/doos2.jpg"); });

        $("#spanCountOfBoxType1").html(0);      // reset the dozen count to 0
        $("#spanLeftFlesOfBoxType2").html(4);   // reset the left fles count to 4
        $("#spanLeftFlesOfBoxType3").html(6);   // reset the left fles count to 6
    }


    if (totalBottleCount > 0) {
        $(".panelCol2 .btnBoxType1Package").show();
        $(".panelCol2 .btnBoxType1Gray").hide();
    }
    else {
        $(".panelCol2 .btnBoxType1Package").hide();
        $(".panelCol2 .btnBoxType1Gray").show();
    }

    if (totalBottleCount > 0 && totalBottleCount % 4 == 0) {
        $(".panelCol2 .btnBoxType2Package").show();
        $(".panelCol2 .btnBoxType2Gray").hide();
    }
    else {
        $(".panelCol2 .btnBoxType2Package").hide();
        $(".panelCol2 .btnBoxType2Gray").show();
    }

    if (totalBottleCount > 0 && totalBottleCount % 6 == 0) {
        $(".panelCol2 .btnBoxType3Package").show();
        $(".panelCol2 .btnBoxType3Gray").hide();
    }
    else {
        $(".panelCol2 .btnBoxType3Package").hide();
        $(".panelCol2 .btnBoxType3Gray").show();
    }
}

function selectAllBottles(selectedBottles) {
    selectedBottles = selectedBottles || getSelectedBottles();
    $("ul.unPackedBottles li").each(function() {
        $this = $(this);
        if (!$this.is(".selected")) {
            toggleItemSelected(this, selectedBottles)
        }
    });
    ShowPackageStatus(selectedBottles);
    setSelectedBottles(selectedBottles);
}

function initializeBottles(selectedBottles) {
    $("ul.unPackedBottles li").each(function() {
        var bottleId = $($(this).find("input[type='hidden']")).val();
        var isSelected = false;
        for (var i = 0; i < selectedBottles.length; i++) {
            if (selectedBottles[i] == bottleId) {
                isSelected = true;
                break;
            }
        }
        $(this).toggleClass("selected", isSelected);
    });
}

function getSelectedBottles() {
    var arrSelectedBottles = null;
    var strSelectedBottles = $("#" + txtSelectedBottles_ClientID).val();
    if (strSelectedBottles && strSelectedBottles.length > 0) {
        arrSelectedBottles = strSelectedBottles.split(",");
    }
    else {
        arrSelectedBottles = new Array();
    }
    if (arrSelectedBottles == undefined || arrSelectedBottles == null) {
        arrSelectedBottles = new Array();
    }
    return arrSelectedBottles;
}
function setSelectedBottles(arrSelectedBottles) {
    var strSelectedBottles = "";
    if (arrSelectedBottles != undefined || arrSelectedBottles != null) {
        strSelectedBottles = arrSelectedBottles.join(",");
    }
    $("#" + txtSelectedBottles_ClientID).val(strSelectedBottles);
}

function validBoxType1(sender, args) {
    var selectedBottles = getSelectedBottles();
    args.IsValid = (selectedBottles != null && selectedBottles.length > 0);
}

function validBoxType2(sender, args) {
    var selectedBottles = getSelectedBottles();
    args.IsValid = (selectedBottles != null && selectedBottles.length > 0 && selectedBottles.length % 4 == 0);
}

function validBoxType3(sender, args) {
    var selectedBottles = getSelectedBottles();
    args.IsValid = (selectedBottles != null && selectedBottles.length > 0 && selectedBottles.length % 6 == 0);
}

function ValidSelfPack()
{
    var unpackCount = parseInt($("._UnpackCount").html());
    if (!isNaN(unpackCount) && unpackCount > 0)
    {
        //var text = $("._leftPacksInfo").html();
        //alert(unpackCount + " unpacked!");
        AlertInfo(unpackCount + " unpacked!");
        return false;
    }
    else
    {
        var text = $("._SendPackage").html();
        return confirm(text);
    }
}

/*-------------- Step 3  End --------------*/


/*-------------- Step 4  Begin --------------*/


function ValidFirstName(sender, args) {
    ValidRequiredField(sender, args);
}

function ValidLastName(sender, args) {
    ValidRequiredField(sender, args);
}

function ValidAddress(sender, args) {
    ValidRequiredField(sender, args);
}

function ValidHouseNumber(sender, args) {
    ValidRequiredField(sender, args);
}

function ValidPostcode(sender, args) {
    var exp = /\d{4}/;
    args.IsValid = exp.test(args.Value);
    SetValidStatus(sender, args);
}

function ValidPostcode2(sender, args) {
    var exp = /[a-zA-Z]{2}/;
    args.IsValid = exp.test(args.Value);
    args.isPostcode2 = true;
    SetValidStatus(sender, args);
}

function ValidPlace(sender, args) {
    ValidRequiredField(sender, args);
}

function ValidTelephone(sender, args) {
    ValidRequiredField(sender, args);
}

function ValidBirthday1(sender, args) {
    ValidRequiredField(sender, args);

    $year = $(".deliveryDate3");
    $month = $(".deliveryDate2");
    $date = $(".deliveryDate1");
    if (args.IsValid) {        
        var now = new Date();
        var difYear = now.getFullYear() - $year.val();
        var difMonth = now.getMonth() + 1 - $month.val();
        var difDate = now.getDate() - $date.val();

        if (difYear < 18
            || (difYear == 18 && difMonth < 0)
            || (difYear == 18 && difMonth == 0 && difDate <0)){
            args.IsValid = false;
        }

        var title_Tmp = $year.attr("title_Tmp");
        if (!title_Tmp) {      // save the title info into "title_Tmp" attribute
            var title = $year.attr("title");
            title_Tmp = title;
            $(".deliveryDate3").attr("title_Tmp", title);
        }
        if (args.IsValid) {
            title_Tmp = "";
        }
        
        $date.attr("title", title_Tmp);
        $month.attr("title", title_Tmp);
        $year.attr("title", title_Tmp);
    }
    $date.toggleClass("validFail", !args.IsValid);
    $month.toggleClass("validFail", !args.IsValid);
    $year.toggleClass("validFail", !args.IsValid);
}

function ValidBirthday(sender, args, isTmpCheck) {
    $year = $(".date_Year");
    $month = $(".date_Month");
    $date = $(".date_Day");
    args.IsValid = ($year.val() && $year.val().length > 0 && $month.val() && $month.val().length > 0 && $date.val() && $date.val().length > 0);
    if (args.IsValid) {
        var now = new Date();
        var difYear = now.getFullYear() - $year.val();
        var difMonth = now.getMonth() + 1 - $month.val();
        var difDate = now.getDate() - $date.val();

        if (difYear < 18
            || (difYear == 18 && difMonth < 0)
            || (difYear == 18 && difMonth == 0 && difDate < 0)) {
            args.IsValid = false;
        }

        var title_Tmp = $year.attr("title_Tmp");
        if (!title_Tmp) {      // save the title info into "title_Tmp" attribute
            var title = $year.attr("title");
            title_Tmp = title;
            $(".date_Year").attr("title_Tmp", title);
        }
        if (args.IsValid) {
            title_Tmp = "";
        }
        else {
            if (typeof (isTmpCheck) == "undefined" || isTmpCheck == null || isTmpCheck == false) {
                AlertInfo(title_Tmp);
            }
        }

        $date.attr("title", title_Tmp);
        $month.attr("title", title_Tmp);
        $year.attr("title", title_Tmp);
    }
    $date.toggleClass("validFail", !args.IsValid);
    $month.toggleClass("validFail", !args.IsValid);
    $year.toggleClass("validFail", !args.IsValid);
}

function IsCheckDelivery() {
    var checked = $(".panel4Right .chkDifferent input[type='checkbox']").attr("checked") || false;
    return checked;
}

/*  deliver address  */



function ValidDeliverInfo(sender, args, validFun) {
    args.IsValid = !IsCheckDelivery();
    if (args.IsValid) {
        SetValidStatus(sender, args);
    }
    else {
        eval(validFun + "(sender,args);");
    }
}

function ValidFirstName_Delivery(sender, args) {
    ValidDeliverInfo(sender, args, "ValidFirstName");
}

function ValidLastName_Delivery(sender, args) {
    ValidDeliverInfo(sender, args, "ValidLastName");
}

function ValidAddress_Delivery(sender, args) {
    ValidDeliverInfo(sender, args, "ValidAddress");
}

function ValidHouseNumber_Delivery(sender, args) {
    ValidDeliverInfo(sender, args, "ValidHouseNumber");
}

function ValidPostcode_Delivery(sender, args) {
    ValidDeliverInfo(sender, args, "ValidPostcode");
}

function ValidPostcode2_Delivery(sender, args) {
    args.isPostcode2 = true;
    ValidDeliverInfo(sender, args, "ValidPostcode2");
}

function ValidPlace_Delivery(sender, args) {
    ValidDeliverInfo(sender, args, "ValidPlace");
}

function ValidTelephone_Delivery(sender, args) {
    ValidDeliverInfo(sender, args, "ValidTelephone");
}

function toggleDeliveryInfo() {
    chkDelivery = $(".panel4Right input[type=checkbox]");
    if (chkDelivery.attr("checked") == true)
        $('.deliveryAddressContainer').show();
    else
        $('.deliveryAddressContainer').hide();
}

/*-------------- Step 4  End --------------*/


/*-------------- Step 5  Begin --------------*/
/*-------------- Step 5  End --------------*/

/*-------------- Step 6  Begin --------------*/
function IsCheckCreditCard() {
    var checked = $("#dvBank input[type='radio']").attr("checked") || false;
    return checked;
}

function ValidCardLapse(sender, args) {
    if (!IsCheckCreditCard())
        args.IsValid = true;
    else {
        var exp = /\d{2}/;
        args.IsValid = exp.test(args.Value);
        SetValidStatus(sender, args);
    }
}

function ValidRequiredField_Card(sender, args) {
    if (!IsCheckCreditCard())
        args.IsValid = true;
    else
        ValidRequiredField(sender, args);
}
/*-------------- Step 6  End --------------*/
