// JavaScript Document
var GnewyearID = 2000;  //not sure if the number is important resubmit()//
var error = false; 
var cal22 = new CalendarPopup("calBook");
document.write(getCalendarStyles());
cal22.showNavigationDropdowns();
cal22.setYearSelectStartOffset(2)
cal22.setReturnFunction("setMultiplevalues22");
/*
document.write(getCalendarStyles());
cal22.showYearNavigation();
cal22.setReturnFunction("setMultiplevalues22");
*/
var cal23 = new CalendarPopup("calBook");
cal23.showNavigationDropdowns();
cal23.setYearSelectStartOffset(2)
cal23.setReturnFunction("setMultiplevalues23");
/*
document.write(getCalendarStyles());
cal23.showYearNavigation();
cal23.setReturnFunction("setMultiplevalues23");
*/

var m_names = new Array("January", "February", "March", 
"April", "May", "June", "July", "August", "September", 
"October", "November", "December");

function calcDate(what, addIt) {
    var TimezoneOffset = -10  // adjust for time zone new Date(today.getYear(), today.getMonth(), today.getDate()-0+interval);
  	var localTime = new Date()
  	var ms = localTime.getTime() 
             + (localTime.getTimezoneOffset() * 60000)
             + TimezoneOffset * 3600000
  	var dateHI =  new Date(ms) 
	switch(what){
		case "y": {what = dateHI.getFullYear(); break;}
		case "m": {what = dateHI.getMonth(); break;}
		case "d": {what = dateHI.getDate(); break;}
		case "today": {what = dateHI; break;};
		case "indelay": {what = new Date(dateHI.getFullYear(), dateHI.getMonth(), dateHI.getDate() + addIt); break;};
	}
	return what;
}

//Called by form onsubmit page to do the actual search // 
function checkDate () {
	document.getElementById('formResponse').innerHTML = '';
	var url = 'kohala-vacation/SEARCH-RENTAL-CALENDAR.HTML';
	var inDate  = GformElements["date12_year"].value+"/"+GformElements["date12_month"].value+"/"+GformElements["date12_day"].value;
	var outDate = GformElements["date12_dyear"].value+"/"+GformElements["date12_nbrmonth"].value+"/"+GformElements["date12_dday"].value;
	var nights = GformElements["nbrNights"].value;	
	if(nights >= GminNights) {
		/*AjaxRequest.js*/
		startLoading();
		AjaxRequest.post(	  
			{
				'url':url
				,'parameters':{'indate':inDate, 'outdate':outDate}
				,'onSuccess':function(req) { finishLoading(); document.getElementById('formResponse').innerHTML = req.responseText; }
			});
	} else if(nights <=0 ){
		alert("Plase recheck your dates, your departure date is before your arival date");
	} else {
		alert("Please recheck your dates, there is a 5 night minimun");
	}
}
function startLoading() {
	document.getElementById('mainAreaLoading').style.display = 'block';
	document.getElementById('mainAreaInternal').style.display = 'none';
}
function finishLoading() {
  document.getElementById('mainAreaInternal').style.display = 'block';
  setTimeout("document.getElementById('mainAreaLoading').style.display = 'none'",1000)
}

//Called by form onsubmit  to call search page//
function searchRentals () { 
var nights = GformElements["nbrNights"].value;
	if(error){
		alert("Sorry unable to take a booking this far in advance for your check-in date."+ '\n' + "Please recheck your dates."); 
		return;
	}else if(nights >= GminNights) {
		document.forms["bookingform"].action = "/kohala-vacation/available-condos.html";
		document.getElementById("bookingform").submit();
	} else if(nights <=0 ){
		alert("Plase recheck your dates, your departure date is before your arival date");
	} else {
		alert("Please recheck your dates, there is a 5 night minimun");
	}
}

// send selected unit information to vacation rental form//
function bookunit(unitNbr){
	//var unitNbr = unitNbr.replace(/_/g, " ");
	GformElements["rentalUnit"].value = unitNbr;
	document.forms["bookingform"].action = "kohala-vacation/vacation-request-form/18/rental-inquiry.html";
	document.getElementById("bookingform").submit();
}


/* Called when numbers are entered in "Number of nights" text box */
function checkEnter(e){ 
	var characterCode 
	if(e && e.which){ 
		e = e;
		characterCode = e.which;
	}else{
		e = event;
		characterCode = e.keyCode;
	}
	if(characterCode == 13){ 
		resubmit()
		return false;
	}
	else{
		return true;
	}
}

function getCheckindate() {
var newMonth = GformElements["date12_month"].value;
var newDay = GformElements["date12_day"].value;
var newYear = GformElements["date12_year"].value;
var checkin = new Date(newYear,newMonth-1,newDay);
var nights = Number(GformElements["nbrNights"].value);
return {c:checkin,y:newYear,m:newMonth,d:newDay,n:nights};
}

function getCheckoutdate() {
var newMonth = GformElements["date12_nbrmonth"].value;
var newDay = GformElements["date12_dday"].value;
var newYear = GformElements["date12_dyear"].value;
var checkout = new Date(newYear,newMonth-1,newDay);
var nights = Number(GformElements["nbrNights"].value);
return {c:checkout,y:newYear,m:newMonth,d:newDay,n:nights};
}

//Returns the last day of the month??
function daysInMonth(month,year) {
var dd = new Date(year, month, 0);
return dd.getDate();
} 

/*called by form onChange*/
function resubmit() { 
var inElements = getCheckindate();
var outElements = getCheckoutdate();
var dt = new Date();
var checkin = new Date(inElements.y,inElements.m-1,inElements.d);
chkdate = chkinDate(inElements.y,inElements.m,inElements.d);
checkout(inElements.c);

//alert(newDay +" - " + newMonth + " - " + newYear);
//alert(GformElements["date12_nbrmonth"].value);

// If check-in is too far out exit//
	if(!chkdate ){return;}
	//if(inElements.m == (dt.getMonth()+1) && inElements.y == dt.getFullYear() && inElements.d < dt.getDate()+3) {
	if(inElements.m == newMonth && inElements.y == newYear && inElements.d < newDay) {
		document.getElementById("dispmsg").innerHTML = "Your Arrival Date must be at least 3 days from today's date!";
		error = true;
		//return;
	//}else if(inElements.m < (dt.getMonth()+1) && inElements.y == dt.getFullYear()) {
	}else if(inElements.m < newMonth && inElements.y == newYear) {
		document.getElementById("dispmsg").innerHTML = "Your Arrival Date must be at least 3 days from today's date!";
		error = true;
		//return;	
	//}else if(GnewyearID >= inElements.y && inElements.m  < (dt.getMonth()+1) && inElements.y  == dt.getFullYear()){
	}else if(GnewyearID >= inElements.y && inElements.m  < newMonth && inElements.y  == newYear){
		document.getElementById("dispmsg").innerHTML = "Sorry your Dates are not available.<br />Your Arrival Date must be at least 3 days from today's date!";
		error = true;
		//return;
	}else if(inElements.n < GminNights) {
		document.getElementById("dispmsg").innerHTML = "Sorry we have a "+ GminNights +" might minimun stay!";
		error = true;
		//return;
	}else if(inElements.n < 7) {
		document.getElementById("dispmsg").innerHTML = "Discounts given for longer stays";
		error = true;
		//return;
	}else if(GminNights == inElements.n) {
		document.getElementById("dispmsg").innerHTML = "Minimun Stay<br />Discounts given for longer stays";
		error = true;
		//return;
	}else if(inElements.n >= 7) {
		document.getElementById("dispmsg").innerHTML = "Call for special discount offers";
		error = true;
		//return;
	}
	error = false;
/*
if(outElements.c > inElements.c) {
	alert('1st ok  '+inElements.c +'  ' + outElements.c + '  ' +outElements.m);
}else{
	alert('else  '+inElements.c +'  ' + outElements.c + '  ' +outElements.m);
}
*/
}

/* see if the requested check-in day is beyond 1.5 years from today */
function chkinDate(y,m,d){
var checkIn = new Date(y,(m-1),d,00,00,00);
var maxDate = new Date();
maxDate = new Date(maxDate.getFullYear(),maxDate.getMonth(),(maxDate.getDate()+GmaxDays));
	if(checkIn > maxDate)  /* check-in date checked to see if exceeds 1 1/2 years from today*/
	{
		alert("Sorry unable to take a booking this far in advance for your check-in date of " + d + "-" + m_names[checkIn.getMonth()]  + "-" + y);
		document.getElementById("dispmsg").innerHTML = "Sorry unable to take a booking this far in advance for your check-in date of " + d + "-" + m_names[checkIn.getMonth()]  + "-" + y;
		error = true;
		return false; 
	}else{
		error = false;
		return true;
	}
}

// calculate difference in days between two dates//
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
function daysElapsed(date1,date2) {
    var difference =
        Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0)
      - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);
    return difference/1000/60/60/24;
}
//Check-out date//
function checkout(chkIn) {
var chknights = Number(GformElements["nbrNights"].value); 
outDate = new Date(chkIn.getFullYear(),chkIn.getMonth(),(chkIn.getDate()+chknights));
var month = Number(outDate.getMonth());
//GformElements["monthNbr"].value = month +1;
GformElements["date12_dyear"].value = outDate.getFullYear();
GformElements["date12_dmonth"].value =  m_names[month];
GformElements["date12_dday"].value = outDate.getDate();
GformElements["date12_nbrmonth"].value = month + 1;
}

//checkin date shown in form//
function checkinDate() {
var monthInput = GformElements["date12_month"].value;
var dayInput = GformElements["date12_day"].value;
var yearInput = GformElements["date12_year"].value;
var inDate = new Date(yearInput,(monthInput-1),dayInput);
return inDate;
}


/*places the select dates from popup calendar into form*/
function setMultiplevalues22(y,m,d) {
var inYear = GformElements["date12_year"].value;
var inMonth = GformElements["date12_month"].value;
var inDay = GformElements["date12_day"].value;
var dt = new Date();
var dtToday = new Date(dt.getFullYear(),dt.getMonth(),(dt.getDate()+3));
var inDate = new Date(y,(m-1),d);
var chkdate = chkinDate(y,(m),d);
	if(!chkdate ){return;}
	checkout(inDate);
	GformElements["date12_year"].value = y;
	GformElements["date12_month"].value = m;
	GformElements["date12_day"].value = d;
	if(inDate < dtToday) {
		error = true;
		alert("Your Arrival Date must be at least 3 days from today's date with a 5 night minimum stay.");
		//document.getElementById("dispmsg").innerHTML = "Your Arrival Date must be at least 3 days from today's date!";
		GformElements["date12_year"].value = inYear;
		GformElements["date12_month"].value = inMonth;
		GformElements["date12_day"].value = inDay;
		resubmit()
	}else{ 
		resubmit()
		error = false;
	}
}
/*places the select dates from popup calendar into form*/
function setMultiplevalues23(y,m,d) {
var dt = new Date();
var dtToday = new Date(dt.getFullYear(),dt.getMonth(),(dt.getDate()+3));
var inDate = new Date(y,(m-1),d);
var inYear = GformElements["date12_year"].value;
var inMonth = GformElements["date12_month"].value;
var inDay = GformElements["date12_day"].value;
var outyear =GformElements["date12_dyear"].value;
var outMonth = GformElements["date12_dmonth"].value ;
var outDay = GformElements["date12_dday"].value;
var orgNights = GformElements["nbrNights"].value;
chkdate = chkinDate(y,(m),d);
	if(!chkdate ){return;}
	GformElements["date12_dyear"].value = y;
	GformElements["date12_dmonth"].value = m_names[m-1];
	GformElements["date12_dday"].value = d;
	if(inDate < dtToday) {
		error = true;
		alert("Your Arrival Date must be at least 3 days from today's date with a 5 night minimum stay.");
  		GformElements["date12_dyear"].value = outyear;
  		GformElements["date12_dmonth"].value = outMonth ;
  		GformElements["date12_dday"].value = outDay;
	}else{ 
		
		var newNights =  daysElapsed(new Date(y,m-1,d),new Date(inYear,inMonth-1,inDay));
		if (newNights >= 0) { 
			GformElements["nbrNights"].value = newNights;
		} else {
			alert("Your selected departure date is before your arrival date, please change your arrival date 1st.");
		/*
			GformElements["date12_year"].value = y;
			GformElements["date12_dmonth"].value = m_names[m + 1];
			GformElements["date12_dday"].value = d;
	*/
		}
		resubmit()
		error = false;
	}
}

/*Notes:
1) SetMultiplevalues changes the nbrNights which changes the check-out fields when resubmit() call checkout()
	This is the only process that automaticly changes the nbrNights field.
2) Manually changing  nbrNights calls to resubmit() which call checkout() to update the check-out fields.
3) Changing anyone of the check-in files call resubmit() which clls checkout() to update check-out fields.



*/

