function DateFilter(ctl)
{
	var sTemp = "";
	var str = ctl.value;
	var len = str.length;
	var i = 0;
	for(i = 0; i < len; i++){
		var c = str.charAt(i);
		if( (c >= '0' && c <= '9') || c == '/' )
			sTemp += c;
	}
	str = sTemp;
	len = str.length;
	sTemp = "";
	if(len > 0){
		var c = str.charAt(len - 1);
		if( c >= '0' && c <= '9' ){
			sTemp = str;
			if(len == 2)
				sTemp = str + "/";
			else if(len > 2){
				var n1 = str.indexOf( "/" );
				var n2 = str.lastIndexOf( "/" );
				if(n1 == n2 && n1 > 0){
					var s1 = str.substring( n1 + 1 );
					if(s1.length == 2)
						sTemp = str + "/";
				}
				else if(n2 > n1 && n1 > 0){
					var s1 = str.substring( n2 + 1 );
					if(s1.length > 4)
						sTemp = str.substring( 0, n2 + 5 );
				}
			}
		}
		else if( c == '/' && len > 1){
			var n1, n2, n3;
			sTemp = str.substring(0, len - 1);
			n1 = sTemp.indexOf( "/" );
			n2 = sTemp.lastIndexOf( "/" );
			n3 = str.lastIndexOf( "/" );
			
			if(n1 > 0 && n1 == n2 && n1 < (n3 - 1) )
				sTemp += "/";
			else if(n1 <= 0)
				return;
		}
		else
			sTemp = str.substring(0, len - 1);
	}
	if(sTemp != ctl.value)
		ctl.value = sTemp;
}

function getAge( sDate )
{
	var d,n1,n2, nMonth, nYear, nDay, nCurrMonth, nCurrYear, nCurrDay, nAge;
	n1 = sDate.indexOf( "/" );
	n2 = sDate.lastIndexOf( "/" );
	nMonth = parseInt(  sDate.substring( 0, n1  ),10);
	nDay = parseInt(  sDate.substring( n1+1, n2  ),10);
	nYear = parseInt(  sDate.substring(n2+1),10);
	d = new Date();
	nCurrMonth = (d.getMonth() + 1);
	nCurrDay = d.getDate() ;
	nCurrYear = d.getFullYear();

	nAge = nCurrYear - nYear - 1;
	if ( (nCurrMonth > nMonth ) || (nCurrMonth == nMonth && nCurrDay >= nDay) ) nAge++;
	return nAge;
}

function isDate( s )
{
	var sDay, sMonth, sYear, nMonth, nDay, nYear, nSep1, nSep2;
	nSep1 = s.indexOf( "/" );	
	if ( nSep1 < 0 ) 
		return false;
	nSep2 = s.lastIndexOf( "/" );	
	if ( nSep2 < 0 ) return false;
	if ( nSep1 == nSep2 ) 
		return false;
	sMonth = s.substring( 0, nSep1  );
	sDay = s.substring( nSep1 + 1, nSep2 );
	sYear = s.substring( nSep2+1 );
	if ( !sMonth.length || !sDay.length || !sYear.length ) 
		return false;
	if ( isNaN(sMonth) || isNaN(sDay) || isNaN(sYear) ) 
		return false;
	nMonth = parseInt(sMonth,10); 
	nDay = parseInt(sDay,10); 
	nYear = parseInt(sYear,10);
	if ( nMonth<=0 || nDay<=0 || nYear<=0 ) 
		return false;
	if ( nMonth > 12 ) 
		return false;
	if (nMonth==1 || nMonth==3 || nMonth==5 || nMonth==7 || nMonth==8 || nMonth==10 || nMonth==12 )
		if ( nDay > 31 ) return false; 
	if (nMonth==4 || nMonth==6 || nMonth==9 || nMonth==11 ){
		if ( nDay > 30 ) 
			return false; 
	}
	if (nMonth==2) {
		if ( isLeapYear(nYear) == true) {
			if ( nDay > 29 ) 
				return false;
		} 
		else if ( nDay > 28 ) 
			return false;
	}
	if(nYear < 1850)
		return false;
	if(nYear > 2050)
		return false;
	return true;
} 
function CompareToToday(sDate)
{
	var d,n1,n2, nMonth, nYear, nDay, nCurrMonth, nCurrYear, nCurrDay;
	n1 = sDate.indexOf( "/" );
	n2 = sDate.lastIndexOf( "/" );
	if(n1 >= n2 || n2 == sDate.length ) return -1;
	nMonth = parseInt(  sDate.substring( 0, n1  ),10 );
	nDay = parseInt(  sDate.substring( n1+1, n2  ),10);
	nYear = parseInt(  sDate.substring(n2+1),10);
	d = new Date();
	nCurrMonth = (d.getMonth() + 1);
	nCurrDay = d.getDate() ;
	nCurrYear = d.getFullYear();
	if ( nYear > nCurrYear ) return 1;
	if ( nYear < nCurrYear ) return -1;
	if ( nMonth > nCurrMonth ) return 1;
	if ( nMonth < nCurrMonth ) return -1;
	if ( nDay > nCurrDay ) return 1;
	if ( nDay < nCurrDay ) return -1;
	return 0;
}

function CompareDate( sStartDate, sEndDate)
{
	var f, startDay, startMonth, startYear, endMonth, endDay, endYear, n1, n2,n3, n4;
	n1 = sStartDate.indexOf( "/" );
	n2 = sStartDate.lastIndexOf( "/" );
	n3 = sEndDate.indexOf( "/" );
	n4 = sEndDate.lastIndexOf( "/" );
	startMonth = parseInt( sStartDate.substring( 0, n1  ),10);
	startDay = parseInt( sStartDate.substring( n1 + 1, n2 ),10);
	startYear = parseInt( sStartDate.substring( n2+1 ),10);
	endMonth = parseInt( sEndDate.substring( 0, n3  ),10);
	endDay = parseInt( sEndDate.substring( n3+1, n4 ),10);
	endYear = parseInt( sEndDate.substring( n4+1 ),10);
	if(startYear < 20)
		startYear = startYear + 2000;
	else if(startYear < 100)
		startYear = startYear + 1900;
	if(endYear < 20)
		endYear = endYear + 2000;
	else if(endYear < 100)
		endYear = endYear + 1900;
	if ( startYear > endYear ) 
		return 1;
	if ( startYear < endYear ) 
		return -1;
	if ( startMonth > endMonth ) 
		return 1;
	if ( startMonth < endMonth ) 
		return -1;
	if ( startDay > endDay ) 
		return 1;
	if ( startDay < endDay ) 
		return -1;
	return 0;
} 
function CompareDate2( sStartDate, sEndDate)
{
	startMonth = sStartDate.getMonth();
	startDay = sStartDate.getDate();
	startYear = sStartDate.getFullYear();
	endMonth = sEndDate.getMonth();
	endDay = sEndDate.getDate();
	endYear = sEndDate.getFullYear();
	
	if(startYear < 20)
		startYear = startYear + 2000;
	else if(startYear < 100)
		startYear = startYear + 1900;
	if(endYear < 20)
		endYear = endYear + 2000;
	else if(endYear < 100)
		endYear = endYear + 1900;
	if ( startYear > endYear ) 
		return 1;
	if ( startYear < endYear ) 
		return -1;
	if ( startMonth > endMonth ) 
		return 1;
	if ( startMonth < endMonth ) 
		return -1;
	if ( startDay > endDay ) 
		return 1;
	if ( startDay < endDay ) 
		return -1;
	return 0;
} 

function CheckDate(txtDate)
{
	var sTemp = txtDate.value;
	
	if( sTemp == "" || !isDate( sTemp )  ){
		var str1;
		if(typeof(FrenchLanguage) != 'undefined' && FrenchLanguage == 1)
			str1 = "Date entrée est invalide!";
		else
			str1 = "Invalid date entered!";
		alert(str1);

		if (typeof (txtDate) != 'undefined' && txtDate != null && !txtDate.isDisabled)
		{
			txtDate.focus();
			txtDate.select(0, -1);
		}
		return false;
	}
	return true;
}
function isLeapYear (Year) 
{
	if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
		return true;
	} 
	else {
		return false;
	}
}
function GetMonthDays(Month, Year)
{
	var MonthDays = new Array('31', '28', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31');
	if( Month != 1 ) return parseInt(MonthDays[Month]);
	if(isLeapYear (Year) ) return 29;
	return 28;
}
function AddOneDay(sDate)
{
	
	var nDays = sDate.getDate();
	var nMonth = sDate.getMonth();
	var nYear = sDate.getFullYear();
	var MDays = GetMonthDays(nMonth, nYear);
	nDays = nDays + 1;
	
	if(nDays > MDays){
		nDays = 1;
		nMonth = nMonth + 1;
		if(nMonth >= 12){
			nMonth = 0;
			nYear = nYear + 1;
		}
	}
	var NDate = new Date(nYear, nMonth, nDays);
	return NDate;
}

function getDays( sDate )
{
	var d1, d2n1,n2, nMonth, nYear, nDay;
	var datediff = 0;
	
	n1 = sDate.indexOf( "/" );
	n2 = sDate.lastIndexOf( "/" );
	nMonth = parseInt(  sDate.substring( 0, n1  ),10);
	nDay = parseInt(  sDate.substring( n1+1, n2  ),10);
	nYear = parseInt(  sDate.substring(n2+1),10);
	d1 = new Date();
	d2 = new Date(nYear, nMonth - 1, nDay);
	
	while(CompareDate2( d2, d1) < 0 && datediff < 10000) {
		d2 = AddOneDay(d2);
		datediff = datediff + 1;
	}
	
	return datediff;
}
function checkAge(sDOB)
{
	var cur = new Date();
	var sTmp = (cur.getMonth() + 1) + "/" + cur.getDate() + "/" + (cur.getFullYear() - 130);
	return CompareDate(sTmp,sDOB);
	
}
function getCurrentDate()
{
	var cur = new Date();
	var m = cur.getMonth() + 1;
	var d = cur.getDate();
	var m_str, d_str;
	if(m < 10) 
	    m_str = '0' + m;
    else
        m_str = '' + m;
        
    if(d < 10)
        d_str = '0' + d;
    else 
        d_str = d; 	 
	return  m_str + "/" + d_str + "/" + cur.getFullYear();
}

