var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var days = new Array("S", "M", "T", "W", "T", "F", "S");

today = new getToday();
var element_id;

function getDays(month, year)
 {
	// Test for leap year when February is selected.
	if (1 == month)
			return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
	else
		return daysInMonth[month];
  }

function getToday()
  {
    // Generate today's date.
	this.now = new Date();
	this.year = this.now.getFullYear(); // Returned year XXXX
	this.month = this.now.getMonth();
	this.day = this.now.getDate();
  }

function newCalendar()
 {
	var parseYear = parseInt(document.all.year[document.all.year.selectedIndex].text);
	var newCal = new Date(parseYear , document.all.month.selectedIndex, 1);
	var day = -1;
	var startDay = newCal.getDay();
	var daily = 0;
	today = new getToday(); // 1st call
	if ((today.year == newCal.getFullYear() ) &&   (today.month == newCal.getMonth()))
	   day = today.day;
	// Cache the calendar table's tBody section, dayList.
	var tableCal = document.all.calendar.tBodies.dayList;
	var intDaysInMonth=getDays(newCal.getMonth(), newCal.getFullYear() );
	for (var intWeek = 0; intWeek < tableCal.rows.length;  intWeek++)
		   for (var intDay = 0;intDay < tableCal.rows[intWeek].cells.length;intDay++)
	         {
		      var cell = tableCal.rows[intWeek].cells[intDay];
		        // Start counting days.
		       if ((intDay == startDay) && (0 == daily))
        		 daily = 1;
		  // Highlight the current day.)

     	  cell.style.color = (day == daily) ? "red" : "";
		  if(day == daily)
		   {
		   	document.all.todayday.innerText= "Today: " +  day + "/" + (newCal.getMonth()+1) + "/" + newCal.getFullYear();
		   }
		  // Output the day number into the cell.
		  if ((daily > 0) && (daily <= intDaysInMonth))
			 cell.innerText = daily++;
		  else
			 cell.innerText = "";
	   }
  }

 function getTodayDay()
	{
		document.all[element_id].value = today.day + "/" + (today.month+1) + "/" + today.year;
		//document.all.calendar.style.visibility="hidden";
		document.all.calendar.style.display="none";
		document.all.year.selectedIndex=100;
		document.all.month.selectedIndex = today.month;
	}

     function getDate()
		 {
            // This code executes when the user clicks on a day
            // in the calendar.
            if ("TD" == event.srcElement.tagName)
			    // Test whether day is valid.
               if ("" != event.srcElement.innerText)
			   {
				 var mn = document.all.month.selectedIndex+1;
    			 var Year = document.all.year[document.all.year.selectedIndex].text;
				 //document.all[element_id].value=event.srcElement.innerText+"/"+mn +"/"  +Year;
				 //document.all.calendar.style.visibility="hidden";
				 if(element_id=="year1")
				 {
				    document.getElementById("date1").value=event.srcElement.innerText;
				    document.getElementById("month1").value=mn;
				    document.getElementById("year1").value=Year;
					//shownight();
			     //document.all.calendar.style.visibility="hidden";
				 }
				if(element_id=="year2")
				 {
				   document.getElementById("date2").value=event.srcElement.innerText;
    			   document.getElementById("month2").value=mn;
				   document.getElementById("year2").value=Year;
				   //shownight();
				 }  
				document.all.calendar.style.display="none";
			 }
		 }

function GetBodyOffsetX(el_name, shift)
 {
	var x;
	var y;
	x = 0;
	y = 0;

	var elem = document.all[el_name];
	do
	 {
		x += elem.offsetLeft+7;
		y += elem.offsetTop-1;
		if (elem.tagName == "BODY")
			break;
		elem = elem.offsetParent;
	 } while  (1 > 0);

	shift[0] = x;
	shift[1] = y;
	return  x;
}

function SetCalendarOnElement(el_name)
 {
	if (el_name=="")
	el_name = element_id;
	var shift = new Array(2);
	GetBodyOffsetX(el_name, shift);
	document.all.calendar.style.pixelLeft  = shift[0]; //  - document.all.calendar.offsetLeft;
	document.all.calendar.style.pixelTop = shift[1] + 25 ;
 }
function ShowCalendar(elem_name)
 {
		if (elem_name=="")
		elem_name = element_id;
		element_id	= elem_name; // element_id is global variable
		newCalendar();
		SetCalendarOnElement(element_id);
		//document.all.calendar.style.visibility = "visible";
		document.all.calendar.style.display="inline";
 } 
function HideCalendar()
 {
	//document.all.calendar.style.visibility="hidden";
	document.all.calendar.style.display="none"; 
 }

function toggleCalendar(elem_name)
{
	//if (document.all.calendar.style.visibility == "hidden")
	if(document.all.calendar.style.display=="none")
		ShowCalendar(elem_name);
	else
		HideCalendar();
}


function dtoday()
 {
   var dd=new Date();
   var yy=dd.getYear();
   var mm=dd.getMonth()+1;
   
   var dat=dd.getDate();

   document.form1.year1.value=yy;
   document.form1.month1.value=mm;
   document.form1.date1.value=dat;
  
   dd.setDate(dd.getDate()+1)
   
   document.form1.year2.value=dd.getYear();
   document.form1.month2.value=dd.getMonth()+1;
   document.form1.date2.value=dd.getDate();
 }
function changevalue()
 {
  var ad=document.form1.adults.value
  var ad1=ad-0
  var j=4-ad1
  removeAllOptions(document.form1.childs)
  for(var i=0;i<=j;i++)
   {
    addOption(document.form1.childs, i, i);
   }
//  alert(i)
 }

function addOption(selectbox,text,value)
 {
   var optn = document.createElement("OPTION");
   optn.text = text;
   optn.value = value;
   selectbox.options.add(optn);
 }

function removeAllOptions(selectbox)
 {
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		selectbox.remove(i);
	}
}

function shownight()
 {
  var yy=document.form1.year1.value
  var mm=document.form1.month1.value
  var dd=document.form1.date1.value

  var yy1=document.form1.year2.value
  var mm1=document.form1.month2.value
  var dd1=document.form1.date2.value
  var one_day=1000*60*60*24
  
  mydate=new Date(yy,mm-1,dd)
  checkout=new Date(yy1,mm1-1,dd1)
  night=Math.ceil((checkout.getTime()-mydate.getTime())/one_day)
  
  document.form1.nights.value=night
  removeAllOptions(document.form1.nights)
  addOption(document.form1.nights, night, night);
 //alert(night)
 //return false
}
function calculate()
 {
  var yy=document.form1.year1.value
  var mm=document.form1.month1.value
  var dd=document.form1.date1.value

  var yy1=document.form1.year2.value
  var mm1=document.form1.month2.value
  var dd1=document.form1.date2.value
  
    
  var mv=daysInMonth[mm-1]
  var mv1=daysInMonth[mm1-1]
  
 // alert(mm)
  
  if(mm==2)
   {
    
     if((yy%4==0) && ((yy%100!=0) || (yy%400==0)))
	  {
	   mv=29
	  }
   }
   if(mm1==2)
   {
     if((yy1%4==0) && ((yy1%100!=0) || (yy1%400==0)))
	  {
	   mv1=29
	  }
   } 

  //alert(mv+" "+mv1)
  
  if(dd>mv || dd1>mv1)
   {
     alert("invalid date")
	 return false
   }
  else
   {

   mydate=new Date(yy,mm-1,dd)
     var odate=new Date();
     odate1=new Date(odate.getYear(),odate.getMonth(),odate.getDate())
     checkout=new Date(yy1,mm1-1,dd1)

  if(checkout < mydate )
   {
     alert("Pls select valid date");
	 return false
   }
 
  if(mydate < odate1)
   {
     alert("Pls select valid date");
	 return false
   }
  }
  

  
 // return false
 
   
 }
