var nameExist = true;

function changeWay()
{
	if( $("way3").checked==true){
		$("other").disabled=false;
	}
	else{
		$("other").disabled=true;
	}
}

function submitInvest()
{
	document.investForm.action="";
	document.investForm.style.display="none";
	document.getElementById("invest").innerHTML="<br />Thank you for your participating. Please go on to <a href=\"#signup\">register</a>."+document.getElementById("invest").innerHTML;
	return true;
}

//used in registration
function checkRegistrationInfo()
{
	var username = document.getElementById("loginName").value.Trim();
	var email = document.getElementById("email").value.Trim();
	var password = document.getElementById("password").value.Trim();

	if(!checkRUsername(username) || !checkRPassword(password)){
		//Recaptcha.reload();
		return false;
	}

	var firstname = document.getElementById("firstName").value.Trim();
	var lastname = document.getElementById("lastName").value.Trim();

	if(!check_firstName_and_lastName(firstname)){		//check_firstName_and_lastName function is in common.js
		alert("But we don't think your \"first Name\" is correct!");
		document.getElementById("firstName").focus();
		//Recaptcha.reload();
		return false;
	}

	if(!check_firstName_and_lastName(lastname)){		//check_firstName_and_lastName function is in common.js
		alert("But we don't think your \"Last Name\" is correct!\"");
		document.getElementById("lastName").focus();
		//Recaptcha.reload();
		return false;
	}
    if(email!=""){
	    if(!checkEmail(email)){
		    alert("But we don't think your \"Email\" is correct!");
		    document.getElementById("email").focus();
		    //Recaptcha.reload();
		    return false;
	    }
	 }

	document.getElementById("submit").disabled=true;
	return true;
}

//used in checkRegistrationInfo function above and update user info pages
//check in the client side if the username is legal
function checkRUsername(username)
{	
	if (!checkUserName(username))	//checkUserName function is in common.js
	{
		alert("Sorry, but the username you want is not legal..");
		document.getElementById("loginNamewarning").style.color="red";
		document.getElementById("loginName").focus();
		return false;
	}
	return true;
}

//used in checkRegistrationInfo function above and update user info pages
//check in the client side if the password is legal
function checkRPassword(password)
{
	if (password=="")	//checkPassword function if is common.js
	{
		alert("Sorry, please enter the password.");
		document.getElementById("password").focus();
		//document.getElementById("passwordwarning").style.color="red";
		return false;
	}

	var repeat = document.getElementById("repeat").value.Trim();
	if(repeat==""){
		alert("Please repeat password.");
		document.getElementById("repeat").focus();
		return false;
	}
	else if(repeat!=password){
		alert("Repeat Error!");
		document.getElementById("repeat").focus();
		return false;
	}

	return true;
}

function checkEmail(email)
{
	var regEmail=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;

	if(!regEmail.test(email)){
		return false;
	}
	return true;
}

function checkPhoneNumber(phoneNumber)
{
	var regPhone = /^(\d|-){0,50}$/;
	if (!regPhone.test(phoneNumber)){
		return false;
	}
	return true;
}

//used in registration page to check if the username which the user wants exists
//
function CheckIfNameExists()
{
	if(!checkRUsername()){
		return false;
	}
	var username = document.getElementById("loginName").value.Trim();

	var xmlHttp = createXMLHttpRequest();

	if(xmlHttp == null)
	{
		alert("Sorry, but your browser donot support XMLHttpRequest object enough.");
		return false;
	}

	xmlHttp.open("get","../backstagePages/whetherANameExists.asp?username="+username+"&xxx="+parseInt(Math.random()*1000),true);	
	xmlHttp.onreadystatechange=function()
	{
		if(4==xmlHttp.readyState)
		{
			if(200==xmlHttp.status)
			{
				var result = 4;
				var ret = xmlHttp.responseXML.getElementsByTagName("data")[0].childNodes[0].nodeValue;
				
				if(!isNaN(ret)){
					result = parseInt(ret);
				}
				
				switch(result){
					case 0:
						alert("You can use the username! Congratulations!");
						break;
					case 1:
						alert("Sorry, but it already Exists now..");
						break;
					case 2:
					case 4:
					default:
						alert("Query error! Try again or refresh your page!");
						break;
				}
			}
		}
	}
	xmlHttp.send(false);
	
}