// Functions to capture and report JavaScript errors
function reportError(message, fileLocation, lineNo) {
//     alert ("Error message = " + message + "\r\nFile = " + fileLocation + "\r\nLine Number = " + lineNo);
     var data = "message=" + message + "&location=" + fileLocation + "&lineNo=" + lineNo;
//     alert (data);
     ajaxSend("errorReport.php", ajaxErrorReportCompleted, "POST", data);
     return true;
}

window.onerror = reportError;

// Functions to implement AJAX
// threadsafe asynchronous XMLHTTPRequest code
function ajaxSend(url, callback, type, data){


// we use a javascript feature here called "inner functions"
// using these means the local variables retain their values after the outer function
// has returned. this is useful for thread safety, so
// reassigning the onreadystatechange function doesn't stomp over earlier requests.


     function ajaxBindCallback(){
          if (ajaxRequest.readyState == 4) {
               if (ajaxRequest.status == 200) {
                    if (ajaxCallback){
                         window.clearTimeout(ajaxRequest.timer);
                         ajaxCallback(ajaxRequest.responseText);
                    } else {
                         alert('no callback defined');
                    }
               } else {
                    alert("There was a problem retrieving the xml data:\n" + ajaxRequest.status + ":\t" + ajaxRequest.statusText + "\n" + ajaxRequest.responseText);
               }
          }
     }


     // use a local variable to hold our request and callback until the inner function is called...
     var ajaxRequest = null;
     var ajaxCallback = callback;
     var ajaxData = data;
     var ajaxType = type;


     // bind our callback then hit the server...
     if (window.XMLHttpRequest) {
          // moz et al
          ajaxRequest = new XMLHttpRequest();
          ajaxRequest.onreadystatechange = ajaxBindCallback;
          ajaxRequest.open(ajaxType, url, true);
          if (type == 'POST') {
               ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
          }
          ajaxRequest.send(ajaxData);
     } else if (window.ActiveXObject) {
          // ie
          ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
          if (ajaxRequest) {
               ajaxRequest.onreadystatechange = ajaxBindCallback;
               ajaxRequest.open(ajaxType, url, true);
               if (type == 'POST') {
                    ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
               }
               ajaxRequest.send(ajaxData);
          }
     } else {
          // Ajax is not supported
          return false;
     }
     ajaxRequest.timer = setTimeout("abortRequest", 10000);
     return true;
}

function abortRequest(){
     if (window.location.hostname == "concerto") {
          alert("Request timed out");
     }
     ajaxRequest.abort();

}

function ajaxErrorReportCompleted(response) {
     // If development system, show error, otherwise end silently
     if (window.location.hostname == "localhost") {
          alert(response);
     }
}


