/* To make this work your html file needs 3 things:
 * 1: It must include this file.  Put the line below
 * just before </head>
<script language="JavaScript" type="text/javascript" src="login.js"></script>
 * 2: It must call this script to get the bit of information 
 * needed from the server.  Since I want the page to be initialized with 
 * the information use this <body> tag:
<body onLoad="getCurrentLogin();">
 * 3: You must put the new improved information somewhere. This script
 * replaces the tag current-login in the DOM.  SO use this where you want
 * the data:
<div id="current-login"> abc123 </div>
 */
   var request = null;

   function createRequest() {
     try {
       request = new XMLHttpRequest();
     } catch (trymicrosoft) {
       try {
         request = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (othermicrosoft) {
         try {
           request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (failed) {
           request = null;
         }
       }
     }

     if (request == null)
       alert("Error creating request object!");
   }
   function getBoardsSold() {
     createRequest();
     var url = "http://www.reviewsense.com/cgi-sys/cgiwrap/scarrico/reviewsense/jscurrentlogin.cgi";
     request.open("GET", url, true);
     request.onreadystatechange = updatePage;
     request.send(null);
  }
/* This is where we do the call to figure out the current login */
/* For other ajax scripts make the call here */
   function getCurrentLogin() {
     createRequest();
     var url = "http://www.reviewsense.com/cgi-sys/cgiwrap/scarrico/reviewsense/jscurrentlogin.cgi";
     request.open("GET", url, true);
     request.onreadystatechange = updatePage;
     request.send(null);
  }

/* This is where we modify the id in the DOM (current-login) */
/* For other ajax scripts make the bit that changes the page here */
  function updatePage() {
    if (request.readyState == 4) {
      var newTotal = request.responseText;
      var mySplitResult = newTotal.split(":");
      var currentLoginEl = document.getElementById("current-login");
      var currentLoginStrEl = document.getElementById("current-loginstr");
      replaceText(currentLoginStrEl, mySplitResult[0]);
      replaceText(currentLoginEl, mySplitResult[1]);
    }
  }

/* Here's a more complicated example that updates a bunch of 
 * stuff on an HTML page
 */
  function OldupdatePage() {
    if (request.readyState == 4) {
      var newTotal = request.responseText;
      var boardsSoldEl = document.getElementById("boards-sold");
      var cashEl = document.getElementById("cash");
      replaceText(boardsSoldEl, newTotal);
    
      /* Figure out how much cash Katie has made */
      var priceEl = document.getElementById("price");
      var price = getText(priceEl);
      var costEl = document.getElementById("cost");
      var cost = getText(costEl);
      var cashPerBoard = price - cost;
      var cash = cashPerBoard * newTotal;

      /* Update the cash for the slopes on the form */
      cash = Math.round(cash * 100) / 100;
      replaceText(cashEl, cash);
    }
  }

function replaceText(el, text) {
  if (el != null) {
    clearText(el);
    var newNode = document.createTextNode(text);
    el.appendChild(newNode);
  }
}

function clearText(el) {
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        el.removeChild(childNode);
      }
    }
  }
}

function getText(el) {
  var text = "";
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        if (childNode.nodeValue != null) {
          text = text + childNode.nodeValue;
        }
      }
    }
  }
  return text;
}

