//date function: valid -------------------------------------------------------------------------
function checkDateValid(theMonth, theDay, theYear, theFieldName)
{
  	var myDayStr = theDay.value;
	var myMonthStr = theMonth.value;
	var myYearStr = theYear.value;
	var myDateStr = myDayStr + ' ' + myMonthStr + ' ' + myYearStr;

/* Using form values, create a new date object
which looks like "Wed Jan 1 00:00:00 EST 1975". */
	var myDate = new Date( myDateStr );

// Convert the date to a string so we can parse it.
	var myDate_string = myDate.toGMTString();

/* Split the string at every space and put the values into an array so,
using the previous example, the first element in the array is "Wed", the
second element is "Jan", the third element is "1", etc. */
	var myDate_array = myDate_string.split( ' ' );

/* If we entered "Feb 31, 1975" in the form, the "new Date()" function
converts the value to "Mar 3, 1975". Therefore, we compare the month
in the array with the month we entered into the form. If they match,
then the date is valid, otherwise, the date is NOT valid. */
	if ( myDate_array[2] != myMonthStr )
	{
		alert("The " + theFieldName + " is invalid.  Please enter a valid " + theFieldName + ".");
		theMonth.focus();
		return (false);
	}
	else
	{
		return (true);
	}
}
//----------------------------------------------------------------------------------------------

//date function: past date ---------------------------------------------------------------------
function checkDatePast(theMonth, theDay, theYear, theFieldName)
{
	//build todays date
	var today = new Date()
	var day = today.getDate()
	var month = today.getMonth() + 1
	var year = today.getFullYear()

   var months = new Array(13);
   months[0]  = "Jan";
   months[1]  = "Feb";
   months[2]  = "Mar";
   months[3]  = "Apr";
   months[4]  = "May";
   months[5]  = "Jun";
   months[6]  = "Jul";
   months[7]  = "Aug";
   months[8]  = "Sep";
   months[9]  = "Oct";
   months[10] = "Nov";
   months[11] = "Dec";
	
	var todayDateStr = day + ' ' + months[month - 1] + ' ' + year
	var todayDate = new Date( todayDateStr )

	//build the date from the form
  	var myDayStr = theDay.value;
	var myMonthStr = theMonth.value;
	var myYearStr = theYear.value;
	var myDateStr = myDayStr + ' ' + myMonthStr + ' ' + myYearStr;
	var myDate = new Date( myDateStr );
	
	if (myDate < todayDate)
	{
		alert("The " + theFieldName + " cannot be prior to today's date.");
		theMonth.focus();
		return (false);
	}
	else
	{
		return (true);
	}
}
//----------------------------------------------------------------------------------------------

//date function: compare dates -----------------------------------------------------------------
function checkDateCompare(theMonth1, theDay1, theYear1, theFieldName1, theMonth2, theDay2, theYear2, theFieldName2)
{
	//build the first date from the form
  var myDayStr1 = theDay1.value;
	var myMonthStr1 = theMonth1.value;
	var myYearStr1 = theYear1.value;
	var myDateStr1 = myDayStr1 + ' ' + myMonthStr1 + ' ' + myYearStr1;
	var myDate1 = new Date( myDateStr1 );

	//build the second date from the form
  	var myDayStr2 = theDay2.value;
	var myMonthStr2 = theMonth2.value;
	var myYearStr2 = theYear2.value;
	var myDateStr2 = myDayStr2 + ' ' + myMonthStr2 + ' ' + myYearStr2;
	var myDate2 = new Date( myDateStr2 );
	
	if (myDate1 > myDate2)
	{
		alert("The " + theFieldName2 + " cannot be prior to the " + theFieldName1 + ".");
		theMonth1.focus();
		return (false);
	}
	else
	{
		return (true);
	}
}
//----------------------------------------------------------------------------------------------

//field length function ------------------------------------------------------------------------
function checkLength(theField,fieldName,minLen,maxLen)
{
	if (theField.value.length > maxLen)
	{
		alert("The maximum number of characters for '" + fieldName + "' is " + maxLen + ".");
		theField.focus();
		return (false);
	}
	else if (theField.value.length < minLen)
	{
		alert("The minimum number of characters for '" + fieldName + "' is " + minLen + ".");
		theField.focus();
		return (false);
	}
	else
	{
		return (true);
	}
}
//----------------------------------------------------------------------------------------------

//left/right functions -------------------------------------------------------------------------
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
//----------------------------------------------------------------------------------------------

//check file extensions ------------------------------------------------------------------------
function CheckFileExt(theFieldName, theFileType, theMessageText)
{
	strFileExt = theFieldName.value
	lenFileExt = strFileExt.length
	if (theFileType == "Img")
	{
		if ((strFileExt.substring(lenFileExt-4,lenFileExt) != ".gif") && (strFileExt.substring(lenFileExt-4,lenFileExt) != ".jpg"))
		{
			alert("Please upload a Valid Image File for '" + theMessageText + "'." + "\n" + "Valid file types are: .gif and .jpg");
			theFieldName.focus();
			return (false);
		}
	}
	if (theFileType == "Doc")
	{
		if ((strFileExt.substring(lenFileExt-4,lenFileExt) != ".pdf") && (strFileExt.substring(lenFileExt-4,lenFileExt) != ".txt") && (strFileExt.substring(lenFileExt-4,lenFileExt) != ".doc") && (strFileExt.substring(lenFileExt-4,lenFileExt) != ".xls"))
		{
			alert("Please upload a Valid Image File for '" + theMessageText + "'." + "\n" + "Valid file types are: .doc, .pdf, .txt, and .xls");
			theFieldName.focus();
			return (false);
		}
	}
}
//----------------------------------------------------------------------------------------------