/*
Public Version 1.0 released 6/15/06

Functions Exposed: Fetch and Validate
Library: PrefsAjaxLib
CallBackFunction: AjaxCallBackFunction

Calling one of the two functions listed above will create an xmlHttpRequest object that calls an ASP page that returns a string which is converted into an object and passed back to the calling page by calling the AjaxCallBackFunction. See the notes attached to each function below for more detail. The object returned is a JSON formatted object, for more info go to http://www.json.org/

Sample AjaxCallBack function that can process a javascript object. This can be copied and pasted to your page and modified as needed.

function AjaxCallBack(obj){
//Gets: a JSON formatted object
//Returns: nothing
//Notes: This function can either handle the object itself, or better yet, hand the object off to a function that handles the object.
  switch(obj.AjaxAction){  //you can also switch on the AjaxCallBackRef
    case "guideCode":
      ProcessCode(obj);break;
  }
}
function ProcessCode(obj){
'Gets: an object
'Returns: nothing, but processes the object. Depending on the info in the object it either displays the values returned  or a warning message stating that they've entered an invalid value.
  if(obj.Valid=="false"){
    alert("The value you entered was invalid");
  }else{
    alert("Congratulations, please visit " + obj.URL + " for more information.";
  }
}
*/

var req;  //the name of the request object
var PrefsAjaxLib = new CreateAjaxLib();

function CreateAjaxLib(){
  this.url="/service/help/4u/ScriptLib/AjaxLib_v1.asp";  //the url is the url of the asp page which will process the request. at this point I see most of the functions being on one page and therefore just defining a default page, but will need to append whatever fields or values to return the url as querystring args.
  this.tempUrl = "";  //this property is internal to the AjaxLib, don't edit it.
  this.debugFlg=false;  //this can allow for some debugging, overwrite it on the local page if you want to access any of these objects built into this library.
  this.DBSource = "Live";  //the DbSource can be used by the ASP page to determine what server to conect to.
  this.depth = 0; //depth has two potential uses 1) it is the number of levels deep to search in a hierarchal recordset. 0 only returns the primary match, 1 gets one level lower, 2 gets levels lower, etc. 2) it can be used to determine how many levels of files/folders to get when looking for a folder, it can return child folders.
  this.runAsync = true;  //this is to define the 3rd argument in the req.open call. Being set to false causes pages to wait for a response, which may or may not ever come and causes the page to run synchronously. If running synchronously the user cannot continue to enter information until it receives a response.
}

function Validate(what,value,AjaxCallBackRef){
/*Gets: what(string):
          possible values are guidecode
        value(string or integer):
          this is value you are validating for, examples would be a passcode.
        AjaxCallBackRef(string or integer):
          this is an optional argument that is passed to the ASP page and back to the local(parent) page and is used to
          identify what piece of information is being returned. This allows pages to identify multiple instances of the
          same type of information. In other words, one page may have the need to validate to different passphrases.
          One would use an AjaxCallBackRef of pass1 and the other would use pass2, which also allows
          for the instance of the user entering pass1, followed immediately by pass2 and receiving pass before pass1.
Returns: nothing, but sets whatever vars it needs to from the PrefsAjaxLib, the tempUrl variable is the most common to edit. The function then calls the CreateProcessReqChange. The default case throws an alert and exits the function.
Examples:
    Validate ("guideCode","Code Str")
*/
  PrefsAjaxLib.tempUrl = PrefsAjaxLib.url + "?DBSource=" + PrefsAjaxLib.DBSource + "&AjaxAction=Validate";
  if(PrefsAjaxLib.debugFlg==true){PrefsAjaxLib.tempUrl += "&debugFlg=true";}
  switch(what.toLowerCase()){
    case "guidecode":
      PrefsAjaxLib.tempUrl += "&what=" + what + "&value=" + value;
      break;
    default:
      return alert("I can't validate for this type (" + what + ") of item.");
  }
  if(AjaxCallBackRef!=""){PrefsAjaxLib.tempUrl += "&AjaxCallBackRef=" + AjaxCallBackRef;}
  CreateProcessReqChange();
}

function Fetch(what,value,AjaxCallBackRef){
/*Gets: what(string):
          possible values are guideCode
        value(string or integer):
          this is value you are validating for, examples would be a passcode.
        AjaxCallBackRef(string or integer):
          this is an optional argument that is passed to the ASP page and back to the local(parent) page and is used to
          identify what piece of information is being returned. This allows pages to identify multiple instances of the
          same type of information. In other words, one page may have the need to validate to different passphrases.
          One would use an AjaxCallBackRef of pass1 and the other would use pass2, which also allows
          for the instance of the user entering pass1, followed immediately by pass2 and receiving pass before pass1.
Returns: nothing, but sets whatever vars it needs to from the PrefsAjaxLib, the tempUrl variable is the most common to edit. The function then calls the CreateProcessReqChange. The default case throws an alert and exits the function.
Examples:
    Validate ("guideCode","myCode")
*/
  PrefsAjaxLib.tempUrl = PrefsAjaxLib.url + "?DBSource=" + PrefsAjaxLib.DBSource + "&AjaxAction=Fetch";
  if(PrefsAjaxLib.debugFlg==true){PrefsAjaxLib.tempUrl += "&debugFlg=true";}
  switch(what.toLowerCase()){
    default:
      return alert("I can't validate for this type (" + what + ") of information.");
  }
  if(AjaxCallBackRef!=""){PrefsAjaxLib.tempUrl += "&AjaxCallBackRef=" + AjaxCallBackRef;}
  CreateProcessReqChange();
}

function CreateProcessReqChange(){
//Gets: nothing
//Returns: nothing but calls the processReqChange function that returns an object to the parent page..
//Notes:
  req = false;
  // branch for native XMLHttpRequest object, in other words Mozilla, Safari, etc.
  if(window.XMLHttpRequest){
    try{
      req = new XMLHttpRequest();
    }catch(e){
      req = false;
    }
  }else if(window.ActiveXObject){ //branch for IE/Windows ActiveX version.
    try{
      req = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
      try{
        req = new ActiveXObject("Microsoft.XMLHTTP");
      }catch(e){
        req = false;
      }
    }
  }
  if(req){
    if(PrefsAjaxLib.debugFlg){alert("url: " + PrefsAjaxLib.tempUrl);}
    req.open("GET", PrefsAjaxLib.tempUrl, PrefsAjaxLib.runAsync);  //3rd arg: true is asynchronous - false is synchoronous this is defined in the PrefsAjaxLib, see notes there for more detail.
    req.onreadystatechange = processReqChange;  //You have to assign the event handler after the open for IE not to cache the page. There is also a line of code at the top of AjaxLib_v1.asp that also address the IE caching issue.
    req.send(null);
  }
}

function processReqChange(){
  //ony if req shows "loaded"
  if(req.readyState==4){
    //only if "OK"
    if(req.status==200){
      if(PrefsAjaxLib.debugFlg){alert(req.responseText);}
      AjaxCallBack(eval('('+req.responseText+')'))
      //send the responseText back to the requesting page???
      //reset the tempUrl, this needs to be done for pages with multiple calls to the PrefsAjaxLib.
      PrefsAjaxLib.tempUrl = "";
    }else{
      alert("There was a problem retrieving the data:\n - " + req.statusText);
    }
  }
}
