﻿var parserVote;
var demarrage=1;
var xmlhttp_vote = null;
var cpt=1;

//##############################################################
//Classe Parseur
//##############################################################
	// Constructeur de la classe
    function Parseur(txtXml){
		this.texteXml=txtXml;
     
        // Déclaration des fonctions  (méthode)
		this.verifXml = VerifierXmlVide;
        this.affichageTouteDonnee = ObtenirDonnee;
		this.affichageDonneePrecise = ObtenirDonneePrecise;
		this.compterMorceaux = ObtenirNbMorceaux;
    }

    // Implémentation du code des fonctions 
	function VerifierXmlVide(){
		var xml=this.texteXml;
		var cherche1="<morceau>";
		var cherche2="</morceau>";

		var res1 = xml.indexOf(cherche1,0)+cherche1.length;
		var res2 = xml.lastIndexOf(cherche2,xml.length);
		var resultat = xml.substring(res1,res2);
		if(resultat=="")
			return true;
		else
			return false;
     }
	 
	 function ObtenirDonneePrecise(baliseRecherchee,pos){
		var xml=this.texteXml;
		var cherche1="<"+baliseRecherchee+">";
		var cherche2="</"+baliseRecherchee+">";
		var baliseInfoFermante="</information>";

		var posInformation1=0;
		var resultat="";
		
		for(i=1;i<=5;i++){
			var posInformation2 = xml.indexOf(baliseInfoFermante,posInformation1);
			var res1 = xml.indexOf(cherche1,posInformation1)+cherche1.length;
			var res2 = xml.lastIndexOf(cherche2,posInformation2);
			if(pos==i)
			resultat += xml.substring(res1,res2);
			posInformation1=posInformation2+14;
		}
		return resultat;
     }
	 
    function ObtenirDonnee(baliseRecherchee){
		var xml=this.texteXml;
		var cherche1="<"+baliseRecherchee+">";
		var cherche2="</"+baliseRecherchee+">";
		var baliseInfoFermante="</information>";

		var posInformation1=0;
		var resultat="";
		for(i=1;i<=5;i++){
			var posInformation2 = xml.indexOf(baliseInfoFermante,posInformation1);
			var res1 = xml.indexOf(cherche1,posInformation1)+cherche1.length;
			var res2 = xml.lastIndexOf(cherche2,posInformation2);
			resultat += xml.substring(res1,res2)+" | ";
			posInformation1=posInformation2+14;
		}
		return resultat;
     }
	 
	 	
	   function ObtenirNbMorceaux(){
		var xml=this.texteXml;
		var cherche1="<information>";
		var baliseInfoFermante="</information>";

		var posInformation1=0;
		var resultat=0;
		for(i=1;i<=5;i++){
			var posInformation2 = xml.indexOf(baliseInfoFermante,posInformation1);
			var res1 = xml.indexOf(cherche1,posInformation1)+cherche1.length;
			if(xml.substring(res1,posInformation2)!="")
				resultat++;
			posInformation1=posInformation2+14;
		}
		return resultat;
     }

if (demarrage==1)
{
	
	if(window.XMLHttpRequest) 
		{
			//Firefox   
			xhr_object_data = new XMLHttpRequest();   
			xhr_object_achat= new XMLHttpRequest();   
			xmlhttp_vote = new XMLHttpRequest();   
			/*if (xhr_object_achat.overrideMimeType) 
				{
					xhr_object_achat.overrideMimeType('text/xml');
				}	*/
					
		}
	 else 
		{ 
			if(window.ActiveXObject) 
				//Internet Explorer   
			/*{
				xhr_object_data = new ActiveXObject("Microsoft.XMLHTTP");   
			}*/	
				
			{
				try 
						{
								
							xhr_object_data = new ActiveXObject("Msxml2.XMLHTTP"); //Microsoft.XmlDom
							xhr_object_achat = new ActiveXObject("Msxml2.XMLHTTP"); //Microsoft.XmlDom
							xmlhttp_vote = new ActiveXObject("Msxml2.XMLHTTP"); //Microsoft.XmlDom
							
						}
						catch (e)
						{
							xhr_object_data = new ActiveXObject("Microsoft.XMLHTTP");
							xhr_object_achat = new ActiveXObject("Microsoft.XMLHTTP");
							xmlhttp_vote = new ActiveXObject("Microsoft.XMLHTTP");
							
						}
			}
		else 
			// XMLHttpRequest non supporté par le navigateur   
			{ 
				alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");   
			}
		}	
	
demarrage=0;
}

function chargementnouveauvote(id_local,units_local,compteur)
{
	cpt=compteur;
	var url='../vote_site/_drawratingSite.php'
	//avec le get on ajoute le ? pour les variables     variable1=valeur1&variable2=valeur2...
	url=url+'?id='+id_local+'&units='+units_local;
    xmlhttp_vote.open('GET', url+ "&date=" + escape(new Date()),true);
    xmlhttp_vote.onreadystatechange = handleResponsennewvote;
    xmlhttp_vote.send(null);
}

//##############################################################	
function handleResponsennewvote()
{
  if(xmlhttp_vote.readyState == 4)
	{
		if (xmlhttp_vote.status == 200)
			{
       	        var response = xmlhttp_vote.responseText;
				//alert(response);
				document.getElementById('vote'+cpt).innerHTML=response;
				//vote_morceau_div.innerHTML=response;
				//demarragevote=1;
				//demarragevote=0;
				//memorisation();
		    }
    }
}

//##############################################################	
//function envoievote(ncount_local,id_local,ip_local,units_local)
//function envoievote(ncountlocal,iplocal,idlocal,unitslocal,nompclocal,originevote)
function envoievote(ncountlocal,id_musique,compteur)
{
	var url='../datasun/iphone/service/vote.php';
	var id_morceau=id_musique;
	cpt=compteur;
	//alert(id_morceau);
	var vote=ncountlocal;
	url=url+'?id_morceau='+id_morceau+'&uuid=ip&vote='+vote;
    xmlhttp_vote.open('get', url,true);
    xmlhttp_vote.onreadystatechange = handleResponsevoteok;
    xmlhttp_vote.send(null);	
		

}	

//##############################################################	
function handleResponsevoteok()
{
  if(xmlhttp_vote.readyState == 4)
	{
		if (xmlhttp_vote.status == 200)
			{
				var xmlVote=xmlhttp_vote.responseText;
				parserVote=new Parseur(xmlVote);
				var verif=parserVote.verifXml();
				if (verif){
					alert('xml vide');
				}	
				else{	
					id_global=parserVote.affichageDonneePrecise("id_morceau",1);
					units_global=parserVote.affichageDonneePrecise("note",1);
					chargementnouveauvote(id_global,units_global,cpt);					
				}
						
		    }
    }
}
