var xhr=null;
var xhr=getXhr();

function getXhr() {
	if (window.XMLHttpRequest)
		return new XMLHttpRequest();
	if (window.ActiveXObject) {
		var names = ["Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
		for(var i in names) {
			try{return new ActiveXObject(names[i]);}
			catch(e){}
		}
	}
	window.alert("Votre navigateur ne prend pas en charge l'objet XMLHTTPRequest.");
	return null; // non supporté
}

function bcpvote(type,numero) {
	document.getElementById('voter'+numero).innerHTML='Vote en cours ...';
	xhr=getXhr();
	xhr.onreadystatechange=function() {
		if(xhr.readyState==4 && xhr.status==200) {
			document.getElementById('voter'+numero).innerHTML='C\'est noté !';
			document.getElementById('voter'+numero).onclick='';
			document.getElementById('nombrevote'+numero).innerHTML=xhr.responseText;
		}
	}
	xhr.open("POST",'bcp-vote.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send('type='+type+'&numero='+numero);
}

function calcul_distance() {
	xhr=getXhr();
	xhr.onreadystatechange=function() {
		if(xhr.readyState==4 && xhr.status==200) {
			document.getElementById('distance').innerHTML=xhr.responseText;
		}
	}
	xhr.open("POST",'/calculs/distance-calcul.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send('lo1='+document.getElementById('lo1').innerHTML+'&la1='+document.getElementById('la1').innerHTML+'&lo2='+document.getElementById('lo2').innerHTML+'&la2='+document.getElementById('la2').innerHTML);
}

function envoyez(theme,numero) {
	window.open('envoyez-'+theme+'-'+numero,'','height=500,width=500,left=40,top=40,scrollbars=yes');
}

function sudoku_score(numero) {
	nom=prompt('Entrez votre surnom pour enregistrer le résultat','');
	if (nom!=null && !nom.match('^[ ]+$','g')) {
		xhr.open("POST",'sudoku-enregistrer.php',true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send('numero='+numero+'&temps='+document.getElementById('temps').innerHTML+'&nom='+nom);
		xhr.onreadystatechange=function() {
			if(xhr.readyState==4 && xhr.status==200) {
				if (xhr.responseText) {
					alert('Votre résultat a bien été enregistré.');
					document.getElementById('reponse').innerHTML='Votre résultat a bien été enregistré. Vous pouvez le consulter sur la <a href="/sudoku-score">page des scores</a>.';
				}
				else alert('Votre score n\'a pas été enregistré. Veuillez réessayer');
			}
		}
	}
}

function chronometre() {
	temps=document.getElementById('temps').innerHTML.split(':');
	temps[2]=parseInt(temps[2],10)+1;
	temps[1]=parseInt(temps[1],10);
	if (temps[2]==60) {
		temps[1]+=1;
		temps[2]=0;
	}
	if (temps[1]==60) {
		temps[0]=parseInt(temps[0],10)+1;
		temps[1]=0;
	}
	temps[2]=((temps[2] < 10) ? "0" : "")+temps[2];
	temps[1]=((temps[1] < 10) ? "0" : "")+temps[1];
	document.getElementById('temps').innerHTML=temps[0]+':'+temps[1]+':'+temps[2];
	setTimeout("chronometre()",1000);
}

function calcul_jour() {
	document.getElementById('nombre').innerHTML='Calculs en cours ...';
	xhr=getXhr();
	xhr.onreadystatechange=function() {
		if (xhr.readyState==4 && xhr.status==200) {
			document.getElementById('nombre').innerHTML=xhr.responseText;
		}
	}
	xhr.open("POST",'/calculs/jour-calcul.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send('debut='+document.getElementById('debut').value+'&fin='+document.getElementById('fin').value);
}

function jour_date(type,debut,chiffre) {
	if (debut==0) document.getElementById(type).value=chiffre+document.getElementById(type).value.substring(2);
	else if (debut==1) document.getElementById(type).value=document.getElementById(type).value.substring(0,3)+chiffre+document.getElementById(type).value.substring(5);
	else if (debut>1) document.getElementById(type).value=document.getElementById(type).value.substring(0,debut)+chiffre+document.getElementById(type).value.substring(debut+1);
}

function calcul_joursemaine() {
	document.getElementById('nombre').innerHTML='Calculs en cours ...';
	xhr=getXhr();
	xhr.onreadystatechange=function() {
		if(xhr.readyState==4 && xhr.status==200) {
			document.getElementById('nombre').innerHTML=xhr.responseText;
		}
	}
	xhr.open("POST",'/calculs/joursemaine-calcul.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send('jour='+document.getElementById('jour').value);
}

function calcul_consommation() {
	if (document.getElementById('carburant').value.match('^[0-9]+(.|,)?[0-9]*$') && document.getElementById('kilometre').value.match('^[0-9]+(.|,)?[0-9]*$')) document.getElementById('consommation').innerHTML=(Math.round(document.getElementById('carburant').value.replace(',','.')/document.getElementById('kilometre').value.replace(',','.')*100000)/1000).toString().replace('.',',')+' L/100 km';
}

function calcul_gain() {
	if (document.getElementById('volume').value.match('^[0-9]+(.|,)?[0-9]*$') && document.getElementById('consommation').value.match('^[0-9]+(.|,)?[0-9]*$') && document.getElementById('pnormal').value.match('^[0-9]+(.|,)?[0-9]*$') && document.getElementById('preduit').value.match('^[0-9]+(.|,)?[0-9]*$') && document.getElementById('eloignement').value.match('^[0-9]+(.|,)?[0-9]*$')) document.getElementById('gain').innerHTML=(Math.round(100*((document.getElementById('volume').value.replace(',','.')*(document.getElementById('pnormal').value.replace(',','.')-document.getElementById('preduit').value.replace(',','.')))-document.getElementById('eloignement').value.replace(',','.')*document.getElementById('pnormal').value.replace(',','.')*document.getElementById('consommation').value.replace(',','.')/100))/100).toString().replace('.',',')+' €';
	if (document.getElementById('volume').value.match('^[0-9]+(.|,)?[0-9]*$') && document.getElementById('consommation').value.match('^[0-9]+(.|,)?[0-9]*$') && document.getElementById('pnormal').value.match('^[0-9]+(.|,)?[0-9]*$') && document.getElementById('preduit').value.match('^[0-9]+(.|,)?[0-9]*$')) document.getElementById('distance').innerHTML=(Math.round(100000*(document.getElementById('volume').value.replace(',','.')*(document.getElementById('pnormal').value.replace(',','.')-document.getElementById('preduit').value.replace(',','.'))/document.getElementById('pnormal').value.replace(',','.')/document.getElementById('consommation').value.replace(',','.')))/1000).toString().replace('.',',')+' km';
	if (document.getElementById('volume').value.match('^[0-9]+(.|,)?[0-9]*$') && document.getElementById('consommation').value.match('^[0-9]+(.|,)?[0-9]*$') && document.getElementById('pnormal').value.match('^[0-9]+(.|,)?[0-9]*$') && document.getElementById('eloignement').value.match('^[0-9]+(.|,)?[0-9]*$')) document.getElementById('ecart').innerHTML=(Math.round(10*document.getElementById('eloignement').value.replace(',','.')*document.getElementById('pnormal').value.replace(',','.')*document.getElementById('consommation').value.replace(',','.')/document.getElementById('volume').value.replace(',','.'))/1000).toString().replace('.',',')+' €';
}

