// calendar.js
// XMLHTTP INLINE CALENDAR (requires xmlhttp.js)

function toggle_calendar(me)
{ var div = document.getElementById('calendar_window');
  
  if(me && div.style.visibility == 'visible')
  { div.style.visibility = 'hidden';
    document.getElementById('route').style.width = s_width;
  } 
  else
  { if(!s_width) s_width = get_style('route','width');
    document.getElementById('route').style.width = "200px";
    request = createrequest();
    if(request)
    { request.onreadystatechange = function()
      { if(request.readyState == 4)
        { if(request.status == 200)
          { if(me)
            { var pos = findpos(me);
	      div.style.left = pos.x+'px';
	      div.style.top = (pos.y-152)+'px';
	    }
	    div.innerHTML = request.responseText;
	    div.style.visibility = "visible";
          }
        }      	
      }
      myurl = location.protocol+"//"+document.domain;
      request.open("GET",myurl+"/booking/core/calendar.php?d="+day+"&m="+month+"&y="+year,true);
      request.send(null);
    }
  }
}

function findpos(obj)
{ var curleft = curtop = 0;
  if(obj.offsetParent)
  { curleft = obj.offsetLeft;
    curtop = obj.offsetTop;
    while(obj = obj.offsetParent)
    { curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    }
  }
  return {x:curleft,y:curtop};
}

//FIX DATE
function fix_date(d,m,y)
{ set_selected('day',d);
  set_selected('month',m);
  set_selected('year',y);
  toggle_calendar(1);
}

//UPDATE
function change_date(d,m,y)
{ day = d; month = m; year = y;
  toggle_calendar(null);
}

function change_date_select()
{ day = calc_selected('day');
  month = calc_selected('month');
  year = calc_selected('year');
  var div = document.getElementById('calendar_window');
  if(div.style.visibility == 'visible') toggle_calendar(null);
}

// REPOSITION CALANDAR ON RESIZE
function handle_resize()
{ var div = document.getElementById('calendar_window');
  if(div.style.visibility == 'visible')
  { var pos = findpos(document.getElementById('calendar_ln'));
    div.style.left = pos.x+'px';
  }
}

// RETREIVE STYLE
function get_style(el,styleid)
{ var x = document.getElementById(el);
  if(x.currentStyle)
   var y = x.currentStyle[styleid];
  else if(window.getComputedStyle)
   var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleid);
  return y;
}

var s_width; // STORE THE SELECT WIDTH
window.onresize = handle_resize;

