function getmth(mth){
	switch (mth)
	{
	   case "01" :
		  out_mth_no = 0;
		  break;
	   case "02" :
		  out_mth_no = 1;
		  break;
	   case "03" :
		  out_mth_no = 2;
		  break;
	   case "04" :
		  out_mth_no = 3;
		  break;
	   case "05" :
		  out_mth_no = 4;
		  break;
	   case "06" :
		  out_mth_no = 5;
		  break;
	   case "07" :
		  out_mth_no = 6;
		  break;
	   case "08" :
		  out_mth_no = 7;
		  break;
	   case "09" :
		  out_mth_no = 8;
		  break;
	   case "10" :
		  out_mth_no = 9;
		  break;
	   case "11" :
		  out_mth_no = 10;
		  break;
	   case "12" :
		  out_mth_no = 11;
		  break;
	} 
	return out_mth_no;
}

function getfullmth(mth){
	switch (mth)
	{
	   case "01" :
		  out_mth_no = "JAN";
		  break;
	   case "02" :
		  out_mth_no = "FEB";
		  break;
	   case "03" :
		  out_mth_no = "MAR";
		  break;
	   case "04" :
		  out_mth_no = "APR";
		  break;
	   case "05" :
		  out_mth_no = "MAY";
		  break;
	   case "06" :
		  out_mth_no = "JUN";
		  break;
	   case "07" :
		  out_mth_no = "JUL";
		  break;
	   case "08" :
		  out_mth_no = "AUG";
		  break;
	   case "09" :
		  out_mth_no = "SEP";
		  break;
	   case "10" :
		  out_mth_no = "OCT";
		  break;
	   case "11" :
		  out_mth_no = "NOV";
		  break;
	   case "12" :
		  out_mth_no = "DEC";
		  break;
	} 
	return out_mth_no;
}

function calnites(){
	var noOfnites = 0;
	
	input_out_date = document.roomrate_form.depart_date.value;
	input_in_date = document.roomrate_form.arrival_date.value;
		
	split_out_date = input_out_date.split("/")
	split_in_date = input_in_date.split("/")
	
	today=new Date()
	var in_date=new Date(split_in_date[2], getmth(split_in_date[1]), split_in_date[0]) //Month is 0-11 in JavaScript
	var out_date=new Date(split_out_date[2], getmth(split_out_date[1]), split_out_date[0]) //Month is 0-11 in JavaScript
	//if (today.getMonth()==11 && today.getDate()>25) //if Christmas has passed already
	//christmas.setFullYear(christmas.getFullYear()+1) //calculate next year's Christmas
	//Set 1 day in milliseconds
	var one_day=1000*60*60*24
		
	//Calculate difference btw the two dates, and convert to days
	noOfnites = Math.ceil((out_date.getTime()-in_date.getTime())/(one_day));	
	
	//document.roomratedisplay_form.noOfnites1.value = noOfnites;
	var inputElement = document.getElementById('noOfnites');
	
	if (noOfnites > 1){
		night = "Geceleme";
	} else {
		night = "Gece";
	}
	
	inputElement.innerHTML = noOfnites + " " + night;
	//PopulateForm();
	return noOfnites;
}

function getArriveDate(){
	var arrive_date = document.roomrate_form.arrival_date.value;
	var split_in_date = arrive_date.split("/");
	
	var in_year = split_in_date[2];
	var in_mth = getmth(split_in_date[1]);
	var in_day = split_in_date[0];
	var arrive_date1 = new Date(in_year, in_mth, in_day);
	
	return arrive_date1;
}

function getDepartDate(){
	var depart_date = document.roomrate_form.depart_date.value;
	var split_out_date = depart_date.split("/");
	
	var out_year = split_out_date[2];
	var out_mth = getmth(split_out_date[1]);
	var out_day = split_out_date[0];
	var depart_date1 = new Date(out_year, out_mth, out_day);
	
	return depart_date1;
}

function getTodayDate(){
	var td = new Date();
	var today_day = td.getDate();
	var today_mth = td.getMonth();
	var today_year = td.getFullYear();
	var today_date = new Date(today_year, today_mth, today_day);
	
	return today_date;
}

function getFormatDate(input_date){
	temp_day = input_date.getDate();
			
	if (temp_day < 10){
		mystring1 = new String("0")
		mystring2 = new String(temp_day)
		temp_day = mystring1.concat(mystring2);
	}

	temp_mth = input_date.getMonth()+1;
	
	if (temp_mth < 10){
		mystring1 = new String("0")
		mystring2 = new String(temp_mth)
		temp_mth = mystring1.concat(mystring2);
	}
				
	temp_year = input_date.getFullYear();
	temp_date = temp_day+'/'+temp_mth+'/'+temp_year;
	
	return temp_date;
}

function checkarrivedate(){
					
	var arrive_date = getArriveDate();
	var depart_date = getDepartDate();
	var today_date = getTodayDate();
	
	// sets the arrive date to current date if date input is less then current date
	if (arrive_date < today_date){
		temp_today_date = getFormatDate(today_date);
		document.roomrate_form.arrival_date.value = temp_today_date;
	}
	
	if (depart_date != ""){
		// if arrival date is greater then the departure date, set departure date to 1 day after arrival date
		arrive_date = getArriveDate();
		document.roomrate_form.arrival_date.value = getFormatDate(arrive_date);
		
		if (arrive_date > depart_date){
						
			var temp_depart_date = arrive_date;
			with (temp_depart_date) setDate(getDate()+1);
			
			temp_depart_date1 = getFormatDate(temp_depart_date);
			document.roomrate_form.depart_date.value = temp_depart_date1;
		}
	}
	
	calnites();
}

function checkdepartdate(){
	
	var arrive_date = getArriveDate();
	var depart_date = getDepartDate();
	var today_date = getTodayDate();
	
	// sets the departure date to current date if date input is less then current date
	if (depart_date < today_date){
		temp_today_date = getFormatDate(today_date);
		document.roomrate_form.depart_date.value = temp_today_date;
	}
	
	if (arrive_date != ""){
		depart_date = getDepartDate();
		document.roomrate_form.depart_date.value = getFormatDate(depart_date);

		if (arrive_date > depart_date){
			var temp_arrive_date = depart_date;
						
			if (depart_date > today_date){
				with (temp_arrive_date) setDate(getDate()-1);
			}
			temp_arrive_date1 = getFormatDate(temp_arrive_date);
			document.roomrate_form.arrival_date.value = temp_arrive_date1;
		}
	}
	
	calnites();
}

function verify1(form)
{
	msg = "";
	msg1 = "";
	err = 0;
	
	if (document.roomrate_form.hotel.value == ""){ 
		msg = "Hotel Destination\n";
		err = 1; 
	}
	
	if (document.roomrate_form.arrival_date.value == ""){ 
		msg = "Arrival\n";
		err = 1; 
	}
	
	if (document.roomrate_form.depart_date.value == ""){ 
		msg = "Departure\n";
		err = 1; 
	}
	
	if (err == 1) {
		alert("The following field(s) are incomplete\n\n" + msg + "\nPlease complete the form and submit again.");
		return false;
	} 
	return true;
}

function redirect(){
	if (verify1(document.roomrate_form) == true){
		
		document.roomrate_form.property.value = document.roomrate_form.hotel.value;				
		document.roomrate_form.guests.value = document.roomrate_form.noguests.value; 
				
		return true;
	} 
	return false;
}
