function checkLength(field, charMax, noAlrt, noTruncate) {
  if (!noAlrt) { noAlrt = ""; }
  var alrt = false;
  if (noAlrt == "") { alrt = true; }
  if (!noTruncate) { noTruncate = ""; }
  var trunc = false;
  if (noTruncate == "") { trunc = true; }
  var msg;
  var alertMsg = "\nYou have reached the " + charMax +" character maximum!";
  var truncMsg = "Text will be truncated at " + charMax + " characters!";
  var noTruncMsg = "Text will NOT be truncated at " + charMax + " characters!";
  var value = field.value;
  if (value.length > charMax) {
    if (trunc) {
      field.value = value.substring(0,charMax);
    }
    if (alrt) {
      if (trunc) {
        msg = alertMsg + "\n\n" + truncMsg;
      }
      else {
        msg = alertMsg + "\n\n" + noTruncMsg;
      }
      alert(msg);
      field.focus();
      field.select();
    }
    return false;
  }
  else {
    return true;
  }
}
// Checks Required Fields
function isRequired(field, fieldName, noAlrt) {
  if (!noAlrt) { noAlrt = ""; }
  var alrt = false;
  if (noAlrt == "") { alrt = true; }
  var str = field.value;
  // Return false if field is blank.
  if (str == "") {
    if (alrt) {
      alert("\nThe " + fieldName + " field is blank.\n\nPlease enter the " + fieldName + ".")
      field.focus();
      field.select();
    }
    return false;
  }
  return true;
}
// Checks Checkboxes
function checkRadioBox(field) {
  var checkbox_choices = 0;
  // Loop from zero to the one minus the number of buttons/boxes
  for (var counter = 0; counter<field.length; counter++) {
    if (field[counter].checked) {
      if (field[counter].value != "") {
        checkbox_choices = checkbox_choices + 1;
      }
    }
  }
  return checkbox_choices;
}
// Clear Radio Buttons or CheckBoxes
function clearRadioBox(field) {
  for (var counter = 0; counter<field.length; counter++) {
    field[counter].checked = false;
  }
}
// checks Listboxes
function checkListBox(field) {
  // set var listbox_choices to zero
  var listbox_choices = 0;
  for (var counter = 0; counter<field.length; counter++) {
    if (field[counter].selected) {
      if (field[counter].value != "") {
        listbox_choices = listbox_choices + 1;
      }
    }
  }
  return listbox_choices;
}
// Clear ListBoxes
function clearListBox(field) {
  for (var counter = 0; counter<field.length; counter++) {
    field[counter].selected = false;
  }
}
// Checks for double quotes
function quoteCheck(field, fieldName, noalrt) {
  if (!noalrt) { noalrt = ""; }
  var alrt = false;
  if (noalrt == "") { alrt = true; }
  var str = field.value;
  if (str == "") return true;
  if (window.RegExp) {
    str = str.replace(/"+/g, "&#34;");
    str = str.replace(/'+/g, "'");
    str = str.replace(/'/g, "&#39;");    
    str = trimAll(str);
    field.value = str;
  }
  // Return false if the field contains a '"'.
  if (str.indexOf('"',0) != -1) {
    if (alrt) {
      alert("\nThe " + fieldName + " field can't contain double quotes.\n\nPlease re-enter the " + fieldName + ".\n")
      field.focus();
      field.select();
    }
    return false;
  }
  return true;
}
// Checks Email Fields
function checkEmail(field, fieldName, noalrt) {
  if (!noalrt) { noalrt = ""; }
  var alrt = false;
  if (noalrt == "") { alrt = true; }
  var str = field.value;
  if (str == "") { return true; }
  if (window.RegExp) {
//    var ex1 = /(^[a-z0-9]([a-z0-9_\.-]*)@([a-z_\.]+)(\.[a-z]{3})$)|(^[a-z0-9]([a-z0-9_\.-]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*)$/i;
    var ex1 = /^[^@]+@([\w-]+\.)+[a-z]{2,4}$/i;
    if (ex1.test(str)) return true;
    if (alrt) {
			   alert("\nThe " + fieldName + " is invalid.\n\nPlease enter a valid " + fieldName + ".\n");
      field.focus();
      field.select();
		  }
    return false;
  }
  var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
  var i;
  for (i=0; i<invalidChars.length; i++) {
    if (str.indexOf(invalidChars.charAt(i),0) > -1) {
      if (alrt) {
        alert("\nThe " + fieldName + "contains invalid characters.\n\nPlease enter a valid " + fieldName + ".\n");
        field.focus();
        field.select();
		    }
      return false;
    }
  }
  for (i=0; i<str.length; i++) {
    if (str.charCodeAt(i)>127) {
      if (alrt) {
        alert("\nThe " + fieldName + "contains non ascii characters.\n\nPlease enter a valid " + fieldName + ".\n");
        field.focus();
        field.select();
		    }
      return false;
    }
  }
  var atPos = str.indexOf('@',0);
  if (atPos == -1) {
    if (alrt) {
      alert("\nThe " + fieldName + "must contain an @.\n\nPlease enter a valid " + fieldName + ".\n");
      field.focus();
      field.select();
		  }
    return false;
  }
  if (atPos == 0) {
    if (alrt) {
      alert("\nThe " + fieldName + "must not start with @.\n\nPlease enter a valid " + fieldName + ".\n");
      field.focus();
      field.select();
		  }
    return false;
  }
  if (str.indexOf('@', atPos + 1) > - 1) {
    if (alrt) {
      alert("\nThe " + fieldName + "must contain only one @.\n\nPlease enter a valid " + fieldName);
      field.focus();
      field.select();
		  }
    return false;
  }
  if (str.indexOf('.', atPos) == -1) {
    if (alrt) {
      alert("\nThe " + fieldName + "must contain a period in the domain name.\n\nPlease enter a valid " + fieldName + ".\n");
      field.focus();
      field.select();
		  }
    return false;
  }
  if (str.indexOf('@.',0) != -1) {
    if (alrt) {
      alert("\na period (.) must not immediately follow the @ in email address.\n\nPlease enter a valid " + fieldName);
      field.focus();
      field.select();
		  }
    return false;
  }
  if (str.indexOf('.@',0) != -1){
    if (alrt) {
      alert("\nA period (.) must not immediately precede the @ in email address.\n\nPlease enter a valid " + fieldName + ".\n");
      field.focus();
      field.select();
		  }
    return false;
  }
  if (str.indexOf('..',0) != -1) {
    if (alrt) {
      alert("\nTwo periods must not be adjacent in email address.\n\nPlease enter a valid " + fieldName + ".\n");
      field.focus();
      field.select();
		  }
    return false;
  }
  var suffix = str.substring(str.lastIndexOf('.')+1);
  if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
    if (alrt) {
      alert("\nInvalid primary domain in email address.\n\nPlease enter a valid " + fieldName + ".\n");
      field.focus();
      field.select();
		  }
    return false;
  }
  return true;
}
function checkURL(field, fieldName, required, noalrt) {
  if (!noalrt) { noalrt = ""; }
  var alrt = false;
  if (noalrt == "") { alrt = true; }
  var str = field.value;
  if (str == "" && required) {
    if (alrt) {
      alert("\nPlease enter a URL in the " + fieldName + " field.\n");
      field.focus();
      field.select();
    }
    return false;
  }
  if (window.RegExp) {
    var ex = /^(http:\/\/)+/;
    if (ex.test(str)) {
      str = str.replace(ex, "http://");
      field.value = str;
    }
    var ex1 = /^http:\/\/$/;
    if (ex1.test(str)) {
      if (required) {
        if (alrt) {
          alert("\nPlease enter a complete URL in the " + fieldName + " field.\n");
          field.focus();
          field.select();
        }
        return false;
      }
      else {
        field.value = "";
        return true;
      }
    }
    var ex2 = /^[\d\w]+\./;
    if (ex2.test(str)) {
      str = "http://" + str;
      field.value = str;
    }
    var ex3 = /\.[\d\w]+$/;
    if (ex3.test(str)) {
      var ex4 = /(\?|\%3f|#|\%23)/i;
      if (!ex4.test(str)) {
        var ex5 = /\.html?$/;
        if (!ex5.test(str)) { 
          str = str + "/";
        }
      }
      field.value = str;
    }
  }
  // Return false if URL field is less than 10 characters.
  if (str.length < 10) {
    if (alrt) {
      alert("\nThe " + fieldName + " field is not a valid length.\n\nPlease re-enter the " + fieldName + ".\n")
      field.focus();
      field.select();
    }
    return false;
  }
  // Return false if characters are not numbers, letters, a colon, a foward slash, a hyphen, a question mark, a tilde, a hash, a percent, an underscore or a fullstop.
  for (var i = 0; i < str.length; i++) {
    var ch = str.substring(i, i + 1);
    if ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch) && (ch != "0") && (ch != "1") && (ch != "2") && (ch != "3") && (ch != "4") && (ch != "5") && (ch != "6") && (ch != "7") && (ch != "8") && (ch != "9") && (ch != ".") && (ch != "/") && (ch != ":") && (ch != "-") && (ch != "?") && (ch != "~") && (ch != "#") && (ch != "%") && (ch != "_") && (ch != "=")) {
      if (alrt) {
        alert("\nThe " + fieldName + " field only accepts numbers, letters, fullstops, :, /, -, ~, #, %, _, =, and ?.\n\nPlease re-enter the " + fieldName + ".\n");
        field.select();
        field.focus();
      }
      return false;
    }
  }
  if (window.RegExp) {
   // Check for valid start of URL
    var protocol = /^\w{3,6}:\/\//;
    if (!protocol.test(str)) {
      if (alrt) {
        alert("\nThe start of the URL field is not valid.\n\nPlease re-enter the URL.\n")
        field.focus();
        field.select();
      }
      return false;
    }
  }
  return true;
}
function encodeURL(field, noalrt) {
  if (!noalrt) { noalrt = ""; }
  var alrt = false;
  if (noalrt == "") { alrt = true; }
  str = field.value;
  if (str == "") return;
  field.value = URLEncode(str, alrt);
}
function decodeURL(field, noalrt) {
  if (!noalrt) { noalrt = ""; }
  var alrt = false;
  if (noalrt == "") { alrt = true; }
  str = field.value;
  if (str == "") return;
  field.value = URLDecode(str, alrt);
}
function URLEncode(str, alrt) {
// The Javascript escape and unescape functions do not correspond with what browsers actually do, so...
  var SAFECHARS = "0123456789" + // Numeric
          "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
          "abcdefghijklmnopqrstuvwxyz" +
          "-_.!~*'()"; // RFC2396 Mark characters
  var HEX = "0123456789ABCDEF";
  var plaintext = str;
  var encoded = "";
  for (var i = 0; i < plaintext.length; i++ ) {
    var ch = plaintext.charAt(i);
    if (ch == " ") {
      encoded += "+"; // x-www-urlencoded, rather than %20
    }
    else if (SAFECHARS.indexOf(ch) != -1) {
        encoded += ch;
    }
    else {
        var charCode = ch.charCodeAt(0);
        if (charCode > 255) {
          if (alrt) {
            alert( "\nUnicode Character '" + ch + "' cannot be encoded using standard URL encoding.\n" + "(URL encoding only supports 8-bit characters.)\n" + "A space (+) will be substituted.\n" );
          }
          encoded += "+";
        }
        else {
          encoded += "%";
          encoded += HEX.charAt((charCode >> 4) & 0xF);
          encoded += HEX.charAt(charCode & 0xF);
        }
      }
   }
   return encoded;
}
function URLDecode(encoded, alrt) {
  // Replace + with ' '
  // Replace %xx with equivalent character
  // Put [ERROR] in output if %xx is invalid.
  if (encoded == "") return "";
  var HEXCHARS = "0123456789ABCDEFabcdef";
  var plaintext = "";
  var i = 0;
  while (i < encoded.length) {
    var ch = encoded.charAt(i);
    if (ch == "+") {
      plaintext += " "; i++;
    } else if (ch == "%") {
      if (i < (encoded.length-2) && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
        plaintext += unescape(encoded.substr(i,3));
        i += 3;
        } else {
          if (alrt) {
            alert( "\nBad escape combination near ..." + encoded.substr(i) + "\n" );
          }
          plaintext += "%[ERROR]"; i++;
      }
    } else { plaintext += ch; i++; }
  }
  return plaintext;
}
function trimAll(str) {
  if (str && window.RegExp) {
    // Removes leading and trailing spaces
    // and replaces multiple spaces with single space
    str = str.replace(/^\s+/, "");
    if (str == "") return str;
    str = str.replace(/\s+$/, "");
    if (str == "") return str;
    str = str.replace(/\s+/, " ");
  }
  return str;
}
function isNumber(strValue) {
  if (isNaN(strValue)) { return false; }
  return true;
}
function isInteger(strValue){
  var i;
  var ch;
  for (i = 0; i < strValue.length; i++){   
    ch = strValue.charAt(i);
    if (((ch < "0") || (ch > "9"))) { return false };
  }
  return true;
}
function stripChars(str, chars) {
  var i;
	var ch;
  var returnString = "";
  // Search through string's characters one by one.
  // If character is not in chars, append to returnString.
  for (i = 0; i < str.length; i++) {   
    ch = str.charAt(i);
    if (chars.indexOf(ch) == -1) returnString += ch;
  }
  return returnString;
}
function chkPhoneNumber(strPhone) {
  // non-digit characters which are allowed in phone numbers
  var phoneNumberDelimiters = "+()- ";
  // Minimum no of digits in a phone number
  var minDigitsInPhoneNumber = 10;
  var str=stripChars(strPhone,phoneNumberDelimiters);
  return (isInteger(str) && str.length >= minDigitsInPhoneNumber);
}
/*
getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue)
The parameters are:
oElm - Mandatory. This is element in whose children you will look for the attribute.
strTagName - Mandatory. This is the name of the HTML elements you want to look in. Use wildcard (*) if you want to look in all elements.
strAttributeName - Mandatory. The name of the attribute you’re looking for.
strAttributeValue - Optional. If you want the attribute you’re looking for to have a certain value as well.
Examples:  
getElementsByAttribute(document.body, "*", "id"); getElementsByAttribute(document.getElementById("the-form"), "input", "type", "text"); 
*/
function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
    var oCurrent;
    var oAttribute;
    for(var i=0; i<arrElements.length; i++){
        oCurrent = arrElements[i];
        oAttribute = oCurrent.getAttribute(strAttributeName);
        if(typeof oAttribute == "string" && oAttribute.length > 0){
            if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
                arrReturnElements.push(oCurrent);
            }
        }
    }
    return arrReturnElements;
}
function detect_IE() {
  var browserName=navigator.appName; 
  if (browserName=="Microsoft Internet Explorer") {
    return 1;
  }
  else {
    return 0;
  }
}