//*******************************************//
//                                           //
//  Copyright http://www.chris-davies.com    //
//    all code here developed by             //
//         Chris Davies                      //
//                                           //
//*******************************************//

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp=false;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
    {
      try
      {
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch (e) {
              alert("Error reading the response: "+ e.tostring());
              }
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else  {
    return xmlHttp;
    }
}

//don't need this for Firefox
//use of time variables to prevent IE from caching the results
var date = new Date();
var timestamp = date.getTime();
var url = 'rotate.php?' + 'time='+ timestamp;
var browser = navigator.appName;
var version = navigator.appVersion;
var agt=navigator.userAgent.toLowerCase();



//test which browser because IE needs POST and Mozilla needs GET
//code for IE cos IE will only work with POST

                               //get the page via Internet Explorer
                               function sendMSIERequest() {
                                 //commented out test code for broswer
                                 //alert(browser + " " + version);
                                 //xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                                 xmlHttp.open('post', url, true);
                                 xmlHttp.onreadystatechange = handleResponse;
                                 xmlHttp.send(null);

                               }

//get the page if Mozilla  cos it will only work with GET


                               //get the page via Internet Explorer
                               function sendMozillaRequest() {
                                 //commented out test code for broswer
                                 //alert(browser + " " + version);
                                 //xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                                 xmlHttp.open('get', url, true);
                                 xmlHttp.onreadystatechange = handleResponse;
                                 xmlHttp.send(null);

                               }




// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();


function handleResponse() {
  if(xmlHttp.readyState == 4) {
    var response = xmlHttp.responseText;
    myDiv = document.getElementById('ajax_music').innerHTML = response;
  }
}

//set the interval for the repeated call.
if(agt.indexOf('msie') != -1){
limit = setInterval('sendMSIERequest()', 5000);
}
else
{limit = setInterval('sendMozillaRequest()', 5000);}


// prevent ajax memory leakage
window.onunload = function() {
delete(xmlHttp.request);
}


