﻿(function ($) {
    $.setVisible = function (control, visible) {
        try {
            if (visible) {
                $('#' + control).attr("style", "display:'';");
            }
            else {
                $('#' + control).attr('style', 'display:none;');
            }
        } catch (err) {
            if (visible) {
                control.attr("style", "display:'';");
            }
            else {
                control.attr('style', 'display:none;');
            }
        }
    };

    $.getVisible = function(control) {
        var temp = $('#' + control).attr('style');
        if (temp == "display:none" || temp == "DISPLAY: none") {
            return false;
        }
        else {
            return true;
        }
    };

    $.getChecked = function (control) {
        var isChecked = $('#' + control).attr('checked');
        if (!isChecked) {
            return false;
        }
        else {
            return true;
        }
    };
})(jQuery);

function ValidateCheckbox(control, message) {
	var isOk = false;
	jQuery(document).ready(function($) {
		isOk = $.getChecked(control);
	});

	if (isOk == false) {
		alert(message);
		return false;
	}
	else {
		return true;
	}
}

function AddToCart(url, productId) {
    //alert(productId);
    jQuery(document).ready(function ($) {
        if (this.timer) clearTimeout(this.timer);
        this.timer = setTimeout(function () {
            $.ajax({
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                url: '/Default.aspx/AddToCart',
                data: "{\"productID\": \"" + productId + "\" }",
                success: function (data) {
                    alert('Sản phẩm của bạn đã được đưa vào giỏ hàng');
                    UpdateMiniCart();
                    //document.location.href = url;
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    //alert(xhr.status);
                    //alert(thrownError);
                }
            });
        }, 1000);
    });
}

function UpdateMiniCart() {
    jQuery(document).ready(function ($) {
        //alert($('#mini-cart strong').html());
        var item = $('#mini-cart strong');
        var count = parseInt(item.html()) + 1;
        item.html(count + ' sản phẩm');
    });
}

function CheckBoxListOne(rpt) {
    $(document).ready(function () {
        $('#' + rpt + ' input').click(function () {
            $('#' + rpt + ' input:checked').each(function () {
                $(this).attr('checked', false);
            });

            $(this).attr('checked', true);
        });
    });
}
