//functions
function createXmlHttpRequestObject(){
  //creates XMLHttpRequest object if it is possible
  var xmlhttp
  try {
  //should work on all browsers except IE6 or older
    xmlHttp = new XMLHttpRequest();
  }
  catch (e){
    //browser is IE6 or older
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
    }
    catch (e){
      //ignore error
    }
  }
  if (!xmlHttp)
    alert ("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function process( yy, mm, dd)
{
  var xmlHttp = createXmlHttpRequestObject();//stores XMLHttpRequestObject
  //sends HTTP request
  if (xmlHttp){
    try{
      //create string of parameters
      var params = "syear=" + yy + "&smonth=" + mm + "&sday=" + dd;
      xmlHttp.open("POST", "http://www.jakubcata.eu/compute.php", true);
//     xmlHttp.open("POST", "http://localhost/jakubcata/compute.php", true);
      xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
      xmlHttp.onreadystatechange = handleRequestStateChange;
      xmlHttp.send(params);

    }
    catch (e){
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}

function handleRequestStateChange(){
  //request state has changed
  if (xmlHttp.readyState==4){//process is completed
    if (xmlHttp.status==200){
    //http status is OK
      try {
        var xmlResponse = xmlHttp.responseXML;
        //process response
        var xmlRoot = xmlResponse.documentElement;
        var responseText = xmlRoot.firstChild.data;
        respDiv = document.getElementById("response");
        respDiv.innerHTML = "";
        respDiv.innerHTML += responseText;
        respMainDiv = document.getElementById("response_box");
        respMainDiv.style.display = "block";
      }
      catch (e){
        alert("Error reading the response: " + e.toString());
      }
    }
  }
}

function showcall(mm)
{
  var xmlHttp = createXmlHttpRequestObject();//stores XMLHttpRequestObject
  //sends HTTP request
  if (xmlHttp){
    try{
      //create string of parameters
      var params = "smonth=" + mm;
      xmlHttp.open("POST", "http://www.jakubcata.eu/calendar.php", true);
//      xmlHttp.open("POST", "http://localhost/jakubcata/calendar.php", true);
      xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
      xmlHttp.onreadystatechange = handleRequestStateChange2;
      xmlHttp.send(params);

    }
    catch (e){
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}

function handleRequestStateChange2(){
  //request state has changed
  if (xmlHttp.readyState==4){//process is completed
    if (xmlHttp.status==200){
    //http status is OK
      try {
        var xmlResponse = xmlHttp.responseXML;
        //process response
        var xmlRoot = xmlResponse.documentElement;
        var responseText = xmlRoot.firstChild.data;
        respDiv = document.getElementById("calendar");
        respDiv.innerHTML = "";
        respDiv.innerHTML += responseText;
      }
      catch (e){
        alert("Error reading the response: " + e.toString());
      }
    }
  }
}

function handleid(){
  //request state has changed
  if (xmlHttp.readyState==4){//process is completed
    if (xmlHttp.status==200){
    //http status is OK
      try {
        var xmlResponse = xmlHttp.responseXML;
        //process response
        var xmlRoot = xmlResponse.documentElement;
        var responseText = xmlRoot.firstChild.data;
        respDiv = document.getElementById("help");
        respDiv.innerHTML = "";
        respDiv.innerHTML += responseText;
      }
      catch (e){
        alert("Error reading the response: " + e.toString());
      }
    }
  }
}

/*function create(element)
{
  var parent = document.getElementById("d_main");
  var element2 = document.createElement("div");
  element2.setAttribute("id", "help");
  //element2.setAttribute("guest", element.getAttribute("id"));
  
  var id = element.getAttribute("id");
  //element2 = document.getElementById("help");//získá element podle jeho id
  element2.style.position = "absolute";//nastavi absolutni pozicovani elementu
  var left = computePlacement(element, "offsetLeft")+300;
  var top = computePlacement(element, "offsetTop");
  left += "px";
  top += "px";
  element2.style.left = left;
  element2.style.top = top;
  parent.appendChild(element2);
  var xmlHttp = createXmlHttpRequestObject();//stores XMLHttpRequestObject
  //sends HTTP request
  if (xmlHttp){
    try{
     //try to connect to server
      var id = element.id;
      var params = "id=" + id;
      //create string of parameters
      xmlHttp.open("POST", "data.php", true);
      xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
      xmlHttp.onreadystatechange = handleid;
      xmlHttp.send(params);

    }
    catch (e){
      alert("Can't connect to server:\n" + e.toString());
    }
  }
  
}*/

function destroy()
{
//         respDiv = document.getElementById("help");
//        respDiv.innerHTML = "";
        
  var parent = document.getElementById("d_main");
	parent.removeChild(document.getElementById("help"));
}

function hide()
{
  var div = document.getElementById("response_box");
  div.style.display = "none";
  
}
