// Javascript pour les pages de dîner à la ferme - Matthey Julien - avril 2009

//if (typeof TSR == 'undefined') TSR = {};
if (typeof TSR.dife == 'undefined') TSR.dife = {
	
	// Attributs
	options: {
		propriety1:null,
		propriety2:null
		// ...
	},
	
	// Fonction init pour afficher les contenus par défaut
	init: function () {
		addToStart (function() {
			TSR.dife.initSubMenus();
		});
	},
	
	// Fonction init pour le(s) submenu(s)
	initSubMenus: function () {
		var withSub1 = document.getElementById('withSub1');
		var subMenu1 = document.getElementById('menuSub1');
		var subMenu1Link = document.getElementById('menuSub1Link');
		
		withSub1.onmouseover = function() {
			subMenu1.style.display = 'block';
			if(subMenu1Link.className.indexOf('active') == -1) subMenu1Link.className ='menuEntryOver';
			else subMenu1Link.className ='menuEntryOver active';
		}
		withSub1.onmouseout = function() {
			subMenu1.style.display = 'none';
			if(subMenu1Link.className.indexOf('active') == -1) subMenu1Link.className = 'menuEntry';
		}
	},
	
	// Fonction pour ajouter un paramètre blank aux liens voulus
	setBlankLinks: function () {
		var links = document.getElementsByTagName('a');
		for(i=0; i < links.length; i++) {
			if (links[i].className.indexOf('_blank') != -1) {
				links[i].target = '_blank';
			}
		}
	},
	
	// Page contact - fonctions du formulaire
	checkFormContact: function () {
		var prenom = document.getElementById('contact-prenom');
		var nom = document.getElementById('contact-nom');
		var email = document.getElementById('contact-email');
		var adresse = document.getElementById('contact-adresse');
		var npa = document.getElementById('contact-npa');
		var localite = document.getElementById('contact-localite');
		var telephone = document.getElementById('contact-telephone');
		var pays = document.getElementById('contact-pays');
		
		if (isEmpty(prenom) || isEmpty(nom) || isEmpty(email) || isEmpty(adresse) || isEmpty(npa) || isEmpty(localite) || isEmpty(telephone) || isEmpty(pays)) {
			this.setError('empty');
			return false;
		} else if (!checkMail(email)) {
			this.setError('email');
			return false;
		} else if (!this.checkNumTel(telephone)) {
			this.setError('telephone');
			return false;
		} else if (!this.checkNpa(npa)) {
			this.setError('npa');
			return false;
		}
		document.mail.submit();
	},
	
	checkNpa: function (npa) {
		npa = npa.value.toString();
		if (npa.length != 4)  {
			return false;
		} else if (!this.isInteger(npa)) {
			return false;
		} else {
			return true;
		}
	},
	
	checkNumTel: function (telephone) {
		telephone = telephone.value.toString();
		telephone = telephone.replace(/ /g, '');
		telephone = telephone.replace(/\//g, '');
		telephone = telephone.replace(/\./g, '');
		return isInteger(telephone) && telephone.length >= 10;
	},
	
	isInteger: function (input) {
		return input == parseInt(input);
	},
	
	setError: function (v) {
		if (!document.getElementById('error')) return false;
		var error = document.getElementById('error');
		//var content = document.getElementById('smallbox');
	
		switch (v) {
			case 'empty' :
				error.innerHTML="Tous les champs sont obligatoires.";
			break;
			case 'email' :
				error.innerHTML="L'adresse e-mail n'est pas valide.";
			break;
			case 'npa' :
				error.innerHTML="Le NPA n'est pas valide.";
			break;
			case 'telephone' :
				error.innerHTML="Le numero de telephone n'est pas valide.";
			break;
			default :
				error.innerHTML="Une erreur inattendue est survenue.";
			break;
		}
		error.style.display='block';
	},
	
	resetForm: function () {
		document.mail.reset();
		var textareas = document.getElementsByTagName('textarea');
		for (i=0; i < textareas.length; i++) {		// <- pour MAC/Safari
			textareas[i].value = '';
		}
		document.getElementById('error').innerHTML = '';
	}
};

// Init
TSR.dife.init();