
function isPassword(el) {
    var pwds = document.getElementById("pws");
    if (!pwds) return true;
    var arr = pwds.value.split(",");
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == el.value) return true;
    }
    alert("Invalid Password!");
    setTimeout("focusElement('" + el.name + "')", 0);
    return false;
}

// validates that the field value string has one or more characters in it
function isNotEmpty(el) {
    var str = el.value;
    var re = /.+/;
    if (!str.match(re)) {
        alert("Please fill in the required " + el.name + " field.");
        setTimeout("focusElement('" + el.name + "')", 0);
        return false;
    } else {
        return true;
    }
}
// validate that the user made a selection other than default
function isChosen(select) {
    if (select.selectedIndex == 0) {
        alert("Please make a choice from the required " + select.name + " dropdown.");
        select.focus();
        return false;
    } else {
        return true;
    }
}
// validate that the user has checked one of the radio buttons
function isValidRadio(radio) {
    var valid = false;
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            return true;
        }
    }
    alert("Make a choice from the required " + radio[0].name + " radio buttons.");
    radio[0].focus();
    return false;
}
//validates that the entry is a positive or negative number
function isNumber(el) {
    var str = el.value;
    var re = /^[-]?\d*\.?\d*$/;
    str = str.toString();
    if (!str.match(re)) {
        alert("Enter only numbers into the field.");
        setTimeout("focusElement('" + el.name + "')", 0);
        return false;
    }
    return true;
}
// validates that the entry is formatted as an e-mail address
function isEmailAddr(el) {
    var str = el.value;
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) {
        alert("Not a valid email address! \nPlease verify the e-mail address format.");
        setTimeout("focusElement('" + el.name + "')", 0);
        return false;
    } else {
        return true;
    }
}
// validates that the entry is 17 characters long
function isLen17(el) {
    var str = el.value;
    var re = /\b.{17}\b/;
    if (!str.match(re)) {
        alert("Entry does not contain the required 17 characters.");
        setTimeout("focusElement('" + el.name + "')", 0);
        return false;
    } else {
        return true;
    }
}
// validates that the entry is 4 characters long
function isLen4(el) {
    var str = el.value;
    var re = /\b.{4}\b/;
    if (!str.match(re)) {
        alert("Entry does not contain the required 4 characters.");
        setTimeout("focusElement('" + el.name + "')", 0);
        return false;
    } else {
        return true;
    }
}
function validateDate(fld) {
  if (!checkDate(fld)) {
    // focus if validation fails
    fld.focus();
    fld.select();
  }
}function checkDate(fld) {
  var mo, day, yr;
  var entry = fld.value;
  var reLong = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
  var reShort = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2}\b/;
  var valid = (reLong.test(entry)) || (reShort.test(entry));
  if (valid) {
    var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
    var delim1 = entry.indexOf(delimChar);
    var delim2 = entry.lastIndexOf(delimChar);
    mo = parseInt(entry.substring(0, delim1), 10);
    day = parseInt(entry.substring(delim1+1, delim2), 10);
    yr = parseInt(entry.substring(delim2+1), 10);
    // handle two-digit year
    if (yr < 100) {
      var today = new Date();
      // get current century floor (e.g., 2000)
      var currCent = parseInt(today.getFullYear() / 100) * 100;
      // two digits up to this year + 15 expands to current century
      var threshold = (today.getFullYear() + 15) - currCent;
      if (yr > threshold) {
          yr += currCent - 100;
      } else {
          yr += currCent;
      }
    }
    
    var testDate = new Date(yr, mo-1, day);
    if (testDate.getDate() == day) {
      if (testDate.getMonth() + 1 == mo) {
        if (testDate.getFullYear() == yr) {
            // fill field with database-friendly format
            fld.value = mo + "/" + day + "/" + yr;
            return true;
        } else {
            alert("There is a problem with the year entry.");
          }
      } else {
          alert("There is a problem with the month entry.");
        }
    } else {
        alert("There is a problem with the date entry.");
    }
	} else {
			alert("Incorrect date format. Enter as mm/dd/yyyy.");
	}
  return false;
}
function phoneFilter(el) {

	var input = el.value;
	var format = "(###) ###-####";

	if(input.length > 0) { //do not perform if empty input

		var numbers = ""; //store all the numbers here
		var chr = "";

		//process to remove non-numbers and spaces
		for(var i = 0; i < input.length; i++) {
			chr = input.charAt(i);
			if (!(isNaN(chr) || chr == " ")) numbers += chr;
		}

		var output = ""; //assign numbers here

		//assign numbers to chosen format
		var n = 0, i = 0;
		while(i < format.length && n < numbers.length) {
			chr = format.charAt(i);
			if(chr == "#") {
				output += numbers.charAt(n++);
			} else {
				output += chr;
			}
			i++;
		}

		//give alert if length is less than 8.
		if(numbers.length < 8) {
			alert("The number must be of length 8");
			el.select();
		}

		el.value = output; //output to form element
	}
}

function focusElement(elName) {
    var el = document.forms[0].elements[elName];
    el.focus();
    el.select();
}

function isPhoneNumber(el) {
    
    var format = "### ###-####";

    var input = el.value;
    if (input.length > 0) {

        var chr = "";
        var numbers = "";

        //remove non-numbers and spaces
        for (var i = 0; i < input.length; i++) {
            chr = input.charAt(i);
            if (!(isNaN(chr) || chr == " ")) numbers += chr;
        }

        if (numbers.length < 10) {
            alert("Please include the area code.");
            setTimeout("focusElement('" + el.name + "')", 0);
            return false;
        }

        //assign numbers to chosen format
        var n = 0, i = 0;
        var output = "";
        while (i < format.length && n < numbers.length) {
            chr = format.charAt(i);
            output += (chr == "#") ? numbers.charAt(n++) : chr;
            i++;
        }

        el.value = output; //output to form element
        return true;
    }
    else {
        alert("Please enter a phone number with area code.");
        setTimeout("focusElement('" + el.name + "')", 0);
        return false;
    }
}

function elementSubmit(elName) {
    var f = document.forms[0];
    var s = location.href;

    if (s.indexOf("-form/") != -1) {
        if (s.indexOf("el~") != -1) {
            var arr = s.split("/");
            s = "";
            for (var i = 0; i < arr.length; i++) {
                if (i > 0) s += "/";
                s += (arr[i].indexOf("el~") != -1) ? "el~" + elName : arr[i];
            }
        }
        else {
            s = s.replace("-form", "-form/el~" + elName);
        }
        f.action = s;
        f.submit();
        return;
    }
    
    s = f.action;

    // modify form action on postbacks from form elements
    var pos = s.indexOf("&elname");
    if (pos == -1) pos = s.indexOf("&elName");
    if (pos > 0) {
        s = s.substring(0, pos)
        f.action = s + "&elname=" + elName;
        f.submit();
        return;
    }

    pos = s.indexOf("?elname");
    if (pos == -1) pos = s.indexOf("?elName");
    if (pos > 0) {
        s = s.substring(0, pos)
        f.action = s + "?elname=" + elName;
    }
    else { // not a postback 
        pos = s.indexOf("?");
        if (pos > 0)
            f.action += "&elname=" + elName;
        else 
           f.action += "?elname=" + elName;
    }

    f.submit();
}

function elementSubmitType(elName, type) {

  var f = document.forms[0];
  var s = f.action;

  // modify form action on postbacks from form elements
  var pos = s.indexOf("&elname");
  if (pos == -1) pos = s.indexOf("&elName");
  if (pos > 0) {
		s = s.substring(0, pos)
		f.action = s + "&elname=" + elName + "&type=" + type;	
		f.submit();
		return;
	}	
	
	var pos = s.indexOf("?elname");
	if (pos == -1) pos = s.indexOf("?elName");
  if (pos > 0) {
		s = s.substring(0, pos)
		f.action = s + "?elname=" + elName + "&type=" + type;		
	}	
	else { // not a postback 
		var pos = s.indexOf("?");
		if (pos > 0) 
			f.action += "&elname=" + elName + "&type=" + type;	
		else
			f.action += "?elname=" + elName + "&type=" + type;	
	}
	
  f.submit();
}

function elementSubmitQS(elName) {

    var f = document.forms[0];
    var s = f.action;
    var elValue = eval("f." + elName + ".value");

    // modify form action on postbacks from form elements
    var pos = s.indexOf("&elname");
    if (pos == -1) pos = s.indexOf("&elName");
    if (pos > 0) {
        s = s.substring(0, pos)
        f.action = s + "&elname=" + elName + "&" + elName + "=" + elValue;
        f.submit();
        return;
    }

    var pos = s.indexOf("?elname");
    if (pos == -1) pos = s.indexOf("?elName");
    if (pos > 0) {
        s = s.substring(0, pos);
        f.action = s + "?elname=" + elName + "&" + elName + "=" + elValue;
    }
    else { // not a postback 
        var pos = s.indexOf("?");
        if (pos > 0)
            f.action += "&elname=" + elName + "&" + elName + "=" + elValue;
        else
            f.action += "?elname=" + elName + "&" + elName + "=" + elValue;
    }

    f.submit();
}

function formSubmit(frmName) {

  var f = document.forms[0];
  var s = f.action;
   
  var pos = s.indexOf("&elname");
  if (pos == -1) pos = s.indexOf("&elName");
  if (pos > 0) {
		f.action = s.substring(0, pos);
  }
  
  var pos = s.indexOf("?elname");
  if (pos == -1) pos = s.indexOf("?elName");
  if (pos > 0) { 
		f.action = s.substring(0, pos);
  }

  if (f.action.indexOf("?") == -1)
      f.action += "?confirm=" + frmName;
  else
      f.action += "&confirm=" + frmName;
      

  var isSubmit;
  if (f.onsubmit == undefined) {
      isSubmit = validateForm();
  }
  else
      isSubmit = f.onsubmit();
	
	if ((isSubmit) || (null == isSubmit)) f.submit();
}

function formSubmit_new(frmName) {
    var f = document.forms[0];
    f.action = location.href + "/confirm";
    var isSubmit;
    if (f.onsubmit == undefined)
        isSubmit = validateForm();
    else
        isSubmit = f.onsubmit();

    if ((isSubmit) || (null == isSubmit)) f.submit();
}

function formSubmit_NoValidation() {
  var f = document.forms[0];
  
  var s = f.action;
  var pos = s.indexOf("?");
  if (pos > 0) f.action = s.substring(0, pos);
	
	f.submit();
}
