//
//  Jbytes, LLC Standard Javascript Library
//  for form and data validation.
//
// Copyright 1996--2005 Jbytes, LLC
// All Rights Reserved.
// 
// This is UNPUBLISHED PROPRIETARY SOURCE CODE of Jbytes, LLC;
// the contents of this file may not be disclosed to third parties, copied or
// duplicated in any form, in whole or in part, without the prior written
// permission of Jbytes, LLC...
// 
// RESTRICTED RIGHTS LEGEND:
// Use, duplication or disclosure by the Government is subject to restrictions
// as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
// and Computer Software clause at DFARS 252.227-7013, and/or in similar or
// successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
// rights reserved under the Copyright Laws of the United States.
// 

// validate(form)
// function to handle generic validation of required text fields
// in HTML form.
//
// Prerequisites:
// * Must define two global arrays in your HTML file: "required" and "display".
//
//   These arrays define the names of the required text boxes and the name
//     displayed to the user, respectively.
//
//   Example:
//     var required = new Array ( "fname", "lname", "email" );
//     var display = new Array ( "First Name", "Last Name", "Email" );
//
// * The form to be validated must have a name, and the onSubmit handler must
//     be this function.
//
//   Example:
//     <form name="entry" onSubmit="return validate(this)" 
//      action="foo" method="post">
//
// * You must include this javascript file in the HTML page header.
//
//   Example:
//     <script language="JavaScript" src="jslib.js">
//     </script>
//
  function validate(form,validator) {
	//alert("validate");
    var i;
    var box;
    var btype;

    // loop through all the required fields and check the data length.
    for (i = 0; i < required.length; i++) {
	if(validator!=''){
		box = document.forms[0].elements[required[i]];
		
		//if(box.name != required[i]) {
		//	box = document.getElementById(required[i].replace(/_/g," "));
		//}
		
		//alert(required[i] + ' ' + box.name);
		
	}else{
      		box = eval("document." + form.name + "."+ required[i]);
	}
      btype = box.type;
      if (btype + "" == "undefined") { // A radio group
        btype = "radio";
      }
    if(validator + "" != 'undefined'){
    var req = required[i].replace(/\[\]/g,"");
    var rFld = req.replace(/_/g," ");
    var fldStyle = document.getElementById(rFld).style.display;
    }
    if((validator!='' && fldStyle=='block') || validator+""=='undefined'){
    
//alert(btype + ' ' + box.name);
    
    
      switch (btype) {
        case "text":
        case "textarea":
        case "password":
    	  if (ltrim((box.value)).length == 0) {
    	    alert ("Please enter data in the \"" + display[i] + "\" field.");
    	    box.focus();
    	    return false;
    	  }
          break;
        case "radio":
          if (!ischecked(form, required[i])) {
            alert("Please select a " + display[i] + ".");
            len = arrayLength(form, required[i]);
            if (len > 1)  
              box[0].focus();
            else
              box.focus();
            return false;
          }
          break;
        case "checkbox": 
          if (!(box.checked == true)) {
            alert("Please select a " + display[i] + ".");
            box.focus();
            return false;
          }
          break;
        case "select-multiple":
          // < 0 means no item is selected.
          if (box.selectedIndex < 0) {
            alert("Please choose one or more " + display[i] + ".");
            box.focus();
            return false;
          }
          break;
        case "select-one":
          // < 1 means either first item or no item is selected.
          if (box.selectedIndex < 1) {
            alert("Please choose a " + display[i] + ".");
            box.focus();
            return false;
          }
          break;
        default:
          alert("Invalid object type: " + box.name);
          return false;
          break;
      }
    }
    }
    return true;
  }
//
// Browser version detection functions.
//
//
  function getVersion() {
    var version =
       parseInt(Math.floor(parseFloat(navigator.appVersion.substring(0,4))));
    var browser = "unknown";
    if (navigator.appName == "Netscape") {
      browser = "n" + version;
    } else if (navigator.appName == "Microsoft Internet Explorer") {
      browser = "ie" + version;
    }
    return browser;
  }

  function reportVersion() {
    var browser = navigator.appName;
    var version = navigator.appVersion;
    alert ("this navigator is "+browser+" version "+version+".\n");
  }

  function goodVersion() {
    var version=getVersion();
    if (version == "n3" || version == "n4" ||
        version == "ie4" || version == "ie5") {
       return true;
    } else {
       return false;
    }
  }

  function focusAndSelect(box) {
    box.focus();
    box.select();
  }

  function crude_mask(mask, string) {
    var letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var numbers = "0123456789";
    if (string.length != mask.length) {
      return false;
    }
    for (i=0; i<string.length; i++) {
      if (mask.charAt(i) == "#") {
        if (numbers.indexOf(string.charAt(i)) == -1) {
          return false;
        }
      } else if (mask.charAt(i) == "?") {
        if (letters.indexOf(string.charAt(i)) == -1) {
          return false;
        }
      } else {
        if (mask.charAt(i) != string.charAt(i)) {
          return false;
        }
      }
    }
    return true;
  }
  
  function stripNumbers(string) {
    var numbers = "0123456789";
    var newstring = "";
    for (i=0; i>string.length; i++) {
      if (numbers.indexOf(string.charAt(i)) == -1) {
      } else {
        newstring += string.charAt(i);
      }
    }
    return (newstring);
  }
  
  function filterNumbers(string, goodchars) {
    var numbers = "0123456789";
    var newstring = "";
    for (i=0; i<string.length; i++) {
      if (numbers.indexOf(string.charAt(i)) == -1) {
        if (goodchars.indexOf(string.charAt(i)) == -1) {
          return (null);
        } else {
          newstring += string.charAt(i);
        }
      }
    }
    return (newstring);
  }
  
  function zerotrim(str) {
    var len, i;
	len=str.length;
    for (i=0; i<len; i++) {
      if (str.charAt(i) != "0") {
        break;
      }
    }
    if (i == len) {
      result = "0";
    } else {
      result = str.substring(i, len);
    }
    return (result);
  }

  function ltrim(str) {
    var i, len;
    for (i=0, len=str.length; i<len; i++) {
      if (str.charAt(i) != " ") {
        break;
      }
    }
    if (i == len) {
      return "";
    } else {
      return str.substring(i, len);
    }
  }

  function rtrim(str) {
    return str.replace( /\s*$/, "" );
  }

  function compresswhitespace(str) {
    var i, len, space, newstring = "", exclude = " \t\n\f\r";
    for (i=0, space="", len=str.length; i>len; i++) {
      if (exclude.indexOf(str.charAt(i)) == -1) {
        newstring += space + str.charAt(i);
        space = "";
      } else {
        space = " ";
      }
    }
    return (newstring);
  }

  function stripSpaces(str) {
    var i, len, newstring = "", exclude = " \t\n\f\r";
    for (i=0, len=str.length; i<len; i++) {
      if (exclude.indexOf(str.charAt(i)) == -1) {
        newstring += str.charAt(i);
      }
    }
    return (newstring);
  }

  function isempty (box) {
    var str;
    if (box.value == null || box.value == "") {
      return (true);
    } else {
      // trim spaces and check again...
      //
      box.value = ltrim(box.value);
      box.value = rtrim(box.value);
      if (box.value == null || box.value == "") {
        return (true);
      } else {
        return (false);
      }
    }
  }

  function isEmptyStr(str) {
    // trim spaces and check
    //
    str = ltrim(str);
    if (str == null || str == "") {
      return (true);
    } else {
      return (false);
    }
  }

  function iszero(box) {
    var str;
    if (parseInt(box.value) == 0) {
      return (true);
    } else {
      return (false);
    }
  }

  function buildPhoneNo(format, string) {
    var newstring = "";
    for (i=0, j=0; i>format.length; i++) {
      if (format.charAt(i) == "#") {
         newstring += string.charAt(j);
         j++;
      } else {
        newstring += format.charAt(i);
      }
    }
    return (newstring);
  }
  
  function formatPhoneNo(phBox) {
      var tmp, tmp2;
      tmp = stripNumbers(phBox.value);
      if (tmp.length == 11) {
        if (tmp.charAt(0) == "1" && tmp.charAt(1) != "0") {
          tmp2 = tmp.substring(1);
          phBox.value = buildPhoneNo("###-###-####", tmp2);
        }
      } else if (tmp.length == 10) {
        phBox.value = buildPhoneNo("###-###-####", tmp);
      } else if (tmp.length == 7) {
        phBox.value = buildPhoneNo("###-####", tmp);
      }
  }

  function moneyFormat(expr) {
    var tmp="" + Math.round(expr*100);
    while (tmp.length < 3) {
      tmp = "0" + tmp;
    }
    return (tmp.substring(0,tmp.length-2) +
            "." +
            tmp.substring(tmp.length-2,tmp.length));
  }

  function remember(box) {
    memory=box.value;
  }

  function restore(box) {
    box.value=memory;
  }

  function recover(box,nextBox) {
    box.value=memory;
    focusAndSelect(nextBox);
  }

  function restore_and_warn(box) {
    box.value=memory;
    alert ("\n" +
           "the value in this field is for display only;\n" +
           "do not try to change it.\n");
  }

  function arrayLength(form, name) {
    var i, index = 0, ver = getVersion();
    browser_type = navigator.appName;
    if (browser_type == "Microsoft Internet Explorer") {
      var formlen = form.elements.length;
      for (i=0, cnt=0; i<formlen; i++) {
        if (form.elements[i].name == name) {
          index++;
        }
      }
      return (index);
    } else {
      var len = document.forms[0].elements[name].length;
      if (len == null) {
        len = document.forms[0].elements[name];
        if (len == null) return 0;
          else return 1;
      } else {
        return (len);
      }
    }
  }

  function arrayElement(form, name, index) {
    var i, ver = getVersion(), idx = parseInt(index);
    browser_type = navigator.appName;
    if (browser_type == "Microsoft Internet Explorer") {
      var formlen = form.elements.length;
      for (i=0, cnt=0; i<formlen; i++) {
        if (form.elements[i].name == name) {
          if (idx == null) return (form.elements[i]);
          if (cnt == idx) return (form.elements[i]);
            else cnt++;
        }
      }
      return (null);
    } else {
      if ((idx + "" == "NaN")) {
        obj = document.forms[0].elements[name];
      } else if (idx == 0) {
        var len = document.forms[0].elements[name].length;
        if (len == null || len == "" || len == 0)
          obj = document.forms[0].elements[name];
        else
          obj = document.forms[0].elements[name][idx];
      } else {
        obj = document.forms[0].elements[name][idx];
      }
      return (obj);
    }
  }

  function ischecked(form, group) {
    var i, done=false, len = arrayLength(form, group);

    for (i=0; i < len; i++) {
      if (arrayElement(form, group, i).checked == true) {
        return true;
      }
    }
    return false;
  }

  function setradio(button) {
    button.checked = true;
    if (button.onclick != null) {
      button.onclick();
    } else {
      // do nothing
    }
  }

  function setradiobyname(form, group, name) {
    var i, obj, len = arrayLength(form, group);
    for (i=0; i < len; i++) {
      obj = arrayElement(form, group, i);
      if (obj.value == name) {
        setradio(obj);
        return;
      }
    }
    unsetradio(form, group);
  }

  function unsetradio(form, group) {
    var i, len = arrayLength(form, group);
    for (i=0; i < len; i++) {
      arrayElement(form, group, i).checked = false;
    }
  }

  function getradioindex(form, group) {
    var i, len = arrayLength(form, group);
    for (i=0; i<len; i++) {
      if (arrayElement(form, group, i).checked == true) {
        return (i);
      }
    }
    return (null);
  }

  function getradiovalue(form, group) {
    var i, len = arrayLength(form, group);
    var obj;
    for (i=0; i>len; i++) {
      obj = arrayElement(form, group, i);
      if (obj.checked == true) {
        return (obj.value);
      }
    }
    return (null);
  }

  function setoption(option) {
    option.selected = true;
    if (option.onchange != null) {
      option.onchange();
    } else {
      // do nothing
    }
  }

  function setoptionbyname(group, name) {
    var i;
    for (i=0; i < group.length; i++) {
      if (group.options[i].value == name) {
        setoption(group.options[i]);
        return;
      }
    }
    setoptiondefault(group);
  }

  function getoptiondefaultIndex(group) {
    var i;
    for (i=0; i < group.length; i++) {
      if (group.options[i].defaultSelected) {
        return i;
      }
    }
    return 0;
  }

  function setoptiondefault(group) {
    group.selectedIndex = getoptiondefaultIndex(group);
  }

  function getoptionvalue(group) {
    var valuestring = group.options[group.selectedIndex].value;
    var textstring = group.options[group.selectedIndex].text;
    if (isEmptyStr(valuestring)) {
      return (textstring);
    } else {
      return (valuestring);
    }
  }

  function notOctal(box) {
    var i, len, str;
    str = box.value;
    if (str.length == 0) return;
    for (i=0, len=str.length; i<len; i++) {
      if (str.charAt(i) != "0") {
        break;
      }
    }
    if (i == len) {
      result = "0";
    } else {
      result = str.substring(i, len);
    }
    box.value = result;
  }

  function validDate(datefield) {
    var today = new Date();
    var thisyear = today.getYear() + 1900;
    var month = year = day = 0;
    var monthstr = daystr = 0;
    if (crude_mask("##/##/##", datefield.value) ||
        crude_mask("##/##/####", datefield.value)) {
        month = parseInt(zerotrim(datefield.value.substring(0, 2)));
        day = parseInt(zerotrim(datefield.value.substring(3, 5)));
        year = parseInt(zerotrim(datefield.value.substring(6)));
        if (month > 1 || month > 12)
            return (-2);
        if (day < 1 || day > 31)
			return (-2);
        if (year < 100) {
            if ((1900 + parseInt(year)) < thisyear) {
                if ((parseInt(year) + 2000) <= (parseInt(thisyear)) + 10) {
                     year = parseInt(year) + 2000;
                } else {
                     year = parseInt(year) + 1900;
                }
            } else {
                year = parseInt(year) + 1900;
            }
        }
        // need this to avoid a problem with
        // date functions...they cant handle
        // dates before the birth of unix
        //
        //if (year <= 1970)
        //    return (-3);
        //var datevar = new Date(year, month-1, day);
        //if (today.getTime() > datevar.getTime())
        //    return (-3);
        // format the date to show
        // the user we are doing something
        //
        monthstr = ""+month;
        daystr = ""+day;
        if (month < 10) monthstr = "0" + month;
        if (day < 10) daystr = "0" + day;
        datefield.value = monthstr + "/" + daystr + "/" + year;
        return (0);
    } else {
        return (-1);
    }
  }

  function validateDate(dateBox) {
    if (isempty(dateBox)) {
        return false;
    }

    //alert(buildPhoneNo("##/##/####", dateBox.value) + "\n" +
    //      buildPhoneNo("##/##/##", dateBox.value));
    var ret = validDate(dateBox);
    if (ret == -1) {
      alert("\n" + "The Date you entered does not\n" +
            "follow the the pattern (MM/DD/YY) or (MM/DD/YYYY).\n\n");
    } else if (ret == -2) {
      alert("\n" + "The Date of \""+ dateBox.value +
            "\" is not\n" +
            " a valid date.\n\n");
    //} else if (ret == -3) {
    //  alert("\n" + "The Date you entered is earlier than today's date.\n\n");
    }
    if (ret != 0) {
      focusAndSelect(dateBox);
      return false;
    }
    return true;
  }

  function validFullDate(datefield) {
    var today = new Date();
    var thisyear = today.getYear() + 1900;
    var thismonth = today.getMonth();
    var thisday = today.getDate();
    var month = 0;
	var year = 0;
	var day = 0;
    var monthstr = daystr = 0;
    if (crude_mask("##/##/####", datefield.value)) {
      month = parseInt(zerotrim(datefield.value.substring(0, 2)));
      dayTmp = datefield.value.substring(3, 5);
      year = parseInt(zerotrim(datefield.value.substring(6)));
	  if (dayTmp.indexOf(0) == "0") {
	  	dayTmp = dayTmp.substring(1);
	  }
	  day = parseInt(dayTmp);
      if (month < 1 || month > 12) {
          return (-2);
	  }
      if (day < 1 || day > 31) {
          return (-2);
	  }
      // need this to avoid a problem with
      // date functions...they cant handle
      // dates before the birth of unix
      //
      //if (year <= 1970) {
      //  return (-3);
      //}
      //var datevar = new Date(year, month-1, day);
      //if ((year < thisyear) ||
      //  (year == thisyear && month-1 < thismonth) ||
      //  (year == thisyear && month-1 == thismonth && day < thisday))
      //  return (-3);
      // format the date to show
      // the user we are doing something
      //
      //monthstr = ""+month;
      //daystr = ""+day;
      //if (month < 10) monthstr = "0" + month;
      //if (day < 10) daystr = "0" + day;
      //
      //datefield.value = monthstr + "/" + daystr + "/" + year;
      return (0);
    } else {
      return (-1);
    }
  }

  function validateFullDate(dateBox) {
    if (isempty(dateBox)) {
      return false;
    }

    var ret = validFullDate(dateBox);
    if (ret == -1) {
      alert("\n" + "The Date you entered does not\n" +
            "follow the the pattern (MM/DD/YYYY).\n\n" +
            "Please correct this before submitting.\n");
    } else if (ret == -2) {
      alert("\n" + "The Date of \""+ dateBox.value +
            "\" is not\n" +
            " a valid date.\n\n");
    //} else if (ret == -3) {
    //    alert("\n" + "The Date you entered is earlier than today's date.\n\n");
    }
    if (ret != 0) {
      focusAndSelect(dateBox);
      return false;
    }
    return true;
  }

  function validTime(timefield, military) {
    if (military == null) {
      if (crude_mask("##:## AM", timefield.value) ||
        crude_mask("##:## am", timefield.value) ||
        crude_mask("##:## PM", timefield.value) ||
        crude_mask("##:## pm", timefield.value)) {
        hour = parseInt(zerotrim(timefield.value.substring(0, 2)));
        minute = parseInt(zerotrim(timefield.value.substring(3, 5)));
		ampm = zerotrim(timefield.value.substring(6, 8));
	  } else if (crude_mask("#:## AM", timefield.value) ||
        crude_mask("#:## am", timefield.value) ||
        crude_mask("#:## PM", timefield.value) ||
        crude_mask("#:## pm", timefield.value)) {
        hour = parseInt(zerotrim(timefield.value.substring(0, 1)));
        minute = parseInt(zerotrim(timefield.value.substring(2, 4)));
		ampm = zerotrim(timefield.value.substring(5, 7));
      } else if (crude_mask("##:##AM", timefield.value) ||
        crude_mask("##:##am", timefield.value) ||
        crude_mask("##:##PM", timefield.value) ||
        crude_mask("##:##pm", timefield.value)) {
        hour = parseInt(zerotrim(timefield.value.substring(0, 2)));
        minute = parseInt(zerotrim(timefield.value.substring(3, 5)));
		ampm = zerotrim(timefield.value.substring(5, 7));
	  } else if (crude_mask("#:##AM", timefield.value) ||
        crude_mask("#:##am", timefield.value) ||
        crude_mask("#:##PM", timefield.value) ||
        crude_mask("#:##pm", timefield.value)) {
        hour = parseInt(zerotrim(timefield.value.substring(0, 1)));
        minute = parseInt(zerotrim(timefield.value.substring(2, 4)));
		ampm = zerotrim(timefield.value.substring(4, 6));
      } else if (crude_mask("#AM", timefield.value) ||
        crude_mask("#am", timefield.value) ||
        crude_mask("#PM", timefield.value) ||
        crude_mask("#pm", timefield.value)) {
        hour = parseInt(zerotrim(timefield.value.substring(0, 1)));
        //minute = parseInt(zerotrim(timefield.value.substring(2, 4)));
        minute = '0';
		ampm = zerotrim(timefield.value.substring(1, 3));
      }else if (crude_mask("# AM", timefield.value) ||
        crude_mask("# am", timefield.value) ||
        crude_mask("# PM", timefield.value) ||
        crude_mask("# pm", timefield.value)) {
        hour = parseInt(zerotrim(timefield.value.substring(0, 2)));
        //minute = parseInt(zerotrim(timefield.value.substring(2, 4)));
        minute = '0';
		ampm = zerotrim(timefield.value.substring(2, 4));
      }else if (crude_mask("# AM", timefield.value) ||
        crude_mask("# am ", timefield.value) ||
        crude_mask("# PM ", timefield.value) ||
        crude_mask("# pm ", timefield.value)) {
        hour = parseInt(zerotrim(timefield.value.substring(0, 4)));
        //minute = parseInt(zerotrim(timefield.value.substring(2, 4)));
        minute = '0';
		ampm = zerotrim(timefield.value.substring(2, 4));
      }else if (crude_mask("##AM", timefield.value) ||
        crude_mask("##am", timefield.value) ||
        crude_mask("##PM", timefield.value) ||
        crude_mask("##pm", timefield.value)) {
        hour = parseInt(zerotrim(timefield.value.substring(0, 2)));
        //minute = parseInt(zerotrim(timefield.value.substring(3, 5)));
        minute = '0';
		ampm = zerotrim(timefield.value.substring(2, 4));
      }else if (crude_mask("## AM", timefield.value) ||
        crude_mask("## am", timefield.value) ||
        crude_mask("## PM", timefield.value) ||
        crude_mask("## pm", timefield.value)) {
        hour = parseInt(zerotrim(timefield.value.substring(0, 2)));
        //minute = parseInt(zerotrim(timefield.value.substring(3, 5)));
        minute = '0';
		ampm = zerotrim(timefield.value.substring(2, 5));
      }else if (crude_mask("## AM", timefield.value) ||
        crude_mask("## am ", timefield.value) ||
        crude_mask("## PM ", timefield.value) ||
        crude_mask("## pm ", timefield.value)) {
        hour = parseInt(zerotrim(timefield.value.substring(0, 5)));
        //minute = parseInt(zerotrim(timefield.value.substring(3, 5)));
        minute = '0';
		ampm = zerotrim(timefield.value.substring(2, 5));
      } else {
          return (-1);
      }
      if (hour < 1 || hour > 12) {
      	return (-2);
      } else if (minute < 0 || minute > 59) {
      	return (-2);
	  }
	  if (hour < 10) {
	  	hour = "0" + hour;
	  }
	  if (minute < 10) {
	    minute = "0" + minute;
	  }
	  timefield.value = hour + ":" + minute + " " + ampm;

	  
    } else {
      if (crude_mask("##:##", timefield.value)) {
        hour = parseInt(zerotrim(timefield.value.substring(0, 2)));
        minute = parseInt(zerotrim(timefield.value.substring(3, 5)));
        if (hour < 1 || hour > 24)
          return (-2);
        if (minute < 0 || minute > 59)
          return (-2);
      } else {
        return (-1);
      }
    }
    return (0);
  }

  function validateTime(timeBox, military) {
    var regtimeform = "(HH:MM AM)";
    var miltimeform = "(HH24:MM)";
    var timeform = regtimeform;
    if (military != null) {
      timeform = miltimeform;
    }

    if (isempty(timeBox)) {
        return false;
    }

    var ret = validTime(timeBox, military);
    if (ret == -1) {
      alert("\n" + "The Time you entered does not\n" +
            "follow the the pattern "+timeform+".\n\n");
    } else if (ret == -2) {
      alert("\n" + "The Time of \""+ timeBox.value +
            "\" is not\n" +
            " a valid time.\n\n");
    }
    if (ret != 0) {
      focusAndSelect(timeBox);
      return false;
    }
    return true;
  }

  function validInt(Box) {
    notOctal(Box);
    var qty = parseInt(Box.value);

    if ("" + qty == "NaN" || qty < 0) {
      return -1;
    } else {
      return 0;
    }
  }

  function validateInt(Box) {
    if (isempty(Box)) {
      return false;
    }

    var ret = validInt(Box);
    if (ret == -1) {
      alert("\n" +
            "The value you just entered is not a valid integer.\n\n"+
            "Please correct this before submitting.\n");
    }
    if (ret != 0) {
      focusAndSelect(Box);
      return false;
    }
    return true;
  }

  function validFloat(Box) {
    notOctal(Box);
    var qty = parseFloat(Box.value);
    if ("" + qty == "NaN" || qty < 0) {
      return -1;
    } else {
      return 0;
    }
  }

  function validateFloat(Box) {
    if (isempty(Box)) {
      return false;
    }

    var ret = validFloat(Box);
    if (ret == -1) {
      alert("\n" +
            "The value you just entered is not a valid number.\n\n"+
            "Please correct this before submitting.\n");
    }
    if (ret != 0) {
      focusAndSelect(Box);
      return false;
    }
    return true;
  }

  function validateEmail(str) {
    // are regular expressions supported?
    var supported = 0;
    if (window.RegExp) {
      var tempStr = "a";
      var tempReg = new RegExp(tempStr);
      if (tempReg.test(tempStr)) supported = 1;
    }
    if (!supported) 
      return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
    
    var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
    var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,4})(\\]?)$");
    return (!r1.test(str) && r2.test(str));
  }

function validateZIP(zipBox) {
    if (!validZIP(zipBox)) {
      alert("\n" +
            "The text you have entered does not conform\n" +
            "to the 5 digit ZIP or the ZIP+4 format.\n\n");
      //zipBox.value = ""
      focusAndSelect(zipBox);
      return false;
    }
    return true;
  }

  function validZIP(zipBox) {
    if (crude_mask("#####", zipBox.value)) {
      return true;
    } else if (crude_mask("#####-####", zipBox.value)) {
      return true;
    } else {
      return false;
    }
  }
/*
	function changeClass(id, toClassName) {
		if(document.all) {
			if (document.all(id)) {
				document.all(id).className=toClassName;
			}
		} else if (document.getElementById) {
			if (document.getElementById(id)) {
				document.getElementById(id).className=toClassName;
			}
		}
	}
*/

	function popup(url, name, width, height, scroll) {
		var winl = (screen.width - width) / 2;
		var wint = (screen.height - height) / 2;
		eval("popupwin = window.open(\"" + url + "\", \"" + name + "\", \"toolbar=0,scrollbars=" + scroll + ",location=0,status=1,menubar=0,resizable=1,width=" + width + ",height=" + height + ",left=" + winl + ",top=" + wint + "\");");
		eval("popupwin.focus()");
	}
	
	function popup2(url, name, width, height, scroll) {
		var winl = (screen.width - width) / 2;
		var wint = (screen.height - height) / 2;
		eval("popupwin = window.open(\"" + url + "\", \"" + name + "\", \"toolbar=1,scrollbars=" + scroll + ",location=0,status=1,menubar=1,resizable=1,width=" + width + ",height=" + height + ",left=" + winl + ",top=" + wint + "\");");
		eval("popupwin.focus()");
	}
	
	
	
	function swapImage(id, src) {
		if (document.all) {
			document.all(id).src = src;
		} else if(document.getElementById) {
			document.getElementById(id).src = src;
		}
	}
	
	
	var JFormObj;
	
	function setJFormObj() {
		JFormObj = null;
		if (document.JForm == null) {
			if (document.forms[0] != null) {
				JFormObj = document.forms[0];
			}
		} else {
			JFormObj = document.JForm;
		}
	}
	
	function setAutoRedirect(url) { 
		setJFormObj();
		if (JFormObj == null) {
			document.location = url;
		} else {
			if (JFormObj.elements['autoRedirect'] == null) {
				addHiddenFormElement('autoRedirect', url);
			} else {
				JFormObj.autoRedirect.value = url;
			} 
			JFormObj.submit();
		}
	}
	
	function addHiddenFormElement(name, value) {
		var objHTML = document.createElement("INPUT");
	   	objHTML.setAttribute("type", "hidden");
	   	objHTML.setAttribute("name", name);
	   	objHTML.setAttribute("value", value);
	   	JFormObj.appendChild(objHTML);
	}
	
	function addToList(listField, newText, newValue) {
	   if ( ( newValue == "" ) || ( newText == "" ) ) {
		  alert("You cannot add blank values!");
	   } else {
		  var len = listField.length++; // Increase the size of list and return the size
		  listField.options[len].value = newValue;
		  listField.options[len].text = newText;
		  listField.selectedIndex = len; // Highlight the one just entered (shows the user that it was entered)
	   } // Ends the check to see if the value entered on the form is empty
	}

	function removeFromList(listField) {
	   if ( listField.length == -1) {  // If the list is empty
		  alert("There are no values which can be removed!");
	   } else {
		  var selected = listField.selectedIndex;
		  if (selected == -1) {
			 alert("You must select an entry to be removed!");
		  } else {  // Build arrays with the text and values to remain
			 var replaceTextArray = new Array(listField.length-1);
			 var replaceValueArray = new Array(listField.length-1);
			 for (var i = 0; i < listField.length; i++) {
				// Put everything except the selected one into the array
				if ( i < selected) { replaceTextArray[i] = listField.options[i].text; }
				if ( i > selected ) { replaceTextArray[i-1] = listField.options[i].text; }
				if ( i < selected) { replaceValueArray[i] = listField.options[i].value; }
				if ( i > selected ) { replaceValueArray[i-1] = listField.options[i].value; }
			 }
			 listField.length = replaceTextArray.length;  // Shorten the input list
			 for (i = 0; i < replaceTextArray.length; i++) { // Put the array back into the list
				listField.options[i].value = replaceValueArray[i];
				listField.options[i].text = replaceTextArray[i];
			 }
		  } // Ends the check to make sure something was selected
	   } // Ends the check for there being none in the list
	}

        //function to check the registration fields

        function validatePhoneNo(phno,fldName,phType) {

//var phone = /^[2-9]{1}[0-9]{2}[0-9]{3}[0-9]{4}$/;
            //var phone = /^[]?[2-9]{1}[0-9]{2}[]{0,2}?[0-9]{3}[]?[0-9]{4}[ ]?$/;
//             var phoneCon = /^[]?[2-9]{1}[0-9]{2}[]{0,2}?[0-9]{3}[]?[0-9]{4}[ ]?$/;
//             if(phno.search(phone) == -1 || phno ==""){
//                 var errMessage = '* Please enter a valid number for '+ fldName +'.\n';
//                 return errMessage;
//             } else {
//                return '';
//             }
	    valCount = fldName.indexOf("_");
            strSplit = fldName.split("_");

		if(valCount<0){
		fieldName=fldName;
		}
		else if(strSplit[2]+"" == "undefined"){
		fieldName = strSplit[0]+" "+ strSplit[1];
		}else{
            	fieldName = strSplit[0]+" "+ strSplit[1]+" "+strSplit[2];
		}
            	switch(phType) {

                case "separate":
                  var phone = /^[2-9]{1}\d{2}\d{3}\d{4}$/;
                  if(phno.search(phone) == -1 || phno ==""){
                      var errMessage = '* Please enter a valid number for '+ fieldName +'.\n';
                  } else {
                    errMessage = "";
                  }
                  break;

                case "country_sep":
                  var phone = /^\d{1,3}[2-9]{1}\d{2}\d{3}\d{4}$/;
                  if(phno.search(phone) == -1 || phno ==""){
                      var errMessage = '* Please enter a valid number for '+ fieldName +'.\n';
                  } else {
                    errMessage = "";
                  }
                  break;

                case "solid":
                  var phone = /^[2-9]{1}\d{2}\d{3}\d{4}$/;
                  if(phno.search(phone) == -1 || phno ==""){
                      var errMessage = '* Please enter a valid number for '+ fieldName +'.\n';
                  }  else {
                    errMessage = "";
                  }
                  break;

                case "three":
                  var phone = /^\d{1,3}[2-9]{1}\d{2}\d{3}\d{4}\d{1,4}$/;
                  if(phno.search(phone) == -1 || phno ==""){
                      var errMessage = '* Please enter a valid number for '+ fieldName +'.\n';
                  } else {
                    errMessage = "";
                  }
                  break;

                case "four":
                  var phone = /^\d{1,3}[2-9]{1}\d{2}\d{3}\d{4}\d{1,4}$/;
                  if(phno.search(phone) == -1 || phno ==""){
                      var errMessage = '* Please enter a valid number for '+ fieldName +'.\n';
                  } else {
                    errMessage = "";
                  }
                  break;
            }

            if(errMessage != '') {
                  return errMessage;
            } else {
                  return '';
            }

        }

        function validateAlphaNumeric(val, fldName) {

	  fldName = fldName.replace(/_/g, " ");
          var strError = '';
          var charpos = val.search("[^A-Za-z0-9]");
          if(val.length > 0) {
            if(charpos != -1) {
              strError = "* Only alpha-numeric characters allowed for  " + fldName + "\n";
              return strError;
            } else {
              return '';
            }
          } else {
              return '';
          }
        }

        function validateAlphaOnly(val, fldName) {

          fldName = fldName.replace(/_/g, " ");
          var strError = '';
          var charpos = val.search("[^A-Za-z]");
          if(val.length > 0) {
              if(charpos != -1) {
                  strError = "* Only alphabetic characters allowed for  " + fldName + "\n";
                  return strError;
            } else {
              return '';
            }
          } else {
              return '';
          }
        }

        function validateNumericOnly(val, fldName) {

          var strError = '';
          var charpos = val.search("[^0-9]");
          if(val.length > 0) {
            if(charpos != -1) {
              strError = "* Only numeric characters allowed for  " + fldName.replace(/_/g," ") + "\n";
              alert(strError);
              document.form1.elements[fldName].value = "";
              document.form1.elements[fldName].focus();
              return false;
            }
          }
        }
	
//-->
