window.onload = function() {
	$('un').focus();
	$('un').select();
	$('un').className = "editing " + $('un').className;
}
      
var PWWin;
function CreateSecurePWWin() {
	PWWin = window.open('/site-shared/docs/password.html?un='+$('un').value,'','scrollbars=no,menubar=no,height=300,width=400,resizable=yes,toolbar=no,location=no,status=no');
}

var LastPWValue = "";
function PWChange() {
	var NewPWValue = "<b style='color: green'>This password is valid.</a>";
	var PW = $("pw").value;
	var PW2 = $("pw2").value;
	var UN = $("un").value;
	
	if (PW.length == 0) {
		NewPWValue = "<em>Choose a strong password.  (<a href='javascript:CreateSecurePWWin()'>Learn how ...</a>)</em>";
	} else if (PW.length < 5) {
		NewPWValue = "<b style='color: red'>Your password must be at least 5 characters long.</b>";
	} else if (PW == UN) {
		NewPWValue = "<b style='color: red'>Your password cannot be identical to your username.</b>";
	} else if (PW.toLowerCase().indexOf("abc") != -1 || PW.indexOf("123") != -1 || PW.toLowerCase().indexOf("pass") != -1 || PW.toLowerCase().indexOf("guess") != -1 || PW.toLowerCase().indexOf("ginogina") != -1 || PW.toLowerCase().indexOf("chillinto") != -1) {
		NewPWValue = "<b style='color: red'>Choose a stronger password.</b>"
	} else if (PW2 == "") {
		NewPWValue = "<em>Please confirm your password by entering it again in the second field.</em>";
	} else if (PW != PW2) {
		NewPWValue = "<b style='color: red'>Your passwords do not match.</b>";
	}
	
	if (LastPWValue != NewPWValue) {
		$("pwinfo").innerHTML = NewPWValue;
		LastPWValue = NewPWValue;
	}
}

var LastEMValue = "";
function EmailChange() {
	var NewEMValue = "<b style='color: green'>Your e-mail address is valid.</a>";
	var EM = $("em").value;
	var EM2 = $("em2").value;
	
	var RegEx = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]{1,})*\.([a-zA-Z]{2,}){1}$/;
	if (EM.length < 5) {
		NewEMValue = "<em>A valid e-mail address is <b>required</b> to create a new account.</em>";
	} else if (EM.indexOf("@") == -1) {
		NewEMValue = "<b style='color: red'>All e-mail addresses contain the @ symbol.</b>";
	} else if (!RegEx.test(EM)) {
		NewEMValue = "<b style='color: red'>Invalid e-mail address.</b>";
	} else if (EM2 == "") {
		NewEMValue = "<em>Please confirm your e-mail by entering it again in the second field.</em>";
	} else if (EM != EM2) {
		NewEMValue = "<b style='color: red'>Your e-mail addresses do not match.</b>";
	}
	
	if (LastEMValue != NewEMValue) {
		$("eminfo").innerHTML = NewEMValue;
		LastEMValue = NewEMValue;
	}
}
      
var LastValue = "";
function UsernameChange() {
	var username = $("un").value;
	
	var NewValue = "<b style='color: #555555'>The username "+ username +" is valid.</a>";
	var AvailVal = NewValue;
	
	if (username.length == 0) {
		NewValue = "<em>Letters, numbers, underscores, and dashes only. Keep it clean.</em>";
	} else if (username.length < 3) {
		NewValue = "<b style='color: red'>Your username must be at least 3 letters long.</b>";
	} else for (var i = 0; i < username.length; i++) {
		if (i > 15) {
			NewValue = "<b style='color: red'>Your username is too long.  Keep it under 16 characters.</a>"
			break;
		}
		var chr = username.charAt(i);
	
		var RegEx = /^[a-z0-9_ -]$/i;
		if (!RegEx.test(chr)) {
			NewValue = "<b style='color: red'>Invalid character '" + chr + "'.  Letters, numbers, underscores, and dashes only.</b>";
			break;
		}
	}
	
	if (LastValue != NewValue) {
		Num++;
		if (AvailVal == NewValue) setTimeout("CheckUNAvail("+Num+");", 1000);
		$("uninfo").innerHTML = NewValue;
		LastValue = NewValue;
	}
}

var CheckForUN = "";
var Num = 0;
function CheckUNAvail(CheckFor) {
	if (Num != CheckFor) return;
	
	CheckForUN = $("un").value;
	
	function handleResult(ex) {
		var NewValue;
		if (!ex) {
			NewValue = "<b style='color: green'>The username "+ CheckForUN +" is available.</b>";
		} else {
			NewValue = "<b style='color: red'>The username "+ CheckForUN +" is not available.</b>";	
		}
		if (NewValue != LastValue) {
			$("uninfo").innerHTML = NewValue;
		}		
	}
	
	HTTP("join-checkusername.php?username=" + escape($("un").value), {
		success : function(s){handleResult(s.res)},
		error : function(){}
	});
}

var UTimeout;
var ETimeout;
var PTimeout;

