// Disable the submit buttons to prevent multiple submissions.
function lockButtons(frm)
{
  for(i=0; i<frm.elements.length; i++){
    if(frm.elements[i].type == 'submit'){
      frm.elements[i].disabled = true;
    }
  }
  return true;
}

// Only allow single button click.
function lockForm()
{
  lockForm = blockForm;
  return true;
}
function blockForm()
{
    return false;
}

function disableSubmitButtons (number) {
  for(var i=1; i<=number; i++) {
    var button = getRawObject('submitButton'+i);
    if(button != null) {
      button.disabled = true;
    }
  }
}


// Popup windows.
var myWindow;
function openCenteredWindow(url, w, h) {
    var width  = 800;
    if(w > 400)
      width = w;
    var height = 600;
    if(h > 300)
      height = h;
    var left   = parseInt((screen.availWidth/2) - (width/2));
    var top    = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" + height + 
        ",status,resizable,left=" + left + ",top=" + top + 
        "screenX=" + left + ",screenY=" + top;
    myWindow = window.open(url, "subWind", windowFeatures);
}


// Allow IE users to select from a pulldown by typing.
function autoSelect(field, listName)
{
  var value = field.value;
  var list  = field.form.elements[listName];
  if(value == '')
  {
    list.selectedIndex = 0;
  }
  
  value     = "^" + value
  var re    = new RegExp(value, "i");
  var index = list.selectedIndex;
  for(var i=0; i < list.length; i++)
  {
    test = list.options[i].text;
    if(test.match(re))
    {
      index = i;
      break;
    }
  }
  list.selectedIndex = index;
}


function centerOnWindow(elemID){
  var obj = getRawObject(elemID);
  var scrollX = 0, scrollY = 0;
  if(document.body && typeof document.body.scrollTop != "undefined"){
    scrollX += document.body.scrollLeft;
    scrollY += document.body.scrollTop;
    if (document.body.parentNode &&
        typeof document.body.parentNode.scrollTop != "undefined"){
      scrollX += document.body.parentNode.scrollLeft;
      scrollY += document.body.parentNode.scrollTop;
    }
  } else if(typeof window.pageXOffset != "undefined"){
    scrollX += window.pageXOffset;
    scrollY += window.pageYOffset;
  }
  var x = Math.round((getInsideWindowWidth()/2) - 
		     (getObjectWidth(obj)/2)) + scrollX;
  var y = Math.round((getInsideWindowHeight()/2) - 
		     (getObjectHeight(obj)/2)) + scrollY;
  shiftTo(obj, x, y);
  show(obj);
  return true;
}


/* ***********************************************************
Example 2-12 
"Dynamic HTML:The Definitive Reference"
2nd Edition
by Danny Goodman
Published by O'Reilly & Associates  ISBN 1-56592-494-0
http://www.oreilly.com
Copyright 2002 Danny Goodman.  All Rights Reserved.
************************************************************ */
// Modified to only support mm/dd/yyyy
function checkDate(fld) {
    var mo, day, yr;
    var entry = fld.value;
    var re = /\b\d{1,2}[\/]\d{1,2}[\/]\d{4}\b/;
    if (re.test(entry)) {
        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);
        var testDate = new Date(yr, mo-1, day);
	//        alert(testDate)
        if (testDate.getDate() == day) {
            if (testDate.getMonth() + 1 == mo) {
                if (testDate.getFullYear() == yr) {
                    return true;
                } else {
                    alert("There is a problem with the year entry.");
                    fld.focus();
                }
            } else {
                alert("There is a problem with the month entry.");
                fld.focus();
            }
        } else {
          fld.focus();
            alert("There is a problem with the date entry.");
        }
    } else {
        alert("Incorrect date format. Enter as mm/dd/yyyy.");
        fld.focus();
    }
    return false;
}





/* ***********************************************************
Example 8-3
"Dynamic HTML:The Definitive Reference"
2nd Edition
by Danny Goodman
Published by O'Reilly & Associates  ISBN 1-56592-494-0
http://www.oreilly.com
Copyright 2002 Danny Goodman.  All Rights Reserved.
************************************************************ */
// validates that the field value string has one or more characters in it
function isNotEmpty(elem, msg) {
  var str = elem.value;
  var re = /.+/;
  if(!str.match(re)) {
    if(msg){
      alert(msg);
    } else {
      alert("Please fill in the required field.");
    }
    setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
    return false;
  } else {
    return true;
  }
}
//validates that the entry is a positive or negative number
function isNumber(elem, msg) {
  var str = elem.value;
  var re = /^[-]?\d*\.?\d*$/;
  str = str.toString();
  if (!str.match(re)) {
    if(msg){
      alert(msg);
    } else {
      alert("Enter only numbers into the field.");
    }
    setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
    return false;
  }
  return true;
}
// validates that the entry is 16 characters long
function isLen16(elem, msg) {
  var str = elem.value;
  var re = /\b.{16}\b/;
  if (!str.match(re)) {
    if(msg){
      alert(msg);
    } else {
      alert("Entry does not contain the required 16 characters.");
    }
    setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
    return false;
  } else {
    return true;
  }
}
// validates that the entry is formatted as an e-mail address
function isEMailAddr(elem, msg) {
  var str = elem.value;
  var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
  if (!str.match(re)) {
    if(msg){
      alert(msg);
    } else {
      alert("Verify the e-mail address format.");
    }
    setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
    return false;
  } else {
    return true;
  }
}
// validate that the user made a selection other than default
function isChosen(select, msg) {
  if (select.selectedIndex <= 0) {
    if(msg){
      alert(msg);
    } else {
      alert("Please make a choice from the list.");
    }
    return false;
  } else {
    return true;
  }
}

// validate that the user has checked one of the radio buttons
function isValidRadio(radio, msg) {
    var valid = false;
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            return true;
        }
    }
    if(msg){
      alert(msg);
    } else {
      alert("Make a choice from the radio buttons.");
    }
    return false;
}

function focusElement(formName, elemName) {
    var elem = document.forms[formName].elements[elemName];
    elem.focus();
    elem.select();
}


function numeralsOnly(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        alert("Enter numerals only in this field.");
        return false;
    }
    return true;
}


function decimalsOnly(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
    if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
        alert("Enter numerals only in this field.");
        return false;
    }
    return true;
}



