var MMDDYY_FMT = 	0	// 		mm/gg/yy	
var DDMMYY_FMT = 	1	// 		dd/mm/yy
var WORLD_FMT =		2	// 		dd-MMM-yy	
var MMDDYYYY_FMT =  3   //      mm/gg/yyyy
var DDMMYYYY_FMT =  4   //      dd/mm/yyyy

function CheckDate (theField, FieldName, CheckNull, UseLocalAlerts, nDateFormat)
{
	if (!theField)
		return (true);
	var inStr1 = theField.value;
	var inLen1 = inStr1.length;
	var FirstSl, LastSl, Value;
	var szMsgDateMask;

	var cSep;
	var inStr, inLen;
	var dd, mm, yy, start;
	var x1, x2, x3;

	if (CheckNull && inLen1 == 0) {
		if (UseLocalAlerts)
			alert (Sprintf(szMsgField, FieldName));
		return (false);
	}

	if (inLen1 > 0) {

		var szDateFormat = IntToFormat (nDateFormat); 
		cSep = FindSeparator (szDateFormat);

		FirstSl = inStr1.indexOf (cSep);
		LastSl = inStr1.lastIndexOf (cSep);
	   
		x1 = inStr1.substring (0, FirstSl);
		x2 = inStr1.substring (FirstSl + 1, LastSl); 
		x3 = inStr1.substring (LastSl + 1, inLen1);
	
		if (nDateFormat == DDMMYY_FMT) {
			if (CheckNumber (x1) || CheckNumber (x2) || CheckNumber (x3)) {
				if (UseLocalAlerts) {
					szMsg = Sprintf(szMsgBadDateFormat, szDateFormat) ;
					alert (szMsg);
				}
				return (false);	
			}	
			dd = ConvertInt (x1);
			mm = ConvertInt (x2);
			yy = ConvertInt (x3);
		}
		
		if (nDateFormat == MMDDYY_FMT) {
			if (CheckNumber (x1) || CheckNumber (x2) || CheckNumber (x3)) {
				if (UseLocalAlerts) {
					szMsg = Sprintf(szMsgBadDateFormat, szDateFormat) ;
					alert (szMsg);
				}
				return (false);	
			}	
			dd = ConvertInt (x2);
			mm = ConvertInt (x1);
			yy = ConvertInt (x3);
		}

		if (nDateFormat == WORLD_FMT) {
			if (CheckNumber (x1) || CheckNumber (x3)) {
				if (UseLocalAlerts) {
					szMsg = Sprintf(szMsgBadDateFormat, szDateFormat) ;
					alert (szMsg);
				}
				return (false);	
			}	
			dd = ConvertInt (x1);
			mm = GetMounth (x2);
			yy = ConvertInt (x3);
		}

		if (nDateFormat == DDMMYYYY_FMT) {
			if (CheckNumber (x1) || CheckNumber (x2) || CheckNumber (x3)) {
				if (UseLocalAlerts) {
					szMsg = Sprintf(szMsgBadDateFormat, szDateFormat) ;
					alert (szMsg);
				}
				return (false);
			}
			dd = ConvertInt (x1);
			mm = ConvertInt (x2);
			yy = ConvertInt (x3);
		}

		if (nDateFormat == MMDDYYYY_FMT) {
			if (CheckNumber (x1) || CheckNumber (x2) || CheckNumber (x3)) {
				if (UseLocalAlerts) {
					szMsg = Sprintf(szMsgBadDateFormat, szDateFormat) ;
					alert (szMsg);
				}
				return (false);
			}
			dd = ConvertInt (x2);
			mm = ConvertInt (x1);
			yy = ConvertInt (x3);
		}
	
		inStr = mm;
		inStr += cSep;
		inStr += dd;
		inStr += cSep;
		inStr += yy;	
	
		inLen = inStr.length;

		if (inLen > 10 || inLen < 5 ) {
			if (UseLocalAlerts) {
				szMsg = Sprintf(szMsgBadDateFormat, szDateFormat) ;
				alert (szMsg);
			}
			return (false);
	   	}
	   
		if (mm < 1 || mm > 12) {
			if (UseLocalAlerts) {
				szMsg = Sprintf(szMsgBadDateFormat, szDateFormat) ;
				alert (szMsg);
			}
			return (false);
		}
		if (dd < 1 || dd > 31) {
			if (UseLocalAlerts) {
				szMsg = Sprintf(szMsgBadDateFormat, szDateFormat) ;
				alert (szMsg);
			}
			return (false);
		}
		if((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11))
			if(dd == 31) {
				if (UseLocalAlerts) {
					szMsg = Sprintf(szMsgBadDateFormat, szDateFormat) ;
					alert (szMsg);
				}
				return (false);
			}
		if (yy < 0 || yy > 9999) {
			if (UseLocalAlerts) {
				szMsg = Sprintf(szMsgBadDateFormat, szDateFormat) ;
				alert (szMsg);
			}
			return (false);
		}
		if (nDateFormat == MMDDYY_FMT || nDateFormat == DDMMYY_FMT){
			 if(yy > 100){
				if (UseLocalAlerts) {
					szMsg = Sprintf(szMsgBadDateFormat, szDateFormat) ;
					alert (szMsg);
				 }
			 	return (false);
			}
			if(yy < 50)
				yy = 2000 + yy;
			else if (yy < 100)
				yy = 1900 + yy;
		}

		 if (nDateFormat == MMDDYYYY_FMT || nDateFormat == DDMMYYYY_FMT) {
			 if(yy < 1000){
				if (UseLocalAlerts) {
					szMsg = Sprintf(szMsgBadDateFormat, szDateFormat) ;
					alert (szMsg);
				 }
			 	return (false);
			}
		 }
	
		if((dd == 29) && (mm == 2))
		{
			if(((yy % 4) != 0) || ((yy % 400) == 0))
			{
				if (UseLocalAlerts) {
					szMsg = Sprintf(szMsgBadDateFormat, szDateFormat) ;
					alert (szMsg);
				}
				return (false);
			}
		}

		if((mm == 2) && (dd > 29))
		{
			if (UseLocalAlerts) {
				szMsg = Sprintf(szMsgBadDateFormat, szDateFormat) ;
				alert (szMsg);
			}
			return (false);
		}

	   }
	return (true);
}

function GetMounth (szMounth) {
	if (szMounth == "Jan")
		return (1);
	else if (szMounth == "Feb")
		return (2);
	else if (szMounth == "Mar")
		return (3);
	else if (szMounth == "Apr")
		return (4);
	else if (szMounth == "May")
		return (5);
	else if (szMounth == "Jun")
		return (6);
	else if (szMounth == "Jul")
		return (7);
	else if (szMounth == "Aug")
		return (8);
	else if (szMounth == "Sep")
		return (9);
	else if (szMounth == "Oct")
		return (10);
	else if (szMounth == "Nov")
		return (11);
	else if (szMounth == "Dec")
		return (12);
	else
		return (0);
}

function CheckNumber (szString) {
	var Ch;
	for (i = 0; i < szString.length; i++) {
		Ch = szString.charAt (i);
		if (Ch < '0' || Ch > '9')
			return (true);
   	}
	return (false);
}

function ConvertInt (szString) {
	var Cnt = 0;
	if (!szString.length)
		return (-1);
	if (szString.charAt (Cnt) == '0') {
		Cnt += 1;
		if (szString.charAt (Cnt) < '0' || szString.charAt (Cnt) > '9')
			return (-1);
	}
	return (parseInt (szString.substring (Cnt, szString.length)));
}

function IntToFormat (nDateFormat) {
	if (nDateFormat == DDMMYY_FMT) 
		return("dd/mm/yy");
	else if (nDateFormat == MMDDYY_FMT) 
		return("mm/dd/yy");
	else if (nDateFormat == WORLD_FMT) 
		return("dd/MMM/yy");
	else if (nDateFormat == DDMMYYYY_FMT) 
		return("dd/mm/yyyy");
	else if (nDateFormat == MMDDYYYY_FMT) 
		return("mm/dd/yyyy");
}


function FindSeparator (szDateFormat) {
	var Value, cSep;
	for (i = 0; i < szDateFormat.length; i++) {
		Value = szDateFormat.charAt (i);
		if ((Value < 'a' || Value > 'z') && (Value < 'A' || Value > 'Z')) 
			cSep = Value;
	}	
	return (cSep);
}
