﻿jQuery(document).ready(function() {
    var guid = jQuery("#typeGuid").val();
    if (isNoBlank(guid)) {
        jQuery("#ckbAll").attr("checked", "checked");
        ckbAll_Click();
    }
    else {
        cbkSelect_Click();
    }
    jQuery(":checkbox[name*='cbkSelect']").click(cbkSelect_Click);
    jQuery("#ckbAll").click(ckbAll_Click);
    controlHead();
});

function ckbAll_Click() {
    if (jQuery("#ckbAll").is(":checked")) {
        jQuery(":checkbox[name*='cbkSelect']").attr("checked", "checked");
        var price = getCheckedPrice();
        var pkPrice = jQuery("#ckbAll").val();
        setSelectAllStyle(price, pkPrice);
    }
    else {
        jQuery(":checkbox[name*='cbkSelect']").attr("checked", "");
        setNormalSelectStyle(0.00);
    }
   
}

function setSelectAllStyle(price, pkprice) {
    jQuery("#pkPrice").text("$" + parseFloat(pkprice).toFixed(2)).show();
    jQuery("#totalPrice").text("$" + parseFloat(price).toFixed(2)).css("text-decoration", "line-through").css("color", "#666");
   
}

function setNormalSelectStyle(price) {
    jQuery("#pkPrice").text("$0.00").hide();
    jQuery("#totalPrice").text("$" + parseFloat(price).toFixed(2)).css("text-decoration", "none").css("color", "red");
}

function controlHead() {
    var prof = jQuery("#hfProfession").val();
    jQuery("#mainNav a>img").hover(function() {
        var imgName = jQuery(this).attr('bimg');
        if (isNoBlank(imgName)) {
            var srcimg = '/images/item-' + imgName + '.jpg';
            jQuery(this).attr('src', srcimg);
        }
    },
            function() {
                var imgName = jQuery(this).attr('aimg');
                var elmName = jQuery(this).attr('name');
                if (isNoBlank(elmName) && isNoBlank(prof)) {

                    if (elmName.toLowerCase() == prof.toLowerCase()) {
                        return;
                    }
                }
                if (isNoBlank(imgName)) {
                    var srcimg = '/images/item-' + imgName + '.jpg';
                    jQuery(this).attr('src', srcimg);
                }
            });


    if (isNoBlank(prof)) {
        var imgName = jQuery("#mainNav a>img[name=" + prof + "]").attr('bimg');
        if (isNoBlank(imgName)) {
            var srcimg = '/images/item-' + imgName + '.jpg';
            jQuery("#mainNav a>img[name=" + prof + "]").attr('src', srcimg);
        }
    }
}
function cbkSelect_Click() {
    var price = getCheckedPrice();
    var pkPrice = jQuery("#ckbAll").val();
    if (isSelectAll()) {
        jQuery("#ckbAll").attr("checked", "checked");
        setSelectAllStyle(price, pkPrice);
    }
    else {
        jQuery("#ckbAll").attr("checked", "");
        setNormalSelectStyle(price);
    }
}

function displayImage(id) {
    jQuery("#" + id).show();
}
function hideImage(id) {
    jQuery("#" + id).hide();
}

function isNoBlank(str) {
    var notBlank = true;
    if (str == undefined || str == null) {
        notBlank = false;
    }
    else {
        var reg = new RegExp(/\S+/);
        notBlank = reg.test(str);
    }

    return notBlank;
}

function formatNum(src, pos) {
    return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos);
}

function getCheckedPrice() {
    var totalPrice = 0.00;
    jQuery("input:checked[name*='cbkSelect']").each(function() {  
        var value = jQuery(this).val();
        if (isNoBlank(value)) {
            var price = parseFloat(value);
            if (isNaN(price) == false) {
                totalPrice += price;
            }
        }
    });
    return totalPrice;
}

function isSelectAll() {
    var all = jQuery("input[name*='cbkSelect']").length;
    var slt = jQuery("input:checked[name*='cbkSelect']").length;
    if (all == slt) {
        return true;
    }
    return false;
}
