// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();
// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject()
{
// will store the reference to the XMLHttpRequest object
var xmlHttp;
// if running Internet Explorer
if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
xmlHttp = false;
}
}
// if running Mozilla or other browsers
else
{
try
{
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
xmlHttp = false;
}
}
// return the created object or display an error message
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}
// make asynchronous HTTP request using the XMLHttpRequest object
function process(){
// proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
      // retrieve the name typed by the user on the form
      id = encodeURIComponent(document.getElementById("info").value);
      // execute the quickstart.php page from the server
      xmlHttp.open("GET", "get_xml.php?id=" + id, true);
     // define the method to handle server responses
     xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }else{
  // if the connection is busy, try again after one second
     setTimeout('process()', 1000);
  }
 }
// executed automatically when a message is received from the server

function show_channel(id) {
    // branch for native XMLHttpRequest object
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
      xmlHttp.open("GET", "get_xml.php?id=" + id, true);
	  
     // define the method to handle server responses
     xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = handleServerResponse;
            req.open("GET","get_xml.php?id=" + id, true);
            req.send();
        }
    }
}


var xmlDoc ;
function loadXML(xmlFile)
{
	
	//alert("Browser");
	if (window.ActiveXObject){ 
	    xmlDoc= new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.onreadystatechange=verify;
		//alert("HERE11");
		xmlDoc.load(xmlFile);
	}else if (document.implementation && document.implementation.createDocument){
		//alert("HERE");
        xmlDoc=document.implementation.createDocument("","",null);
		xmlDoc.load(xmlFile);

    }else{
	    //alert("HERE22");	
	}
ticker=xmlDoc.documentElement;
return ticker;
} 


function verify()
{  
//alert(xmlDoc.readyState);
if (xmlDoc.readyState != 4)
{  
 return false;  
}
}








function build_data(){
	output=loadXML('output.xml');   
        //alert(output);
	
		var records=output.childNodes;
				
		for ( var i = 0; i < records.length; i++ ) {
			// townName = records[i].firstChild.firstChild.nodeValue;
			 var id=output.childNodes[i].childNodes[0].text;
			 var name=output.childNodes[i].childNodes[1].text;
			 var desc=output.childNodes[i].childNodes[2].text;
			 name_url=name.replace(/ /,"_");
			 //alert(townName);
			 		 
			 
			  html += '<li><a href="show_stream.php?id=' + id + '&name=' + name_url + '" target="iframe" onMouseOver="javascript:change(\'' + desc + '\')"  onMouseOut="javascript:change(\'\')">' + name + '</a> </li>';
		}
		
		country=output.childNodes[0].childNodes[3].text;	
	
}


function handleServerResponse(){
// move forward only if the transaction has completed
if (xmlHttp.readyState == 4){
// status of 200 indicates the transaction completed successfully
   if (xmlHttp.status == 200){
   // extract the XML retrieved from the server
   
   
      xmlResponse = xmlHttp.responseXML;
	  
	 // alert(xmlHttp.responseText);
     // obtain the document element (the root element) of the XML structure
     // xmlRoot = xmlResponse.documentElement;
     // get the text message, which is in the first child of
     // the the document element
      //typemessage = xmlRoot.firstChild.data;
      // update the client display using the data received from the server
     /*if(typemessage=="Doesn't exist"){
	    myDiv = document.getElementById("myDivElement");
      // display the HTML output
        myDiv.innerHTML = "User not found";}else{*/
       var html = "";
	   
	   html += "<ul>";
		
   if(xmlResponse.documentElement==null){
	file=document.getElementById('info').innerHTML;
	file=file + ".xml";
	//alert(file);
	output=loadXML(file);   
        //alert(output);
	
		var records=output.childNodes;
				
		for ( var i = 0; i < records.length; i++ ) {
			// townName = records[i].firstChild.firstChild.nodeValue;
			 var id=output.childNodes[i].childNodes[0].text;
			 var name=output.childNodes[i].childNodes[1].text;
			 var desc=output.childNodes[i].childNodes[2].text;
			 name_url=name.replace(/ /,"_");
			 //alert(townName);
		  statut=output.childNodes[i].childNodes[4].text;
		  
		  var star="";
		  
		      if(statut ==1){
			     star="**";
			  }else if(statut ==4){
			    star="***";
			  }else if(statut ==3){
			    star="*";
			  }
			 		 
			 
			  html += '<li><a href="show_stream1.php?id=' + id + '&name=' + name_url + '" target="iframe" onMouseOver="javascript:change(\'' + desc + '\')"  onMouseOut="javascript:change(\'\')">' + name + ' ' + star +'</a> </li>';
		}
		
		country=output.childNodes[0].childNodes[3].text;		     //alert("Cant be loaded"); 

		/*var t=records.length;
		
		alert(l);*/
        //alert(p);
}else{	
xmlRoot = xmlResponse.documentElement;
//alert('Je suis la');

//alert(xmlHttp.responseText);
/*alert(xmlRoot);
		nameArray = xmlRoot.getElementsByTagName("name");
		sql=nameArray.item(0).firstChild.data;
		alert(sql);*/
// obtain arrays with book titles and ISBNs
       idArray = xmlRoot.getElementsByTagName("id");
	   var con = idArray.length;
	   //alert(idArray.length);
	   if(con==0){
	output=loadXML('output.xml');   
        //alert(output);
	
		var records=output.childNodes;
				
		for ( var i = 0; i < records.length; i++ ) {
			// townName = records[i].firstChild.firstChild.nodeValue;
			 var id=output.childNodes[i].childNodes[0].text;
			 var name=output.childNodes[i].childNodes[1].text;
			 var desc=output.childNodes[i].childNodes[2].text;
			 name_url=name.replace(/ /,"_");
			 //alert(townName);
		  statut=output.childNodes[i].childNodes[4].text;
		  
		  var star="";
		  
		      if(statut ==1){
			     star="**";
			  }else if(statut ==4){
			    star="***";
			  }else if(statut ==3){
			    star="*";
			  }
			 		 
			 
			  html += '<li><a href="show_stream1.php?id=' + id + '&name=' + name_url + '" target="iframe" onMouseOver="javascript:change(\'' + desc + '\')"  onMouseOut="javascript:change(\'\')">' + name + ' ' + star +'</a> </li>';
		}
		
		country=output.childNodes[0].childNodes[3].text;	
		
		}else{
	   //alert(idArray.length + "||");
       nameArray = xmlRoot.getElementsByTagName("name");
	   descArray = xmlRoot.getElementsByTagName("desc");
	   statsArray = xmlRoot.getElementsByTagName("statut");
	   //test=descArray.item(0).firstChild.data;
       //alert (test);
// iterate through the arrays and create an HTML structure
      countryArray= xmlRoot.getElementsByTagName("country");
      country=countryArray.item(0).firstChild.data;
	  
	  
	  //alert(country);
      for (var i=0; i<idArray.length; i++){
		    
		  channel=nameArray.item(i).firstChild.data;
		  //alert(channel);
		  name_url=channel.replace(/ /,"_");
		  var desc=descArray.item(i).firstChild.data;
		  statut=statsArray.item(i).firstChild.data;
		  
		  var star="";
		  
		      if(statut ==1){
			     star="**";
			  }else if(statut ==4){
			    star="***";
			  }else if(statut ==3){
			    star="*";
			  }
			  
		 // alert (desc1);
 html += '<li><a href="show_stream1.php?id=' + idArray.item(i).firstChild.data + '&name=' + name_url + '" target="iframe" onMouseOver="javascript:change(\'' + desc + '\')"  onMouseOut="javascript:change(\'\')">' + channel + ' ' + star +'</a></li>';
	  }
	  
	   }
}  
	  html += "</ul>";
// obtain a reference to the <div> element on the page
         divname='country' + country;
		 //alert (divname);
         myDiv = document.getElementById(divname);
// display the HTML output
        myDiv.innerHTML = html;
      
      
   //}//end if :typemsg
      //setTimeout('process()', 1000);
// a HTTP status different than 200 signals an error
 
}else{ //status
    alert("There was a problem accessing the server: " + xmlHttp.statusText);
  }//end if status
}//end if  ready
}//end function

// JavaScript Document