function formDateValid(displayAlerts){
    with(document.hotelSearchForm){
        var cur_year = parseInt(StartYear.options[StartYear.selectedIndex].value, 10);
        var cur_month = parseInt(StartMonth.options[StartMonth.selectedIndex].value, 10);
		var cur_day = parseInt(StartDay.options[StartDay.selectedIndex].value, 10);
		
        if(!cur_day || !cur_month || !cur_year){
          if(displayAlerts){
          	alert('Please select a day, a month and a year');
          }
          return false;
        }
        
        if(!isDate(cur_year + '-' + cur_month + '-' + cur_day, '-')){
          	if(displayAlerts){
          		alert('Please choose a valid date');
        	}
			return false;
        }
        
        var earliestDate = byId('earliestYYYYMMDD').value;
        var selectedDate = buildYYYYMMDDDate(cur_day, cur_month, cur_year);
        
        if(selectedDate < earliestDate){
          	if(displayAlerts){
          		alert('The selected date must be in the future');
          	}
        	return false;
        }
        
        return true;
    }		 
}
function changeMonthOrYear(){
  	rebuildDaySB();
  	updateCalendarDate();
}

function rebuildDaySB(){
  	with(document.hotelSearchForm){
		month = parseInt(StartMonth.value, 10);
		year = parseInt(StartYear.value, 10);
		
		day_sb = StartDay;
		
		if(month && year){
			daysInNewMonth = daysInMonth(month, year);
		}
		else{
		  
		  	//if only a month is set, we can predict the number of days in the month. 
			//Pass a known leap year into the
		  	//days in month function so the 29 is returned for February
		  	if(month){
		  		daysInNewMonth = daysInMonth(month, 1992);
		  	}
		  	else{
				daysInNewMonth = 31;
			}
		}
		
		curMaxDay = parseInt(day_sb.options[day_sb.options.length - 1].value, 10);
			
		if(daysInNewMonth != curMaxDay){
			new_sb = day_sb.cloneNode(false);
	
			addEvent(new_sb, 'click', cancelEvent, false);
					
			if(day_sb.options[0].value != '1'){
			  new_sb.appendChild(day_sb.options[0].cloneNode(true));
			}
			for(i=1; i <= daysInNewMonth; i++){
			  opt = document.createElement('option');
			  opt.setAttribute('value', ''+i);
			  
			  opt.appendChild(document.createTextNode(''+i));
			  
			  new_sb.appendChild(opt);
			}
			
			if(day_sb.selectedIndex < new_sb.options.length){
			  new_sb.selectedIndex = day_sb.selectedIndex;
			}
			else{
			  new_sb.selectedIndex = new_sb.options.length - 1;
			}
			
			day_sb.parentNode.replaceChild(new_sb, day_sb);
			//alert(new_sb.innerHTML);
			//alert(month+"*" + year + "*" + daysInMonth(month, year));  
		}
	}
};

function updateCalendarDate(){

	if(byId('calendarWin').style.display != 'none'){
		var date = getFormDate();
/*
		if(formDateValid()){
		  var highlight_date = date;
		}
		else{
		  var highlight_date = '';
		}
*/
		setDateParams(date, highlight_date);
	}  

};

function calendarOnShow(){

    with(document.hotelSearchForm){
		var date = getFormDate();
		
   	    var highlight_date = date;

		
		if(byId('calendar')){
		  	setDateParams(date, highlight_date);
		}
		else{
		  	node = byId('calendarContainer');
		  	
		  	latest_year = StartYear.options[0].text;
		  	earliest_year = StartYear.options[StartYear.options.length - 1].text;

			node.style.overflow = 'hidden';
			node.appendChild(createCalendarPage(date, highlight_date, latest_year, earliest_year, 'setDate', 'a', true));
		}
		//resizeCalendarWindow();
    }
};

function setFormDate(date){
  with(document.hotelSearchForm){
      var sel_year = date.substring(0, 4);
      var sel_month = date.substring(5, 7);
      while(sel_month.substring(0,1) == '0'){
        sel_month = sel_month.substring(1, sel_month.length);
      }

      var sel_day = date.substring(8, 10);
      while(sel_day.substring(0,1) == '0'){
        sel_day = sel_day.substring(1, sel_day.length);
      }

      for(i=0; i < StartYear.options.length; i++){
        if(StartYear.options[i].value == sel_year){
            StartYear.options[i].selected = true;
        }
      }

      for(i=0; i < StartMonth.options.length; i++){
        if(StartMonth.options[i].value == sel_month){
            StartMonth.options[i].selected = true;
        }
      }

      for(i=0; i < StartDay.options.length; i++){
        if(StartDay.options[i].value == sel_day){
            StartDay.options[i].selected = true;
        }
      }

	  hideCalendar();
	  rebuildDaySB();
  }
};

function getFormDate(){
  	var earliestDate = byId('earliestYYYYMMDD').value;

    with(document.hotelSearchForm){
        var cur_year = parseInt(StartYear.options[StartYear.selectedIndex].value, 10);
        if(!cur_year){
          cur_year = parseInt(earliestDate.substr(0,4));
        }
        var cur_month = parseInt(StartMonth.options[StartMonth.selectedIndex].value, 10);
        
        if(!cur_month){
          if(cur_year == parseInt(earliestDate.substr(0,4), 10)){
            cur_month = parseInt(earliestDate.substr(4,2), 10);
          }
          else{
          	cur_month = 1;
          }
		}
        var cur_day = parseInt(StartDay.options[StartDay.selectedIndex].value, 10);
        if(!cur_day){
          if(cur_year == parseInt(earliestDate.substr(0,4), 10)
		  		&& cur_month == parseInt(earliestDate.substr(4,2), 10)){
            cur_day = parseInt(earliestDate.substr(6,2), 10);
          }
          else{
          	cur_day = 1;
          }
        }
		
		cur_year = '' + cur_year;
		cur_month = '' + cur_month;
		cur_day = '' + cur_day;
		

        while(cur_month.length < 2){
          cur_month = '0' + cur_month;
        }
        while(cur_day.length < 2){
          cur_day = '0' + cur_day;
        }
        var date = buildYYYYMMDDDate(cur_day, cur_month, cur_year);
        
        return date;
    }
};
