//check script is compiling!
//alert('javascript wins!');

function initAjax(){
//Ajax browser sniffer from w3schools
var xmlHttp;

try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer, cascading versions
  try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
        xmlHttp = false;
      }
    }
  }
  return xmlHttp;
}

function ajaxCallWithLoad(jobName, divName, funcName){
  var phone = initAjax();
  /*//set the loading div to the place of the new div and display it
  setPos(document.getElementById(divName), document.getElementById('loading'));*/
  document.getElementById('loading').style.border='none';
  document.getElementById('loading').style.background='none';
  document.getElementById('loading').style.display='block';
  //switch the ready states...
  phone.onreadystatechange=function()
  {
    //we only care about it now when it's finished getting a whole response from php
    if(phone.readyState==4)
    {
      //check for error
      if(phone.responseText == 'error'){
        //Ajax call to php has failed for some reason - alert the user and fail.
        alert('An error has occured, please try again');
      }
      else{
        //Ajax call has succeeded... call the appropriate function to update teh page
        document.getElementById('loading').style.display='none';
        scriptThis=(function(){return this;})(); //we are in an XMLHttp object, therefore we need to do this to get the parent script's 'this' instance
        scriptThis[funcName](divName, phone.responseText); //call the given function name from the parent script and pass it the response text and the div name to update
      }
    }
  }
  phone.open('GET', 'index.php?page=signups&job=' +jobName +'&ajax=true&divname=' +divName , true);
  phone.send(null);
}

function signups_showEventsList(divName, response){
  document.getElementById(divName).innerHTML=response;
  document.getElementById(divName).style.display='block';
}

function showSignupsEventsMenu(tr_obj, tr_type, tr_date){
  var specific = '&edate=' +tr_date +'&etype=' +tr_type;
  var content = '<a href="javascript:ajaxCallWithLoad(\'viewspecific' +specific +'\', \'signups_opt_result\', \'signups_showEventsList\')" onclick="document.getElementById(\'signups_edit_signups\').style.display=\'none\'">View Signups</a><br />';
  content    += '<a href="javascript:ajaxCallWithLoad(\'allUsersStatus' +specific +'\', \'signups_edit_signups\', \'signups_showEventsList\')" onclick="document.getElementById(\'signups_opt_result\').style.display=\'none\'">Edit Signups</a><br /><br />';
  content    += '<a href="index.php?page=events&job=edit' +specific +'">Edit This Event</a><br /><br />';
  content    += '<a href="index.php?page=events&job=delete' +specific +'">Delete This Event</a><br />';
  
  document.getElementById("signups_events_opts_menu").innerHTML=content;
  //move div to position of tr_obj
  setRelPos(tr_obj, document.getElementById("signups_events_opts_menu")); //set the position
  //show div
  //document.getElementById("signups_events_opts_menu").setAttribute("style", document.getElementById("signups_events_opts_menu").getAttribute("style") +"display:block");
  document.getElementById("signups_events_opts_menu").style.display='block';
}

//sets the position of an element el2 to the rightmost-side of element el1 (for objects within the 'main' div)
function setRelPos(el1, el2){
  var done=0;
  var newtop = 0;
  var newleft = el1.offsetWidth +10;
  do{
    newtop  += el1.offsetTop;
    newleft += el1.offsetLeft;
    el1 = el1.offsetParent;
  }while((el1) && el1.id != 'main');
  el2.style.position='absolute';
  el2.style.top=newtop +'px';
  el2.style.left=newleft +'px';
}
//same but sets the position to the actual position
function setPos(el1, el2){
  var done=0;
  var newtop = 0;
  var newleft = 0;
  do{
    newtop  += el1.offsetTop;
    newleft += el1.offsetLeft;
    el1 = el1.offsetParent;
  }while((el1) && el1.id != 'main');
  el2.style.position='absolute';
  el2.style.top=newtop +'px';
  el2.style.left=newleft +'px';
}

function updateSchedule(divName, response){
  alert('Status changed successfully!');
}
