function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
//alert(dateStr + " Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
//alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
//alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
//alert("Month "+month+" doesn't have 31 days!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
//alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;
}

function chkNumeric(objName,addedValues)
{
	// Check field contains only numeric values
	// only allow 0-9 be entered, plus any values passed
	// (can be in any order, and don't have to be comma, period, or hyphen)
	// if all numbers allow commas, periods, hyphens or whatever,
	// just hard code it here and take out the passed parameters
	var checkOK = "0123456789" + addedValues;
	var checkStr = objName;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	var j = 0;
	var i = 0;
	for (i = 0;  i < checkStr.length;  i++)
	{
		//alert(checkStr.charAt(i);
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		{
			if (ch == checkOK.charAt(j))
			break;
		}
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
			if (ch != ",")
			allNum += ch;
	}
		return(allValid);
}	

function ValidateDateFields(FieldsToValidate, FieldsToValidateLabels)
// Validate all the date fields are filled correct before to submit
{
	var allValid = true;
	var j = 0;
	var firstError=null;
	myRequiredFields=new Array;
	var messageTxt=null 
	myRequiredFieldsLabels=new Array;
	myRequiredFieldsLabels = GetsRequiredFieldIndexes(FieldsToValidateLabels);
	myRequiredFields = GetsRequiredFieldIndexes(FieldsToValidate);
	for (j = 0;  j < myRequiredFields.length;  j++)
	{
		if (document.forms[0][myRequiredFields[j]].value.length > 0)	
		{
			if (!(isValidDate(document.forms[0][myRequiredFields[j]].value)))	
			{
				allValid=false;
				if (firstError==null) 
				{
					firstError=j;
					messageTxt = "The following invalid date field(s) were found on your form:\n\n" + myRequiredFieldsLabels[j]
				}
				else
					messageTxt = messageTxt + ", " + myRequiredFieldsLabels[j];
			}
		}
	}
	if (!(allValid))
	{
		document.forms[0][myRequiredFields[firstError]].focus();
		alert(messageTxt);
	}
	return allValid
}

function ValidateRequiredFields(FieldsToValidate, FieldsToValidateLabels)
// Validate all the required fields are filled before to submit
{
	var allValid = true;
	var j = 0;
	var firstError=null;
	myRequiredFields=new Array;
	var messageTxt=null 
	myRequiredFieldsLabels=new Array;
	myRequiredFieldsLabels = GetsRequiredFieldIndexes(FieldsToValidateLabels);
	myRequiredFields = GetsRequiredFieldIndexes(FieldsToValidate);

	for (j = 0;  j < myRequiredFields.length;  j++)
	{
		if (document.forms[0][myRequiredFields[j]].value.length <= 0)	
		{
			allValid=false;
			if (firstError==null) 
			{
				firstError=j;
				messageTxt = "The following required field(s) needs to be fill:\n\n" + myRequiredFieldsLabels[j]
			}
			else
				messageTxt = messageTxt + ", " + myRequiredFieldsLabels[j];
		}
	}
	if (!(allValid))
	{
		document.forms[0][myRequiredFields[firstError]].focus();
		alert(messageTxt);
	}
	return allValid
}

function ValidateNumericFields(FieldsToValidate, FieldsToValidateLabels)
// Validate all the numeric fields are filled correct before to submit
{
	var allValid = true;
	var j = 0;
	var firstError=null;
	var messageTxt=null 
	myRequiredFields=new Array;
	myRequiredFieldsLabels=new Array;
	myRequiredFieldsLabels = GetsRequiredFieldIndexes(FieldsToValidateLabels);
	myRequiredFields = GetsRequiredFieldIndexes(FieldsToValidate);
	for (j = 0;  j < myRequiredFields.length;  j++)
	{
		if (!(chkNumeric(document.forms[0][myRequiredFields[j]].value,".")))	
		{
			allValid=false;
			if (firstError==null) 
			{
				firstError=j;
				messageTxt = "The following invalid numeric field(s) were found on your form:\n\n" + myRequiredFieldsLabels[j]
			}
			else
				messageTxt = messageTxt + ", " + myRequiredFieldsLabels[j];
		}
	}
	if (!(allValid))
	{
		document.forms[0][myRequiredFields[firstError]].focus();
		alert(messageTxt);
	}
	return allValid
}



function GetsRequiredFieldIndexes(FieldsToValidate)
{
//Gets the index numbers for all the required fields and insert on a array
	myRequiredFields=new Array;
	var i=0;
	var count=0;
	var j=0;
	for (j = 0;  j < FieldsToValidate.length;  j++)
	{
		if (FieldsToValidate.charAt(j)==",")
		{
			myRequiredFields[count]=FieldsToValidate.substring(i,j);
			count++;
			i=j+1;
		}
	}
	if (FieldsToValidate.length > 0)
		myRequiredFields[count]=FieldsToValidate.substring(i,FieldsToValidate.length);
	return myRequiredFields;
}
