
// Welcome to the HTMLHttpRequest demonstration!
var docClickLoader = new RemoteFileLoader('docClickLoader');

// At any stage you can call docClickLoader.loadInto('file.html', 'IDOfTarget') to trigger
// a content load into an element from your script.
// The example below does this when suitable links in the document are clicked.

function loadInto(src, destId, evt)
{
 // Called to when a link with class="loadinto-IdOfTarget" is clicked.
 // Parameters: src = reference to link, destId = ID of target element, evt = event object.
 var ok = docClickLoader.loadInto(src.href || src.getAttribute('href'), destId);
 if (ok) cancelEvent(evt);
};

function addInto(src, destId, evt)
{
 // Called to when a link with class="addinto-IdOfTarget" is clicked.
 // Parameters: src = reference to link, destId = ID of target element, evt = event object.

 destId = "schedule"+(parseInt(destId.replace("schedule",""))+1);
 alert(destId);
 var ok = docClickLoader.loadInto(src.href || src.getAttribute('href'), destId);
 if (ok) cancelEvent(evt);
 
};


function toggleInto(src, destId, evt)
{
 // As above, but loads only once and toggles the display of the target.
 var dest = document.getElementById(destId);
 if (!dest.contentLoaded)
 {
  var ok = docClickLoader.loadInto(src.href || src.getAttribute('href'), destId);
  if (ok) dest.contentLoaded = true;
 }
 cancelEvent(evt);
 if (!dest.toggleState)
 {
  src.innerHTML = 'Close: ' + src.innerHTML;
  dest.style.display = 'block';
  dest.toggleState = 1;
 }
 else
 {
  src.innerHTML = src.innerHTML.replace(/^Close: /, '');
  dest.style.display = 'none';
  dest.toggleState = 0;
 }
};


// addEvent is defined within htmlhttprequest.js, feel free to reuse :).
addEvent(document, 'click', function(evt)
{
 // Here we capture all clicks on the document, scanning for links with a CLASS
 // attribute of "loadinto-IdOfTarget" and routing them to loadInto() above.
 evt = evt || window.event;
 var src = evt.target || evt.srcElement;
 if (src.nodeType && src.nodeType != 1) src = src.parentNode;
 // Loop up the DOM tree scanning all elements to find a matching one.
 while (src)
 {
  var srcName = (src.nodeName||src.tagName||'').toLowerCase();
  if (srcName == 'a' && src.className && src.className.match(/^(load|add|toggle)into-(.+)$/))
  {
   // Call our load handlers if we have a match; they'll cancel the normal action.
   if (RegExp.$1 == 'load') return loadInto(src, RegExp.$2, evt);
   if (RegExp.$1 == 'toggle') return toggleInto(src, RegExp.$2, evt);
   if (RegExp.$1 == 'add') return addInto(src, RegExp.$2, evt);
  }
  src = src.parentNode;
 }
}, 1);


