var winModalWindow;
var affiche = false;
var justeFermee = false;
var jour;
var mois;
var annee;
var champCourant;

function refresh() {
	window.location.reload();
}

function getX(largeur) {
	width = screen.availWidth;
	return (width - largeur)/2;
}

function getY(hauteur) {
	height = screen.availHeight;
	return (height - hauteur)/2;
}
 
function ignoreEvents(e) {
	return false;
}
 
function showWindow(url, titre, w, h) {
	if (window.showModalDialog) {
		// Internet Explorer
		winModalWindow = window.open(url,titre,'dependent=yes,left=' + getX(w) + ',top=' + getY(h) + ',width=' + w + ',height=' + h + ',status=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes');
	}	
	else {
		// Netscape
		winModalWindow = window.open(url,titre,'dependent=yes,screenX=' + getX(w) + ',screenY=' + getY(h) + ',width=' + w + ',height=' + h + ',status=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes');
	}	
	affiche = true;
}

function newWindow(newContent, name)
{
	winContent = window.open(newContent, name, 'left=165, top=310,width=790,height=500, toolbar=yes,scrollbars=yes, resizable=yes')         
}

function activation() {
	if (affiche) {
		winModalWindow.focus();
	}
	else if (justeFermee) {
		refresh();
	}
}

function bouge(slOrigine, slCible) {
	var tab = new Array();
	var tabSel = new Array();
	var c = 0;
	var cSel = 0;
				
	for (i = 0; i < slOrigine.options.length; i++) {
		if (slOrigine.options[i].selected) {
			if (slOrigine.options[i].className != 'optionindispo') {
				// On ne remt la ressource dans le pool que si elle est dispo encore
				// On ajoute l'option selectionnée à l'autre liste
				slCible.options[slCible.options.length] = new Option(slOrigine.options[i].text,slOrigine.options[i].value);
				// On l'ajoute au tableau des options à sélectionner dans la liste cible
				tabSel[cSel++] = slCible.options[slCible.options.length - 1];
			}	
		}
		else {
			// On l'ajoute au tableau des options non choisies dans la liste origine
			tab[c++] =  new Option(slOrigine.options[i].text,slOrigine.options[i].value);
		}
	}
			
	// On reconstruit la liste d'origine
	slOrigine.selectedIndex = -1;
	slOrigine.options.length = 0;
	for (i = 0; i < tab.length; i++) {
		slOrigine.options[i] = tab[i];
	}
				
	slCible.selectedIndex = -1;
	// On sélectionne les options qui ont été ajoutées
	for (i = 0; i < tabSel.length; i++) {
		tabSel[i].selected = true;	
	}
}
 
function selectionne(sl) {
	// On sélectionne toutes les options affectées pour bien les transmettre 
 	var opt = sl.options;
 	for (i=0; i<opt.length; i++) {
		opt[i].selected = true;
 	}
 	return true;
}

function showCalendrier(champ) {
	// Affiche le calendrier pour le champ
	champCourant = champ;
	i = champ.value.indexOf('/');
	l = champ.value.lastIndexOf('/');
	
	if (i == -1 || l == -1) {
		aujourdhui = new Date();
		jour = aujourdhui.getDay();
		mois = aujourdhui.getMonth();
		annee = aujourdhui.getYear();
	}
	else {
		jour = eval(champ.value.substring(0,i));
		mois = eval(champ.value.substring(i+1,l)) - 1;
		annee = eval(champ.value.substring(l+1,l+5)); 
	}	
	showWindow('../site/html/calendrier.html','calendrier',285,160);
}

function refreshDate() {
	affiche = false;
	champCourant.value = jour + '/' + (eval(mois) + 1) + '/' + annee;
}

function contientDate(champ) {
	d = champ.value;
    d = _trim(d);
    if (d == '') {
    	return false;
    }
    e = new RegExp("^[0-9]{1,2}\/[0-9]{1,2}\/([0-9]{2}|[0-9]{4})$");
    if (!e.test(d)) {
    // On teste l'expression regulière pour valider la forme de la date
        return false; // Si pas bon, retourne faux
    }    

    // separation de la date en 3 variables pour verification
    j = parseInt(d.split("/")[0],10); // jour
    m = (d.split("/")[1]); // mois
    a = parseInt(d.split("/")[2],10); // annee

	if (m.charAt(0) == 0) {
 		m = m.charAt(1);
    }
    m = parseInt(m, 10);
    // Si l'annee n'est composee que de 2 chiffres on complète automatiquement
    if (a < 1000) {
        if (a < 89) {
        	a += 2000; // Si a < 89 alors on ajoute 2000 sinon on ajoute 1900
        }
        else {
        	a += 1900;
        }	
    }

    // Definition du dernier jour de février
    // Annee bissextile si annnee divisible par 4 et que ce n'est pas un siècle, ou bien si divisible par 400
    if (a % 4 == 0 && a % 100 !=0 || a % 400 == 0) {
    	fev = 29;
    }
    else { 
    	fev = 28;
    }

    // Nombre de jours pour chaque mois
    nbJours = new Array(31,fev,31,30,31,30,31,31,30,31,30,31);

    // Enfin, retourne vrai si le jour est bien entre 1 et le bon nombre de jours, idem pour les mois, sinon retourn faux
    if ( m >= 1 && m <=12 && j >= 1 && j <= nbJours[m-1]) {
    	champ.value = j + '/' + m + '/' + a;
    	return true;
    }
    else {
    	return false;
    }
}

function compareDate(dateinf,datesup) {

	if (!(contientDate(dateinf)) || !(contientDate(datesup)))  {
		return false;
	}
	
	d1 = dateinf.value;
	d2 = datesup.value;	
			   
    // separation de la date en 3 variables pour verification
    j1 = parseInt(d1.split("/")[0],10); // jour
    m1 = (d1.split("/")[1]); // mois
    a1 = parseInt(d1.split("/")[2],10); // annee

    // separation de la date en 3 variables pour verification
    j2 = parseInt(d2.split("/")[0],10); // jour
    m2 = (d2.split("/")[1]); // mois
    a2 = parseInt(d2.split("/")[2],10); // annee
    
    d1 = a1 + m1 + j1;
    d2 = a2 + m2 + j2; 
    
    return (d1 <= d2);   

}

function _trim(str) {
	str = str.replace(/^\s*|\s*$/g, "");
	return str ;
}
