  var reservations_multilocations_enable_update=true;

  function days_between_full(date1, date2) {
	    var ONE_DAY = 1000 * 60 * 60 * 24;
	    var date1_ms = date1.getTime();
	    var date2_ms = date2.getTime();
	    var difference_ms = date1_ms - date2_ms;
	    return Math.round(difference_ms/ONE_DAY);
  }

  function disallowADate(date) {
    if ( date < low_limit_datea )  return true; else return false;
  }

  function AdateChanged(calendar) {
    if (calendar.dateClicked) {
      var enddate_tag=document.getElementById('enddate');
      var days_tag=document.getElementById('days_number');
      var days_number=2;
      if (days_tag != null && typeof(days_tag) != 'undefined' ) {
    	  days_number=parseInt(days_tag.value);
      }
      // OK, a date was clicked, setup the end date
      var enddate=new Date(calendar.date.getFullYear(), calendar.date.getMonth(), calendar.date.getDate() + days_number );
      enddate_tag.value = enddate.print("%m/%d/%Y");
    }
  }

  function disallowBDate(date) {
    if ( date < low_limit_datea )  return true; else return false;
  }

  function BdateChanged(calendar) {
    if (calendar.dateClicked) {
      var startdate_tag=document.getElementById('startdate');
      var days_tag=document.getElementById('days_number');

      if (days_tag != null && typeof(days_tag) != 'undefined' ) {
    	  var days_number=days_between_full(calendar.date, Date.parseDate(startdate_tag.value, "%m/%d/%Y"));
    	  if (days_number < 1 ) {
    		  days_number=1;
    		  var startdate=new Date(calendar.date.getFullYear(), calendar.date.getMonth(), calendar.date.getDate() - days_number );
    		  startdate_tag.value=startdate.print("%m/%d/%Y");
    	  }
    	  if ( days_tag.selectedIndex != null ) {
    		  days_tag.selectedIndex= days_number-1;
    	  } else {
    		  days_tag.value=days_number;
    	  }
      }
    }
  }

  function DaysChanged() {
    var days_tag=document.getElementById('days_number');
    var days_number=parseInt(days_tag.value);
    var startdate_tag=document.getElementById('startdate');
    var filter=/^\d{1,2}\/\d{1,2}\/\d{2,4}$/;
    if ( filter.test(startdate_tag.value)) {
      var limit_date=Date.parseDate(startdate_tag.value, "%m/%d/%Y");
      limit_date.setDate(limit_date.getDate() + days_number);
      var enddate_tag=document.getElementById('enddate');
      enddate_tag.value = limit_date.print("%m/%d/%Y");
    }
  }





  function verify_prereservation_simple_form() {
    var tag=document.getElementById('occupants');
    if ( tag.options[tag.selectedIndex].value == '0') { alert("Please select a number of occupants!"); return false; }

    var occupants=parseInt(tag.options[tag.selectedIndex].value);
    var all_occupants=parseInt(tag.options[tag.selectedIndex].value);
    tag=document.getElementById('occupants_small');
    all_occupants+=parseInt(tag.options[tag.selectedIndex].value);

    var date_begin=document.getElementById('startdate').value;
    var date_end=document.getElementById('enddate').value;
    if (! check_usdate(date_begin)) { alert(date_begin+ ' is not a valid date value. Please use mm/dd/yy format.'); return false; }
    if (! check_usdate(date_end)) { alert(date_end + ' is not a valid date value. Please use mm/dd/yy format.'); return false; }
    if (! compare_dates_strong(date_begin, date_end)) { alert('Please select a valid reservation period! Begin date can not be later or equal the end date.');  return false;}

    return true;
  }

  function verify_property_prereservation_simple_form() {
    var tag=document.getElementById('occupants');

    var occupants=parseInt(tag.options[tag.selectedIndex].value);
    var all_occupants=parseInt(tag.options[tag.selectedIndex].value);

    var date_begin=document.getElementById('startdate').value;
    var date_end=document.getElementById('enddate').value;
    if (! check_usdate(date_begin)) { alert(date_begin+ ' is not a valid date value. Please use mm/dd/yy format.'); return false; }
    if (! check_usdate(date_end)) { alert(date_end + ' is not a valid date value. Please use mm/dd/yy format.'); return false; }
    if (! compare_dates_strong(date_begin, date_end)) { alert('Please select a valid reservation period! Begin date can not be later or equal the end date.');  return false;}

    return true;
  }

  function verify_property_prereservation_simple_form_without_dates() {
    var tag=document.getElementById('occupants');

    var occupants=parseInt(tag.options[tag.selectedIndex].value);
    var all_occupants=parseInt(tag.options[tag.selectedIndex].value);

    return true;
  }


  function verify_prereservation_form() {
    var location_tag=document.getElementById('location_id');
    var location_id=location_tag.options[location_tag.selectedIndex].value;
    if ( location_id == '0') { alert("Please select a location!"); return false; }
    var condo_type=document.getElementById('condo_type_id');
    var ctype_id=condo_type.options[condo_type.selectedIndex].value;
    if ( ctype_id == '0') { alert("Please select condo type!"); return false; }

    var tag=document.getElementById('occupants');
    if ( tag.options[tag.selectedIndex].value == '0') { alert("Please select a number of occupants!"); return false; }

    var occupants=parseInt(tag.options[tag.selectedIndex].value);
    var all_occupants=parseInt(tag.options[tag.selectedIndex].value);
    tag=document.getElementById('occupants_small');
    all_occupants+=parseInt(tag.options[tag.selectedIndex].value);

    var date_begin=document.getElementById('startdate').value;
    var date_end=document.getElementById('enddate').value;
    if (! check_usdate(date_begin)) { alert(date_begin+ ' is not a valid date value. Please use mm/dd/yy format.'); return false; }
    if (! check_usdate(date_end)) { alert(date_end + ' is not a valid date value. Please use mm/dd/yy format.'); return false; }
    if (! compare_dates_strong(date_begin, date_end)) { alert('Please select a valid reservation period! Begin date can not be later or equal the end date.');  return false;}

    return true;
  }

  function step1_update_lodgingtype_locations() {
    var company_tag=document.getElementById('company_id');
    var company_id=company_tag.value;
    var lodging_type_tag=document.getElementById('lodging_type_id');
    var lodging_type_id=lodging_type_tag.value;
    xajax_ajax_update_lodgingtype_locations(company_id, lodging_type_id);
    if (reservations_multilocations_enable_update) {
    	var div=document.getElementById('ajax_available_units');
    	div.innerHTML="&nbsp;";
    }
  }

  function step1_property_update_lodgingtype_locations() {
    var company_tag=document.getElementById('company_id');
    var company_id=company_tag.value;
    var lodging_type_tag=document.getElementById('lodging_type_id');
    var lodging_type_id=lodging_type_tag.value;
    xajax_ajax_update_lodgingtype_locations(company_id, lodging_type_id, '# of bedrooms');
    if (reservations_multilocations_enable_update) {
    	var div=document.getElementById('ajax_available_units');
    	div.innerHTML="&nbsp;";
    }
  }

  function step1_update_lodgingtype_condotypes() {
    var company_tag=document.getElementById('company_id');
    var company_id=company_tag.value;
    var location_tag=document.getElementById('location_id');
    var location_id=location_tag.value;
    var lodging_type_tag=document.getElementById('lodging_type_id');
    var lodging_type_id=lodging_type_tag.value;
    xajax_ajax_update_lodgingtype_condotypes(company_id, lodging_type_id, location_id);
    if (reservations_multilocations_enable_update) {
    	var div=document.getElementById('ajax_available_units');
    	div.innerHTML="&nbsp;";
    }
  }

  function step1_property_update_lodgingtype_condotypes() {
    var company_tag=document.getElementById('company_id');
    var company_id=company_tag.value;
    var location_tag=document.getElementById('location_id');
    var location_id=location_tag.value;
    var lodging_type_id=3;
    xajax_ajax_update_lodgingtype_condotypes(company_id, lodging_type_id, location_id, '# of bedrooms');
    if (reservations_multilocations_enable_update) {
    	var div=document.getElementById('ajax_available_units');
    	div.innerHTML="&nbsp;";
    }
  }

  function show_step1_lodgingtype_available_units(show_image) {
    var div=document.getElementById('ajax_available_units');
    if (verify_prereservation_simple_form()) {
      var company_tag=document.getElementById('company_id');
      var company_id=company_tag.value;
      var lodging_type_tag=document.getElementById('lodging_type_id');
      var lodging_type_id=lodging_type_tag.value;
      var location_tag=document.getElementById('location_id');
      var location_id=location_tag.options[location_tag.selectedIndex].value;
      var tag=document.getElementById('condo_type_id');
      var condo_type_id=parseInt(tag.options[tag.selectedIndex].value);
      var startdate=document.getElementById('startdate').value;
      var enddate=document.getElementById('enddate').value;
      var tag=document.getElementById('occupants');
      var occupants=parseInt(tag.options[tag.selectedIndex].value);
      var tag=document.getElementById('occupants_small');
      var occupants_small=parseInt(tag.options[tag.selectedIndex].value);

      var travelagent_code='';
      var tag=document.getElementById('travelagent_code');
      if ( typeof(tag)  != "undefined" && tag != null  ) { travelagent_code=tag.value; }
      var filter_unit_name='';
      var tag=document.getElementById('filter_unit_name');
      if ( typeof(tag)  != "undefined" && tag != null  ) { filter_unit_name=tag.value; }

      //pets
      var pets_tag=document.getElementById('pets');
      var pets=0;
      if (pets_tag !=null  && typeof(pets_tag) != 'undefined') { pets=pets_tag.value; }

      div.innerHTML="<br/><br/><br/><img src='/pmt_common/images/bigrotation.gif' border='0' /><br/><br/><br/><br/>";
      xajax_ajax_update_lodgingtype_available_units_matrix(company_id, lodging_type_id, location_id, condo_type_id, startdate, enddate, occupants, occupants_small, travelagent_code, 'ajax_available_units', show_image, filter_unit_name, pets);
      return true;
    } else {
      return false;
    }
  }

  function version2_show_step1_lodgingtype_available_units(form_id) {
	    var div=document.getElementById('ajax_available_units');
	    if (verify_prereservation_simple_form()) {
	      div.innerHTML="<br/><br/><br/><img src='/pmt_common/images/bigrotation.gif' border='0' /><br/><br/><br/><br/>";
	      xajax_ajax2_update_lodgingtype_available_units_matrix(xajax.getFormValues('form_id'));
	      return true;
	    } else {
	      return false;
	    }
	  }

  function show_property_step1_lodgingtype_available_units(show_image) {
    var div=document.getElementById('ajax_available_units');
    if (verify_property_prereservation_simple_form()) {
      div.innerHTML="<br/><br/><br/><img src='/pmt_common/images/bigrotation.gif' border='0' /><br/><br/><br/><br/>";
      var company_tag=document.getElementById('company_id');
      var company_id=company_tag.value;
      var home_type_tag=document.getElementById('home_type_id');
      var home_type_id=home_type_tag.value;
      var location_tag=document.getElementById('location_id');
      var location_id=location_tag.options[location_tag.selectedIndex].value;
      var tag=document.getElementById('bedrooms_numbers');
      
      var bedrooms_numbers='-1';
      var tag=document.getElementById('bedrooms_numbers');
      if (tag != null && typeof(tag) != undefined) { bedrooms_numbers=parseInt(tag.value); } 
      
      var startdate=document.getElementById('startdate').value;
      var enddate=document.getElementById('enddate').value;
      var tag=document.getElementById('occupants');
      var occupants=parseInt(tag.options[tag.selectedIndex].value);
      var occupants_small = 0;

      var tag=document.getElementById('smoking_id');
      var smoking=0;
      if (tag != null && typeof(tag)  != "undefined" ) {
    	  smoking=parseInt(tag.value);
      }
      var tag=document.getElementById('pet_friendly_id');
      var pet_friendly=0;
      if (tag != null && typeof(tag)  != "undefined" ) {
    	  pet_friendly=parseInt(tag.value);
      }
      var tag=document.getElementById('pets');
      var pets=0;
      if (tag != null && typeof(tag)  != "undefined" ) {
    	  pets=parseInt(tag.value);
      }

      var travelagent_code='';
      var tag=document.getElementById('travelagent_code');
      if (tag != null && typeof(tag)  != "undefined" ) { travelagent_code=tag.value; }
      var filter_unit_name='';
      var tag=document.getElementById('filter_unit_name');
      if (tag != null && typeof(tag)  != "undefined" ) { filter_unit_name=tag.value; }
      xajax_ajax_update_property_type_available_units_matrix(company_id, home_type_id, location_id, bedrooms_numbers, startdate, enddate, occupants, occupants_small, travelagent_code, 'ajax_available_units', show_image, filter_unit_name, smoking, pet_friendly, pets);
      return true;
    } else {
      return false;
    }
  }

  function show_property_step1_lodgingtype_available_units_without_dates(show_image) {
    var div=document.getElementById('ajax_available_units');

	var startdate = 0;
	var enddate = 0;

	if (document.getElementById('startdate') && document.getElementById('startdate').value != ''){
		startdate = document.getElementById('startdate').value;
	}

	if (document.getElementById('enddate') && document.getElementById('enddate').value != ''){
		enddate = document.getElementById('enddate').value;
	}

	var is_valid = startdate == 0 && enddate == 0 ?  verify_property_prereservation_simple_form_without_dates() : verify_property_prereservation_simple_form();

    if (is_valid) {
      var company_tag=document.getElementById('company_id');
      var company_id=company_tag.value;
      var home_type_tag=document.getElementById('home_type_id');
      var home_type_id=home_type_tag.value;
      var location_tag=document.getElementById('location_id');
      var location_id=location_tag.value;
      var bedrooms_numbers='-1';
      var tag=document.getElementById('bedrooms_numbers');
      if (tag != null && typeof(tag) != undefined) { bedrooms_numbers=parseInt(tag.value); } 

      var tag=document.getElementById('occupants');
      var occupants=parseInt(tag.value);
      var occupants_small = 0;

      var tag=document.getElementById('smoking_id');
      var smoking=0;
      if (tag != null && typeof(tag)  != "undefined" ) {
    	  smoking=parseInt(tag.value);
      }
      var tag=document.getElementById('pet_friendly_id');
      var pet_friendly=0;
      if (tag != null && typeof(tag)  != "undefined" ) {
    	  pet_friendly=parseInt(tag.value);
      }
      var tag=document.getElementById('pets');
      var pets=0;
      if (tag != null && typeof(tag)  != "undefined" ) {
    	  pets=parseInt(tag.value);
      }

      var travelagent_code='';
      var tag=document.getElementById('travelagent_code');
      if (tag != null && typeof(tag)  != "undefined" ) { travelagent_code=tag.value; }
      var filter_unit_name='';
      var tag=document.getElementById('filter_unit_name');
      if (tag != null && typeof(tag)  != "undefined" ) { filter_unit_name=tag.value; }

      div.innerHTML="<center><br/><br/><br/><img src='/pmt_common/images/bigrotation.gif' border='0' /><br/><br/><br/><br/></center>";

      xajax_ajax_update_property_type_available_units_matrix(company_id, home_type_id, location_id, bedrooms_numbers, startdate, enddate, occupants, occupants_small, travelagent_code, 'ajax_available_units', show_image, filter_unit_name, smoking, pet_friendly, pets);

      return true;
    } else {
      return false;
    }
  }


  function step1_update_condotypes() {
    var company_tag=document.getElementById('company_id');
    var company_id=company_tag.value;
    var location_tag=document.getElementById('location_id');
    var location_id=location_tag.options[location_tag.selectedIndex].value;
    xajax_ajax_update_condotypes(company_id, location_id);
    if (reservations_multilocations_enable_update) {
    	var div=document.getElementById('ajax_available_units');
    	div.innerHTML="&nbsp;";
    }
  }

  function step1_update_occupants() {
    var location_tag=document.getElementById('location_id');
    var condo_type_tag=document.getElementById('condo_type_id');
    var location_id=location_tag.options[location_tag.selectedIndex].value;
    var condo_type_id=condo_type_tag.options[condo_type_tag.selectedIndex].value;
    xajax_ajax_update_occupants(condo_type_id, location_id);
    if (reservations_multilocations_enable_update) {
    	var div=document.getElementById('ajax_available_units');
    	div.innerHTML="&nbsp;";
    }
  }

  function step2_update_occupants() {
    var location_tag=document.getElementById('location_id');
    var condo_type_tag=document.getElementById('condo_type_id');
    var location_id=location_tag.options[location_tag.selectedIndex].value;
    var condo_type_id=condo_type_tag.options[condo_type_tag.selectedIndex].value;
    xajax_ajax_update_occupants(condo_type_id, location_id);
    if (reservations_multilocations_enable_update) {
    	var div=document.getElementById('ajax_available_units');
    	div.innerHTML="&nbsp;";
    }
    document.getElementById('reservation_table2').style.display='none';
  }

  function show_step1_available_units(show_image) {
    var div=document.getElementById('ajax_available_units');
    if (verify_prereservation_simple_form()) {
			div.innerHTML="<br/><br/><br/><img src='/pmt_common/images/bigrotation.gif' border='0' /><br/><br/><br/><br/>";
			var company_tag=document.getElementById('company_id');
      var company_id=company_tag.value;
      var location_tag=document.getElementById('location_id');
      var location_id=location_tag.options[location_tag.selectedIndex].value;
      var tag=document.getElementById('condo_type_id');
      var condo_type_id=parseInt(tag.options[tag.selectedIndex].value);
      var startdate=document.getElementById('startdate').value;
      var enddate=document.getElementById('enddate').value;
      var tag=document.getElementById('occupants');
      var occupants=parseInt(tag.options[tag.selectedIndex].value);
      var tag=document.getElementById('occupants_small');
      var occupants_small=parseInt(tag.options[tag.selectedIndex].value);

      var travelagent_code='';
      var tag=document.getElementById('travelagent_code');
      if ( typeof(tag)  != "undefined" && tag != null  ) { travelagent_code=tag.value; }
      xajax_ajax_update_available_units_matrix(company_id, location_id, condo_type_id, startdate, enddate, occupants, occupants_small, travelagent_code, 'ajax_available_units', show_image);
      return true;
    } else {
      return false;
    }
  }

  function show_step1_price_matrix() {
    var div=document.getElementById('ajax_available_units');
    if (verify_prereservation_form()) {
      div.innerHTML="<br/><br/><br/><img src='/pmt_common/images/bigrotation.gif' border='0' /><br/><br/><br/><br/>";
      var tag=document.getElementById('location_id');
      var location_id=parseInt(tag.value);
      var tag=document.getElementById('condo_type_id');
      var condo_type_id=parseInt(tag.options[tag.selectedIndex].value);
      var startdate=document.getElementById('startdate').value;
      var enddate=document.getElementById('enddate').value;
      var tag=document.getElementById('occupants');
      var occupants=parseInt(tag.options[tag.selectedIndex].value);
      var tag=document.getElementById('occupants_small');
      var occupants_small=parseInt(tag.options[tag.selectedIndex].value);

      var travelagent_code='';
      var tag=document.getElementById('travelagent_code');
      if ( typeof(tag)  != "undefined" && tag != null  ) { travelagent_code=tag.value; }
      xajax_ajax_update_pricing_matrix(location_id, condo_type_id, startdate, enddate, occupants, occupants_small, 1, travelagent_code);
      return true;
    } else {
      return false;
    }
  }

  function show_step2_price_matrix() {
    var div=document.getElementById('ajax_pricing_matrix');
    if (verify_prereservation_form()) {
      div.innerHTML="<br/><br/><br/><img src='/pmt_common/images/bigrotation.gif' border='0' /><br/><br/><br/><br/>";
      var tag=document.getElementById('location_id');
      var location_id=parseInt(tag.value);
      var tag=document.getElementById('condo_type_id');
      var condo_type_id=parseInt(tag.options[tag.selectedIndex].value);
      var startdate=document.getElementById('startdate').value;
      var enddate=document.getElementById('enddate').value;
      var tag=document.getElementById('occupants');
      var occupants=parseInt(tag.options[tag.selectedIndex].value);
      var tag=document.getElementById('occupants_small');
      var occupants_small=parseInt(tag.options[tag.selectedIndex].value);

      var travelagent_code='';
      var tag=document.getElementById('travelagent_code');
      if ( typeof(tag)  != "undefined" && tag != null ) { travelagent_code=tag.value; }
      xajax_ajax_update_pricing_matrix(location_id, condo_type_id, startdate, enddate, occupants, occupants_small, 2, travelagent_code);
      return true;
    } else {
      return false;
    }
  }

  //new - ajax functions
  function show_available_listings(form_id, div_id) {
	    var div=document.getElementById(div_id);
	    div.innerHTML="<br/><br/><br/><img sc='/pmt_common/images/bigrotation.gif' border='0' /><br/><br/><br/><br/>";
	    xajax_ajax_update_available_listings(xajax.getFormValues(form_id));
  }

  function verify_search_simple_form(form_id) {
	    var form=document.getElementById(form_id);

	    var occupants=parseInt(form.occupants.value);
	    if ( occupants == '0') { alert("Please select a number of occupants!"); return false; }

	    var date_begin=form.startdate.value;
	    var date_end=form.enddate.value;
	    if (! check_usdate(date_begin)) { alert(date_begin+ ' is not a valid date value. Please use mm/dd/yy format.'); return false; }
	    if (! check_usdate(date_end)) { alert(date_end + ' is not a valid date value. Please use mm/dd/yy format.'); return false; }
	    if (! compare_dates_strong(date_begin, date_end)) { alert('Please select a valid reservation period! Begin date can not be later or equal the end date.');  return false;}

	    return true;
  }

  function update_available_listings(form_id, div_id) {
	    var div=document.getElementById(div_id);
	    if (verify_search_simple_form(form_id)) {
	      div.innerHTML="<br/><br/><br/><img src='/pmt_common/images/bigrotation.gif' border='0' /><br/><br/><br/><br/>";
	      xajax_ajax_update_available_listings(xajax.getFormValues(form_id));
	      return true;
	    } else {
	      return false;
	    }
}

