/*  Form Builder form field validation routines    */


//check for a valid date
function check_valid_date(fld, label) {
  return (true);
}





function check_valid_number(fld, label) {
  
  val = theForm[fld].value;
  
  val = val.replace(',', '');
  val = val.replace(' ', '');
  
  if(val == '' || isNaN(val)) {
    
    alert("Please enter a numeric value in the '" + label + "' field.");
    theForm[fld].focus();
    return (false);
  }
  
  return (true);
}






//checks for a valid selection from a select list
function check_valid_selection(fld, label) {
  var idx = theForm[fld].selectedIndex;
  
  if(idx <= 0) {
    show_msg(fld, "'" + label + "' may not be left blank. Please make a selection to continue.");
    theForm[fld].focus();
    return (false);
  }
  
  return (true);
}






//checks for a valid selection from a radio group or checkbox group
function check_valid_option(fld, label) {
  
  var checked = false;
  
  for(i=0;i<theForm[fld].length;i++) {
    if(theForm[fld][i].checked == true) {
      checked = true;
      break;
    }
  }
  
  if(!checked) {
    show_msg(fld, "'" + label + "' may not be left blank. Please make a selection to continue.");
    return (false);
  }
  
  return (true);
}






//checks for a valid entry into a standard text box
function check_valid_length(fld, min, max, label) {
  
  if(theForm[fld].value.length == 0 || theForm[fld].value.length < min) {
    
    if(min <= 1) {
      show_msg(fld, "'" + label + "' may not be left blank.");
    }
    else {
      show_msg(fld, "'" + label + "' must contain at least " + min + " characters.");
    }
    
    theForm[fld].focus();
    return (false);
  }
  
  if(theForm[fld].value.length > max) {
    
    show_msg(fld, "'" + label + "' must contain less than " + max + " characters.");
    
    theForm[fld].focus();
    return (false);
  }
  
  return (true);
}






//check for valid social security number from text box (fld)
function check_valid_ssn(fld, label) {
  
  part1 = theForm[fld + "_1"].value;
  part2 = theForm[fld + "_2"].value;
  part3 = theForm[fld + "_3"].value;
  
  if(part1.length != "3" || isNaN(part1)) {
    alert("Please enter a valid social security number.");
    theForm[fld + "_1"].focus();
    return (false);
  }
  
  if(part2.length != "2" || isNaN(part2)) {
    alert("Please enter a valid social security number.");
    theForm[fld + "_2"].focus();
    return (false);
  }
  
  if(part3.length != "4" || isNaN(part3)) {
    alert("Please enter a valid social security number.");
    theForm[fld + "_3"].focus();
    return (false);
  }
  
  return (true);
}







//check for valid e-mail address from text box (fld)
function check_valid_phone(fld, label) {
  phone_1 = theForm[fld + '_1'].value;
  phone_2 = theForm[fld + '_2'].value;
  phone_3 = theForm[fld + '_3'].value;
  
  if(isNaN(phone_1) || phone_1.length != 3) {
    alert("Please enter a valid phone number.");
    theForm[fld + '_1'].focus();
    return (false);
  }
  
  if(isNaN(phone_2) || phone_2.length != 3) {
    alert("Please enter a valid phone number.");
    theForm[fld + '_2'].focus();
    return (false);
  }
  
  if(isNaN(phone_3) || phone_3.length != 4) {
    alert("Please enter a valid phone number.");
    theForm[fld + '_3'].focus();
    return (false);
  }
  
  return (true);
}







//check for valid e-mail address from text box (fld)
function check_valid_email(fld, label) {
  var s;
  
  s = theForm[fld].value;
  
  if(s == "") {
    show_msg(fld, "The field e-mail address may not be left blank. Please enter a valid address to continue.");
    theForm[fld].focus();
    return (false);
  }
  else {
    aPos = s.indexOf('@');
          
    if(aPos > -1) {
      sleft = s.substring(0, aPos);
      sright = s.substring(aPos + 1);
      
      if(sleft.length < 1) {
        alert("The e-mail address entered does not appear to be valid. Please enter a valid address to continue.");
        theForm[fld].focus();
        return (false);
      }
            
      dpos = sright.indexOf(".");
      
      if(dpos != -1) {
        stld = sright.substring(dpos + 1);
        
        if(stld.length < 2) {
          alert("The e-mail address entered does not appear to be valid. Please enter a valid address to continue.");
          theForm[fld].focus();
          return (false);
          return (false);
        }
      }
      else {
        alert("The e-mail address entered does not appear to be valid. Please enter a valid address to continue.");
        theForm[fld].focus();
        return (false);
      }
              
      return (true);
            
    }
    else {
      alert("The e-mail address entered does not appear to be valid. Please enter a valid address to continue.");
      theForm[fld].focus();
      return (false);
    }
  }
}





function show_msg(fld, msg) {
  if(theForm[fld + "_msg"]) {
    if(theForm[fld + "_msg"].value != "") {
      alert(theForm[fld + "_msg"].value);
    }
    else {
      alert(msg);
    }
  }
  else {
    alert(msg);
  }
}

var validNums = '0123456789.';
var validInt = '0123456789';
var validLetters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var validTel = '0123456789()-.extEXT';






function validateKeyPress(e, validSet)
{
var key;
var keychar;

if(window.event || !e.which) // IE
	key = e.keyCode; // IE
else if(e)
	key = e.which; // Netscape
else
	return true; // no validation

keychar = String.fromCharCode(key);
validSet += String.fromCharCode(8);

if (key == 9)  //tab key
	return true;
else if (key == 13) //enter key
	return true;
else if (validSet.indexOf(keychar) < 0)
	return false;

return true;
}

