////////////////////////////////////////////////////////////////////////
// Class Object - "ajaxianClass"
////////////////////////////////////////////////////////////////////////

// define
var ajaxianClass = Class.create();


ajaxianClass.prototype.statType = "";
ajaxianClass.prototype.className = "";
ajaxianClass.prototype.returns="";
ajaxianClass.prototype.theCode="";
ajaxianClass.prototype.returnsData=null;
ajaxianClass.prototype.responseText="";
ajaxianClass.prototype.responseXML="";
ajaxianClass.prototype.responseData="";
ajaxianClass.prototype.numInfoGroups=0;
ajaxianClass.prototype.onResponseData=null;
ajaxianClass.prototype.errorInfo="";

ajaxianClass.prototype = {

/////////////////////////////////////////////////////////
// Function: --> ajax object initialization
// 
// purpose: to allow interclass functionality by initializing the class varibles
// input: st --> arbitrary data to pass in (ie: a specific value of some html tag, or even "this" if an object is related)
// input: cn --> explicit parent class name of this very class.  passes in for interclass functionc calls.
//
// call: eg --> if ajaxClass = new ajaxianClass() then you would call like this:
//              ajaxClass.intialize(value,ajaxClass) 
////////////////////////////////////////////////////////
  // init, call upon creation
  initialize: function(st,cn,code){
      arbValue = st;
      // set default url
      this.url = "http://thesportsequation.myvnc.com/scripts/db_call.php";
      // clear
      this.sendReq = null;
      // clear
      this.logWindow = null;
      // clear
      this.handlerFunc = null;

      // check
      if(code){
        eval(code);
      }  
  },

/////////////////////////////////////////////////////////
// Function: --> GENERAL OBJECT HANDLER
// 
// purpose: to interact in all ways with a database, either insert, change or grabbing data
//
// input: Action --> the current interaction [SelectFromAndSort,Insert,Change,getrowCount,Delete]
// input: database --> the database to query
// input: table --> the table where the values are kept
// input: (ARRAY) theReturns --> an array with a list of returns we desire to grab or insert
// input: (ARRAY) theDestinations --> an array which tells the returns where to go
// input: theConditions --> a string of MySQL conditional statements
// input: Code --> explicit javascript to execute at the end of the ajax routine.
////////////////////////////////////////////////////////

  Grab: function(Action,database,table,theReturns,theConditions,theDestinations,Code){
    // clear
    errorInfo = "";

    // check
    if(this.sendReq){
      // free
      delete this.sendReq;
    }

    //these are local variables!!!
    this.sendReq = this.getXMLhttpRequest();

    //this is really just a group of values used in context of the ajax call, no need to assign it to an object via keyword "this".
    this.theConnection = new Array(database,table);
    this.theAction = Action;
    this.returns = theReturns;
    this.theCode = Code;

    // clear
    var passstring = "";

    ///////////set up the transforms to pass into dbcall//////////////
    var parse = "raw:";
      if (theConditions.search(parse) != -1){
        filter=theConditions;
        filter=filter.slice(parse.length,theConditions.length);
      }else if(theConditions!=""&&theConditions!=theDestinations){
        filter=" WHERE " + theConditions;
      }else{
        filter = " " + theConditions;
      }

/*
    passstring = "val=" + this.theConnection + ";" + this.theAction + ";" +
      theReturns + ";" + theDestinations + ";" + filter;
*/
    passstring = "val=" + this.theConnection + "~" + this.theAction + "~" +
      theReturns + "~" + theDestinations + "~" + filter;

//		passstring = encodeURIComponent(passstring);	
    // check
    if(this.logWindow){
      this.logWindow.postLog("AJAX REQ: sendReq.readyState = " + this.sendReq.readyState);
    }

    //same old structure as before except all of the 'this' keywords are not needed
    if(this.sendReq.readyState == 4 || this.sendReq.readyState == 0) {
      //PLEASE REFER TO THE ISSUE 21 IN MANTIS BEFORE CHANGING THIS TO POST//
//      this.sendReq.open("get",this.url+"?"+passstring,true);
      this.sendReq.open("post",this.url+"?"+passstring,true);

      ///////////////////////////////////////////////////////////////////////
      this.sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

      // set the owner of the send request to be the ajaxian class (IE support)
      this.sendReq.owner = this;
      this.sendReq.sendReq = this.sendReq;

      // check
      if(this.handlerFunc){
        // free
        delete this.handlerFunc;
      }

      // create
      this.handlerFunc = new Function("var theClass = this.owner;var sendReq = this.sendReq;theClass.processResponse(sendReq);");
      // set
      this.handlerFunc.owner = this;
      this.handlerFunc.sendReq = this.sendReq;
      // set
      this.sendReq.onreadystatechange = this.handlerFunc;

      // send
      this.sendReq.send(null);
/*****
      try{
        // send
        this.sendReq.send(null);
      }
      catch(e){
      }
*/

      // check
      if(this.logWindow){
        this.logWindow.postLog("AJAX REQ: " + passstring);
      }
    }
  },


/////////////////////////////////////////////////////////
// Function: --> ajax object creation
// 
// purpose: standard ajax object creation script.
////////////////////////////////////////////////////////
  getXMLhttpRequest: function(){
    if(window.XMLHttpRequest){
/*
      // check
      if(this.logWindow){
        this.logWindow.postLog("AJAX: creating XMLHttpRequest for browsers (Firefox,Safari,Etc.)");
      }
*/

      return new XMLHttpRequest();
    }
    else if(window.ActiveXObject) {
/*
      // check
      if(this.logWindow){
        this.logWindow.postLog("AJAX: creating XMLHttpRequest for browsers (IE)");
      }
*/
      return new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
/*
      // check
      if(this.logWindow){
        this.logWindow.postLog("AJAX: Error, browser doesn't support AJAX!");
      }
*/
      alert("Ajax not detected, consider enabling javascript to increase your accessability.")
    }
  },


  processResponse: function(sendReq){
    // set
    var theClass = this;
    
    // check
    if(theClass.logWindow){
      theClass.logWindow.postLog("AJAX RESPONSE: HANDLER!");
    }

    // check
    if(theClass.logWindow){
      theClass.logWindow.postLog("AJAX RESPONSE: sendReq.readyState = " + sendReq.readyState);
    }

    //populate the structure
    if(sendReq.readyState==4){
      // check
      if(theClass.logWindow){
        theClass.logWindow.postLog("AJAX RESPONSE: " + sendReq.responseText);
      }

      //now create a new data structure
      var grabObj = new Object();
      // set
      grabObj.responseText = sendReq.responseText;
      grabObj.responseXML = sendReq.responseXML;
      // check
      if(theClass.theAction=='SelectFromAndSort' ||
        theClass.theAction=='GetRowCount'){
        grabObj.responseXML = sendReq.responseXML;
        // check
        if(grabObj.responseText.length > 0){
          grabObj.responseData = grabObj.responseText.split("|");
        }

        grabObj.returnsData = theClass.returns.split(",");
      }else{
  //        alert(sendReq.responseText);
      }

      //evaluate any JS included in the call to grab
      eval(theClass.theCode);
    }
  },  

  Ajax_Handler: function (){
    // set
    var theClass = this.owner;
    var sendReq = this.sendReq;

    alert(this.ownerName);

    // process all the response data
    theClass.processResponse(sendReq);
  },

  checkError: function(grabObj){
    // check
    if(grabObj){
      // check DB errors
      if(grabObj.responseText.responseText == "No database selected"){
        errorInfo = "No Database Selected or DB Not AVailable"
      }
      else if(grabObj.responseText.search() > 0){
      }
    }  
  },

  ///THIS FUNCTION HERE IS FOR HANDLING STRAIGHT CALLS TO THE CGI WITHOUT DATABASE ACTION
  Request: function(theUrl){
    ///HAVE NOT FIGURED OUT HOW TO FIX THIS///
    this.theAction = "";
    this.theCode="clearTimeout(window.t);if($('theLine').value==''){alert('You must enter in a betting line for the AI to make a prediction')}else{LBInit()};";
    //////////////////////////////////////////
    this.sendReq = this.getXMLhttpRequest();
    if(this.sendReq.readyState == 4 || this.sendReq.readyState == 0) {
      //PLEASE REFER TO THE ISSUE 21 IN MANTIS BEFORE CHANGING THIS TO POST//
      this.sendReq.open("get",theUrl,true);
      ///////////////////////////////////////////////////////////////////////
      this.sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

      // check
      if(this.handlerFunc){
        // free
        delete this.handlerFunc;
      }

      // for cross browser compatibility fo rthe
      // set the owner of the send request to be the ajaxian class (IE support)
      this.sendReq.owner = this;
      this.sendReq.sendReq = this.sendReq;
      // create
      this.handlerFunc = new Function("if(this.sendReq.readyState==4){if(this.owner.onScrapeComplete){this.owner.onScrapeComplete(this.sendReq.responseText);}}");

      // set
      this.handlerFunc.owner = this;
      this.handlerFunc.sendReq = this.sendReq;
      // set
      this.sendReq.onreadystatechange = this.handlerFunc;
      // send
      this.sendReq.send(null);
/*****
      // send
      try{
        // send
        this.sendReq.send(null);
      }
      catch(e){
      }
*/      
    }
  }
}


