
/** Updates the reservation data when the user has selected a date range. */
function reloadReservationData(reload){
	spIn = Page.FindControl("spCheckinDate");
	spInPayment = Page.FindControl("spCheckinDatePayment");
	spOut = Page.FindControl("spCheckoutDate");
	spOutPayment = Page.FindControl("spCheckoutDatePayment");



	if (curMin && curMax){
		spIn.innerHTML = months[curMin.getMonth()] + " " + curMin.getDate() + ", " + curMin.getFullYear();
		spInPayment.innerHTML = months[curMin.getMonth()] + " " + curMin.getDate() + ", " + curMin.getFullYear();

		spOut.innerHTML = months[curMax.getMonth()] + " " + curMax.getDate() + ", " + curMax.getFullYear();
		spOutPayment.innerHTML = months[curMax.getMonth()] + " " + curMax.getDate() + ", " + curMax.getFullYear();

		datediff = (curMax - curMin) / 60 / 60 / 24 / 1000;
		Page.FindControl("spQtyNights").innerHTML = datediff;
	}else{
		spIn.innerHTML = checkinStartValue;
		spInPayment.innerHTML = checkinStartValue;

		spOut.innerHTML = checkoutStartValue;
		spOutPayment.innerHTML = checkoutStartValue;

		Page.FindControl("spQtyNights").innerHTML = "0";
	}

	if (reload){
        loadrooms();
	}
}

/** Updates the quantities values from the data selected on the comboboxes */
function updateQuantities(){

	validateQuantities();

	Page.FindControl("spQtyRooms").innerHTML = Page.FindControl("qtyRooms").value;
	Page.FindControl("spQtyAdults").innerHTML = Page.FindControl("qtyAdults").value;
	Page.FindControl("spQtyChildren").innerHTML = Page.FindControl("qtyChildren").value;
/*
	Page.FindControl("spQtyNights")
	Page.FindControl("spQtyRooms")

*/


	calculatePrice();
}

/** Perform the person-quantity logic */
function validateQuantities(){
	var qtyPeople = parseInt(Page.FindControl("qtyAdults").value) + parseInt(Page.FindControl("qtyChildren").value);
	var qtyRooms = parseInt(Page.FindControl("qtyRooms").value);

	if (qtyPeople > (qtyRooms*2)){
		Modal.Window("index.php?module=modal_message&title=reservation.validation.qtypeople.title&message=reservation.validation.qtypeople&idHotel="+_idHotel);
		Page.FindControl("qtyRooms").value = Math.ceil((qtyPeople / 2));
	}

}


/** Calculates the price for the current reservation */
function calculatePrice(){
	qtyRooms = Page.FindControl("qtyRooms").value;

	datediff = (curMax - curMin) / 60 / 60 / 24 / 1000 + 1;

	qtyNights = datediff - 1;
	if (room){
		subtotal = qtyNights * room.price * qtyRooms;
		subtotal = parseFloat(subtotal.toPrecision(4));
		taxes = subtotal * room.taxes;
		taxes = parseFloat(taxes.toPrecision(4));
	}else{
		subtotal = 0;
		taxes = 0;
	}

	var amount = subtotal + taxes;
	amount = parseFloat(amount.toPrecision(4));

	Page.FindControl("spSubtotal").innerHTML = subtotal.toFixed(2);
	Page.FindControl("spTaxes").innerHTML = taxes.toFixed(2);
	Page.FindControl("spAmount").innerHTML = amount.toFixed(2);
}



/** validates and processes the reservation */
function processreservation(){

	Modal.Message("Please wait while we process your reservation.",200,70);

	var url = getSaveUrl();

	//Perform the validation
	xmlhttpPost(
		"index.php",
		"module=ajax_validatereservation"+url,
		"processValidation"
	);

}

/**
	Receives the data returned by the validation, On success, calls the ajax
	module that saves the reservation.
 */
function processValidation(res){
	var url = saveURL;
//	if (!res){
	if(res=='correct'){
		url = "module=ajax_savereservation"+url;


		xmlhttpPost(
			"index.php",
			url,
			"processSave"
		);

	}else{
		Modal.Message(res,250,250,false);
		saveURL = "";
	}

}

/** Redirects to the confirmation page. */
function processSave(res){
	window.location = res;
}



function modifyReservation(){
	Modal.Window("index.php?module=modal_modifyreservation&idHotel="+_idHotel,310,150,false);
}

function cancelReservation(){
	Modal.Window("index.php?module=modal_cancelreservation&idHotel="+_idHotel,310,150,false);
}

function findCancelReservation(){

	var lastName = Page.FindControl("modifyLastName").value;
	var confirmationNumber = Page.FindControl("modifyConfirmationNumber").value;

	var url = "module=ajax_findreservation&lastName="+lastName+"&confirmationNumber="+confirmationNumber;

    //prompt('ver',url);

	modeCancel = true;
	xmlhttpPost(
		"index.php",
		url,
		"processFindModifyReservation"
	);

	Modal.Close();

}

function findModifyReservation(){
	var lastName = Page.FindControl("modifyLastName").value;
	var confirmationNumber = Page.FindControl("modifyConfirmationNumber").value;

	var url = "module=ajax_findreservation&lastName="+lastName+"&confirmationNumber="+confirmationNumber;

	xmlhttpPost(
		"index.php",
		url,
		"processFindModifyReservation"
	);

	Modal.Close();
}

function processFindModifyReservation(res){
	if(res == "-1"){
		Modal.Window("index.php?module=modal_message&title=error.warning&message=reservation.notfound&idHotel="+_idHotel);
	}else{

		Modal.Window("index.php?module=modal_message&title=message.info&message=message.wait&allowclose=0&idHotel="+_idHotel);

		//resetCalendarSelection();

		eval("previousReservation = " + res);
		var curMin = new Date(previousReservation.datestartF);
		var curMax = new Date(previousReservation.dateendF);//01/26/2007
		var instr = "";
		
		//set the hotel region
		Page.FindControl("idRegion").value = previousReservation.idRegion;
		instr = 'getHotels()';
		window.setTimeout(instr,1000);
		
		//set the selected hotel
		Page.FindControl("idHotel").value =  previousReservation.idHotel ;
		changeHotel(previousReservation.idHotel);
		
		spIn = Page.FindControl("spCheckinDate");
		spInPayment = Page.FindControl("spCheckinDatePayment");
		spOut = Page.FindControl("spCheckoutDate");
		spOutPayment = Page.FindControl("spCheckoutDatePayment");
	
		if (curMin && curMax){
			spIn.innerHTML = months[curMin.getMonth()] + " " + curMin.getDate() + ", " + curMin.getFullYear();
			spInPayment.innerHTML = months[curMin.getMonth()] + " " + curMin.getDate() + ", " + curMin.getFullYear();
	
			spOut.innerHTML = months[curMax.getMonth()] + " " + curMax.getDate() + ", " + curMax.getFullYear();
			spOutPayment.innerHTML = months[curMax.getMonth()] + " " + curMax.getDate() + ", " + curMax.getFullYear();
	
			datediff = (curMax - curMin) / 60 / 60 / 24 / 1000;
			Page.FindControl("spQtyNights").innerHTML = previousReservation.qtyNights;
			
			Page.FindControl("checkIn").value = curMin.getDate() + "/" + (curMin.getMonth()+1) + "/" + curMin.getFullYear();
			Page.FindControl("checkOut").value = curMax.getDate() + "/" + (curMax.getMonth()+1) + "/" + curMax.getFullYear();
		}


		Page.FindControl("spSubtotal").innerHTML = parseFloat(previousReservation.price).toFixed(2);
		Page.FindControl("spTaxes").innerHTML = parseFloat(previousReservation.tax).toFixed(2);
		Page.FindControl("spAmount").innerHTML = parseFloat(previousReservation.totalPrice).toFixed(2);


/*
		//select start and end dates
		objTDId = "td_"+(min.getFullYear())+"_"+(min.getMonth()+1)+"_"+(min.getDate());
		objTD = Page.FindControl(objTDId);
		instr = 'selectdate(null,null,true, '+min.getFullYear()+', '+(min.getMonth()+1)+', '+min.getDate()+'  ,true);';
        window.setTimeout(instr,1000);

		if (min != max){
            objTDId = "td_"+(max.getFullYear())+"_"+(max.getMonth()+1)+"_"+(max.getDate());
			objTD = Page.FindControl(objTDId);
			//selectdate(null,null,objTD, max.getFullYear(), (max.getMonth()+1), (max.getDate())  ,true);
            instr = 'selectdate(null,null,objTD, '+max.getFullYear()+', '+(max.getMonth()+1)+', '+(max.getDate())+'  ,true);';
            window.setTimeout(instr,2000);
		}*/
		//set  values
		Page.FindControl("idReservation").value = previousReservation.idReservation;

		Page.FindControl("qtyAdults").value = previousReservation.qtyAdults;
		Page.FindControl("qtyChildren").value = previousReservation.qtyChildren;
		Page.FindControl("qtyRooms").value = previousReservation.qtyRooms;

		Page.FindControl("firstName").LastValue = Page.FindControl("firstName").value;
		Page.FindControl("firstName").focused = true;
		Page.FindControl("firstName").value = previousReservation.firstName;

		Page.FindControl("lastName").LastValue = Page.FindControl("lastName").value;
		Page.FindControl("lastName").focused = true;
		Page.FindControl("lastName").value = previousReservation.lastName;

		Page.FindControl("streetAddress").LastValue = Page.FindControl("streetAddress").value;
		Page.FindControl("streetAddress").focused = true;
		Page.FindControl("streetAddress").value = previousReservation.streetAddress;
        if(previousReservation.streetAddress.length<1){
            WebControls.ClearValueOnBlur(Page.FindControl("streetAddress"));
        }

		Page.FindControl("city").LastValue = Page.FindControl("city").value;
		Page.FindControl("city").focused = true;
		Page.FindControl("city").value = previousReservation.city;

		Page.FindControl("zipCode").LastValue = Page.FindControl("zipCode").value;
		Page.FindControl("zipCode").focused = true;
		Page.FindControl("zipCode").value = previousReservation.zipCode;
        if(previousReservation.zipCode.length<1){
            WebControls.ClearValueOnBlur(Page.FindControl("zipCode"));
        }

		Page.FindControl("email").LastValue = Page.FindControl("email").value;
		Page.FindControl("email").focused = true;
		Page.FindControl("email").value = previousReservation.email;

		Page.FindControl("phoneNumber").LastValue = Page.FindControl("phoneNumber").value;
		Page.FindControl("phoneNumber").focused = true;
		Page.FindControl("phoneNumber").value = previousReservation.phoneNumber;

		Page.FindControl("faxNumber").LastValue = Page.FindControl("faxNumber").value;
		Page.FindControl("faxNumber").focused = true;
		Page.FindControl("faxNumber").value = previousReservation.faxNumber;
        if(previousReservation.faxNumber.length<1){
            WebControls.ClearValueOnBlur(Page.FindControl("faxNumber"));
        }

        Page.FindControl("guaranteeName").LastValue = Page.FindControl("guaranteeName").value;
		Page.FindControl("guaranteeName").focused = true;
		Page.FindControl("guaranteeName").value = previousReservation.guaranteeName;

		Page.FindControl("guaranteeCredCardExpiration").LastValue = Page.FindControl("guaranteeCredCardExpiration").value;
		Page.FindControl("guaranteeCredCardExpiration").focused = true;
		Page.FindControl("guaranteeCredCardExpiration").value = previousReservation.guaranteeCredCardExpiration;


		Page.FindControl("guaranteeCredCardNumber").LastValue = Page.FindControl("guaranteeCredCardNumber").value;
		Page.FindControl("guaranteeCredCardNumber").focused = true;
		Page.FindControl("guaranteeCredCardNumber").value = previousReservation.guaranteeCredCardNumber;


        Page.FindControl("arrivalInformation").LastValue = Page.FindControl("arrivalInformation").value;
        Page.FindControl("arrivalInformation").value = previousReservation.arrivalInformation;
        if(previousReservation.arrivalInformation.length<1){
            WebControls.ClearValueOnBlur(Page.FindControl("arrivalInformation"));
        }

        Page.FindControl("comments").LastValue = Page.FindControl("comments").value;
        Page.FindControl("comments").value = previousReservation.comments;
        if(previousReservation.comments.length<1){
            WebControls.ClearValueOnBlur(Page.FindControl("comments"));
        }
		Page.FindControl("idCountry").value = previousReservation.idCountry;
		Page.FindControl("idExternalUser").value = previousReservation.idExternalUser;

		//change the current country and load its states
		var url = "module=ajax_getstates&idCountry="+previousReservation.idCountry;
		instr = 'xmlhttpPost("index.php","'+url+'","processChangeCountry");';
		window.setTimeout(instr,2000);

		//select the state
		instr = 'Page.FindControl("idState").value = '+previousReservation.idState+';'
		window.setTimeout(instr,3000);

		//select the credit card
		for (i = 0; i < document.frmHome.idGuaranteeCredCardCode.length; i++){
			if (document.frmHome.idGuaranteeCredCardCode[i].value == previousReservation.idGuaranteeCredCardCode){
				document.frmHome.idGuaranteeCredCardCode[i].checked = true;
			}
		}

		//load the hotel rooms
		instr = 'loadrooms()';
		window.setTimeout(instr,3000);
		

		//select the room
		instr = "selectRoom("+previousReservation.idRoom+");";
		window.setTimeout(instr,4000);

		//allow the user to interact with the page
		instr = "Modal.Close()";
		window.setTimeout(instr,5000);

		if (modeCancel){
			instr = 'Modal.Window("index.php?module=modal_message&title=message.info&message=cancelreservation.description&idHotel='+_idHotel+'");';
			window.setTimeout(instr,6000);

			var url = "module=ajax_getcancelbutton";
			instr = 'xmlhttpPost("index.php","'+url+'","processCancelButton");';
			window.setTimeout(instr,7000);


		}
	}
}

function processCancelButton(res){
	Page.FindControl("spSubmitForm").innerHTML = res;
}

function confirmcancelreservation(){
	var idReservation = Page.FindControl("idReservation").value;
	var url = "module=ajax_cancelreservation&idReservation="+idReservation;

	xmlhttpPost("index.php",url,"processCancelReservation");
}

function processCancelReservation(){
	Modal.Window("index.php?module=modal_message&title=message.info&message=cancelreservation.completed&closefunction=cancelationredirect&idHotel="+_idHotel);
}

function cancelationredirect(){
	Modal.Close();
	redirecthome();
}

function selectReservationByCustomer(lastName,confirmationNumber){
	var url = "module=ajax_findreservation&lastName="+lastName+"&confirmationNumber="+confirmationNumber;

	xmlhttpPost(
		"index.php",
		url,
		"processFindModifyReservation"
	);

	Modal.Close();
}
