///////////////////////////////////////////////////////////////////////////////
//																			 //
//				二跳页面统计程序-By Simon Huang 2007-02-07					 //
//												Last Modified 2007-03-23	 //
//																			 //
///////////////////////////////////////////////////////////////////////////////
var union;

var uuid;
var siter_id;
var ad_id;
var ader_id;
var site_id;
var order_id;
var old_url;

var dm_id;
var dm_webid;
var dm_key;

stat_main(1,1);

///////////////////////////////////////////////////////////////////////////////
//																			 //
//						  　　　	主函数		　　						 //
//																			 //
///////////////////////////////////////////////////////////////////////////////

function stat_main(makecookie,freshform)//参数(是否种cookie,是否统计表单提交行为)
{

	if (get_para()) {

		uuid = para[1];

		siter_id = para[2];

		ad_id = para[3];

		ader_id = para[4];

		order_id = para[5];

		site_id = para[6];

		old_url = parament[0];
	
		response_head();
		
		if (makecookie == 1)
		{
			make_cookie();
		}

		var timer = setInterval("fresh_onclick()",1000);//为链接,JS点击行为添加汇报行为

		if (freshform == 1)
		{
			setInterval("fresh_form();",1000);//为表单添加汇报行为
		}

	} else 

		return false;

}

///////////////////////////获取二跳参数//////////////////////////////

function get_para() 
{
	var url = top.location.href;

	if (url.indexOf('?8le8le=') != -1)
	{
		parament = url.split('?8le8le=');

		union = '8le8le';
	}
	else if (url.indexOf('&8le8le=') != -1)
	{
		parament = url.split('&8le8le=');

		union = '8le8le';
	}
	else if (url.indexOf('51cpm=dm&') != -1)
	{
		parament = url.split('51cpm=dm&'); 
		
		union = 'dm';
	}else

	return false;

	if (parament[1]==null)
	{
		return false;
	}

	para = parament[1].split("|");

	return para;

	return parament;
}

///////////////////////////着陆汇报//////////////////////////////////////////

function response_head() {

	if (union == 'dm')
	{
		return false;
	}else if (uuid == 'AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE')
	{
		alert('二跳着陆汇报成功！');
	}

	var notnull = /^\S+$/;

	var isnumber = /^\d+$/;

	if (notnull.exec(uuid) && isnumber.exec(siter_id) && isnumber.exec(ad_id) && isnumber.exec(ader_id) && isnumber.exec(site_id) && isnumber.exec(order_id))
	{
		var url_str = "http://secondrefer.8le8le.com/header.html?uuid=" + uuid + "&siter_id=" + siter_id + "&ad_id=" + ad_id + "&ader_id=" + ader_id + "&order_id=" + order_id + "&site_id=" + site_id;document.write("<iframe src="+url_str+" width=0 height=0 frameborder=0 marginwidth=0 marginheight=0 hspace=0 vspace=0 scrolling=no></iframe>");
	}
	return true;
}	

///////////////////////////页尾汇报//////////////////////////////////////////

function response_foot() {

	if (uuid == 'AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE')
	{
		alert('二跳打开汇报成功！');
	}

	var notnull = /^\S+$/;

	var isnumber = /^\d+$/;

	if (notnull.exec(uuid) && isnumber.exec(siter_id) && isnumber.exec(ad_id) && isnumber.exec(ader_id) && isnumber.exec(site_id) && isnumber.exec(order_id))
	{
		var url_str = "http://secondrefer.8le8le.com/footer.html?uuid=" + uuid + "&siter_id=" + siter_id + "&ad_id=" + ad_id + "&ader_id=" + ader_id + "&order_id=" + order_id + "&site_id=" + site_id;document.write("<iframe src="+url_str+" width=0 height=0 frameborder=0 marginwidth=0 marginheight=0 hspace=0 vspace=0 scrolling=no></iframe>");
	}
	return true;
}	

///////////////////////////成功注册汇报//////////////////////////////////////////

function response_reg() {

	var notnull = /^\S+$/;

	var isnumber = /^\d+$/;

	if (notnull.exec(uuid) && isnumber.exec(siter_id) && isnumber.exec(ad_id) && isnumber.exec(ader_id) && isnumber.exec(site_id) && isnumber.exec(order_id))
	{
		var url_str = "http://secondrefer.8le8le.com/succeed.html?uuid=" + uuid + "&siter_id=" + siter_id + "&ad_id=" + ad_id + "&ader_id=" + ader_id + "&order_id=" + order_id + "&site_id=" + site_id;document.write("<iframe src="+url_str+" width=0 height=0 frameborder=0 marginwidth=0 marginheight=0 hspace=0 vspace=0 scrolling=no></iframe>");
	}
	return true;
}	

//////////////////////////////Make Cookie//////////////////////////////////////

function make_cookie() {

	var value = parament[1];

	var expires = new Date();

	expires.setTime(expires.getTime() + 2 * 60 * 60 * 1000);

	document.cookie = "8le8le=" + value + ";path=/;expires=" + expires.toGMTString();
}
//////////////////////////////Get Cookie///////////////////////////////////////

function get_cookie() {

}
///////////////////////////////////////////////////////////////////////////////
//																			 //
//								  生成随机数								 //
//																			 //
// The Central Randomizer 1.3 (C) 1997 by Paul Houle (houle@msc.cornell.edu) //
//																			 //
// See: http://www.msc.cornell.edu/~houle/javascript/randomizer.html		 //
//																			 //
///////////////////////////////////////////////////////////////////////////////

rnd.today=new Date();

rnd.seed=rnd.today.getTime();

function rnd() {

	rnd.seed = (rnd.seed*9301+49297) % 233280;

	return rnd.seed/(233280.0);

}

function rand(number) {

	return Math.ceil(rnd()*number);

}

// end central randomizer.

///////////////////////////////////////////////////////////////////////////////
//																			 //
//						  为统计对象添加统计行为							 //
//																			 //
///////////////////////////////////////////////////////////////////////////////

function fresh_onclick()
{
	if (top.location.href.indexOf("8le8le=") == -1 && top.location.href.indexOf("51cpm=dm&") == -1)
	{
		return false;
	}

	var r_a = document.getElementsByTagName("a");//获取所有链接

	var r_img = document.getElementsByTagName("img");//获取所有图片

	var r_form = document.getElementsByTagName("form");

	for (i = 0; i < r_a.length; i ++)//为所有链接添加onclick汇报行为
	{
		if (r_a[i].getAttribute("onclick") != null)
		{
			r_a[i].setAttribute("target","_blank");

			var getoc = r_a[i].getAttribute("onclick") + "";

			if (getoc.indexOf("response_click(this.href)") != -1)//判断是否已经添加汇报行为
			{
				continue;
			}

			var oc_start = getoc.indexOf("{") + 1;

			var oc_end = getoc.lastIndexOf("}");

			var oldoc = getoc.substring(oc_start,oc_end);

			var newoc = "response_click(this.href);" + oldoc;

			r_a[i].onclick = new Function (newoc);

		}
		else if (r_a[i].getAttribute("onclick") == null)
		{
			r_a[i].onclick = new Function ("response_click(this.href)");
		}
	}

	for (i = 0; i < r_img.length; i ++)//为所有有链接或点击行为的图片添加onclick汇报行为
	{
		if (r_img[i].getAttribute("onclick") != null)
		{
			var getoc = r_img[i].getAttribute("onclick") + "";

			if (getoc.indexOf("response_click(this.getAttribute(\"onclick\"))") != -1)//判断是否已经添加汇报行为
			{
				continue;
			}

			var oc_start = getoc.indexOf("{") + 1;

			var oc_end = getoc.lastIndexOf("}");

			var oldoc = getoc.substring(oc_start,oc_end);

			var newoc = "response_click(this.getAttribute(\"onclick\"));" + oldoc;

			r_img[i].onclick = new Function (newoc);
		}
	}

}

//////////////////////////////为表单添加统计行为///////////////////////////////

function fresh_form()
{
	if (top.location.href.indexOf("8le8le=") == -1)
	{
		return false;
	}

	var r_form = document.getElementsByTagName("form");

	for (i = 0; i < r_form.length; i ++)
	{
		if (r_form[i].getAttribute("onsubmit") != null)
		{
			var getos = r_form[i].getAttribute("onsubmit") + "";

			if (getos.indexOf("response_click()") != -1)
			{
				continue;
			}

			var os_start = getos.indexOf("{") + 1;

			var os_end = getos.lastIndexOf("}");

			var oldos = getos.substring(os_start,os_end);

			var newos = "response_click();" + oldos;

			r_form[i].onsubmit = new Function (newos);
		}
		else if (r_form[i].getAttribute("onsubmit") == null)
		{
			r_form[i].onsubmit = new Function ("response_click()");
		}
//alert(r_form[i].onsubmit);
	}
}

///////////////////////////////////////////////////////////////////////////////
//																			 //
//							返回点击统计请求函数							 //
//																			 //
///////////////////////////////////////////////////////////////////////////////

function response_click(action) 
{
	click_num = rand(10000);//第n次有效点击

	var image = document.createElement("<img></img>");// create a new empty element    

	image.height = 0;

	image.width = 0;

	switch (union)
	{
	case '8le8le':

		image.src = "http://secondrefer.8le8le.com/click.html?uuid=" + uuid + "&siter_id=" + siter_id + "&ad_id=" + ad_id + "&ader_id=" + ader_id + "&order_id=" + order_id + "&site_id=" + site_id + "&click_num=" + click_num + "&to=" + action;

		break;

	case 'dm':
	
		image.src = "http://refer.newcnad.com/click.html?" + parament[1] + "&click_num=" + click_num + "&to=" + action;

		break;
	}

	document.body.insertBefore(image);//send request to stat server

	if (uuid == 'AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE' || top.location.href.indexOf('id=dmtest') != -1)
	{
		alert('二跳点击汇报成功！指向目标为:' + action);
	}
	
	return true;
}