/**AJAX.JS**/
//Some javascript ajax functions...

//send a blind GET request to the server (reply is not checked for)
function ajaxBlindRequest(request){

  request+="&timestamp_ie_nocache="+new Date().getTime();
  var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Could not initiate Ajax. Some features may not function as expected.\n\nPlease upgrade to a modern browser.");
				return false;
			}
		}
	}
	ajaxRequest.open("GET", request, true);
	ajaxRequest.send(null); 
}

//send a GET request to the server (reply sent to a callback function)
//optional: specify a timeout (IN SECONDS) after which the operation should be cancelled, and (optional) a callback function for when the timeout time is reached
function ajaxRequest(request,callback,timeout,timeOutCallback){
  request+="&timestamp_ie_nocache="+new Date().getTime();
  var timer=null;
  var timedOut=false;
  
  function callTimeOutCallback(){
    timedOut=true;
    timeOutCallback();
  }
  
  var alertTimerId=null
  if (timeout!=undefined && timeOutCallback!=undefined){//set up the timeout timer
    alertTimerId = setTimeout (callTimeOutCallback, timeout*1000);
  }
  var ajaxRequest;
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Could not initiate Ajax. Some features may not function as expected.\n\nPlease update to a modern browser.");
				return false;
			}
		}
	}
	ajaxRequest.onreadystatechange = function(){
	  if (alertTimerId!=null)
	   clearTimeout(alertTimerId);
  	if(ajaxRequest.readyState == 4 && !timedOut)
      callback(ajaxRequest.responseText);
  }
  
	ajaxRequest.open("GET", request, true);
	ajaxRequest.send(null); 
}

//load content into an id using ajax with an OPTIONAL loading bar
function ajaxLoadContent(request,id,bargraphic,user_callback){

    request+="&timestamp_ie_nocache="+new Date().getTime();

    var e=document.getElementById(id);
    
    function addRow(tds){
        var tr = document.createElement('tr');
        for (var i=0;i<tds.length;i++){
            var td = document.createElement("td");
            td.innerHTML = tds[i];
            tr.appendChild(td);
        }
        
        e.appendChild(tr);    
    }
    if (bargraphic!=null){
        if (e.nodeName=='TBODY'){
            while (e.hasChildNodes())
                e.removeChild(e.firstChild);
            addRow(new Array("","<img src='"+bargraphic+"'>"));
            //BADGEGAMES ONLY:
            var _TR = document.createElement('tr');
            _TR.style.height=holdHeight;
            e.appendChild(_TR);
        }else{
           var put="<div style='";
            put+='margin-bottom:'+holdHeight;
            put+="'><img src='"+bargraphic+"'></div>";
            e.innerHTML=put;
           
        }
        
    }
    function callback(s){
        
        if (e.nodeName=='TBODY'){
            //empty e
             while (e.hasChildNodes())
                e.removeChild(e.firstChild);
             
             s=s.replace(/[\r\n]+/g, "");;
            //create array of td's from e:
            var tr=s.match(/<tr>.*?<\/tr>/g);
            for (var y=0;y<tr.length;y++){
                
                var TR = document.createElement('tr');
                
                
                tr[y]=tr[y].replace("<tr>","");
                tr[y]=tr[y].replace("</tr>","");
                
                var td=tr[y].match(/<td.*?>.*?<\/td>/g);
                
                for (var x=0;x<td.length;x++){
                    var clas=td[x].match(/class="(.*?)"/);
                    clas=clas[1];//capture class
                    td[x]=td[x].replace(/<td.*?>/,"");
                    td[x]=td[x].replace("</td>","");
                    var TD = document.createElement("td");
                    TD.innerHTML = td[x];
                    if (clas!=null)
                        addClass(TD,clas);
                    TR.appendChild(TD);
                }
                
                e.appendChild(TR); 
            }
        }else
            e.innerHTML=s;
        if (user_callback!=undefined && user_callback!=null)
            eval(user_callback+"()");
    }
    ajaxRequest(request,callback,10);
    
    function addClass(element, value) {
      if(!element.className) {
          element.className = value;
      } else {
          newClassName = element.className;
          newClassName+= " ";
          newClassName+= value;
          element.className = newClassName;
      }
    }
}
