//show menu
function showSub(groupId){
	var check = document.getElementById(groupId).style.display;	
	if(check =="none"){
		document.getElementById("icon_"+groupId).src = "/Images/Common/expand_on.gif";
		$("#"+groupId).slideDown("slow");
	}else{
		document.getElementById("icon_"+groupId).src = "/Images/Common/expand_off.gif";
		$("#"+groupId).slideUp("slow");
	}
}

function showMoreCat(groupId){
	var check = document.getElementById(groupId).style.display;	
	if(check =="none"){
	    document.getElementById(groupId+"_opt").innerHTML = "<a href=\"javascript:showMoreCat('" + groupId + "')\"><< Rút gọn</a>";
		$("#"+groupId).slideDown("slow");
	}else{
	    document.getElementById(groupId+"_opt").innerHTML = "<a href=\"javascript:showMoreCat('" + groupId + "')\">Xem thêm >></a>";
		$("#"+groupId).slideUp("slow");
	}
}

function addCompareList(productId){
    var checked = document.getElementById("compareItem_"+productId).checked;
    var currentList = document.getElementById("productCompareList").value;
    var currentNumItem = currentList.split(";").length - 2;
    var productImageUrl = document.getElementById("productHiddenImage_"+productId).value;
    var iconHtml = "<div class=\"img_compare_icon\"><img src=\""+ productImageUrl+"\" width=30></div>";
    if(checked){
        if(currentNumItem > 10){
            //Cho phep so sánh tối đa 10 sản phẩm
            document.getElementById("compareItem_"+productId).checked = "";
            alert("Bạn chỉ có thể so sánh tối đa 10 sản phẩm\nDanh sách đã có đủ 10");
        }else{
            document.getElementById("productCompareList").value = currentList + productId + ";";
            //Thêm tiếp ảnh
            nextContainer = currentNumItem + 1;
            document.getElementById("compareItemContain_"+ nextContainer).innerHTML = iconHtml;
            document.getElementById("productItemContain_"+ nextContainer).value = productId;
        }   
    }else{
        document.getElementById("productCompareList").value = currentList.replace(";" + productId + ";",";");
        //Xóa bỏ ảnh
        var containId = 0;
        for(var i=1;i<=currentNumItem;i++){
            var productContainer = document.getElementById("productItemContain_"+ i).value;
            if(productContainer != ""){
                if(productId == productContainer){
                    document.getElementById("compareItemContain_"+ i).innerHTML = "";
                    document.getElementById("productItemContain_"+ i).value = "";
                    containId = i;
                    break;
                }
            }
        }
        //Sắp xếp lại ảnh
        if(containId != 0){
            arrangeImageContainer(containId,4);
        }
    }
}

//Sắp xếp lại ảnh trong các container đảm bảo các ảnh từ 1 -> n
//startId là ID của container có ảnh bị remove, do đó cần được bù bằng ảnh khác nằm ở container lớn hơn gần nhất nếu có
//rồi lặp lại quá trình này cho container vừa bị lấy ảnh

function arrangeImageContainer(startId,maxNum){
    var nextStartId = 0;
    for(var i=startId+1;i<=maxNum;i++){
        var contentHtml = document.getElementById("compareItemContain_"+ i).innerHTML;
        var productContainer = document.getElementById("productItemContain_"+ i).value;
        if(contentHtml.length > 2)
        {
            document.getElementById("compareItemContain_"+ startId).innerHTML = contentHtml;
            document.getElementById("productItemContain_"+ startId).value = productContainer;
            document.getElementById("compareItemContain_"+ i).innerHTML = "";
            document.getElementById("productItemContain_"+ i).value = "";
            nextStartId = i;
            arrangeImageContainer(nextStartId,maxNum);
            break;
        }
    }
}

function goComparePage(categoryId){
    var compareList = document.getElementById("productCompareList").value;
    var numberProduct = compareList.split(';').length;
    if(numberProduct > 3){
        window.location.href = "ProductCompare.aspx?List=" + compareList + "&CategoryId=" + categoryId;
       // alert(numberProduct);
    }else{
        alert("Bạn cần chọn ít nhất 2 sản phẩm để so sánh\nChọn sản phẩm bằng tích ô dưới ảnh sản phẩm");
    }
}

function showProductImage(imageName)
{
    document.getElementById("pro_big").innerHTML = "<img src=\"Images/Product/"+ imageName+"\">";
}

function saveProduct(productId)
{
    $("#save_" + productId).load("ProductSave.aspx?ProductId=" + productId);
}

function loadReviewForm(id){
    document.getElementById('pro_content_review').innerHTML = "<img src=/Images/loading_icon_small.gif> ... đang tải form";
    $("#pro_content_review").load("/ProductReviewForm.aspx?ProductID="+id);
}

function SendProductReview(id){
    $.post("ProductReviewForm.aspx",{
                    ProductID:id
                    , review_rate : document.getElementById("review_rate_select").value
                    , RatingTitleTextBox : document.getElementById("RatingTitleTextBox").value
                    , RatingContentTextArea : document.getElementById("RatingContentTextArea").value
                  },function(data){
            document.getElementById('pro_content_review').innerHTML = "<p>Cảm ơn bạn đã gửi ý kiến về sản phẩm. Chúng tôi sẽ kiểm duyệt và cho nội dung hiển thị trong thời gian sớm nhất.</p>";
        });
}

function popUpProductGallery(url) 
{
     var width  = 600;
     var height = 550;
     var left   = (screen.width  - width)/2;
     var top    = (screen.height - height)/2;
     var params = 'width='+width+', height='+height;
     params += ', top='+top+', left='+left;
     params += ', directories=no';
     params += ', location=no';
     params += ', menubar=no';
     params += ', resizable=yes';
     params += ', scrollbars=yes';
     params += ', status=no';
     params += ', toolbar=no';
     newwin=window.open(url,'productGalleryWindow', params);
     if (window.focus) {newwin.focus()}
     return false;
 }

 //08-12-2008
 function show(elid) {
     var checkvisible = document.getElementById(elid).style.display;
     if (checkvisible == "none") {
         document.getElementById(elid).style.display = "block";
     } else {
         document.getElementById(elid).style.display = "none";
     }
 }

 function choosePaymentOption(option) {
     var checked = document.getElementById(option).checked;
     var instId = document.getElementById(option).value;
     if (checked) {
         document.getElementById("selectPaymentMethod").value = instId;
         
        //Hide other options
         for (var i in paymentOptionList) {
             if (option != paymentOptionList[i]) {
                 document.getElementById("pay_" + paymentOptionList[i]).style.display = "none";
                 document.getElementById(paymentOptionList[i]).checked = false;
             }
         }
         //show the instruction
         document.getElementById("block_" + instId).style.display = "block";
     } else {
        document.getElementById("selectPaymentMethod").value = "";
        //Uncheck, show other options again
        for (var i in paymentOptionList) {
             document.getElementById("pay_" + paymentOptionList[i]).style.display = "block";
             document.getElementById(paymentOptionList[i]).checked = false;
             var elt = document.getElementById(paymentOptionList[i]).value;
             document.getElementById("block_"+elt).style.display = "none";
         }
     }
 }
 
 function trackProduct(productId, option){
    if(option == "click"){
        $.get("ProductTrack.aspx", { action: "click", productId: productId } );
    }else{
        $.get("ProductTrack.aspx", { action: "visit", productId: productId } );
    }
 }
 
 function trackCategory(categoryId){
    $.get("ProductTrack.aspx", { action: "category", categoryId: categoryId } );
 }
 
 function showProductTap(id,tapId){
    var checkdisplay = document.getElementById(id).style.display;
    var lastOpen = document.getElementById('lastOpenTap').value;
    var lastOpenId = document.getElementById('lastOpenTapId').value;
    //alert(lastOpen + " = " + lastOpenId);
    if(checkdisplay =="none"){
        //Close last open tap if exist       
        document.getElementById(lastOpen).style.display = "none";
        document.getElementById(lastOpenId).className = "productTapUnselect";
        //Open new
        document.getElementById(id).style.display = "block";
        document.getElementById(tapId).className = "productTapselect";
        document.getElementById('lastOpenTap').value = id;
        document.getElementById('lastOpenTapId').value = tapId;
    }
}

function showProductHistory(){
    $("#showHistoryProductView").load("ProductViewHistory.aspx");    
}

function chooseShippingOption(method_id){
    document.getElementById('selectShippingMethod').value = method_id;
}

function selectDistrict(province_id){
    var last = document.getElementById('last_province').value;
    //if(last > 0){
        document.getElementById('district_select_'+last).style.display = "none";
    //}
    document.getElementById('district_select_'+province_id).style.display = "block";
    document.getElementById('last_province').value = province_id;
}

function chooseThisDistrict(dis_id) {
    document.getElementById('select_district').value = dis_id;
}

//pop-up
function openPopUp(url) {
    var winW = document.documentElement.clientWidth;
    var winH = document.documentElement.clientHeight;
    var topH = 30;
    var left = winW / 2 - 350;
    window.open(url, "new_window", "width=700,height=600,status=no,scrollbars=yes,left="+left+",top="+topH+"");
}

function closePopUp() {
    window.close();
}

function showMap(map_name) {
    if (map_name.length > 1) {
        var url = "Media/Map" + map_name + ".html";
        openPopUp(url);
    }
}

function verifyEmail(email){
    if(email.indexOf("@") < 0){
        alert("Vui lòng nhập địa chỉ email");
        return false;
    }else{
        return true;
    }
}
//Dang ky email ban tin
function subcribeNewsletter() {
    var email = document.getElementById('email_subcribe').value;
    //test value
    if (email.length < 6) {
        alert('Vui lòng điền địa chỉ email');
    } else {
        if(verifyEmail(email)){
            document.getElementById('subcribing_status').innerHTML = ' ... đang xử lý<br>';
            $.post("SubcribeNewsletter.aspx", { email: email },
               function(data) {
                   if (data == 1) {
                       document.getElementById('subcribing_status').innerHTML = "<b style='color:red;'>Email đã được ghi nhận. Cảm ơn quý khách.</b><br>";
                   } else {
                       document.getElementById('subcribing_status').innerHTML = '';
                       alert('Xin lỗi. Email này đã được đăng ký từ trước');
                   }
               });
        }
    }
}

function loadProductFeatured(type) {
    var array_tap = [ 'bestseller', 'new'];
    for (var i = 0; i < array_tap.length; i++) {
    elevalue = array_tap[i]
    if (type != elevalue) {
        document.getElementById(elevalue + "_product").className = '';
    }
    }
    document.getElementById(type + "_product").className = 'home_pro_bar_menu_select';
    //get product
    /*
    $.get("FeaturedProAjax.aspx", { type: type }, function(data) {
        document.getElementById('homeLoadProduct').innerHTML = data;
    });
    */
    document.getElementById('homeLoadProduct').innerHTML = "<img src=\"/Images/loading-small.gif\" /> Nội dung đang tải về ..."
    var content = "<iframe frameborder=0 width=950px height=180px align=middle ";
    content += " src=\"FeaturedProAjax.aspx?type="+type+"\"></iframe>";
    document.getElementById('homeLoadProduct').innerHTML = content;
}

//chuyen doi cac cum trong featured Pro
function topProductNext() {
    var max_value = document.getElementById('max_top_product_row').value;
    var current_value = document.getElementById('current_top_product_row').value;
    var next_value = parseInt(current_value) + 1;
    if (max_value < next_value) next_value = 1; //back to the first row
    document.getElementById('top_group_' + next_value).style.display = 'block';
    document.getElementById('top_group_' + current_value).style.display = 'none';
    document.getElementById('current_top_product_row').value = next_value;
}

function topProductBack() {
    var max_value = document.getElementById('max_top_product_row').value;
    var current_value = document.getElementById('current_top_product_row').value;
    var next_value = parseInt(current_value) - 1;
    if (next_value < 1) next_value = max_value; //back to the first row
    document.getElementById('top_group_' + next_value).style.display = 'block';
    document.getElementById('top_group_' + current_value).style.display = 'none';
    document.getElementById('current_top_product_row').value = next_value;
}

//Login home page
function loginAccount() {
    var pass = document.getElementById('pass_login').value;
    var email = document.getElementById('email_login').value;
    //test value
    if (pass.length < 3 || email.length < 4) {
        alert('Vui lòng điền thông tin đăng nhập');
    } else {
        document.getElementById('sending_login').innerHTML = ' ... đang đăng nhập<br>';
        $.post("Login2.aspx", { password: pass, email: email },
       function(data) {
           document.getElementById('sending_login').innerHTML = '';
           if (data != 2) {
               document.getElementById('login_success').style.display = 'block';
               document.getElementById('form_home_login').style.display = 'none';
               document.getElementById('login_name').innerHTML = "<b>" + data + "</b>";
               
           } else {
               alert('Sai mật khẩu hoặc email');
           }
       });
    }
}