function gotop() {
    window.scroll(0, 0);
}

function changeTab(obj) {
    if (obj.id.indexOf('1') > 0)
        toShow('1', '2', '3');
    else if (obj.id.indexOf('2') > 0)
        toShow('2', '1', '3');
    else if (obj.id.indexOf('3') > 0)
        toShow('3', '1', '2');
}

function deleteNoUseClass() {
    for (var i = 1; i < 4; i++) {
        var tableObj = $('proHpTable' + i);
        if (tableObj) {
            var trs = tableObj.getElementsByTagName('tr');
            trs[trs.length - 1].className = 'last';
        }
    }
}

function toShow(id1, id2, id3) {
    if ($('tab' + id1)) {
        $('tab' + id1).className = 'now';
        $('p' + id1).style.display = '';
    }
    if ($('tab' + id2)) {
        $('tab' + id2).className = '';
        $('p' + id2).style.display = 'none';
    }
    if ($('tab' + id3)) {
        $('tab' + id3).className = '';
        $('p' + id3).style.display = 'none';
    }
}

function changeOrder(obj) {
    window.location = orderByUrl + obj.value;
}

function checkSearch(id) {
    if (trimStr($F(id)) == '') {
        alert(getTip(lanInt, 0));
        //alert('请输入关键词');
        return false;
    }
}

function minOrMax(obj) {
    if (obj.className == 'zom') {
        setCookie('isMax', 'no', undefined, '/');
        obj.className = 'min';
        $('show').className = 'collapsed';
        $('operationButtons').className = 'collapsed';
    }
    else {
        setCookie('isMax', 'yes', undefined, '/');
        obj.className = 'zom';
        $('show').className = 'expanded';
        $('operationButtons').className = 'function';
    }
}

function clearCookie() {
    var now = new Date();
    var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
    setCookie('isMax', 'yes', yesterday, '/');
    setCookie('isMax', 'no', yesterday, '/');
}

function setCookie(cookieName, cookieValue, expires, path, domain, secure) {
    document.cookie = escape(cookieName) + '=' + escape(cookieValue) +
    (expires ? '; expires=' + expires.toGMTString() : '') +
    (path ? '; path=' + path : '') +
    (domain ? '; domain=' + domain : '') +
    (secure ? '; secure' : '');
}

function getCookie(cookieName) {
    var cookieValue = '';
    var posName = document.cookie.indexOf(escape(cookieName) + '=');
    if (posName != -1) {
        var posValue = posName + (escape(cookieName) + '=').length;
        var endPos = document.cookie.indexOf(';', posValue);
        if (endPos != -1)
            cookieValue = unescape(document.cookie.substring(posValue, endPos));
        else
            cookieValue = unescape(document.cookie.substring(posValue));
    }
    return (cookieValue);
}

function toVip() {
    //if(confirm('VIP会员才能查看,是否申请为VIP会员?'))
    if (confirm(getTip(lanInt, 1))) {
        window.location = '/preview.do?xcase=contactUs&theme=' + getTip(lanInt, 2);
    }
}

var isLogon = false;
var bodyScrollWidth = 0;
var bodyScrollHeight = 0;
function reCalBodySize() {
    bodyScrollWidth = document.documentElement.scrollWidth;
    bodyScrollHeight = document.documentElement.scrollHeight;
}

function GetCenterXY_ForLayer(objdiv) {
    var clientWidth = parseInt(objdiv.clientWidth);
    var clientHeight = objdiv.clientHeight;
    var objLeft = parseInt(document.documentElement.scrollLeft + (document.documentElement.clientWidth - clientWidth) / 2) + 'px';
    var relTop = (document.documentElement.clientHeight - clientHeight) / 2 > 0 ? (document.documentElement.clientHeight - clientHeight) / 2 : 0;
    var objTop = parseInt(document.documentElement.scrollTop + relTop) + 'px';
    objdiv.style.top = objTop;
    objdiv.style.left = objLeft;
    //checkAndResetStyleTop(objdiv);
}

function checkAndResetStyleTop(obj) {
    var clientHeight = obj.clientHeight;
    var styleTop = parseInt(obj.style.top);
    if (clientHeight + styleTop > bodyScrollHeight) {
        obj.style.top = (bodyScrollHeight - clientHeight) + 'px';
    }
}

function showHideSelect() {
    if (window.navigator.userAgent.indexOf("Firefox") < 0) {
        var selects = document.getElementsByTagName('select');
        for (var i = 0; i < selects.length; i++)
            selects[i].style.visibility = arguments[0];
    }
}

function showAlpha() {
    var alphaObj = $("alpha");
    alphaObj.className = "Alpha expanded";
    if (window.screen.availHeight > document.body.clientHeight)
        alphaObj.style.height = window.screen.availHeight + 'px';
    else
        alphaObj.style.height = document.body.clientHeight + 'px';
}

function unshowAlpha() {
    var alphaObj = $("alpha");
    alphaObj.className = "Alpha collapsed";
}

function modifyPassword(id) {
    showAlpha();
    showHideSelect('hidden');
    var obj = $(id);
    reCalBodySize();
    setInterval(function() {
        GetCenterXY_ForLayer(obj)
    }, 50);
    show(id);
    if ($('compare'))
        hide('compare');
    if ($('drop'))
        hide('drop');
}

function modifyCancel() {
    if (compareIds != undefined && compareIds.length > 0 && $('drop'))
        show('drop');
    var spans = $('modifyPassword').getElementsByTagName(checkLabel == 'en' ? 'div' : 'span');
    for (var i = 0; i < spans.length; i++) {
        if (spans[i].className == 'formError')
            spans[i].innerHTML = '';
    }
    unshowAlpha();
    showHideSelect('visible');
    hide('modifyPassword');
}

function register() {
    if ($('registerId') && $F('registerId').indexOf(',') < 0) {
        window.location = '/preview.do?xcase=registerPage&id=' + $F('registerId');
    }
    else
        window.location = '/preview.do?xcase=registerPage';
}

function logon(tipNum) {
    var pars = 'xcase=logonPage&tipNum=' + tipNum;
    var url = '/account.do';
    var id = 'login';
    var myAjax = new Ajax.Request(url, {
        method: 'post',
        parameters: pars,
        onComplete: function() {
            var head = arguments[0].getResponseHeader('requestSuccess4Ajax');
            if (head != "1") {
                window.location = '/errorDocs/connection_error.htm';
                return;
            }
            else {
                $(id).innerHTML = arguments[0].responseText;
                showAlpha();
                showHideSelect('hidden');
                var obj = $(id);
                reCalBodySize();
                setInterval(function() {
                    GetCenterXY_ForLayer(obj)
                }, 50);
                show(id);
                if ($('compare'))
                    hide('compare');
                $('userName').focus();
            }
        },
        onFailure: reportError
    });
}

function resetLogon() {
    document.forms['logonForm'].className = 'collapsed';
    $('logon').className = 'expanded';
    $('register').className = 'expanded';
}

function logonCheck() {
    if (trimStr($F('userName')) == '' || trimStr($F('password')) == '') {
        //alert('请输入帐号或者密码');
        alert(getTip(lanInt, 3));
        $('userName').focus();
    }
    else {
        var url = '/account.do';
        var pars = 'xcase=logon&coreUserInfo.userLogName=' + $F('userName') + "&coreUserInfo.userLogPassword=" + $F('password');
        var myajax = new Ajax.Request(url, {
            method: 'post',
            parameters: pars,
            onComplete: function() {
                var head = arguments[0].getResponseHeader('requestSuccess4Ajax');
                if (head != "1") {
                    window.location = '/errorDocs/connection_error.htm';
                    return;
                }
                else {
                    var logonResult = arguments[0].responseText;
                    //帐号密码正确
                    if (logonResult == '1') {
                        var url = window.location.href;
                        if (url.indexOf("xcase=contact") > 0) {
                            url = "/preview.do?xcase=contactUs";
                        }
                        else if (url.indexOf("sendInquire") > 0) {
                            var registerIdValue = document.getElementById('registerId').value;
                            if (registerIdValue.indexOf(',') < 0) {
                                url = "/preview.do?xcase=inquire&id=" + registerIdValue;
                            }
                            else {
                                url = "/preview.do?xcase=inquireCompared";
                            }
                        }
                        else if (url.indexOf("xcase=findPassword") > 0) {
                            url = "/preview.do?xcase=findPasswordPage";
                        }
                        else if (url.indexOf("xcase=register") > 0) {
                            url = "/preview.do?xcase=registerPage";
                        }
                        window.location = url;
                    }
                    else if (logonResult == '0') {
                        alert(getTip(lanInt, 4));
                        $('userName').select();
                        $('userName').focus();
                    }
                    else if (logonResult == '2') {
                        alert(getTip(lanInt, 5));
                        $('userName').select();
                        $('userName').focus();
                    }
                }
            },
            onFailure: reportError
        });
    }
}

function cancel() {
    unshowAlpha();
    showHideSelect('visible');
    hide('login');
    hide('compare');
}

function popClose() {
    cancel();
    if (compareIds != undefined)
        if (compareIds.length > 0)
            commonProcess('0');
    show('drop');
}

function searchEvent(e) {
    caller = searchEvent.caller;
    while (!caller.arguments[0] || !caller.arguments[0] instanceof Event) {
        caller = caller.caller;
    }
    return caller.arguments[0];
}

function userNamePress(e) {
    e = e || window.event || searchEvent(e);
    if (e.keyCode == 13) {
        $('password').select();
        $('password').focus();
        return false;
    }
}

function passPress(e) {
    e = e || window.event || searchEvent(e);
    if (e.keyCode == 13)
        logonCheck();
}

function showLoginTip() {
    $('loginTip').className = 'expanded';
    document.documentElement.scrollTop = '0px';
    setTimeout(function() {
        $('loginTip').className = 'collapsed';
    }, 3000);
}

function trimStr(str) {
    return str.replace(/(^\s*)|(\s*$)/g, '');
}

function compareProduct(id, obj) {
    for (var i = 0; i < compareIds.length; i++) {
        if (compareIds[i] == id) {
            var newComparedIds = [];
            for (var i = 0; i < compareIds.length; i++) {
                if (compareIds[i] != id)
                    newComparedIds.push(compareIds[i]);
                else {
                    if ($('img' + id))
                        $('img' + id).src = compareImg.src;
                }
            }
            compareIds = newComparedIds;
            commonProcess('0');
            return;
        }
    }
    if (compareIds.length >= 9) {
        alert(getTip(lanInt, 6));
        return;
    }
    compareIds.push(id);
    obj.childNodes[0].src = comparedImg.src;
    setCookie('isMax', 'yes', undefined, '/');
    commonProcess();
}

function commonProcess(comparePageFlag) {
    if (!$('drop'))
        return;
    var pars = 'xcase=compareProductPage&comparePageFlag=';
    pars += comparePageFlag;
    for (var i = 0; i < compareIds.length; i++) {
        pars += '&comparedId=';
        pars += compareIds[i];
    }
    var url = '/preview.do';
    var updateDiv = 'show';
    var operationDiv = 'drop';
    if (comparePageFlag == '1') {
        updateDiv = 'compare';
        operationDiv = 'compare';
    }
    var myAjax = new Ajax.Request(url, {
        method: 'post',
        parameters: pars,
        evalScripts: true,
        onComplete: function() {
            var head = arguments[0].getResponseHeader('requestSuccess4Ajax');
            if (head != "1") {
                window.location = '/errorDocs/connection_error.htm';
                return;
            }
            else {
                $(updateDiv).innerHTML = arguments[0].responseText;
                if (operationDiv == 'compare') {
                    hide('drop');
                    if (compareIds.length > 0) {
                        reCalBodySize();
                        setInterval(function() {
                            GetCenterXY_ForLayer($('compare'))
                        }, 50);
                        showAlpha();
                        showHideSelect('hidden');
                        show(operationDiv);
                    }
                }
                else if (operationDiv == 'drop') {
                    if ($('drop')) {
                        if (window.navigator.userAgent.indexOf("Firefox") > 0 || window.navigator.userAgent.indexOf("MSIE 7.0;") > 0) {
                            $('drop').style.position = 'fixed';
                        }
                        else {
                            setInterval(function() {
                                leftmove();
                            }, 50);
                        }
                        if ($('modifyPassword') && $('modifyPassword').className.indexOf('expanded') != -1)
                            hide('drop');
                        else
                            show('drop');
                        var cookieValue = getCookie('isMax');
                        if (cookieValue == 'no') {
                            $('minMax').className = 'min';
                            $('show').className = 'collapsed';
                            $('operationButtons').className = 'collapsed';
                        }
                        else {
                            $('minMax').className = 'zom';
                            $('show').className = 'expanded';
                            $('operationButtons').className = 'function';
                        }
                    }
                }
                if (compareIds.length == 0) {
                    cancel();
                    hide('drop');
                }
            }
        },
        onFailure: reportError
    });
}

var isFirst = false;
var childPage;
function toCompareProducts() {
    isFirst = true;
    commonProcess('1');
}

function getTip(lan, index) {
    var messages = [['Please enter the keyword!', 'This infomation is available for VIP members. Do you want to apply for it now?', 'Apply for VIP members', 'Please enter your username or password!', 'The username or password is invalid, please try again!', 'The account is not available now, please ask the web administrator for assistance.', 'You can compare only 9 products!', 'Inquirement of the product', 'Are you sure to delete the product?', 'Are you sure to clear all the products?', 'sorry,error'], ['请输入搜索关键词!', '该内容仅VIP会员才有权查看,您是否需要申请为VIP会员?', 'VIP会员申请信', '请输入您的用户名或密码！', '您的用户名或密码错误，请重新输入!', '该帐号已冻结，请与网站管理员联系!', '您最多可比较9个产品!', '产品询价', '您确定要删除该产品吗?', '您确定要清空所有产品吗?', '对不起，发生错误']];
    return messages[lan][index];
}

function inquire(id) {
    window.location = '/preview.do?xcase=inquire&id=' + id;
}

function sendInquire() {
    window.location = '/preview.do?xcase=inquireCompared';
}

//从询盘篮删除
function deleteCompareProduct(id, comparePageFlag) {
    if (confirm(getTip(lanInt, 8))) {
        var newComparedIds = [];
        for (var i = 0; i < compareIds.length; i++) {
            if (compareIds[i] != id)
                newComparedIds.push(compareIds[i]);
            else if ($('img' + id))
                $('img' + id).src = compareImg.src;
        }
        compareIds = newComparedIds;
        commonProcess(comparePageFlag);
    }
}

//从比较页面删除
function deleteInquireProduct(id) {
    if (confirm(getTip(lanInt, 8)))
        window.location = '/preview.do?xcase=deleteInquireProduct&id=' + id;
}

function clearAll() {
    if (confirm(getTip(lanInt, 9))) {
        clearCookie();
        compareIds = [];
        var images = document.getElementsByTagName('img');
        for (var i = 0; i < images.length; i++) {
            if (images[i].src.indexOf('compared') >= 0) {
                images[i].src = compareImg.src;
            }
        }
        $('show').innerHTML = '';
        hide('drop');
        cancel();
        var url = '/preview.do';
        var pars = 'xcase=clearAll';
        var myAjax = new Ajax.Request(url, {
            method: 'post',
            parameters: pars,
            onComplete: function() {
                var head = arguments[0].getResponseHeader('requestSuccess4Ajax');
                if (head != "1") {
                    window.location = '/errorDocs/connection_error.htm';
                    return;
                }
                else {
                    if (window.location.href.indexOf('inquireCompared') > 0 || window.location.href.indexOf('sendInquire') > 0)
                        if (compareIds.length > 0)
                            window.location = 'preview.do?xcase=inquireCompared';
                        else
                            window.location = '/preview.do?xcase=home';
                }
            },
            onFailure: reportError
        });
    }
}

function clickImge(srcImage) {
    $("image").src = $(srcImage).value;
}

function outImge() {
    $("image").src = $("imageBak").value;
}

function leftmove() {
    var divHeight = parseInt($("drop").offsetHeight);
    var divWidth = parseInt($("drop").offsetWidth);
    var docWidth = document.documentElement.clientWidth;
    var docHeight = document.documentElement.clientHeight;
    $("drop").style.top = docHeight - divHeight + parseInt(document.documentElement.scrollTop);
    $("drop").style.left = docWidth - divWidth + parseInt(document.documentElement.scrollLeft);
}

function showHideDivForComparedTop(id) {

}

function reportError(request) {
    alert(getTip(lanInt, 10));
}

function hide(obj) {
    if ($(obj))
        $(obj).className = $(obj).className.replace("expanded", "collapsed");
}

function show(obj) {
    if ($(obj))
        $(obj).className = $(obj).className.replace("collapsed", "expanded");
}

function showHide(obj) {
    if ($(obj).className.indexOf("collapsed") >= 0)
        $(obj).className = $(obj).className.replace("collapsed", "expanded");
    else
        $(obj).className = $(obj).className.replace("expanded", "collapsed");
}

function checkImgWidth(obj) {
    if (obj.width >= obj.height && obj.width > 150) {
        obj.width = 150;
    }
    else if (obj.height >= obj.width && 150 > obj.height) {
        obj.height = 150;
    }
}

var lastSub;
var timeOut;
function siteShowInit() {
    //二级菜单显示
    var lis = $('nav').getElementsByTagName('li');
    var alink = null;
    var subMenuNode = null;
    var subMenuStyle = $('subMenuStyle') ? $F('subMenuStyle') : '0';
    for (var i = 0; i < lis.length; i++) {
        if (lis[i].parentNode.className == 'subnav') {
            lis[i].parentNode.onmouseover = function() {
                if (timeOut) {
                    clearTimeout(timeOut);
                }
                if (lastSub)
                    lastSub.style.display = 'none';
                lastSub = this;
                this.style.display = '';
            }
            lis[i].parentNode.onmouseout = function() {
                var curSubmenu = this;
                timeOut = setTimeout(function() {
                    curSubmenu.style.display = 'none';
                }, 500);
            }
            continue;
        }
        //alink=lis[i].getElementsByTagName('a')[0];
        //设置二级菜单的位置
        subMenuNode = lis[i].getElementsByTagName('ul');
        subMenuNode = subMenuNode.length == 1 ? subMenuNode[0] : null;
        if (subMenuNode && subMenuNode.className == 'subnav') {
            //获取一级菜单位置
            var alinkPosition = Position.positionedOffset(lis[i]);
            if (subMenuStyle == '0') { //将2级菜单的位置定位在1级菜单的下方
                subMenuNode.style.top = alinkPosition[1] + lis[i].offsetHeight + "px";
                subMenuNode.style.left = alinkPosition[0] + "px";
            }
            else if (subMenuStyle == '1') { //将2级菜单的位置定位在1级菜单的右方
                subMenuNode.style.top = alinkPosition[1] + "px";
                subMenuNode.style.left = alinkPosition[0] + lis[i].offsetWidth + "px";
            }
            lis[i].onmouseover = function() {
                var submenu = this.getElementsByTagName('ul');
                if (submenu.length == 1) {
                    if (timeOut)
                        clearTimeout(timeOut);
                    if (lastSub)
                        lastSub.style.display = 'none';
                    lastSub = submenu[0];
                    submenu[0].style.display = '';
                }
            };
            lis[i].onmouseout = function() {
                var submenu = this.getElementsByTagName('ul');
                if (submenu.length == 1) {
                    if (timeOut)
                        clearTimeout(timeOut);
                    timeOut = setTimeout(function() {
                        submenu[0].style.display = 'none';
                    }, 500);
                }
            };
        }
    }
    //
    if (compareIds != undefined)
        if (compareIds.length > 0)
            commonProcess('0');
    if ($('modPass'))
        setTimeout(function() {
            $('modPass').innerHTML = '&nbsp;';
        }, 2000);
    if ($('modifyPassword') && $('modifyPassword').className.indexOf('expanded') != -1)
        modifyPassword('modifyPassword');
    if (window.popnotice)
        popnotice();
    //open the images of defined category for new page
    if (window.location.href.indexOf('custom-category') == -1) {
        return;
    }
    var inside = document.getElementById('inside');
    if (inside) {
        var imgs = inside.getElementsByTagName('img');
        for (var i = 0; i < imgs.length; i++) {
            imgs[i].style.cursor = 'pointer';
            imgs[i].onclick = function() {
                var url = this.getAttribute('src');
                if (url.indexOf('photoType=19') != -1)
                    url = url.replace('photoType=19', 'photoType=18');
                window.open(url);
            }
        }
    }
}

function resizeLogo(obj) {
    var image = new Image();
    image.src = obj.src;
    //alert(image.width+':'+image.height);
    var scaleWidth, scaleHeight, scale;
    var imageWidth = image.width;
    var imageHeight = image.height;
    if (imageWidth <= 200 && imageHeight <= 60) {
        return;
    }
    var widthHeightScale = imageWidth / imageHeight;
    var conversionWidthHeightScale = 200 / 60;
    if (widthHeightScale > conversionWidthHeightScale) {
        scale = 200 / imageWidth;
    }
    else {
        scale = 60 / imageHeight;
    }
    scaleWidth = scale * imageWidth;
    scaleHeight = scale * imageHeight;
    obj.style.width = scaleWidth + 'px';
    obj.style.height = scaleHeight + 'px';
}

if (window.addEventListener) {
    window.addEventListener("load", siteShowInit, false);
}
else if (window.attachEvent) {
    window.attachEvent("onload", siteShowInit);
}
