/*
 AJAX.js
 This is AJAX/JSON scripting for forming XML:HTTP requests to a server
 for data requests. This script assumes that your serverside script returns
 data encapsulated in XML formed as:
     &lt;?xml version="1.0">
     &lt;data>
        &lt;blah>
            Your data in JSON notation
        &lt;/blah>
     &lt;/data>
 It then builds a javascript object based upon the structure of the JSON data
 and stores it in a variable for retrieval.
 By: Justin Pearce 1/18/2008
*/
var xmlobj=null; // XMLHttpRequest object
var data=null; // XML goes here
var check=false; // Boolean boolean
var dataOBJ=null; // JavaScript object
var thisTimeOut; // Timeout handle
var infoDiv;
var currentFocus;

/* Function: sendRequest()
 * This function builds an XMLRPC request for the browser and submits it to the
 * server for processing.
 * @Args: URL doc
 * @Returns: None
 */
function sendRequest(doc){ // send http request
    if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4) // check for existing requests
    {xmlobj.abort();} //<- Abort request if one is in the works
    try{xmlobj=new XMLHttpRequest();} // instantiate object for Mozilla, Netscape, etc...
    catch(e){
        try{xmlobj=new ActiveXObject('Msxml2.XMLHTTP');} // instantiate object for Internet Explorer...
        catch(e){//Obviously, your browser is something very different...
                 xmlobj=null; // Ajax is not supported by the browser
                 alert("Your browser lacks AJAX support. Please try Firefox (http://www.mozilla.com) or Internet Explorer 6+. Some newer versions of Safari also work.");
                 return false;}
            }
    /* The following three lines are used if getting data asynchronously */
    //xmlobj.onreadystatechange=stateChecker; // assign state handler
    //xmlobj.open('GET', doc, true); // open socket connection
    //return true;
    /* The following lines are used for getting data synchronously*/
    xmlobj.open('GET', doc, false); // open socket connection
    xmlobj.send(null); // send GET request
    if(xmlobj.status==200){
        data=xmlobj.responseXML; // read XML data
        getData(); // make XML data into JSON
        return true; //We did our thing
    }else{
        return false;
    }
}

/* Function: sendPostRequest()
 * This function builds an XMLRPC request for the browser and submits it to the
 * server for processing.
 * @Args: URL doc
 *        postData formatted data string to be sent as a POST request.
 * @Returns: None
 */
function sendPostRequest(doc, postData){ // send http request
    if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4) // check for existing requests
    {xmlobj.abort();} //<- Abort request if one is in the works
    try{xmlobj=new XMLHttpRequest();} // instantiate object for Mozilla, Netscape, etc...
    catch(e){
        try{xmlobj=new ActiveXObject('Msxml2.XMLHTTP');} // instantiate object for Internet Explorer...
        catch(e){//Obviously, your browser is something very different...
                 xmlobj=null; // Ajax is not supported by the browser
                 alert("Your browser lacks AJAX support. Please try Firefox (http://www.mozilla.com) or Internet Explorer 6+. Some newer versions of Safari also work.");
                 return false;}
            }
    /* The following three lines are used if getting data asynchronously */
    //xmlobj.onreadystatechange=stateChecker; // assign state handler
    //xmlobj.open('GET', doc, true); // open socket connection
    //return true;
    /* The following lines are used for getting data synchronously*/
    xmlobj.open('POST', doc, false); // open socket connection
    xmlobj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //POST data does not send without this line
    xmlobj.send(postData); // send POST request
    if(xmlobj.status==200){
        data=xmlobj.responseXML; // read XML data
        getData(); // make XML data into JSON
        return true; //We did our thing
    }else{
        return false;
    }
}

/* Function stateChecker()
 * This function does things based on the state machine of the xmlobj object.
 * It primarily lets us know when we can fish around in the object for data,
 * when to wait a bit more for data, and when to go looking elsewhere. The
 * xmlobj calls this function on some interval to find out what to do at a
 * certain state. Used in asynchronous calls.
 * @Args: None
 * @Returns: None
 */
function stateChecker(){ // check request status
    if(xmlobj.readyState==4){ // if request is completed
        if(xmlobj.status==200){ // if status == 200 display text file
            data=xmlobj.responseXML; // read XML data
            getData(); // make XML data into JSON
        }
        else{
            //Something didn't work.
            alert('Failed to get response :'+ xmlobj.statusText);
        }
    }else
    if(xmlobj.readyState==2 || xmlobj.readyState==3)
    {
      //If we are in the process of getting data
    }
    else
    if(xmlobj.readyState==1)
    {
      //If we start grabbing data
    }
}

/* Function getData()
 * This function takes the XML data returned by the xmlobj and extracts the
 * JSON data, parsing it into a Javascript object and saving it to a global
 * variable.
 * @Args: None
 * @Returns: boolean successful
 */
function getData(){
      if(data){
           if(window.ActiveXObject){
              xmlDoc=data.getElementsByTagName('data');
              dataOBJ = JSON.parse(xmlDoc[0].childNodes[0].text);
           }else{
              xmlData=data.getElementsByTagName('data');
              dataOBJ = JSON.parse(xmlData[0].firstChild.firstChild.data);
           }
           check=true;
        }else{
           check=false;
        }
}


function addHit(video) {
    var success = sendRequest('xmlrpc.php?vid='+video); // load XML file
    if(dataOBJ!==undefined){
      if(dataOBJ.type=='viewLogged'){
        //the video has been looged as viewed
      }
    }
}
