function makeXmlhttp() {
  var xmlhttp = false;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(x) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch(x) { xmlhttp = false; }}
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp = new XMLHttpRequest();
  return xmlhttp;
}

function setSlot(id, body) {
    //    alert(id+' '+body);
  if(typeof body == 'undefined') return;
  if(document.getElementById) slot = document.getElementById(id);
  else if(document.all) slot = document.all[id];
  else return;
  if(slot) slot.innerHTML = body;
  var next = 0;
  while((scriptStart = body.indexOf("<script>", next))!=-1) {
    next = body.indexOf("</script>", scriptStart);
    eval(body.substring(scriptStart+8, next));
  }
}

function setSlots(text) {
  var next = 0;
  while((updateStart = text.indexOf("<update ", next))!=-1) {
    next = text.indexOf("</update>", updateStart);
    idEnd = text.indexOf(">", updateStart+1);
    id = text.substring(updateStart+8, idEnd);
    body = text.substring(idEnd+1, next);
    setSlot(id, body);
  }
}

function fetchSlotsDebug(url) {
  document.body.style.cursor="wait";
  var xmlhttp = makeXmlhttp();
  xmlhttp.open("GET", url, true);
  xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
  xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState==4) { setSlots(xmlhttp.responseText); document.body.style.cursor="default" }}
  xmlhttp.send(null);
}

function fetchSlots(url) {
  document.body.style.cursor="wait";
  var xmlhttp = makeXmlhttp();
  xmlhttp.open("GET", url, true);
  xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
  xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState==4) { setSlots(xmlhttp.responseText); document.body.style.cursor="default" }}
  xmlhttp.send(null);
}

function fetchSlots_nocursor(url) {
  var xmlhttp = makeXmlhttp();
  xmlhttp.open("GET", url, true);
  xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
  xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState==4) { setSlots(xmlhttp.responseText) }}
  xmlhttp.send(null);
}

function fetchSlotsFromPost(url, postData) {
  document.body.style.cursor="wait";
  var xmlhttp = makeXmlhttp();
  xmlhttp.open("POST", url, true);
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState==4) { setSlots(xmlhttp.responseText); document.body.style.cursor="default" }}
  xmlhttp.send(postData);
}

function fetchSlotsFromPostDebug(url, postData) {
  document.body.style.cursor="wait";
  var xmlhttp = makeXmlhttp();
  xmlhttp.open("POST", url, true);
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState==4) { setSlots(xmlhttp.responseText); document.body.style.cursor="default" }}
  xmlhttp.send(postData);
  alert("posted ok: "+url+" "+postData);
}

function fetchSlotsFromElementList(url, elementList, extraArguments) {
  var postData = '';
  for(i=0; i<elementList.length; i++) {
    element = document.getElementById(elementList[i]);
    postData = postData + elementList[i] + '=' + escape(element.value);
    if(i!=elementList.length-1) postData = postData + '&';
  }
  postData = postData + '&' + extraArguments;
  fetchSlotsFromPost(url, postData);
}

function fetchSlotsFromForm(url, formId) {
  var form = document.getElementById(formId);
  var postData = '';
  for(i=0; i<form.elements.length; i++) if(form.elements[i].name) {
    if(form.elements[i].type=='checkbox' && !form.elements[i].checked) continue;
    postData = postData + form.elements[i].name + '=' + escape(form.elements[i].value) + '&';
  }
  postData += 'update=1';
  fetchSlotsFromPost(url, postData);
}

function fetchNewSlotsFromForm(url, formId) {
  var form = document.getElementById(formId);
  var postData = '';
  for(i=0; i<form.elements.length; i++) if(form.elements[i].name) {
    if(form.elements[i].type=='checkbox' && !form.elements[i].checked) continue;
    postData = postData + form.elements[i].name + '=' + escape(form.elements[i].value) + '&';
  }
  postData += 'newUpdate=1';
  fetchSlotsFromPost(url, postData);
}

function fetchSlotsFromFormDebug(url, formId) {
  var form = document.getElementById(formId);
  var postData = '';
  for(i=0; i<form.elements.length; i++) if(form.elements[i].name)
    postData = postData + form.elements[i].name + '=' + escape(form.elements[i].value) + '&';
  postData += 'update=1';
  fetchSlotsFromPostDebug(url, postData);
}

function fetchSlot(slotId, url, eltList) {
//  if(eltList) return fetchSlotFromPost(slotId, url, elementPostData(eltList));
  if(eltList) {
    if(url.indexOf("?")>=0) url += '&';
    else url += '?';
    url += elementPostData(eltList);
  }
  //  pleaseWait(slotId);
  var xmlhttp = makeXmlhttp();
  xmlhttp.open("GET", url, true);
  xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
  xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState==4) { setSlot(slotId, xmlhttp.responseText); doneWaiting(slotId) }}
  xmlhttp.send(null);
}

function fetchSlotFromPost(slotId, url, postData) {
  pleaseWait(slotId);
  var xmlhttp = makeXmlhttp();
  xmlhttp.open("POST", url, true);
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState==4) { setSlot(slotId, xmlhttp.responseText); }}
  xmlhttp.send(postData);
}

function fetchSlotFromForm(slotId, url, formId) {
  var form = document.getElementById(formId);
  var postData = '';
  for(i=0; i<form.elements.length; i++) if(form.elements[i].name)
    postData = postData + form.elements[i].name + '=' + escape(form.elements[i].value) + '&';
  postData += 'update=1';
  fetchSlotFromPost(slotId, url, postData);
}

function loadQuicktimeVideo(name) {
  var text = '<OBJECT WIDTH=320 HEIGHT=256 CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">';
  text += '<PARAM name=SRC VALUE="http://goasu.com/autoimage/'+name+'"><PARAM name=AUTOPLAY VALUE=true><PARAM name=CONTROLLER VALUE=true>';
  text += '<PARAM name=SCALE value=aspect><EMBED SRC="http://goasu.com/autoimage/'+name+'" WIDTH=320 HEIGHT=256 ';
  text += 'AUTOPLAY=true CONTROLLER=true PLUGINSPAGE="http://www.apple.com/quicktime/download/" SCALE=aspect></EMBED></OBJECT>';
  setSlot('mediasample', text);
}

function loadWMVideo(name) {
  var text = '<OBJECT WIDTH=320 HEIGHT=256 CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">';
  text += '<PARAM name=SRC VALUE="http://goasu.com/autoimage/'+name+'"><PARAM name=AUTOPLAY VALUE=true><PARAM name=CONTROLLER VALUE=true>';
  text += '<PARAM name=SCALE value=aspect><EMBED SRC="http://goasu.com/autoimage/'+name+'" WIDTH=320 HEIGHT=256 ';
  text += 'AUTOPLAY=true CONTROLLER=true PLUGINSPAGE="http://www.apple.com/quicktime/download/" SCALE=aspect></EMBED></OBJECT>';
  setSlot('mediasample', text);
}
