var webroot = "http://www.natomasrotary.org";
var proxy = "./scripts/ajax_proxy.php?url=";

function setThrobber(obj) {
  obj.innerHTML = "<div style=\"width: 100%; margin: 0 auto; text-align: center;\">" +
                  "<img src=\"../images/throbber.gif\" alt=\"Loading&#8230;\" title=\"Loading&#8230;\" />\n" +
                  "<p>Loading&#8230;</p>" +
                  "</div>";
}

function openPipe() {
  var pipe = false;
/*@cc_on
  @if (@_jscript_version >= 5)
    try {
      pipe = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e){
      try {
        pipe = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e2){
        pipe = false;
      }
    }
  @end
@*/

  if (!pipe && typeof XMLHttpRequest != 'undefined') {
    pipe = new XMLHttpRequest();
  }
  return pipe;
}

function getCalendar(date) {
  var obj = document.getElementById("calendar");
  var calendarPipe = openPipe();
  if (calendarPipe == null) {
    alert("This browser does not support AJAX. Some objects \nmay not render correctly.");
    return;
  }
  calendarPipe.onreadystatechange = function() {
    with(calendarPipe) {
      if ((readyState == 4) && (status == 200)) {
        obj.innerHTML = responseText;
      }
    }
  }
  calendarPipe.open("GET", proxy + encodeURIComponent("/scripts/calendar_builder.php?sd="+date), true);
  calendarPipe.send(null);
}

function getCalendarAdmin(date, perspective, pk) {
  if (pk === undefined) {
    pk = '';
  } else {
    pk = "&pk="+pk;
  }
  var obj = document.getElementById("calendar_admin");
  var calendarPipe = openPipe();
  if (calendarPipe == null) {
    alert("This browser does not support AJAX. Some objects \nmay not render correctly.");
    return;
  }
  calendarPipe.onreadystatechange = function() {
    with(calendarPipe) {
      if ((readyState == 4) && (status == 200)) {
        obj.innerHTML = responseText;
      }
    }
  }
  calendarPipe.open("GET", "../" + proxy + encodeURIComponent("/scripts/calendar_admin.php?sd="+date+"&p="+perspective+pk), true);
  calendarPipe.send(null);
}

function getEventInfo(e, event_pk) {
  var obj = document.getElementById("event_info");
  var eventInfoPipe = openPipe();
  var cursor = getMousePosition(e);
  if (eventInfoPipe == null) {
    alert("This browser does not support AJAX. Some objects \nmay not render correctly.");
    return;
  }
  eventInfoPipe.onreadystatechange = function() {
    with(eventInfoPipe) {
      if ((readyState == 4) && (status == 200)) {
        obj.innerHTML = responseText;
        showElement(obj.id);
        obj.style.top = cursor.y + 'px';
        obj.style.left = cursor.x + 'px';
      }
    }
  }
  eventInfoPipe.open("GET", proxy + encodeURIComponent("/scripts/event_data.php?e="+event_pk), true);
  eventInfoPipe.send(null);
}

function getGallery(div_id, url) {
  var obj = document.getElementById(div_id);
  var galleryPipe = openPipe();
  if (galleryPipe == null) {
    alert("This browser does not support AJAX. Some objects \nmay not render correctly.");
    return;
  }
  galleryPipe.onreadystatechange = function() {
    with(galleryPipe) {
      if ((readyState == 4) && (status == 200)) {
        obj.innerHTML = responseText;
      }
    }
  }
  galleryPipe.open("GET", proxy + encodeURIComponent("/scripts/gallery_builder.php?" + url), true);
  galleryPipe.send(null);
}

function getEditPhotosTable(form_name, div_id, sql, gallery) {
  var obj = document.getElementById(div_id);

  setThrobber(obj);

  var editPhotosPipe = openPipe();
  var args = "";
  if (editPhotosPipe == null) {
    alert("This browser does not support AJAX. Some objects \nmay not render correctly.");
    return;
  }
  editPhotosPipe.onreadystatechange = function() {
    with(editPhotosPipe) {
      if ((readyState == 4) && (status == 200)) {
        obj.innerHTML = responseText;
      }
    }
  }
  args = "url=" + encodeURIComponent("/scripts/edit_photos_builder.php");
  args += "&sql=" + sql + "&form=" + form_name;
  args += (gallery) ? "&gallery=" + gallery : "";
  with(editPhotosPipe) {
    open("POST", '../scripts/ajax_proxy.php' , true);
    setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    setRequestHeader("Content-length", args.length);
    setRequestHeader("Connection", "close");
    send(args);
  }
}