/*
var locurl = location.href;
var num=locurl.indexOf("?");
locurl=locurl.substr(num+1);

var referurl = document.referrer;

var img = new Image();

img.src="http://pccinfo.baihe.com/ckadinfo.jsp?"+locurl+"&ReferUrl="+escape(referurl);
*/
/*Cookie操作*/
// utility function called by getCookie( )
function pcc_getCookieVal(offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}

// primary function to retrieve cookie by name
function pcc_getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return pcc_getCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
    }
    return "";
}

// store cookie value with optional details as needed
function pcc_setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

// remove the cookie by setting ancient expiration date
function pcc_deleteCookie(name,path,domain) {
    if (pcc_getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

// utility function to retrieve an expiration date in proper
// format; pass three integer parameters for the number of days, hours,
// and minutes from now you want the cookie to expire (or negative
// values for a past date); all three parameters are required,
// so use zeros where appropriate
function pcc_getExpDate(days, hours, minutes) {
    var expDate = new Date( );
    if (typeof days == "number" && typeof hours == "number" && 
        typeof hours == "number") {
        expDate.setDate(expDate.getDate( ) + parseInt(days));
        expDate.setHours(expDate.getHours( ) + parseInt(hours));
        expDate.setMinutes(expDate.getMinutes( ) + parseInt(minutes));
        return expDate.toGMTString( );
    }
}

var pcc_ck_name = "cookie_pcc";

var policy_seo = "601";
var policy_link = "701";

function generateADInfoCookie(){
	//从页面参数获取pcc
	var pcc_cookie = getPccFromCookie();

	var policy = "";

	if(pcc_cookie != null){		
		policy = pcc_cookie.policy;

		if(policy != null && policy.length > 0 && policy != policy_seo && policy != policy_link){
			return;
		}	
	}

	var pcc_param = getPccFromParam();

	if(pcc_param.policy.length + pcc_param.Channel.length + pcc_param.Code.length == 0){
		
		if(policy != policy_seo){
			var pcc_refer = getPccFromRefer();
			if(pcc_refer != null){
				if(pcc_refer.policy == policy_seo || policy != policy_link){
					setPccCookie(pcc_ck_name,pcc_refer);
				}
			}
		}

	}else{//pcc_param
		setPccCookie(pcc_ck_name,pcc_param);
	}

}

function getPccFromParam(){
	var param_pcc = {"policy":"","Channel":"","Code":"","ReferUrl":document.referrer};
	
	var locurl = location.href;
	var num=locurl.indexOf("?");
	
	if(num > 0 && num < locurl.length){
		locurl=locurl.substr(num+1);

		var arr = locurl.split("&");
		
		for(var i = 0 ; i < arr.length ; i ++){
			var p = arr[i];

			var arr1 = p.split("=");
			
			if(arr1[0] == "policy"){
				param_pcc.policy = arr1[1];
			}else if(arr1[0] == "Channel"){
				param_pcc.Channel = arr1[1];
			}else if(arr1[0] == "Code"){
				param_pcc.Code = arr1[1];
			}
		}
	}

	return param_pcc;
}

function getPccFromCookie(){
	var ck_str = pcc_getCookie(pcc_ck_name);

	if(ck_str != ""){
		ck_str = unescape(ck_str);
		
		var arr = ck_str.split("||");

		return {"policy":arr[0],"Channel":arr[1],"Code":arr[2],"ReferUrl":arr[3]};
	}else{
		return null;
	}
}

function getPccFromRefer(){
	var referurl = document.referrer;

	if(referurl == ""){
		return null;
	}

	var pos = referurl.indexOf("?");
	var subrefer = referurl;
	if(pos > 0){
		subrefer = referurl.substring(0,pos);
	}
	if(subrefer.indexOf("baihe.com") > 0){
		return null;
	}
	
	for(var i = 0 ; i  < seoUrlArr.length ; i ++){
		if(referurl.indexOf(seoUrlArr[i][1]) == 0){
			return {"policy":policy_seo,"Channel":seoUrlArr[i][0],"Code":"","ReferUrl":referurl};
		}
	}
	
	referurl = referurl.replace("?","/");
	var pos0 = referurl.indexOf("//");
	var pos1 = referurl.indexOf("/",pos0+2);

	if(pos1 < 0){
		pos1 = referurl.length;
	}

	return {"policy":policy_link,"Channel":referurl.substring(pos0+2,pos1),"Code":"","ReferUrl":referurl};
}

var seoUrlArr = [["www.baidu.com","http://www.baidu.com/s?"],
			["www.google.cn","http://www.google.cn/search?"],
			["www.google.cn","http://search.yahoo.com/search?"],
			["search.cn.yahoo.com","http://search.cn.yahoo.com/s?"],
			["search.cn.yahoo.com","http://search.cn.yahoo.com/s?v=web&ei=gbk"],
			["search.cn.yahoo.com","http://search.cn.yahoo.com/search?"],
			["search.cn.yahoo.com","http://one.cn.yahoo.com/s?v=web&ei=gbk"],
			["search.cn.yahoo.com","http://one.cn.yahoo.com/s?"],
			["www.sogou.com","http://www.sogou.com/web?"],
			["www.soso.com","http://www.soso.com/q?"],
			["www.youdao.com","http://www.youdao.com/search?"]];


function setPccCookie(ckName,pcc){
	pcc_setCookie(ckName,pcc.policy+"||"+pcc.Channel+"||"+pcc.Code+"||"+pcc.ReferUrl,pcc_getExpDate(365,0,0),"/","baihe.com");
}

generateADInfoCookie();