﻿var $URL="http://www.99114.com";
var $URL_M="http://Manager.99114.com";
var $URL_F="http://free.99114.com";
var $URL_L="http://login.99114.com";
var OPENER = null;

String.prototype.padLeft = function(len,ch)
{
    var self = this;
    if(!self) return "";
    self = self.toString();
    if(!ch) return self;
    for(var i=self.length;i<len;i++)
    {
        self = ch + self;
    }
    return self;
};
String.prototype.trim = function()
{
    var s = this;
    return s.replace(/(^\s*)|(\s*$)/g, "");
};
/*去空格*/
function trim(s)
{
    if(s==null)
        return "";
    return s.replace(/(^\s*)|(\s*$)/g, "");
}
 /*  
JavaScript的Cookie类  
 注：js设定cookie的时间是以毫秒为单位  
*/  
  function jsCookie(){   
    var COOKIE_LIFE ={year:31536000,month:2592000,week:604800,day:86400,hour:3600,browser:0}   
    this.expires = 'browser';   
    this.path = null;   
    this.domain = null;   
    this.get=function(fieldName){       
  
        var regexp = window.eval("/"+fieldName+"=([\\w%,]+)(|;)/");   
        var result = document.cookie.match(regexp);   
        return (result?unescape(result[1]):"");   
    }       
    this.set=function(fieldName,fieldValue){   
  
        var cookie_list;   
        cookie_list = fieldName+"="+escape(fieldValue);           
        cookie_list += setExpires(this.expires);   
        cookie_list += setPath(this.path);   
        cookie_list += setDomain(this.domain);   
        document.cookie = cookie_list;   
    }   
    function setExpires(expires){   
  
        var tm=0;   
          if(typeof(expires)=='string'){   
            if(!COOKIE_LIFE[expires]) return "";       
            tm = COOKIE_LIFE[expires]*1000;           
        }else if(typeof(expires)=='number'){               
            tm = expires*1000;   
        }else{return "";}   
        expires = ";expires="+new Date(new Date().getTime()+ tm).toUTCString();   
        return expires;   
    }   
    function setPath(path){   
        if(!path) return "";   
        return (";path="+path);   
    }   
    function setDomain(domain){   
        if(!domain) return "";   
        return (";domain="+domain);   
    }   
}
ckn = new jsCookie();   
ckn.expires = 'hour';   
ckn.path ="/";
ckn.domain = "99114.com";
var $d = function(id){return document.getElementById(id);}
/********************************************************************************************/
function getValue(sNameKey)
{
    sNameKey = sNameKey.toUpperCase()
    var arrPara = new Array();
    arrPara = window.location.href.toUpperCase().split("?");
        if(arrPara.length >=2)
        {
           arrPara = arrPara[1].split("&");
           for(var i=0  ; i < arrPara.length ; i++)
           {
                if(arrPara[i].indexOf("=")!=-1)
                {
                    if(arrPara[i].indexOf(sNameKey +"=")==0)
                    {
                    return arrPara[i].split("=")[1].toLowerCase();
                    }
                }
           }
        }
        return "";
}
/********************************总站头部航******************************/
function loadTopSearch(name,siteId){
    var arrItems = new Array();
    arrItems[0] = new Array("供应",true,"请输入您感兴趣的内容","http://www.99114.com/Supply/List.shtml","supply");
    arrItems[1] = new Array("求购",true,"请输入您感兴趣的内容","http://www.99114.com/Buy/List.shtml","buy","Buy/Detail");
    arrItems[2] = new Array("公司",true,"请输入您感兴趣的内容","http://www.99114.com/Corporation/List.Shtml","corporation");
    arrItems[3] = new Array("招商",true,"请输入您感兴趣的内容","http://www.99114.com/Information/List.Shtml","information");
    arrItems[4] = new Array("展会",false,"请输入您感兴趣的内容","/Exhibition/List.shtml","exhibit");
    arrItems[5] = new Array("行情",false,"请输入您感兴趣的内容","","market");
    arrItems[6] = new Array("社区",false,"请输入您感兴趣的内容","http://bbs.99114.com/","bbs");
    arrItems[7] = new Array("B2B",false,"请输入您感兴趣的内容","http://www.99114.com/Tool/List.shtml","tool");
    arrItems[8] = new Array("进店网",false,"请输入您感兴趣的内容","http://www.jindian.com.cn/","jindian");
    arrItems[9] = new Array("资讯",false,"请输入您感兴趣的内容","/Article4/List.shtml","article4");
    arrItems[10] = new Array("资讯",false,"请输入您感兴趣的内容","/Article3/List.shtml","article3");
    arrItems[11] = new Array("资讯",false,"请输入您感兴趣的内容","/Article2/List.shtml","article2");
    arrItems[12] = new Array("资讯",false,"请输入您感兴趣的内容","/Article1/List.shtml","article1");
    arrItems[13] = new Array("资讯",false,"请输入您感兴趣的内容","http://www.99114.com/Article/List.shtml","article"); 
    this.name = name;
    this.txtObj = null;/*搜索输入框*/
    this.btnObj =null ;/*搜索按钮*/
    this.currentIndex = 0;/*搜索选项*/
    this.tabDiv = null;
    this.key="";
    this.isChild=false;
    this.isTabSearch =false;
    this.siteId=siteId;//siteId站点ID 可选参数
//        if(this.siteId)
//        {
//            arrItems=arrItems.insertAt(1,new Array("报价",true,"请输入您感兴趣的内容","/Quotation/","quotation"));
//        }
    this.arrItem = arrItems;/*选项数组*/
}
loadTopSearch.prototype.doSearch=function()
{
   if(!this.isTabSearch)
      this.getIndex();
   if(this.arrItem[this.currentIndex][2]==trim(this.txtObj.value) || trim(this.txtObj.value)=='' || trim(this.txtObj.value)=='请输入产品名称')
   {
        //总站搜索供应，不输入关键字跳转到大类别页
        if(this.siteId&&(this.currentIndex==0))
        {
            window.location.href="http://www.99114.com/Category/Category_0_0.shtml";
            return;
        }
        else
        {
            alert("请输入您感兴趣的关键词！");
            this.txtObj.select();
            return;
        }
   }
   this.key = this.txtObj.value;
   
    var productTypeUrl ="";
    var type = document.getElementsByName("TopSearchradioProductType");
    for(var i=0 ; i < type.length; i ++)
    {
        if(type[i].checked==true)
        {
            productTypeUrl =  "&t=" + type[i].value;
            break;
        }
    }
    
   ckn.set("searchKey",this.key);
   var searchUrl = this.arrItem[this.currentIndex][3] +"?k="+ encodeURIComponent(this.key) + productTypeUrl;
   
   //保存搜索关键字信息
   var stype;
   switch(this.currentIndex)
   {
        case 0:
         stype = 1  //找供应
         break
       case 1:
         stype = 2  //找采购
         break
       case 2:
         stype = 3  //找公司
         break
       case 3:
         stype = 4  //找招商
         break
       case 5:
       case 7:
       case 8:
       case 10:
       case 11:
       case 12:
       case 13:
         stype = 5  //找资讯
         break  
       default:
         stype = 1

   }
//   saveSearchKeyWord(this.key,stype);
   
  //添加mode参数
  if(getValue("mode")!='') 
  searchUrl=searchUrl+"&mode="+getValue("mode");
  
  //总站并且为报价搜索
//      if(this.siteId&&(this.currentIndex==1))
//      {
//        searchUrl=this.arrItem[this.currentIndex][3]+"List_0_0_{k}.shtml";
//        searchUrl=searchUrl.replace("{k}",encodeURIComponent(this.key))
//      }
   window.location.href=searchUrl;
}
loadTopSearch.prototype.getIndex=function()
{
    this.currentIndex=0;
    for(var i = 0 ; i < this.arrItem.length ; i++)
    {
        for(var j = 4; j < this.arrItem[i].length ; j++)
        {
            if(window.location.pathname.toUpperCase().indexOf(this.arrItem[i][j].toUpperCase())!=-1)
            {
                this.currentIndex = i;
                return;
            }           
        }
    }
}
 
loadTopSearch.prototype.setIndex=function(index,className)
{   
    this.currentIndex = index;
    if(!this.tabDiv)
    {
        alert("加载导航搜索栏必须要有一个容器!");
        return;
    }
    
    var sTemp="",sCss="",sSplit="";
    for(var i=0 ; i < this.arrItem.length ; i++)
    {
        if(!this.arrItem[i][1])
            continue;
        if(i == index)
        {
            sCss=className||"on";
        }
        else
        {
            sCss="";
        }
        if(className)
        {
//                if(i==1)
//                {
//            sTemp += "<li class=\""+ sCss+"\"><span class=\"newBG\"><a href=\"javaScript://\" onclick=\"javaScript:"+ this.name +".setIndex("+ i +",'"+className+"');" +"\">"+this.arrItem[i][0]+"</a></span></li>" ;
//                }
//                else
//                {
                sTemp += "<li class=\""+ sCss+"\"><a href=\"javaScript://\" onclick=\"javaScript:"+ this.name +".setIndex("+ i +",'"+className+"');" +"\">"+this.arrItem[i][0]+"</a></li>" ;
//                }
        }
        else
        sTemp += "<li class=\""+ sCss+"\"><a href=\"javaScript://\" onclick=\"javaScript:"+ this.name +".setIndex("+ i +");" +"\">"+this.arrItem[i][0]+"</a></li>" ;
    }
    this.btnObj.value =  "找" + this.arrItem[index][0];
    this.tabDiv.innerHTML = sTemp;
}
loadTopSearch.prototype.createHTML=function()
{
    this.getIndex();
    if(arguments.length>0&&arguments[0])
     this.setIndex(this.currentIndex,arguments[0]);
    else
     this.setIndex(this.currentIndex);
}

 
loadTopSearch.prototype.initEvents=function()
{
    /*初始化控件，添加事件*/
    var currentObj = this;
    this.getIndex();
    if(this.txtObj)
    {
    
        var key = this.arrItem[this.currentIndex][2];
        var tempKey = getValue("k") || ckn.get("searchKey");

        if(this.isChild)
            key ="请输入产品名称";
            
        if(trim(decodeURIComponent(tempKey))=="" || trim(decodeURIComponent(tempKey))== key)
            this.txtObj.value = key;
        else
            this.txtObj.value = trim(decodeURIComponent(tempKey));
            
        this.txtObj.onkeypress=function(e){
            var iKeyCode = window.event?event.keyCode:e.which;
            if(iKeyCode==13)
            {
               currentObj.doSearch();
               return false;
            }
            return true;
        }
        this.txtObj.onfocus=function(e)
        {
           this.select();
        }
    }
    if(this.btnObj)
    {
        this.btnObj.onclick=function(){
            /*按钮的onclick事件=doSearch()*/
            currentObj.doSearch();
            return false;
            }
    }
    if(this.imgObj)
    {
        this.imgObj.onclick=function(){
            currentObj.doSearch();
            return false;
            }
    }
}
