/*
Calls function from DataValidationRoutines.js
*/

function GetXMLHttpRequestObject(){
// try W3C XMLHttpRequest object constructor
if(window.XMLHttpRequest){req = new XMLHttpRequest();}
if(req){if(typeof req == "object"){return req;}}

// try  newest ActiveXObject constructor
if(window.ActiveXObject){
req = new ActiveXObject("Msxml3.XMLHTTP");
if(req){if(typeof req == "object"){return req;}}
req = new ActiveXObject("Msxml2.XMLHTTP");
if(req){if(typeof req == "object"){return req;}}
req = new ActiveXObject("Microsoft.XMLHTTP");
if(req){if(typeof req == "object"){return req;}}}

// there's nothing left to try
else {return null;}
} // end fcn GetXMLHttpRequestObject

//sending & retrieving data using the XMLHttpRequest object
/*
xreq.open("POST", "<?php echo $_SERVER['PHP_SELF']; ?>");
xreq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xreq.onreadystatechange=function(){FunctionToHandleHttpReturn(xreq, other, variables, to, send);}
xreq.send(poststr);

Note: to resolve function arguments at the time the Function is called
(as opposed to when the JS is first loaded and read), use this construction:
= new Function("FunctionToHandleHttpReturn(" + t + ");");
The new Function construction is much like the eval statement

FunctionToHandleHttpReturn(xreq, other, variables, to, send){
if(xreq.readyState != 4){return;} // page is not ready
if(xreq.status != 200){
things to do to show page could not load;
return;}
}
*/


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; 
  var L5 = d.substr(0, 5);
  if(d=="Error"){alert("There was an error saving the changed value:\n\n" + L5 + "\n\nRefresh your browser, then try again.");}}}
  
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


function GetTotalAmountDue(rtp, sda, q, n){
// get total regular ticket price
rtp = FixFunkyFloat(rtp, false);
var regtot = parseFloat(q * parseFloat(rtp));
regtot = FixFunkyFloat(regtot, false);

// resolve single discount amount
sda = parseFloat(sda);
if(isNaN(sda)){sda = 0;}
else if(!sda){sda = 0;}
else if(sda > 0){sda = sda * rtp;}
else {sda = Math.abs(sda);}

// correct sda
if(isNaN(sda)){sda = 0;}
else if(sda < 0){sda = 0;}

// calculate total discount amout
var tdc = n * sda;
tdc = parseFloat(tdc);
if(isNaN(tdc)){tdc = 0;}

// calculate total amount due
var tad = regtot - tdc;
tad = parseFloat(tad);

// return tad
if(isNaN(tad)){return 0;}
else if(!tad){return 0;}
else if(tad < 0.01){return 0;}
return tad;
} // end fcn GetTotalAmountDue


function GetOptionFromValue(s, v){
// reference dropdown
var dd = ReturnObjRef(s);
if(!dd){return null;}
v = JStrim(v);
var t = null;
var p = null;

// get options
var onl = dd.options;
var L = onl.length;

// scan options for value
for(var i = 0; i < L; i++){
p = onl[i];
if(!p){continue;}

// get option value
t = p.value;
if(!t){continue;}
t = JStrim(t);

// compare option values
if(t == v){return p;}}

// search failed
return null;
} // end fcn GetOptionFromValue


function DisableFormElements(F, disableTF){
// if reservation is superceded, nothing should be edited
if(F){if(F.elements){
var Feles = F.elements;
var L = Feles.length;
for(var i = 0; i < L; i++){
g = Feles[i];
if("disabled" in g){g.disabled = disableTF;}}}}
} // end fcn DisableAllChildren


function FixFunkyFloat(f, fint){
if(!f){return 0;}
if(fint){f = parseInt(f);}
else {f = parseFloat(f);}
if(isNaN(f)){return 0;}
else if(!f){return 0;}
else if(f < 0){return 0;}
return f;
} // end fcn FixFunkyFloat


function GetDisplayPrice(p, CutTF, CurrSym){
// get float for the sent string
p = p + "";
p = JStrim(p);

// strip all numbers and see if there is anything left
// if there is, there is text and no further processing is needed
p = p.replace(/^\+/, "");
var s = p.replace(/[0-9.$\s]/g, "");
if(s){return p;}

// at this point, the string is nothing but numbers so round to 2 decimal places
p = parseFloat(p);
p = p * 100;
p = parseInt(p);
p = p/100;
s = p.toString();

// cut off any 00 cents
if(CutTF){s = s.replace(/\.00+$/, "" );}

// add currency symbol
if(CurrSym){s = CurrSym + s;}
return s;
} // end fcn GetDisplayablePrice

