//SOLVE BACK BUTTON BY ADD HISTORY DHTML
//BEGIN WITH <body onload="initialize();">
//FROM: http://codinginparadise.org/projects/dhtml_history/README.html
var hLocation;
var locnum;
function initialize() {
  // initialize our DHTML history
  dhtmlHistory.initialize();

  // if this is the first time we have loaded the page...
  if (dhtmlHistory.isFirstLoad()) {
    hLocation = "0";
    dhtmlHistory.add(hLocation, document.getElementById("d_main").innerHTML);
  }

  // add ourselves as a listener for history change events
  dhtmlHistory.addListener(handleHistoryChange);
}

/** A function that is called whenever the user presses the back or forward buttons. This
    function will be passed the newLocation, as well as any history data we associated
    with the location. */
function handleHistoryChange(newLocation, historyData) {
  // use the history data to update our UI
  updateUI(newLocation, historyData);                           
//  document.getElementById('testing').innerHTML = "location="+newLocation;
}

/** A simple method that updates our user interface using the new location. */
function updateUI(newLocation, historyData) {
  var output = document.getElementById("d_main");
  
  if (historyData == null) {
    message = "";
  } else {
    message = historyData;
  }
  output.innerHTML = message;
}

function replace(argvalue, x, y) {
//FROM: http://www.tneoh.zoneit.com/javascript/js_func.html
//Substitute a string X to a string Y in an argument.
//Usage: replace("abc123defgh", "123", "ABC");
  if ((x == y) || (parseInt(y.indexOf(x)) > -1)) {
    errmessage = "replace function error: ";
    errmessage += "Second argument and third argument could be the same ";
    errmessage += "or third argument contains second argument.";
    errmessage += "This will create an infinite loop as it's replaced globally.";
    alert(errmessage);
    return false;
  }
  while (argvalue.indexOf(x) != -1) {
    var leading = argvalue.substring(0, argvalue.indexOf(x));
    var trailing = argvalue.substring(argvalue.indexOf(x) + x.length,
    argvalue.length);
    argvalue = leading + y + trailing;
  }
  return argvalue;
}

function char2url(arg) {
  arg = replace(arg, '\\n', "|_newline_|");
  arg = replace(arg, ";", "|_scln_|");
  arg = replace(arg, "&", "|_amp_|");
  arg = replace(arg, "+", "|_plus_|");
  arg = replace(arg, "%", "|_percent_|");
  arg = replace(arg, "'", "|_apos_|");
  arg = replace(arg, '"', "|_quot_|");
  arg = replace(arg, "<", "|_lt_|");
  arg = replace(arg, ">", "|_gt_|");
  return arg;
}

function getXmlHttpRequestObject() {
  if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Not IE
  } else {
    if(window.ActiveXObject) {
      return new ActiveXObject("Microsoft.XMLHTTP"); //IE
    } else {
      alert("Your browser doesn't support the XmlHttpRequest object. Better upgrade to Firefox.");
    } 
  }
}

var req = getXmlHttpRequestObject();

function d_post(cmd,argv) {
  document.getElementById("loading").className = "";
  modargv = char2url(argv);
  setTimeout( 'xd_post("' + cmd + '","' + modargv + '");' , 60);
} 
function xd_post(cmd,argv) {
  //HISTORY
  arg1 = "cmd="+cmd+"/"+argv;
  req.open("POST", "?", false);
  req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  req.send(arg1);


  if (cmd=="info" || cmd=="source") {
    document.getElementById("d_main").innerHTML = req.responseText;
    document.getElementById("loading").className = "hidden";
    document.getElementById("search_form").focus();

  } else if (cmd=="c2e" || cmd=="e2c") {
    document.getElementById("d_main").innerHTML = req.responseText;
    document.getElementById("loading").className = "hidden";
    document.getElementById("search_form").focus();

  }
  setTimeout('addHistory()',60);
} 
function addHistory() {
  hLocation = dhtmlHistory.getCurrentLocation();
  hLocation = ""+(parseInt(hLocation)+1)
  dhtmlHistory.add(hLocation, document.getElementById("d_main").innerHTML);
}
