﻿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 = "<img src=\""+ productImageUrl+"\" width=30>";
    if(checked){
        if(currentNumItem > 2){
            //Cho phep so sánh tối đa 3 sản phẩm
            document.getElementById("compareItem_"+productId).checked = "";
            alert("Bạn chỉ có thể so sánh tối đa 3 sản phẩm\nDanh sách đã có đủ 3");
        }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,3);
        }
    }
}

//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("productImageHolder").innerHTML = "<img src=\"Images/Product/"+ imageName+"\">";
}

function saveProduct(productId)
{
    $("#save_" + productId).load("ProductSave.aspx?ProductId=" + productId);
}

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();
}

//Login home page
function loginAccount() {
    var pass = document.getElementById('password').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 == 1) {
               document.getElementById('login_success').style.display = 'block';
               document.getElementById('table_home_login').style.display = 'none';
	       document.getElementById('recommend_login').style.display = 'none';

           } else {
               alert('Sai mật khẩu hoặc email');
           }
       });
    }
}

//Dang ky email ban tin
function subcribeNewsletter() {
    var email = document.getElementById('email_subcribe').value;    
    //test value
    if (email.length < 4 || email.search('@') < 0 || email.search('\.') < 0) {
        alert('Vui lòng điền địa chỉ email');
    } else {
        document.getElementById('subcribing_status').innerHTML = ' ... đang xử lý<br>';
        $.post("SubcribeNewsletter.aspx", { email: email },
       function(data) {
           if (data == 1) {
               document.getElementById('subcribing_status').innerHTML = 'Email đã được ghi nhận. Cảm ơn<br>';
           } else {
               document.getElementById('subcribing_status').innerHTML = '';
               alert('Email này đã được đăng ký từ trước');
           }
       });
    }
}