var html = "";
var confirm_text = "";
var elementList = "";

function validateText(name, field_label, req, minchars, maxchars)
{
   var success = true;
   // if (! name ) {
   //     alert ("Field " + field_label + " does not exist");
   //     return false;
   // };
   // if (! name.value ) {
   //     alert ("Field " + field_label + " does not exist");
   //     return false;
   // };
   if (name.value.length == 0 && req == "T")
   {
		success = false;
		alert("Please enter a response in the required field titled:\n" + "\"" + field_label + "\"");
		name.focus();
   }
   if (success && name.value.length > 0)
   {
	   if (success && name.value.length < minchars)
	   {
			success = false;
			alert("The response entered in the field titled: \n" + "\"" + field_label + "\"\n" + "must contain at least " + minchars + " characters.  Please correct your entry and re-submit.");
			name.focus();
	   }
	   if (success && name.value.length > maxchars)
	   {
			success = false;
			alert("The response entered in the field titled: \n" + "\"" + field_label + "\"\n" + "exceeded the allowed maximum of " + maxchars + " characters.  Please correct your entry and re-submit.");
			name.focus();
	   } 
   }
   return success;
}

function validateNumeric(name, field_label, req, negative, decimal, minnum, maxnum)
{
   var success = true;
   var allDigit = /^\-?([0-9]*)?(\.[0-9])?[0-9]?$/
   var withDecimal = /^\-?([0-9]*)?(\.)([0-9])?[0-9]?$/
   if (name.value.length == 0 && req == "T")
   {
	 	success = false;
		alert("Please enter a response in the required field titled:\n" + "\"" + field_label + "\"");
		name.focus();
   }
   if (success && name.value.length > 0)
   {
	   if (success && name.value.match(allDigit) == null)
	   {
	   		success = false;
			alert("The response entered in the field titled: \n" + "\"" + field_label + "\"\n" + "must be a numeric value.");
			name.focus();
		}	
	   if (success && name.value < minnum)
	   {
			success = false;
			alert("The response entered in the field titled: \n" + "\"" + field_label + "\"\n" + "is less than the minimum allowed value of " + minnum + ". Please correct your entry and re-submit.");
			name.focus();
	   } 
	   if (success && name.value > maxnum)
	   {
			success = false;
			alert("The response entered in the field titled: \n" + "\"" + field_label + "\"\n" + "is greater than the maximum allowed value of " + maxnum + ". Please correct your entry and re-submit.");
			name.focus();
	   } 
	   if (success && name.value < 0 && negative == "F")
	   {
		 	success = false;
			alert("The response entered in the field titled: \n" + "\"" + field_label + "\"\n" + "must be a positive value. Please correct your entry and re-submit.");
			name.focus();
	   }
	   if (success && decimal == "F")
	   {
		 	if (name.value.match(withDecimal) == null) 
			{
				success = true;
			}
			else 
			{
				success = false;
				alert("The response entered in the field titled: \n" + "\"" + field_label + "\"\n" + "must be an integer value. Please correct your entry and re-submit.");
				name.focus();
				
			}
	   }
   } 
   return success;
}

function validateDate(name, field_label, req, mindate, maxdate)
{
   var success = true;
   if (name.value.length == 0 && req == "T")
   {
		success = false;
		alert("Please enter a response in the required field titled:\n" + "\"" + field_label + "\"");
		name.focus();
   }
   if (success && name.value.length > 0)
   {
		   	var fullCalValue = /^[0-9][0-9\/]+[0-9][0-9\/]+[0-9][0-9][0-9][0-9]$/
				var partCalValue = /^[0-9\/]+[0-9\/]+[0-9][0-9][0-9][0-9]$/
				if ((name.value.match(fullCalValue) == null) && (name.value.match(partCalValue) == null))
				{ 
					success = false;
					alert("The date entered in the field titled:\n" +  "\"" + field_label + "\"\n" + "is not in the format of mm/dd/yyyy.  Please correct your entry and re-submit.");
					name.focus();
				}
				var w = new Date(name.value);
	      var x = new Date(mindate);
				var y = new Date(maxdate);
				var z = parseInt((w.valueOf() - x.valueOf())/(24*60*60*1000));
				if (success && z < 0)
				{
					success = false;
					alert("The earliest valid date for the field titled:\n" + field_label + "\"\n" + "is " + mindate + ". Please correct your entry and re-submit.");
					name.focus();
				}
			  var z = parseInt((y.valueOf() - w.valueOf())/(24*60*60*1000));
			  if (success && z < 0)
				{
					success = false;
					alert("The latest valid date for the field titled:\n" + field_label + "\"\n" + "is " + maxdate + ". Please correct your entry and re-submit.");
					name.focus();
				}
   }
   return success;
}

function validateDropDown(name, field_label, req)
{
   var success = true;
   if (name.value.length == 0 && req == "T")
   {
		success = false;
		alert("Please enter a response in the required field titled:\n" + "\"" + field_label + "\"");
		name.focus();
   }
   return success;
}

function validateRadio(name, field_label, req)
{
   var success = true;
   if (req == "T")
   {
	   for (x = 0; x < name.length; x++)
	   { 
			success = false;
			if (name[x].checked == true)
			{ 
				success = true;
				break;
			} 
	   } 
	   if (success == false)
	   {
	   		alert("Please enter a response in the required field titled:\n" + "\"" + field_label + "\"");
	   }
   }
   return success;
}


//AR starts 05292006
//Function to validate email element.
//AR starts 09282006
//function validateEmail(name, field_label)
function validateEmail(name, field_label, req)
{//AR ends 09282006
	var success = true;
	var at="@";
	var dot=".";
	var valid=/^[A-Za-z0-9]+$/
	var str=name.value;
	var indexOfAt=str.indexOf(at);//Atleast one '@' character is present.
	var strLength=str.length;
	var indexOfDot=str.indexOf(dot, indexOfAt+1);//Atleast one '.' character is present after the '@' character.
	
	//AR starts 09282006
	if (strLength == 0 && req == "T")
	{
		alert("Please enter an email address in the field titled:\n" + "\"" + field_label + "\"");
		name.focus();
		success = false;
	}
	else if(strLength > 0)
	{
	//AR ends 09282006
		if(indexOfAt==-1 || indexOfDot==-1)
		{
			//Either '@' or a '.' following the '@' doesnot exist in the email string.
			alert("Please enter a valid email address in the field titled:\n" + "\"" + field_label + "\"");
			name.focus();
			success = false;
		}
		else if(strLength==(indexOfDot+1))
		{
			//'.' is the last character of the email string. So no letter follows it.
			alert("Please enter a valid email address in the field titled:\n" + "\"" + field_label + "\"");
			name.focus();
			success = false;
		}
		else if((str.charAt(indexOfDot+1)).match(valid) == null)
		{
			//'.' is not followed by a letter or number in the email string.
			alert("Please enter a valid email address in the field titled:\n" + "\"" + field_label + "\"");
			name.focus();
			success = false;
		}
		else if((str.charAt(indexOfAt+1)).match(valid) == null)
		{
			//'@' is not followed by a letter or number in the email string.
			alert("Please enter a valid email address in the field titled:\n" + "\"" + field_label + "\"");
			name.focus();
			success = false;
		}
	//AR starts 09282006
	}
     	//AR ends 09282006
	return success;
}
//AR ends 05292006

function findRadioButtonValue(name)
{
   var returnValue = "";
   for (x = 0; x < name.length; x++)
   { 
		if (name[x].checked == true)
		{ 
			returnValue = name[x].value;
			break;
		} 
   } 
   return returnValue;
}
