// Common field validators

//IsNotEmpty - Validates that the field value string has one or more characters in it.
function IsNotEmpty(elem)
{
	return !IsEmpty(elem);
}

//IsEmpty - Returns true if the field is empty
function IsEmpty(elem)
{
	var str = elem.value;
	return (str == null || str.length == 0);
}

//IsPositiveNumber - Validates that the entry is a positive number.
function IsPositiveWholeNumber(elem)
{
	var str = elem.value;
	var oneDecimal = false;
	var oneChar = 0;
	//make sure value hasn't been cast to a number data type
	str = str.toString();
	for(var i=0; i < str.length; i++)
	{
		oneChar=str.charAt(i).charCodeAt(0);
		//characters outside of 0 through 9 not O.K.
		if(oneChar < 48 || oneChar > 57)
		{
			return false;
		}
		
	}
	
	return true;
	
}

/*-------------------------------------------------------------------------------------
 -IsValidEmail - Validates that the entry is formatted as an email address
 --------------------------------------------------------------------------------------
  Looks for a match that begins (^) with one or more letters, numerals, underscores,
  or hyphens([\w-]), followed by zero or more combinations of a period, letter, numeral
  underscore, or hyphen ((\.[\w-]+)*), followed by the @ symbol, followed by one or more
  computer or domains (([\w-]+\.)+), followed by two to seven upper or lowercase letters 
  for the top-level domain name([a-zA-Z]{2,7}) on the tail end($).
 ---------------------------------------------------------------------------------------
 */
function IsValidEmail(elem)
{
	var str = elem.value;
	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	return str.match(re);
}

/*---------------------------------------------------------------------------------------
 -IsValidPolicyNumber - Validates that the entry is in an acceptable policy number format.
 ----------------------------------------------------------------------------------------
 Valid formats are (optional) ca or CA, followed by an optional space, nine digits with a 
 hypen and two digits or just nine digits.
 -----------------------------------------------------------------------------------------
 */
function IsValidPolicyNumber(elem)
{
	var str = elem.value;
	var re = /([cC][aA][\s]|[cC][aA])?([0-9]{9}[-][0-9]{2}|[0-9]{2})/;
	if(!str.match(re))
	{
		return false;
	}
	else
	{
		return true;
	}
}

/*---------------------------------------------------------------------------------------
 -IsValidDate - Validates that the entry is in an actual date.
 -monthString must be month abbreviation - e.g. "Mar" (see direct_policy_addvehicle.aspx)
 ----------------------------------------------------------------------------------------

 -----------------------------------------------------------------------------------------
 */
function IsValidDate(dayString,monthString,yearString)
{
	var myDateStr = yearString + '/' + monthString + '/' + dayString;

	/* Using form values, create a new date object
	which ends up looking like "Wed Jan 1 00:00:00 EST 1975". */
	var myDate = new Date(myDateStr);
	//alert(myDate);
	// Convert the date to a string so we can parse it.
	var myDate_string = myDate.toDateString();


	/* Split the string at every space and put the values into an array so,
	using the previous example, the first element in the array is "Wed", the
	second element is "Jan", the third element is "1", etc. */
	var myDate_array = myDate_string.split( ' ' );

	/* If we entered "Feb 31, 1975" in the form, the "new Date()" function
	converts the value to "Mar 3, 1975". Therefore, we compare the month
	in the array with the month we entered into the form. If they match,
	then the date is valid, otherwise, the date is NOT valid. */

	if (myDate_array[1] != monthString) 
	{
	return false;
	} 
	else 
	{
	return true;
	}
}

/*-------------------------------------------------------------------------------------------
 -numeralsOnly - Key Press event for text box entry.  
				 To use, add attribute to text boxes: onkeypress="return numeralsOnly(event)"
 --------------------------------------------------------------------------------------------

 --------------------------------------------------------------------------------------------
 */
function numeralsOnly(evt)
{
	//Equalize event models.
	evt = (evt)? evt:event;
	var charCode=(evt.charCode)?evt.charCode:((evt.keyCode)? evt.keyCode:((evt.which)? evt.which:0));
	
	//For characters out of "numeric" range, cancel bubble.
	if(charCode > 31 && (charCode < 48 || charCode > 57))
	{
		return false;
	}
	
	return true;
}

/*-------------------------------------------------------------------------------------------
 -CheckGroupList - Validate radio button has been selected"
 --------------------------------------------------------------------------------------------*/	
function CheckGroupList(ControlID)
{
	var SelectionChecked = false;
	var evalstring = ContPref + ControlID + ".length";
	var VehCount = eval(evalstring);

	for(j = 0;j < VehCount; j++)
	{
		evalstring = ContPref + ControlID + "[" + j + "].checked";

		SelectionChecked = eval(evalstring);
		if(SelectionChecked)
		{
			return SelectionChecked;
		}
	} 

	return SelectionChecked;
}

/*-------------------------------------------------------------------------------------------
 -IsValidAreaCode - Validate that the area code is valid
 --------------------------------------------------------------------------------------------*/	
function IsValidAreaCode(elem)
{
	var acode = elem.value;
	return (acode != null && acode.length == 3 && IsPositiveWholeNumber(elem));
}

/*-------------------------------------------------------------------------------------------
 -IsValidPhonePrefix - Validate that the phone prefix is valid
 --------------------------------------------------------------------------------------------*/	
function IsValidPhonePrefix(elem)
{
	var prefix = elem.value;
	return (prefix != null && prefix.length == 3 && IsPositiveWholeNumber(elem));
}

/*-------------------------------------------------------------------------------------------
 -IsValidPhonePrefix - Validate that the phone prefix is valid
 --------------------------------------------------------------------------------------------*/	
function IsValidPhoneSuffix(elem)
{
	var suffix = elem.value;
	return (suffix != null && suffix.length == 4 && IsPositiveWholeNumber(elem));
}

/*-------------------------------------------------------------------------------------
 -IsValidPhoneNumber - Validates that the entry is formatted as a valid phone number
 --------------------------------------------------------------------------------------*/
function IsValidPhoneNumber(elem)
{
	// Strip out acceptable non-numeric characters
	var stripped = elem.value.replace(/[\(\)\.\-\ ]/g, '');
	// Make sure only numbers are left
	if (isNaN(parseInt(stripped))) false;
	// Make sure there are 10 numbers
	return (stripped.length == 10);
}

/*-------------------------------------------------------------------------------------
 -showErrorMessage - Shows the standard Hagerty error dialog box
 --------------------------------------------------------------------------------------*/
function showErrorMessage(msg)
{
	msg = "__________________________________________________________\n\n"
		+ "The form was not submitted because of the following error(s).\n"
		+ "Please correct these error(s) and re-submit.\n"
		+ "\n"
		+ "Please call us at 800-922-4050 with any questions.\n"
		+ "__________________________________________________________\n\n"
		+ msg;
	alert(msg);								
}

/*-------------------------------------------------------------------------------------
 -showErrorMessages - Shows the standard Hagerty error dialog box
 -messages is an array or strings
 --------------------------------------------------------------------------------------*/
function showErrorMessages(messages)
{
	var msg;
	msg = "__________________________________________________________\n\n"
		+ "The form was not submitted because of the following error(s).\n"
		+ "Please correct these error(s) and re-submit.\n"
		+ "\n"
		+ "Please call us at 800-922-4050 with any questions.\n"
		+ "__________________________________________________________\n\n";
		
	for (i in messages)
	{
		msg += (messages[i] + "\n\n");
	}
	
	alert(msg);								
}