

function ValidateDate(ObjDateForm)
{

if(ObjDateForm.value != "")
	{
	var strValue = new String;strValue = ObjDateForm.value;
	
	/************************************************
	DESCRIPTION: Validates that a string contains only 
	    valid dates with 2 digit or 3 letter month, 2 digit day, 
	    4 digit year. Date separator can be  -, \ or /.
	    Uses combination of regular expressions and 
	    string parsing to validate date.
	    
	    If the date is in the correct format, it is written back 
	    to the passed object value in the form dd-mmm-yyyy
	    
	    Valid date are in the form.
			dd/mm/yyyy	or dd-mm-yyyy	,dd/mmm/yyyy	or dd-mmm-yyyy 
			dd/mm/yy	or dd-mm-yy		,dd/mmm/yy		or dd-mmm-yy 
			
	PARAMETERS:
	   ObjDateForm - Text box object containing date value.
	   
	RETURNS:
	   True if valid, otherwise false.
	   
	REMARKS:
	   Avoids some of the limitations of the Date.parse()
	   method such as the date separator character.
	*************************************************/
	var objRegExpY4or2 = /^\d{1,2}(\-|\/)((\d{1,2})|([a-zA-Z]{3}))\1(\d\d){1,2}$/
	var objRegExpMonths = /(JAN)|(FEB)|(MAR)|(APR)|(MAY)|(JUN)|(JUL)|(AUG)|(SEP)|(OCT)|(NOV)|(DEC)/
	var arrayDate, intDay, intMonth, intYear, strMonth;
	var ValidDate = new Boolean(true)
	var stringMonthFormat = new Boolean(false);
	var ThisDate = new Date;
	var returnDate = '';
	var tempBool = new Boolean(false);
	
	  strValue=strValue.toUpperCase();
	  //check to see if in correct format
	  if(!objRegExpY4or2.test(strValue)) 
	  {//doesn't match pattern, bad date
		ValidDate = false;
		 }
	  else
	  { //date has correct shape anyway
	    arrayDate = strValue.split(RegExp.$1); //split date into month, day, year
		intDay = parseInt(arrayDate[0],10); 
	    strMonth = arrayDate[1];
	    if(objRegExpMonths.test(strMonth))
	    {
			intMonth = 0;
			stringMonthFormat = true;	//Month is in a text format
		}
		else
			intMonth = parseInt(arrayDate[1],10);
			
		intYear = parseInt(arrayDate[2],10);
		if ((intYear < 100)&&(intYear>30))
			intYear += 1900;
		else if ((intYear <= 30)&&(intYear>=0))
			intYear += 2000
	  }
	  if(ValidDate) 
	  {
	  	//check for valid month
		if(	((intMonth > 12) || (intMonth < 1)) && !stringMonthFormat)
		{
			ValidDate = false;
		}
		if(stringMonthFormat==false)
		{   
			switch(intMonth){
				case (1):
					strMonth = 'Jan'
					break;
				case (3):
					strMonth = 'Mar'
					break;
				case (4):
					strMonth = 'Apr'
					break;
				case (5):
					strMonth = 'May'
					break;
				case (6):
					strMonth = 'Jun'
					break;
				case (7):
					strMonth = 'Jul'
					break;
				case (8):
					strMonth = 'Aug'
					break;
				case (9):
					strMonth = 'Sep'
					break;
				case (10):
					strMonth = 'Oct'
					break;
				case (11):
					strMonth = 'Nov'
					break;
				case (12):
					strMonth = 'Dec'
					break;
				case (2):
					strMonth = 'Feb'
					break;
				default:
					ValidDate = false;
					break;
			}
		}	
		if(stringMonthFormat==true)
		{   
			switch(strMonth){
				case 'JAN':
					if((intDay<=31)&& (intDay != 0)) 
						tempBool = true;
						strMonth='Jan'
					break;
				case 'MAR':
					if((intDay<=31) && (intDay != 0))
						tempBool = true;
						strMonth='Mar'
					break;
				case 'APR':
					if((intDay<=30) && (intDay != 0)) 
						tempBool = true;
						strMonth='Apr'
					break;
				case 'MAY':
					if((intDay<=31) && (intDay != 0)) 
						tempBool = true;
						strMonth='May'
					break;
				case 'JUN':
					if((intDay<=30) && (intDay != 0)) 
						tempBool = true;
						strMonth='Jun'
					break;
				case 'JUL':
					if((intDay<=31) && (intDay != 0)) 
						tempBool = true;
						strMonth='Jul'
					break;
				case 'AUG':
					if((intDay<=31) && (intDay != 0)) 
						tempBool = true;
						strMonth='Aug'
					break;
				case 'SEP':
					if((intDay<=30) && (intDay != 0))
						tempBool = true;
						strMonth='Sep'
					break;
				case 'OCT':
					if((intDay<=31) && (intDay != 0)) 
						tempBool = true;
						strMonth='Oct'
					break;
				case 'NOV':
					if((intDay<=30) && (intDay != 0)) 
						tempBool = true;
						strMonth='Nov'
					break;
				case 'DEC':
					if((intDay<=31) && (intDay != 0)) 
						tempBool = true;
						strMonth='Dec'
					break;
				case 'FEB':
					strMonth='Feb'
					break;
				default:
					ValidDate = false;
					break;
			}
		returnDate = intDay+"-"+strMonth+"-"+intYear;
		
				if (tempBool==true)
				{
						ObjDateForm.value = returnDate
						return true;
				}
				else ValidDate = false;
			
		}
		else
		{
			var arrayLookup = new Array('31', '' ,'31','30','31','30','31','31','30','31','30','31')
		}
		returnDate = intDay+"-"+strMonth+"-"+intYear;
		if ((strMonth!='Feb')&&(ValidDate!=false))
			{	
				//check if month value and day value agree
				if(arrayLookup[intMonth-1] != null) {
				  if((intDay <= arrayLookup[intMonth-1]) && (intDay != 0))
				  {
						ObjDateForm.value = returnDate
						return true;
					}				   
			}
		}
	    //check for February
	    if((intMonth==2)||(strMonth=='Feb'))
		{
			var booLeapYear = ( ((intYear % 4) == 0) && (((intYear % 100) != 0) || (intYear % 400) == 0) );
			if( (((booLeapYear==true) && (intDay <= 29)) || ((booLeapYear==false) && (intDay <=28))) && (intDay !=0))
				  {
						ObjDateForm.value = returnDate
						return true;
					}
	     }
	  }
	ValidDate = false;
}
if (ValidDate==false)
{
	ObjDateForm.select();
	alert(ObjDateForm.value+" is not a valid date\n Please us the format dd/mm/yyy eg 31/10/1954") 
	return false;
}
return false;
}

