// JavaScript Document
var xmlHttp;
var objHTML;
var user;
function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
return xmlHttp;
}

function usernameCheck(u, p, b, e)
{
	var today = new Date(); 
user = u;
        objHTML = document.getElementById("username_verdict").innerHTML;
        objHTML = "Loading...";
        xmlHttp = GetXmlHttpObject();
        //var xmlHttp = main.xmlHttp;
        if (xmlHttp==null){objHTML="Your explore doesn't support this site, please use another to retry!";return;}
        var url;
		if (b == 1) {
        url="my_user_reg.php";
        url+="?acct="+u+"&pswd="+p+"&mail="+e;
		} else {
        url="my_Mod_pswd.php";
        url+="?acct="+u+"&pswd="+p+"&newpswd="+e;
		}
	url+="&sn="+today.getTime();
        xmlHttp.onreadystatechange=ChangeCont;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
    }
     
function ChangeCont()
{
    if(xmlHttp.readyState==4)
        {
			if (xmlHttp.responseText == "1") {
        		document.getElementById("username_verdict").innerHTML="<center><br>congratulate!<br>Account (<font color=#ff0000>"+user+"</font>) has been successfully created.<br>Please wait 2 - 3 minutes, then login.<br><br><br></center>";
			} else if (xmlHttp.responseText == "0") {
        		document.getElementById("username_verdict").innerHTML="<center><br>Registration failed!<br>This username is used by other player.<br>Please use some else to <a href=Account.php>retry</a>.<br>Good luck<br><br><br><br></center>";
			} else if (xmlHttp.responseText == "2") {
        		document.getElementById("username_verdict").innerHTML="<center><br>You've succeed to change your password.<br>Please storage it carefully</center>";
			} else if (xmlHttp.responseText == "3"){
        		document.getElementById("username_verdict").innerHTML="<center><br>it's might the following make your request refushed:<ul><li>The account doesn't exsit.</li><li>You have submit a wrong password.</li><li>we will delete the accouts who haven't login for 3 monthes.<br>Did you?</li><li>Your new password is the same as the old one.</li><li>Your account has been banned!</li></ul></center>";
			} else {
        		document.getElementById("username_verdict").innerHTML=objHTML;
			}
        }
}


function OnUserReg()
{
	if (document.reg.login.value == "") {
		alert("Please make sure that ur login is not empty!");
		return;
	}
	if (document.reg.login.value.length < 4 ||document.reg.login.value.length > 10) {
		alert("account must more then 4 words and less then 10 words!");
		return;	
	}
	if (document.reg.password.value.length < 4 ||document.reg.password.value.length > 10) {
		alert("password must more then 4 words and less then 10 words!");
		return;		
	}
	if (document.reg.password.value != document.reg.password2.value) {
		alert("Please sure ur [Retype Password] is the same to ur [Retype Password]!");
		return;
	}
	if (document.reg.email.value == "") {
		alert("Please make sure that ur email is not empty!");
		return;
	}
	usernameCheck(document.reg.login.value, document.reg.password.value, 1, document.reg.email.value);
}

function OnModPswd()
{
	if (document.mdpswd.account.value == "") {
		alert("Please make sure that ur [Account Name] is not empty!");
		return;
	}
	if (document.mdpswd.passwordOld.value == "") {
		alert("Please make sure that ur [Old-Password] is not empty!");
		return;
	}
	if (document.mdpswd.passwordNew.value == "") {
		alert("Please make sure that ur [New-Password] is not empty!");
		return;
	}
	if (document.mdpswd.passwordNew.value.length < 4 ||document.mdpswd.passwordNew.value.length > 10) {
		alert("password must more then 4 words and less then 10 words!");
		return;		
	}
	if (document.mdpswd.passwordNew.value != document.mdpswd.passwordNew1.value) {
		alert("Please sure ur [Re-Enter new password] is the same to ur [New-password]!");
		return;
	}
	usernameCheck(document.mdpswd.account.value, document.mdpswd.passwordOld.value, 0, document.mdpswd.passwordNew.value);
}
function SubmitVote(form)
{
var char;
var code;
var inputCode;
switch(form) {
case 1:
	char = document.sitevote1.char.value;
	code = document.sitevote1.checkCode.value;	
	inputCode = document.sitevote1.code.value;
	break;
case 2:
	char = document.sitevote2.char.value;
	code = document.sitevote2.checkCode.value;	
	inputCode = document.sitevote2.code.value;
	break;
case 3:
	char = document.sitevote3.char.value;
	code = document.sitevote3.checkCode.value;	
	inputCode = document.sitevote3.code.value;
	break;
case 4:
	char = document.sitevote4.char.value;
	code = document.sitevote4.checkCode.value;	
	inputCode = document.sitevote4.code.value;
	break;
case 5:
	char = document.sitevote5.char.value;
	code = document.sitevote5.checkCode.value;	
	inputCode = document.sitevote5.code.value;
	break;
default :
	char = document.sitevote1.char.value;
	code = document.sitevote1.checkCode.value;	
	inputCode = document.sitevote1.code.value;
	break;
}
if (!char) {
	alert("Please insert u character!");
	return false;	
}
if (inputCode == "" ) {
	alert("Please insert correct code!");
	return false;
}
if (inputCode != code ) {
	alert("Please insert correct code!");
	return false;
}

}

function createCode(id){ 
	var code = "";
	var codeLength = 4;//验证码的长度
	var checkCode = document.getElementById(id);
	checkCode.value = "";
	var selectChar = new Array(0,1,2,3,4,5,6,7,8,9);
	for(var i=0;i<codeLength;i++) {
	   var charIndex = Math.floor(Math.random()*10);
	   code +=selectChar[charIndex];
	}
	if(code.length != codeLength) {
	   createCode();
	}
	checkCode.value = code;
}