// 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(day, month, year)
{
    // Returns true(1) if it is a valid date in gregorian calendar  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/checkdate
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Pyerre
    // *     example 1: checkdate(12, 31, 2000);
    // *     returns 1: true
    // *     example 2: checkdate(2, 29, 2001);
    // *     returns 2: false
    // *     example 3: checkdate(03, 31, 2008);
    // *     returns 3: true
    // *     example 4: checkdate(1, 390, 2000);
    // *     returns 4: false
    var myDate = new Date();
    myDate.setFullYear( year, (month - 1), day );

    return month >= 1 && month <= 12 && year >= 1 && year <= 32767 && ((myDate.getMonth()+1) == month && day<32);
}

/*-------------------------------------------------------------------------------------------
 -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);								
}