function GetXMLHttpRequestObject(){
if(window.XMLHttpRequest){
try {req = new XMLHttpRequest();}
catch(e) {req=null;}}
else if(window.ActiveXObject){
try{req = new ActiveXObject("Msxml2.XMLHTTP");}
catch(e){
  try {req=new ActiveXObject("Microsoft.XMLHTTP");}
  catch(e){req=null;}}}
return req;}


function EditText(gob, mc, fldmsg){
//get new value from user; exit if null
var rv = LocalString(gob, fldmsg);
if(rv == null){return;}
rv = rv.substr(0, mc);

/*
//escape special characters
rv = rv.replace(/'/, "\\\'");
rv = rv.replace(/"/, "\\\"");
*/

//get the row id idno[0]=editcode; [1]=VolID
var id = gob.id
var idno = id.split("x");

//update the database (edit, new, match)
PostCell(idno[0], rv, idno[1]);
}//end EditText fcn


function EditCheck(gob, fldmsg){
//get the row id idno[0]=editcode; [1]=VolID
var idno = new Array();
var id = gob.id
var i = id.indexOf("x");
idno[0] = id.substr(0, i);
idno[1] = id.substr(i+1);

//get current text node or make & append one
gob.normalize();
var fc = gob.firstChild;
if(!fc){
  fc = document.createTextNode("");
  fc = gob.appendChild(fc);}
  
//get node value
tdtext = fc.nodeValue;

//reverse DOM node value
if(!tdtext){fc.nodeValue = fldmsg;}
else if(tdtext == ""){fc.nodeValue = fldmsg;}
else {fc.nodeValue = "";}

//send new value to db
if(!fc.nodeValue){rv="null";}
else if(fc.nodeValue == ""){rv="null";}
else if(fc.nodeValue == " "){rv="null";}
else {rv = 1;}
PostCell(idno[0], rv, idno[1]);
}//end EditCheck fcn


function LocalString(gob, fldmsg){
var it;
var rv;

//fade background to indicate cell editing
var origbc = gob.style.backgroundColor;
if(!origbc){origbc = "transparent";}
gob.style.backgroundColor = "#ffd080";

//get the text of the td ele
gob.normalize();
if(gob.childNodes.length > 0){
  it = gob.firstChild.nodeValue;}
else {
  it = "";}
rv = prompt("Enter a new value for " + fldmsg + ".\nThe current value is " + it + ".", it);
gob.style.backgroundColor = origbc;

//if entry is null return null
if(rv == null){return null;}

//otherwise, change element and provide a return value
if(gob.childNodes.length>0){
  gob.firstChild.nodeValue = rv;}
else {
  gob.appendChild(document.createTextNode(rv))}
return rv;
}//end fcn LocalString


function LocalSeats(gob, fldmsg){
//fade background to indicate cell editing
var origbc = gob.style.backgroundColor;
if(!origbc){origbc = "transparent";}
gob.style.backgroundColor = "#ffd080";

//get the text of the td ele
gob.normalize();
if(gob.childNodes.length > 0){
  it = gob.firstChild.nodeValue;}
else {
  it = "";}
rv = prompt("Enter a new value for " + fldmsg + ".\nThe current value is " + it + ".", it);
gob.style.backgroundColor = origbc;

//if entry is null or not valid integer, return null
if(rv == null){return null;}
else if(rv == ""){rv = "0";}
else if(isNaN(parseInt(rv))){
  alert("The valued entered is not a valid number.");
  return null;}

//otherwise, change element and provide a return value
rv = parseInt(rv);
if(gob.childNodes.length>0){
  gob.firstChild.nodeValue = rv;}
else {
  gob.appendChild(document.createTextNode(rv))}
return rv;
}//end fcn LocalSeats



function RecalculateTotal(gob){
var ttl = 0;
var ce;

//get references to objects
var ci = gob.cellIndex;
var ro = document.getElementById("resvtbl").rows;
var sp = document.getElementById("seatcount");
var res = document.getElementById("res");

//scan no. seats in each row
for (var i=1; i<ro.length; i++){
ce = ro[i].cells[ci];

//get reference to check box in that row
var id = ce.id;
var idno = parseInt(id.substr(1));
var ic = document.getElementById("i" + idno);

//sum seats if reservation is active
if(ic.checked == false){
if(ce.childNodes.length > 0){
  ce.normalize();
  ttl += parseInt(ce.firstChild.nodeValue);
}}}  //end if} //end if //end for

//update the count display
sp.firstChild.nodeValue = ttl;
res.firstChild.nodeValue = ttl;
return ttl;
} //end recalculatetotal fcn


function RemoteCell(a, b, c){
var req = GetXMLHttpRequestObject();
req.onreadystatechange = function(){
  if(req.readyState==4 && req.status==200){
  var d = req.responseText;  
  if(d=="Error"){alert("Error. There was an error saving the changed value to the database.  Refresh your browser, then try again.");}
  }//end if
}//end anon inner fcn
req.open("GET", "Eukaryote.php?a=" + encodeURIComponent(a) + "&b=" + encodeURIComponent(b) + "&c=" + encodeURIComponent(c));
req.send(null);
} //end fcn RemoteCell


function PostCell(a, b, c){
//edit code, new value, match value
//create data xfer objects
var req = GetXMLHttpRequestObject();
if(!req){
alert("There was an error saving the changed value to the database.  Refresh your browser, then try again.");
return;}

//customize req
reqvarstr = "a=" + encodeURIComponent(a) + "&b=" + encodeURIComponent(b) + "&c=" + encodeURIComponent(c);
req.open("POST", "Protozoa.php");
req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
req.onreadystatechange = function(){ProcessPost(req);}
req.send(reqvarstr);
} //end fcn PostCell


function ProcessPost(xreq){
if(xreq.readyState != 4 || xreq.status != 200){return;}
var d = req.responseText;
//alert("Response text: " + d);
if(d.substr(0, 5) == "Error"){alert("Error. There was an error saving the changed value to the database.  Refresh your browser, then try again.");}
} //end fcn ProcessPost

