/* Scripts Javascripts*/

function InsereImage(source,rang,nombre,texte,format,largeur,hauteur)
	{
		a = Math.round((Math.random()*(nombre-1))+(rang-1)*nombre+1);
		document.write('<img src="'+source+a+'.'+format+'" alt="" width="'+largeur+'" height="'+hauteur+'" />');
	}

function PopupImage(image){ 
	w=open("",'image','width=400, height=400, top=100, left=100, toolbar=no, scrollbars=yes, resizable=yes');
	w.document.write("<html><head><title>"+image+"</title></head>");
	w.document.write("<body>");
	w.document.write("<p><img src='"+image+"' alt='' /></p>");
	w.document.write("</body></html>");
	w.document.close();
}


/*

CUSTOM FORM ELEMENTS

Created by Ryan Fait
www.ryanfait.com
Visit http://ryanfait.com/ for more information.

*/

var checkboxHeight = "18";
var radioHeight = "18";
var selectWidth = "190";

document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; } .disabled { opacity: 0.5; filter: alpha(opacity=50); }</style>');

var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
				span[a] = document.createElement("span");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						position = "0 -" + (checkboxHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					} else {
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.clear;
				if(!inputs[a].getAttribute("disabled")) {
					span[a].onmousedown = Custom.pushed;
					span[a].onmouseup = Custom.check;
				} else {
					span[a].className = span[a].className += " disabled";
				}
			}
		}
		inputs = document.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className == "styled") {
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				if(!inputs[a].getAttribute("disabled")) {
					inputs[a].onchange = Custom.choose;
				} else {
					inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled";
				}
			}
		}
		document.onmouseup = Custom.clear;
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
		} else if(element.checked == true && element.type == "radio") {
			this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
	},
	check: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 0";
			element.checked = false;
		} else {
			if(element.type == "checkbox") {
				this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else {
				this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
				group = this.nextSibling.name;
				inputs = document.getElementsByTagName("input");
				for(a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a] != this.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
					}
				}
			}
			element.checked = true;
		}
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			}
		}
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}
window.onload = Custom.init;

/* Fonctions de validation de formulaire */

function couleur(obj) {
	obj.style.backgroundColor = "#FFFFFF";
}

function verif_date(input)
{
	var separateur = new RegExp("[/-]");
	var date = input.split(separateur);
	var nbJours = new Array('',31,28,31,30,31,30,31,31,30,31,30,31);
	var resultat = true;
	var DateCourante = new Date();

	if ( date['2']%4 == 0 && date['2']%100 > 0 || date['2']%400 == 0 )
	nbJours['2'] = 29;

	if( isNaN(date['2']) )
	resultat=false;

	if ( isNaN(date['1']) || date['1'] > 12 || date['1'] < 1 )
	resultat=false;

	if ( isNaN(date['0']) || date['0'] > nbJours[Math.round(date['1'])] || date['0'] < 1 )
	resultat=false;

	var DateJS = new Date(parseInt(date['2']),parseInt(date['1']),parseInt(date['0']));	
	var dDate = Date.parse(DateJS);
	var dDateCourante = Date.parse(DateCourante);
	if ( dDate < dDateCourante )
	resultat=false;

	return resultat;
}

                
function verif_heure(input){
	var separateur = new RegExp("[:h-]");
	var temps = input.split(separateur);
	var heures = parseInt(temps[0]);
	var minutes = parseInt(temps[1]);
	return  (heures >= 0 && heures < 24) && (minutes >= 0 && minutes < 60);
}

function check() {
	var oblig = "";
	var option = "Avertissement préalable : aucun contrôle n'est fait sur les quantités commandées. Il vous appartient de vérifier qu'elles sont bien sous forme numérique.\n\n";
	var reg_noms = new RegExp("^[a-zéèçàùâêîôûäëïöü\\'\\- ]{2,}$", "i");
	var reg_codpos = new RegExp("^[0-9]{5}$", "i");
	var reg_tel = new RegExp("^0[1-59]([-. ]?[0-9]{2}){4}$", "i");
	var reg_portable = new RegExp("^0[67]([-. ]?[0-9]{2}){4}$", "i");
	var reg_mail = new RegExp("^[a-z0-9-_]+(\.[a-z0-9-_]+)*@[a-z0-9-_]+(\.[a-z0-9-_]+)*\.[a-z0-9-_]{2,}$", "i");
	var reg_nombre = new RegExp("^[0-9]{1,3}$", "i");

	if (!reg_noms.test(document.forms[0].elements["Nom"].value))
		{
		oblig += "\n  - votre nom";
		document.forms[0].elements["Nom"].style.backgroundColor = "#FF8000";
		}
 
	if (!reg_noms.test(document.forms[0].elements["Prenom"].value))
		{
		if (oblig != "") 	oblig += " ;";
		oblig += "\n  - votre prénom";
		document.forms[0].elements["Prenom"].style.backgroundColor = "#FF8000";
		}
 
//	if (!reg_noms.test(document.forms[0].elements["Profession"].value))
//		{
//		if (oblig != "") 	oblig += " ;";
//		oblig += "\n  - le champ Profession n'a pas à être rempli, c'est juste une protection anti-robots";
//		}
 
	if (document.forms[0].elements["Adresse"].value == "")
		{
		if (oblig != "") 	oblig += " ;";
		oblig += "\n  - votre adresse";
		document.forms[0].elements["Adresse"].style.backgroundColor = "#FF8000";
		}
 
	if (!reg_codpos.test(document.forms[0].elements["CodePostal"].value))
		{
		if (oblig != "") 	oblig += " ;";
		oblig += "\n  - votre code postal";
		document.forms[0].elements["CodePostal"].style.backgroundColor = "#FF8000";
		}
 
	if (!reg_noms.test(document.forms[0].elements["Ville"].value))
		{
		if (oblig != "") 	oblig += " ;";
		oblig += "\n  - votre ville";
		document.forms[0].elements["Ville"].style.backgroundColor = "#FF8000";
		}

	if (!reg_tel.test(document.forms[0].elements["Telephone"].value))
		{
		option += "\n  - votre téléphone";
		document.forms[0].elements["Telephone"].style.backgroundColor = "#F3C200";
		}

	if (!reg_tel.test(document.forms[0].elements["Fax"].value))
		{
		if (option != "") option += " ;";
		option += "\n  - votre fax";
		document.forms[0].elements["Fax"].style.backgroundColor = "#F3C200";
		}

	if (!reg_portable.test(document.forms[0].elements["Portable"].value))
		{
		if (option != "")	option += " ;";
		option += "\n  - votre portable";
		document.forms[0].elements["Portable"].style.backgroundColor = "#F3C200";
		}

	if (!reg_mail.test(document.forms[0].elements["Mail"].value))
		{
		if (option != "") option += " ;";
		option += "\n  - votre adresse mail";
		document.forms[0].elements["Mail"].style.backgroundColor = "#F3C200";
		}

	if (document.forms[0].elements["Telephone"].value + document.forms[0].elements["Fax"].value + document.forms[0].elements["Portable"].value + document.forms[0].elements["Mail"].value == "")
		{
		if (oblig != "") 	oblig += " ;";
		oblig += "\n  - un numéro de téléphone, de fax, ou une adresse mail";
		}

	var msg = 0;
	if (oblig != "") msg += 1;
	if (option != "") msg += 2;
	switch(msg) {
		case 0:
			var defmsg = "Tous les champs semblent correctement renseignés.\nConfirmez-vous l'envoi de votre demande de devis ?";
			if (confirm(defmsg)) return(true); else return(false);
			break;			
		case 1:
			oblig = "Vous n'avez pas correctement renseigné les champs obligatoires suivants :\n" + oblig + ".";
			alert(oblig);
			return(false);
			break;
		case 2:
			option = "Nous vous informons qu'un ou plusieurs des champs optionnels suivants n'ont pas été correctement renseignés :\n" + option + ".\n\nVoulez-vous cependant envoyer maintenant votre demande de devis ?";
			if (confirm(option)) return(true); else return(false);
			break;
		case 3:
			oblig = "Vous n'avez pas correctement renseigné les champs obligatoires suivants :\n" + oblig + ".";
			option = "\n\nNous vous informons qu'en outre un ou plusieurs des champs optionnels suivants n'ont pas été correctement renseignés :\n" + option + ".";
			alert(oblig + option);
			return(false);
			break;
		}
}

function checkmail() {
	var manque = "";
	var reg_noms = new RegExp("^[a-zéèçàùâêîôûäëïöü\\'\\- ]{2,}$", "i");
	var reg_mail = new RegExp("^[a-z0-9-_]+(\.[a-z0-9-_]+)*@[a-z0-9-_]+(\.[a-z0-9-_]+)*\.[a-z0-9-_]{2,}$", "i");

	if (!reg_noms.test(document.forms[0].elements["Nom"].value))
		{
		manque += "\n  - votre nom";
		document.forms[0].elements["Nom"].style.backgroundColor = "#FF8000";
		}
 
	if (!reg_mail.test(document.forms[0].elements["Mail"].value))
		{
		if (manque != "") manque += " ;";
		manque += "\n  - votre adresse mail";
		document.forms[0].elements["Mail"].style.backgroundColor = "#FF8000";
		}

	if (!reg_noms.test(document.forms[0].elements["Sujet"].value))
		{
		if (manque != "") manque += " ;";
		manque += "\n  - le sujet de votre courrier";
		document.forms[0].elements["Sujet"].style.backgroundColor = "#FF8000";
		}
 
	if (manque != "")
		{
			manque = "Vous n'avez pas correctement renseigné les champs suivants :\n" + manque + ".";
			alert(manque);
			return(false);
		}
	else
		{
			var defmsg = "Confirmez-vous l'envoi de votre courrier ?";
			if (confirm(defmsg)) return(true); else return(false);
		}		
}

