/*
	checks if an array of fields has been entered or not
	the array is of the format
		0 -> field object
		1 -> field name
		2 -> field object
		3 -> field name
		.
		.
		i -> field object
		i + 1 -> field name
*/
function CheckFieldsEntered(fieldList)
{
	var result = true;
	for(i = 0; i < fieldList.length && result; i += 2)	
	{
		field = fieldList[i];
		if (field.type == "text" || field.type == "textarea")
			result = IsEntered(field, fieldList[i+1]);
		else
			result = IsSelectEntered(field, fieldList[i+1]);
	}
	return result;
}

function IsEntered(field, name)
{	
	if (field.value == "")
	{	
		alert("Please Enter " + name );
		field.focus();
		return false;
	}
	
	return true;
}

function IsValidEmail(fieldObject,fieldName)
{
	var value;
	value = fieldObject.value;
	var wrongTestRE = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(^\s)/;
	var correctTestRE = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	if (!wrongTestRE.test(value) && correctTestRE.test(value)) 
	{
		return true;
	}

	alert('You have not entered a valid email address');

	fieldObject.focus();
	fieldObject.select();

	return false;
}

function IsFieldComplete(field,fieldName)
{
	if(field.value.length < 4 )
	{
		alert(fieldName + " Must be 4 or more characters long");
		field.focus();
		return false;
	}
return true;
}


function IsSelectEntered(fieldObject, fieldName){
 if ( (fieldObject.selectedIndex == -1) || (fieldObject[fieldObject.selectedIndex].value == "") || (fieldObject[fieldObject.selectedIndex].value == 0)){
   	alert("Please Select a  " + fieldName);
   	
	fieldObject.focus();
   	return false;
 }
	
 return true;
}


function IsValidMonth( month ){
	return ( (month > 0 ) && ( month <= 12 ));
}

function IsValidDay( day, month, year){
	var totalDays;
	
	if ( month == 2 )
		totalDays =  28 + ( ( year % 4 == 0 ) && ( ( year % 100 != 0 ) || ( year % 400 == 0 ) ) );
	else
		totalDays = 30 + ( ( month % 2 == 0 ) ^ ( month <= 7 ) );
	
	//alert(day + month + year + totalDays);
	if ( ( day > 0 ) && ( day <= totalDays ) )
		return true;
	
	
	return false;
}

function CheckMonthYearIsValid(fieldObject, fieldName){
	var value;
	value = fieldObject.value;
	
	// check if the field has been mm/dd/yy or mm/yy
	var mmddRE = /(\d{1,2})[\/-](\d{2}|\d{4})$/;
	if ( mmddRE.test( value ) ){
		var dummy;
		dummy = mmddRE.exec(value);

		var month;
		var year;
		
		month = dummy[1];
		year = dummy[2];

		if ( isValidMonth( month )) 
			return true;
	}
	
	alert("Date entered in " + fieldName + " is not valid!");
	fieldObject.focus();
	fieldObject.select();
	return false;
}

function CheckFullDateIsValid(fieldObject, fieldName){
	var value;
	value = fieldObject.value;
	
	// check if the field has been mm/dd/yy or mm/yy
	var mmddyyRE = /(\d{1,2})[\/-](\d{1,2})[\/-](\d{4})$/;
	if ( mmddyyRE.test( value ) ){
		var dummy;
		dummy = mmddyyRE.exec(value);

		var month;
		var year;
		var day;
		
		month = dummy[1];
		day = dummy[2];
		year = dummy[3];

		if ( (isValidMonth( month )) && (isValidDay( day, month, year ) ) )
			return true;
	}

	alert("Date entered in " + fieldName + " is not valid!");
	fieldObject.focus();
	fieldObject.select();
	return false;
}

function IsIntegerField(field, name)
{
	if ( isNaN(field.value) )
	{
		alert("Please Enter a " + name);
		field.focus();
		field.select();
		return false;
	}
	
	return true;
}

function FieldMatch(src, target, desc)
{
	if (src.value != target.value)
	{
		alert("Values entered in " + desc + " do not match");
		src.focus();
		return false;
	}
		
	return true;
}

function chk_US_TelFax(val,desc)
{
	var re=/^\d{3}-\d{3}-\d{4}$/;
	if (!val.match(re)) 
	{
		alert("Please Enter Valid"+ desc);
		return false;
	}
	else
		return true;

}
