// JavaScript Document
function isBrowserSupp() {
	// Get the version of the browser
	version =  parseFloat( navigator.appVersion );
	if ( ( version >= 2.0 ) && ( version < 2.1 ) && ( navigator.appName.indexOf( "Netscape" ) != -1 ) ) {
		return false;
	}else {
		return true;
	}
	return true;
	}
	
	function isLeapYear(yrStr) {
	var year = parseInt(yrStr, 10);
	// every fourth year is a leap year
	if ((year%4 == 0 && year%100 != 0) || year%400 == 0) {
	    leapYear=true;
	}
	else {
		leapYear=false;
	}
	return leapYear;
	}

function getDaysInMonth(Mth, Yr)
{
	if (Mth==4 || Mth==6 || Mth==9 || Mth==11){ 
		maxDays=30;
	}
	else if (Mth==2){
		if (isLeapYear(Yr)){
			maxDays=29;
		}
		else {
			maxDays=28;
		}
	}
	else
		var maxDays=31
	return maxDays;
}

//the function which does some magic to the date fields
// return non-zero if it is the last day of the month
function adjustDate(Mth){
	var today = new Date();
	if (12 < Mth.value) {
		theYear = (parseInt(today.getYear(), 10) + 1);
		MthValue = Mth.value-12;
	}
	else {
	   	theYear = parseInt(today.getYear(), 10);
		MthValue = Mth.value; 
	}
	var value = getDaysInMonth(MthValue, theYear);
    return value;
}

	//changes departure month when arrival month is changed
function amadChange(inM,inD,outM,outD){
	if (!isBrowserSupp()){
		return;
	}
	var maxDay = adjustDate(inM);
	if (inD.value < maxDay) {
		 outD.options.selectedIndex = inD.options.selectedIndex+1;
		 outM.options.selectedIndex = inM.options.selectedIndex;
	}
	else if (inD.value==maxDay) {
		 outD.options.selectedIndex = 0;
		 outM.options.selectedIndex=inM.options.selectedIndex + 1;
	}
	else {
		inD.options.selectedIndex = maxDay-1;
		outD.options.selectedIndex = 0;
		outM.options.selectedIndex=inM.options.selectedIndex + 1;
	}
	changeDay(inM.value, inD.value, outM.value, outD.value);
	return;
}

function changeOutDay(id, cid) {
	  var nowday = new Date();
	  var obj1 = $("#"+id);
	  var obj2 = $("#"+cid);
	  var fullDay = obj1.val().split('-');
	  var d = new Date(parseInt(fullDay[0],10),parseInt(fullDay[1],10)-1,parseInt(fullDay[2],10),0,0,0);
	  obj2.val(d.format('Y-m-d D'));
	  //obj2.datepicker('option','minDate',d);
	  setTimeout(function(){
          obj2.datepicker('option','minDate',d);
      }, 300);
}
	
function changeRoomNum(id, val) {
	var odj = document.getElementById(id);
	odj.length = val*2;
	var index = 0;
	for (var i=0;i<val;i++)
	{
	  odj.options[index].value = index+1; odj.options[index].text = index+1;
	  odj.options[index+1].value = index+2; odj.options[index+1].text = index+2;
	   index += 2;
	}
}