
	function Trim(string)
	{
		var i = string.indexOf(" ");
		var l = string.length
		//alert("Start: value='"+string+"' i="+i+" l="+l);
		while ((i > -1) && (i < 1))
		{
			//alert("Front: value='"+string+"' i="+i+" l="+l);
			string = string.substring(i+1, l);
			i = string.indexOf(" ");
			l = string.length
		}
		i = string.lastIndexOf(" ");
		l = string.length
		//alert("End: value='"+string+"' i="+i+" l="+l);
		while ((i > -1) && (i > l-1))
		{
			//alert("Back: value='"+string+"' i="+i+" l="+l);
			string = string.substring(0, i-1);
			i = string.lastIndexOf(" ");
			l = string.length;
		}
		//var i = string.indexOf(" ");
		//var l = string.length
		//alert("Final: value='"+string+"' i="+i+" l="+l);
		
		return string
	}
	
	function IsValidDate(date)
	{
		 if (date.length!= 10 )
		 {
		 	return false; 
		 }
		 
		 Year	= date.substring(6,10);
		 Month = date.substring(0,2);
		 Day = date.substring(3,5);

		 if ((Year <= "1900") || (Year >= "2050"))
		 {
		     return false;
		 }

		 mmddyyDate	=	Month + "/" + Day + "/" + Year;

		 return isDate(mmddyyDate);
	}

	function isDate(field)
	{
		//Trim(field);
		var inDate=field;

		// Parse the date string to create a new Date object.
		var chkDate=new Date(Date.parse(inDate));
		if (chkDate == null)
		{
			return false;
		}
	
		// Check the gross format of the date; it must be one of the
		// following formats:
		//		mm/dd/yyyy
		
		var slash1 = inDate.indexOf("/");
		var slash2 = inDate.lastIndexOf("/");
		if ((slash1 == 0 ) || (slash1 == slash2))
		{
			// There is only one delimiter in the string.
			//return showAlertFocus(field, "The date must contain two slashes.");
			return(false);
		}
		
		var mm;
		var dd;
		var yearString;
		var ccyy;
		var yy;
		var cc;
		// Tear apart the date into components for month, day, and year.
		// Use base 10 conversions in the parseInt() functions, because
		// the components may begin with leading zeroes.
		if (slash1 != -1)
		{

			
			mm			= inDate.substring(0,slash1);
			dd			= inDate.substring(slash1+1,slash2);
			yearString	= inDate.substring(slash2+1,inDate.length);
			ccyy		= inDate.substring(slash2+1,inDate.length);
			cc			= inDate.substring(slash2+1,slash2+3);
			yy			= inDate.substring(slash2+3,inDate.length);

		}
		
		// Check if any of the date components are not a number (NaN).
		if (isNaN(mm))
		{
			//return showAlertFocus(field, "The date must contain a numeric month value (01 - 12).");
			return(false);
		}
		if (isNaN(dd))
		{
			//return showAlertFocus(field, "The date must contain a numeric day value (01 - 31).");
			return(false);
		}

		if (ccyy== "0000")
		{
			
			//return showAlertFocus(field, "The date must contain a numeric four-digit year value.");
			return(false);
		}
		

		if (isNaN(ccyy))
		{
			
			//return showAlertFocus(field, "The date must contain a numeric four-digit year value.");
			return(false);
		}
		if (ccyy < 0)
			{
			//return showAlertFocus(field, "The date must contain a valid four-digit year value.");
			return(false);
			}
			
		// Check that a four-digit year has been entered.

		if (yearString.length != 4)
		{
			//return showAlertFocus(field, "The date must contain a four-digit year value.");
			return(false);
		}
		
		// Check if the month is valid.
		if (mm < 1 || mm > 12)
		{
			//return showAlertFocus(field, "The date must contain a month value in the range 01 to 12.");
			return(false);
		}
		
		// Check if the day of the month is valid.
		if (dd < 1 || dd > 31)
		{
			//return showAlertFocus(field, "The date must contain a day value in the range 01 to 31.");
			return(false);
		}
		else
		{
			if (((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11)) && (dd > 30))
			{
				//return showAlertFocus(field, "The months April, June, September, and November contain only 30 days.");
				return(false);
			}
			else
			{
				if (mm == 2)
				{
					if (dd > 29)
					{
						//return showAlertFocus(field, "The month February can contain at most 29 days.");
						return(false);
					}
					else
					{
						// For years like 1800, 1900, 2000, etc. to be leap
						// years, the century must be divisible by 400, i.e.,
						// the years 1600 and 2000 are leap years, but the
						// years 1700, 1800, and 1900 are not.
						if ((yy == 0) && (cc % 4 > 0) && (dd > 28))
						{
							//return showAlertFocus(field, "The month February can contain only 28 days in a non-leap year.");
							return(false);
						}
						else
						{
							// Other years must be divisible by 4 to be leap
							// years.
							if ((yy % 4 > 0) && (dd > 28))
							{
								//return showAlertFocus(field, "The month February can contain only 28 days in a non-leap year.");
								return(false);
							}
						}
					}
				}
			}
		}
		return true;
	}

	function DateValidation(FieldName)
	{
		var today = new Date()
		var todayYear = today.getFullYear()
		var todayMonth = today.getMonth()
		var todayDay = today.getDate()
		var fieldValue = FormattedDate(Trim(document.getElementById(FieldName).value))

		if ((fieldValue == null) || (fieldValue.length == 0)) 
		{
		    return true;
		}

		document.getElementById(FieldName).value = fieldValue;

		if (!IsValidDate(fieldValue))
		{
            alert("Invalid date...please format as mm/dd/ccyy");
            document.getElementById(FieldName).focus();
            return false;
        }
	}
    
    function FormattedDate(InputDate)
    {
        var strDate = InputDate
        var slash1 = InputDate.indexOf("/");
        var slash2 = InputDate.lastIndexOf("/");
        var today = new Date()
        var todayYear = today.getFullYear() + ""

        if (slash1 != -1) {
            //	    only one slash  
            if (slash1 == slash2) {
                return (strDate);
            }
        }
        if (strDate.length == 9) {
            if ((slash1 == 1) && (slash2 == 4)) {
                Month = "0" + strDate.substring(0, 1);
                Day = strDate.substring(2, 4);
                Year = strDate.substring(5)
                strDate = Month + "/" + Day + "/" + Year;
            }
            else {
                if ((slash1 == 2) && (slash2 == 4)) {
                    Month = strDate.substring(0, 2);
                    Day = 0 + strDate.substring(3, 4);
                    Year = strDate.substring(5)
                    strDate = Month + "/" + Day + "/" + Year;
                }
            }
        }
        
        
        if (strDate.length == 8) {
            if ((slash1 == 1) && (slash2 == 3)) {
                Month = "0" + strDate.substring(0, 1);
                Day = "0" + strDate.substring(2, 3);
                Year = strDate.substring(4)
                strDate = Month + "/" + Day + "/" + Year;
            }
            else {
                if (slash1 < 1) {
                    Year = strDate.substring(4);
                    Month = strDate.substring(0, 2);
                    Day = strDate.substring(2, 4);
                    strDate = Month + "/" + Day + "/" + Year;
                }
                else {
                    Year = strDate.substring(6)
                    tdYear = todayYear.substring(2)
                    if (Year <= tdYear) {
                        Year = "20" + Year
                    }
                    else {
                        Year = "19" + Year
                    }

                    Month = strDate.substring(0, 2);
                    Day = strDate.substring(3, 5);
                    strDate = Month + "/" + Day + "/" + Year;
                }
            }
        }
        if (strDate.length == 6) {
            if (slash1 < 1) {
                Year = strDate.substring(4);
                tdYear = todayYear.substring(2)
                if (Year <= tdYear) {
                    Year = "20" + Year
                }
                else {
                    Year = "19" + Year
                }
                Month = strDate.substring(0, 2);
                Day = strDate.substring(2, 4);
                strDate = Month + "/" + Day + "/" + Year;
            }
        }

        return (strDate);
    }
	
