﻿// JScript 文件：存储一些基本方法，供其它地方调用，相当于是一个公共库lib

// js 对象序列化
function Serialize(obj){
    switch(obj.constructor){
        case Object:
            var str = "{";
            for(var o in obj){
                str += o + ":" + Serialize(obj[o]) +",";
            }
            if(str.substr(str.length-1) == ",")
                str = str.substr(0,str.length -1);
            return str + "}";
            break;
        case Array:            
            var str = "[";
            for(var o in obj){
                str += Serialize(obj[o]) +",";
            }
            if(str.substr(str.length-1) == ",")
                str = str.substr(0,str.length -1);
            return str + "]";
            break;
        case Boolean:
            return "\"" + obj.toString() + "\"";
            break;
        case Date:
            return "\"" + obj.toString() + "\"";
            break;
        case Function:
            break;
        case Number:
            return "\"" + obj.toString() + "\"";
            break; 
        case String:
            return "\"" + obj.toString() + "\"";
            break;
    }
}


//写cookies函数
function SetCookie(name,value)//两个参数，一个是cookie的名子，一个是值
{
      //浏览器关闭，清除cookie
      document.cookie = name + "="+ escape (value) + ";expires=";
      
//    var Days = 1; //此 cookie 将被保存 30 天
//    var exp  = new Date();    //new Date("December 31, 9998");
//    exp.setTime(exp.getTime() + Days*24*60*60*1000);
//    document.cookie = name + "="+ escape (value) + ";expires=";
    //document.cookie = name + "="+ value + ";expires=" + exp.toGMTString();
}
function GetCookie(name)//取cookies函数        
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;
    // if(arr != null) return arr[2]; return null;
}
function DelCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=GetCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
//判断是否在数组内
function IsInArray(ary,v)
{
    var rtn = -1;
    if(ary == null || ary.length == 0) return rtn;
    
    for(i=0;i<ary.length;i++)
    {
        if(ary[i] == v) return i;
    }
    
    if(i== ary.length) return rtn;
}

//将数组写入COOKIE
function WriteToCookie(cookieName,ary)
{
    SetCookie(cookieName,Serialize(ary));
}



//#############################################################   页面共用方法开始:显示左边数据  #################################################################

//数据格式（不要删除此注释）
//aryProductCate:不加name,否则cookie存不下
//{idList:[],objList:[{CateChar:'A',CCID:11,CCCID:111}]}
//aryCountry
//{idList:[],objList:[{INDEX:1,AID:1,AName:***,CID:11,CName:***}]}
//aryBN: Business nature(注:oidList:other所代表ID列表,oname:显示other文本)
//{idList:[],objList:[{BID:1,BName:***}],oidList:'',oName:'***'}
//aryKeyWord
//{BSearch:{ByName:***,KeyWord:***},KeySearch:[{ByName:***,KeyWord:***}]}

//*************************************************  全局变量定义开始 **********************************************************

//特效样式
var flyDiv = '<div id="fly_shadow" style="display: none; background-color: #bbb; border:1px solid #aaa;z-index: 9999;border:solid 1px blue;"> </div>';

//存储选择的产品类别数组
var aryProductCate = {idList:new Array(),objList:new Array()};
//存储cookie中的产品类别id列表如1,2,3
var strCateIDList='';

//国家数组
var aryCountry = {idList:new Array(),objList:new Array()};
//存储cookie中分段的id列表如1,2,3
var strCountryIDList = '';

//Business nature数组
var aryBN = {idList:new Array(),objList:new Array(),oidList:'',oName:''};
var strBNIDList = '';
var iBNFilterNum = 8;

//Keyword数据数组
var aryKeyword = null;

//设置变量，COOKIE名
//存放MD形式(4组): 产品种类ID集合{sep}国家ID集合{sep}business nature ID 集合{sep}1;Company name;keyword|1;Company name;keyword1 |1;Company name;keyword2 |1;Company name;keyword3
var ckMD = "ckMD";


//当前语言
var lang = location.href.indexOf("_cht.aspx") > 0 ? 1:2;
var strLoading = lang == 1 ? "正在加載...":"Loading...";
var strTimeOut = lang == 1 ? "讀取數據超時,請重試":"Time out!";
var strError = lang == 1 ? "讀取數據錯誤,請重試":"Load data error!";
var strOther = lang == 1 ? "其它":"Others";
var strAll = lang == 1 ? "全選 / 清除":"Select All / Unclick to clear all";
var strOperate = lang == 1 ? "請勿頻繁操作":"Please don't frequently operate it.";
var strBack2BasicSearch = lang == 1 ? "你確定要重設進階搜索嗎？":"Are you sure to reset refine search?";
var strBasicSearchAlert = lang == 1 ? "你的選擇項目為空，請選擇。":"Your Selection is empty, please select.";
var strSelectKeywordType = lang == 1 ? "請選擇關鍵字類型。":"Please select keyword type.";

//子类标题
//var subCategoryTitle = lang == 1 ? "<h3 class=\"checked_title_blue\">產品次類別</h3>" : "<h3 class=\"checked_title_blue\">Sub Category</h3>";
var subCategoryTitle = lang == 1 ? "<h3 class=\"checked_title_blue\">產品/服務次類別 <img src=\"images/warn_chi.gif\"  width=\"160\" height=\"12\" border=\"0\" alt=\" \" class=\"imgSubCategoryTitle\" /></h3>" : "<h3 class=\"checked_title_blue\">Sub Category <img src=\"images/warn.gif\"  width=\"160\" height=\"12\" border=\"0\" alt=\" \" class=\"imgSubCategoryTitle\" /></h3>";


//定义分隔串:将产品类别ID,国家ID,Business nature ID, Keyword等信息用分隔串组合后存入COOKIE
var SepString = '{sep}';
var SepKeywordChar = ';';
//*************************************************  全局变量定义结束 **********************************************************
function GetCookieData()
{
     var strMDCookie = GetCookie(ckMD);
     if(strMDCookie != null && strMDCookie !="" && strMDCookie.indexOf(SepString) > 0)
     {
            var ary = strMDCookie.split(SepString);
            strCateIDList = ary[0]; 
            strCountryIDList = ary[1]; 
            strBNIDList = ary[2]; 
            var strKeyword = ary[3];
            
           if(strKeyword !=null && strKeyword !="")
           {
                aryKeyword = strKeyword.split('|');
           }
     }
}

//pLang:系统语言代号: 1代表中文,2代表英文
function DisplayLeft_ProductCate(pLang)
{
     //默认参数设为2(英文)
     if(arguments.length==0){   
          pLang   =   lang;   
      }  
      
     $("#divLeftCate").find("div").hide();  
     if($.trim(strCateIDList) !="")
     {
         $.ajax({
                 type: "POST",
                 url: "doRequest.ashx",
                 data: {t:4,strid:strCateIDList},
                 dataType:"json",
                 timeout:10000,
                 beforeSend:function(){
                                        $("#divLeftCate").append("<dl class='dl_search' id='LeftCate_Loading'><dd><img src='images/loading_small.gif' border='0' id='imgLoadingLeftCate'/></dd></dl>");
                            },
                 complete:function(res,msg){ 
                                       $("#divLeftCate").find("#LeftCate_Loading").remove();
                                       if(res.status == 0)//请求超时
                                       {
                                            $("#LeftCate_Loading").remove();
                                            $("#divLeftCate").append('<dl class="dl_search" id="LeftCate_TimeOut"><dd>'+strTimeOut+'</dd></dl>');
                                       }
                                       else if(res.status == 200)//请求成功
                                       {
                                            var resAry = null;
                                            eval("resAry = "+ res.responseText);
                                            if(resAry !=null)
                                            {
                                                var tmpHtml = '';
                                                //清空数组,为重新构造
                                                aryProductCate.idList.splice(0,aryProductCate.idList.length);
                                                aryProductCate.objList.splice(0,aryProductCate.objList.length);
                                                for(i=0;i<resAry.data.length;i++)
                                                {
                                                        tmpHtml = '';
                                                        $("#LeftCate_"+resAry.data[i].CateChar).show();
                                                        
                                                        if(!document.getElementById("LeftCate_"+resAry.data[i].CateChar+'_'+resAry.data[i].CCID))
                                                        {
                                                            tmpHtml = '<dl id="LeftCate_'+resAry.data[i].CateChar+'_'+resAry.data[i].CCID+'" class="dl_search">';
                                                            tmpHtml +='<dt class="dl_title">'+(pLang == 1 ? resAry.data[i].CCName_Chi:resAry.data[i].CCName_Eng);
                                                            tmpHtml +='</dt><dd id="LeftCate_'+resAry.data[i].CateChar+'_'+resAry.data[i].CCID+'_'+resAry.data[i].CCCID+'">';
                                                            tmpHtml += pLang == 1 ? resAry.data[i].CCCName_Chi:resAry.data[i].CCCName_Eng;
                                                            tmpHtml +='</dd></dl>';
                                                            $("#LeftCate_"+resAry.data[i].CateChar).append(tmpHtml);
                                                        }
                                                        else
                                                        {
                                                            tmpHtml ='<dd id="LeftCate_'+resAry.data[i].CateChar+'_'+resAry.data[i].CCID+'_'+resAry.data[i].CCCID+'">';
                                                            tmpHtml += (pLang == 1 ? resAry.data[i].CCCName_Chi:resAry.data[i].CCCName_Eng);
                                                            tmpHtml +='</dd>';
                                                            $("#LeftCate_"+resAry.data[i].CateChar+'_'+resAry.data[i].CCID).append(tmpHtml);
                                                        }
                                                        aryProductCate.idList.push(resAry.data[i].CCCID);
                                                        aryProductCate.objList.push({CateChar:resAry.data[i].CateChar,CCID:resAry.data[i].CCID,CCCID:resAry.data[i].CCCID});
                                                }
                                            }
                                       }
                                       else//发生错误
                                       {
                                           $("#divLeftCate").append('<dl class="dl_search" id="LeftCate_TimeOut"><dd>'+strError+'</dd></dl>');
                                       }
                          }
          }); //End of : $.ajax
     }
}

//显示左右页面显示
function DisplayLeft_Country()
{
    //默认参数设为2(英文)
     if(arguments.length==0){   
          pLang   =   lang;   
      }   
     if($.trim(strCountryIDList) != "")
     {
             $.ajax({
                     type: "POST",
                     url: "doRequest.ashx",
                     data: {t:5,strid:strCountryIDList},
                     dataType:"json",
                     timeout:10000,
                     beforeSend:function(){
                                            $("#divLeftCountry").find("div").hide();
                                            $("#divLeftCountry").append("<dl class='dl_search' id='LeftCountry_Loading'><dd><img src='images/loading_small.gif' border='0' id='imgLoadingLeftCountry'/></dd></dl>");
                                },
                     complete:function(res,msg){ 
                                           $("#LeftCountry_Loading").remove();
                                           
                                           if(res.status == 0)//请求超时
                                           {
                                                $("#divLeftCountry").append('<dl class="dl_search" id="LeftCountry_TimeOut"><dd>'+strTimeOut+'</dd></dl>');
                                           }
                                           else if(res.status == 200)//请求成功
                                           {
                                                var resAry = null;
                                                eval("resAry = "+ res.responseText);
                                                if(resAry !=null)
                                                {
                                                    var tmpHtml = '';
                                                    //清空数组,为重新构造
                                                    aryCountry.idList.splice(0,aryCountry.idList.length);
                                                    aryCountry.objList.splice(0,aryCountry.objList.length);
                                                    for(i=0;i<resAry.data.length;i++)
                                                    {                                          
                                                            tmpHtml = '';
                                                            
                                                            if(!document.getElementById("divLeftCountry_" + resAry.data[i].AreaUID))
                                                            {
                                                                tmpHtml ='<div id="divLeftCountry_' + resAry.data[i].AreaUID + '">';
                                                                tmpHtml +='<h3 class="h3_search2" id="h3Country_' + resAry.data[i].AreaUID + '">'+ (pLang == 1 ? resAry.data[i].Area_Chi:resAry.data[i].Area_Eng) +'</h3>';
                                                                tmpHtml +='<dl class="dl_search" id="LeftCountry_' + resAry.data[i].AreaUID + '">';
                                                                tmpHtml +='<dd id="LeftCountry_' + resAry.data[i].AreaUID + '_' + resAry.data[i].CountryUID + '">';
                                                                tmpHtml += pLang == 1 ? resAry.data[i].Country_Chi:resAry.data[i].Country_Eng;
                                                                tmpHtml +='</dd></dl>';
                                                                $("#divLeftCountry").append(tmpHtml);
                                                            }
                                                            else
                                                            {
                                                                tmpHtml ='<dd id="LeftCountry_' + resAry.data[i].AreaUID + '_' + resAry.data[i].CountryUID + '">';
                                                                tmpHtml += pLang == 1 ? resAry.data[i].Country_Chi:resAry.data[i].Country_Eng;
                                                                tmpHtml +='</dd>';
                                                                $("#LeftCountry_" + resAry.data[i].AreaUID).append(tmpHtml);
                                                            }
                                                            aryCountry.idList.push(resAry.data[i].CountryUID);
                                                            aryCountry.objList.push({AID:resAry.data[i].AreaUID,CID:resAry.data[i].CountryUID});
                                                    }
                                                }
                                           }
                                           else//发生错误
                                           {
                                               $("#divLeftCountry").append('<dl class="dl_search" id="LeftCountry_TimeOut"><dd>'+strError+'</dd></dl>');
                                           }
                              }
              }); //End of:  $.ajax
      }
      else
      {
            $("#divLeftCountry").empty();
      }
}
//显示左边business nature
function DisplayLeft_BN()
{
    //默认参数设为2(英文)
     if(arguments.length==0){   
          pLang   =  lang;   
      }    
     var tmpMDCookie = GetCookie(ckMD);
     
     if((strBNIDList ==null  || $.trim(strBNIDList) =="") && $.trim($("#divLeftKeyword2").html()) == "")
     {
        $("#divRefineSearch").hide();
     } 
     else
     {
         $("#divRefineSearch").show();
     }
     if($.trim(strBNIDList) != "")
     {  
             $.ajax({
                     type: "POST",
                     url: "doRequest.ashx",
                     data: {t:6,strid:strBNIDList},
                     dataType:"json",
                     timeout:10000, 
                     beforeSend:function(){
                                            $("#dlBN").append("<dl class='dl_search' id='LeftBN_Loading'><dd><img src='images/loading_small.gif' border='0' id='imgLoadingLeftBN'/></dd></dl>");
                                },
                     complete:function(res,msg){ 
                                           $("#imgLoadingLeftBN").remove();
                                           
                                           if(res.status == 0)//请求超时
                                           {
                                                $("#LeftBN_Loading").append('<dl class="dl_search" id="LeftBN_TimeOut"><dd>'+strTimeOut+'</dd></dl>');
                                           }
                                           else if(res.status == 200)//请求成功
                                           {
                                                //删除上面标签
                                                $("#LeftBN_Loading").remove();
                                                
                                                var resAry = null;
                                                var tmpHtml = '';
                                                eval("resAry = "+ res.responseText);
                                                if(resAry !=null && resAry.data.length > 0)
                                                {
                                                    //清空数组,为重新构造
                                                    aryBN.idList.splice(0,aryBN.idList.length);

                                                    var tmpBNName = '';
                                                    for(i=0;i<resAry.data.length;i++)
                                                    {       
                                                            tmpBNName = pLang == 1 ? resAry.data[i].BusNature_Chi:resAry.data[i].BusNature_Eng;   
                                                            aryBN.idList.push(resAry.data[i].BusNatureUID);
                                                            aryBN.objList.push({BID:resAry.data[i].BusNatureUID,BName:tmpBNName});    
                                  
                                                            tmpHtml+='<dd id="LeftBN_' + resAry.data[i].BusNatureUID + '">' + tmpBNName +'</dd>';
                                                    }
                                                }
                                                $("#dlBN").html(tmpHtml);
                                           }
                                           else//发生错误
                                           {
                                               $("#LeftBN_Loading").append('<dl class="dl_search" id="LeftBN_Error"><dd>'+strError+'</dd></dl>');
                                           }
                              }
              }); 
    }//End of:strBNIDList
}
//显示左边Keyword
function DisplayLeft_Keyword()
{
   var htmlKeyword = '';
   if(aryKeyword != null)
   {
        var strBKeyword = aryKeyword[0];
        
        var strKeyword1 = aryKeyword[1];
        var strKeyword2 = aryKeyword[2];
        var strKeyword3 = aryKeyword[3];
        
        htmlKeyword += dealKeywordAry(strKeyword1);
        htmlKeyword += dealKeywordAry(strKeyword2);
        htmlKeyword += dealKeywordAry(strKeyword3);
   }
   $("#divLeftKeyword1").html(dealKeywordAry(strBKeyword));
   $("#divLeftKeyword2").html(htmlKeyword);   
}
//只供DisplayLeft_Keyword()方法调用
function dealKeywordAry(strAry)
{
    var rtn = '';
    if(strAry != null && strAry != "" && strAry.indexOf(SepKeywordChar) > 0)
    {
        var aryTmp = strAry.split(SepKeywordChar);
        if(aryTmp !=null && aryTmp[2] !=null && aryTmp[2] !="")
        {
            rtn = '<h3 class="h3_search2">' + aryTmp[1] + '</h3><dl class="dl_search"><dd>' + aryTmp[2] + '</dd></dl>'
        }
    }
    return rtn;
}

//#############################################################   页面共用方法结束:显示左边数据  #################################################################
