<!--
//////////////////////////////////////////////////////////////////////////////
//**************************************************************************//
//* FILE: validation.js													   *//
//* AUTHOR: Michael Brogdon												   *//
//* EMAIL: michael.brogdon@colorado.edu 								   *//
//* DATE: 05/17/2005													   *//
//* MODIFIED ...														   *//
//* 		ON: 05/24/2005 CHANGED: Found a couple of bugs in code		   *//
//*																		   *//
//**************************************************************************//
//////////////////////////////////////////////////////////////////////////////
function validate(field,value,type)
//////////////////////////////////////////////////////////////////////////////
//	function validate(field,value,type)										//
// 	Precondition:  field is a valid input or textarea html object, value is	//
//				   the value for the html object field, and type is one of 	//
//				   the following:											//
//						--> basic											//
//						--> email											//
//						--> phone											//
// 						--> date											//
//						--> checked											//
//						--> digit											//
//																			//
//	Postcondition: function will return true if the value meets the type	//
// 				   requirements and false otherwise							//
////////////////////////////////////////////////////////////////////////////// 
{
	// For a better format, if the name is dilimited with a _ then split it
	var field_str = field.name.split("_");
	var field_str_cat = "";
	// Form the better formatted string
	for(var i = 0; i < field_str.length; i++)
		field_str_cat += " " + field_str[i];
		
	// If type is equal to basic then will test for no characters at all and
	// also for the case where the input is nothing but white space, if either
	// of these two cases or true then function will return false
	if(type == "basic")
	{
		// If the value is blank return false
		if(value.length < 1)
		{
			alert(field_str_cat + " cannot be blank.");
			field.focus();
			field.select();
			return false;
		}
		// Else test for white space
		else
		{
			// Split input with respect to each character in the input
			var characters = value.split("");
			// Variable to keep up with how many of the characters is white space
			var space_count = 0;
			// Loop through each character
			for(var k = 0; k < characters.length; k++)
				// If it is a space then increment space_count
				if(characters[k] == " ")
					space_count++;
			// If our space_count is the same as the length of our input
			// then return false
			if(space_count==characters.length)
			{
				alert(field_str_cat + " cannot be blank.");
				field.focus();
				field.select();
				return false;
			}
			return true;
		}
	}
	// If type is equal to email then will return false if call to Email_is_Valid(value) is 
	// not true. 
	else if(type == "email")
	{
		if(!Email_is_Valid(value))
		{
			alert("Email must be valid.");
			field.focus();
			field.select();
			return false;
		}
		else
			return true;
	}
	// If type is equal to phone then will return false if phone_is_valid(value) is false
	else if(type == "phone")
	{
		if(!phone_is_valid(value))
		{
			alert("A valid phone number must be entered (e.g. 303-492-0735)");
			field.focus();
			field.select();
			return false;
		}
		else
			return true;
	}
	// If type is equal to date (in this case not directly testing for date because
	// it can take on the format of required,date:# so we are checking for the word date)
	else if(type.indexOf("date") > -1)
	{
		// Split the type with respect to :
		var minimum = type.split(":");
		// min_spec is used to test whether or not there was a minimum amount of days or not
		var min_spec = true;
		if(minimum.length<=1)
			min_spec = false;
		else
			minimum = minimum[1];
		// Split our date value with respect to -
		var date_fields = value.split("-");
		// If it was not formatted with a -
		if(date_fields.length <= 1)
		// Then split with respect to /
			date_fields = value.split("/");
		
		// Date variables
		var current_date = new Date();					// The full system date
		var current_year = current_date.getFullYear(); 	// System year
		var current_month = current_date.getMonth();	// System month of the year
		var current_day = current_date.getDate();		// System day of the month
		var one_day = 1000*60*60*24;					// one day in seconds and scaled by 1k
		var is_four_digit = true;
		var err_msg = "";
		
		err_msg += "A valid date must be entered, some examples are:\n";
		err_msg += "\t" + "05/05/05\n";
		err_msg += "\t" + "05/05/2005\n";
		err_msg += "\t" + "5/5/05\n";
		err_msg += "\t" + "5/5/2005\n";
		err_msg += "\n";
		err_msg += "\t" + "05-05-05\n";
		err_msg += "\t" + "05-05-2005\n";
		err_msg += "\t" + "5-5-05\n";
		err_msg += "\t" + "5-5-2005";
		
		if(!isNaN(parseInt(date_fields[2])) && date_fields[2].length == 2)
			is_four_digit = false;
		else if (!isNaN(parseInt(date_fields[2])) && date_fields[2].length == 3)
		{
			alert(err_msg);
			return false;
		}
		
		// If our date_fields does not have three elements then return false
		if(date_fields.length != 3)
		{
			alert(err_msg);
			field.focus();
			field.select();
			return false;
		}
		// If first element (MM) is not of length 2, second element (DD) is not of length 2,
		// or third element (YYYY) is not of length 4 then return false.
		else if((date_fields[0].length < 1 || date_fields[0].length > 2) 
				|| (date_fields[1].length < 1 || date_fields[1].length > 2) 
				|| (date_fields[2].length < 2 || date_fields[2].length > 4))
		{
			alert(err_msg);
			field.focus();
			field.select();
			return false;
		}
		// If the month is not a valid month, 1-12 then return false
		else if(date_fields[0] < 1 || date_fields[0] > 12)
		{
			alert(err_msg);
			field.focus();
			field.select();
			return false;
		}
		// If the day specified is not in the valid ranges for days of the month 1-31
		// return false
		//else if(parseInt(date_fields[1]) < 01 || parseInt(date_fields[1]) > 31)
		else if(date_fields[1] < 01 || date_fields[1] > 31)
		{
			alert(err_msg);
			field.focus();
			field.select();
			return false;
		}
		// If the specified year is four digit format and less than this year return false
		else if(is_four_digit && (parseInt(date_fields[2]) < current_year))
		{
			alert(err_msg);
			field.focus();
			field.select();
			return false;
		}
		// If the specified year two digit format and is less than the current year return false
		else if(!is_four_digit)
		{
			var two_digit_year;
			two_digit_year = (current_year.toString()).substr(2,2);
			if(date_fields[2] < two_digit_year)
			{
				alert(err_msg);
				field.focus();
				field.select();
				return false;
			}
		}
		// If there was a minimum amount of days between todays date and specified date 
		// then test for length difference in days
		else if(min_spec)
		{
			// Turn date value (of text format) into date format
			if(date_fields.length == 3)
				var entered_date = new Date(date_fields[2],date_fields[0]-1,date_fields[1]);
			// Get the difference in the dates in seconds and divide by one day to 
			// see if it is less than the minimum, if so return false
			if(Math.ceil((entered_date-current_date.getTime())/(one_day)) < minimum)
			{
				alert("Date must be more than " + minimum + " days from todays date.");
				field.focus();
				field.select();
				return false;		
			}
		}	
		return true;	
	}
	// Since checkboxes take on the format of "required,checked:bool" check
	// to see if type contains the word checked, if so then process
	else if(type.indexOf("checked") > -1)
	{
		// Set default checked to true
		var checked = true;
		// Split our type
		var true_false = type.split(":");
		// If true or false was specified then check what checked should be
		if(true_false.length > 1)
			// If true_false[1] = true then set checked to 1, else 0
			checked = (true_false[1]=="true") ? 1:0;
		// Set field_checked equal to 1 if the field is checked, else 0
		var field_checked = (field.checked==true) ? 1:0;
		// If the field_checked and checked or not equal return false
		if(field_checked != checked)
		{
			alert("You must check all required checkboxes");
			field.focus();
			field.select();
			return false;
		}
		return true;	
	}
	// Digit takes on format "required,digit:#", so test for the word digit in typ
	// if present process
	else if(type.indexOf("digit")>-1)
	{
		// Split our type with respect to :
		var digit = type.split(":");
		// Variable for required length of digit field
		var dig_len;
		// If specified length then set dig_len to that specification
		if(digit.length > 1)
			dig_len = digit[1];
		// Otherwise set it to a default of 4
		else
			dig_len = 4;
		// A string of valid characters (0-9)
		var valid_chars = "0123456789";
		
		// Split the value with respect to each character
		var numbers = value.split("");
		// Loop through array of characters and make sure they are a part of the valid_chars
		for(var i = 0; i < numbers.length; i++)
		{
			// If the character is not a 0-9 return false
			if(valid_chars.indexOf(numbers[i])<=-1)
			{
				alert(field_str_cat + " must be valid characters (digits 0-9)");
				field.focus();
				field.select();
				return false;
			}
		}
		// If the length of the digit input is less the the required length return false
		if(value.length < dig_len)
		{
			alert(field_str_cat + " must be of valid length (" + dig_len + ").");
			field.focus();
			field.select();
			return false;
		}
		return true;
	}
	else	
		return true;
}

function phone_is_valid(phone_number)
//////////////////////////////////////////////////////////////////////////////
// function phone_is_valid(phone_number)									//
// Precondition:  phone_number is inputted									//
// Postcondition: If phone_number is in a valid format of ###-###-#### then //
//				  function will return true, and false otherwise			//
//////////////////////////////////////////////////////////////////////////////
{
	// Split phone_number with respect to -
	var digits = phone_number.split("-");
	// Default to is_valid
	var is_valid = true;
	// String of valid characters
	var valid_chars = "0123456789";
	var sub_dig;
	// If the lenght of our digits array is not 3 then return false
	if(digits.length != 3)
	{
		is_valid = false;
		return is_valid;
	}
	// If first element (###) is not of length 3, second element (###) is not of length 3,
	// or third element (####) is not of length 4 return false
	if(digits[0].length != 3 || digits[1].length != 3 || digits[2].length != 4)
	{
		is_valid = false;
		return is_valid;
	}
	// Step through all three elements of the digits array
	for(var i = 0; i < digits.length; i++)
	{
		// Set sub_dig to the present digits element
		sub_dig = digits[i];
		// Loop through all the characters in the element
		for(var j = 0; j < sub_dig.length; j++)
		{
			// If the character happens to be something other than
			// a number return false
			if(valid_chars.indexOf(sub_dig.charAt(j)) <= -1)
			{
				is_valid = false;
				return is_valid;
			}
		}
	}
	return is_valid;
}

function Email_is_Valid(email_address)
//////////////////////////////////////////////////////////////////////////////
// function Email_is_Valid(email_address)									//
// Precondition:  email_address is not null									//
// Postcondition: Will return true if email_address as the format of the 	//
//				  following:												//
//					--> *****@****.****										//
//					--> *****@***.***										//
//					--> *****@***.**										//
//				  and will return false otherwise							//
//																			//
//																			//
// NOTE: Function does not query a registry, just checks for valid			//
//   characters in the email address, checks for an @ and makes sure that	//
//   the extension is either 2, 3, or 4 characters long (.**,.***,.****)	//
//																			//
//////////////////////////////////////////////////////////////////////////////
{
	var strArray;
	var strItem;
	var i;
	var j;
	var c;
	var blnIsItValid;
  
	// Assume the email address is correct
	blnIsItValid = true;

	// Split the email address in two parts: name@domain.ext
	strArray = email_address.split("@");

	// Get size of array after split.
	var array_size = strArray.length;

	// If the size of the array is one then invalid email
	if (array_size == 1)
	{
		blnIsItValid = false;
		return blnIsItValid;
	}


  	// First part cannot be void.
	if(strArray[0].length==0)
	{
		blnIsItValid = false;
		return blnIsItValid;
	}
	
	// Get number of characters in first element of strArray
	var elements = strArray[0].length;
	// Create an array with each character in its own element.
	var elements_a = strArray[0].split("");
	//Check to make sure that all characters in the first part
	//is a valid entry.
	for(i=0;i<elements;i++)
	{
		//Convert array to all lowercase.
		c = elements_a[i].toLowerCase();
		//Create string of valid characters.
		var chars="0123456789abcdefghijklmnopqrstuvwxyz_-.";
		//Search for matches.
		var occur = chars.match(c);
		//If array occur is smaller than 1 element then false.
		if(occur==null || occur.length < 1)
		{
			blnIsItValid = false;
			return blnIsItValid;
		}

	}

	//Check that first and last element of the array or not . (dots).
	if (strArray[0][0]=="." || strArray[0][(strArray[0].length - 1)]==".")
	{
		blnIsItValid = false;
		return blnIsItValid;
	}
    
    //Check to make sure second part of email (domain.ext) has a . (dot)
    occur = strArray[1].split(".");
    //If there was a dot then length will be less than 1
    if(occur.length<2)
    {
    	blnIsItValid=false;
    	return blnIsItValid;
    }
    // Check that extension is a valid one.
    var last_element = (occur.length - 1);
	if(occur[last_element].length != 2 && occur[last_element].length != 3 && occur[last_element].length != 4)
	{
		blnIsItValid=false;
	    return blnIsItValid;
	}
	return blnIsItValid;
}
//-->