// This function checks if the username field
// is at least 6 characters long.
// If so, it attaches class="welldone" to the 
// containing fieldset.
function checkUsernameForLength(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length > 4) {
		fieldset.className = "correct";
		var url = "/ajax/ajax-checkusername.php?username=";
		var fullurl = url + txt;
		new Ajax(fullurl, {
			method: 'get',
			update: $('usernamecheck')
		}).request();
	}
	else {
		fieldset.className = "";
	}
}

function checkName(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length > 1) {
		fieldset.className = "correct";
	}
	else {
		fieldset.className = "";
	}
}


// If the password is at least 4 characters long, the containing 
// fieldset is assigned class="kindagood".
// If it's at least 8 characters long, the containing
// fieldset is assigned class="welldone", to give the user
// the indication that they've selected a harder-to-crack
// password.

function checkPassword(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length > 4 && txt.length < 8) {
		fieldset.className = "neutral";
	} else if (txt.length > 7) {
		fieldset.className = "correct";
	} else {
		fieldset.className = "";
	}
}

// Check for the postcoode to be a valid uk postcode
function checkPostcode(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode.parentNode;
	var txt = whatYouTyped.value;
	txt = txt.toUpperCase();
	if (/\b[A-PR-UWYZ][A-HK-Y0-9][A-HJKSTUW0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}\b/g.test(txt)) {
		fieldset.className = "correct";
	} else {
		fieldset.className = "";
	}
}

// This function checks the email address to be sure
// it follows a certain pattern:
// blah@blah.blah
// If so, it assigns class="welldone" to the containing
// fieldset.

function checkEmail(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode.parentNode;
	var txt = whatYouTyped.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
		fieldset.className = "correct";
	} else {
		fieldset.className = "";
	}
}

// Check to make sure the day is from 1 to 31
function checkDay(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode.parentNode;
	var txt = whatYouTyped.value;
	if (txt > 0 && txt < 32) {
		fieldset.className = "correct";
	} else {
		fieldset.className = "";
	}
}

// Check to make sure the year is from 1 to 31
function checkYear(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode.parentNode;
	var txt = whatYouTyped.value;
	if (txt > 1901 && txt < 1992) {
		fieldset.className = "correct";
	} else {
		fieldset.className = "";
	}
}


// this part is for the form field hints to display
// only on the condition that the text input has focus.
// otherwise, it stays hidden.

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function prepareInputsForHints() {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		// test to see if the hint span exists first
		if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
			// the span exists!  on focus, show the hint
			inputs[i].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			// when the cursor moves away from the field, hide the hint
			inputs[i].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
	// repeat the same tests as above for selects
	var selects = document.getElementsByTagName("select");
	for (var k=0; k<selects.length; k++){
		if (selects[k].parentNode.getElementsByTagName("span")[0]) {
			selects[k].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			selects[k].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
}
addLoadEvent(prepareInputsForHints);