//---------------------------------------------------------------------------------
// This javascript function checks for a blank/empty form field.  If blank alert 
// the user.
//---------------------------------------------------------------------------------
function chkBlank(textObj) {
	if(textObj.value.length < 1)  {
	  	alert("Required field! Please enter information in the highlighted field.");
		 textObj.style.backgroundColor='#FFFF63';
		textObj.focus()
		return (true);
	}
	 textObj.style.backgroundColor='#FFFFFF';
	return (false);
 } 
//----------------------------------------------------------------------
// This function checks the form field ensuring only ALPHA (text) 
// characters are entered with the exception of the 'extraChars' that 
// are explicitly listed.
//----------------------------------------------------------------------
function isString(textObj, c) {
			var chars = c
			var newValue = textObj.value
			var newLength = newValue.length
			var extraChars = "'.-óñãáíúçéàõ" + chars   //pass chars to the var 'c' you wish not be checked
			var search
			for (var i = 0; i != newLength; i++) {
				aChar = newValue.substring(i,i+1)
				aChar = aChar.toUpperCase()
				search = extraChars.indexOf(aChar)
				if (search == -1 && (aChar < "A" || aChar > "Z"))
				  {
					alert("You've entered an invalid character in the highlighted field.");
					 textObj.style.backgroundColor='#FFFF63';
					 textObj.focus()
					 return false;
					break;
				  }
				}
			return (true);
 }
//------------------------------------------------------------------------------------------
// This Javascript function ensures the form field value are integers only.
// Anything else produces an error and requires the user to re-enter valid information.
//------------------------------------------------------------------------------------------
 function isInt(textObj) {
	var newValue = textObj.value
	var newLength = newValue.length
	for(var i=0; i != newLength; i++) {
	  aChar = newValue.substring(i,i+1)
	  if(aChar < "0" || aChar > "9") {
	    alert("You've entered an invalid character in the highlighted field.");
			textObj.style.backgroundColor='#FFFF63';
			textObj.focus()
			return false;
		 break;
		}
	 }
	 return true;
}
//-------------------------------------------------------------------------------------
// This javascript function checks for alpha-numeric entries and does not allow
// extraneous characters to be entered such as: %$#()&* in the field.  Alerts the user.
//-------------------------------------------------------------------------------------
function isAlphaNum(textObj) {
			var newValue = textObj.value
			var newLength = newValue.length
			var extraChars = "0123456789'.# "
			var search
			for (var i = 0; i != newLength; i++) {
				aChar = newValue.substring(i,i+1)
				aChar = aChar.toUpperCase()
				search = extraChars.indexOf(aChar)
				if (search == -1 && (aChar < "A" || aChar > "Z"))
				  {
					alert("You've entered an invalid character, please correct.");
					  textObj.style.backgroundColor='#FFFF63';
					  textObj.focus()
					 return false;
					break;
				  }
				}
			return (true);
 }
//----------------------------------------------------------------------------------------
// Basic email validation javascript function.  Checks for all normal entry errors.
//----------------------------------------------------------------------------------------
function chkEmail(textObj) {
	var emailAddr = textObj.value
	var newLength = textObj.value.length
if (newLength > 0) {
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test
	(emailAddr)){
			return (true)
			}
				alert("Invalid e-Mail address. Please correct or re-Enter.");
					textObj.style.backgroundColor='#FFFF63';
					textObj.focus()
	  return (false)
	}
	 textObj.style.backgroundColor='#FFFF63';
	return (true)		//blank field is ok, use chkBlank above for blank field validation
}
//-----------------------------------------------------------------------------------------
// Phone number javascript validation using regular expressions.
// this function check the entry for the following phone formats...
//   chk1 = (123)-456-7890 or (123) 456-7890
//   chk2 = 123-456-7890
//   chk3 = 1234567890
//-----------------------------------------------------------------------------------------
function chkPhone(textObj) {
	var chk1 = /^\({1}([0-9]){3}\){1}(\s|\-){1}([0-9]){3}\-{1}([0-9]){4}$/
	var chk2 = /^([0-9]){3}\-{1}([0-9]){3}\-{1}([0-9]){4}$/
	var chk3 = /^([0-9]){10}$/
	var pnum = textObj.value
if ((chk1.test(pnum)) || (chk2.test(pnum)) || (chk3.test(pnum)))
		{
			return (true)
			}
			else if (pnum.length < 1) {
				return (true)
			}
			else {
				alert("Invalid Phone number entry. Please correct or re-Enter.");
					textObj.style.backgroundColor='#FFFF63';
					textObj.focus()
				return (false)
				}
}
//--------------------------------------------------------------------------------
// this Javascript function composed of regular expressions verifies that the date 
// entered is valid.  Either a 2 or 4 digit year can be entered in M/D/Y format (USA)
//--------------------------------------------------------------------------------
function isValidDate(textObj, format) {
	     var dateStr = textObj.value
		 
if (dateStr.length > 0) {		//if not blank field then continue

   if (format == null) { format = "MDY"; }
   		format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ((format.indexOf("M") == -1) || (format.indexOf("D") == -1) || (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { 			// If the year is first
      var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
      var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
   } else if (format.substring(1, 2) == "Y") { 		// If the year is second
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
   } else { 										// The year must be third
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
   }
   
   // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
   if ((reg1.test(dateStr) == false) && (reg2.test(dateStr) == false)) {
   		alert("You've entered an invalid date in the highlighted field.");
				textObj.style.backgroundColor='#FFFF63';
				textObj.focus()
				return false;
    		}
   	var parts = dateStr.split(RegExp.$1); 		// Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else 
      if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else 
      if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else
      if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 30) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   	var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) {
   		alert("You've entered an invalid date in the highlighted field.");
				textObj.style.backgroundColor='#FFFF63';
				textObj.focus()
   			 return false;
		}
   if (parseFloat(mm)-1 != dt.getMonth()) {
   		alert("You've entered an invalid date in the highlighted field.");
			 textObj.style.backgroundColor='#FFFF63';
			 textObj.focus()
         return false;
		}
			textObj.style.backgroundColor='transparent';
   return true;
	}
  return true; //if blank just continue, not a required field.
}