var tab_matiere	=	new Array();
var tab_mat_tmp	=	new Array();
var tab_matnom_tmp	=	new Array();
var tab_matnom	=	new Array();

var tab_niveau	=	new Array();
var tab_niv_tmp	=	new Array();
var tab_nivnom_tmp	=	new Array();
var tab_nivnom	=	new Array();

var tab_final='';
var tab_final_nom='';
var tab_final_nom2='';
var tab_t1='';

function _preciser(e){
if(e==1)
document.getElementById('preciser').innerHTML='<input type=\'text\' name=\'postuler_precision\' size=\'11\'> Préciser<br /><br />';
else
document.getElementById('preciser').innerHTML='';
}

function _preciser_2(e){
if(e==1)
document.getElementById('preciser_2').innerHTML='<input type=\'text\' name=\'postuler_precision_niveau\' size=\'11\'> Préciser<br /><br />';
else
document.getElementById('preciser_2').innerHTML='';
}

function clear_com(){
var comm = document.postuler.niveau_commentaire.value;
if(comm=="Expliquez brièvement votre parcours, expérience dans l'enseignement...")
document.postuler.niveau_commentaire.value="";
}
function suppr_matiere(matiere, niveau){
el = document.postuler; 
longueur	=	tab_matiere.length;
verif=false;
y=1;

	while(verif==false) {
	tmp_matiere	=	tab_matiere.pop();
	tmp_niveau	=	tab_niveau.pop();
	tmp_matnom	=	tab_matnom.pop();
	tmp_nivnom	=	tab_nivnom.pop();
	
		if((tmp_matiere==matiere && tmp_niveau==niveau) || y>longueur){
		tab_matiere	=	tab_matiere.concat(tab_mat_tmp);
		tab_niveau	=	tab_niveau.concat(tab_niv_tmp);
		tab_matnom	=	tab_matnom.concat(tab_matnom_tmp);
		tab_nivnom	=	tab_nivnom.concat(tab_nivnom_tmp);
		verif=true;
		} else {
		tab_mat_tmp.push(tmp_matiere);
		tab_niv_tmp.push(tmp_niveau);
		tab_matnom_tmp.push(tmp_matnom);
		tab_nivnom_tmp.push(tmp_nivnom);
		}
		y++;
		
	}
	i=0;
	while(i<tab_matiere.length) {
	tab_final=tab_final+tab_matiere[i]+'_'+tab_niveau[i]+'|';
	tab_final_nom	=	tab_final_nom+tab_matnom[i]+' - '+tab_nivnom[i]+' | \n';
	tab_final_nom2	=	tab_final_nom2+tab_matnom[i]+' - '+tab_nivnom[i]+' | ';
	tab_t1			=	tab_t1+"<input type='button' value='Suppr' onClick='suppr_matiere("+tab_matiere[i]+","+tab_niveau[i]+");'>&nbsp;"+tab_matnom[i]+' - '+tab_nivnom[i]+"&nbsp;<br />";
	i++;
	}
el.id_matiere.value				=	tab_final;
el.selected_matiere.value		=	tab_final_nom;
el.matiere_niveau_text.value	=	tab_final_nom2;
document.getElementById('test_sao').innerHTML=tab_t1;
tab_final		=	'';
tab_final_nom	=	'';
tab_t1			=	'';
tab_mat_tmp		=	new Array();	
tab_niv_tmp		=	new Array();
tab_matnom_tmp	=	new Array();
tab_nivnom_tmp	=	new Array();
}

function add_matiere(){
el = document.postuler; 

matiere_nom = document.getElementById('id_mat').options[el.postuler_matiere.selectedIndex].text; 
niveau_nom 	= document.getElementById('id_niv').options[el.postuler_niveau_matiere.selectedIndex].text; 
matiere		= el.postuler_matiere.value;
niveau 		= el.postuler_niveau_matiere.value;

tab_matiere.push(matiere);
tab_niveau.push(niveau);
tab_matnom.push(matiere_nom);
tab_nivnom.push(niveau_nom);

i=0;
	while(i<tab_matiere.length) {
	tab_final		=	tab_final+tab_matiere[i]+'_'+tab_niveau[i]+'|';
	tab_final_nom	=	tab_final_nom+tab_matnom[i]+' - '+tab_nivnom[i]+' | \n';
	tab_final_nom2	=	tab_final_nom2+tab_matnom[i]+' - '+tab_nivnom[i]+' | ';
	tab_t1			=	tab_t1+"<input type='button' value='Suppr' onClick='suppr_matiere("+tab_matiere[i]+","+tab_niveau[i]+");'>&nbsp;"+tab_matnom[i]+' - '+tab_nivnom[i]+"&nbsp;<br />";
	i++;
	}
el.id_matiere.value=tab_final;
el.selected_matiere.value=tab_final_nom;
el.matiere_niveau_text.value=tab_final_nom2;
document.getElementById('test_sao').innerHTML=tab_t1;
tab_final='';
tab_final_nom='';
tab_final_nom2='';
tab_t1='';
}

function finale() {
var verification=true;
var nom				=	document.postuler.postuler_nom.value;
var nom_length		=	document.postuler.postuler_nom.value.length;
var prenom			=	document.postuler.postuler_prenom.value;
var prenom_length	=	document.postuler.postuler_prenom.value.length;
var telephone		=	document.postuler.postuler_telephone.value;
var telephone_length=	document.postuler.postuler_telephone.value.length;
var file			=	document.postuler.upload_file.value;
var file_length		=	document.postuler.upload_file.value.length;
var matiere			=	document.postuler.selected_matiere.value.length;
var count_number1	=0;

document.getElementById('phrase_tel').innerHTML='';
document.getElementById('phrase_nom').innerHTML='';
document.getElementById('phrase_file').innerHTML='';
document.getElementById('phrase_email').innerHTML='';
document.getElementById('phrase_matiere').innerHTML='';

	if(nom_length<1) {
	document.getElementById('phrase_nom').innerHTML="Vous devez renseignez les champs nom et téléphone.";
	verification=false; }
	
	if(matiere<1) {
	document.getElementById('phrase_matiere').innerHTML="Vous devez ajouter au moins une matiére. Selectionnez une matiére et le niveau auquel vous pouvez l'enseigner, puis cliquez sur ajouter.";
	verification=false; }
	if(telephone_length<10) {
	document.getElementById('phrase_tel').innerHTML="Vous devez renseignez au moins un numero de téléphone valide.";
	verification=false;
	} else {
			for(i=0;i<telephone_length;i++) {
			y=i+1;
			chara=telephone.substring(i,y)
				if(!isNaN(chara))
				count_number1++;
			}
			
		if(count_number1<10 ) {
		verification=false;
		document.getElementById('phrase_tel').innerHTML="Vous devez renseignez au moins un numero de téléphone valide.";
		}
	}
	
	var mail_message = document.postuler.postuler_email.value;
	var mail_message_length = document.postuler.postuler_email.value.length;
	if (mail_message_length>0) {
	    re = /^([a-zA-Z0-9]+(([\.\-\_]?[a-zA-Z0-9]+)+)?)\@(([a-zA-Z0-9]+[\.\-\_])+[a-zA-Z]{2,4})$/gi;
	    result = mail_message.match(re) ;
	    if (result==null) {
		verification=false;
		document.getElementById('phrase_email').innerHTML="Votre email n'est pas valide";
	    } 
	}	
	return verification;
}
