﻿    function BindEnter(obj) {
        if ("activeElement" in document) {
            if (obj.keyCode == 13 && (document.activeElement.id == "Supply_keyword" || document.activeElement.id == "Supply_CAS" || document.activeElement.id == "Supply_Formula" || document.activeElement.id == "Request_keyword" || document.activeElement.id == "Request_CAS" || document.activeElement.id == "Request_Formula" || document.activeElement.id == "Company" || document.activeElement.id == "News")) {
                obj.returnValue = false;
                obj.cancel = true;
                //执行搜索
                EnterSearch();
                fouceName = null;
            }
        }
        else {
            if (obj.keyCode == 13 && (event.explicitOriginalTarget.id == "Supply_keyword" || event.explicitOriginalTarget.id == "Supply_CAS" || event.explicitOriginalTarget.id == "Supply_Formula" || event.explicitOriginalTarget.id == "Request_keyword" || event.explicitOriginalTarget.id == "Request_CAS" || event.explicitOriginalTarget.id == "Request_Formula" || event.explicitOriginalTarget.id == "Company" || event.explicitOriginalTarget.id == "News")) {
                obj.returnValue = false;
                obj.cancel = true;
                //执行搜索
                EnterSearch();
                fouceName = null;
            }
        }
    }
    
    var fouceName;

    function ShowFouceName(id) {
        
        if (id != null) {
            fouceName = id;
        }
    }

    function EnterSearch() {
 
        if (fouceName != null) {
            
            //alert(fouceName);
            if (fouceName == "Supply") {
                SelectSupply();
            }
            if (fouceName == "Request") {
                SelectRequest();
            }
            if (fouceName == "Company") {
                SelectCompany();
            }
            if (fouceName == "News") {
                SelectNews();
            }
            
            //登陆
            if (fouceName == "Login") {
                document.getElementById("btnlogin").click();
            }
        }
    }


    if (document.addEventListener) {
        document.addEventListener("keypress", fireFoxHandler, true);
    } else {
        document.attachEvent("onkeypress", ieHandler);
    }

    function fireFoxHandler(evt) {
        if (evt.keyCode == 13 && (event.explicitOriginalTarget.id == "Supply_keyword" || event.explicitOriginalTarget.id == "Supply_CAS" || event.explicitOriginalTarget.id == "Supply_Formula" || event.explicitOriginalTarget.id == "Request_keyword" || event.explicitOriginalTarget.id == "Request_CAS" || event.explicitOriginalTarget.id == "Request_Formula" || event.explicitOriginalTarget.id == "Company" || event.explicitOriginalTarget.id == "News")) {
                evt.returnValue = false;
                evt.cancel = true;
                //执行搜索
                EnterSearch();
                fouceName = null;
        }
    }

    function ieHandler(evt) {
        if (evt.keyCode == 13 && (document.activeElement.id == "Supply_keyword" || document.activeElement.id == "Supply_CAS" || document.activeElement.id == "Supply_Formula" || document.activeElement.id == "Request_keyword" || document.activeElement.id == "Request_CAS" || document.activeElement.id == "Request_Formula" || document.activeElement.id == "Company" || document.activeElement.id == "News")) {
            evt.returnValue = false;
            evt.cancel = true;
            //执行搜索
            EnterSearch();
            fouceName = null;
        }
    }

    //Modify By ZF End

    function kencode(unzipStr, isCusEncode) {
        if (isCusEncode) {
            var zipArray = new Array();
            var zipstr = "";
            var lens = new Array();

            for (var i = 0; i < unzipStr.length; i++) {
                var ac = unzipStr.charCodeAt(i);
                zipstr += ac;
                lens = lens.concat(ac.toString().length);
            }

            zipArray = zipArray.concat(zipstr);
            zipArray = zipArray.concat(lens.join("O"));
            return zipArray.join("N");
        }
        else {
            return encodeURI(unzipStr);
        }


    }

    function kdecode(zipStr, isCusEncode) {
        if (isCusEncode) {

            if (zipStr.indexOf('N') == 0)
                return '';

            var zipArray = zipStr.split("N");
            var zipSrcStr = zipArray[0];
            var zipLens = zipArray[1].split("O");
            var uzipStr = "";
            for (var j = 0; j < zipLens.length; j++) {
                var charLen = parseInt(zipLens[j]);
                uzipStr += String.fromCharCode(zipSrcStr.substr(0, charLen));
                zipSrcStr = zipSrcStr.slice(charLen, zipSrcStr.length);
            }
            return uzipStr;
        } else {
            return decodeURI(zipStr);
        }
    }

    

    function myReplac(myValue) {

        var str = myValue;

        if (str != '') {
            str = str.replace('?', '').replace('&', '').replace('=', '').replace(/(^\s*)|(\s*$)/g, "");
        }

        return str;

    }
    
    function EN_HeadChange(c_name,x,n) {
        for (var i = 1; n >= i; i++) {
            document.getElementById(c_name + i).style.display = "none";
        }
        document.getElementById(c_name + x).style.display = "block";
    }


    function SelectSupply() {
    
        var keyword = kencode(myReplac(document.getElementById('Supply_keyword').value),true);
        var CAS = kencode(myReplac(document.getElementById('Supply_CAS').value),true);
        var Formula = kencode(myReplac(document.getElementById('Supply_Formula').value), true);
        

        var Url = 'http://www.echinachem.com/EN/products/search.aspx?SupplyOrRequestType=1&' + 'KeyWord=' + keyword + '&CAS=' + CAS + '&FormulaText=' + Formula + '&Srch=1';

        document.location.href = Url;

    }

    function SelectRequest() {

        var keyword = kencode(myReplac(document.getElementById('Request_keyword').value),true);
        var CAS = kencode(myReplac(document.getElementById('Request_CAS').value),true);
        var Formula = kencode(myReplac(document.getElementById('Request_Formula').value),true);

        var Url = 'http://www.echinachem.com/EN/products/search.aspx?SupplyOrRequestType=0&' + 'KeyWord=' + keyword + '&CAS=' + CAS + '&FormulaText=' + Formula + '&Srch=1';

        document.location.href = Url;

    }


    function SelectCompany() {

        var Company = kencode(myReplac(document.getElementById('Company').value),true);

        var Url = 'http://www.echinachem.com/EN/products/LatestCompany.aspx?Company=&Keywords=' + Company;

        document.location.href = Url;

    }

    function SelectNews() {

        var news = kencode(myReplac(document.getElementById('txtKeyWord').value), true);

        var Section = "";
        var Url = 'http://www.echinachem.com/EN/news/news_list.aspx?KeyWords=' + news + '&SectionID=' + Section;

        document.location.href = Url;

    }


    function search_onblur(myID) {

        if (document.getElementById(myID).value == '')
            document.getElementById(myID).className = 'input_1';

    }
    function search_onblur2(myID) {

        if (document.getElementById(myID).value == '')
            document.getElementById(myID).className = 'input_3';

    }


    function myUrl() {
        var str = window.location.search;

        var Nub = 1;

        if (str.indexOf("&") > 0) {

            var values = str.split('&');

            switch (values[0]) {
                case '?SupplyOrRequestType=1': getValue(values, 1); Nub = 1; break
                case '?SupplyOrRequestType=0': getValue(values, 2); Nub = 2; break
                case '?Company=': Nub = 3; getValue(values, 3); break;
                default: Nub = 1;
            }

            EN_HeadChange('headDiv_', Nub, 3)

        }

    }

    function mySplit(Value) {

        var str;
        var Values = Value.split('=');

        if (Values.length > 1) {
            str = kdecode(Values[1],true);
        }

        return str;

    }

    function getValue(Values, nub) {

        var Keyword;
        var CAS;
        var Formt


        if (Values.length > 1) {

            Keyword = decodeURI(mySplit(Values[1]));

        }

        if (Values.length > 2) {

            CAS = decodeURI(mySplit(Values[2]));

        }

        if (Values.length > 3) {
            Formt = decodeURI(mySplit(Values[3]));
        }


        switch (nub) {
            case 1:
                if (Keyword != '') {
                    document.getElementById('Supply_keyword').value = Keyword;
                    document.getElementById('Supply_keyword').className = 'input_1_focus';
                    document.getElementById('myselect').value = 1;
                }
                
                if(CAS != 'N')
                    document.getElementById('Supply_CAS').value = CAS;
                else
                    document.getElementById('Supply_CAS').value = '';

                if (Formt != 'N')
                    document.getElementById('Supply_Formula').value = Formt;
                else
                    document.getElementById('Supply_Formula').value = '';
                
                break
            case 2:
                if (Keyword != '') {
                    document.getElementById('Request_keyword').value = Keyword;
                    document.getElementById('Request_keyword').className = 'input_1_focus';
                    document.getElementById('myselect').value = 2;

                }
                if (CAS != 'N')
                    document.getElementById('Supply_CAS').value = CAS;
                else
                    document.getElementById('Supply_CAS').value = '';

                if (Formt != 'N')
                    document.getElementById('Supply_Formula').value = Formt;
                else
                    document.getElementById('Supply_Formula').value = '';
                break
            case 3:
                document.getElementById('Company').value = Keyword;
                document.getElementById('Company').className = 'input_1_focus';
                document.getElementById('myselect').value = 3;
                break;
        }

    }

    // JavaScript Document
    ///代替body的onlaod的事件
    function addLoadEventHead(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function() {
                oldonload();
                func();
            }
        }
    }

    //调用onlaod事件的函数
    addLoadEventHead(function() {

        myUrl();

    })

