function searchForm() { this.rows = Array(); } searchForm.prototype.addRow = function() { var new_row = Array('1', '1'); this.rows.push(new_row); } searchForm.prototype.deleteRow = function(del) { var new_rows = Array(); for(var i = 0; i < this.rows.length; i++) { if (i != del) { new_rows.push(this.rows[i]); } } this.rows = new_rows; } searchForm.prototype.setType = function(row, type) { this.rows[row][0] = type; } searchForm.prototype.setNo = function(row, no) { this.rows[row][1] = no; } searchForm.prototype.createRowNode = function(table, i, type, no) { var new_tr = document.createElement('tr'); var td = document.createElement('td'); td.style.textAlign = 'right'; var checkbox = document.createElement('input'); checkbox.setAttribute('type', 'checkbox'); checkbox.setAttribute('value', i); td.appendChild(checkbox); checkbox.onclick = function() { search_form.deleteRow(this.value); search_form.display(); } var text = document.createTextNode('room type ' + (i + 2)); td.appendChild(text); new_tr.appendChild(td); var td = document.createElement('td'); var select = document.createElement('select'); select.setAttribute('name', 'room_type[' + (i + 2) + ']'); var room_types = ['single: sleeps 1', 'double: sleeps 2', 'twin: sleeps 2', 'triple: sleeps 3', 'quad: sleeps 4']; for (j = 0; j < room_types.length; j++) { var option = document.createElement('option'); option.setAttribute('value', i + '_' + (j + 1)); var text = document.createTextNode(room_types[j]); option.appendChild(text); select.appendChild(option); } select.selectedIndex = parseInt(type) - 1; td.appendChild(select); select.onchange = function() { var comp = this.value.split('_'); search_form.setType(comp[0], comp[1]); } text = document.createTextNode(' no. of rooms '); td.appendChild(text); select = document.createElement('select'); select.setAttribute('name', 'room_number[' + (i + 2) + ']'); for (j = 1; j <= 9; j++) { option = document.createElement('option'); option.setAttribute('value', i + '_' +j); text = document.createTextNode(j); option.appendChild(text); select.appendChild(option); } select.selectedIndex = parseInt(no) - 1; td.appendChild(select); select.onchange = function() { var comp = this.value.split('_'); search_form.setNo(comp[0], comp[1]); } new_tr.appendChild(td); table.appendChild(new_tr); checkbox.checked = true; } searchForm.prototype.display = function() { var table = document.getElementById('searchform'); var remove = Array(); for (var i = 0; i < table.childNodes.length; i++) { if (table.childNodes[i].className != 'static') { remove.push(table.childNodes[i]); } } for (var i = 0; i < remove.length; i++) { table.removeChild(remove[i]); } for (var i = 0; i < this.rows.length; i++) { this.createRowNode(table, i, this.rows[i][0], this.rows[i][1]); } if (this.rows.length < 5) { var new_row = document.createElement('tr'); var td = document.createElement('td'); td.style.textAlign = 'right'; var checkbox = document.createElement('input'); checkbox.setAttribute('type', 'checkbox'); td.appendChild(checkbox); checkbox.onclick = function() { search_form.addRow(); search_form.display(); } var text = document.createTextNode('room type ' + (this.rows.length + 2)); td.appendChild(text); new_row.appendChild(td); td = document.createElement('td'); new_row.appendChild(td); table.appendChild(new_row); } } function dateChange() { var comp = document.getElementById('checkin_month').value.split('_'); var checkin_date = new Date(comp[1], parseInt(comp[0]) - 1, document.getElementById('checkin_day').value); var comp = document.getElementById('checkout_month').value.split('_'); var checkout_date = new Date(comp[1], parseInt(comp[0]) - 1, document.getElementById('checkout_day').value); var now = new Date(2008, 4, 16); //now = new Date(now.getFullYear(), now.getMonth(), now.getDate()); if (checkin_date < now) { checkin_date = now; document.getElementById('checkin_day').selectedIndex = checkin_date.getDate() - 1; } document.getElementById('checkin_day').selectedIndex = checkin_date.getDate() - 1; var checkin_month = document.getElementById('checkin_month'); for (var i = 0; i < checkin_month.childNodes.length; i++) { if (checkin_month.options[i].value == (checkin_date.getMonth() + 1) + '_' + checkin_date.getFullYear()) { break; } } checkin_month.selectedIndex = i; if (checkin_date >= checkout_date) { checkout_date = new Date(checkin_date.valueOf() + 86400000); document.getElementById('checkout_day').selectedIndex = checkout_date.getDate() - 1; var checkout_month = document.getElementById('checkout_month'); for (var i = 0; i < checkout_month.options.length; i++) { if (checkout_month.options[i].value == (checkout_date.getMonth() + 1) + '_' + checkout_date.getFullYear()) { break; } } checkout_month.selectedIndex = i; } var no_nights = Math.round((checkout_date - checkin_date) / 86400000); no_of_nights.removeChild(no_of_nights.firstChild); var text = document.createTextNode(no_nights); no_of_nights.appendChild(text); calendar1.setDate(checkin_date); calendar2.setDate(checkout_date); } function calendarSelect(calendar, date) { var comp = date.split(' '); if (calendar == calendar1) { document.getElementById('checkin_day').selectedIndex = parseInt(comp[0]) - 1; var checkin_month = document.getElementById('checkin_month'); for (var i = 0; i < checkin_month.childNodes.length; i++) { if (checkin_month.options[i].value == parseInt(comp[1]) + '_' + comp[2]) { break; } } checkin_month.selectedIndex = i; } if (calendar == calendar2) { document.getElementById('checkout_day').selectedIndex = parseInt(comp[0]) - 1; var checkin_month = document.getElementById('checkout_month'); for (var i = 0; i < checkin_month.childNodes.length; i++) { if (checkin_month.options[i].value == parseInt(comp[1]) + '_' + comp[2]) { break; } } checkin_month.selectedIndex = i; } dateChange(); this.callCloseHandler(); } function calendarHide() { this.hide(); } function calendar1Check(date) { var now = new Date(2008, 4, 16); //now = new Date(now.getFullYear(), now.getMonth(), now.getDate()); return (date < now); } function calendar2Check(date) { var comp = document.getElementById('checkin_month').value.split('_'); var checkin_date = new Date(comp[1], parseInt(comp[0]) - 1, document.getElementById('checkin_day').value); return (date.valueOf() < checkin_date.valueOf() + 86400000); } function searchFormLoad() { search_form = new searchForm(); search_form.display(); var span = document.createElement('span'); var text = document.createTextNode(' nights: '); span.appendChild(text); no_of_nights = document.createElement('strong'); span.appendChild(no_of_nights); text = document.createTextNode('1'); no_of_nights.appendChild(text); var placeholder = document.getElementById('nights_placeholder'); var placeholder_parent = placeholder.parentNode; placeholder_parent.replaceChild(span, placeholder); document.getElementById('checkin_day').onchange = dateChange; document.getElementById('checkin_month').onchange = dateChange; document.getElementById('checkout_day').onchange = dateChange; document.getElementById('checkout_month').onchange = dateChange; document.getElementById('sh_calendar1').onclick = function() { calendar1.showAtElement(this, 'bR'); } document.getElementById('sh_calendar2').onclick = function() { calendar2.showAtElement(this, 'bR'); } calendar1 = new Calendar(0, null, calendarSelect, calendarHide); calendar1.setDateFormat('%e %m %Y'); calendar1.setDateStatusHandler(calendar1Check); calendar1.create(); calendar2 = new Calendar(0, null, calendarSelect, calendarHide); calendar2.setDateFormat('%e %m %Y'); calendar2.setDateStatusHandler(calendar2Check); calendar2.create(); dateChange(); } if (window.addEventListener) { window.addEventListener('load', searchFormLoad, false); } else { window.attachEvent('onload', searchFormLoad); // Internet Explorer :( }