// DEFINE VARIABLES
// whitespace characters
var whitespace = " \t\n\r";

function trim(val)
{
	return val.replace(/^\s\s*/, '').replace(/\s\s*$/, '');

}

// PURPOSE:  Check to see if the string passed in is a valid time.
//	A valid time is defined as a string which is postfixed with either
//  "PM" or "AM".  Next it checks to see if there is a colon in the
//  string.  If there is, it makes sure that at least one digit preceeds
//  it and two proceed it.
function IsTime(strTime)
{
	var strTestTime = new String(strTime);
	strTestTime.toUpperCase();

	var bolTime = false;

	if (strTestTime.indexOf("PM",1) != -1 || strTestTime.indexOf("AM",1))
		bolTime = true;

	if (bolTime && strTestTime.indexOf(":",0) == 0)
		bolTime = false;

	var nColonPlace = strTestTime.indexOf(":",1);
	if (bolTime && ((parseInt(nColonPlace) + 5) < (strTestTime.length - 1) || (parseInt(nColonPlace) + 4) > (strTestTime.length - 1)))
		bolTime = false;


	return bolTime;
}

function Validate_String(string, return_invalid_chars)
 {
 valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 invalid_chars = '';
 
 if(string == null || string == '')
	return(true);
 
 //For every character on the string.   
 for(index = 0; index < string.length; index++)
	{
	char = string.substr(index, 1);                        
	
	//Is it a valid character?
	if(valid_chars.indexOf(char) == -1)
	  {
	  //If not, is it already on the list of invalid characters?
	  if(invalid_chars.indexOf(char) == -1)
		{
		//If it's not, add it.
		if(invalid_chars == '')
		   invalid_chars += char;
		else
		   invalid_chars += ', ' + char;
		}
	  }
	}                     
	
 //If the string does not contain invalid characters, the function will return true.
 //If it does, it will either return false or a list of the invalid characters used
 //in the string, depending on the value of the second parameter.
 if(return_invalid_chars == true && invalid_chars != '')
   {
   last_comma = invalid_chars.lastIndexOf(',');
   
   if(last_comma != -1)
	  invalid_chars = invalid_chars.substr(0, $last_comma) + 
	  ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
			  
   return(invalid_chars);
   }
 else
   return(invalid_chars == ''); 
 }


function Validate_Email_Address(email_address)
{
	//Assumes that valid email addresses consist of user_name@domain.tld
	at = email_address.indexOf('@');
	dot = email_address.lastIndexOf('.');
	//dot = email_address.indexOf('.');

	if(at == -1 || 
	dot == -1 || 
	dot <= at + 1 ||
	dot == 0 || 
	dot == email_address.length - 1)
	return(false);

	user_name = email_address.substr(0, at);
	domain_name = email_address.substr(at + 1, email_address.length);                  

	if(Validate_String(user_name) === false || 
	Validate_String(domain_name) === false)
	return(false);                     

	return(true);
}


/****************************************************************/

// Displays an alert box with the passed in string...

function PromptErrorMsg(Field,strError)
{
	alert("You have entered an invalid date for " + strError + ".  Please make sure your date format is in MM/DD/YYYY format.");
	//Field.focus();
}

/****************************************************************/

/* PURPOSE: Checks to see if the string is a valid date.  A valid
	date is defined as any of the following:

		MM/DD/YY, MM/DD/YYYY, M/D/YY, M/D/YYYY,
		MM-DD-YY, MM-DD-YYYY, M-D-YY, M-D-YYYY
*/

function ForceDate(strDate,strField,showMessage)
{
	var str = strDate;
	var validDate = true;

	if (isWhitespace(str)) 
	{
		return false;
		// if the field is empty, just return true...
	}

	var i = 0, count = str.length, j = 0;
	while ((str.charAt(i) != "/" && str.charAt(i) != "-") && i < count)
		i++;

	if (i == count || i > 2 && validDate != false) 
	{
		if(showMessage)
		{
			PromptErrorMsg(strDate,strField);
		}
		validDate = false;
	}

	var addOne = false;
	if (i == 2) addOne = true;

	if (!isDateNumber(str.substring(0,i),1) && validDate != false) 
	{
		if(showMessage)
		{
			PromptErrorMsg(strDate,strField);
		}
		validDate = false;
	}

	if(validDate != false)
	{
		j = i+1;
		i = 0;

		while ((str.charAt(i+j) != "/" && str.charAt(j+i) != "-") && i+j < count)
			i++;

		if (i+j == count || i > 2) 
		{
			if(showMessage)
			{
				PromptErrorMsg(strDate,strField);
			}
			validDate = false;
		}

		if (!isDateNumber(str.substring(j,i+j),2)) 
		{
			if(showMessage)
			{
				PromptErrorMsg(strDate,strField);
			}
			validDate = false;
		}

		j = i+3;
		i = 0;

		if (addOne) j++;

		while (i+j < count)
			i++;


		if (i != 2 && i != 4) 
		{
			if(showMessage)
			{
				PromptErrorMsg(strDate,strField);
			}
			validDate = false;
		}

		if (!isDateNumber(str.substring(j,i+j),3)) 
		{
			if(showMessage)
			{
				PromptErrorMsg(strDate,strField);
			}
			validDate = false;
		}
	}
	
	return validDate;
}


// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)
{   var i;
    // Is s empty?
    if (isEmpty(s)) 
	{
		return true;
	}

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
	// Check that current character isn't whitespace.
	var c = s.charAt(i);

	if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// Check whether string s is empty.
function isEmpty(s)
{
   return ((s == null) || (s.length == 0))
}

/* PURPOSE:  Returns true if the string is a valid date number.
	A method is passed in (1 = month, 2 = day).  If the string is
	nonnumeric, false is passed back.  If the day in the date string
	is greater than 31, false is returned.  If the month is greater
	than 12, an error is returned.
*/
function isDateNumber(strNum,method)
{
	var str = new String(strNum);
	var i = 0;

	if (isNaN(parseInt(str)) || parseInt(str) < 0) return false;

	if (method == 2)
		if (parseInt(str) > 31)
			return false;
	if (method == 1)
		if (parseInt(str) > 12)
			return false;

	for (i = 0; i < str.length; i++)
		if (str.charAt(i) < '0' || str.charAt(i) > '9')
			return false;


	return true;
}

function valButton(btn) {
    var cnt = -1;
	var buttonChecked = false;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) 
		{
			buttonChecked = true;
			break;
		}
    }
    //if (cnt > -1) return btn[cnt].value;
    //else return null;
	return buttonChecked;
}

function check_phone_number(strPhone) {

  /* Set whether the user should use a -, a space, or one long number without divisions.
     Use the following values to set:
	 1 = Use - (i.e 123-456-7890)
	 2 = Use a space (i.e. 123 456 7890)
	 3 = Use none (i.e. 1234567890)
  */	 
  var num_division = 1;
  
  if (strPhone != "") {	
    
	var phone_num_OK = false; 
	var the_delim = "";
	var the_ph_test = "";
	var ph_err_msg ="";
    var the_phone_num = strPhone;
	
	if (num_division == 1) {
		the_delim = "-";
		the_ph_test = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;
	   	phone_num_OK = the_ph_test.test(the_phone_num);
	}
	else if (num_division == 2) {
		the_delim = " ";
		the_ph_test = /^[0-9]{3} [0-9]{3} [0-9]{4}$/;
	    phone_num_OK = the_ph_test.test(the_phone_num);
	}
	else if (num_division == 3) {
		the_delim = "";
		the_ph_test = /^[0-9]{10}$/;
	    phone_num_OK = the_ph_test.test(the_phone_num);
	}
	else {
		window.alert("Cannot validate.");
		return false;	
	}
	
    if (phone_num_OK) {
	  return true;	
	}
	// else {
	    // ph_err_msg="An ivalid phone number was entered. Valid format is:\n123"+the_delim+"456"+the_delim+"7890";
	    // window.alert(ph_err_msg);
	    // return false;
	// }
	
  } 
}

