//form objects
var frm;
var monthInObj;
var dateInObj;
var yearInObj;
var monthOutObj;
var dateOutObj;
var yearOutObj;
var checkInDate;
var checkOutDate;
var checkInTime;
var checkOutTime;

function initFields( type ){ //only used for checkIn/Out selectors
	//set form object
	frm = ( type == "step3" ) ? document.forms.respath_checkAvailabilityForm : document.forms[0];
	//set form elements
	if( type == "earlyAvail" ){
		monthInObj = frm['availability.palsra_IND_M'];
		dateInObj = frm['availability.palsra_IND_D'];
		yearInObj = frm['availability.palsra_IND_Y'];
		monthOutObj = frm['availability.palsra_OTD_M'];
		dateOutObj = frm['availability.palsra_OTD_D'];
		yearOutObj = frm['availability.palsra_OTD_Y'];
	}
	else{ //type == regular
		monthInObj = frm['palsra_IND_M'];
		dateInObj = frm['palsra_IND_D'];
		yearInObj = frm['palsra_IND_Y'];
		monthOutObj = frm['palsra_OTD_M'];
		dateOutObj = frm['palsra_OTD_D'];
		yearOutObj = frm['palsra_OTD_Y'];
	}
}

function init_Reservation_Fields( type ){ //only used for checkIn/Out selectors
	//set form object
	frm = ( type == "step3" ) ? document.forms.respath_checkAvailabilityForm : document.forms[1];
	//set form elements
	if( type == "earlyAvail" ){
		monthInObj = frm['availability.palsra_IND_M'];
		dateInObj = frm['availability.palsra_IND_D'];
		yearInObj = frm['availability.palsra_IND_Y'];
		monthOutObj = frm['availability.palsra_OTD_M'];
		dateOutObj = frm['availability.palsra_OTD_D'];
		yearOutObj = frm['availability.palsra_OTD_Y'];
	}
	else{ //type == regular
		monthInObj = frm['palsra_IND_M'];
		dateInObj = frm['palsra_IND_D'];
		yearInObj = frm['palsra_IND_Y'];
		monthOutObj = frm['palsra_OTD_M'];
		dateOutObj = frm['palsra_OTD_D'];
		yearOutObj = frm['palsra_OTD_Y'];
	}
}

function initParameters( type ){ //only used for checkIn/Out selectors
	//set form object
	frm = document.forms[0];
	//set form elements
	if( type == "earlyAvail" ){
		monthInObj = frm['montharrival'];
		dateInObj = frm['dayarrival'];
		yearInObj = frm['yeararrival'];
		monthOutObj = frm['monthdep'];
		dateOutObj = frm['daydep'];
		yearOutObj = frm['yeardep'];
	}
	else{ //type == regular
		monthInObj = frm['specialsDealsForm.montharrival'];
		dateInObj = frm['specialsDealsForm.dayarrival'];
		yearInObj = frm['specialsDealsForm.yeararrival'];
		monthOutObj = frm['specialsDealsForm.monthdep'];
		dateOutObj = frm['specialsDealsForm.daydep'];
		yearOutObj = frm['specialsDealsForm.yeardep'];
	}
}
function setFields(){ //set fields to match LQ cookies
    if( getCookie("lqarrmonth") ){
        monthInObj.value = getCookie("lqarrmonth");
        dateInObj.value = getCookie("lqarrday");
        monthOutObj.value = getCookie("lqdepmonth");
        dateOutObj.value = getCookie("lqdepday");
    }
}
//calendar launcher
function launchCalendar( url, frm, monthObj, dateObj, yearObj, mode, brand ){

        if(document.luxuryResortRedeemForm != null)
        {
                monthInObj = luxuryResortRedeemForm.arriveMonth;
                monthOutObj = luxuryResortRedeemForm.departMonth;
                dateInObj = luxuryResortRedeemForm.arriveDay;
                dateOutObj = luxuryResortRedeemForm.departDay;
                yearInObj = luxuryResortRedeemForm.arriveYear;
                yearOutObj = luxuryResortRedeemForm.departYear;
        }

	if( monthObj.value != "" && dateObj.value != "" ){
		var ds = monthObj.value + "/" + dateObj.value + "/" + returnYear(yearObj);
		var dt = new Date( ds );
		url = url + '&date=' + dt.getTime();
	}
	url = url + '&formName=' + frm.name + '&brand=' + brand;
	
	popWin( url, 'calendarWin', 260, 200 );
	return false;
}
function returnYear( yearObj ){ //returns current year if isn't selected yet
	if( yearObj.value != "" ) return yearObj.value;
	return new Date().getFullYear();
}

//check and move dates
function checkDates( mode ){

	if(document.luxuryResortRedeemForm != null)
	{
		monthInObj = luxuryResortRedeemForm.arriveMonth;
		monthOutObj = luxuryResortRedeemForm.departMonth;
		dateInObj = luxuryResortRedeemForm.arriveDay;
		dateOutObj = luxuryResortRedeemForm.departDay;
		yearInObj = luxuryResortRedeemForm.arriveYear;
		yearOutObj = luxuryResortRedeemForm.departYear;
	}

	var tempDate = new Date();
	var now = new Date( (tempDate.getMonth()+1) +"/"+ tempDate.getDate() +"/"+ tempDate.getFullYear() );
	//checkIn/Out times
	checkInDate = new Date( ""+ monthInObj.value +"/"+ dateInObj.value +"/"+ yearInObj.value );
	checkOutDate = new Date( ""+ monthOutObj.value +"/"+ dateOutObj.value +"/"+ yearOutObj.value );
	//set years if empty
	if( yearInObj.value == "" || yearInObj.value == null ) yearInObj.value = now.getFullYear();
	if( yearOutObj.value == "" || yearOutObj.value == null ) yearOutObj.value = now.getFullYear();
	//if both checkIn month and date are set, make sure checkOut dates are at least 1 day ahead
	if( mode == "checkIn" && monthInObj.value != "" && dateInObj.value != "" ){
		//process years
		if( checkInDate.getMonth() >= now.getMonth() ) yearInObj.value = now.getFullYear();
		else yearInObj.value = now.getFullYear()+1;
		resetTimes();
		//if no checkOut date or checkOut < checkIn, moveDate()
		if( !checkOutTime || checkOutTime<=checkInTime ) moveDate( mode );
	}
	//else if checkOut month and date are set, make sure checkIn dates are at least 1 day behind
	else if( monthOutObj.value != "" && dateOutObj.value != "" ){ //mode == checkOut
		//process years
		if( checkOutDate.getMonth() >= now.getMonth() ) yearOutObj.value = now.getFullYear();
		else yearOutObj.value = now.getFullYear()+1;
		resetTimes();
		//if no checkIn date or checkIn > checkOut, moveDate()
		if( !checkInTime || checkInTime>=checkOutTime ) moveDate( mode );
	}
}
function resetTimes(){


        if(document.luxuryResortRedeemForm != null)
        {
                monthInObj = luxuryResortRedeemForm.arriveMonth;
                monthOutObj = luxuryResortRedeemForm.departMonth;
                dateInObj = luxuryResortRedeemForm.arriveDay;
                dateOutObj = luxuryResortRedeemForm.departDay;
                yearInObj = luxuryResortRedeemForm.arriveYear;
                yearOutObj = luxuryResortRedeemForm.departYear;
        }

	checkInTime = new Date( ""+ monthInObj.value +"/"+ dateInObj.value +"/"+ yearInObj.value ).getTime();
	checkOutTime = new Date( ""+ monthOutObj.value +"/"+ dateOutObj.value +"/"+ yearOutObj.value ).getTime();

}
function moveDate( mode ){



        if(document.luxuryResortRedeemForm != null)
        {
                monthInObj = luxuryResortRedeemForm.arriveMonth;
                monthOutObj = luxuryResortRedeemForm.departMonth;
                dateInObj = luxuryResortRedeemForm.arriveDay;
                dateOutObj = luxuryResortRedeemForm.departDay;
                yearInObj = luxuryResortRedeemForm.arriveYear;
                yearOutObj = luxuryResortRedeemForm.departYear;
        }



	if( mode == "checkIn" ){
		var newMonth = parseInt(monthInObj.value, 10);
		var newDate = parseInt(dateInObj.value, 10)+1;
		var newYear = parseInt(yearInObj.value, 10);
		var newCheckOut = new Date( newMonth +'/'+ newDate +'/'+ newYear );
		monthOutObj.value = getTwoDigit( newCheckOut.getMonth()+1 );
		dateOutObj.value = getTwoDigit( newCheckOut.getDate() );
		yearOutObj.value = newCheckOut.getFullYear();
	}
	else{ //mode == checkOut
		var newMonth = parseInt(monthOutObj.value, 10);
		var newDate = parseInt(dateOutObj.value, 10)-1;
		var newYear = parseInt(yearOutObj.value, 10);
		var newCheckIn = new Date( newMonth +'/'+ newDate +'/'+ newYear );
		monthInObj.value = getTwoDigit( newCheckIn.getMonth()+1 ) ;
		dateInObj.value = getTwoDigit( newCheckIn.getDate() );
		yearInObj.value = newCheckIn.getFullYear();
	}
}
function getTwoDigit( i ){
	i = "" + i;
	if( i.length == 1 ) i = "0" + i;
	return i;
}
