/* 
 *  All of this code is © Copyright 2002-2003 Travis M. Buettner. All rights reserved.
 *  This code may not be used, distributed or copied for any reason without the express written permission from Travis M. Buettner.
 *
 *  File:  checkForm.js  
 *  Current Version Number: 
 * 		TMB Version 16.3, 9/20/2002
 * 			-checkDateMessage(form.dateRequired.value, "Required by date", form.dateRequired, false)
 * 			-isEmailMessage( form.supplierQuoteNumber, "Please enter the jeamil!", false ) 
 *			-accepts a flag of TRUE or FALSE that will make the field required or not required
 *			-this function is backwards compatible and will work as a REQUIRED field if the flag is not passed
 *
 *	Previous Version Updates:
 * 		TMB Version 16.2, 9/17/2002
 * 			-isWholeNumberMessage( form.lifeInsOptionalPercentDead2, "Please enter the percent for the !", true )
 * 			-isNumber2DecimalMessage( form.lifeInsOptionalPercentDead2, "Please enter the percent for the !", false )
 *			-accepts a flag of TRUE or FALSE that will make the field required or not required
 *			-this function is backwards compatible and will work as a REQUIRED field if the flag is not passed
 * 		TMB Version 16.1, 9/9/2002
 * 			-checkDateMessage(data, message, dateFocus)
 *			-small addition that checks to make sure that no one can enter in a zero for the day, month or year
 *
 *		TMB Version 16.0, 6/14/2002
 * 			-checkTextareaMessage( form.objective, "Please file in your objective!", 1000, true ) 
 *			-this function uses checkText model and add a count function that can be set by the 3rd parameter for the maximum length
 *				this is mainly used for textarea boxes but can be used on other form elements like text
 *
 * 		TMB Version 15.5, 6/5/2002
 *			-checkDuplicateFieldsEmailMessage(form.email, form.email2, "Your email and your re typed email must match exactly!", false) &&
 *			-this function is much like the check password function only it 
 *				is for email address and their is a flag true/false if it is not requird
 *
 *		TMB Version 15, 5/10/02
 *			-completely rescripted the date validator, will accept mm/dd/yyyy, m/dd/yyyy, mm/d/yyyy, mm/d/yyyy
 *			-check for valid months, days in a particular month, does not check for leap year, feb is 29days for now
 *
 *		TMB Version 14, 3/11/02
 *			-added function isWholeNumberMessage, this will only allow users to enter in a whole number
 *
 *			-added function isNumber2DecimalMessage, this will only allow users to enter in a whole number or a number with 2 decimal places
 *
 *		TMB Version 13.5, 2/20/02
 *			-added validation for a web address, makes sure a web address starts with "http://" or is equal to nothing
 *
 *		TMB Version 13.1, 2/7/02
 *			-change the alert box output for the password validation to say "password" in the message
 *
 *		TMB Version 13, 1/25/02
 *			-added password validation, you must enter a password that is atleast 6 characters in length,
 *				must contain atleast one number and atleast one letter
 *
 *		TMB Version 12, 1/18/02
 *			-added a onfocus for the date function along with a third field just for date validation purposes
 *
 *  	TMB Version 11, 1/10/02
 *  		-added a validator for 3 or mor enumbers entered in (checkThreeMessage)
 *
 *  Automatic form checking (for ver 4.0 browsers and above)
 *
 *  Usage:
 *
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/checkForm.js">
 *    </script>
 *    ...
 *    <form ... onsubmit="return checkForm( this )">
 *
 *  Insert the <script> tags in the <head> or <body> of your document.
 *  Insert the <form> tag in the <body> of your document.
 *
 *  Example:  checkFormTest.html
 *
 *  The following input elements are recognized by checkForm:
 *
 *                     Checked?
 *    Element          Yes   No   Result
 *    ------------------------------------------------------------------
 *    button                 X    NA
 *    checkbox          X         Warning if none in a group are checked
 *    file              X         Error if input field is empty
 *    hidden                 X    NA
 *    password          X         Error if input field is empty
 *    radio             X         Error if none in a group are checked
 *    reset                  X    NA
 *    select-one        X         Warning if no option is selected
 *    select-multiple   X         Warning if no options are selected
 *    submit                 X    NA
 *    text              X         Error if input field is empty
 *    textarea          X         Error if input field is empty
 *
 *  Note:  A select-multiple element is a select element with the
 *         MULTIPLE attribute, whereas a select-one element has no 
 *         MULTIPLE attribute.
 *
 *  For custom form checking, simply override the checkForm() function.
 *  For example, to check a subset of the input elements, try this:
 *
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/checkForm.js">
 *    </script>
 *    <script type="text/javascript">
 *      // Overrides checkForm() (skips over some input elements):
 *      function checkForm( form ) {
 *        return checkText( form.mailTo ) &&
 *               checkText( form.mailFrom ) &&
 *               checkText( form.mailSubject ) &&
 *               checkSelectOne( form.group ) &&
 *               checkTextarea( form.message );
 *      }
 *    </script>
 *    ...
 *    <form ... onsubmit="return checkForm( this )">
 * 
 *  where mailTo, mailFrom, mailSubject, group, and message are the 
 *  names of specific input elements that require checking (all other
 *  input elements are ignored).
 *
 *  Here is a complete list of the functions called by checkForm():
 *
 *    Element           Function
 *    ------------------------------------------------------------------
 *    text              checkText
 *    textarea          checkTextarea
 *    radio             checkRadio
 *    checkbox          checkCheckbox
 *    select-one        checkSelectOne
 *    select-multiple   checkSelectMultiple
 *    file              checkFile
 *    password          checkPassword
 *
 *  These boolean functions may be used separately (as above) or may be
 *  overridden for further customization.  For example, here's how to 
 *  use a more restrictive form of the checkSelectMultiple() function:
 *
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/checkForm.js">
 *    </script>
 *    <script type="text/javascript">
 *      // checkSelectStrict returns true if an option is selected:
 *      var checkSelectMultiple = checkSelectStrict;
 *    </script>
 *    ...
 *    <form ... onsubmit="return checkForm( this )">
 *    
 *  Finally, here is an example that handles select elements with 
 *  attribute SIZE="1" (which is the default size in HTML):
 *
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/checkForm.js">
 *    </script>
 *    <script type="text/javascript">
 *      // checkSelectSpecial returns true if the user makes a 
 *      // selection other than options[0]:
 *      var checkSelectOne = checkSelectSpecial;
 *    </script>
 *    ...
 *    <form ... onsubmit="return checkForm( this )">
 *    
 *  The checkSelectSpecial() function returns true if the user 
 *  makes a selection other than options[0].
 *
 */

// Returns true if all form elements pass their respective test:
function checkForm( formObj ) {

  // Local variables:
  var element, type, name, oldName;
  
  // Check all input elements:
  var n = formObj.elements.length;
  for ( var i = 0; i < n; i++ ) {
    element = formObj.elements[i];
    type = element.type;
    // Ignore buttons and hidden fields:
    if ( type == "text" ) {
      if ( !checkText( element ) ) return false;
    } else if ( type == "textarea" ) {
      if ( !checkTextarea( element ) ) return false;
    } else if ( type == "radio" || type == "checkbox" ) {
      name = element.name;
      if ( name != oldName ) {
        // Obtain the corresponding group element:
        element = eval( "formObj." + name );
        if ( type == "radio" && !checkRadio( element ) ) return false;
        if ( type == "checkbox" && !checkCheckbox( element ) ) return false;
        oldName = name;
      }
    } else if ( type == "select-one" ) {
      if ( !checkSelectOne( element ) ) return false;
    } else if ( type == "select-multiple" ) {
      if ( !checkSelectMultiple( element ) ) return false;
    } else if ( type == "file" ) {
      if ( !checkFile( element ) ) return false;
    } else if ( type == "password" ) {
      if ( !checkPassword( element ) ) return false;
    }
  }
  
  // All tests were successful:
  return true;
  
}

// Default function assignments:
var checkText = checkTextGeneric;
var checkTextarea = checkTextGeneric;
var checkFile = checkTextGeneric;
var checkPassword = checkTextGeneric;
var checkSelectOne = checkSelectGeneric;
var checkSelectMultiple = checkSelectGeneric;


//will check a text box to make sure that you enter a "http://" at the beginning of the text box
function checkWebsiteMessage( address, message ) 
{
  if ( isEmpty( address ) ) 
  { 
  	return true;
  }
  else
  {	
  	var regexp = "http://";
  	string = "\b" + address.value;
	
	//alert(address.value);
	//alert(string.search(regexp));
	if(string.search(regexp) == -1 )
	{
		window.alert( message );
    	address.value = "";
    	address.focus();
		return false;
	}
	else
	{
		 return true;
	}
  }
}

// Returns true if a generic text element is not empty:
function checkTextareaMessage( textObj, message, maxLength, requiredFlag ) 
{
	if ( (requiredFlag == false) && (textObj.value == "") )
		{ return true; }
	else
	{
		 if ( isEmpty( textObj ) ) 
		 {
			window.alert( message );
			textObj.value = "";
			textObj.focus();
			return false;
	 	 }
	  
	 	 textareaLength = textObj.value
	  
	  	if ( textareaLength.length > maxLength ) 
		{
			window.alert( "You have entered " +  textareaLength.length + " characters and the maximum characters you can enter is " + maxLength + " characters!" );
			textObj.focus();
			return false;
  		}
	}
  
  return true;
}

// Returns true if a generic text element is not empty:
function checkTextMessage( textObj, message ) {
  if ( isEmpty( textObj ) ) {
    window.alert( message );
    textObj.value = "";
    textObj.focus();
    return false;
  }
  return true;
}

function checkDuplicateFieldsEmailMessage(emailObj1, emailObj2, message, requiredFlag)
{
	
	if ( (requiredFlag == false) && (emailObj1.value == "") && (emailObj2.value == "") )
	{ return true; }
	else
	{
		var regexp =/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
		if(regexp.test(emailObj1.value) == false) 
		{
			window.alert( "You must enter a valid email address!" );
			emailObj1.focus();
			return false;
		}
		
		if(regexp.test(emailObj2.value) == false) 
		{
			window.alert( "You must enter a valid email address for your re typed email address!" );
			emailObj2.focus();
			return false;
		}
		
		if( (emailObj1.value) == (emailObj2.value) )
			return true;
		else
		{
			alert( message );
			emailObj1.value = ""
			emailObj2.value = ""
			emailObj1.focus();
			return false;
		}
	}
}

function checkPasswordFieldsMessage(passwordObj1, passwordObj2, message)
{
	if( passwordObj1.value.length < 6 )
	{
		alert("Your password must be atleast 6 characters in length!");
		passwordObj1.focus();
		return false;
	}
	
	if( passwordObj2.value.length < 6 )
	{
		alert("Your retyped password must be atleast 6 characters in length!");
		passwordObj2.focus();
		return false;
	}
	
	var regexp =/[a-zA-Z]/;
    if(regexp.test(passwordObj1.value) == false) 
	{
		window.alert( "You must enter at least one letter for your password!" );
		passwordObj1.focus();
		return false;
	}
	
	if(regexp.test(passwordObj2.value) == false) 
	{
		window.alert( "You must enter at least one letter for your password!" );
		passwordObj2.focus();
		return false;
	}
	
	var regexp2 =/[0-9]/;
    if(regexp2.test(passwordObj1.value) == false) 
	{
		window.alert( "You must enter at least one number for your password!" );
		passwordObj1.focus();
		return false;
	}
	
	if(regexp2.test(passwordObj2.value) == false) 
	{
		window.alert( "You must enter at least one number for your password!" );
		passwordObj2.focus();
		return false;
	}
	
	if( (passwordObj1.value) == (passwordObj2.value) )
			return true;
	else
	{
		alert( message );
		passwordObj1.value = ""
		passwordObj2.value = ""
		passwordObj1.focus();
		return false;
	}
}

function isEmailMessage(emailObj, message, requiredFlag ) 
{
	var regexp =/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	
	if ( (requiredFlag == false) && (emailObj.value == "") )
		{ return true; }
	else
	{
		if(regexp.test(emailObj.value) == false) 
		{
			window.alert( message );
			emailObj.focus();
			return false;
		}
		else
				return true;
	}
  	return true;
}

// A function to check select elements with attribute SIZE="1".
// Returns true if the user makes a selection other than options[0]:
function checkSelectSpecialMessage( selectObj, message ) {
  if ( selectObj.selectedIndex == 0 ) {
    window.alert( message );
    selectObj.focus();
    return false;
  }
  return true;
}

// Returns true if a generic text element is not empty:
function checkTextGeneric( textObj ) {
  if ( isEmpty( textObj ) ) {
    window.alert( "Enter " + textObj.name + "!" );
    textObj.value = "";
    textObj.focus();
    return false;
  }
  return true;
}

// Returns true if an option of a generic select element is selected 
// or if the user does not wish to select any options:
function checkSelectGeneric( selectObj ) {
  if ( selectObj.selectedIndex == -1 ) {
    var name = selectObj.name;
    if ( window.confirm( "Do you want to select " + name + "?" ) ) {
      selectObj.focus();
      return false;
    }
  }
  return true;
}

// Returns true if a radio button is checked:
function checkRadio( radioObj ) {
  var n = radioObj.length;
  for ( var i = 0; i < n; i++ ) {
    if ( radioObj[i].checked ) return true;
  }
  window.alert( "Please check " + radioObj[0].name + "!" );
  radioObj[0].focus();
  return false;
}

// Returns true if at least one of a group of checkboxes is checked
// or if the user does not wish to check any checkboxes:
function checkCheckbox( checkboxObj ) {
  var n = checkboxObj.length;
  for ( var i = 0; i < n; i++ ) {
    if ( checkboxObj[i].checked ) return true;
  }
  var name = checkboxObj[0].name;
  if ( window.confirm( "Do you want to check " + name + "?" ) ) {
    checkboxObj[0].focus();
    return false;
  }
  return true;
}

// Returns true if at least one of a group of checkboxes is checked
// or if the user does not wish to check any checkboxes:
function checkCheckboxMessage( checkboxObj, message ) {
  var n = checkboxObj.length;
  for ( var i = 0; i < n; i++ ) {
    if ( checkboxObj[i].checked ) return true;
  }
  var name = checkboxObj[0].name;

  	window.alert( message ) ;
    checkboxObj[0].focus();
    return false;
}

// Returns true if an option is selected:
function checkSelectStrict( selectObj ) {
  if ( selectObj.selectedIndex == -1 ) {
    window.alert( "Please select " + selectObj.name + "!" );
    selectObj.focus();
    return false;
  }
  return true;
}

// A function to check select elements with attribute SIZE="1".
// Returns true if the user makes a selection other than options[0]:
function checkSelectSpecial( selectObj ) {
  if ( selectObj.selectedIndex == 0 ) {
    window.alert( "Please select " + selectObj.name + "!" );
    selectObj.focus();
    return false;
  }
  return true;
}

// Returns true if the value of the text element is null or whitespace:
function isEmpty( textObj ) {
  var regexp = /^\s*$/;
  return regexp.test( textObj.value );
}

// For backwards compatibility with a previous version of this script:
// (Do not use!  Will be removed in a future version!)
var checkSelect = checkSelectSpecial;


function checkDateMessage(data, message, dateFocus, requiredFlag )
{ 
	error = 0;
	var validFormat = false;
	if ( (requiredFlag == false) && (dateFocus.value == "") )
		{ return true; }
	else
	{
		//---------------------------------------------------------------------------------------
		// mm/dd/yyyy format
		if( (data.charAt(2)=="/" && data.charAt(5)=="/" && data.length==10) )
		{
			if( isNaN(data.charAt(0))||isNaN(data.charAt(1))||isNaN(data.charAt(3))||isNaN(data.charAt(4))||isNaN(data.charAt(6))||isNaN(data.charAt(7))||isNaN(data.charAt(8))||isNaN(data.charAt(9)) )
			{ 
				error = 1; 
				validFormat = false;
			}
			else
			{ 
				validFormat = true; 
				day = eval(data.charAt(3)+data.charAt(4));
				day = parseInt(day);
		
				mnth = eval(data.charAt(0)+data.charAt(1));
				mnth = parseInt(mnth);
		
				yr = eval(data.charAt(6)+data.charAt(7)+data.charAt(8)+data.charAt(9));
				yr = parseInt(yr);
			}
			//alert("mm/dd/yyyy format");
		}
		//--------------------------------------------------------------------------------------------
		// m/dd/yyyy format
		else if( (data.charAt(1)=="/" && data.charAt(4)=="/" && data.length==9) )
		{
			if( isNaN(data.charAt(0))||isNaN(data.charAt(2))||isNaN(data.charAt(3))||isNaN(data.charAt(5))||isNaN(data.charAt(6))||isNaN(data.charAt(7))||isNaN(data.charAt(8)) )
			{ 
				error = 1; 
				validFormat = false;
			}
			else
			{ 
				validFormat = true; 
				day = eval(data.charAt(2)+data.charAt(3));
				day = parseInt(day);
		
				mnth = eval(data.charAt(0));
				mnth = parseInt(mnth);
		
				yr = eval(data.charAt(5)+data.charAt(6)+data.charAt(7)+data.charAt(8));
				yr = parseInt(yr);
			}
			//alert("m/dd/yyyy format");
		}
		//---------------------------------------------------------------------------------------------------
		// mm/d/yyyy format
		else if( (data.charAt(2)=="/" && data.charAt(4)=="/" && data.length==9) )
		{
			if( isNaN(data.charAt(0))||isNaN(data.charAt(1))||isNaN(data.charAt(3))||isNaN(data.charAt(5))||isNaN(data.charAt(6))||isNaN(data.charAt(7))||isNaN(data.charAt(8)) )
			{ 
				error = 1; 
				validFormat = false;
			}
			else
			{ 
				validFormat = true; 
				day = eval(data.charAt(3));
				day = parseInt(day);
		
				mnth = eval(data.charAt(0)+data.charAt(1));
				mnth = parseInt(mnth);
		
				yr = eval(data.charAt(5)+data.charAt(6)+data.charAt(7)+data.charAt(8));
				yr = parseInt(yr);
			}
			//alert("mm/d/yyyy format");
		}
		//-----------------------------------------------------------------------------------------------------
		// m/d/yyyy format
		else if( (data.charAt(1)=="/" && data.charAt(3)=="/" && data.length==8) )
		{
			if( isNaN(data.charAt(0))||isNaN(data.charAt(2))||isNaN(data.charAt(4))||isNaN(data.charAt(5))||isNaN(data.charAt(6))||isNaN(data.charAt(7)) )
			{ 
				error = 1; 
				validFormat = false;
			}
			else
			{ 
				validFormat = true; 
				day = eval(data.charAt(2));
				day = parseInt(day);
		
				mnth = eval(data.charAt(0));
				mnth = parseInt(mnth);
		
				yr = eval(data.charAt(4)+data.charAt(5)+data.charAt(6)+data.charAt(7));
				yr = parseInt(yr);
			}
			//alert("m/d/yyyy format");
		}
		// none of the above are true
		else 
		{ validFormat = false; }
		
		if(validFormat == false)
		{ error = 1; }
		
		if(validFormat == true)
		{	
			 // mnthArray[0] is january, mnthArray[11] is december
			mnthArray = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
			mydate = new Date(yr,mnth,day);
	
			monthArray =  mnth - 1;
			if( parseInt(day) > parseInt(mnthArray[parseInt(monthArray)]) )
			{ error = 3; }
			
			if( parseInt(day) == parseInt(0) )
			{ error = 3; }
			
			if( parseInt(yr) == parseInt(0) )
			{ error = 4; }		
				
			if(mnth>12 || mnth<1)
			{ error = 2; }
		}
		
		//alert("error is " + error);
		
		switch(error)
		{
		case 1: alert( message + " must be in mm/dd/yyyy format");
				dateFocus.focus();
				return false;
				break;
		case 2: alert("The month you entered in " + message + " is an invalid month.");
				dateFocus.focus();
				return false;
				break;
		case 3: alert("The day you entered in " + message + " is an invalid day.");
				dateFocus.focus();
				return false;
				break;
		case 4: alert("The year you entered in " + message + " is an invalid year.");
				dateFocus.focus();
				return false;
				break;
		case 5: return true;
				break;
			default: return true;
		}
	}
  	return true;
}
		   
function isCurrency(currency) 
{   
    var regexp = /^([$]?\d*|[$]?\d*[.]\d{2})$/;
		  
    if(regexp.test(currency) == false || currency=="$" || currency=="") 
    {
		alert( "Please check the Currency Field!" );
		return false;
    }
    else
    {
		return true;
    }
}

function isCurrencyMessage(currencyObj, message) 
{   
    var regexp = /^([$]?\d*|[$]?\d*[.]\d{2})$/;
		  
    if(regexp.test(currencyObj) == false || currencyObj=="$" || currencyObj=="") 
    {
		alert( message );
		currencyObj.focus();
		return false;
    }
    else
    {
		return true;
    }
}
		                    		   
function isZip(zip)
{
	var regexp = /^\d{5}$/;
	if(regexp.test(zip.value) == false)
	{
		window.alert( "Please check zip field!" );
		zip.value.focus();
		return false;
	}
	
	else
		return true;
}

function checkThreeMessage(field, message)
{ //checks if there is 3 numbers
	var regexp = /^(\d{3,})|(\w{3,})$/;
	if(regexp.test(field.value) == false)
	{
		window.alert( message );
		field.focus();
		return false;
	}
	
	else
		return true;
}
		   
function isPhone(form)
{
	if(form.phone.value == "")
	{
		return true;
	}
	else
	{
		var regexp = /^((\(\d{3}\)|d{3})[- ]?)?\d{3}[- ]?\d{4}$/;
		if(regexp.test(form.phone.value) == false)
		{
			window.alert( "Please check phone field!" );
			return false;
		}
		else
			return true;
	}
} 

// Returns true if a radio button is checked:
function checkRadioMessage( radioObj, message ) {
  var n = radioObj.length;
  for ( var i = 0; i < n; i++ ) {
    if ( radioObj[i].checked ) return true;
  }
  window.alert( message );
  radioObj[0].focus();
  return false;
}

// Returns true if the value of the text element is a number:
function isWholeNumberMessage( textObj, message, requiredFlag ) {
  var regexp = /^\d*$/;
  
  	if ( (requiredFlag == false) && (textObj.value == "") )
		{ return true; }
	else
	{
		  if ( isEmpty( textObj ) ) {
			window.alert( message );
			textObj.value = "";
			textObj.focus();
			return false;
		  }
		  
		  if(regexp.test( textObj.value ) == false)
			{
				window.alert( message );
				textObj.value = "";
				textObj.focus();
				return false;
			}
			
			else
				return true;
	}
  	return true;
}

// Returns true if the value of the text element is a number:
function isNumber2DecimalMessage( textObj, message, requiredFlag ) {
  var regexp = /^(\d*|\d*.[0-9]{2}|\d*.[0-9]{1})$/;

  	if ( (requiredFlag == false) && (textObj.value == "") )
		{ return true; }
	else
	{
	  if ( isEmpty( textObj ) ) {
		window.alert( message );
		textObj.value = "";
		textObj.focus();
		return false;
	  }
	  
	  if(regexp.test( textObj.value ) == false)
		{
			window.alert( message );
			textObj.value = "";
			textObj.focus();
			return false;
		}
		
		else
			return true;
	}
  	return true;
}  