﻿//代替document.getElementById为$(id)
function $(id) {
	return document.getElementById(id);
}

//将验证信息反馈到ObjectID中
function ShowCheckResult(ObjectID, Message, ImageName) {
	obj = $(ObjectID);
	obj.style.display = '';
	obj.innerHTML = '<img src="images/check_'+ImageName+'.gif" align=absmiddle>&nbsp;' + Message;
}

//Ajax Start
function Ajax_GetXMLHttpRequest() {
	if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} 
	else if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
}

function Ajax_CallBack(ID,URL)
{
	var ID = $(ID);
	ID.innerHTML = '<img src="images/check_loading.gif" align=absmiddle>';
	var x = Ajax_GetXMLHttpRequest();
	x.open("POST",URL);
	x.setRequestHeader("REFERER", location.href);
	x.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
	x.onreadystatechange = function()
	{
		if(x.readyState == 4 && x.status == 200)
		{
			ID.innerHTML=x.responseText;
			ID.style.display = '';
			//x=null;
		}
	}
	var encodedData=""
	encodedData=escape(URL);
	x.send(encodedData);
}

function isNumberString (InString,RefString)
{
	if(InString.length==0) return (false);
	for (Count=0; Count < InString.length; Count++)  
	{
		TempChar= InString.substring (Count, Count+1);
		if (RefString.indexOf (TempChar, 0)==-1)  
		return (false);
	}
	return (true);
}
function CheckUserName(UserName,objid) {
	if(UserName.length < 4 || UserName.length >20) {
		ShowCheckResult(objid,"The length of user name should be within the scope of 4-20","error");
		return false;
	}
	if(isNumberString(UserName,"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.")!=1)
	{
		ShowCheckResult(objid,"Username illegal","error");
		return false;	
	}
	Ajax_CallBack(objid,"load.asp?action=CheckUserName&username="+UserName);
}
function CheckPassword(password,objid)
{
	if(password.length < 6 || password.length >16) {
		ShowCheckResult(objid,"Password length should be within the scope of 6-16","error");
		return false;
	}
	ShowCheckResult(objid,"","right");
}
function CheckPassword2(password,objid)
{
	if(password!=$("Password").value)
	{
		ShowCheckResult(objid,"Two different password","error");
		return false;
	}
	if(password=="")
	{
		ShowCheckResult(objid,"Passwords can not be empty","error");
		return false;		
	}
	ShowCheckResult(objid,"","right");
}
function CheckQuestion(question,objid)
{
	if(question=="")
	{
		ShowCheckResult(objid,"The problem can not be empty","error");
		return false;
	}
	ShowCheckResult(objid,"","right");
}
function CheckAnswer(answer,objid)
{
	if(answer=="")
	{
		ShowCheckResult(objid,"The answer can not be empty","error");
		return false;
	}
	ShowCheckResult(objid,"","right");
}

function CheckUserMail(Mail,objid) {
	if(Mail.indexOf("@") == -1 || Mail.indexOf(".") == -1) {
		ShowCheckResult(objid, "Please enter the correct E-mail Address","error");
		return false;
	}
	Ajax_CallBack(objid,"load.asp?action=CheckMail&Mail=" + escape(Mail));
}
function CheckREG()
{

	if($("Account").value=="")
	{
		ShowCheckResult('CKAccount',"Please enter the account number to register","error");
		return false;
	}
	
	if($("Account").value.length<4 || $("Account").value.length>20 || isNumberString($("Account").value,"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.")!=1)
	{
		ShowCheckResult('CKAccount',"User name from the 4-20 bit characters","error");
		return false;	
	}
	if($("Password").value=="")
	{
		ShowCheckResult('CKPassword',"Passwords can not be empty","error");	
		return false;		
	}
	if($("Password").value.length<6 || $("Password").value.length>16)
	{
		ShowCheckResult('CKPassword',"Password length should be 6-16 bit","error");	
		return false;		
	}
	if($("Password").value!=$("Password2").value)
	{
		ShowCheckResult('CKPassword',"Enter two different passwords","error");	
		return false;
	}
	if($("Question").value=="")
	{
		ShowCheckResult('CKQuestion',"Please enter the problem","error");	
		return false;		
	}
	if($("Answer").value=="")
	{
		ShowCheckResult('CKAnswer',"Please enter the answer","error");	
		return false;		
	}
	if($("mail").value=="")
	{
		ShowCheckResult('CKMail',"E-mail address can not be empty","error");	
		return false;	
	}
	if($("mail").value.indexOf("@") == -1 || $("mail").value.indexOf(".") == -1) {
		ShowCheckResult('CKMail',"E-mail address is not in the right format","error");	
		return false;
	}
	function disabledSubmit(id)
	{
		if ($(id) != null)
		{
			$(id).disabled = true;
		}
		return false;
	}
	disabledSubmit("Submit");
	return true;	
}
function CheckLogin()
{
	if($("Account").value=="")
	{
		alert("Please enter the account number to login!");
		$("Account").focus();
		return false;
	}
	if($("Password").value=="")
	{
		alert("Please enter a password!");
		$("Password").focus();
		return false;
	}
	function disabledSubmit(id)
	{
		if ($(id) != null)
		{
			$(id).disabled = true;
		}
		return false;
	}
	disabledSubmit("Submit");
	return true;
}

function CheckTLG()
{
	if(LGForm.UName.value=="")
	{
		alert("Please enter the account number to login!");
		LGForm.UName.focus();
		return;
	}
	if(LGForm.UPassword.value=="")
	{
		alert("Please enter a password!");
		LGForm.UPassword.focus();
		return;
	}
	document.LGForm.submit();
}