var cnFlag = true;
var emailFlag = true;
//cn start
function cnOnFocus(obj) {
	obj.style.backgroundColor="";
	document.getElementById("cnEX").innerHTML = "Account ID is used to login to our games and account system, must be 5-16 characters in length. Use letters and digits or valid punctuation (dot, hyphen and underscore) and must start with a letter or digit.";
}

function cnValidate(obj) {
	document.getElementById("cnEX").innerHTML = "";
	obj.style.backgroundColor = "";
	var cn = obj.value;
	var reg = /^[a-zA-Z0-9]{1}[-._a-zA-Z0-9]{4,15}$/;
	if (!reg.test(cn)) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("cnEX").innerHTML = "<font color='red'>5-16 characters. Use letters and digits or valid punctuation (dot, hyphen, underscore) and must start with a letter or digit.</font>";
        return false;
	}
	//DWREngine.setAsync(false);
	cnFlag = true;
	CheckObject.validateCN(cn, cnDWRHandler);
	//DWREngine.setAsync(true);
	if (cnFlag == false) {
		return false;
	}

	obj.style.backgroundColor = "ccff99";
	return true;
}

function cnDWRHandler(flag) {
	if (flag == 7) {
		document.getElementById("cn").style.backgroundColor = 'pink';
		document.getElementById("cnEX").innerHTML = "<font color='red'>Account ID already exists. Please try another one.</font>";
		cnFlag = false;
	}
	if (flag == 6) {
		document.getElementById("cn").style.backgroundColor = 'pink';
		document.getElementById("cnEX").innerHTML = "<font color='red'>Account ID contains invalid characters. Please try another one.</font>";
		cnFlag = false;
	}
	if (flag == 999) {
		document.getElementById("cn").style.backgroundColor = 'pink';
		document.getElementById("cnEX").innerHTML = "<font color='red'>There was an error verifying the Account ID.</font>";
		cnFlag = false;
	}
}
//cn end

//password start
function passwordOnFocus(obj) {
	obj.style.backgroundColor="";
	document.getElementById("passwordEX").innerHTML = "4-16 characters. Letters, digits and common punctuation characters allowed. Case sensitive.";	
}

function passwordValidate(obj) {
	document.getElementById("passwordEX").innerHTML = "";
	obj.style.backgroundColor = "";
	var password = obj.value;
	var cn = document.getElementById("cn").value;
	var reg = /^[~`!@#$%\^&*()_+=\-{}\]\[:;',.<>\/?"a-zA-Z0-9]{4,16}$/;
	//without space,'"
	if (!reg.test(password)) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("passwordEX").innerHTML = "<font color='red'>4-16 characters. Letters, digits and common punctuation characters allowed.</font>";
        return false;
	}
	if (password == cn) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("passwordEX").innerHTML = "<font color='red'>Account ID and Password cannot be the same. Please change your Password.</font>";
        return false;
	}
	obj.style.backgroundColor = "ccff99";
	return true;
}
//password end

//verifyPassword start
function verifyPasswordOnFocus(obj) {
	obj.style.backgroundColor="";
	document.getElementById("verifyPasswordEX").innerHTML = "Retype the Password.";	
}

function verifyPasswordValidate(obj) {
	document.getElementById("verifyPasswordEX").innerHTML = "";
	obj.style.backgroundColor = "";
	var password = document.getElementById("password").value;
	var verifyPassword = obj.value;
	if (verifyPassword.length == 0) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("verifyPasswordEX").innerHTML = "<font color='red'>Retype the Password.</font>";
        return false;
	}
	if (password != verifyPassword) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("verifyPasswordEX").innerHTML = "<font color='red'>Passwords do not match.</font>";
        return false;
	}
	obj.style.backgroundColor = "ccff99";
	return true;
}
//verifyPassword end

//superPassword start
function superPasswordOnFocus(obj) {
	obj.style.backgroundColor="";
	document.getElementById("superPasswordEX").innerHTML = "8-16 characters. Letters, digits and common punctuation characters allowed. Case sensitive.";	
}

function superPasswordValidate(obj) {
	document.getElementById("superPasswordEX").innerHTML = "";
	obj.style.backgroundColor = "";
	var superPassword = obj.value;
	var cn = document.getElementById("cn").value;
	var password = document.getElementById("password").value;
	var reg = /^[^\s,\u0027\u0022]{8,16}$/;
	//without space,'"
	if (!reg.test(superPassword)) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("superPasswordEX").innerHTML = "<font color='red'>8-16 characters. Letters, digits and common punctuation characters allowed.</font>";
        return false;
	}
	if (superPassword == cn) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("superPasswordEX").innerHTML = "<font color='red'>Account ID and Super Password cannot be the same. Please change your Super Password.</font>";
        return false;
	}
	if (superPassword == password) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("superPasswordEX").innerHTML = "<font color='red'>Password and Super Password cannot be the same. Please change your Super Password.</font>";
        return false;
	}
	obj.style.backgroundColor = "ccff99";
	return true;
}
//superPassword end

//verifySuperPassword start
function verifySuperPasswordOnFocus(obj) {
	obj.style.backgroundColor="";
	document.getElementById("verifySuperPasswordEX").innerHTML = "Retype the Super Password.";	
}

function verifySuperPasswordValidate(obj) {
	document.getElementById("verifySuperPasswordEX").innerHTML = "";
	obj.style.backgroundColor = "";
	var superPassword = document.getElementById("superPassword").value;
	var verifySuperPassword = obj.value;
	if (verifySuperPassword.length == 0) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("verifySuperPasswordEX").innerHTML = "<font color='red'>Retype the Super Password.</font>";
        return false;
	}
	if (superPassword != verifySuperPassword) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("verifySuperPasswordEX").innerHTML = "<font color='red'>Passwords do not match.</font>";
        return false;
	}
	obj.style.backgroundColor = "ccff99";
	return true;
}
//verifySuperPassword end

//firstName start
function firstNameOnFocus(obj) {
	obj.style.backgroundColor="";
	document.getElementById("firstNameEX").innerHTML = "Maximum 50 characters. Case sensitive.";	
}

function firstNameValidate(obj) {
	document.getElementById("firstNameEX").innerHTML = "";
	obj.style.backgroundColor = "";
	var firstname = strim(obj.value);
	if (firstname.length == 0) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("firstNameEX").innerHTML = "<font color='red'>Please enter First Name.</font>";
        return false;
	}
	if (firstname.length > 50) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("firstNameEX").innerHTML = "<font color='red'>Maximum 50 characters.</font>";
        return false;
	}
	obj.style.backgroundColor = "ccff99";
	return true;
}
//firstName end

//lastName start
function lastNameOnFocus(obj) {
	obj.style.backgroundColor="";
	document.getElementById("lastNameEX").innerHTML = "Maximum 50 characters. Case sensitive.";	
}

function lastNameValidate(obj) {
	document.getElementById("lastNameEX").innerHTML = "";
	obj.style.backgroundColor = "";
	var lastname = strim(obj.value);
	if (lastname.length == 0) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("lastNameEX").innerHTML = "<font color='red'>Please enter Last Name.</font>";
        return false;
	}
	if (lastname.length > 50) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("lastNameEX").innerHTML = "<font color='red'>Maximum 50 characters.</font>";
        return false;
	}
	obj.style.backgroundColor = "ccff99";
	return true;
}
//lastName end

//gender start
function genderOnFocus() {
	document.getElementById("genderEX").innerHTML = "";
}

function genderValidate(obj) {
	var gender = obj.value;
	alert(gender);
	if (gender !="F" || gender != "M") {
		document.getElementById("genderEX").innerHTML = "<font color='red'>Please choose your gender.</font>";
		return false;
	}
	return true;
}
//gender end

//birthDate start
function birthDateOnFocus(obj) {
	document.getElementById("birthYear").style.backgroundColor="";
	document.getElementById("birthMonth").style.backgroundColor="";
	document.getElementById("birthday").style.backgroundColor="";
	document.getElementById("birthDateEX").innerHTML = "You must be at least 18 years old to create an Account ID.  If you are between 13 and 18, you must receive guardian or parental permission.";
}

function birthDateValidate(year,month,day) {
	document.getElementById("birthDateEX").innerHTML = "";
	var objYear=document.getElementById("birthYear");
	var objMonth=document.getElementById("birthMonth");
	var objDay=document.getElementById("birthday");
	objYear.style.backgroundColor="";
	objMonth.style.backgroundColor="";
	objDay.style.backgroundColor="";
	
	var date = new Date();
	date.setYear(year-13);
	date.setMonth(month);
	date.setDate(day);
	var birthday = new Date();
	birthday.setYear(objYear.value);
	birthday.setMonth(objMonth.value);
	birthday.setDate(objDay.value);

	var activeobj = document.activeElement.name;
    if (activeobj == undefined) {
        if (date < birthday) {
        	objYear.style.backgroundColor="pink";
        	objMonth.style.backgroundColor="pink";
        	objDay.style.backgroundColor="pink";
			document.getElementById("birthDateEX").innerHTML = "<font color='red'>You must be at least 18 years old to create an Account ID.  If you are between 13 and 18, you must receive guardian or parental permission.</font>";
	        return false;
        } else {
        	date = new Date();
			date.setYear(year-18);
			date.setMonth(month);
			date.setDate(day);
			if (date < birthday) {
				objYear.style.backgroundColor="ccff99";
    			objMonth.style.backgroundColor="ccff99";
    			objDay.style.backgroundColor="ccff99";
    			document.getElementById("birthDateEX").innerHTML = "<font color='red'>You must be at least 18 years old to create an Account ID.  If you are between 13 and 18, you must receive guardian or parental permission.</font>";
    			return true;
			}
        }
    } else if (activeobj != undefined && activeobj != 'birthMonth' && activeobj != 'birthday' && activeobj != 'birthYear') {
        if (date < birthday) {
        	objYear.style.backgroundColor="pink";
        	objMonth.style.backgroundColor="pink";
        	objDay.style.backgroundColor="pink";
			document.getElementById("birthDateEX").innerHTML = "<font color='red'>You must be at least 18 years old to create an Account ID.  If you are between 13 and 18, you must receive guardian or parental permission.</font>";
	        return false;
        } else {
        	date = new Date();
			date.setYear(year-18);
			date.setMonth(month);
			date.setDate(day);
			if (date < birthday) {
				objYear.style.backgroundColor="ccff99";
    			objMonth.style.backgroundColor="ccff99";
    			objDay.style.backgroundColor="ccff99";
    			document.getElementById("birthDateEX").innerHTML = "<font color='red'>You must be at least 18 years old to create an Account ID.  If you are between 13 and 18, you must receive guardian or parental permission.</font>";
    			return true;
			}
        }
    }
    objYear.style.backgroundColor="ccff99";
    objMonth.style.backgroundColor="ccff99";
    objDay.style.backgroundColor="ccff99";
    return true;
}
//birthDate end

//email start
function emailOnFocus(obj) {
	obj.style.backgroundColor="";
	document.getElementById("emailEX").innerHTML = "Please use a valid e-mail address for your Security E-mail to complete your registration and help you manage your account. There is a maximum of 10 game accounts for each e-mail. Each Security E-mail has a limit of 50 characters.";
}

function emailValidate(obj) {
	document.getElementById("emailEX").innerHTML = "";
	obj.style.backgroundColor = "";
	var email = obj.value;
	if (email.length == 0) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("emailEX").innerHTML = "<font color='red'>Please enter your Security E-mail.</font>";
        return false;
	}
	if (email.length > 50) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("emailEX").innerHTML = "<font color='red'>There is a 50 characters limit.</font>";
        return false;
	}
	var reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if (!reg.test(email)) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("emailEX").innerHTML = "<font color='red'>Invalid E-mail address format.</font>";
        return false;
	}
	//DWREngine.setAsync(false);
	emailFlag = true;
	CheckObject.validateEmail(email,emailDWRHandler);
	//DWREngine.setAsync(true);
	
	if (emailFlag == false) {
		return false;
	}
	
	obj.style.backgroundColor="ccff99";
	return true;
}

function emailDWRHandler(flag) {
	if (flag == 17) {
		document.getElementById("email").style.backgroundColor = 'pink';
		document.getElementById("emailEX").innerHTML = "<font color='red'>Security Email contains invalid character(s). Please try again.</font>";
		emailFlag = false;
	}
	if (flag == 18) {
		document.getElementById("email").style.backgroundColor = 'pink';
		document.getElementById("emailEX").innerHTML = "<font color='red'>The E-mail has reached its maximum of 10 accounts.</font>";
		emailFlag = false;
	}
	if (flag == 999) {
		document.getElementById("email").style.backgroundColor = 'pink';
		document.getElementById("emailEX").innerHTML = "<font color='red'>Unexpected error.</font>";
		emailFlag = false;
	}
}
//email end

//verifyEmail start
function verifyEmailOnFocus(obj) {
	obj.style.backgroundColor="";
	document.getElementById("verifyEmailEX").innerHTML = "Please enter your Security E-mail again to make sure there is no mistake.";
}

function verifyEmailValidate(obj) {
	document.getElementById("verifyEmailEX").innerHTML = "";
	obj.style.backgroundColor = "";
	var verifyemail = obj.value.toLowerCase();
	var email = document.getElementById("email").value.toLowerCase();
	if (verifyemail.length == 0) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("verifyEmailEX").innerHTML = "<font color='red'>Please enter your Security E-mail again.</font>";
        return false;
	}
	if (verifyemail != email) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("verifyEmailEX").innerHTML = "<font color='red'>Security Emails do not match.</font>";
        return false;
	}
	obj.style.backgroundColor="ccff99";
	return true;
}
//verifyEmail end

//question start
function questionOnFocus(obj) {
	obj.style.backgroundColor="";
	var question = obj.value;
	if (question == 99) {
	    document.getElementById("questionEX").innerHTML = "Security Question & Answer are used to identify your account.";	
	}
}

function questionValidate(obj) {
	obj.style.backgroundColor = "";
	document.getElementById("questionEX").innerHTML = "";
	var question = obj.value;
	if (question == 99) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("questionEX").innerHTML = "<font color='red'>Please choose Security Question and enter its Answer.</font>";
        return false;
	}
	obj.style.backgroundColor = "ccff99";
	return true;
}
//question end

//answer start
function answerOnFocus(obj) {
	obj.style.backgroundColor="";
	document.getElementById("answerEX").innerHTML = "4-50 characters. Case sensitive.";	
}

function answerValidate(obj) {
	document.getElementById("answerEX").innerHTML = "";
	obj.style.backgroundColor = "";
	var answer = strim(obj.value);
	if (answer.length == 0) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("answerEX").innerHTML = "<font color='red'>Please choose Security Question and enter its Answer.</font>";
        return false;
	}
	if (answer.length < 4 || answer.length > 50) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("answerEX").innerHTML = "<font color='red'>4-50 characters.</font>";
        return false;
	}
	obj.style.backgroundColor = "ccff99";
	return true;
}
//answer end

//country start
function countryOnFocus(obj) {
	obj.style.backgroundColor="";
}

function countryValidate(obj) {
	obj.style.backgroundColor = "ccff99";
}
//country end

//address start
function addressOnFocus(obj) {
	obj.style.backgroundColor="";
	document.getElementById("addressEX").innerHTML = "Maximum 150 characters. Use letters, space, comma, digits or dot.";
}

function addressValidate(obj) {
	document.getElementById("addressEX").innerHTML = "";
	obj.style.backgroundColor = "";
	var address = strim(obj.value);
	var reg = /^[,.a-zA-Z\s0-9]{0,300}$/;
	if (!reg.test(address)) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("addressEX").innerHTML = "<font color='red'>Use letters, space, comma, digits or dot. Maximum 300 characters.</font>";
        return false;
	}
	obj.style.backgroundColor = "ccff99";	
	return true;
}
//address end

//zip start
function zipOnFocus(obj) {
	obj.style.backgroundColor="";
	document.getElementById("zipEX").innerHTML = "10 characters. Use letters or digits.";
}

function zipValidate(obj) {
	document.getElementById("zipEX").innerHTML = "";
	obj.style.backgroundColor = "";
	var zip = obj.value;
	var reg = /^[0-9a-zA-Z]{0,10}$/;
	if (!reg.test(zip)) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("zipEX").innerHTML = "<font color='red'>Please use letters or digits.</font>";
        return false;
	}
	obj.style.backgroundColor = "ccff99";
	return true;
}
//zip end

//telephone start
function telephoneOnFocus(obj) {
	obj.style.backgroundColor="";
	document.getElementById("telephoneEX").innerHTML = "Maximum 20 characters. Use digits, -. Example: 001-123-456-7890";	
}
function telephoneValidate(obj) {
	document.getElementById("telephoneEX").innerHTML = "";
	obj.style.backgroundColor = "";
	var telephone = obj.value;
	var reg = /^[-0-9]{0,20}$/;
	if (!reg.test(telephone)) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("telephoneEX").innerHTML = "<font color='red'>Please use digits, -.</font>";
        return false;
	}
	obj.style.backgroundColor = "ccff99";	
	return true;
}
//telephone end

//activeCode start
function activeCodeValidate(obj) {
	document.getElementById("activeCodeEX").innerHTML = "";
	obj.style.backgroundColor = "";
	var va = obj.value;
	var active = /^[a-zA-Z0-9]{12}$/;
	if (va.length==0 ) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("activeCodeEX").innerHTML = "<font color='red'>Please enter the Pre-Closed Beta Key.</font>";
        return;
	} else if (!active.test(va)) {
		obj.style.backgroundColor = 'pink';
		document.getElementById("activeCodeEX").innerHTML = "<font color='red'>Pre-Closed Beta Key error</font>";
        return;
	}
	obj.style.backgroundColor = "ccff99";
}

function activeCodeOnFocus(obj) {
	obj.style.backgroundColor="";
	document.getElementById("activeCodeEX").innerHTML = "Please enter the Pre-Closed Beta Key.";
}
//activeCode end