/**
 *	Utilitaires Javascript
 *	Validations, formats etc
 *	Atos Origin - ALZ
 */

/*
 *	Les trois liens "outils" doivent ouvrir de nouvelles fenêtres
 *	Ajout de réaction sur onclick
 *	Modification des titles
 */
function init_outils() {
	var tabLiensPopup = getAnchorsWithClass('popup');
	for (var i=0; i<tabLiensPopup.length; i++) {
		lien = tabLiensPopup[i];
		titre = lien.getAttribute('title');
		// Onclick
		lien.onclick = function() {
			p = window.open(this.getAttribute('href'), 'P', 'scrollbars=yes,top=50,left=50,width=600,height=500');
			p.focus();
			return false;
		}
		// MAJ title
		lien.setAttribute('title', titre + ' - fenêtre popup');
	}
	
	var tabLiensExternes = getAnchorsWithClass('externe');
	for (var j=0; j<tabLiensExternes.length; j++) {
		lien = tabLiensExternes[j];
		titre = lien.getAttribute('title');
		// Onclick
		lien.onclick = function() {
			window.open(this.getAttribute('href'));
			return false;
		}
		// MAJ title
		lien.setAttribute('title', titre + ' - nouvelle fenêtre ou onglet');
	}
}

function init_nav() {
	// Boutons Retour, Fermer, Valider
	if (document.getElementById('retour')) {
		var r = document.getElementById('retour');
		r.style.display = '';
		r.onclick = function() {
			history.back();
		}
	}
	
	if (document.getElementById('fermer')) {
		var f = document.getElementById('fermer');
		f.style.display = '';
		f.onclick = function() {
			window.close();
		}
	}
}

function init_print() {
	// Bouton d'impression sur les pages concernées (résultats individuels)
	var c = document.getElementById('content');
	if (!c) return;
	
	if (c.className == 'print') {
		// Création du bouton d'impression
		var img = document.createElement('input');
		img.setAttribute('type', 'image');
		img.setAttribute('src', '/publinet/images/bouton_imprimer.gif');
		img.setAttribute('title', 'Imprimer');
		img.setAttribute('alt', 'Imprimer');
		img.setAttribute('class', 'print');
		img.onclick = function() {
			print();
		}
	
		// On l'attache à c
		c.appendChild(img);		
	}
}

function init_anchors() {
	var tabLiens = document.getElementsByTagName('a');
	for (var i=0; i<tabLiens.length; i++) {
		var lien = tabLiens[i];
		if (lien.getAttribute("href") && lien.getAttribute("href").substr(0, 1) == "#") {
			lien.onclick = function() {
				var target = this.getAttribute("href").substring(1);
				document.getElementById(target).setAttribute("tabindex", "0");
				document.getElementById(target).href = "#"+target;
				document.getElementById(target).innerText = ".";
				document.getElementById(target).focus();
			}
		}
	}
}

// L'appel des scripts de la forêt
function addEvent(obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, false);
		return true;
	}
	else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
	else {
		return false;
	}
}

function getAnchorsWithClass(c) {
	if (document.getElementsByClassName) return document.getElementsByClassName(c);
	else {
		var tabLiens = document.getElementsByTagName('a');
		var tab = new Array();
		for (var i=0; i<tabLiens.length; i++) {
			temp = tabLiens[i];
			if (temp.className.indexOf(c) != -1) tab.push(temp);
        }
        return tab;
	}
}


/* Validations formulaires */
function resultat() {
	var numero = document.getElementById('i_numero').value;
	var jj = document.getElementById('i_jj').value;
	var mm = document.getElementById('i_mm').value;
	var aaaa = document.getElementById('i_aaaa').value;

	if (!checkNumCandidat(numero)) return false;
	if (!valDate(mm, jj, aaaa)) {
		alert("La saisie de votre date de naissance est incorrecte.");
		document.getElementById('i_jj').focus();
		return false;
	}
	
	return true;
}

function checkNumCandidat(pNum) {
	if (!checkString(pNum)) {
		alert ("Vous devez saisir votre numéro de candidat.");
		document.getElementById('i_numero').focus();
		return false;
	}
	var numExp = new RegExp("[0-9]{10}");
	var eNumExp = numExp.test(pNum);
	if (!eNumExp) {
		alert("Votre numéro du candidat doit être composé de 10 chiffres.");
		document.getElementById('i_numero').focus();
	}
	return eNumExp;
}

function checkString(entry) {
	for (var i=0; i<entry.length; i++) {
		if (entry.charAt(i) != " ") return true;
  	}
	return false;
}

function valDate(M, D, Y) {
	Months = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	
	Leap = false;
	if ((Y % 4 == 0) && ((Y % 100 != 0) || (Y %400 == 0))) Leap = true;

	// Date invalide
	if((D < 0) || (D > 31) || (M < 0) || (M > 12) || (Y < 0)) return(false);
	if ((D > Months[M-1]) && !((M == 2) && (D > 28))) return(false);
	if (!(Leap) && (M == 2) && (D > 28)) return(false);
	if((Leap)  && (M == 2) && (D > 29)) return(false);

	// Date incohérente
	date_jour = new Date();
	num = 0;
	if (date_jour.getMonth()+1-M < 0) num = 1;
	else if (((date_jour.getMonth()+1)-M)==0 && (date_jour.getDate()-D)<0) num = 1;
	age = date_jour.getFullYear()-Y-num;
	if (age < 18 || age > 65) return false;
	
	// Si tout va bien
	return(true);
}

function checkConcours(concours) {
	// On vérifie juste que l'utilisateur a sélectionné un concours dans la liste déroulante
	var champ = document.getElementById(concours + '_concours');
	if (champ.value == '') {
		alert('Veuillez sélectionner un concours');
		champ.focus();
		return false;
	}
	return true;
}

function afficheDate() {
	date = new Date();
	jour = date.getDate();
	if (jour < 10) jour = '0' + jour;
	mois = date.getMonth() + 1;
	if (mois < 10) mois = '0' + mois;
	document.write('<span id="ddj">Le ' + date.getDate() + '/' + mois + '/' + date.getFullYear() + '</span>');
}


/* Actualités */
function validerActu() {
	if (window.opener && !window.opener.closed) window.opener.document.maj_actualite.submit();
	window.close();
}


addEvent(window, 'load', init_outils);
addEvent(window, 'load', init_nav);
addEvent(window, 'load', init_print);
// Fix liens d'évitement pour Webkit
if (navigator.userAgent.toLowerCase().indexOf('webkit') > -1) addEvent(window, 'load', init_anchors);

