/*
Calendar Script
Main Function   : startCal(cdate, mindt, maxdt, object, output)
Parameters      : 1. cdate : The Default date to be highlighted, if nothing specified current date will be taken
                  2. mindt : The Minimum date, All dates before minimum date will not be clickable, Default Value is January 01, 1900
                  3. maxdt : The Maximum date, All dates after minimum date will not be clickable, Default Value is January 01, 9999
                  4. object: The table data id on which the calendar will be drawn.
                  5. output: The text box or control name to which the value will be set. Ex: document.Form1.txtDate
Customization   : Change the color variables to modify the colors


//--------Changes Done For From And To Date Validation By Joshana Patil -24/06/2008-----------
                  6. output1   : Variable for to dates text name .
				  7. checkflag : Variable for from or to text.

*/

var curdate = new Date();
var nextdate = new Date();
var mindate = new Date("January 01, 1850");  // dd/mm/yy
var maxdate = new Date("January 01, 9999");
var tempdate = new Date();
var seldate = new Date();
var outHTML = "";
var winPointer;
var winCaller;
var M = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var D = new Array("Su","Mo","Tu","We","Th","Fr","Sa");
var splitter = new Array();
var minSplit = new Array();
var maxSplit = new Array();
var weekdaysbgcolor = "#CCCCCC";
var bordercolor = "#f8f8f8";
var dt = "";


function showCalender(cdate, mindt, maxdt, object, output, output1,checkflag,cal,fromdate,todate)
{

//Validation Check for leave date must be first entered before return date 
  var showcalender=true;
  //alert(checkflag);
  if(checkflag=="to" || checkflag=="tojp")
	{
		/*if(document.getElementById(output).value=="")
		{
		alert("First Select Leave date ");
		showcalender=false;
		}*/
	}
	//If leave date entered then 
	if(showcalender==true)
	{
		document.getElementById(cal).style.visibility = 'visible';
  		  if ((cdate == "")||(cdate == null)) 
		  {
			tempdate = new Date();
		  }
		  else
		  {
			splitter = cdate.split("-");
			splitter[1] = MonthConvertion(splitter[1]);
			var i = 0
			i = splitter[1] - 1;
			splitter[1] = i;
			dt = M[parseInt(splitter[1])] + " " + splitter[0] + "," + eval(splitter[2]);
			tempdate = new Date(dt);
			seldate = new Date(dt);
		 }
		  //Set Current and Next Month/Year to draw Calendar
		  curdate = new Date(tempdate);
		  nextdate.setDate("01");
		  nextdate.setMonth(tempdate.getMonth() + 1);// = new Date(tempdate);

		  //Set the min and max date to default if not min/max specified
		  if ((mindt == "") || (mindt == null)){
			mindate = new Date();
			mindate.setDate(mindate.getDate() - 1);
		  }
		  else
			  {
			minSplit = mindt.split("/");
			minSplit[0] = parseInt(minSplit[0]) - 1;
			dt = M[parseInt(minSplit[0])] + " " + parseInt(minSplit[1]) + "," + eval(minSplit[2]);
			mindate = new Date(dt);
			  }

			  if ((maxdt == "") || (maxdt == null))
				  {
			  }
			  else
			{
				maxSplit = maxdt.split("/");
				maxSplit[0] = parseInt(maxSplit[0]) - 1;
				dt = M[parseInt(maxSplit[0])] + " " + parseInt(maxSplit[1]) + "," + eval(maxSplit[2]);
				maxdate = new Date(dt);
			  }
			  obj = object;
			 // alert("upto main function");
			 /* var value1=7;
			  var value2=10;

			  if(value1>value2)
		      {
				  alert("value1 is G");
					  
		      }
			  else
		      {
				  alert("value2 is G");
		      }*/
			 /* alert("start");
            alert(seldate);
			alert("end");*/
  	getCalenderTable(curdate.getMonth() , curdate.getFullYear() , seldate, object , output, output1,checkflag,cal,fromdate,todate);
	}
	
}

function convertText2Date(txtdate)
{
  var tmp;
  //set the calendars date if not provided
  if ((txtdate == "")|| (txtdate == null))  {
    tmp = new Date();
  }else{
    splitter = txtdate.split("/");
    splitter[0] = parseInt(splitter[0]) - 1;

    dt = M[parseInt(splitter[0])] + " " + parseInt(splitter[1]) + "," + eval(splitter[2]);
    tmp = new Date(dt);

  }
}

function getDateString(day)
{
  switch(day)
  {
    case 0:
      return "Sunday";
      break;
    case 1:
      return "Monday";
      break;
    case 2:
      return "Tuesday";
      break;
    case 3:
      return "Wednesday";
      break;
    case 4:
      return "Thursday";
      break;
    case 5:
      return "Friday";
      break;
    case 6:
      return "Saturday";
      break;
  }
}

function getCalenderTable(imonth, iyear, selecteddate , object, output, output1,checkflag,cal,fromdate,todate)
{
  var curdate = new Date();
  var presentDay = getDateString(curdate.getDay());
  var mindate = new Date("January 01, 1850");  // dd/mm/yy
  var maxdate = new Date("January 01, 9999");
  var M = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  var D = new Array("Su","Mo","Tu","We","Th","Fr","Sa");
  var weekdaysbgcolor = "#CCCCCC";
  var bordercolor = "#ffffff";
  var dt = "";
  var retHTML = "";
  var maxday = 0;
  var i=0;
  var j=0;
  var txt = "";
  var myTempDate = new Date(M[imonth] + " " + "01" + ", " + iyear);
  var sldate = new Date(selecteddate);
  var highlightCurrent = false;

  var prvdate = new Date(myTempDate);
  prvdate.setMonth(myTempDate.getMonth() - 1);
  var nxtdate = new Date(myTempDate);
  nxtdate.setMonth(myTempDate.getMonth() + 1);
 // alert("next date");
  //alert(nxtdate);
// alert("update");
  retHTML = "<table border=0 width='100%' class=normal cellspacing=5 cellpadding=0 style='border:0px SOLID #cccccc'>";
  retHTML += "<tr bgcolor='#ffffff'>";
  retHTML += "<td align=center><a style='text-decoration:none' href=\"javascript:getCalenderTable(" + prvdate.getMonth() + "," + prvdate.getFullYear() + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "', '" + checkflag + "','" + cal +"','" + fromdate +"','" + todate +"');\"><img src='"+serverpath+"/modflightsearch/prev.gif' style='cursor:hand;' alt='Previous Month' title='Previous Month' align=absmiddle border=0></a></td>";
  retHTML += "<td align=center>"+ presentDay + ", " + M[curdate.getMonth()] + " " + curdate.getDate() + ", " +curdate.getFullYear() + "</td>";
  retHTML += "<td align=right><a href=\"javascript:getCalenderTable(" + nxtdate.getMonth() + "," + nxtdate.getFullYear() + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "', '" + checkflag + "','" + cal +"','" + fromdate +"','" + todate +"');\"><img src='"+serverpath+"/modflightsearch/next.gif' style='cursor:hand;' alt='Next Month' title='Next Month' align=absmiddle border=0></a></td>";
  retHTML += "</tr>";
  retHTML += "</table>";
  retHTML += "<table border=1 width='100%' class=normal cellspacing=5 cellpadding=0 style='border:0px SOLID #cccccc'>";
  retHTML += "<tr>";
  retHTML += "<td colspan=3>";
  retHTML += "<table width=150px height=100px cellspacing=0 cellpadding=0 style='border-top:0px solid " + bordercolor + ";border-left:0px solid " + bordercolor + ";'>";
  retHTML += "<tr bgcolor='#ffffff'>";
  retHTML += "<td align=center colspan='8'>"+ M[myTempDate.getMonth()] + " " + myTempDate.getFullYear() + "</td>";
  retHTML += "</tr>";
  retHTML += "<tr height=20px>";

  //Print Week Headers
  for(i=0;i<7;i++){
    retHTML += "<td style='border-right:0px solid " + bordercolor + ";border-bottom:0px solid " + bordercolor + ";' class=calendarsmall width=30px>" + D[i] + "</td>";
  }
  retHTML += "</tr>";

  //Check Max Days
  maxday = getMaxDays(imonth, iyear);

  //Loop Weeks and Days
  counter = 1;

// total date format split for geting date only start
 selecetddate_array=sldate.toDateString().split(" ");
 currentdate_array=curdate.toDateString().split(" ");
// alert(currentdate_array);
 //alert(selecetddate_array);
 //alert("check");
// total date format split for geting date only end
  for(i=0;i<6;i++)
	  {
       retHTML += "<tr class=daterow>";
       for(j=0;j<7;j++)
		{
         if(counter>maxday)
		 {
         retHTML += "<td style='cursor:hand;border-bottom:0px solid " + bordercolor + ";border-right:0px solid " + bordercolor + ";' class=blank>&nbsp;</td>";
		 }
		 else
		 {
			 
			// main validation start 
              lessdate=false;
			//Main validation Start for first month table
            //For Year Start
          if((checkflag=="from"))
			   {
	           currentdate_array=currentdate_array;
	           }
			   else
			   {
				  /*alert("in TO");
				   alert("The form date is");
				   alert(selecetddate_array);*/
				currentdate_array=selecetddate_array;
				//alert(currentdate_array);
			   }
			   //alert("in loop");
	 
	    if((currentdate_array[3]==iyear)||(currentdate_array[3]<iyear))
	     { 
	        //For Month Start
			//alert("month start");
			
	         myTempMonth = MonthConvertion(M[myTempDate.getMonth()]);
			// alert(myTempMonth);
			 currentMonth= MonthConvertion(currentdate_array[1]);
			 //alert(currentMonth);
	     if((currentMonth==myTempMonth)&&(currentdate_array[3]==iyear))
		   {
		    if(currentdate_array[2]>myTempDate.getDate())
		     {
				//alert("1");
			lessdate=true;
             }
			 else
			 {
			lessdate=false;
		     }
		   }
	     else
		  {
			/* alert(currentMonth);
			 alert(myTempMonth);
			 alert(currentdate_array[3]);
			 alert(iyear);*/
			 
		   if((parseInt(currentMonth) > parseInt(myTempMonth))&&(currentdate_array[3]==iyear))
		        {
			  // alert("2");
    	       lessdate=true;
                }
	       }//For month end
	  }//For Year End
	else
	{
		//alert("3");
	 lessdate=true;
	}
   
	//alert("the flag is");
//Main validation End for first month table
			   
		 if(sldate.toDateString() == myTempDate.toDateString())
		      {
			   txt = "style='cursor:hand;color:#B7C4C8;background-color:#B7C4C8'";
              }
			  else
			  {
				  txt= "style='cursor:hand;border-bottom:1px solid " + bordercolor + ";border-right:0px solid " + bordercolor + ";'";
			  }

			 
			 


        //WeekDay
            if((myTempDate.getDate() == 1))
		    {
             if((j == myTempDate.getDay()))
		     {
              if((myTempDate.getDay() == 0) || (myTempDate.getDay() == 6))
			   {
              //Check Min Max Dates			  
              if ((myTempDate >= mindate) && (myTempDate <= maxdate))
			    {
				  if(lessdate==true)
					{
					   retHTML += "<td " + txt + " align=center onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' href=\"javascript:setMessage('" + checkflag + "');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					 // retHTML += "<td " + txt + " align=center  onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' ;\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					}
					else
					{
                    retHTML += "<td " + txt + " align=center onClick=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\" onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' href=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
				    }
					//showCalender(document.getElementById("txtDatemulti_1").value,'','','calendermulti_'+cal_id,'txtDatemulti_'+cal_id,'txtDatemulti_'+cal_id,'stop'+cal_id,'showCalmulti_'+cal_id,'fldfromdatemulti_'+cal_id,'fldfromdatemulti_'+cal_id);
				 // retHTML += "<td> " + value1  +"</td>";
                }
			  else
			    {
                  retHTML += "<td " + txt + " align=center><font color=#cccccc>" + counter + "</td>";
				  // retHTML += "<td> " + value2  +"</td>";
                 }
              myTempDate.setDate(myTempDate.getDate() + 1);
              counter++;
            }
			else
			{
              //Check Min Max Dates
              if ((myTempDate >= mindate) && (myTempDate <= maxdate))
			  {
				  if(lessdate==true)
					{
					  retHTML += "<td " + txt + " align=center onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' href=\"javascript:setMessage('" + checkflag + "');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					  //  retHTML += "<td " + txt + " align=center  onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' ><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					}
					else
					{
                  retHTML += "<td " + txt + " align=center onClick=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\" onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' href=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					}
				   //retHTML += "<td> " + value3  +"</td>";
              }
			  else
			  {
                  retHTML += "<td " + txt + " align=center><font color=#cccccc>" + counter + "</td>";
				  // retHTML += "<td> " + value4  +"</td>";
              }
              myTempDate.setDate(myTempDate.getDate() + 1);
              counter++;
            }
          }
		  else
			  {
             retHTML += "<td style='border-bottom:1px solid " + bordercolor + ";border-right:1px solid " + bordercolor + ";'>&nbsp;</td>";
			  //retHTML += "<td> " + value5  +"</td>";
             }
        }
		else
			{
          if((myTempDate.getDay() == 0) || (myTempDate.getDay() == 6))
			 {
            //Check Min Max Dates
              if ((myTempDate >= mindate) && (myTempDate <= maxdate))
				 {
				  if(lessdate==true)
					{
					   retHTML += "<td " + txt + " align=center onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' href=\"javascript:setMessage('" + checkflag + "');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					  // retHTML += "<td " + txt + " align=center  onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none'><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					}
					else
					{
                       retHTML += "<td  " + txt + " align=center onClick=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\" onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#ffffff'\"><a style='text-decoration:none' href=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
				    }
				  // retHTML += "<td>" + value6  +"</td>";
                 }
				 else
				 {
                  retHTML += "<td " + txt + "  bgcolor='#ffffff' align=center><font color=#cccccc>" + counter + "</td>";
				  // retHTML += "<td> " + value7  +"</td>";
                 }
               myTempDate.setDate(myTempDate.getDate() + 1);
               counter++;
          }
		  else
			  {
            //Check Min Max Dates
              if ((myTempDate >= mindate) && (myTempDate <= maxdate))
				{

				  if(lessdate==true)
					{
					  retHTML += "<td " + txt + " align=center onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' href=\"javascript:setMessage('" + checkflag + "');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					 //  retHTML += "<td " + txt + " align=center  onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none'><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					}
					else
					{
                       retHTML += "<td " + txt + " align=center onClick=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\" onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' href=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
				    }
				   //retHTML += "<td> " + value8  +"</td>";
                }
				else
				{
                  retHTML += "<td " + txt + " align=center><font color=#cccccc>" + counter + "</td>";
				   //retHTML += "<td> " + value9  +"</td>";
                }
            myTempDate.setDate(myTempDate.getDate() + 1);
            counter++;
             }
        }
      }
    }
    retHTML += "</tr>";
  }
//alert("for second month");
 //Start second month table
  retHTML += "</table></td>";
  retHTML += "<td colspan=3>";
  retHTML += "<table width=150px height=100px cellspacing=0 cellpadding=0 style='border-top:1px solid " + bordercolor + ";border-left:0px solid " + bordercolor + ";'>";
  retHTML += "<tr bgcolor='#ffffff'>";
  retHTML += "<td align=center colspan='8'>"+ M[myTempDate.getMonth()] + " " + myTempDate.getFullYear() + "</td>";
  retHTML += "</tr>";
  retHTML += "<tr height=20px>";

  //Print Week Headers
  for(i=0;i<7;i++){
    retHTML += "<td style='border-right:0px solid " + bordercolor + ";border-bottom:0px solid " + bordercolor + ";' class=calendarsmall width=30px>" + D[i] + "</td>";
  }
  retHTML += "</tr>";

  if(imonth != 11 )
  {
    imonth  = imonth + 1;
  }

  var myTempDateSecd = new Date(M[imonth] + " " + "01" + ", " + iyear);
  maxday = getMaxDays(imonth, iyear);

  //Loop Weeks and Days
  counter = 1;
  for(i=0;i<6;i++)
	  {
    retHTML += "<tr class=daterow>";
    for(j=0;j<7;j++)
		{
      if(counter>maxday)
		  {
        retHTML += "<td style='cursor:hand;border-bottom:0px solid " + bordercolor + ";border-right:0px solid " + bordercolor + ";' class=blank>&nbsp;</td>";
          }
	  else
		  {

		   lessdate=false;
			   

 //Main validation Start for second month table
 //For Year Start
// alert("hello1");
          if((checkflag=="from"))
			   {
	           currentdate_array=currentdate_array;
	           }
			   else
			   {
				currentdate_array=selecetddate_array;
			   }
	 //alert(currentdate_array);
	// alert("hello2");*/
	/* alert("year");
	 alert(currentdate_array[3]);
	 alert(iyear);
	 alert(myTempDate.getFullYear());*/
	 
	// alert("date");
	 if((currentdate_array[3]==myTempDate.getFullYear())||(currentdate_array[3]<myTempDate.getFullYear()))
	  { 
	   //For Month Start
	         myTempMonth = MonthConvertion(M[myTempDate.getMonth()]);
			 currentMonth= MonthConvertion(currentdate_array[1]);
			 /*alert("month");
     		 alert(currentMonth);
			 alert(myTempMonth);
			 alert("date");
			 alert(currentdate_array[2]);
			 alert(myTempDate.getDate());*/
	   if((currentMonth==myTempMonth)&&(currentdate_array[3]==myTempDate.getFullYear()))
		   {
		    if(currentdate_array[2]>myTempDate.getDate())
		     {
				//alert("1");
			lessdate=true;
             }
			 else
			 {
			lessdate=false;
		     }
		 }
	   else
		  {
			 /*if(currentMonth>myTempMonth)
			  {
				 alert("in month loop");
			  }*/
			 /*  alert(currentMonth);
			 alert(myTempMonth);
			 alert(currentdate_array[3]);
			 alert(iyear);*/
		   if((parseInt(currentMonth) > parseInt(myTempMonth))&&(currentdate_array[3]==myTempDate.getFullYear()))
		        {
			 // alert("2");
    	       lessdate=true;
                }
	       }//For month end
	  }//For Year End
	else
	{
		//alert("3");
	 lessdate=true;
	}
	//alert(lessdate);
	//alert("next month");
	//alert(lessdate);
	// main validation end for second month table

        if(sldate.toDateString() == myTempDateSecd.toDateString()){
          txt = "style='cursor:hand;color:#B7C4C8;background-color:#B7C4C8'";
        }else{
          txt= "style='cursor:hand;border-bottom:1px solid " + bordercolor + ";border-right:0px solid " + bordercolor + ";'";
        }

        //WeekDay
        if((myTempDate.getDate() == 1))
			{
          if((j == myTempDate.getDay()))
			  {
            if((myTempDate.getDay() == 0) || (myTempDate.getDay() == 6))
				{
              //Check Min Max Dates
              if ((myTempDate >= mindate) && (myTempDate <= maxdate))
				  {

				   if(lessdate==true)
					{
					   retHTML += "<td " + txt + " align=center onClick=\"javascript:setMessage('" + checkflag + "');\" onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' href=\"javascript:setMessage('" + checkflag + "');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					  // retHTML += "<td " + txt + " align=center  onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none'><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					}
					else
					{
                  retHTML += "<td " + txt + " align=center onClick=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\" onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' href=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					}
              }
			  else
				  {
                  retHTML += "<td " + txt + " align=center><font color=#cccccc>" + counter + "</td>";
              }
              myTempDate.setDate(myTempDate.getDate() + 1);
              counter++;
            }else{
              //Check Min Max Dates
              if ((myTempDate >= mindate) && (myTempDate <= maxdate)){
				   if(lessdate==true)
					{
					   retHTML += "<td " + txt + " align=center onClick=\"javascript:setMessage('" + checkflag + "');\" onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' href=\"javascript:setMessage('" + checkflag + "');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					  // retHTML += "<td " + txt + " align=center onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' ><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					}
					else
					{
                  retHTML += "<td " + txt + " align=center onClick=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\" onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' href=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
				    }
              }else{
                  retHTML += "<td " + txt + " align=center><font color=#cccccc>" + counter + "</td>";
              }
              myTempDate.setDate(myTempDate.getDate() + 1);
              counter++;
            }
          }else{
            retHTML += "<td style='border-bottom:1px solid " + bordercolor + ";border-right:1px solid " + bordercolor + ";'>&nbsp;</td>";
          }
        }else{
          if((myTempDate.getDay() == 0) || (myTempDate.getDay() == 6)){
            //Check Min Max Dates
              if ((myTempDate >= mindate) && (myTempDate <= maxdate)){
				   if(lessdate==true)
					{
           			 retHTML += "<td " + txt + " align=center onClick=\"javascript:setMessage('" + checkflag + "');\" onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' href=\"javascript:setMessage('" + checkflag + "');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					//   retHTML += "<td " + txt + " align=center onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' ><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					}
					else
					{
                  retHTML += "<td  " + txt + " align=center onClick=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\" onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#ffffff'\"><a style='text-decoration:none' href=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
				    }
              }else{
                  retHTML += "<td " + txt + "  bgcolor='#ffffff' align=center><font color=#cccccc>" + counter + "</td>";
              }
            myTempDate.setDate(myTempDate.getDate() + 1);
            counter++;
          }else{
            //Check Min Max Dates
              if ((myTempDate >= mindate) && (myTempDate <= maxdate)){
				   if(lessdate==true)
					{
					   retHTML += "<td " + txt + " align=center onClick=\"javascript:setMessage('" + checkflag + "');\" onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' href=\"javascript:setMessage('" + checkflag + "');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					  // retHTML += "<td " + txt + " align=center onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' ><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
					}
					else
					{
                  retHTML += "<td " + txt + " align=center onClick=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\" onMouseOver=\"this.style.background='#B7C4C8'\" onMouseOut=\"this.style.background='#FFFFFF'\"><a style='text-decoration:none' href=\"javascript:setVal(" + imonth + "," + iyear + ",'" + selecteddate + "','" + object + "','" + output + "','" + output1 + "','" + (myTempDate.getMonth()+1) + "/" + myTempDate.getDate()  + "/" + myTempDate.getFullYear() + "','"+ cal +"','"+ fromdate +"','"+ todate +"');\"><table style='cursor:hand;' border=0 cellpadding=0 cellspacing=0><tr><td><font color=#19237E>" + counter + "</font></td></tr></table></a></td>";
				    }
              }else{
                  retHTML += "<td " + txt + " align=center><font color=#cccccc>" + counter + "</td>";
              }
            myTempDate.setDate(myTempDate.getDate() + 1);
            counter++;
          }
        }
      }
    }
    retHTML += "</tr>";
  }
  retHTML += "</table></td>";
	retHTML += "<tr><td style='border: 0px;' colspan='6' align='right'><span style='cursor: pointer;' onClick=\"javascript:   document.getElementById('"+cal+"').style.visibility = 'hidden';\">Close</span></td></tr>";
  retHTML += "</tr></table>";
  
  document.getElementById(object).innerHTML = retHTML;
}

function setVal(imonth, iyear, selecteddate , object, output ,output1, val,cal,fromdate,todate){	
  var dtExtract;
  dtExtract = val.split("/");
  dateVal = dtExtract[1]+'/'+dtExtract[0]+'/'+dtExtract[2];


  // dateVal = dtExtract[0]+'/'+dtExtract[1]+'/'+dtExtract[2];
 newdate1 = dtExtract[1]+"-"+MonthIndexConvertion(dtExtract[0]) + "-" + dtExtract[2];

var newd = new Date(M[imonth] + " " + dtExtract[1].toString() + ", " + iyear);
  
  document.getElementById(output).value = newdate1;
  //document.getElementById(output1).value = dateVal;
  
  //alert(document.getElementById(output).value);
  //alert(document.getElementById(output1).value)
	//  alert(dateVal);

		if(dateVal.length==8)
		{
			var	from=dateVal;
			var fromday=from.substring(0,1);
			var frommonth=from.substring(1,3);
			var fromyear=from.substring(3,8);
			fromday=fromday.replace("/","-");
			fromday='0'+fromday;
			frommonth=frommonth.replace("/","-0");
			fromyear=fromyear.replace("/","-");
			document.getElementById(fromdate).value=fromday+frommonth+fromyear;
			document.getElementById(todate).value=fromday+frommonth+fromyear;
		}
		else if(dateVal.length==9)
		{
			var	from=dateVal;
			var fromday=from.substring(0,2);
			var frommonth=from.substring(2,4);
			var fromyear=from.substring(4,9);
			if(fromday.charAt(1)=='/')
			{
					fromday=fromday.replace("/","-");
					fromday='0'+fromday;
					fromyear=fromyear.replace("/","-");
					document.getElementById(fromdate).value=fromday+frommonth+fromyear;
					document.getElementById(todate).value=fromday+frommonth+fromyear;
			}
			else
			{
					document.getElementById(fromdate).value=dateVal.replace("/","-0");
					document.getElementById(fromdate).value=document.getElementById(fromdate).value.replace("/","-"); 
					document.getElementById(todate).value=dateVal.replace("/","-0");
					document.getElementById(todate).value=document.getElementById(fromdate).value.replace("/","-"); 
					//alert(document.getElementById("fldfromdate1").value);
			}
			
		}
		else 
		{
			document.getElementById(fromdate).value=dateVal.replace("/","-");
			document.getElementById(fromdate).value=document.getElementById(fromdate).value.replace("/","-"); 			
		}

		if(dateVal.length==8)
		{
			var	to=dateVal;
			var today=to.substring(0,1);
			var tomonth=to.substring(1,3);
			var toyear=to.substring(3,8);
			today=today.replace("/","-");
			today='0'+today;
			tomonth=tomonth.replace("/","-0");
			toyear=toyear.replace("/","-");
			document.getElementById(todate).value=today+tomonth+toyear;
			
		}
		else if(dateVal.length==9)
		{
			//alert('9');
			var	to=dateVal;
			var today=to.substring(0,2);
			var tomonth=to.substring(2,4);
			var toyear=to.substring(4,9);
			if(today.charAt(1)=='/')
			{
					today=today.replace("/","-");
					today='0'+today;
					toyear=toyear.replace("/","-");
					document.getElementById(todate).value=today+tomonth+toyear;
			}
			else
			{
				document.getElementById(todate).value=dateVal.replace("/","-0");
				document.getElementById(todate).value=document.getElementById(todate).value.replace("/","-"); 
			}
			
		}
		else
		{
			document.getElementById(todate).value=dateVal.replace("/","-");
			document.getElementById(todate).value=document.getElementById(todate).value.replace("/","-");  
		}

 // alert(document.getElementById("fldtodate1").value);

 
 
  getCalenderTable(imonth, iyear, newd , object, output ,output1, val,cal,fromdate,todate);  
  
	document.getElementById(cal).style.visibility = 'hidden';	
}

function getMaxDays(imonth, iyear){
   switch(imonth){
    case 0: return 31;
    case 1:
      if ((iyear % 4) == 0) {
        if (!(iyear % 100 == 0 && iyear % 400 != 0))
        {
          return 29;
        }
        else
        {
          return 28;
        }}else{  return 28;}
    case 2: return 31;  //March
    case 3: return 30;  //April
    case 4: return 31;  //May
    case 5: return 30;  //June
    case 6: return 31;  //July
    case 7: return 31;  //August
    case 8: return 30;  //September
    case 9: return 31;  //October
    case 10: return 30;  //November
    case 11: return 31;  //December
  }
}

// Function for wrong date message start
function setMessage(checkflag)
{
	if(checkflag=="from" || checkflag=="fromjp")
	{
		alert("Select greater date than today's date");	  
	}
	else
    {
		alert("Select greater date than Leave date");	  
	}
}
// Function for wrong date message end

//Function for month name convertion start
function MonthConvertion(monthname)
{
	
	switch(monthname)
	{
     
		case 'Jan':
			 monthindex='1';
		break;
		case 'Feb':
			 monthindex='2';
		break;
		case 'Mar':
			 monthindex='3';
		break;
		case 'Apr':
			monthindex='4';
		break;
		case 'May': 
			monthindex='5';
		break;
		case 'Jun':
			monthindex='6';
		break;
		case 'Jul':
			monthindex='7';
		break;
		case 'Aug':
			monthindex='8';
		break;
		case 'Sep':
			monthindex='9';
		break;
		case 'Oct':
			monthindex='10';
		break;
		case 'Nov':
			monthindex='11';
		break;
		case 'Dec':
			monthindex='12';
		break;
	}
	return(monthindex);
	
}
function MonthIndexConvertion(monthindex)
{
	switch(monthindex)
	{
     
		case '1':
			 monthname='Jan';
		break;
		case '2':
			 monthname='Feb';
		break;
		case '3':
			 monthname='Mar';
		break;
		case '4':
			monthname='Apr';
		break;
		case '5': 
			monthname='May';
		break;
		case '6':
			monthname='Jun';
		break;
		case '7':
			monthname='Jul';
		break;
		case '8':
			monthname='Aug';
		break;
		case '9':
			monthname='Sep';
		break;
		case '10':
			monthname='Oct';
		break;
		case '11':
			monthname='Nov';
		break;
		case '12':
			monthname='Dec';
		break;
	}
	return(monthname);
	
}
//Function for month name convertion end

