// ---------- Object singleton tpvB ---------- var tpvB = { // Properties // Functions // -- $ -- $: function() { var elements = new Array(); for (var i = 0 ;i < arguments.length; i++) { var element = arguments[i]; if (typeof element == 'string') element = document.getElementById(element); if (arguments.length == 1) return element; elements.push(element); } return elements; }, // -- addEvent -- addEvent: function(elmId, evType, fn, useCapture) { var elm = document.getElementById(elmId); if ( elm != null ) { if (elm.addEventListener) { elm.addEventListener(evType, fn, useCapture); return true; } else if (elm.attachEvent) { var r = elm.attachEvent('on' + evType, fn); return r; } else { elm['on' + evType] = fn; } } }, // -- removeEvent -- removeEvent: function(elmId, evType, fn, useCapture) { var elm = document.getElementById(elmId); if ( elm != null ) { if (elm.removeEventListener) { elm.removeEventListener(evType, fn, useCapture); return true; } } }, // -- rtrim -- rtrim: function() { return this.replace(/\n\r/,""); }, // -- insertOption -- // Insert an option in a select dom object insertOption: function(obj,text,value,before) { if (obj != null) { // Create and element option var option=document.createElement('option'); option.text = text; option.value = value; if (before == '') { before = null; } try { obj.add(option,before); // Standards compliant } catch(ex) { obj.add(option); // IE only } } }, // -- isValidDate -- isValidDate: function(date) { RegExpformat = /^(([2][0]|[1][9])\d{2}-([0]\d|[1][0-2])-([0-2]\d|[3][0-1]))$/; return RegExpformat.test(date); }, // -- normalizeDate -- // Add 0 to a date for pass it through url // Tranform: yyyy-m-d to yyyy-mm-dd normalizeDate: function(date) { var arr = date.split('-'); if (arr[0].length == 1) { arr[0] = '0'+arr[0]; } if (arr[1].length == 1) { arr[1] = '0'+arr[1]; } if (arr[2].length == 1) { arr[2] = '0'+arr[2]; } date = arr[0]+'-'+arr[1]+'-'+arr[2]; return date; } } // ---------- /Object singleton tpvB ---------- // ---------- Search Disponibility Form Class --------- tpvB.dispForm = function(params) { // Properties this.checkIn = null; this.checkOut = null; this.nights = null; this.adults = null; this.childrens = null; this.babies = null; this.objects = []; // Constructor this._construct = function(params) { // Default values for (x in params) { this[x] = params[x]; } // Get interface objects // CheckIn Day Select this.objects.checkInDay = tpvB.$(params.id+'_form_dates_checkin_selects_day'); // CheckIn Month-Year Select this.objects.checkInMonthYear = tpvB.$(params.id+'_form_dates_checkin_selects_monthyear'); // Check-out this.objects.checkOut = tpvB.$(params.id+'_form_checkout_date'); // Nights Select this.objects.nights = tpvB.$(params.id+'_form_nights_select'); // Adults Select this.objects.adults = tpvB.$(params.id+'_form_guests_adults_select'); // Childrens Select this.objects.childrens = tpvB.$(params.id+'_form_guests_childrens_select'); // Babies Select this.objects.babies = tpvB.$(params.id+'_form_guests_babies_select'); this.update(); } // --- [Public] Update --- this.update = function() { // Get values var numDaysNewMonth = this._getDaysOfMonth(this.objects.checkInMonthYear.value.split('_')[0]-1,this.objects.checkInMonthYear.value.split('_')[1]); if (this.objects.checkInDay.value > numDaysNewMonth) { this.checkIn = new Date(this.objects.checkInMonthYear.value.split('_')[1],(this.objects.checkInMonthYear.value.split('_')[0]-1),numDaysNewMonth); } else { this.checkIn = new Date(this.objects.checkInMonthYear.value.split('_')[1],(this.objects.checkInMonthYear.value.split('_')[0]-1),this.objects.checkInDay.value); } this.nights = parseFloat(this.objects.nights.value); this.adults = parseFloat(this.objects.adults.value); this.childrens = parseFloat(this.objects.childrens.value); this.babies = parseFloat(this.objects.babies.value); this.checkOut = new Date(this.checkIn.getFullYear(),this.checkIn.getMonth(),this.checkIn.getDate()); this.checkOut.setDate(this.checkIn.getDate()+this.nights); // Set days this._setDays(); this.objects.checkOut.innerHTML = this.checkOut.getDate()+'/'+(this.checkOut.getMonth()+1)+'/'+this.checkOut.getFullYear(); } // --- [Public] doSubmit --- this.doSubmit = function() { // GET method if (this.actionMethod == 'GET') { params = this.getParams(this.action); document.location = this.action+params; } // POST method if (this.actionMethod == 'POST') { } } // --- [Public] getParams --- this.getParams = function(action) { // Params if (action != null) { if (action.indexOf('?') != -1) { ini='&'; } else { ini='?'; } }else { ini='&'; } params = ini+'checkin='+tpvB.normalizeDate(this.checkIn.getFullYear()+'-'+(this.checkIn.getMonth()+1)+'-'+this.checkIn.getDate()); params += '&checkout='+tpvB.normalizeDate(this.checkOut.getFullYear()+'-'+(this.checkOut.getMonth()+1)+'-'+this.checkOut.getDate()) params += '&adultos='+this.adults+'&ninios='+this.childrens+'&bebes='+this.babies+'&idioma='+this.lng; params += '&hotel='+this.hotel; return params; } // --- [Public] setCheckIn --- this.setCheckIn = function(date,format) { invalid = false; // SQL format if (format == 'sql') { // Valida la fecha pasada if (tpvB.isValidDate(date)) { var date = date.split('-'); date[0] = parseFloat(date[0]); date[1] = parseFloat(date[1]); date[2] = parseFloat(date[2]); this.objects.checkInMonthYear.value = date[1]+'_'+date[0]; this.update(); this.objects.checkInDay.value = date[2]; this.update(); } else { invalid = true; } } // Javascript format if (format == null || format == 'javascript' || format == 'js') { this.objects.checkInMonthYear.value = (date.getMonth()+1)+'_'+date.getFullYear(); this.update(); this.objects.checkInDay.value = date.getDate(); this.update(); } } // --- [Public] getCheckIn --- // Returns date in sql or javascript date format this.getCheckIn = function(format) { // SQL format if (format == 'sql') { var date = this.checkIn.getFullYear()+'-'+(this.checkIn.getMonth()+1)+'-'+this.checkIn.getDate(); } // Javascript format else if(format == null || format == 'javascript' || format == 'js') { var date = this.checkIn; } return date; } // --- [Public] getCheckOut --- this.getCheckOut = function(format) { // SQL format if (format == 'sql') { var date = this.checkOut.getFullYear()+'-'+(this.checkOut.getMonth()+1)+'-'+this.checkOut.getDate(); } // Javascript format else if(format == null || format == 'javascript' || format == 'js') { var date = this.checkOut; } return date; } // --- [Public] setNights --- this.setNights = function(nights) { this.objects.nights.value = parseFloat(nights); this.update(); } // --- [Public] getNights --- this.getNights = function() { return this.nights; } // --- [Public] setAdults --- this.setAdults = function(adults) { this.objects.adults.value = parseFloat(adults); this.update(); } // --- [Public] getAdults --- this.getAdults = function() { return this.adults; } // --- [Public] setChildrens --- this.setChildrens = function(childrens) { this.objects.childrens.value = parseFloat(childrens); this.update(); } // --- [Public] getChildrens --- this.getChildrens = function() { return this.childrens; } // --- [Public] setBabies --- this.setBabies = function(babies) { this.objects.babies.value = parseFloat(babies); this.update(); } // --- [Public] getBabies --- this.getBabies = function() { return this.babies; } // --- [Private] _setDays --- // Complete day select this._setDays = function() { var daysOfMonth=this._getDaysOfMonth(this.checkIn.getMonth(),this.checkIn.getFullYear()); var elem = this.objects.checkInDay; if (elem.options.length != daysOfMonth) { // Add days if (elem.options.length < daysOfMonth) { for(x=elem.options.length+1;x<=daysOfMonth;x++) { tpvB.insertOption(elem,x,x,null); } } // Remove days if (elem.options.length > daysOfMonth) { for(m=elem.options.length-1;m>-1;m--) { elem.options[m]=null; } for(x=1;x<=daysOfMonth;x++) { tpvB.insertOption(elem,x,x,null); } } } if (this.checkIn.getDate() > daysOfMonth) { this.checkIn = new Date(this.checkIn.getFullYear(),this.checkIn.getMonth(),daysOfMonth); } elem.value=this.checkIn.getDate(); } // --- [Private] _getDaysOfMonth --- this._getDaysOfMonth = function(month,year) { switch (month){ case 0: return 31; break; case 1: if (year % 4 == 0) { if (year % 400 == 0) { return 29; } else { if (year % 100 == 0) { return 28; } else { return 29; } } } else { return 28; }; break; case 2: return 31; break; case 3: return 30; break; case 4: return 31; break; case 5: return 30; break; case 6: return 31; break; case 7: return 31; break; case 8: return 30; break; case 9: return 31; break; case 10: return 30; break; case 11: return 31; break; } // end switch } this._construct(params); } // ---------- /Search Disponibility Form Class ---------