﻿// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();
var ajaxUrl = '/ajax.php';
var elemId  = 0;

var offsetX = 0;
var offsetY = 0;

/* creates an XMLHttpRequest instance */
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.7.0",
                                    "MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// make asynchronous HTTP request using the XMLHttpRequest object 
function displayPageEditForm(id)
{
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // call the server page to execute the server-side operation
    xmlHttp.open("GET", ajaxUrl + "?mode=page_form&id=" + id, true);  
    xmlHttp.onreadystatechange = handleDisplayPageEditForm;
    xmlHttp.send(null);
  }
  else
  {
    // if the connection is busy, try again after one second  
    setTimeout('displayPageEditForm(' + id + ')', 1000);
  }
}

// executed automatically when a message is received from the server
function handleDisplayPageEditForm() 
{
  //alert(xmlHttp.readyState + ' ' + xmlHttp.status + ' ' + xmlHttp.responseText);
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      document.getElementById("page_head").innerHTML = xmlHttp.responseText;
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("Nastal problém při spojení se serverem: " + xmlHttp.statusText);
    }
  }
}

function processPageEditForm(id)
{
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    id_parent  = document.getElementById("id_parent").value;
    name       = document.getElementById("page_name").value;
    page_title = document.getElementById("page_title").value;
    meta_keyw  = document.getElementById("meta_keyw").value;
    meta_desc  = document.getElementById("meta_desc").value;
    
    params = "mode=page_update&id=" + id + "&id_parent=" + id_parent + "&name=" + name + "&page_title=" + page_title + "&meta_keyw=" + meta_keyw + "&meta_desc=" + meta_desc;
    
    // call the server page to execute the server-side operation
    xmlHttp.open("POST", ajaxUrl, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange = handleProcessPageEditForm;
    xmlHttp.send(params);
  }
  else
  {
    // if the connection is busy, try again after one second  
    setTimeout('processPageEditForm(' + id + ')', 1000);
  }
}

// executed automatically when a message is received from the server
function handleProcessPageEditForm() 
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      document.getElementById("page_head").innerHTML = xmlHttp.responseText;
      displayStatus("page_status", 'Údaje byly úspěšně upraveny');
      setTimeout('hideElem("page_status")', 2000);
      displayPageMenu();
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("Nastal problém při spojení se serverem: " + xmlHttp.statusText);
    }
  }
}

function processPageDelete(id)
{
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    params = "mode=page_delete&id=" + id;
    
    // call the server page to execute the server-side operation
    xmlHttp.open("POST", ajaxUrl, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange = handleProcessPageDelete;
    xmlHttp.send(params);
  }
  else
  {
    // if the connection is busy, try again after one second  
    setTimeout('processPageDelete(' + id + ')', 1000);
  }
}

// executed automatically when a message is received from the server
function handleProcessPageDelete() 
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      document.getElementById("page_head").innerHTML = '';
      document.getElementById("page_cont").innerHTML = '';
      displayStatus("page_status", xmlHttp.responseText);
      displayPageMenu();
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("Nastal problém při spojení se serverem: " + xmlHttp.statusText);
    }
  }
}

// make asynchronous HTTP request using the XMLHttpRequest object 
function displayPageMenu()
{
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // call the server page to execute the server-side operation
    xmlHttp.open("GET", ajaxUrl + "?mode=page_menu", true);  
    xmlHttp.onreadystatechange = handleDisplayPageMenu;
    xmlHttp.send(null);
  }
  else
  {
    // if the connection is busy, try again after one second  
    setTimeout('displayPageMenu()', 1000);
  }
}

// executed automatically when a message is received from the server
function handleDisplayPageMenu() 
{
  //alert(xmlHttp.readyState + ' ' + xmlHttp.status + ' ' + xmlHttp.responseText);
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      document.getElementById("menu").innerHTML = xmlHttp.responseText;
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("Nastal problém při spojení se serverem: " + xmlHttp.statusText);
    }
  }
}

// make asynchronous HTTP request using the XMLHttpRequest object 
function displayParEditForm(id, id_page)
{
  elemId = id;
  
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // call the server page to execute the server-side operation
    xmlHttp.open("GET", ajaxUrl + "?mode=par_form&id=" + id + "&id_page=" + id_page, true);  
    xmlHttp.onreadystatechange = handleDisplayParEditForm;
    xmlHttp.send(null);
  }
  else
  {
    // if the connection is busy, try again after one second  
    setTimeout('displayParEditForm(' + id + ', ' + id_page + ')', 1000);
  }
}

// executed automatically when a message is received from the server
function handleDisplayParEditForm() 
{
  //alert(xmlHttp.readyState + ' ' + xmlHttp.status + ' ' + xmlHttp.responseText);
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      document.getElementById("par_" + elemId).innerHTML = xmlHttp.responseText;
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("Nastal problém při spojení se serverem: " + xmlHttp.statusText);
    }
  }
}

function processParEditForm(id)
{
  elemId = id;
  
  image_file = document.getElementById("image_file_" + id).value;
  
  if (image_file)
  {
    reg = new RegExp("\.jpe?g$", "i");
    reg2 = new RegExp("\.gif$", "i");
    
    if (image_file.match(reg) || image_file.match(reg2))
    {
      document.getElementById("par_form_" + id).submit();
      return;
    }
    else
    {
      alert("Nahrávání obrázků je možné pouze ve formátu JPG i GIF");
    }
  }
  
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    id_page  = document.getElementById("id_page_" + id).value;
    par_text = document.getElementById("par_text_" + id).value;
    
    params = "mode=par_update&id=" + id + "&id_page=" + id_page + "&par_text_" + id + "=" + par_text;
    
    // call the server page to execute the server-side operation
    xmlHttp.open("POST", ajaxUrl, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange = handleProcessParEditForm;
    xmlHttp.send(params);
  }
  else
  {
    // if the connection is busy, try again after one second  
    setTimeout('processParEditForm(' + id + ')', 1000);
  }
}

// executed automatically when a message is received from the server
function handleProcessParEditForm() 
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      if (elemId)
      {
        document.getElementById("par_" + elemId).innerHTML = xmlHttp.responseText;
        displayStatus("par_status_" + elemId, 'Údaje byly úspěšně upraveny');
        setTimeout('hideElem("par_status_" + elemId)', 2000);
      }
      else
      {
        document.getElementById("page_pars").innerHTML = xmlHttp.responseText;
        //displayStatus("par_status_0", 'Údaje byly úspěšně upraveny');
        //setTimeout('hideElem("par_status_0")', 2000);
      }
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("Nastal problém při spojení se serverem: " + xmlHttp.statusText);
    }
  }
}

function processParDelete(id)
{
  elemId = id;
  
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    params = "mode=par_delete&id=" + id;
    
    // call the server page to execute the server-side operation
    xmlHttp.open("POST", ajaxUrl, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange = handleProcessParDelete;
    xmlHttp.send(params);
  }
  else
  {
    // if the connection is busy, try again after one second  
    setTimeout('processParDelete(' + id + ')', 1000);
  }
}

// executed automatically when a message is received from the server
function handleProcessParDelete() 
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      hideElem("par_" + elemId, '');
      displayStatus("par_status_" + elemId, xmlHttp.responseText);
      setTimeout('hideElem("par_status_" + elemId)', 2000);
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("Nastal problém při spojení se serverem: " + xmlHttp.statusText);
    }
  }
}

function processParImgDelete(id)
{
  elemId = id;
  
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    params = "mode=par_img_delete&id=" + id;
    
    // call the server page to execute the server-side operation
    xmlHttp.open("POST", ajaxUrl, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange = handleProcessParImgDelete;
    xmlHttp.send(params);
  }
  else
  {
    // if the connection is busy, try again after one second  
    setTimeout('processParImgDelete(' + id + ')', 1000);
  }
}

// executed automatically when a message is received from the server
function handleProcessParImgDelete() 
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      document.getElementById("par_form_" + elemId).innerHTML = xmlHttp.responseText;
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("Nastal problém při spojení se serverem: " + xmlHttp.statusText);
    }
  }
}


// make asynchronous HTTP request using the XMLHttpRequest object 
function displayPhotoAddForm(id_page)
{
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // call the server page to execute the server-side operation
    xmlHttp.open("GET", ajaxUrl + "?mode=photo_form&id_page=" + id_page, true);  
    xmlHttp.onreadystatechange = handleDisplayPhotoAddForm;
    xmlHttp.send(null);
  }
  else
  {
    // if the connection is busy, try again after one second  
    setTimeout('displayParEditForm(' + id_page + ')', 1000);
  }
}

// executed automatically when a message is received from the server
function handleDisplayPhotoAddForm() 
{
  //alert(xmlHttp.readyState + ' ' + xmlHttp.status + ' ' + xmlHttp.responseText);
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      document.getElementById("photo").innerHTML = xmlHttp.responseText;
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("Nastal problém při spojení se serverem: " + xmlHttp.statusText);
    }
  }
}

// make asynchronous HTTP request using the XMLHttpRequest object 
function displayPhoto(link, elem)
{
  offsetX = findPos(elem, 'left');
  offsetY = findPos(elem, 'top');
  
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // call the server page to execute the server-side operation
    xmlHttp.open("GET", ajaxUrl + "?mode=photo_display&link=" + link, true);  
    xmlHttp.onreadystatechange = handleDisplayPhoto;
    xmlHttp.send(null);
  }
  else
  {
    // if the connection is busy, try again after one second  
    setTimeout('displayPhoto(' + id + ')', 1000);
  }
}

// executed automatically when a message is received from the server
function handleDisplayPhoto() 
{
  //alert(xmlHttp.readyState + ' ' + xmlHttp.status + ' ' + xmlHttp.responseText);
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      document.getElementById("image_browser").innerHTML = xmlHttp.responseText;
      if (xmlHttp.responseText != '') 
      {
        document.getElementById("image_browser").style.top     = (offsetY - 260)+'px';
        document.getElementById("image_browser").style.left    = (offsetX - 350)+'px';
        document.getElementById("image_browser").style.display = 'block';
      }
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("Nastal problém při spojení se serverem: " + xmlHttp.statusText);
    }
  }
}

function displayStatus(elem, text)
{
  document.getElementById(elem).innerHTML = text;
  document.getElementById(elem).style.display = 'block';
}

function hideElem(elem)
{
  document.getElementById(elem).innerHTML = '';
  document.getElementById(elem).style.display = 'none';
}
