var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
// holds the remote server address and parameters
//var serverAddress = "inc/searchFunctions.php";
 
// 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.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;
}

// function that displays a new message on the page
function display1($message)
{
  // obtain a reference to the <div> element on the page
  myDiv = document.getElementById("menuDisp");
  // myDiv = document.menuDisp;
  // display message
  myDiv.innerHTML = $message;
  // collapseMenu();
}

// function that displays an error message
function displayError1($message)
{
  // display error message, with more technical details if debugMode is true
  display1("An error has occured, please contact the administrator." + $message);
  // restart sequence 
  // setTimeout("process();", 1000);
}

// call server asynchronously
function addNew(string,desc)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
 
    // try to connect to the server
    try
    {
      // remove this line if you don't like the 'Receiving...' message
      //display("<img src='images/loading.gif'><br><b>Loading...</b>");
      // make asynchronous HTTP request to retrieve new message
      params = "string="+string+"&desc="+desc;
      //alert(params);
      xmlHttp.open("POST", "../inc/addCategory.php", true);
      xmlHttp.setRequestHeader("Content-Type", 
                                 "application/x-www-form-urlencoded");
      xmlHttp.onreadystatechange = handleUpdateProfile1;
      xmlHttp.send(params);
    }
    catch(e)
    {
      displayError1(e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleUpdateProfile1() 
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        getResp1();
      }
      catch(e)
      {
        // display error message
        displayError1(e.toString());
      }
    } 
    else
    {
      // display error message
      displayError1(xmlHttp.statusText);   
    }
  }
}

// handles the response received from the server
function getResp1()
{
  // retrieve the server's response 
  var response = xmlHttp.responseText;
    // server error?
  if (response.indexOf("ERRNO") >= 0 
      || response.indexOf("error") >= 0
      || response.length == 0)
    throw(response.length == 0 ? "Server error." : response);
  // display the message
document.getElementById('CategoryName').value="";
document.getElementById('CatDescription').value="";
process();  
  // restart sequence
 //  setTimeout("process();", 1000);
}

function process()
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
 
    // try to connect to the server
    try
    {
      // remove this line if you don't like the 'Receiving...' message
      //display("Updating your profile....")
      // make asynchronous HTTP request to retrieve new message
      params = "";
      xmlHttp.open("POST", "../inc/popCategory.php", true);
      xmlHttp.setRequestHeader("Content-Type", 
                                 "application/x-www-form-urlencoded");
      xmlHttp.onreadystatechange = handleUpdateProfile;
      xmlHttp.send(params);
    }
    catch(e)
    {
      displayError1(e.toString());
    }
  }
}

function processmenu(mid)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
 
    // try to connect to the server
    try
    {
      // remove this line if you don't like the 'Receiving...' message
     display1("<center><img src='../images/loading2.gif'></center>");
     //display1("Loading");
      // make asynchronous HTTP request to retrieve new message
      params = "mid="+mid;
      xmlHttp.open("POST", "./inc/getMenu.php", true);
      xmlHttp.setRequestHeader("Content-Type", 
                                 "application/x-www-form-urlencoded");
      xmlHttp.onreadystatechange = handleUpdateProfile;
      setTimeout("xmlHttp.send(params)", 100);
    }
    catch(e)
    {
      displayError1(e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleUpdateProfile() 
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        getResp();
      }
      catch(e)
      {
        // display error message
        displayError1(e.toString());
      }
    } 
    else
    {
      // display error message
      displayError1(xmlHttp.statusText);   
    }
  }
}

// handles the response received from the server
function getResp()
{
  // retrieve the server's response 
  var response = xmlHttp.responseText;
  //response=response.split("~");
  var Nstring = response.split("~");
  // server error?
  if (response.indexOf("ERRNO") >= 0 
      || response.indexOf("error") >= 0
      || response.length == 0)
    throw(response.length == 0 ? "Server error." : response);
  // display the message
display1(Nstring[0]);
collapseMenu();
document.getElementById("printlink").onclick=function(){window.open('http://www.rivergrillenepa.com/menupdf.php?m='+encode64(Nstring[1]),'Menu','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');};

  // restart sequence
 //  setTimeout("process();", 1000);
}

function delCategory(catid)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
 
    // try to connect to the server
    try
    {
      // remove this line if you don't like the 'Receiving...' message
      //display("Updating your profile....")
      // make asynchronous HTTP request to retrieve new message
      params = "catid="+catid;
      xmlHttp.open("POST", "../inc/delCategory.php", true);
      xmlHttp.setRequestHeader("Content-Type", 
                                 "application/x-www-form-urlencoded");
      xmlHttp.onreadystatechange = handleUpdateProfile2;
      xmlHttp.send(params);
    }
    catch(e)
    {
      displayError1(e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleUpdateProfile2() 
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        getResp2();
      }
      catch(e)
      {
        // display error message
        displayError1(e.toString());
      }
    } 
    else
    {
      // display error message
      displayError1(xmlHttp.statusText);   
    }
  }
}

// handles the response received from the server
function getResp2()
{
  // retrieve the server's response 
  var response = xmlHttp.responseText;
  //var Nstring = response.split("_");
  // server error?
  if (response.indexOf("ERRNO") >= 0 
      || response.indexOf("error") >= 0
      || response.length == 0)
    throw(response.length == 0 ? "Server error." : response);
  // display the message
  document.getElementById('CategoryName').value="";
  document.getElementById('CatDescription').value="";
process(); 
  
  // restart sequence
 //  setTimeout("process();", 1000);
}

function updateCategory(catid,catname,catdesc)
{
  // only continue if xmlHttp isn't void
 // alert(pid+" "+sid+" "+sname+" "+sdescription);
  if (xmlHttp)
  {
 
    // try to connect to the server
    try
    {
      // remove this line if you don't like the 'Receiving...' message
      //display("<img src='images/loading.gif'><br><b>Loading...</b>");
      // make asynchronous HTTP request to retrieve new message
      params = "catid="+catid+"&catname="+catname+"&catdesc="+catdesc;
      xmlHttp.open("POST", "../inc/updateCategory.php", true);
      xmlHttp.setRequestHeader("Content-Type", 
                                 "application/x-www-form-urlencoded");
      xmlHttp.onreadystatechange = handleUpdateProfile3;
      xmlHttp.send(params);
    }
    catch(e)
    {
      displayError1(e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleUpdateProfile3() 
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        getResp3();
      }
      catch(e)
      {
        // display error message
        displayError1(e.toString());
      }
    } 
    else
    {
      // display error message
      displayError1(xmlHttp.statusText);   
    }
  }
}

// handles the response received from the server
function getResp3()
{
  // retrieve the server's response 
  var response = xmlHttp.responseText;
    // server error?
  if (response.indexOf("ERRNO") >= 0 
      || response.indexOf("error") >= 0
      || response.length == 0)
    throw(response.length == 0 ? "Server error." : response);
  // display the message
document.getElementById('CategoryName').value="";
document.getElementById('CatDescription').value="";
document.getElementById('updateSet').style.display='none';  
document.getElementById('addSet').style.display='block'; 
//alert(Npid+"  "+Nsid);
//alert(response);
process();  
  // restart sequence
 //  setTimeout("process();", 1000);
}

function displayCT(catid)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
 
    // try to connect to the server
    try
    {
      // remove this line if you don't like the 'Receiving...' message
      //display("Updating your profile....")
      // make asynchronous HTTP request to retrieve new message
      params = "catid="+catid;
      xmlHttp.open("POST", "../inc/displayCT.php", true);
      xmlHttp.setRequestHeader("Content-Type", 
                                 "application/x-www-form-urlencoded");
      xmlHttp.onreadystatechange = handleUpdateProfile4;
      xmlHttp.send(params);
    }
    catch(e)
    {
      displayError1(e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleUpdateProfile4() 
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        getResp4();
      }
      catch(e)
      {
        // display error message
        displayError1(e.toString());
      }
    } 
    else
    {
      // display error message
      displayError1(xmlHttp.statusText);   
    }
  }
}

// handles the response received from the server
function getResp4()
{
  // retrieve the server's response 
  var response = xmlHttp.responseText;
  var Nstring2 = response.split("_");
  Ntname=Nstring2[0];
  Ntdesc=Nstring2[1];
  Nctid=Nstring2[2];
  // server error?
  if (response.indexOf("ERRNO") >= 0 
      || response.indexOf("error") >= 0
      || response.length == 0)
    throw(response.length == 0 ? "Server error." : response);
  // display the message
document.getElementById('updateSet').style.display='block';  
document.getElementById('addSet').style.display='none'; 
document.getElementById('CategoryName').value=Ntname;
document.getElementById('CatDescription').value=Ntdesc;
document.getElementById('catid').value=Nctid;
//alert(document.getElementById('ctid').value);
//  alert(response);
  // restart sequence
 //  setTimeout("process();", 1000);
}