var xmlhttp = findXMLHttp();

//Funktion för att hitta bästa möjliga XMLHTTP activex 
function findXMLHttp() {
  var xmlhttp;

  //Prova först med inbygd xmlhttp
  if (window.XMLHttpRequest) {
  xmlhttp = new XMLHttpRequest();
  }

  //om inte prova med activeX varianter
  else{

  //Lista med activex verisioner, 
  //radera en eller fler om du inte vill prova alla.
  //Effektivast överst, mest utbrett stöd underst
  var xmlhttpVersions = ["MSXML2.XMLHttp.6.0",
  "MSXML2.XMLHttp.5.0",
  "MSXML2.XMLHttp.4.0",
  "MSXML2.XMLHttp.3.0",  
  "MSXML2.XMLHttp",
  "Microsoft.XMLHttp"];


  //Prova de olika verisionerna
  for (var i = 0; i < xmlhttpVersions.length; i++) {
  if (!xmlhttp) {
  try{xmlhttp = new ActiveXObject(xmlhttpVersions[i]);} 
  catch (e) { xmlhttp = false;}
  }
  }

  //Starta en instans av vald activex, 
  //om den inte redan är startad som inbygd
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
  try { xmlhttp = new XMLHttpRequest();} 
  catch (e) { xmlhttp = false;}
  }
  
  //om activexen inte stödjs, visa det för besökaren.
  if (!xmlhttp) {
  alert('Kontrollera att din webläsare stödjer XMLhttp');
  return false;
  }
  
  }
  return xmlhttp;
}


function preSearch() {
    //Put the form data into a variable
	var theQuery = document.getElementById('query').value;
	
    //If the form data is *not* blank, query the DB and return the results
	if(theQuery !== "") {
        //Change the content of the "lajv" DIV to "Searching..."
        //This gives our user confidence that the script is working if it takes a moment for the lajv to be returned. However the user will likely never see this...
		document.getElementById('lajv').innerHTML = "Searching...";
		
        //This sets a variable with the URL (and query strings) to our PHP script
		var url = 'search.php?q=' + theQuery;
        //Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
		xmlhttp.open('GET', url, true);
        //Check that the PHP script has finished sending us the result
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                //Replace the content of the "lajv" DIV with the result returned by the PHP script
				document.getElementById('lajv').innerHTML = xmlhttp.responseText + ' ';
			} else {
                //If the PHP script fails to send a response, or sends back an error, display a simple user-friendly notification
				document.getElementById('lajv').innerHTML = "Searching...";
			}
		};
		xmlhttp.send(null);  
	}
}
	
function sndReq(vote,id_num,ip_num) {
	
	var element = document.getElementById('unit_long'+id_num);
	//new Effect.Fade(element);
    element.innerHTML = '<div style="height: 30px;"><em><IMG SRC="/infusions/varcade/img/loading.gif">Loading ...</em></div>';
	
    xmlhttp.open('get', '/infusions/mods/rpc.php?j='+vote+'&q='+id_num+'&t='+ip_num);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);
	
}


function PlayerReq(id_num,mod_num) {
	
	var element = document.getElementById('player'+id_num);
	//new Effect.Fade(element);
    element.innerHTML = 'Loading...';
    xmlhttp.open('GET', '/infusions/mods/player.php?j='+id_num+'&m='+mod_num);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);
	
}


function sortbyReq(id_num,mod_num) {
	
	var element = document.getElementById('sortby'+id_num);
	//new Effect.Fade(element);
    element.innerHTML = 'Loading...';
	
    xmlhttp.open('get', '/infusions/mods/sortby.php?j='+id_num+'&m='+mod_num);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);
	
}


function handleResponse() {
    if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
       	
        var response = xmlhttp.responseText;
        var update = new Array();

        if(response.indexOf('|') != -1) {
            update = response.split('|');
            changeText(update[0], update[1]);
        }
		}
    }
}


function changeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"

    if (DOM) {
        var viewer = document.getElementById(div2show)
        viewer.innerHTML=text
    }
    else if(IE) {
        document.all[div2show].innerHTML=text
    }
}
