//Base64
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + //all caps
"abcdefghijklmnopqrstuvwxyz" + //all lowercase
"0123456789+/="; // all numbers plus +/=

//Heres the encode function
function encode64(inp){
	var out = ""; //This is the output
	var chr1, chr2, chr3 = ""; //These are the 3 bytes to be encoded
	var enc1, enc2, enc3, enc4 = ""; //These are the 4 encoded bytes
	var i = 0; //Position counter

	do { //Set up the loop here
	chr1 = inp.charCodeAt(i++); //Grab the first byte
	chr2 = inp.charCodeAt(i++); //Grab the second byte
	chr3 = inp.charCodeAt(i++); //Grab the third byte

	//Here is the actual base64 encode part.
	//There really is only one way to do it.
	enc1 = chr1 >> 2;
	enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	enc4 = chr3 & 63;

	if (isNaN(chr2)) {
		enc3 = enc4 = 64;
	} else if (isNaN(chr3)) {
		enc4 = 64;
	}

	//Lets spit out the 4 encoded bytes
	out = out + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) +
	keyStr.charAt(enc4);

	// OK, now clean out the variables used.
	chr1 = chr2 = chr3 = "";
	enc1 = enc2 = enc3 = enc4 = "";

	} while (i < inp.length); //And finish off the loop

	//Now return the encoded values.
	return out;
}

//Heres the decode function
function decode64(inp){
	var out = ""; //This is the output
	var chr1, chr2, chr3 = ""; //These are the 3 decoded bytes
	var enc1, enc2, enc3, enc4 = ""; //These are the 4 bytes to be decoded
	var i = 0; //Position counter

	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	var base64test = /[^A-Za-z0-9\+\/\=]/g;

	if (base64test.exec(inp)) { //Do some error checking
	alert("There were invalid base64 characters in the input text.\n" +
	"Valid base64 characters are A-Z, a-z, 0-9, ?+?, ?/?, and ?=?\n" +
	"Expect errors in decoding.");
	}
	inp = inp.replace(/[^A-Za-z0-9\+\/\=]/g, "");

	do { //Here the decode loop.

	//Grab 4 bytes of encoded content.
	enc1 = keyStr.indexOf(inp.charAt(i++));
	enc2 = keyStr.indexOf(inp.charAt(i++));
	enc3 = keyStr.indexOf(inp.charAt(i++));
	enc4 = keyStr.indexOf(inp.charAt(i++));

	//Heres the decode part. There��s really only one way to do it.
	chr1 = (enc1 << 2) | (enc2 >> 4);
	chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
	chr3 = ((enc3 & 3) << 6) | enc4;

	//Start to output decoded content
	out = out + String.fromCharCode(chr1);

	if (enc3 != 64) {
		out = out + String.fromCharCode(chr2);
	}
	if (enc4 != 64) {
		out = out + String.fromCharCode(chr3);
	}

	//now clean out the variables used
	chr1 = chr2 = chr3 = "";
	enc1 = enc2 = enc3 = enc4 = "";

	} while (i < inp.length); //finish off the loop

	//Now return the decoded values.
	return out;
}

function getCookie(name){
	var cname = name + "=";
	var dc = document.cookie;

	if (dc.length > 0) {
		begin = dc.indexOf(cname);

		if (begin != -1) {
			begin += cname.length;
			end = dc.indexOf(";", begin);

			if (end == -1) {end = dc.length;}
			return unescape(dc.substring(begin, end));
		}
	}
	return "";
}

function setCookie(name, value){
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;

	var path = (3 < argc) ? argv[3] : null;
	var domain = (4 < argc) ? argv[4] : null;
	var secure = (5 < argc) ? argv[5] : false;
	var expires =(2 < argc) ? argv[2] : null;
	var newcookie = name + "=" + value +
	((expires == null) ? "" : ("; expires="+expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
	document.cookie = newcookie;
}

if(getCookie("GCY_UVID") == null || getCookie("GCY_UVID") == ""){
	var cookieval = new Date();
	cookieval = cookieval.getTime();
	var rStr_1 = "" + Math.random();
	var rStr_2 = "" + Math.random();
	var rStr_3 = "" + Math.random();
	var rStr_4 = "" + Math.random();
	var rStr_5 = "" + Math.random();
	rStr_1 = rStr_1.charAt(2);
	rStr_2 = rStr_2.charAt(2);
	rStr_3 = rStr_3.charAt(2);
	rStr_4 = rStr_4.charAt(2);
	rStr_5 = rStr_5.charAt(2);

	var uv_expired_data = new Date(2011,1,1);
	cookieval = cookieval + rStr_1 + rStr_2 + rStr_3 + rStr_4 + rStr_5;
	setCookie("GCY_UVID", encode64(cookieval) , uv_expired_data, "/", ".cyworld.com.cn", false);
}
function getAllyesChannelid(str_ad){
	//str_ad = "linkid%3D12457%26channelid%3D45789";
	var strArr = str_ad.split("%26");
	var result = "";
	for(var i=0;i<strArr.length;i++){
		var temp = strArr[i].split("%3D");
		if(temp[0]=="channelid"){
			result = temp[1];
		}
	}

	return result;
}

function pageStatistics(event_code){
	i = new Image();

	if(typeof(allyes_channelid)!="undefined" && allyes_channelid!=""){
		//allyes_channelid 为好耶广告的来源依据
		i.src =  "http://st.cyworld.com.cn/stat.tiff?e_cd=["+event_code+"]&from="+from_ad_id+"&allyes_channelid="+allyes_channelid;
	}else if(typeof(from_ad_id)!="undefined" && from_ad_id!=""){
		i.src =  "http://st.cyworld.com.cn/stat.tiff?e_cd=["+event_code+"]&from="+from_ad_id;
	}else{
		i.src =  "http://st.cyworld.com.cn/stat.tiff?e_cd=["+event_code+"]";
	}
}
function relationStatistics(cyid, mode){
	i = new Image();
	i.src =  "http://st.cyworld.com.cn/friend_recv.tiff?cyid="+cyid+"&arg="+mode+"";
}
function boardStatistics(cyid, file_cnt){
	i = new Image();
	i.src =  "http://st.cyworld.com.cn/board_recv.tiff?cyid="+cyid+"&arg="+file_cnt+"";
}