﻿var $$ = function(id){ return document.getElementById(id); }
var trim = function(str) { return (str || "").replace( /^\s+|\s+$/g, "");}

var Ajax1 = function Ajax1(url)
{
	var m_xmlReq=null;
	if(window.ActiveXObject)
	{
	    try 
	    {
	        m_xmlReq = new ActiveXObject('Msxml2.XMLHTTP'); 
	    }
	    catch(e)
	    {
	        try{m_xmlReq = new ActiveXObject('Microsoft.XMLHTTP');}catch(e){}
	    }
	}
	else if(window.XMLHttpRequest)
	{
	    m_xmlReq = new XMLHttpRequest();
	}
	
	this.postResult=function(d)
	{
	    if(!m_xmlReq)  return;
	    m_xmlReq.open('POST',url,false);
	    m_xmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
	    m_xmlReq.send(d);	
	    var requesttxt=m_xmlReq.responseText;	    
	    return requesttxt;
	}
    this.getResult=function()
	{
	    if(!m_xmlReq)  return;
	    m_xmlReq.open('GET',url,false);
	    m_xmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
	    m_xmlReq.send(null);	
	    var requesttxt=m_xmlReq.responseText;	    
	    return requesttxt;
	}
}

//获取参数
function requestseacher(paras){   
	var url=location.href;
	var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");     
	var paraObj = {}     
	for (i=0; j=paraString[i]; i++){     
		paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);     
	}     
	var returnValue = paraObj[paras.toLowerCase()];     
	if(typeof(returnValue)=="undefined"){     
		return "";     
	}else{     
		return returnValue;     
	}     
}

var k6 =
{
    CookieGet : function(cookie_name)
    {
	    var arg = cookie_name + "=";
	    var ck = document.cookie;
	    if(ck.indexOf(arg) != -1) 
	    {
		    var vstr = ck.indexOf(arg) + arg.length;
		        if(ck.indexOf(";",vstr)==-1)
		            var vend = ck.length;
		        else 
		            var vend = ck.indexOf(";",vstr);
		        return unescape(ck.substring(vstr,vend));
	    }
	    return null;
    },
    CookieSet : function(cookie_name , cookie_val)
    {
	    var never = new Date();
	    never.setTime(never.getTime()+24*60*60*1000);
	    document.cookie = cookie_name+"="+cookie_val+";expires="+never.toGMTString()+";";
    },
    CookieDel : function(cookName)
    {   
        var date = new Date();
        date.setTime(date.getTime()-24*60*60*1000);
        document.cookie = cookName+"=000;expires="+date.toGMTString();
    },
    CookieDelAll : function()
    {
        var date = new Date();
        date.setTime(date.getTime()-24*60*60*1000);
        var ck=document.cookie.split(";");
        for(var i=0;i<ck.length;i++)
        {
            var cname = ck[i].split('=')[0];
            document.cookie = cname+"=;expires="+date.toGMTString();
        }
//        window.location = decodeURIComponent('/index.shtml');
        window.open ("http://www.k6.cn/index.shtml");
    },
    Login : function()
    {
       if(Number(this.CookieGet("_k6userid")) <= 0)
//          window.location = decodeURIComponent('/login.html');
          window.open ("http://www.k6.cn/login.html");
    },
    LocationRedirect : function(location,redirect)
    {        
        this.CookieSet("_k6locationRedirect",encodeURIComponent(location));
//        window.location = decodeURIComponent(redirect);
        window.open (redirect);
    },
    Location : function(location)
    {        
        this.CookieSet("_k6location",encodeURIComponent(location));
//        window.location = decodeURIComponent('/login.html');
        window.open ("http://www.k6.cn/login.html");
    },
    Location_href : function(location)
    {
        if(Number(this.CookieGet("_k6userid")) > 0)
        {
          if(location == '/CheckOut.html')
          {
           if(Number(k6.CookieGet("KDX.KLW.Cart")!=null?k6.CookieGet("KDX.KLW.Cart").split('|').length:"0")>0)
//               window.location = decodeURIComponent(location);
                window.open ("http://www.k6.cn/"+location);
          }
          else
//            window.location = decodeURIComponent(location);
             window.open (location);
        }
        else
           this.Location(location);
    },
    loginStart : function()
    {
      return "<a href='javascript:k6.Location(location);'>登陆</a> / <a href='javascript:k6.LocationRedirect(location,\"http://www.k6.cn/registered.html\")'>注册</a>";  },
    loginEnd : function()
    {
      return "您好！<a href='javascript:k6.MemberCenter();'>"+this.CookieGet((this.CookieGet('_k6username')==null || this.CookieGet('_k6username')=="")?"_k6useremail":"_k6username").split('@')[0]+"</a> [<a href='javascript:k6.CookieDelAll();'>退出</a>]";
    },
    MemberCenter : function()
    {
      this.Location_href((Number(k6.CookieGet("_k6userlevel")) > 0)?"/my_home_1.html":"/my_home_2.html");
    }
};
//k6.Login();

if($$("k6login") != null)
    $$("k6login").innerHTML = Number(k6.CookieGet("_k6userid")) > 0?k6.loginEnd():k6.loginStart();
if($$("k6_CartSum") != null)
    $$("k6_CartSum").innerHTML = k6.CookieGet("KDX.KLW.Cart")!=null?k6.CookieGet("KDX.KLW.Cart").split('|').length:"0";
if($$("pageHeader") != null)///HTML/Public/
    $$("pageHeader").innerHTML = new Ajax1("/HTML/Public/search.html?t="+new Date().getTime()).getResult();

//关键字汉字编码转换
var GBK2URL=function(A){//A:为关键字列表父容器ID；
var GBKas=document.getElementById(A).getElementsByTagName("a");
//alert(GBKas.length);
var t = "";
if(location.href.toString().indexOf("t=1")!=-1)t="&t=1";
for(var i=0;i<GBKas.length;i++){
	if(GBKas[i].href.indexOf("keyword")>-1){
	    GBKas[i].href=GBKas[i].href.split("keyword")[0]+"keyword="+escape(GBKas[i].href.split("keyword")[1])+t;
		}
}
}


//搜索-----------------------------------------------------
 var K6_Search = {
    keyWord : function(){ return requestseacher('KeyWord'); },
    keyWordFocus : function(){ if($$("KeyWord").value == "产品搜索：请输入关键字") $$("KeyWord").value = "";  },
    keyWordBlur : function(){  if(trim($$("KeyWord").value).length <= 0) $$("KeyWord").value = "产品搜索：请输入关键字"; 
    },
    searchEnter : function(evt){ var evt = window.event || evt ; if(evt.keyCode==13)this.search(1,0); },
    search :function(i,j){ var kw = $$("KeyWord"); if(trim(kw.value).length > 0 && kw.value != "产品搜索：请输入关键字"){ 
    //location = "/Search/"+(arguments.length==3?"IndexKeyWordSearch":"SearchSwastika")+".aspx?keyword="+escape(kw.value)+(i==1?"&c=1":"")+(j==1?"&t=1":"");
    var strkw=encodeURI(kw.value);
    location = "http://www.k6.cn/Search/SearchSwastika.aspx?keyword="+strkw+(i==1?"&c=1":"")+(j==1?"&t=1":"");
    }else{ alert('请输入要搜索的内容'); }
    }
 }
 
//-----------更改头部标题-------
var _top = $$("Ting");
if(_top){
var reg = new RegExp("<a[^<]+>(季节养生)</a>","i");
var ms = _top.innerHTML.match(reg);
if(ms) _top.innerHTML=_top.innerHTML.replace(ms[0],ms[0].replace(ms[1],"当季热销"));
}

function URIescape(wrap){
	var links=wrap.getElementsByTagName('A');
	var URI = "";
	var URIarr = null;
	for(var i=links.length;i--;){
	    URIarr = links[i].href.split("?");
	    if(URIarr.length>0){
	        links[i].href = links[i].href.replace(URIarr[1],encodeURI(URIarr[1]))
	    }
	    
	}
}

function defaultKeyword(str){
    if(document.getElementById("KeyWord")){
        document.getElementById("KeyWord").value=str;
    }
}


