﻿var oldclassName;
//$("img").lazyload();
function mouseOver(source) {
    oldclassName = source.className;
    source.className = 'ProductsHover';
}
function mouseOut(source) {
    source.className = oldclassName;
}

function HeadOver(obj, imgName) {
    obj.src = '/Images/' + imgName;
}

function HeadOut(obj, imgName) {
    obj.src = '/Images/' + imgName;
}

function Redirect(url) {
    blockForm();
    location.href = url;
}
function NextmouseOver(source, isOver) {
    if (isOver == true)
        source.src = '/Images/Product/NextHover.gif';
    else
        source.src = '/Images/Product/Next.gif';
}

function PrevmouseOver(source, isOver) {
    if (isOver == true)
        source.src = '/Images/Product/PreviousHover.gif';
    else
        source.src = '/Images/Product/Previous.gif';
}

function LoginNext(obj, evt, t) {
    var e = evt || window.event;
    if (evt.keyCode == 9 || evt.keyCode == 13) {
        if (obj.type == 'text' && obj.value == "") {
            alert('請輸入帳號!!');
        } else if (obj.type == 'password' && obj.value == "") {
            alert('請輸入密碼!!');
        } else if (document.forms[0].elements[t].type == "password") {
            document.forms[0].elements[t].focus();
        } else {
            document.forms[0].elements[t].onclick();
        }
        return false;
    }
}

function centerMe(obj) {
    obj.style.margin = Math.floor((obj.parentNode.clientHeight - obj.clientHeight) / 2) + 'px '
    + Math.floor((obj.parentNode.clientWidth - obj.clientWidth) / 2) + 'px';
}
var idle_Time = new Date();
function IdleTime() {
    var myObject = new Object();
    var nowTime = new Date();
    var dif = nowTime.getTime() - idle_Time.getTime();
    myObject.hours = Math.floor(dif / 1000 / 60 / 60);
    dif = dif - myObject.hours * 1000 * 60 * 60
    myObject.minutes = Math.floor(dif / 1000 / 60);
    dif = dif - myObject.minutes * 1000 * 60
    myObject.seconds = Math.floor(dif / 1000);
    if (myObject.minutes == 0 && myObject.hours== 0 )
        $('#IdleTime').text('閒置時間：' + myObject.seconds + "秒");
    else if (myObject.minutes > 0 && myObject.hours == 0)
        $('#IdleTime').text('閒置時間：' + myObject.minutes + "分" + myObject.seconds + "秒");
    else
        $('#IdleTime').text('閒置時間：' + myObject.hours + "時" + myObject.minutes + "分" + myObject.seconds + "秒");
    return myObject;
    //setTimeout("IdleTime()", 1000);
}

function TimeOut() {
    setTimeout("TimeOutFunction()", 1000 * 60 * 120 + 1000);
}
function TimeOutFunction() {
    var nowDate = new Date();
    $.post("/Member/TimeOut.ashx", { timer: nowDate.getDate().toString() },
        function(data) {
            alert('操作逾時,請重新登入!!');
            location.href = '/member/login.aspx';
        });
}
function TextChange(obj, isChange) {
    obj.style.textDecorationUnderline = isChange;
}

function ShowHistoryOrder(obj, pid) {
    var dataPanel = document.getElementById('DataPanel');
    if (pid == null || pid == '' || pid == "-1") {
        dataPanel.style.display = 'none';
        dataPanel.innerHTML = '';
    } else {
        dataPanel.innerHTML = "<div align='center' style='background-color:White;color:Black;width:100px;'><br/><img src='/images/loading.gif'/><br/><br/>資料處理中....</div>";
        dataPanel.style.display = '';
        dataPanel.style.top = posY(obj) - 50;
        dataPanel.style.left = posX(obj) + 60;
        $.post("/member/HistoryOrder.ashx", { PID: pid }, function(data) { dataPanel.innerHTML = data; });
    }

}

function posX(elmt) {
    //var elmt = document.getElementById(objID);
    var x = 0;
    //繞行 offsetParents
    for (var e = elmt; e; e = e.offsetParent) {
        //把 offsetLeft 值加總
        x += e.offsetLeft;
    }
    //繞行至 document.body
    for (e = elmt.parentNode; e && e != document.body; e = e.parentNode) {
        //減去捲軸值
        if (e.scrollLeft) x -= e.scrollLeft;
    }
    return x;
}

function posY(elmt) {
    //var elmt = document.getElementById(objID);
    var y = 0;
    //繞行 offsetParents
    for (var e = elmt; e; e = e.offsetParent) {
        //把 offsetTop 值加總
        y += e.offsetTop;
    }
    //繞行至 document.body
    for (e = elmt.parentNode; e && e != document.body; e = e.parentNode) {
        //減去捲軸值
        if (e.scrollTop) y -= e.scrollTop;
    }
    return y;
}

function LoadMenu(obj, cid, company) {
    $.post("LoadMenu.ashx", { CID: cid, Company: company, obj: obj }, function(data) { $('#tableMenu_' + obj).html(data); });
}

function DealersEdit(seq) {
    location.href = '/member/DealersEdit.aspx?seq=' + seq;
}

function DealersDel(seq, ccode, email) {
    if (confirm("要刪除此聯絡人嗎?") == false) return;
    $.post('/member/DealersDel.ashx', { SEQ: seq, CCODE: ccode, Email: email },
        function(data) {
            if (data == "") {
                alert("刪除成功!!");
            } else {
                alert("刪除失敗!!");
            }
            location.href = '/member/DealersList.aspx?t=' + new Date().toDateString();
        });

}

function LoadImage(obj, pid, index, pp, pType) {
    //$(document).ready();
    $.post('/ProdImage.ashx', { PID: pid, index: index, pType: pType }, function(data) { obj.src = data; });
}

function LoadImages(pids, index, ptype) {
    $(document).ready(function() {
        //setTimeout("LoadPicture('" + pids + "','" + index + "','" + ptype + "');", 100);//Math.floor(Math.random() * (1000 - 100 + 1)) + 100);
        $.post('/ProdPictures.ashx', { PIDS: pids, index: index, pType: ptype == null ? "" : ptype }, function(data) {
            var imgs = data.toString().split(',');
            for (var i = 0; i < imgs.length; i++) {
                if (ptype == null || ptype == "")
                    $('#ProdImg_' + i).attr('src', imgs[i]);
                else if (ptype == 'n')
                    $('#ProdImg_' + i + 'n').attr('src', imgs[i]);
                else if (ptype == 'p')
                    $('#ProdImg_' + i + 'p').attr('src', imgs[i]);
                else if (ptype == 'w')
                    $('#ProdImg_' + i + 'w').attr('src', imgs[i]);
                else if (ptype == 's')
                    $('#ProdImg_' + i + 's').attr('src', imgs[i]);
                else if (ptype == 'nn')
                    $('#ProdImg_' + i + 'nn').attr('src', imgs[i]);
            }
        });
    });
}

function LoadGoodImages(pids, index, ptype, count) {
    $(document).ready(function () {
        //setTimeout("LoadPicture('" + pids + "','" + index + "','" + ptype + "');", 100);//Math.floor(Math.random() * (1000 - 100 + 1)) + 100);
        $.post('/ProdPictures.ashx', { PIDS: pids, index: index, pType: ptype == null ? "" : ptype }, function (data) {
            var imgs = data.toString().split(',');
            for (var i = 0; i < imgs.length; i++) {
                if (ptype == null || ptype == "") 
                    $('#ProdImg_' + (i + count)).attr('src', imgs[i]);
                else if (ptype == 'n')
                    $('#ProdImg_' + (i + count) + 'n').attr('src', imgs[i]);
                else if (ptype == 'p')
                    $('#ProdImg_' + (i + count) + 'p').attr('src', imgs[i]);
                else if (ptype == 'w')
                    $('#ProdImg_' + (i + count) + 'w').attr('src', imgs[i]);
                else if (ptype == 's')
                    $('#ProdImg_' + (i + count) + 's').attr('src', imgs[i]);
                else if (ptype == 'nn')
                    $('#ProdImg_' + (i + count) + 'nn').attr('src', imgs[i]);
                else
                    $('#ProdImg_' + (i + count)).attr('src', imgs[i]);
            }
        });
    });
}

function ok() { }
function LoadPicture(pids, index, ptype) {
    //alert(ptype);
    $.post('/ProdPictures.ashx', { PIDS: pids, index: index, pType: ptype == null ? "" : ptype }, function(data) {
        var imgs = data.toString().split(',');
        for (var i = 0; i < imgs.length; i++) {
            if (ptype == null || ptype == "")
                $('#ProdImg_' + i).attr('src', imgs[i]);
            else if (ptype == 'n')
                $('#ProdImg_' + i + 'n').attr('src', imgs[i]);
            else if (ptype == 'p')
                $('#ProdImg_' + i + 'p').attr('src', imgs[i]);
            else if (ptype == 'w')
                $('#ProdImg_' + i + 'w').attr('src', imgs[i]);
            else if (ptype == 's')
                $('#ProdImg_' + i + 's').attr('src', imgs[i]);
        }
    });
}

function Checkout(clientID) {
    //blockForm("結帳中...");
    $('#' + clientID).click();
}

function blockForm(text) {
    $.blockUI({
        message: "<div><img src='/images/loading.gif' border='0'/><br><br>" + ((text == null || text == "") ? "資料載入中" : text) + ".....</div>",
        css: { border: 'none', padding: '15px', backgroundColor: '#555555', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: 0.8, color: '#fff' },
        overlayCSS: { backgroundColor: '#eeeeee', opacity: 0.5 }
    });
}

//function block123() {
//    $.blockUI({
//        message: "<div><img src='/images/loading.gif' border='0'/><br><br>" + ((text == null || text == "") ? "資料載入中" : text) + ".....</div>",
//        css: { border: 'none', padding: '15px', backgroundColor: '#555555', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: 0.8, color: '#fff' },
//        overlayCSS: { backgroundColor: '#eeeeee', opacity: 0.5 }
//    });
//    $("#dialog").dialog();
//}

function GetProductList(cid, page, isOnChange) {
    var barData, productData;
    $('#ProductData').html('');
    location.hash = "#TOP";
    $.post("GetCategoryBar.ashx", { CID: cid },
        function(data) {
            if (data == null || data == "") {
                location.href = "ProductList.aspx?t=" + Date().getDate().toString();
                return;
            }
            $('#CategoryBar').html(data);
        });
    $.post("GetProductList.ashx", { CID: cid, page: page },
        function(data) {
            if (data == null || data == "") {
                location.href = "ProductList.aspx?t=" + Date().getDate().toString();
                return;
            }
            $('#ProductData').html(data);
        });

    $.post("GetProductPageBar.ashx", { CID: cid, page: page },
        function(data) {
            if (data == null || data == "") {
                location.href = "ProductList.aspx?t=" + Date().getDate().toString();
                return;
            }
            if (data != "0")
                $('#PageBar').html(data);
            else
                $('#PageBar').html('');
        });
}

function Product(PID, categoryID, page) {
    location.hash = "#TOP";
    $('#PageBar').html('');
    $('#ProductData').html('');
    $.post("GetCategoryBar.ashx", { categoryID: categoryID },
        function(data) {
            if (data == null || data == "") {
                location.href = "ProductList.aspx?t=" + Date().getDate().toString();
                return;
            }
            $('#CategoryBar').html(data);
        });

    $.post("GetProduct.ashx", { PID: PID, categoryID: categoryID, page: page },
        function(data) {
            if (data == null || data == "") {
                location.href = "ProductList.aspx?t=" + Date().getDate().toString();
                return;
            }
            $('#ProductData').html(data);
        });
}

function PageOnChange(obj, categorID, isLogin) {
    //GetProductList(categorID, $('#' + obj.id + ' :selected').val());
    ShowProducts(categorID, $('#' + obj.id + ' :selected').val(), isLogin);
}

function ShowProducts(cid, page, isLogin, url) {
    blockForm();
    var path = "";
    if (url == null || url == "")
        url = "/ListProducts.aspx";
    if (isLogin == true)
        path = "/Member";
    location.href = path + url + "?CID=" + cid + "&Page=" + page;

}

function ShowProduct(pid, cid, page, isLogin, url) {
    var path = "";
    blockForm();
    if (url == null || url == "")
        url = "/Product.aspx";
    if (isLogin == true)
        path = "/Member";
    pid = escape(pid);
    pid = pid.replace("+", "%2B").replace("/", "%2F");
    location.href = path + url + "?pid=" + pid + "&CID=" + cid + "&Page=" + page;
}

function GetProductImage(imgobj, pid, type) {
    $.post("GetProductPicture.ashx", { PID: pid, TYPE: type },
        function(data) { $('#' + imgobj).html(data); });
}

function ShoppingAdd(isProduct, qtyID, page, cid, iCode, clientID, type) {
    TimeOut();
    if (!IsNumeric($('#' + qtyID).val())) {
        alert('訂購數量請輸入數字!!');
        return;
    }
    //blockForm("商品加入購物車中....");
    blockForm();
    var t = type.toString().split(',');
    $.post("/Member/AddShoppingCar.aspx", { CID: cid, ICode: iCode, QTY: $('#' + qtyID).val(), Type: t[0], Bon: t[1] },
        function(data) {
            if (data != null && data != "") {
                if (data == "售完")
                    alert("這個商品巳經售完!!");
                else if (data == "大於數量")
                    alert("這個商品[" + iCode + "]購買數量不可大於促銷數量!!");
                else
                    alert("這個商品[" + data + "] 加入購物車!!");
            } else {
                alert("這個商品[" + iCode + "] 加入購物車失敗!!請稍後再試!!");
            }
            $('#SCarList').css('display', 'none');
            //$.unblockUI();
            var date = new Date();
            var url = clientID.toString().split('#');

            if (url.length > 1 && type != '促銷,1') {
                location.href = url[0] + "&t=" + date.getTime().toString() + "#" + url[1];
            } else if (url.length > 1 && type == '促銷,1') {
                location.href = url[0] + "?t=" + date.getTime().toString() + "#" + url[1];
            } else {
                location.href = url[0] + "&t=" + date.getTime().toString();
            }

        });
}
function ShoppingDel(isProduct, itemUID, iCode, cid, page, clientID) {
    TimeOut();
    blockForm("商品從購物車中移除....");
    $.post("/Member/DelShoppingCar.aspx", { ICode: iCode, UID: itemUID },
        function(data) {
            if (data != null && data != "") {
                alert("這個商品[" + data + "] 已經從購物車中移除!!");
                /*if (clientID != null && clientID != "")
                $('#' + clientID).click();*/
                location.href = clientID;
            }
            /*if (isProduct == true)
            location.href = "/Member/Product.aspx?pid=" + iCode + "&CID=" + cid + "&Page=" + page;
            else
            location.href = "/Member/ListProducts.aspx?CID=" + cid + "&Page=" + page;*/
        });
}

function IsNumeric(sText) {
    var ValidChars = "0123456789";
    for (i = 0; i < sText.length; i++) if (ValidChars.indexOf(sText.charAt(i)) == -1) return false;
    return true;
}

function ODetails(num) {
    location.href = '/Member/OrderDetails.aspx?OrderID=' + num;
}
function ODelete(oid) {
    TimeOut();
    blockForm("訂單取消中....");
    $.post("/Member/OredrCancel.ashx", { oid: oid },
        function(data) {
            var t = new Date();
            alert(data);
            location.href = '/member/OrderList.aspx?t=' + t.getTime().toString();
        });
}

function ShopingCarRead(url) {
    var t = new Date();
    if (ShoppingUpdate() == '') {
        alert('訂講數量修改錯誤，請重修改訂講數量!!');
    } else {
        //alert('訂講數量修改成功!!\n結帳請至結帳頁面下方,按下[結帳]按鈕結帳!!');
        alert('訂講數量修改成功!!');
    }
    location.href = url + "?t=" + t.getTime().toString();
}
var help1 = "◎商品搜尋要訣：(可輸入任一品名或編號及產品規格中所包含的字元)<br />(1)搜尋單一條件：輸入”條件”做為搜尋條件，符合此” 條件”之商品將全部列出。<br />&nbsp;&nbsp;&nbsp;例.關鍵字「kt」。";
help1 += "<br>(2)進階搜尋：輸入「`條件1` or `條件2`」，<br>&nbsp;&nbsp;&nbsp;只要商品其中符合其中一項條件即會列出。例.關建字「滑鼠 or 鍵盤」。";
help1 += "<br>(3)進階搜尋：輸入「`條件1` and `條件2`」，<br>&nbsp;&nbsp;&nbsp;則`同時`符合條件1及條件2之商品全部列出。例.關建字「kt and 鍵盤」。";
help1 += "<br>(4)條件也可多項以上,如「滑鼠 or 鍵盤 and ktnet」則符合ktnet的滑鼠與鍵盤都會列出";
var help2 = "快速購物區功能:利用常採購產品列表,可快速採購所需商品。";
help2 += "<br>◆只要是在半年內曾經買過的商品，【<span style='color:red'>快速購物區</span>】<span style='color:red'>迅速為您列出</span>，<br>&nbsp;&nbsp;&nbsp;只要依所需商品點選加入購物車即可，不必網頁一一搜尋，快速又方便。";
help2 += "<br>◆您可以選擇列出今天至【60天內】(內訂值)，【61-120天內】<br>&nbsp;&nbsp;&nbsp;(61天前至180天內)或今天至【180天內】買過的商品，且每個品項中<br>&nbsp;&nbsp;&nbsp;均有歷史交易的粉紅色按鈕，馬上知道您買過的數量,日期及價格。";
help2 += "<br>◆符合需要&nbsp;<span style='color:red'>快速補貨</span>&nbsp;及&nbsp;<span style='color:red'>買過要再買</span>&nbsp;的您。"
var oHistory = "「<span style='color:red'>交易歷史查詢</span>」要訣：日期不填則為二年內所有的交易,輸入日期則為日期區間的所有交易";
oHistory += "<br><span style='color:blue'>◆</span>快速查詢:皆不輸入,直接按「<span style='color:red'>送出</span>」,即會將二年內跟我司交易商品導出。";
oHistory += "<br><span style='color:blue'>◆</span>詳細查詢:輸入產品代號或名稱的字串只要符合的商品皆會列出。";
oHistory += "<br><span style='color:blue'>◆本查詢不含促銷品,退貨的歷史查詢</span>。";
var quote = "「<span style='color:red'>列印報價單</span>」可將商品詳細規格及價格編輯成貴司抬頭發給 USER 您可選擇「<span style='color:blue'>我要列印</span>」或「<span style='color:blue'>我要e-mail</span>」";
quote += "<br><span style='color:blue'>◆</span>收件人E-Mail可自行輸入10組。";
quote += "<br><span style='color:blue'>◆</span>貴公司全銜、電話、地址,可自行編修。";
quote += "<br><span style='color:blue'>◆</span>報價價格可依需求隨意修改。";
function SearchHelp(obj, type) {
    $("#" + obj).mouseover(function(e) {
        var tooltip = "<div id='tooltip' onfocus='jQuery(this).remove();'><p>" + (type == 1 ? help1 : (type == 2 ? help2 : (type == 3 ? oHistory : quote))) + "</p></div>";
        $('body').append(tooltip);
        $('#tooltip').css({ "opacity": "0.95", "top": (e.pageY + 20) + "px", "left": (e.pageX + 10) + "px" }).show('fast');
    }).mouseout(function() {
        $('#tooltip').remove();

    }).mousemove(function(e) {
        $('#tooltip').css({ "top": (e.pageY + 20) + "px", "left": (e.pageX + 10) + "px" });
    });
}

function quickShopping(type) {
    location.href = '/member/quickshopping.aspx?quick=' + type + '&page=1';
}

function LoadProdState(pid, objid) {
    $(document).ready(function() { $.post('/GetProdState.ashx', { PID: pid }, function(data) { $('#' + objid).html(data); }); });

}

function SetCwinHeight(obj) {
    var bobo = obj;
    if (document.getElementById) {
        if (bobo && !window.opera) {
            if (bobo.contentDocument && bobo.contentDocument.body.offsetHeight) {
                bobo.height = bobo.contentDocument.body.offsetHeight + 30;
            } else if (bobo.Document && bobo.Document.body.scrollHeight) {
                bobo.height = bobo.Document.body.scrollHeight + 30;
            }
        }
    }
}

function ShowPromotes(pid, div) {
    //Member_ShowPromotes
    $(div).html("<iframe width='920' src='/Member/ShowPromotes.aspx?pid=" + pid + "' scrolling='no' id='frame_" + pid + "' onload='SetCwinHeight(this);' frameborder='0'></iframe><br><input type='button' value='關閉' onclick='$(\"" + div + "\").hide();' />");
    $(div).show(1000);
}




function AdshowImg() {


    document.getElementById('img1').innerHTML = "<a href='" + Adlink[iImg] + "'><img class='AD_img' src='" + AdImg[iImg] + "' title='" + AdTitle[iImg] + "'>";
    iImg++;
    if (iImg == AdImg.length)
        iImg = 0;

    document.getElementById('img2').innerHTML = "<a href='" + Adlink[iImg] + "'><img class='AD_img' src='" + AdImg[iImg] + "' title='" + AdTitle[iImg] + "'>";
    iImg++
    if (iImg == AdImg.length)
        iImg = 0;

    document.getElementById('img3').innerHTML = "<a href='" + Adlink[iImg] + "'><img class='AD_img' src='" + AdImg[iImg] + "' title='" + AdTitle[iImg] + "'>";
    iImg++
    if (iImg == AdImg.length)
        iImg = 0;

}


function favority(title, url) {

    if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    } else if (document.all) {
        window.external.AddFavorite(url, title);
    } else if (window.opera && window.print) {
        return true;
    }
}


function logoutcheck()
{ 
        $.ajax({
               url: "/checkshopping.ashx",
               type: "post",
               dataType: "text",
               async: false,
               success: function (data) { msg = data; }
              });

        if (msg != "" && msg != null) 
        {
            if (confirm(msg))
                Redirect('/logout.ashx')
        }
        else
            Redirect('/logout.ashx')
}

function checkshop() {
    var msg = "";
    if (event.clientX > document.body.clientWidth - 180 && event.clientY < 0 || event.altKey) {
        $.ajax({
            url: "/checkshopping.ashx",
            type: "post",
            dataType: "text",
            async: false,
            success: function (data) { msg = data; }
        });
        if (msg != "" && msg != null) {
            //alert(msg);
                return msg;
        }
    }
}
