/*
	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 chkFullName(val,msg)
{
	var re = /^[a-zA-Z\ \\]*$/;
	if (!val.value.match(re)) 
	{
		alert("Please enter a valid " + msg);
		val.focus();
		val.select();
		return false;
	}
	else
		return 1;

}


function IsEntered(field, name)
{	
	if (LTrim(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 valid " + name);
		field.focus();
		field.select();
		return false;
	}
	
	return true;
}


function verifyPhone_OLD(field, name)
{
	var re=/^\d{4}-\d{4}-\d{6}$/;
	if (LTrim(field.value) != "")
	{
		if (!field.value.match(re))
		{
			alert("Please Enter a valid " + name+"\n Valid Format is 123-456-7890");
			field.focus();
			field.select();
			return false;
		}
		else
			return true;
	}
	else
		return 	true;
}


function verifyPhone(field, name)
{
/* verify phone number having space . () - characters*/
var re = /^[0-9\-\.\ \(\)\\]*$/;
	
	if (LTrim(field.value) != "")
	{
		if (!field.value.match(re))
		{
			alert("Please Enter a valid " + name);
			field.focus();
			field.select();
			return false;
		}
		else
			return true;
	}
	else
		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;

}
function LTrim(str)
{
  var whitespace = new String(" \t\n\r");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(0)) != -1) 
  {
    // We have a string with leading blank(s)...
    var j=0, i = s.length;
    // Iterate from the far left of string until we
    // don't have any more whitespace...
    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
    j++;
    // Get the substring from the first non-whitespace
    // character to the end of the string...
    s = s.substring(j, i);
  }

  return s;
}

function CheckRadioButton(radio_form,msg)
{

// set var radio_choice to false
	var radio_choice = false;
// Loop from zero to the one minus the number of radio button selections
		for (counter = 0; counter < radio_form.length; counter++)
		{
		// If a radio button has been selected it will return true
		// (If not it will return false)
			if (radio_form[counter].checked)
				radio_choice = true; 
		}
		if (!radio_choice)
		{
			// If there were no selections made display an alert box 
			alert(msg);
			return (false);
		}
	return (true);
}
function checkCheckBox(checkboc,desc) 
{
	if (checkboc.checked == false)
	{
		alert (desc);
		return false;
	}
	else
	{
		return true;
	}
}
function check2CheckBox(checkbox,checkbox2,desc) 
{
	if (checkbox.checked == false && checkbox2.checked == false)
	{
		alert (desc);
		checkbox.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function CheckURL(field,msg) 
{

	  var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; 
	if(RegExp.test(field.value) == false)
	{
		alert(msg);	
		field.focus();
		return(false);
	}
	else
		return(true);
}


function chkOnlyChar(field,name)
{
	
	var val 	= field.value;	
	var re = /^[a-zA-Z\\]*$/;
	if (!val.match(re)) 
	{
		alert('Please enter valid '+name);
		field.focus();
		 return false;

	}
	else
		return 1;
}

function IsSelectEntered(fieldObject, fieldName)
{

 if ( (fieldObject.selectedIndex == -1) || (fieldObject[fieldObject.selectedIndex].value == "") || (fieldObject[fieldObject.selectedIndex].value == -1))
 {

   	alert("Please select a " +fieldName);

	fieldObject.focus();

   	return false;

 }
 else
 return true;
}

function IsSelectEmp(fieldObject, fieldName)
{

 if ( (fieldObject.selectedIndex == -1) || (fieldObject[fieldObject.selectedIndex].value == "") || (fieldObject[fieldObject.selectedIndex].value == -1))
 {

   	alert("Please select " +fieldName);

	fieldObject.focus();

   	return false;

 }
 else
 return true;
}


