  var calLoaded=null;
  var calStartDate=null;
  var calStartCell=null;
  var calSelection=null;
  var calPromptCell=null;
  var calDiv=null;
  var calStandby=null;
  function calLoad(dst,pid,start) {
    calLoaded=false;
    if (dst) calDiv=dst;
    if (!calDiv) return false;
    if (!calStandby) calStandby=calDiv.innerHTML; else calDiv.innerHTML=calStandby;
    new ajax('/calendar.php?pID='+pid+(start?'&start='+start:''),{onComplete:function(req) {
      calDiv.innerHTML=req.responseText;
      var cs=calSelection;
      calSelection=null;
      calLoaded=true;
      if (cs) calSetSelection(cs.from,cs.to);
    }});
  }
  function calGetDate(td) {
    if (!td || !td.id) return null;
    var dp=td.id.match(/(\d\d)(\d\d)(\d\d)/);
    if (!dp) return null;
    var d=new Date(Number(dp[1])+2000,Number(dp[2])-1,Number(dp[3]),12,0,0);
//    d.setTime(d.getTime()-d.getTimezoneOffset()*60000);
    return d;
  }
  function calGetCell(date) {
    return $('cal'+String(date.getFullYear()).substr(2,2)+String(date.getMonth()+101).substr(1,2)+String(date.getDate()+100).substr(1,2));
  }
  function calSelectCell(date,f) {
    var td=calGetCell(date);
    if (td) {
      if (f) td.className+=' cal_day_selected';
      else td.className=td.className.replace(/ cal_day_selected\b/g,'');
    }
  }
  function calCheckCell(date) {
    var td=calGetCell(date);
    if (!td) return false;
    return !td.className.match(/\bcal_day_na\b/);
  }
  function calCellIsSpecial(date) {
    var td=calGetCell(date);
    if (!td) return false;
    return td.className.match(/\bcal_day_special\b/);
  }
  function calSelectRange(from,to,f) {
//  $('debug').innerHTML+=from+' - '+to+' '+f+'<br>\n';
    var d=new Date();
    for (d.setTime(from.getTime());d.getTime()<=to.getTime()+14400000;d.setTime(d.getTime()+86400000)) calSelectCell(d,f);
  }
  function calCheckRange(from,to) {
    var d=new Date();
    for (d.setTime(from.getTime());d.getTime()<=to.getTime()+14400000;d.setTime(d.getTime()+86400000)) if (!calCheckCell(d)) return false;
    return true;
  }
  function calDayOffset(date,off) {
    var d=new Date();
    d.setTime(date.getTime()+86400000*off);
    return d;
  }
  function calSetSelection(from,to) {
    if (from>to) return calSetSelection(to,from);
    if (!calLoaded) {
      calSelection={from:from,to:to};
      return;
    }
    if (from && to && from<to) {
      from.setHours(12);
      to.setHours(12);
      if (calSelection) {
        if (from<=calSelection.to && to>=calSelection.from) {
          calSelectRange(from,calDayOffset(calSelection.from,-1),true);
          calSelectRange(calDayOffset(calSelection.to,1),to,true);
          calSelectRange(calSelection.from,calDayOffset(from,-1),false);
          calSelectRange(calDayOffset(to,1),calSelection.to,false);
        } else {
          calSelectRange(calSelection.from,calSelection.to,false);
          calSelectRange(from,to,true);
        }
      } else calSelectRange(from,to,true);
      calSelection={from:from,to:to};
    } else if (calSelection) {
      calSelectRange(calSelection.from,calSelection.to,false);
      calSelection=null;
    }
    var ngts=(to-from)/86400000;
    if (ngts>0) calSetPrompt(ngts>1?ngts+' nights':'1 night');
  }
  function calCheckSelection(from,to) {
    if (from>to) return calCheckSelection(to,from);
    if (from && to) {
      if (calSelection && (from<calSelection.to && to>calSelection.from))
        return calCheckRange(from,calSelection.from) && calCheckRange(calSelection.to,to);
      else return calCheckRange(from,to);
    }
    return true;
  }
  function calSelectSpecials(date) {
    for (var from=calDayOffset(date,0);calCellIsSpecial(from);from=calDayOffset(from,-1));
    for (var to=calDayOffset(date,0);calCellIsSpecial(to);to=calDayOffset(to,1));
    if (to>from) calSetSelection(calDayOffset(from,1),calDayOffset(to,-1));
  }
  function calSetPrompt(txt) {
    if (calPromptCell) {
      if (!calPromptCell.titleCalSaved) calPromptCell.titleCalSaved=calPromptCell.title;
      calPromptCell.title=txt;
    }
  }
  function calStart(td) {
    calStartDate=calGetDate(td);
    calSelectSpecials(calStartDate);
    return false;
  }
  function calSelect(td) {
    if (calPromptCell && td!=calPromptCell && calPromptCell.titleCalSaved) {
      calPromptCell.title=calPromptCell.titleCalSaved;
      calPromptCell.titleCalSaved=null;
    }
    calPromptCell=td;
    if (!calStartDate) return null;
    var date=calGetDate(td);
    if (calCheckSelection(calStartDate,date)) calSetSelection(calStartDate,date);
  }
  function calEnd(td) {
    calStartDate=null;
    if (calSelection) {
      $('date_from').value=String(calSelection.from.getMonth()+1)+'/'+String(calSelection.from.getDate())+'/'+String(calSelection.from.getFullYear());
      $('date_to').value=String(calSelection.to.getMonth()+1)+'/'+String(calSelection.to.getDate())+'/'+String(calSelection.to.getFullYear());
      checkAvail();
    }
  }
