function prechargimg() {
	var doc=document;
	if(doc.images){
		if(!doc.precharg) doc.precharg=new Array();
		var i,j=doc.precharg.length,x=prechargimg.arguments; for(i=0; i<x.length; i++)
		if (x[i].indexOf("#")!=0){ 
			doc.precharg[j]=new Image; doc.precharg[j++].src=x[i];
		}
	}
}

function couleur(obj) {
     obj.style.backgroundColor = "#FFFFFF";
}

function check() {
	var msg = "";

	var L=document.form_contact.civilite.length;
	var bt_checked="0";
	for (var i = 0 ; i< L ; i++){
		if(document.form_contact.civilite[i].checked) {
			bt_checked="1";
			break;
		}
	}
	if (bt_checked == "0")	{
		msg += "Veuillez sélectionner votre civilité.\n\n";
	}
	
	if (document.form_contact.nom.value == "")	{
		msg += "Veuillez saisir votre nom.\n\n";
		document.form_contact.nom.style.backgroundColor = "#FFBBBB";
	}

	if (document.form_contact.prenom.value == "")	{
		msg += "Veuillez saisir votre prénom.\n\n";
		document.form_contact.prenom.style.backgroundColor = "#FFBBBB";
	}
	
	if (document.form_contact.cp.value == "")	{
		msg += "Veuillez saisir votre code postal.\n\n";
		document.form_contact.cp.style.backgroundColor = "#FFBBBB";
	}
	
	if (document.form_contact.mail.value != "")	{
		indexAroba = document.form_contact.mail.value.indexOf('@');
		indexPoint = document.form_contact.mail.value.indexOf('.');
		if ((indexAroba < 0) || (indexPoint < 0))		{
			document.form_contact.mail.style.backgroundColor = "#FFBBBB";
			msg += "L'Email est incorrecte.\n\n";
		}
	} else {
		document.form_contact.mail.style.backgroundColor = "#FFBBBB";
		msg += "Veuillez saisir votre e-mail.\n\n";
	}
	
	if (document.form_contact.commentaires.value == "")	{
		msg += "Veuillez saisir un commentaire.\n\n";
		document.form_contact.commentaires.style.backgroundColor = "#FFBBBB";
	}

	if (msg == "") return true;
	else {
		alert(msg);
		return false;
	}
}

function calcul_tableau(obj_select){
	var prix_unitaire = document.getElementById('prix_'+obj_select.name).innerHTML;
	prix_unitaire = prix_unitaire.replace(',' , '.');
	prix_unitaire = parseFloat(prix_unitaire);
	var valeur = (obj_select.value * prix_unitaire).toFixed(2);
	valeur = valeur.replace('.' , ',');
	document.getElementById('total_'+obj_select.name).innerHTML = valeur + " €";
	document.getElementById('input_total_'+obj_select.name).value = valeur + " €";
	if(obj_select.name == 'outil_1'){ //frais d'envoi par kit
		document.getElementById('outil_2').selectedIndex = obj_select.value;
		document.getElementById('input_outil_2').value = obj_select.value;
		calcul_tableau(document.getElementById('outil_2'));
	} else if (obj_select.name != 'outil_2' && obj_select.name != 'outil_11'){ //frais d'envoi outils complémentaire
		for(i=1; i<=8; i++){
			if(document.getElementById('outil_'+(i+2)).selectedIndex > 0){
				document.getElementById('outil_11').selectedIndex = 1;
				document.getElementById('input_outil_11').value = 1;
				break;
			} else {
				document.getElementById('outil_11').selectedIndex = 0;
				document.getElementById('input_outil_11').value = 0;
			}
		}
		calcul_tableau(document.getElementById('outil_11'));
	}
	//calcul total :
	var total_ht = 0;
	var total_prix = 0;
	var total_tva = 0;
	var total_ttc = 0;
	var poucentageTVA = 0.196;
	for(i=1; i<=11; i++){
		total_prix = document.getElementById('total_outil_'+i).innerHTML;
		total_prix = total_prix.replace(',' , '.');
		total_prix = parseFloat(total_prix);
		total_ht += total_prix;
	}
	total_ttc = total_ht + total_ht * poucentageTVA;
	total_tva = Math.round(total_ht * poucentageTVA *100)/100;
	total_tva = total_tva.toFixed(2) + " €";
	total_ttc = total_ttc.toFixed(2) + " €";
	total_ht = total_ht.toFixed(2) + " €";
	total_tva = total_tva.replace('.' , ',');	
	total_ht = total_ht.replace('.' , ',');
	total_ttc = total_ttc.replace('.' , ',');
	document.getElementById('total_HT').innerHTML = total_ht;
	document.getElementById('input_total_HT').value = total_ht;
    document.getElementById('total_TVA').innerHTML = total_tva;
	document.getElementById('input_total_TVA').value = total_tva;
    document.getElementById('total_TTC').innerHTML = total_ttc;
	document.getElementById('input_total_TTC').value = total_ttc;
}

function check_commande() {
	var msg = "";
	if(document.getElementById('input_total_TTC').value == "0,00 €"){
		msg += "Votre commande est de 0,00 € et ne peut donc être envoyée !\n\n";
	}
	if (msg == "") return true;
	else {
		alert(msg);
		return false;
	}
}

function valider_distributeur() {
	var msg = "";
	
	var invalid = " "; // Invalid character is a space
	var minLength = 6; // Minimum length
	
	var mdp = document.form_modif.mdp.value;
	var login = document.form_modif.login.value;
	
	if (document.form_modif.raison.value == "")	{
		msg += "Veuillez saisir une raison sociale.\n\n";
		document.form_modif.raison.style.backgroundColor = "#FFBBBB";
	}
	if (document.form_modif.adresse.value == "")	{
		msg += "Veuillez saisir une adresse.\n\n";
		document.form_modif.adresse.style.backgroundColor = "#FFBBBB";
	}
	if (document.form_modif.codePostal.value == "")	{
		msg += "Veuillez saisir un code postal.\n\n";
		document.form_modif.codePostal.style.backgroundColor = "#FFBBBB";
	}
	if (document.form_modif.ville.value == "")	{
		msg += "Veuillez saisir une ville.\n\n";
		document.form_modif.ville.style.backgroundColor = "#FFBBBB";
	}
	if (document.form_modif.departement.value == "")	{
		msg += "Veuillez choisir un département.\n\n";
		document.form_modif.departement.style.backgroundColor = "#FFBBBB";
	}
	if (login==""){
		msg+='Veuillez saisir un identifiant.\n\n';
		document.form_modif.login.style.backgroundColor = "#FFBBBB";
	} else if(login == mdp) {
		msg+='Le mot de passe doit être différent de l\'identifiant.\n\n';
		document.form_modif.login.style.backgroundColor = "#FFBBBB";
		document.form_modif.mdp.style.backgroundColor = "#FFBBBB";
	} 
	// check for minimum length
	/*if (mdp==""){
		msg+='Veuillez saisir un mot de passe.\n\n';
		document.form_modif.mdp.style.backgroundColor = "#FFBBBB";
	} else if (document.form_modif.mdp.value.length < minLength) {
		msg+='Le mot de passe doit contenir au moins ' + minLength + ' caractères.\n\n';
		document.form_modif.mdp.style.backgroundColor = "#FFBBBB";
	}*/
	// check for spaces
	if (document.form_modif.mdp.value.indexOf(invalid) > -1) {
		msg+='Les espaces ne sont pas acceptés dans les mots de passe.\n\n';
		document.form_modif.login.style.backgroundColor = "#FFBBBB";
	}
	// test du type de fichier pour les images
	var ext_logo = document.getElementById('logo').value.split('.').reverse()[0].toLowerCase();
	var ext_photo = document.getElementById('photo').value.split('.').reverse()[0].toLowerCase();
	if(ext_logo != "" && ext_logo != "jpg" && ext_logo != "jpeg" && ext_logo != "png" && ext_logo != "gif"){
		msg+='Le format de l\'image pour le logo est invalide.\n\nLes formats acceptés sont "jpg", "jpeg", "png" et "gif".\n\n';
		document.form_modif.logo.style.backgroundColor = "#FFBBBB";
	} else {
		document.form_modif.logo.style.backgroundColor = "#CCCCCC";
	}
	if(ext_photo != "" && ext_photo != "jpg" && ext_photo != "jpeg" && ext_photo != "png" && ext_photo != "gif"){
		msg+='Le format de l\'image pour la photo est invalide.\n\nLes formats acceptés sont "jpg", "jpeg", "png" et "gif".\n\n';
		document.form_modif.photo.style.backgroundColor = "#FFBBBB";
	} else {
		document.form_modif.photo.style.backgroundColor = "#CCCCCC";
	}
	if (msg == "") return true;
	else {
		alert(msg);
		return false;
	}
}
