// JavaScript Document

function select_innerHTML(objeto,innerHTML){

    objeto.innerHTML = ""
    var selTemp = document.createElement("micoxselect")
    var opt;
    selTemp.id="micoxselect1"
    document.body.appendChild(selTemp)
    selTemp = document.getElementById("micoxselect1")
    selTemp.style.display="none"
    if(innerHTML.toLowerCase().indexOf("<option")<0){//se não é option eu converto
        innerHTML = "<option>" + innerHTML + "</option>"
    }
    innerHTML = innerHTML.toLowerCase().replace(/<option/g,"<span").replace(/<\/option/g,"</span")
    selTemp.innerHTML = innerHTML
      
    
    for(var i=0;i<selTemp.childNodes.length;i++){
  var spantemp = selTemp.childNodes[i];
  
        if(spantemp.tagName){     
            opt = document.createElement("OPTION")
    
   if(document.all){ //IE
    objeto.add(opt)
   }else{
    objeto.appendChild(opt)
   }       
    
   //getting attributes
   for(var j=0; j<spantemp.attributes.length ; j++){
    var attrName = spantemp.attributes[j].nodeName;
    var attrVal = spantemp.attributes[j].nodeValue;
    if(attrVal){
     try{
      opt.setAttribute(attrName,attrVal);
      opt.setAttributeNode(spantemp.attributes[j].cloneNode(true));
     }catch(e){}
    }
   }
   //getting styles
   if(spantemp.style){
    for(var y in spantemp.style){
     try{opt.style[y] = spantemp.style[y];}catch(e){}
    }
   }
   //value and text
   opt.value = spantemp.getAttribute("value")
   opt.text = spantemp.innerHTML
   //IE
   opt.selected = spantemp.getAttribute('selected');
   opt.className = spantemp.className;
  } 
 }    
 document.body.removeChild(selTemp)
 selTemp = null
}

function ajaxSport(idSport, racine, idselect)
{ 
    var xhr=null;
    if (window.XMLHttpRequest) { 
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) 
    {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.onreadystatechange = function() { ajaxFinishEvent(xhr,idselect); };
	xhr.open("GET", racine+"/inc/find_event.php?idSport="+idSport+"&idselect="+idselect, true);
	xhr.send(null);
}

function getXMLHttpObj(){
	if(typeof(XMLHttpRequest)!='undefined')
		return new XMLHttpRequest();

	var axO=['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.4.0',
		'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'], i;
	for(i=0;i<axO.length;i++)
		try{
			return new ActiveXObject(axO[i]);
		}catch(e){}
	return null;
}

function loadScript(scriptpath){
	var oXML = getXMLHttpObj();
	oXML.open('GET', scriptpath, false);
	oXML.send('');
	//alert(oXML.responseText);
	eval(oXML.responseText);
	
}
function ajaxFinishEvent(xhr,idselect)
{
	
	if (xhr.readyState==4) 
    {
	var docXML= xhr.responseXML;

	var items = docXML.getElementsByTagName("event");
	var value;
	var newOption="";
	newOption="\n<option value=0>Then select an event</option>";
			for (i=0;i<items.length;i++)
			{
				value=items.item(i).firstChild.data.split(";");
				newOption+="\n<option value="+value[0]+">"+value[1]+"</option>";
			}
				if(document.all){ //IE
				select_innerHTML(document.getElementById("id_event"),newOption);
				}
				else document.getElementById("id_event").innerHTML=newOption; // ne fonctionne pas sous IE
	}	
}
function ajaxEvent(idEvent, racine, idselect)
{ 
    var xhr=null;
    if (window.XMLHttpRequest) { 
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) 
    {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.onreadystatechange = function() { ajaxFinishMatch(xhr,idselect); };
		xhr.open("GET", racine+"/inc/find_match.php?idEvent="+idEvent, true);
		xhr.send(null);
}

function ajaxFinishMatch(xhr)
{
	
	if (xhr.readyState==4) 
    {
	var docXML= xhr.responseXML;

	var items = docXML.getElementsByTagName("match");
	var value;
	var newOption="";
	
	newOption="\n<option value=0>Then select a fixture</option>";
		for (i=0;i<items.length;i++)
		{
			value=items.item(i).firstChild.data.split(";");
			
			newOption+="\n<option value="+value[0]+">"+value[1]+"</option>";
			
		}
			if(document.all){ //IE
			select_innerHTML(document.getElementById("id_match"),newOption);
			}
			else document.getElementById("id_match").innerHTML=newOption; // ne fonctionne pas sous IE
					
	}	
}

function ajaxCity(idCity, racine, idselect)
{ 
	var xhr=null;
    if (window.XMLHttpRequest) { 
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) 
    {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.onreadystatechange = function() { ajaxFinishCity(xhr,idselect); };
	xhr.open("GET", racine+"/inc/find_area.php?idCity="+idCity+"&idselect="+idselect, true);
	xhr.send(null);
}
function ajaxFinishCity(xhr)
{
	
	if (xhr.readyState==4) 
    {
	var docXML= xhr.responseXML;

	var items = docXML.getElementsByTagName("area");
	var value;
	var newOption="";
	
		for (i=0;i<items.length;i++)
		{
			value=items.item(i).firstChild.data.split(";");
			
			newOption+="\n<option value="+value[0]+">"+value[1]+"</option>";
			
		}
			
			//alert (newOption);
			if(document.all){ //IE
			select_innerHTML(document.getElementById("area"),newOption);
			}
			else document.getElementById("area").innerHTML=newOption; // ne fonctionne pas sous IE
					
	}	
}