﻿var isIE = (window.navigator.userAgent.indexOf("MSIE") > 0);

if (! isIE) {
    HTMLElement.prototype.__defineGetter__("innerText",
    function () { return(this.textContent); });
    HTMLElement.prototype.__defineSetter__("innerText",
    function (txt) { this.textContent = txt; });
}

// 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(typedText,minLength) {
	var fieldset=typedText.parentNode;
	var txt=typedText.value;
	if (txt.length>=minLength) {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 (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
		fieldset.className = "welldone";
	} else {
		fieldset.className = "";
	}
}

// This function is adapted from a script Originating from:
// Brian Swalwell / The JavaScript Source!! http://javascript.internet.com
function validateZIP(inputZip) {
	var fieldset=inputZip.parentNode;
	var txt=inputZip.value;
    var valid="0123456789-";
    var hyphencount=0;
    if (txt.length!=5&&txt.length!=10) {
        for (var i=0; i<txt.length; i++) {
            temp=""+txt.substring(i, i+1);
            if (temp=="-") hyphencount++;
            if (valid.indexOf(temp) == "-1") {
                setHintText(inputZip.parentNode,"There are invalid characters in your zip code. Please enter a 5 digit zip code.")
    		    fieldset.className="";
                return;
            }
            if ((hyphencount > 1) || ((txt.length==10) && ""+txt.charAt(5)!="-") || ((hyphencount == 1) && (txt.length<=5))) {
                setHintText(inputZip.parentNode,"The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'. Please enter a valid 5 digit zip code.")
    		    fieldset.className = "";
                return;
            }
        }
        setHintText(inputZip.parentNode,"Please enter a 5 digit zip code.")
        fieldset.className = "";
        return;
    }
    for (var i=0; i < txt.length; i++) {
        temp = "" + txt.substring(i, i+1);
        if (temp == "-") hyphencount++;
        if (valid.indexOf(temp) == "-1") {
            setHintText(inputZip.parentNode,"There are invalid characters in your zip code.")
            //alert("There are invalid characters in your zip code.");
    		fieldset.className = "";
            return;
        }
        if ((hyphencount > 1) || ((txt.length==10) && ""+txt.charAt(5)!="-")) {
            setHintText(inputZip.parentNode,"The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.")
            //alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
    		fieldset.className = "";
            return;
        }
    }
    fieldset.className = "welldone";
}

// To change hint text
function setHintText(container,hintText) {
    var hints=container.getElementsByTagName("strong");
    for (var k=0; k<hints.length; k++){
        if (k==0) {hints[k].innerText=hintText;}
    }
}

// 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 hideHints() {
    var fieldsets=document.getElementsByTagName("fieldset");
    for (var j=0; j<fieldsets.length; j++){
        var hints=fieldsets[j].getElementsByTagName("strong");
        for (var k=0; k<hints.length; k++){hints[k].style.display="none";}
    }
}

function prepareInputsForHints() {
  var inputs = document.getElementsByTagName("input");
  for (var i=0; i<inputs.length; i++){
    inputs[i].onfocus = function () {
      hideHints();
      if (this.parentNode.getElementsByTagName("strong")[0] != null) {
        this.parentNode.getElementsByTagName("strong")[0].style.display = "inline";
      }
    }
  }
  var textareas = document.getElementsByTagName("textarea");
  for (var i=0; i<textareas.length; i++){
    textareas[i].onfocus = function () {
      hideHints();
      if (this.parentNode.getElementsByTagName("strong")[0] != null) {
        this.parentNode.getElementsByTagName("strong")[0].style.display = "inline";
      }
    }
  }
  var selects = document.getElementsByTagName("select");
  for (var i=0; i<selects.length; i++){
    selects[i].onfocus = function () {hideHints();}
  }
}

// CSS and other visual cues and effects for form elements
function setInputClasses() {
    var inputs = document.getElementsByTagName("input");
    for (i=0; i < inputs.length; i++) {
        if (inputs[i].className == '') { 
            inputs[i].onfocus=function() {this.className='focus'};
            inputs[i].onblur=function() {this.className=''};
        }
    }
    var textareas = document.getElementsByTagName("textarea");
    for (i=0; i < textareas.length; i++) {
        if (textareas[i].className == '') { 
            textareas[i].onfocus=function() {this.className='focus'};
            textareas[i].onblur=function() {this.className=''};
        }
    }
}

// Original:  Brian Swalwell 
// This script and many more are available free online at 
// The JavaScript Source!! http://javascript.internet.com 
function validateZipCode(field) {
    var valid = "0123456789-";
    var hyphencount = 0;
    if (field.length!=5 && field.length!=10) {
        alert("Please enter a 5 digit zip code.");
        return false;
    }
    for (var i=0; i < field.length; i++) {
        temp = "" + field.substring(i, i+1);
        if (temp == "-") hyphencount++;
        if (valid.indexOf(temp) == "-1") {
            alert("Invalid characters in your zip code.  Please try again.");
            return false;
        }
        if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
            alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
            return false;
        }
    }
    return true;
}
