function note(that,power) {
	var notes = {
		'username' : 'Choose a username between 3 and 15 characters that only contains numbers and letters.',
		'email' : 'Choose an address that you have access to so we can send you a verification email.',
		'password' : 'Choose a secure password that is at least 6 characters long.',
		'password2' : 'Re-enter your password for verification purposes.',
		'country' : 'Please select the country you live in. This is for statistical purposes.',
		'state' : 'If you live within the United States, please select your state. This is for statistical purposes.',
		'city' : 'Please select the city you currently live in. This is for statistical purposes.',
		'gender' : 'Please select your gender. This is for statistical purposes.',
		'name' : 'Please enter your real name.'
	}
	if(power == 1) {
		var icon = document.getElementsByName(that.name+"_icon");
		if(icon[0].src.indexOf("empty") != -1) {
			updateNote(that,notes[that.id])
		}
	} else {
		updateNote(that,"");
		checkFormElement(that,1);
	}
}
function updateNote(that,text,status) {
		document.getElementById(that.id + "_note").innerHTML = text;
		var icon = document.getElementsByName(that.name+"_icon");
		if(status == 0) {
			icon[0].src = "/images/icons/empty.gif";
		} else if(status == 1) { 
			icon[0].src = "/images/icons/checkmark.gif";
		} else if(status == -1) {
			icon[0].src = "/images/icons/error.gif";
		}
}
function checkFormElement(that,ignoreempty) {
	if(that.name == "username") {
		if(that.value.length >= 3 && that.value.length <= 15) {
			var usernameformat = new RegExp("[^a-zA-Z0-9]");
			if(usernameformat.test(that.value)) {
				updateNote(that,"Your username may only include letters and numbers. No symbols.",-1);
				return false; exit;
			} else {
				//return checkAvailability("username",that.value);
				updateNote(that,"",1);
				return true; exit;
			}
		} else if(that.value.length != 0 || ignoreempty == 0) {
			updateNote(that,"Your username must be between 3 and 15 characters long.",-1);
			return false; exit;
		} else {
			updateNote(that,"",0);
		}
	} else if(that.name == "email") {
		var emailformat = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(emailformat.test(that.value)) {
			//return checkAvailability("email",that.value);
			updateNote(that,"",1);
			return true; exit;
		} else if(that.value.length != "" || ignoreempty == 0 ){
			updateNote(that,"Please enter a valid email.",-1);
			return false; exit;
		} else {
			updateNote(that,"",0);
		}
	} else if(that.name == "password") {
		if(that.value.length >= 6 && that.value.length !=0) {
			updateNote(that,"",1);
			return true;
		} else if(that.value.length != 0 || ignoreempty == 0) {
			updateNote(that,"Password must be at least 6 characters long!",-1);
			return false; exit;
		} else {
			updateNote(that,"",0);
		}
	} else if(that.name == "password2") {
		if(that.value == document.registration.password.value && that.value.length != 0) {
			updateNote(that,"",1);
			return true;
		} else if(that.value.length != 0) {
			updateNote(that,"Passwords do not match",-1);
			return false; exit;
		} else if(ignoreempty == 0) {
			updateNote(that,"",-1);
		} else {
			updateNote(that,"",0);
		}
	} else if(that.name == "country") {
		updateNote(that,"",1);
		if(that.value == "United States") {
			document.registration.state.disabled = "";
		} else {
			document.registration.state.disabled = "disabled";
			updateNote(document.registration.state,"",1);
			return true;
		}
	} else if(that.name == "state") {
		if(document.registration.country.value == "United States") {
			if(that.options[that.selectedIndex].value == "-") {
				updateNote(that,"Please select your state. If you live outside the U.S., choose another country.",-1);
				return false; exit;
			} else {
				updateNote(that,"",1);
				return true;
			}

		} else if(that.options[that.selectedIndex].value != "-") {
			updateNote(that,"For users in the U.S. only",-1);
			return false;
		} else {
			updateNote(that,"",1); return true;
		}
	} else if(that.name == "city") {
		if(that.value != "") {
			updateNote(that,"",1);
			return true;
		} else {
			updateNote(that,"Please choose your city!",-1);
			return false; exit;
		}
	} else if(that.name == "gender") {
		if(that.options[that.selectedIndex].value == "-") {
			updateNote(that,"Please select your gender!",-1);
			return false; exit;
		} else {
			updateNote(that,"",1);
			return true;
		}
	} else if(that.name == "name") {
		var nameformat = new RegExp("[^A-Za-z]");
		if(nameformat.test(that.value)) {
			updateNote(that,"Names can only contain letters.",-1);
			return false; exit;
		} else {
			updateNote(that,"",1);
			return true;
		}
		return true;
	}
}
//Check when page loads (in case data is left in)
function checkFormElements() {
	var formelements = new Array("username","email","password","password2");
	for(var i=0; i< formelements.length;i++) {
		checkFormElement(document.registration[formelements[i]],1);
	}
	return false;
}
//Check when form submits
function checkFormElementsSubmit() {
	var formelements = new Array("username","email","password","password2","name","country","state","city","gender");
	var results = new Array();
	for(var i=0; i< formelements.length;i++) {
		if(checkFormElement(document.registration[formelements[i]],0) != true) {
			results[i] = true;
		} else { results[i] = false; }
		
	}
	for(var i=0; i<results.length;i++) {
		if(results[i] == true) {
			return false;
		}
	}
	
	return true;
}

//Thanks to Steve Degraeve
//I'll implement this later
function checkAvailability(name,value) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', '/ajax/register', true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {

           	if(self.xmlHttpReq.responseText.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ') == value) {
			if(name == "username") {
				updateNote(document.registration[name],"The username <b>"+value+"</b> is already being used! Please try another.",-1);
			} else {
				updateNote(document.registration[name],"That e-mail is already associated with an account. You may only have one account, please <a href=\"login\">login</a>.",-1);
			}
		} else {
			if(name == "username") {
				updateNote(document.registration[name],"Hey now! The username <b>"+value+"</b> is available!",+1);
			} else {
				updateNote(document.registration[name],"",1);
			}
		}
        }
    }
    self.xmlHttpReq.send(name+"="+value);
}
