  // depends on moA and moB  being set by PHP in host page
  // also minNights, holNights set by PHP in host page
  var realstart;
  var startdate;
  var enddate;
  var orig = true;
  var selecting = false;
  var alerting = false;
  var days = new Array();
  var class_was = new Array();
  var Holi;
  var s, e;

  function collapseSelection(){  var sel;  var ctl;
    if(document.selection && document.selection.empty){
      document.selection.empty();
    }else if(window.getSelection){
      sel=window.getSelection();
      if(sel && sel.removeAllRanges){
        sel.removeAllRanges(); } } }
  
  function isHoliday(ndx){
    if(class_was[ndx].indexOf('holiday') > 0){
      return true;
    }else{
      return false; } }

  function isDormant(ndx){
    if(class_was[ndx].indexOf('dormant') > 0){
      return true;
    }else{
      return false; } }

  function isReserved(ndx){
    if(class_was[ndx].indexOf('reserved') > 0){
      return true;
    }else{
      return false; } }

  function setDays(){ var i;  var d;  var j = 0;
    // load array sequentially with all days currently displayed
    for(i=1; i<32; i++){  d = moA;
      if(i<10)  d+='-0'+i;  else d+='-'+i; 
      ctl = document.getElementById(d);  if(!ctl) break;
      days[j] = ctl.id;  class_was[j] = ctl.className;  j++; }
    for(i=1; i<32; i++){  d = moB;
      if(i<10)  d+='-0'+i;  else d+='-'+i; 
      ctl = document.getElementById(d);  if(!ctl) break;
      days[j] = ctl.id;  class_was[j] = ctl.className;  j++; } }

  function extendToBarrier(min){  var movement;
    // determine direction of movement
    if(s <= e)  movement=1;  else movement=-1;
    // reset enddate (if needed) to meet minimum nights requirement
    if(Math.abs(e - s) < min) e = (s + (min * movement));
    // walk startdate-to-enddate, looking for barriers & holidays
    i = s;
    while(1==1){  
      if(isHoliday(i)) Holi = true;
      if(i==e) break;
      i = (i + movement);
      if(i < 0 || i > (days.length-1)){ i = (i - movement);  break; }
      if(isReserved(i) && movement==1)                       break; 
      if(isDormant(i)){                 i = (i - movement);  break; } }
    e = i;
    // back up startdate (if needed) to meet minimum nights requirement
    if(Math.abs(e - s) < min){
      i = s;
      while(1==1){  
        i = (i - movement);
        if(i < 0 || i > (days.length-1)){ i = (i + movement);  break; }
        if(isReserved(i) && movement==1)                       break; 
        if(isDormant(i)){                 i = (i + movement);  break; }
        if(isHoliday(i)) Holi = true;
        if(Math.abs(e - i) >= min) break; }
      s = i;
    } // if backup startdate
  }  // end function extendToBarrier

  function dayWalk(sdate, edate){ var i;  var min;
    // locate startdate, enddate
    for(i=0; i<days.length; i++) if(days[i]==edate) break;  e = i;
    for(i=0; i<days.length; i++) if(days[i]==sdate) break;  s = i;
    // step to enddate, checking for reserved days, holidays, etc.
    min = minNights;
    extendToBarrier(min);
    if(Holi){
      min = holNights;
      extendToBarrier(min); }
    if(Math.abs(e - s) < min){
      return false;
    }else{
      return true;
    } } // end function dayWalk
  
  function highlight(){
    var validRange, lo, hi, msg;
    Holi = false; // until proven otherwise
    // assure date array is populated
    if(!days.length || days.length < 3)  setDays(); 
    // toggle all classes to original display
    for(i=0; i<days.length; i++){
      ctl = document.getElementById(days[i]);
      ctl.className = class_was[i]; }
    // use dayWalk to find (valid?) range [s:e] from [startdate:enddate]
    validRange = dayWalk(realstart, enddate.value);
    startdate.value = days[s];  
    if(validRange) enddate.value = days[e];  else enddate.value = '';
    // highlight new range (valid or not)
    if(s <= e){ lo = s;  hi = e; }else{ lo = e;  hi = s; }
    for(i=lo; i<=hi; i++){
      ctl = document.getElementById(days[i]);
      ctl.className = 'cal selected';
      if(i==lo) ctl.className = ctl.className + ' checkin';
      if(i==hi) ctl.className = ctl.className + ' checkout'; }
    // throw error for invalid selection
    if(! validRange){
      // throw error
      if(Holi){
        msg = 'The current selection cannot be'+'\n';
        msg+= 'expanded to '+(holNights + 1)+' days and '+holNights+' nights,'+'\n';
        msg+= 'our established Holiday minimum.\n\n';
        msg+= 'Call us at 866.686.8423 to book\nthese dates.';
        alerting = true;  alert(msg);  alerting = false;
      }else{
        msg = 'The current selection cannot be'+'\n';
        msg+= 'expanded to '+(minNights + 1)+' days and '+minNights+' nights,'+'\n';
        msg+= 'our established minimum booking.\n\n';
        msg+= 'Call us at 866.686.8423 to book\nthese dates.';
        alerting = true;  alert(msg);  alerting = false; }
      // cancel selection
      for(i=0; i<days.length; i++){
        ctl = document.getElementById(days[i]);
        ctl.className = class_was[i]; }
      selecting = false;
      startdate.value = '';
      enddate.value = '';
      return false;
    } // if(! validRange)
    return true;
  } // end function highlight

  function clickOn(ctl){
    if(ctl.className.indexOf('dormant')==-1){
      startdate = document.reservations.startdate;
      startdate.value = ctl.id; 
      realstart = ctl.id;
      selecting = true; } }

  function dragOn(ctl){
    if(selecting && !alerting){
      enddate = document.reservations.enddate;
      enddate.value = ctl.id;
      highlight(); } }
  
  function clickOff(ctl){
    if(selecting && !alerting){
      enddate = document.reservations.enddate;
      enddate.value = ctl.id;
      highlight();
      window.setTimeout('collapseSelection()', 3);
      selecting = false; } }

  function validate(){
    var err = '';
    if(startdate.value=='' || enddate.value=='') 
      err+='Please select a valid date range.\n';
    if(selecting){ // overrides earlier error
      err ='Your selection\'s endpoint is not set:\n';
      err+='    The mouse button was probably\n';
      err+='    released, while out-of-bounds.'; }
    if(err != ''){
      alerting = true;  alert(err);  alerting = false;
      return false;
    }else{
      return true; } }
