/* Copyright (c) 1995-2008 Choice Hotels International, Inc. All Rights Reserved. */
var ReservationLang={MAX_ROOMS_ERROR_START:"The number of rooms selected exceeds the total"+"\n"+"number of rooms (",MAX_ROOMS_ERROR_END:") allowed in this reservation."+"\n\n",MAX_ROOMS_SELECT_START:"Please select no more than ",MAX_ROOMS_SELECT_END:" room(s) on this page.",BED_INVENTORY_ERROR_CRIBS:"Sorry, the hotel has no more cribs available for the dates selected.",BED_INVENTORY_ERROR_ROLLAWAYS:"Sorry, the hotel has no more rollaways available for the dates selected.",BOOK_ROOMS_ERROR:"Please select at least one room on this page to continue.",BOOK_ROOMTYPE_ERROR:"Please correct errors on this page to continue.",BOOK_CRIB_ERROR:"You must have at least one child in a room to select a crib."};Number.prototype.toDecimals=function(n){n=(isNaN(n))?2:n;var nT=Math.pow(10,n);function pad(s){s=s||'.';return (s.length>n)?s:pad(s+'0')};return (isNaN(this))?this:(new String(Math.round(this*nT)/nT)).replace(/(\.\d*)?$/,pad)};var Reservation={los:null,adults:null,children:null,precision:null,cribinventory:null,cribtotal:0,rollawayinventory:null,rollawaytotal:0,numroomsmax:null,numroomstotal:0,numroomsbooked:null,numroomtypes:null,roomTypes:{},requiredpoints:null,multiroommode:true,roomDropMax:9,init:function(){if(document.forms["availabilityForm"]!=null){document.forms["availabilityForm"].reset();$("#div_updaterates").css("display","none");Reservation.load();if(Reservation.isMultiRoom()){for(var code in Reservation.roomTypes){Reservation.onchangeNumRooms($("#"+code+"_nroom")[0])}}}},load:function(){this.los=$.parseInt($('#data_los').text(),1);this.adults=$.parseInt($('#data_adults').text(),1);this.children=$.parseInt($('#data_children').text(),0);this.precision=$.parseInt($('#data_precision').text(),2);this.cribinventory=$.parseInt($('#data_cribinventory').text(),0);this.rollawayinventory=$.parseInt($('#data_rollawayinventory').text(),0);this.requiredpoints=$.parseInt($('#data_requiredpoints').text(),0);this.numroomtypes=$.parseInt($('#data_numrooms').text(),0);this.numroomsmax=$.parseInt($('#data_numroomsmax').text(),9);this.numroomsbooked=$.parseInt($('#data_numroomsbooked').text(),0);this.multiroommode=$.parseStr($('#data_multiroommode').text(),"false");this.loadRoomTypes()},loadRoomTypes:function(){for(var i=1;i<=this.numroomtypes;i++){var roomType=new this.RoomType(i).load();this.roomTypes[roomType.code]=roomType}},onchangeExtraBed:function(select){var roomTypeCode=select.id.split('_')[0];var roomType=this.roomTypes[roomTypeCode];var bedCode=$.parseStr($("option:selected",select).val(),"none");var room=roomType.rooms[1];if(this.isMultiRoom()){var roomIndex=select.id.split('_')[2];room=roomType.rooms[roomIndex]}if(!room.updateExtraBed(bedCode)){$(select).val(room.bedCode);return }if(!roomType.update(bedCode)){return }if(this.isMultiRoom()){this.updateStay()}},onchangeNumRooms:function(select){var roomsSelected=parseInt($("option:selected",select).val());if(isNaN(roomsSelected))roomsSelected=0;var roomTypeCode=select.id.split('_')[0];var roomType=this.roomTypes[roomTypeCode];if(roomType!=null){var roomsChange=roomsSelected-roomType.numrooms;if(roomsChange+this.numroomstotal>this.numroomsmax){alert(ReservationLang.MAX_ROOMS_ERROR_START+(this.numroomsbooked+this.numroomsmax)+ReservationLang.MAX_ROOMS_ERROR_END+ReservationLang.MAX_ROOMS_SELECT_START+this.numroomsmax+ReservationLang.MAX_ROOMS_SELECT_END);select.selectedIndex=this.numroomslast;select.onchange()}else {this.numroomstotal+=roomsChange;roomType.numroomslast=roomType.numrooms;roomType.numrooms=roomsSelected}if(roomType.update()){this.updateStay()}roomType.resetDeselectedRooms()}},onchangeNumAdults:function(select){var adultsSelected=$.parseInt($("option:selected",select).val(),1);var roomTypeCode=select.id.split('_')[0];var roomType=this.roomTypes[roomTypeCode];var roomIndex=select.id.split('_')[2];var room=roomType.rooms[roomIndex];room.adults=adultsSelected;if(roomType.update()){this.updateStay()}},onchangeNumChildren:function(select){var childrenSelected=$.parseInt($("option:selected",select).val(),0);var roomTypeCode=select.id.split('_')[0];var roomType=this.roomTypes[roomTypeCode];var roomIndex=select.id.split('_')[2];var room=roomType.rooms[roomIndex];room.children=childrenSelected;if(roomType.update()){this.updateStay()}},onclickBookRooms:function(){var haveRooms=false;var roomError=false;var cribError=false;if(this.numroomsbooked>0)haveRooms=true;for(var code in this.roomTypes){var roomType=this.roomTypes[code];if(roomType.numrooms>0)haveRooms=true;if(roomType.hasError())roomError=true;for(var i=1;i<=roomType.numrooms;i++)if(roomType.rooms[i].bedCode=="CRIB"&&roomType.rooms[i].children==0)cribError=true}if(roomError){alert(ReservationLang.BOOK_ROOMTYPE_ERROR);return false}if(!haveRooms){alert(ReservationLang.BOOK_ROOMS_ERROR);return false}if(cribError){alert(ReservationLang.BOOK_CRIB_ERROR);return false}return true},updateStay:function(){var stay=this.getStay();if(Reservation.usingPoints()){stay=stay+" Points"}else if(Reservation.precision>0){stay=stay.toDecimals(Reservation.precision)}$("#span_estimatedtotal").text(String(stay));$("#span_estimatedtotal").css("visibility","visible")},isMultiRoom:function(){if(this.multiroommode=='true'){return true}return false},usingPoints:function(){if(this.requiredpoints){return true}return false},getStay:function(){var stay=0.0;for(var code in this.roomTypes){stay+=this.roomTypes[code].getStay()}return stay},reset:function(){if(this.isMultiRoom()){for(var code in Reservation.roomTypes){$("#"+code+"_nroom")[0].selectedIndex=0}for(var code in Reservation.roomTypes){Reservation.onchangeNumRooms($("#"+code+"_nroom")[0])}}},RoomType:function(index){this.ERROR_OCCUPANCY="occupancy";this.ERROR_EXTRABED="extraBed";this.ERROR_NONE="none";this.index=index;this.code=null;this.occ=null;this.maxocc=null;this.numrooms=null;this.numroomslast=null;this.numrates=null;this.rooms=null;this.rates=null;this.load=function(){this.code=$.parseStr($("#data_roomtype_"+this.index).text(),"");this.occ=$.parseInt($("#data_"+this.code+"_occ").text(),1);this.maxocc=$.parseInt($("#data_"+this.code+"_maxocc").text(),1);if(Reservation.isMultiRoom())this.numrooms=0;else this.numrooms=1;this.numroomslast=this.numrooms;this.numrates=$.parseInt($("#data_"+this.code+"_numrates").text(),0);this.rooms=new Array(this.numrooms);this.rates=new Array(this.numrates);this.loadRooms();this.loadRates();return this};this.loadRooms=function(){var numberOfRooms=(Reservation.isMultiRoom())?Reservation.roomDropMax:1;for(var i=1;i<=numberOfRooms;i++){this.rooms[i]=new Reservation.Room().load(this.code,i)}};this.loadRates=function(){for(var i=1;i<=this.numrates;i++)this.rates[i]=new Reservation.Rate().load(this.code,i)};this.update=function(bedCode){if(this.hasError())return false;this.updateNightly();if(Reservation.isMultiRoom())this.updateRateChangeLink();else this.updateBookThisLink(bedCode);return true};this.hasError=function(){if(this.hasMaxOccupancyError()){if(Reservation.isMultiRoom()){$("#span_estimatedtotal").css("visibility","hidden")}this.toggleError(this.ERROR_OCCUPANCY);return true}else if(this.hasExtraBedError()){if(Reservation.isMultiRoom()){$("#span_estimatedtotal").css("visibility","hidden")}this.toggleError(this.ERROR_EXTRABED);return true}else {this.toggleError(this.ERROR_NONE)}return false};this.toggleError=function(error){var $rate=$("#div_"+this.code+"_rate");var $sorry=$("#span_"+this.code+"_sorrymsg");var $extraBed=$("#span_"+this.code+"_needextrabedmsg");var $roomsleft=$("#div_"+this.code+"_onlyxroomsleft");switch(error){case this.ERROR_OCCUPANCY:;$rate.hide();$extraBed.hide();$sorry.css("display","block");$roomsleft.hide();if(!Reservation.isMultiRoom())$("#span_"+this.code+"_bookthisroom").hide();break;case this.ERROR_EXTRABED:;$rate.hide();$sorry.hide();$extraBed.css("display","block");$roomsleft.hide();if(!Reservation.isMultiRoom())$("#span_"+this.code+"_bookthisroom").hide();break;default: ;$sorry.hide();$extraBed.hide();$rate.css("display","block");$roomsleft.css("display","block");if(!Reservation.isMultiRoom())$("#span_"+this.code+"_bookthisroom").show()}};this.hasMaxOccupancyError=function(){var error=false;for(var i=1;i<=this.numrooms;i++){if(this.rooms[i].isOverMaxOccupancy()){error=true;this.rooms[i].displayErrorMarker(true)}else {this.rooms[i].displayErrorMarker(false)}}return error};this.hasExtraBedError=function(){var error=false;for(var i=1;i<=this.numrooms;i++){if(this.rooms[i].needsExtraBed()){error=true;this.rooms[i].displayErrorMarker(true)}else {this.rooms[i].displayErrorMarker(false)}}return error};this.updateBookThisLink=function(bedCode){var anchor=$("#anchor_"+this.code+"_bookthisroom")[0];anchor.href=ResUtil.setParameter(anchor.href,"xtrabed",bedCode)};this.updateNightly=function(){var nightly=0.0;if(Reservation.usingPoints()){nightly=this.numrooms*Reservation.requiredpoints}else {for(var i=1;i<=this.numrooms;i++){nightly+=this.rooms[i].getNightly(this.rates[1])}}if(this.numrooms==0){nightly=this.getLowestNightly();$("#span_"+this.code+"_ratefrom").show()}else {$("#span_"+this.code+"_ratefrom").hide()}this.displayRateAndChange("inline",nightly)};this.updateRateChangeLink=function(){var span=$("#span_"+this.code+"_ratechangelink")[0];if(span){var anchor=$("a",span)[0];if(anchor){var splitHref=anchor.href.split("'");var url=splitHref[1];var nroom;if(this.numrooms<1)nroom=1;else nroom=this.numrooms;url=ResUtil.setParameter(url,"nroom",nroom);splitHref[1]=url;anchor.href=splitHref.join("'")}}};this.getNightly=function(rate){var nightly=0.0;for(var i=1;i<=this.numrooms;i++){nightly+=this.rooms[i].getNightly(rate)}return nightly};this.getStay=function(){var stay=0.0;for(var i=1;i<=this.numrooms;i++){if(Reservation.usingPoints()){stay+=Reservation.requiredpoints*Reservation.los}else {stay+=this.rooms[i].getStay()}}return stay};this.getLowestNightly=function(){if(Reservation.usingPoints()){return Reservation.requiredpoints}var nightly=0;var room=new Reservation.Room();room.adults=1;room.children=0;room.bedCode="none";for(var i=1;i<this.rates.length;i++){var current=room.getNightly(this.rates[i]);if(nightly==0||current<nightly)nightly=current}return nightly};this.displayRateAndChange=function(option,rate){if(typeof rate=="number"){if(Reservation.usingPoints()){rate=rate+" Points"}else if(Reservation.precision>0){rate=rate.toDecimals(Reservation.precision)}if(Reservation.isMultiRoom()&&this.numrooms==0&&!Reservation.usingPoints()){rate=rate+" *"}$("#span_"+this.code+"_ratedisplay").text(String(rate))}$("#span_"+this.code+"_ratedisplay").css("display",option);$("#span_"+this.code+"_ratechangelink").css("display",option)};this.resetDeselectedRooms=function(){for(var i=1;i<this.rooms.length;i++){if(i>this.numrooms){this.rooms[i].reset()}}}},Room:function(){this.typeCode=null;this.index=null;this.adults=null;this.children=null;this.bedCode='none';this.load=function(typeCode,index){this.typeCode=typeCode;this.index=index;if(Reservation.isMultiRoom()){this.adults=$.parseInt($("#"+typeCode+"_nadult_"+index).find("option:selected").val(),1);this.children=$.parseInt($("#"+typeCode+"_nchild_"+index).find("option:selected").val(),0);this.bedCode=$.parseStr($("#"+typeCode+"_xtrabed_"+index).find("option:selected").val(),"none")}else {this.adults=Reservation.adults;this.children=Reservation.children;this.bedCode=$.parseStr($("#"+typeCode+"_xtrabed").find("option:selected").val(),"none")}return this};this.getRoomType=function(){return Reservation.roomTypes[this.typeCode]};this.getOccupants=function(){return this.adults+this.children};this.needsExtraBed=function(){var roomType=this.getRoomType();var occupants=this.getOccupants();if(occupants>roomType.occ&&occupants<=roomType.maxocc&&this.bedCode=="none"){return true}return false};this.getNightly=function(rate){var nightly=0.0;if(this.adults==1){nightly=rate.oneperson}else {if(rate.twoperson){nightly=rate.twoperson}else {nightly=rate.oneperson+rate.extraperson}for(var i=3;i<=this.adults;i++){nightly+=rate.extraperson}}switch(this.bedCode){case "CRIB":;nightly+=rate.crib;break;case "RLWY":;nightly+=rate.rollaway;break}return nightly};this.getStay=function(){var rates=this.getRoomType().rates;var stay=0.0;for(var i=1;i<rates.length;i++){stay+=this.getNightly(rates[i])*rates[i].los}return stay};this.isOverMaxOccupancy=function(){if(this.getOccupants()>this.getRoomType().maxocc){return true}return false};this.displayErrorMarker=function(display){if(Reservation.isMultiRoom())if(display)$("#span_"+this.typeCode+"_roomErrorMarker_"+this.index).css("visibility","visible");else $("#span_"+this.typeCode+"_roomErrorMarker_"+this.index).css("visibility","hidden")};this.willExceedBedInventory=function(newBedCode){var cribtotal=Reservation.cribtotal;var rollawaytotal=Reservation.rollawaytotal;switch(this.bedCode){case "CRIB":;cribtotal-=1;break;case "RLWY":;rollawaytotal-=1;break}switch(newBedCode){case "CRIB":;cribtotal+=1;break;case "RLWY":;rollawaytotal+=1;break}if(cribtotal>Reservation.cribinventory||rollawaytotal>Reservation.rollawayinventory){return true}return false};this.updateExtraBed=function(newBedCode){if(Reservation.isMultiRoom()&&this.willExceedBedInventory(newBedCode)){switch(newBedCode){case "CRIB":;alert(ReservationLang.BED_INVENTORY_ERROR_CRIBS);return false;case "RLWY":;alert(ReservationLang.BED_INVENTORY_ERROR_ROLLAWAYS);return false}}switch(this.bedCode){case "CRIB":;Reservation.cribtotal-=1;break;case "RLWY":;Reservation.rollawaytotal-=1;break}switch(newBedCode){case "CRIB":;Reservation.cribtotal+=1;break;case "RLWY":;Reservation.rollawaytotal+=1;break}this.bedCode=newBedCode;return true};this.reset=function(){this.adults=1;this.children=0;this.updateExtraBed("none");$("#"+this.typeCode+"_nadult_"+this.index).val("1");$("#"+this.typeCode+"_child_"+this.index).val("0");$("#"+this.typeCode+"_xtrabed_"+this.index).val("none");this.displayErrorMarker(false)}},Rate:function(){this.los=null;this.oneperson=null;this.twoperson=null;this.extraperson=null;this.crib=null;this.rollaway=null;this.load=function(code,index){this.los=$.parseInt($("#data_"+code+"_los_"+index).text(),0);this.oneperson=$.parseFlt($("#data_"+code+"_onepersonrate_"+index).text(),0.0);this.twoperson=$.parseFlt($("#data_"+code+"_twopersonrate_"+index).text(),0.0);this.extraperson=$.parseFlt($("#data_"+code+"_extrapersonrate_"+index).text(),0.0);this.crib=$.parseFlt($("#data_"+code+"_cribrate_"+index).text(),0.0);this.rollaway=$.parseFlt($("#data_"+code+"_rollawayrate_"+index).text(),0.0);return this}}};var ResUtil={setParameter:function(url,name,value){url=url.replace(/(\\?|&|\\?&)$/,"");var regex=new RegExp("(\\?|&)"+name+"=[^&]*(&|$)");var tokens=null;if(regex.test(url)){tokens=regex.exec(url);var newParam=tokens[1]+name+"="+value;if(tokens[2]=="&"){newParam=newParam+"&"}url=url.replace(regex,newParam)}else if(/\?/.test(url)){url=url+"&"+name+"="+value}else {url=url+"?"+name+"="+value}return url},removeParameter:function(url,name){var regex=new RegExp("(\\?|&)"+name+"=[^&]*&?");url=url.replace(regex,"$1");return url.replace(/(\\?|&|\\?&)$/,"")},dump:function(object){var msg="";for(var prop in object){msg+=prop+":  "+object[prop]+"\n"}return msg}};