// This function checks if the username field
// is at least 6 characters long.
// If so, it attaches class="welldone" to the 
// containing fieldset.

function checkPercentage(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/(^\d).(\d{2})/.test(txt)) {
		fieldset.className = "welldone";
	} else {
		fieldset.className = "";
	}
}


function checkLettersAndNumbers(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt.length != 0) {
		fieldset.className = "welldone";
	} else {
		fieldset.className = "";
	}
}


function checkSelectValueValid(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (txt != '-1.1') {
		fieldset.className = "welldone";
	}
	else {	
		fieldset.className = "";
	}
}

function checkMonetaryAmount(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if ((txt.length > 4) && (/(^\d+$)/.test(txt))) {
		fieldset.className = "welldone";
	}
	else {	
		fieldset.className = "";
	}
}

function checkZip(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/(^\d{5}$)/.test(txt)) {
		fieldset.className = "welldone";
	}
	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;
	var txt = whatYouTyped.value;
	if (txt.length > 3 && txt.length < 8) {
		fieldset.className = "kindagood";
	} else if (txt.length > 7) {
		fieldset.className = "welldone";
	} 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;
	var txt = whatYouTyped.value;
	if (/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/.test(txt)) {
		
		fieldset.className = "welldone";
	} else {
		fieldset.className = "";
	}
}

function checkUrl(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/(((ht|f)tp(s?):\/\/)|(www\.[^ \[\]\(\)\n\r\t]+)|(([012]?[0-9]{1,2}\.){3}[012]?[0-9]{1,2})\/)([^ \[\]\(\),;&quot;'&lt;&gt;\n\r\t]+)([^\. \[\]\(\),;&quot;'&lt;&gt;\n\r\t])|(([012]?[0-9]{1,2}\.){3}[012]?[0-9]{1,2})/.test(txt)) {
		fieldset.className = "welldone";
	} else {
		fieldset.className = "";
	}
}

function checkPhone(whatYouTyped) {
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$/.test(txt)) {
		fieldset.className = "welldone";
	} 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++){
    inputs[i].onfocus = function () {
      if (this.parentNode.getElementsByTagName("span")[0] != undefined){
      	this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
      }
    }
    inputs[i].onblur = function () {
      if (this.parentNode.getElementsByTagName("span")[0] != undefined){
      	this.parentNode.getElementsByTagName("span")[0].style.display = "none";
      }
    }
  }
  
  var selects = document.getElementsByTagName("select");
  
  for (var i=0; i<selects.length; i++){
    selects[i].onfocus = function () {
      if (this.parentNode.getElementsByTagName("span")[0] != undefined){
      	this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
      }
    }
    selects[i].onblur = function () {
      if (this.parentNode.getElementsByTagName("span")[0] != undefined){
      	this.parentNode.getElementsByTagName("span")[0].style.display = "none";
      }
    }
  }
  
  var textarea = document.getElementsByTagName("textarea");
   for (var i=0; i<textarea.length; i++){
    textarea[i].onfocus = function () {
      if (this.parentNode.getElementsByTagName("span")[0] != undefined){
      	this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
      }
    }
    textarea[i].onblur = function () {
      if (this.parentNode.getElementsByTagName("span")[0] != undefined){
      	this.parentNode.getElementsByTagName("span")[0].style.display = "none";
      }
    }
  }
  
}
//addLoadEvent(prepareInputsForHints);