var doc = null;
function ajax()
{
    if (window.XMLHttpRequest) {
        try {
            doc = new XMLHttpRequest();
        } catch(e)  {}
    } else if (window.ActiveXObject) {
        try {
            doc = new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e)  {
          try {
              doc = new ActiveXObject('Microsoft.XMLHTTP');
          } catch(e)  {}
        }
    }
}//Ajax engine

function FormatNumber(x)//Format Number (1.5 to 1.50)
{
	if (x=="")
	{
		window.location.href=window.location.href;//If x is null
	}
	
	var f_x = parseFloat(x);
	if (isNaN(f_x))
	{
		return false;
	}
	var f_x = Math.round(x*100)/100;
	var s_x = f_x.toString();
	var pos_decimal = s_x.indexOf('.');
	if (pos_decimal < 0)
	{
		pos_decimal = s_x.length;
		s_x += '.';
	}
	while (s_x.length <= pos_decimal + 2)
	{
		s_x += '0';
	}
	return s_x;
}

function LastMenu(s)
{
	if (s == '1')
	{
		document.getElementById("lastmenus").style.backgroundImage  = "url(ks.assets/topmenubg.jpg)";
		document.getElementById("lastmenus").style.backgroundRepeat = "no-repeat";
	}else{
		document.getElementById("lastmenus").style.backgroundImage  = "url(ks.assets/blank.png)";
	}
}


/*------------------------------------*/
function get_Category_View(s)
{
	if (document.getElementById("listdiv_"+s).style.display == 'none')
	{
		document.getElementById("lktitle_"+s).style.backgroundPosition = '0 -31px';
		document.getElementById("listdiv_"+s).style.display = 'block';
	}else{
		document.getElementById("lktitle_"+s).style.backgroundPosition = '0 -51px';
		document.getElementById("listdiv_"+s).style.display = 'none';
	}
}
/*------------------------------------*/

function CheckEmail(gm)
{
	var field = document.getElementById(gm);
	var lenemail = field.value.length;
	if(lenemail > 50)
	{
		StatFunction(gm,0,"对不起，您的电子邮件地址太长");
		return false;
	}
	
	kk1 = field.value.indexOf("@");
	kk2 = field.value.indexOf(".");
	kk3 = field.value.lastIndexOf("@");
	kk4 = field.value.lastIndexOf(".");
	if((kk1<=0)||(kk2<=0)||(kk1==lenemail-1)||(kk2==lenemail-1))
	{
		StatFunction(gm,0,"请输入正确的电子邮件地址");
		return false;
	}
	if((kk1!=kk3)||(kk1==kk2+1)||(kk2==kk1+1)||(kk3>kk4))
	{
		StatFunction(gm,0,"请输入正确的电子邮件地址");
		return false;
	}

	StatFunction(gm,1,"");

	var semail = document.getElementById("e_email").value;
	var eemail = document.getElementById("e_emailcon").value;

	if (semail != eemail)
	{
		StatFunction('e_emailcon',0,"对不起，确认邮箱不正确");
		return false;
	}
}

function CheckPassword()
{
	var p1 = document.getElementById("e_password").value;
	var p2 = document.getElementById("e_passwordcon").value;
	if (p1 != p2)
	{
		StatFunction('e_passwordcon',0,"对不起，确认密码不正确");
	}
}

function StatFunction(gm,st,txt)
{
	if (st == '0')
	{
		document.getElementById("k_"+gm).style.backgroundImage = "url(ks.assets/regsta.png)";
		document.getElementById("k_"+gm).style.backgroundRepeat = "no-repeat";
		document.getElementById("k_"+gm).style.backgroundPosition = "0 -15px";
		document.getElementById("k_"+gm).innerHTML = txt;
	}else{
		document.getElementById("k_"+gm).style.backgroundImage = "url(ks.assets/regsta.png)";
		document.getElementById("k_"+gm).style.backgroundRepeat = "no-repeat";
		document.getElementById("k_"+gm).style.backgroundPosition = "0px 0px";
		document.getElementById("k_"+gm).innerHTML = txt;
	}
}

function CheckRegForm()
{
	CheckEmail("e_email");
	CheckEmail("e_emailcon");
	if (document.getElementById("e_password").value == ''){StatFunction('e_password',0,"请输入登陆密码");return false;}
	CheckPassword();
	if (document.getElementById("e_code").value == ''){StatFunction('e_code',0,"请输入验证码");return false;}
	if (document.getElementById("agrees").checked == false){alert("请阅读用户服务协议");return false;}

	var e = document.getElementById("e_email").value;
	var p = document.getElementById("e_password").value;
	var c = document.getElementById("e_code").value;

	ajax();
	if (doc)
	{
		doc.open("GET", "/tmtor.function.php?action=userreg&e="+e+"&p="+p+"&c="+c+"&t="+Math.random(),false);
		doc.send(null);
		if (doc.readyState==4 && doc.status==200)
		{
			var len = doc.responseText.split("****");
			if (len[0] == '0')
			{
				alert(len[1]);
			}else{
				window.location.href = window.location.href;
			}
		}
	}
}

function ReRegister()
{
	ajax();
	if (doc)
	{
		doc.open("GET", "/tmtor.function.php?action=reregister&t="+Math.random(),false);
		doc.send(null);
		if (doc.readyState==4 && doc.status==200)
		{
			window.location.href = window.location.href;
		}
	}
}

function TryLogin()
{
	if (document.getElementById("username").value == '')
	{
		alert('请输入您的登陆名称');
		return false;
	}
	if (document.getElementById("password").value == '')
	{
		alert('请输入您的登陆密码');
		return false;
	}
	var u = document.getElementById("username").value;
	var p = document.getElementById("password").value;
	ajax();
	if (doc)
	{
		doc.open("GET", "/tmtor.function.php?action=userlogin&u="+u+"&p="+p+"&t="+Math.random(),false);
		doc.send(null);
		if (doc.readyState==4 && doc.status==200)
		{
			var len = doc.responseText.split("****");
			if (len[0] == '0')
			{
				alert(len[1]);
			}else{
				window.location.href = "/zztms";
			}
		}
	}
}

function Swlink_Over(obj)
{
	obj.style.background = "url(/ks.assets/bdi2.png) no-repeat left center";
}

function Swlink_Out(obj)
{
	obj.style.background = "transparent";
}

function OpenExt(gid,gk)
{
	if (gk == 1)
	{
		document.getElementById("extio_"+gid).style.display = "block";
	}else{
		document.getElementById("extio_"+gid).style.display = "none";
	}
}

function SwitchPosiDiv(getid)
{
	document.getElementById("tmmdl-"+rightid).style.backgroundPosition = "-890px -33px";
	document.getElementById("mdld-"+rightid).style.display = "none";
	document.getElementById("tmmdl-"+rightid).style.color = "#555555";

	document.getElementById("tmmdl-"+getid).style.backgroundPosition = "-720px -33px";
	document.getElementById("mdld-"+getid).style.display = "block";
	document.getElementById("tmmdl-"+getid).style.color = "#0B5392";
	rightid = getid;
}

function SwitchPosiOm(getid)
{
	document.getElementById("tmom-"+rightid).style.backgroundPosition = "-890px -33px";
	document.getElementById("omd-"+rightid).style.display = "none";
	document.getElementById("tmom-"+rightid).style.color = "#555555";

	document.getElementById("tmom-"+getid).style.backgroundPosition = "-720px -33px";
	document.getElementById("omd-"+getid).style.display = "block";
	document.getElementById("tmom-"+getid).style.color = "#0B5392";
	rightid = getid;
}
