﻿function addToListLayer(flyId) {
    var checked = document.getElementById("id_" + flyId).checked;
    var currentList = document.getElementById("currentSelectFlyer").value;
    var currentNumItem = currentList.split(";").length - 2;
    if (checked) {
        if (currentNumItem > 2) {
            document.getElementById("id_" + flyId).value;
            alert("Tối đa chỉ dự đoán 3 người");
        }
        else {
            document.getElementById("row_" + flyId).style.backgroundColor = "#FFCC00";
            document.getElementById("currentSelectFlyer").value = currentList + flyId + ";";
        }

    } else {
        document.getElementById("row_" + flyId).style.backgroundColor = "";
        document.getElementById("currentSelectFlyer").value = currentList.replace(";" + flyId + ";", ";");
    }
    //alert(document.getElementById("listPageId").value);
}

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=20>";
    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");
        }
        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 khỏi compare
        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 showAboutImage(imageName) {
    document.getElementById("aboutImageHolder").innerHTML = "<img src=\"/Images/Intro/" + imageName + "\">";
}

function showFlyerImage(imageName) {
    document.getElementById("flyerImageHolder").innerHTML = "<img src=\"/Images/FlyGallery/" + imageName + "\">";
}

function saveProduct(productId)
{
    $("#save_" + productId).load("ProductSave.aspx?ProductId=" + productId);
}

function popUpProductGallery(url) 
{
     var width  = 600;
     var height = 600;
     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;
 }

 function popUpLiveChat(url) {
     var width = 400;
     var height = 400;
     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, 'popUpLiveChatWindow', 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";
         }
     }
 }
 // Ghi lại lượt click sản phẩm vào Database
 function trackProduct(productId, option){
    if (option == "click"){
        $.get("ProductTrack.aspx", { action: "click", productId: productId } );
    }else{
        $.get("ProductTrack.aspx", { action: "visit", productId: productId } );
    }
  }

 // Ghi lại lượt click danh mục vào Database
 function trackCategory(categoryId){
    $.get("ProductTrack.aspx", { action: "category", categoryId: categoryId } );
 }

 // Luu luot click ban tin
 function trackNews(newsId, option) {
   $.get("/NewsTrack.aspx", { action: "click", newsId: newsId } );

}
// Luu luot click ban tin ho tro
function trackSupport(blogsId, option) {
    $.get("/SupportTrack.aspx", { action: "click", blogsId: blogsId } );
}

// Luu luot click ban tin tuyen dung
function trackCarrier(carrierId, option) {
    $.get("/CarrierTrack.aspx", { action: "click", carrierId: carrierId } );
}

// Luu luot click nguoi choi Fly With Us
function trackFlyNow(flyID, option) {
    $.get("/TrackFly.aspx", { action: "click", flyID: flyID });
}
 
 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"){
        //Đóng lại Tab cuối cùng khi thoát       
        document.getElementById(lastOpen).style.display = "none";
        document.getElementById(lastOpenId).className = "productTapUnselect";
        //Mở Tab mới
        document.getElementById(id).style.display = "block";
        document.getElementById(tapId).className = "productTapSelect";
        document.getElementById('lastOpenTap').value = id;
        document.getElementById('lastOpenTapId').value = tapId;
    }
}

// Những sản phẩm đã xem qua
function showProductHistory(){
    $("#showHistoryProductView").load("ProductViewHistory.aspx");    
}

// Chọn hình thức thanh toán
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;
}

//Hiển thị cửa sổ pop-up
function openPopUp(url) {
    var winW = document.documentElement.clientWidth;
    var winH = document.documentElement.clientHeight;
    var width = 600;
    var height = 600;
    var leftW = (winW - width) / 2;
    var topH = (winH - height) / 2;
    window.open(url, "new_window", "width=" + width + ", height=" + height  + ", status=no, scrollbars=yes, left=" + leftW + ", top=" + topH + "");
}
// Đóng cửa sổ pop-up
function closePopUp() {
    window.close();
}

//Đăng nhập tài khoản ở trang chủ
function loginAccount() {
    var pass = document.getElementById('password').value;
    var email = document.getElementById('email_login').value;
    //Giá trị thử nghiệm
    if (pass.length < 3 || email.length < 7) {
        alert('Vui lòng điền thông tin đăng nhập...');
    } else {
        document.getElementById('sending_login').innerHTML = '...đang đăng nhập';
        $.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('login_start').style.display = 'none';
	           document.getElementById('recommend_login').style.display = 'none';
           } else {
               alert('Sai mật khẩu hoặc email. Bạn hãy kiểm tra lại');
           }
       });
    }
}

//Đăng ký EmailSubcribe trang chủ
function subcribeNewsletter() {
    var email = document.getElementById('email_subcribe').value;    
    //Giá trị thử nghiệm
    if (email.length < 8 || email.search('@') < 0 || email.search('\.') < 0) {
        alert('Vui lòng điền địa chỉ email');
    } else {
        document.getElementById('subcribing_status').innerHTML = '...đang xử lý';
        $.post("SubcribeNewsletter.aspx", { email: email },
       function(data) {
           if (data == 1) {
               document.getElementById('subcribing_status').innerHTML = 'Đã ghi nhận Email...';
           } else {
               document.getElementById('subcribing_status').innerHTML = '';
               alert('Email này đã được đăng ký từ trước');
           }
       });
    }
}
function IniRunDevil(ctrl) {
    var parent = document.getElementById(ctrl);
    var arrChild = new Array();

    var allChild = parent.getElementsByTagName("*");

    for (var i = 0; i < allChild.length; i++) {
        if (allChild[i].className == 'newsDevil') {
            arrChild[arrChild.length] = allChild[i];
        }
    }
    if (arrChild.length > 0) {
        arrChild[0].style.display = 'block';
        for (var i = 1; i < arrChild.length; i++) {
            arrChild[1].style.display = 'none';
        }
    }
}

function RunDevil(ctrl) {
    var parent = document.getElementById(ctrl);
    var arrChild = new Array();
    var allChild = parent.getElementsByTagName("*");

    for (var i = 0; i < allChild.length; i++) {
        if (allChild[i].className == 'newsDevil') {
            arrChild[arrChild.length] = allChild[i];
        }
    }

    for (var i = 0; i < arrChild.length; i++) {
        if (arrChild[i].style.display == 'block') {
            arrChild[i].style.display = 'none';
            if (i < arrChild.length - 1) {
                arrChild[i + 1].style.display = 'block';
            }
            else {
                arrChild[0].style.display = 'block';
            }
            break;
        }
    }
}

var product_url = window.location.href;
function share_facebook() { window.open('http://www.facebook.com/share.php?u=' + product_url); }
function share_twitter() { window.open('http://twitter.com/home?status=' + product_url); }
function share_google() { window.open('http://www.google.com/bookmarks/mark?op=edit&bkmk=' + product_url); }
function share_buzz() { window.open('http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&u=' + product_url); }
function share_delicious() { window.open('http://www.delicious.com/save?url=' + product_url); }
function share_digg() {window.open('http://digg.com/submit?url=' + product_url); }



