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;
	}
}

var nbre_vetements = 5;
var nbre_outils = 9;
function calcul_tableau(obj_select){
	if(obj_select.name.search('outil') != -1){ // kits et outils
		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_12'){ //frais d'envoi outils complémentaire
			for(i=1; i<=nbre_outils; i++){
				if(document.getElementById('outil_'+(i+2)).selectedIndex > 0){
					document.getElementById('outil_12').selectedIndex = 1;
					document.getElementById('input_outil_12').value = 1;
					break;
				} else {
					document.getElementById('outil_12').selectedIndex = 0;
					document.getElementById('input_outil_12').value = 0;
				}
			}
			calcul_tableau(document.getElementById('outil_12'));
		}
	} else { // vetements
		if (obj_select.name != 'vetement_6'){
			var objet_vet = obj_select.name.split("_");
			var nom_vet = objet_vet[0] + "_" + objet_vet[1];
			var taille_vet = objet_vet[2];
			var prix_unitaire = document.getElementById('prix_'+nom_vet).innerHTML;
			prix_unitaire = prix_unitaire.replace(',' , '.');
			prix_unitaire = parseFloat(prix_unitaire);
			var total_vetements_ligne = parseInt(document.getElementById(nom_vet).value);
			var total_vetements_taille = parseInt(obj_select.value);
			var previousValue = parseInt(obj_select.previousValue);
			var difference_taille = total_vetements_taille - previousValue;
			document.getElementById('input_' + nom_vet + '_' + taille_vet).value = total_vetements_taille; // stockage nbre vetement par taille
			document.getElementById(nom_vet).value = total_vetements_ligne + difference_taille; // stockage nbre total vetement toutes tailles confondues
			document.getElementById('input_' + nom_vet).value = total_vetements_ligne + difference_taille; // stockage nbre total vetement toutes tailles confondues
			var valeur = (document.getElementById(nom_vet).value * prix_unitaire).toFixed(2);
			valeur = valeur.replace('.' , ',');
			document.getElementById('total_'+nom_vet).innerHTML = valeur + "€";
			document.getElementById('input_total_'+nom_vet).value = valeur + "€";
			for(i=1; i<=nbre_vetements; i++){
				if(document.getElementById('vetement_'+i).selectedIndex > 0){
					document.getElementById('vetement_6').selectedIndex = 1;
					document.getElementById('input_vetement_6').value = 1;
					break;
				} else {
					document.getElementById('vetement_6').selectedIndex = 0;
					document.getElementById('input_vetement_6').value = 0;
				}
			}
			calcul_tableau(document.getElementById('vetement_6'));
		} else { //frais d'envoi vetements
			var prix_unitaire = document.getElementById('prix_vetement_6').innerHTML;
			prix_unitaire = prix_unitaire.replace(',' , '.');
			prix_unitaire = parseFloat(prix_unitaire);
			var valeur = (document.getElementById('vetement_6').value * prix_unitaire).toFixed(2);
			valeur = valeur.replace('.' , ',');
			document.getElementById('total_vetement_6').innerHTML = valeur + "€";
			document.getElementById('input_total_vetement_6').value = valeur + "€";
		}
	}
	//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<=12; i++){
		total_prix = document.getElementById('total_outil_'+i).innerHTML;
		total_prix = total_prix.replace(',' , '.');
		total_prix = parseFloat(total_prix);
		total_ht += total_prix;
	}
	for(i=1; i<=6; i++){
		total_prix = document.getElementById('total_vetement_'+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;
	}
}

