var dtCh= "/";

var d, s= "";
	d=new Date();
	s += d.getDate() + "/";
	s += (d.getMonth()+ 1) + "/";
	s += d.getFullYear();
   	currdate = s;
	
	
	arraydate=currdate.split("/");
	idate=parseInt(arraydate[0]);
	imonth=parseInt(arraydate[1]);
	iyear=parseInt(arraydate[2]);
	var maxYear=iyear +1;	/* get maxYear */
	var minYear=iyear;	/* get minYear */
	
/* FOLLOWING FUNCTION CHECKS FOR THE NUMERIC VALIDATION */	
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

/* CHECK FOR THE DAYS IN FEBRUARY MONTH */
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

/* THIS CHECK IS ONLY FOR DAYS IN MONTH */
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

/* NEXT FUNCTION IS CALLED WHEN FROM DATE CHECKING IS DONE */
function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) 
	{
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	fromdate=document.frmcarrental.txtdtfrom.value;
	todate=document.frmcarrental.txtdtto.value;
	
	/* Check month length */
	 var date=new Date();
	 y=parseInt(date.getYear());
	 
	if (strMonth.length<1 || month<1 || month>12 || month < imonth)
	{     // if the year is not greter than current, then it check for month validation 
	       if(y==strYear)
		  {
		      if(month < imonth)
			  {
				alert("From Date should be greater than Today's Date");
				document.frmcarrental.txtdtfrom.focus();
				return false;
			   }
		  }	
	}
	/* Check days in months */
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		alert("From Date should be greater than Today's Date");
		document.frmcarrental.txtdtfrom.focus();
		return false;
	}
	/* Check year */
	if (strYear.length != 4 || year==0 || year<minYear){
		<!-- alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear) -->
		alert("From Date should be greater than Today's Date");
		document.frmcarrental.txtdtfrom.focus();
		return false;
	}
	/* If month is right then check for the date */
	if(month == imonth)
	{
		if(day < idate)
		{
			alert("From Date should be greater than Today's Date");
			document.frmcarrental.txtdtfrom.focus();
			return false;
		}
		else {}
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		alert("Please enter a valid date");
		return false;
	}
	else
	{
		
		//return true		
	}

}

/* NEXT FUNCTION IS CALLED WHEN TO DATE IS ENTERED */
function isDate1(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	fromdate=document.frmcarrental.txtdtfrom.value;
	todate=document.frmcarrental.txtdtto.value;
	
	/* Check month length */
	if (strMonth.length<1 || month<1 || month>12)
	{
		alert("Month cannot be less than Today's Month");
		return false;
	}
	/* Check date */
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		alert("Day cannot be less than Today's Date!");
		return false;
	}
	/* Check year */
	if (strYear.length != 4 || year==0 || year<minYear){
		<!-- alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear) -->
		alert("Year cannot be less than current year");
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		alert("Please enter a valid date");
		return false;
	}
	fd = document.frmcarrental.txtdtfrom.value;	/* from date */
	td = document.frmcarrental.txtdtto.value;	/* to date */
	arraydate1=fd.split("/");
	arrday = arraydate1[0];
	arrmonth = arraydate1[1];
	arryear = arraydate1[2];
	if (year > arryear)
	{
		/*If to year is greater than from year*/
		 if(datediff(fromdate, todate)==false)
		{	/*If to year is greater than from year*/	
				return false;
		}
	}
	else if ((month < arrmonth))
	{	/*if month is < than from date fields month */
		alert("ToDate should be greater than FromDate");
		return false;
	}
	else if ((year < arryear))
	{	/*if year is < than from date fields year */
		alert("ToDate should be greater than FromDate");
		return false;
	}
	else
	{
		if(datediff(fromdate, todate)==false)
		{
			return false;
		}
		else
		{}
		/*if (day < arrday)
		{
			alert("A2");
			alert("ToDate should be greater than FromDate");
			return false;	
		}*/
	}


//return true
}

/* datediff FUNCTION COMPARES BOTH THE DATES AND IF MONTH DIFFERENCE IS GREATER THAN 1 IT FIRES MESSAGE */
function datediff(fdate, tdate)
{
	arrfrom = fdate.split("/");
	fday = arrfrom[0];		/* Fromdate's Day */
	fmonth = arrfrom[1];	/* Fromdate's Month */
	fyear = arrfrom[2];		/* Fromdate's Year */
	
	arrto = tdate.split("/");
	tday = arrto[0];		/* Todate's Day */
	tmonth = arrto[1];		/* Todate's Month */		
	tyear = arrto[2];		/* Todate's year */
	
	if(tyear > fyear)	
	{
		if(tyear-fyear > 1)
		{
			alert("Please Enter valid ToDate");
			return false;
		}
		else
		{
			mdiff = tmonth-fmonth;
			if ((mdiff != -11))
			{
				
				alert("Date Difference should not be greater than 1 month.");
				return false;
			}
			else
			{ /*return to page safely*/ }
		}
	}
	else if(tyear == fyear)
	{
		mdiff = tmonth-fmonth;
		if ((mdiff != 1)&&(mdiff != 0))
		{
			alert("Date Difference should not be greater than 1 month.");
			return false;
		}
		else
		{ /*return to page safely*/ }
	}
	else
	{ /* return to page safely */	}
}

/* WHEN USER ENTERS BOTH THE DATES CONTROL FIRST COMES TO THIS FUNCTION */
function ValidateForm()
{
		var dt=document.frmcarrental.txtdtfrom;
		if (isDate(dt.value)==false)
		{
			dt.focus()
			return false;
		}
		else
		{
			var dt1=document.frmcarrental.txtdtto;
			if (isDate1(dt1.value)==false)
			{
				dt1.focus()
				return false;
			}
			else
			{
				return true;				
			}
		}
}

