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 AJAX.");
	
    return null; 
	
}

function GetId(id) {
	
    return document.getElementById(id);
    
}

function Trim(aString) {
	
    return aString.replace('/^\s+/', "").replace('/\s+$/', "");
    
}

function Loaded() {
	
	var imgs = document.getElementsByTagName('img');
	
	for (var i = 0; i < imgs.length; ++i) {
		
		var img = imgs[i];
		
		if (img.title == "Image") {
			
			var width = parseInt(img.parentNode.parentNode.clientWidth) - 10;
			
			if (width == -10)
				width =  parseInt(img.parentNode.parentNode.offsetWidth) - 10;
			
			if (img.width > width) {
				
				var ratio = img.height / img.width;
				
				img.width = width;
				img.height = width * ratio;
			
			}
			
		}
		
		
	}
	
}

function ShowLangage(id) {
	
	var div = document.getElementById('langage' + id);
	
	if (div.style.display == 'none')
		div.style.display = '';
	else
		div.style.display = 'none';
	
}

function CheckJonTeamForm() {
	
	var txtNom = document.getElementById('txtNom');
	var txtPrenom = document.getElementById('txtPrenom');
	var txtMail = document.getElementById('txtMail');
	var txtLangages = document.getElementById('txtLangages');
	var txtMotivation = document.getElementById('txtMotivation');
	var txtCaptcha = document.getElementById('txtCaptcha');
	
	var returnValue = true;
	
	if (Trim(txtNom.value) == '') {
		GetId('spanErrorNom').style.display = '';
		GetId('spanErrorNom').innerHTML = '<br />Veuillez saisir votre nom';
		returnValue = false;
	}
	else
		GetId('spanErrorNom').style.display = 'none';
		
	if (Trim(txtPrenom.value) == '') {
		GetId('spanErrorPrenom').style.display = '';
		GetId('spanErrorPrenom').innerHTML = '<br />Veuillez saisir votre prénom';
		returnValue = false;
	}
	else
		GetId('spanErrorPrenom').style.display = 'none';
	
	if (Trim(txtMail.value) == '') {
		GetId('spanErrorMail').style.display = '';
		GetId('spanErrorMail').innerHTML = '<br />Veuillez saisir votre adresse e-mail';
		returnValue = false;
	}
	else
		GetId('spanErrorMail').style.display = 'none';
	
	if (Trim(txtLangages.value) == '') {
		GetId('spanErrorLangages').style.display = '';
		GetId('spanErrorLangages').innerHTML = '<br />Veuillez saisir les langages visés par vos tutoriels';
		returnValue = false;
	}
	else
		GetId('spanErrorLangages').style.display = 'none';
	
	if (Trim(txtMotivation.value) == '') {
		GetId('spanErrorMotivation').style.display = '';
		GetId('spanErrorMotivation').innerHTML = '<br />Veuillez saisir votre motivation';
		returnValue = false;
	}
	else
		GetId('spanErrorMotivation').style.display = 'none';
	
	if (Trim(txtCaptcha.value) == '') {
		GetId('spanErrorCaptcha').style.display = '';
		GetId('spanErrorCaptcha').innerHTML = '<br />Veuillez saisir la réponse prouvant que vous n\'êtes pas un bot';
		returnValue = false;
	}
	else
		GetId('spanErrorCaptcha').style.display = 'none';
	
	if (txtCaptcha.value != 64) {
		GetId('spanErrorCaptcha').style.display = '';
		GetId('spanErrorCaptcha').innerHTML = '<br />Réponse incorrecte';
		returnValue = false;
	}
	else
		GetId('spanErrorCaptcha').style.display = 'none';
		
	
	return returnValue;
	
}

function colorStar(index) {
	
	for (var i = 0; i <= index; ++i)
		document.getElementById('star' + i).src = "http://www.areaprog.com/images/star.png";
	
}

function uncolorStar(index) {
	
	for (var i = 0; i <= index; ++i)
		document.getElementById('star' + i).src = "http://www.areaprog.com/images/unstar.png";
	
}

function vote(id_cours, note) {
	
	var xhr = getXhr();
	
	xhr.onreadystatechange = function() {
		
		if(xhr.readyState == 4 && xhr.status == 200) {

			document.getElementById('vote').innerHTML = "Merci d'avoir voté !";
			
		}
	}

	xhr.open("POST", "../kernel/vote.php", true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("&id_cours=" + id_cours + "&note=" + (note + 1));
	
}