// ***** Tests pour champs obligatoires ******

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
	
	return true
}


function isValidEmail(str) {

   return (str.indexOf(".") > 0) && (str.indexOf("@") > 0);
}




function check_radio(formulaire, radio_b) {
	for (i = 0; i < document.forms[formulaire].elements[radio_b].length; i++){
			if (document.forms[formulaire].elements[radio_b][i].checked)
				return true;
	}
	return false;
}

function check_checkbox(formulaire, checkbox_b, nb) {	
	for (i = 1; i <= nb; i++){
		if (typeof(document.forms[formulaire].elements[checkbox_b+"_"+i]) != "undefined"){
			if (document.forms[formulaire].elements[checkbox_b+"_"+i].checked)
				return true;
		}
	}
	return false;
}

	
function check_checkbox2(checkbox_name) {

	var checkboxes = document.getElementsByName(checkbox_name);

	for (i = 0; i < checkboxes.length; i++) {
 	    if (checkboxes[i].checked){ return true; }
	}
	return false;
}
	

// ***** aide sur un input text ******
// usage : 
//<input type="text" id="nationalite" name="nationalite" value="{$projet[0].nationalite}">
//<select id="nationalite_aide" class="aide" name="nationalite_aide" onChange="formulaire_aide('nationalite',this);">
function formulaire_aide(champ, champ_aide) {

	document.getElementById(champ).value = champ_aide.options[champ_aide.selectedIndex].value;
	
}







// ***** champs obligatoires ******



/* ETUDIANT */

function formcomptecreationetudiant(){

	//remise a zero des classname
	document.getElementById('forms_check_alertes').className = "alerte";

	texte_alerte = '';
	
	// tests de validites
	if(!document.creationcompte.civilite.options[document.creationcompte.civilite.selectedIndex].value){
		texte_alerte += "- Civilité<br />";
	}
	if(!document.creationcompte.nom.value){
		texte_alerte += "- Nom<br />";
	}
	if(!document.creationcompte.prenom.value){
		texte_alerte += "- Prénom<br />";
	}
	
	if(!document.creationcompte.email.value){
		texte_alerte += "- Email<br />";
	}else{
		if(isValidEmail(document.creationcompte.email.value)==false){
			texte_alerte += "- Email (email non valide !)<br />";
		}
	}
	if(!document.creationcompte.email_verif.value){
		texte_alerte += "- Email (verification)<br />";
	}else{
		if(isValidEmail(document.creationcompte.email_verif.value)==false){
			texte_alerte += "- Email (verification)<br />";
		}
	}
	
	if(!document.creationcompte.motdepasse.value){
		texte_alerte += "- Mot de passe<br />";
	}else{
		if(document.creationcompte.motdepasse.value.length < 4){
			texte_alerte += "- Mot de passe trop court<br />";
		}
	}
	if(!document.creationcompte.motdepasse_verif.value){
		texte_alerte += "- Mot de passe (vérification)<br />";
	}
	
	if(!document.creationcompte.etudiant_cursus.value){
		texte_alerte += "- Formation en cours<br />";
	}
	if(!document.creationcompte.etudiant_ecole.value){
		texte_alerte += "- Ecole<br />";
	}
	
	if(!document.creationcompte.user_code.value){
		texte_alerte += "- Code de vérification<br />";
	}
	
	
	if(document.creationcompte.email.value != document.creationcompte.email_verif.value){
		texte_alerte += "<b>Attention</b> : l'adresse email et celle de verification doivent être identiques<br />";
	}
	if(document.creationcompte.motdepasse.value != document.creationcompte.motdepasse_verif.value){
		texte_alerte += "<b>Attention</b> : le mot de passe et celui de verification doivent être identiques<br />";
	}
	
	
	// message d'alerte
	if(texte_alerte){
	
		texte_alerte = "<b>Champs obligatoires :</b> <br />"+texte_alerte;
		document.getElementById('forms_check_alertes').innerHTML = texte_alerte;
		document.getElementById('forms_check_alertes').className = "alerteon";
		//alert(texte_alerte);
	
		return false;
	
	}else{
		
		return true;
		
	}

}

// modification compte etudiant
function formcomptemodificationetudiant(){

	//remise a zero des classname
	document.getElementById('forms_check_alertes').className = "alerte";

	texte_alerte = '';
	
	// tests de validites
	if(!document.modificationcompte.civilite.options[document.creationcompte.civilite.selectedIndex].value){
		texte_alerte += "- Civilité<br />";
	}
	if(!document.modificationcompte.nom.value){
		texte_alerte += "- Nom<br />";
	}
	if(!document.modificationcompte.prenom.value){
		texte_alerte += "- Prénom<br />";
	}
	
	if(!document.modificationcompte.etudiant_cursus.value){
		texte_alerte += "- Formation en cours<br />";
	}
	if(!document.modificationcompte.etudiant_ecole.value){
		texte_alerte += "- Ecole<br />";
	}
	
	
	// message d'alerte
	if(texte_alerte){
	
		texte_alerte = "<b>Champs obligatoires :</b> <br />"+texte_alerte;
		document.getElementById('forms_check_alertes').innerHTML = texte_alerte;
		document.getElementById('forms_check_alertes').className = "alerteon";
		//alert(texte_alerte);
	
		return false;
	
	}else{
		
		return true;
		
	}

}



/* ENTREPRISE */

// creation compte entreprise
function formcomptecreationentreprise(){

	//remise a zero des classname
	document.getElementById('forms_check_alertes').className = "alerte";

	texte_alerte = '';
	
	
	if(!document.creationcompte.entreprise_rs.value){
		texte_alerte += "- Raison sociale<br />";
	}
	if(!document.creationcompte.entreprise_enseigne.value){
		texte_alerte += "- Nom d'enseigne<br />";
	}
	if(!document.creationcompte.entreprise_adresse1.value){
		texte_alerte += "- Adresse<br />";
	}
	if(!document.creationcompte.entreprise_cp.value){
		texte_alerte += "- Code Postal<br />";
	}
	if(!document.creationcompte.entreprise_ville.value){
		texte_alerte += "- Ville<br />";
	}
	if(!document.creationcompte.entreprise_tel.value){
		texte_alerte += "- Téléphone<br />";
	}
	
	
	
	// tests de validites
	if(!document.creationcompte.civilite.options[document.creationcompte.civilite.selectedIndex].value){
		texte_alerte += "- Civilité<br />";
	}
	if(!document.creationcompte.nom.value){
		texte_alerte += "- Nom<br />";
	}
	if(!document.creationcompte.prenom.value){
		texte_alerte += "- Prénom<br />";
	}
	
	if(!document.creationcompte.email.value){
		texte_alerte += "- Email<br />";
	}else{
		if(isValidEmail(document.creationcompte.email.value)==false){
			texte_alerte += "- Email (email non valide !)<br />";
		}
	}
	if(!document.creationcompte.email_verif.value){
		texte_alerte += "- Email (verification)<br />";
	}else{
		if(isValidEmail(document.creationcompte.email_verif.value)==false){
			texte_alerte += "- Email (verification)<br />";
		}
	}
	
	if(!document.creationcompte.motdepasse.value){
		texte_alerte += "- Mot de passe<br />";
	}else{
		if(document.creationcompte.motdepasse.value.length < 4){
			texte_alerte += "- Mot de passe trop court<br />";
		}
	}
	if(!document.creationcompte.motdepasse_verif.value){
		texte_alerte += "- Mot de passe (vérification)<br />";
	}
	
	
	if(!document.creationcompte.user_code.value){
		texte_alerte += "- Code de vérification<br />";
	}
	
	
	if(document.creationcompte.email.value != document.creationcompte.email_verif.value){
		texte_alerte += "<b>Attention</b> : l'adresse email et celle de verification doivent être identiques<br />";
	}
	if(document.creationcompte.motdepasse.value != document.creationcompte.motdepasse_verif.value){
		texte_alerte += "<b>Attention</b> : le mot de passe et celui de verification doivent être identiques<br />";
	}
	
	
	// message d'alerte
	if(texte_alerte){
	
		texte_alerte = "<b>Champs obligatoires :</b> <br />"+texte_alerte;
		document.getElementById('forms_check_alertes').innerHTML = texte_alerte;
		document.getElementById('forms_check_alertes').className = "alerteon";
		//alert(texte_alerte);
	
		return false;
	
	}else{
		
		return true;
		
	}

}



function formcomptemodificationentreprise(){


	//remise a zero des classname
	document.getElementById('forms_check_alertes').className = "alerte";

	texte_alerte = '';
	
	
	if(!document.modificationcompte.entreprise_rs.value){
		texte_alerte += "- Raison sociale<br />";
	}
	if(!document.modificationcompte.entreprise_enseigne.value){
		texte_alerte += "- Nom d'enseigne<br />";
	}
	if(!document.modificationcompte.entreprise_adresse1.value){
		texte_alerte += "- Adresse<br />";
	}
	if(!document.modificationcompte.entreprise_cp.value){
		texte_alerte += "- Code Postal<br />";
	}
	if(!document.modificationcompte.entreprise_ville.value){
		texte_alerte += "- Ville<br />";
	}
	if(!document.modificationcompte.entreprise_tel.value){
		texte_alerte += "- Téléphone<br />";
	}
	
	
	// tests de validites
	if(!document.modificationcompte.civilite.options[document.modificationcompte.civilite.selectedIndex].value){
		texte_alerte += "- Civilité<br />";
	}
	if(!document.modificationcompte.nom.value){
		texte_alerte += "- Nom<br />";
	}
	if(!document.modificationcompte.prenom.value){
		texte_alerte += "- Prénom<br />";
	}

	

	// message d'alerte
	if(texte_alerte){
	
		texte_alerte = "<b>Champs obligatoires :</b> <br />"+texte_alerte;
		document.getElementById('forms_check_alertes').innerHTML = texte_alerte;
		document.getElementById('forms_check_alertes').className = "alerteon";
		//alert(texte_alerte);
	
		return false;
	
	}else{
		
		return true;
		
	}
}





/* ECOLE */

// creation compte entreprise
function formcomptecreationecole(){

	//remise a zero des classname
	document.getElementById('forms_check_alertes').className = "alerte";

	texte_alerte = '';
	

	if(!document.creationcompte.entreprise_rs.value){
		texte_alerte += "- Nom de l'école<br />";
	}
	if(!document.creationcompte.entreprise_adresse1.value){
		texte_alerte += "- Adresse<br />";
	}
	if(!document.creationcompte.entreprise_cp.value){
		texte_alerte += "- Code Postal<br />";
	}
	if(!document.creationcompte.entreprise_ville.value){
		texte_alerte += "- Ville<br />";
	}
	if(!document.creationcompte.entreprise_tel.value){
		texte_alerte += "- Téléphone<br />";
	}
	
	
	
	// tests de validites
	if(!document.creationcompte.civilite.options[document.creationcompte.civilite.selectedIndex].value){
		texte_alerte += "- Civilité<br />";
	}
	if(!document.creationcompte.nom.value){
		texte_alerte += "- Nom<br />";
	}
	if(!document.creationcompte.prenom.value){
		texte_alerte += "- Prénom<br />";
	}
	
	if(!document.creationcompte.email.value){
		texte_alerte += "- Email<br />";
	}else{
		if(isValidEmail(document.creationcompte.email.value)==false){
			texte_alerte += "- Email (email non valide !)<br />";
		}
	}
	if(!document.creationcompte.email_verif.value){
		texte_alerte += "- Email (verification)<br />";
	}else{
		if(isValidEmail(document.creationcompte.email_verif.value)==false){
			texte_alerte += "- Email (verification)<br />";
		}
	}
	
	if(!document.creationcompte.motdepasse.value){
		texte_alerte += "- Mot de passe<br />";
	}else{
		if(document.creationcompte.motdepasse.value.length < 4){
			texte_alerte += "- Mot de passe trop court<br />";
		}
	}
	if(!document.creationcompte.motdepasse_verif.value){
		texte_alerte += "- Mot de passe (vérification)<br />";
	}
	
	
	if(!document.creationcompte.user_code.value){
		texte_alerte += "- Code de vérification<br />";
	}
	
	
	if(document.creationcompte.email.value != document.creationcompte.email_verif.value){
		texte_alerte += "<b>Attention</b> : l'adresse email et celle de verification doivent être identiques<br />";
	}
	if(document.creationcompte.motdepasse.value != document.creationcompte.motdepasse_verif.value){
		texte_alerte += "<b>Attention</b> : le mot de passe et celui de verification doivent être identiques<br />";
	}
	
	
	// message d'alerte
	if(texte_alerte){
	
		texte_alerte = "<b>Champs obligatoires :</b> <br />"+texte_alerte;
		document.getElementById('forms_check_alertes').innerHTML = texte_alerte;
		document.getElementById('forms_check_alertes').className = "alerteon";
		//alert(texte_alerte);
	
		return false;
	
	}else{
		
		return true;
		
	}

}


function formcomptemodificationecole(){

	//remise a zero des classname
	document.getElementById('forms_check_alertes').className = "alerte";

	texte_alerte = '';
	

	if(!document.modificationcompte.entreprise_rs.value){
		texte_alerte += "- Nom de l'école<br />";
	}
	if(!document.modificationcompte.entreprise_adresse1.value){
		texte_alerte += "- Adresse<br />";
	}
	if(!document.modificationcompte.entreprise_cp.value){
		texte_alerte += "- Code Postal<br />";
	}
	if(!document.modificationcompte.entreprise_ville.value){
		texte_alerte += "- Ville<br />";
	}
	if(!document.modificationcompte.entreprise_tel.value){
		texte_alerte += "- Téléphone<br />";
	}

	
	// tests de validites
	if(!document.modificationcompte.civilite.options[document.modificationcompte.civilite.selectedIndex].value){
		texte_alerte += "- Civilité<br />";
	}
	if(!document.modificationcompte.nom.value){
		texte_alerte += "- Nom<br />";
	}
	if(!document.modificationcompte.prenom.value){
		texte_alerte += "- Prénom<br />";
	}

	

	// message d'alerte
	if(texte_alerte){
	
		texte_alerte = "<b>Champs obligatoires :</b> <br />"+texte_alerte;
		document.getElementById('forms_check_alertes').innerHTML = texte_alerte;
		document.getElementById('forms_check_alertes').className = "alerteon";
		//alert(texte_alerte);
	
		return false;
	
	}else{
		
		return true;
		
	}
}







function check_offre(type){

	//remise a zero des classname
	document.getElementById('forms_check_alertes').className = "alerte";

	texte_alerte = '';
	
	
	if(!document.form_offre.intitule.value){
		texte_alerte += "- Intitulé de l'offre<br />";
	}

	
	if(type == "Entreprise"){
		if(!check_checkbox2("secteurs[]")){
			texte_alerte += "- Au moins un secteur d'activité<br />";
		}
	}
	
	if(type == "Entreprise"){
		if(!document.form_offre.fonction_id.options[document.form_offre.fonction_id.selectedIndex].value){
			texte_alerte += "- Fonction<br />";
		}
	}
	
	if(type == "Entreprise" || type == "Etudiant" || type == "Ecole"){
		if(!document.form_offre.niveau_id.options[document.form_offre.niveau_id.selectedIndex].value){
			texte_alerte += "- Niveau d'étude<br />";
		}
	}
	
	if(type == "Entreprise"){
		if(!document.form_offre.lieu.value){
			texte_alerte += "- Lieu<br />";
		}
	}
	
	if(type == "Etudiant" || type == "Ecole"){
		if(!document.form_offre.duree.value){
			texte_alerte += "- Durée du stage<br />";
		}
		if(!document.form_offre.periode_debut.value){
			texte_alerte += "- Période (début)<br />";
		}
		if(!document.form_offre.periode_fin.value){
			texte_alerte += "- Période (fin)<br />";
		}
	}
	
	if(!document.form_offre.description.value){
		texte_alerte += "- Description<br />";
	}


	if(type == "Entreprise"){
		if(!document.form_offre.entreprise_rs.value){
			texte_alerte += "- Raison sociale<br />";
		}
		if(!document.form_offre.entreprise_enseigne.value){
			texte_alerte += "- Nom d'enseigne<br />";
		}
		if(!document.form_offre.entreprise_adresse1.value){
			texte_alerte += "- Adresse<br />";
		}
		if(!document.form_offre.entreprise_cp.value){
			texte_alerte += "- Code Postal<br />";
		}
		if(!document.form_offre.entreprise_ville.value){
			texte_alerte += "- Ville<br />";
		}
		if(!document.form_offre.entreprise_tel.value){
			texte_alerte += "- Téléphone<br />";
		}
	}
	
	if(type == "Etudiant" || type == "Ecole"){
		if(!document.form_offre.etudiant_cursus.value){
			texte_alerte += "- Votre Cursus<br />";
		}
		if(!document.form_offre.etudiant_ecole.value){
			texte_alerte += "- Votre Ecole<br />";
		}
	}
	
	if(type == "Entreprise"){
		if(!document.form_offre.contact_nom.value){
			texte_alerte += "- Contact : nom<br />";
		}
		if(!document.form_offre.contact_prenom.value){
			texte_alerte += "- Contact : prénom<br />";
		}
	}
		
	// message d'alerte
	if(texte_alerte){
	
		texte_alerte = "<b>Champs obligatoires :</b> <br />"+texte_alerte;
		document.getElementById('forms_check_alertes').innerHTML = texte_alerte;
		document.getElementById('forms_check_alertes').className = "alerteon";
		window.location = "#messages";
		//alert(texte_alerte);
	
		return false;
	
	}else{
		
		return true;
		
	}
}





function check_stage(){

	//remise a zero des classname
	document.getElementById('forms_check_alertes').className = "alerte";

	texte_alerte = '';
	
	
	if(!document.form_stage.diplome.value){
		texte_alerte += "- Diplôme préparé<br />";
	}
	if(!document.form_stage.intitule_formation.value){
		texte_alerte += "- Intitule de la formation<br />";
	}
	if(!document.form_stage.periode.value){
		texte_alerte += "- Période du stage<br />";
	}
	if(!document.form_stage.duree.value){
		texte_alerte += "- Durée du stage<br />";
	}
		
	// message d'alerte
	if(texte_alerte){
	
		texte_alerte = "<b>Champs obligatoires :</b> <br />"+texte_alerte;
		document.getElementById('forms_check_alertes').innerHTML = texte_alerte;
		document.getElementById('forms_check_alertes').className = "alerteon";
		//alert(texte_alerte);
	
		return false;
	
	}else{
		
		return true;
		
	}
}




function check_depublier(){

	texte_alerte = '';
	
	if(!document.susp_offre.motif_fin_publication.value){
		texte_alerte += "Merci d'indiquer la raison de la dépublication.";
	}
		
	// message d'alerte
	if(texte_alerte){
	
		alert(texte_alerte);
	
		return false;
	
	}else{
		
		return true;
		
	}
}


function supp_stage(id_stage){
	
	if(id_stage){
		var answer = confirm("Etes vous certain de vouloir supprimer ce stage ?");
		if (answer){
			window.location = "compte.php?action=supp_stage&id_stage="+id_stage;
		}
	}
}




function formenvoyermessage(){
	
	if(!document.form_message.intitule.value || !document.form_message.description.value){
		alert("Sujet et texte sont obligatoires !");
		return false;
	}else{
		return true;
	}

}

