// JavaScript Document
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/////////////////////OPERATION//////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////

// FONCTION POUR L'ASSOCIATION DES COMPTES AUX OPERATIONS
function getXMLHttpRequest() {
	var xhr = null;
	
	if (window.XMLHttpRequest || window.ActiveXObject) {
		if (window.ActiveXObject) {
			try {
				var xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				var xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
		} else {
			var xhr = new XMLHttpRequest(); 
		}
	} else {
		alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
		return null;
	}	
	return xhr;
}

function request(selection,callback,page){
	if(selection.value != "" && selection.value != "0"){
		document.getElementById("id_form_operation").className = "";
		var xhr = getXMLHttpRequest();
		
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
				callback(xhr.responseText);	
				document.getElementById("loader").className="hidden";
				document.getElementById("id_compte_assoc").className="";
			} else if (xhr.readyState < 4) {
				document.getElementById("loader").className="";
				document.getElementById("id_compte_assoc").className="hidden";
			}
		};			
		
		if(page == "ajout"){
			var id_compte_assoc = selection.value;
			xhr.open("GET", "operation_ajout_traitement_ajax.php?id_compte_assoc=" + id_compte_assoc, true);
		}else if(page == "modif"){
			var id_operation_assoc = document.getElementById("id_operation_assoc").value;
			if(selection.value == undefined){
				var id_compte_assoc = document.getElementById(selection).value;
			}else{
				var id_compte_assoc = selection.value;
			}
			xhr.open("GET", "operation_modif_traitement_ajax.php?id_operation_assoc="+id_operation_assoc+"&id_form_assoc_compte=" +id_compte_assoc, true);				
		}
		
		xhr.send(null);
		
	}else{
		document.getElementById("id_tr_assoc_contact").className="back_nom_input hidden";	
	}
}

function readData(sData) {
	document.getElementById("id_tr_assoc_contact").className="back_nom_input";
	document.getElementById("id_compte_assoc").innerHTML = sData;
}

////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
///////////////COMPTE MOTEUR DE RECHERCHE///////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////

var value_resultat = document.createElement("font");
var champ_rechercher_value = 'Rechercher';

function reset_value_form(champ, resultat){
	if(champ.value == ''){
		champ.value = champ_rechercher_value;		
	}
}

function delete_value_form(champ, resultat, page, readData_compte, id_operation, id_recommandation){
	if(champ.value == champ_rechercher_value){
		champ.value = '';
	}else if(champ.value != ''){		
		moteur_recherche(champ, resultat, page, readData_compte, id_operation, id_recommandation);
	}
}


function moteur_recherche(champ, resultat, page, readData_compte, id_operation, id_recommandation){
	var resultat = document.getElementById(resultat);
	resultat.style.display="block";
	if(champ.value != ''){
		request_compte(champ, readData_compte, page, id_operation, id_recommandation);
	}else{
		reset_div_resultat('id_resultat_recherche',interval);
		resultat.innerHTML = "";	
	}
}

function reset_div_resultat(resultat,interval){
	var resultat = document.getElementById(resultat);
	resultat.style.display="none";
	clearTimeout(interval);
}
function affiche_div_resultat(resultat){
	resultat.style.display="block";
}

function request_compte(selection,callback,page,id_operation,id_recommandation) {
		var xhr = getXMLHttpRequest();		
		
		var form_search = selection.value;
		xhr.open("GET", "recherche_traitement_ajax.php?page="+page+"&search="+form_search+"&nawak=" + Math.random() +"&id_operation="+id_operation+"&id_recommandation="+id_recommandation, true);
		xhr.send(null);
		
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)){
				document.getElementById("loader").className="hidden";
				callback(xhr.responseText);	
			} else if (xhr.readyState < 4) {
				document.getElementById("loader").className="hidden";
			}
		};
}

function readData_compte(sData) {
	value_resultat.innerHTML = sData;
	document.getElementById("id_resultat_recherche").appendChild(value_resultat);

	//document.getElementById("id_resultat_recherche").innerHTML = sData;
}
//////////////////////////////////////

/* GESTION DES RECOMMANDATIONS (RAFRAICHISSEMENT AUTO) */ 

function request_recommandation(callback,page,id_operation,id_recommandation,id_assoc_collaborateur,type) {
		var xhr = getXMLHttpRequest();		
		
		xhr.open("GET", "operation_refresh_auto.php?page="+page+"&id_operation="+id_operation+"&id_recommandation="+id_recommandation+"&id_assoc_collaborateur="+id_assoc_collaborateur+"&type="+type+"", true);
		xhr.send(null); 
		
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {				
				callback(xhr.responseText,page);	
			}
		};
}
function readData_recommandation(sData,page) {
	document.getElementById("div_"+page).innerHTML = sData;
}
///////////////////////////////////////

/* GESTION DE LA SELECTION DES CV POUR LES OPERATIONS */
function request_select_cv_op(callback,chek,id_operation,id_recommandation,type){
	var xhr = getXMLHttpRequest();		
	xhr.open("GET", "operation_ajout_cv_traitement_ajax.php?id_operation="+id_operation+"&id_recommandation="+id_recommandation+"&id_chek="+chek+"&type="+type, true);
	xhr.send(null); 
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			callback(xhr.responseText);	
		}
	};
}
function readData_select_cv_op(sData) {
	document.getElementById("affich_cv_reco").innerHTML = sData;
}
///////////////////////////////////////

/* GESTION DU MOTEUR DE RECHERCHE DES CV POUR LES OPERATIONS */ 
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

function request_recherche_cv_op( callback, id_operation, id_recommandation, nb_pole, nb_langue, nb_theme_1, nb_theme_2 ){
	
	if(document.getElementById("id_texte_recherche").value != ""){
		var id_texte_recherche = document.getElementById("id_texte_recherche").value;
	}else{
		var id_texte_recherche = '';
		
	}
	
	var id_checkbox_pole_cv = new Array();
	for(i=0;i<nb_pole;i++){
		if (document.getElementById("id_checkbox_pole_cv"+i).checked != false){
			id_checkbox_pole_cv.push(document.getElementById("id_checkbox_pole_cv"+i).value);
		}
	}
	var id_checkbox_langue_cv = new Array();
	for(i=0;i<nb_langue;i++){
		if (document.getElementById("id_checkbox_langue_cv"+i).checked != false){
			id_checkbox_langue_cv.push(document.getElementById("id_checkbox_langue_cv"+i).value);
		}
	}
	
	var id_theme_cv1 = new Array();
	for(i=0;i<nb_theme_1;i++){
		if (document.getElementById("id_theme_cv1"+i).checked != false){
			id_theme_cv1.push(document.getElementById("id_theme_cv1"+i).value);
		}
	}
	var id_theme_cv2 = new Array();
	for(i=0;i<nb_theme_2;i++){
		if (document.getElementById("id_theme_cv2"+i).checked != false){
			id_theme_cv2.push(document.getElementById("id_theme_cv2"+i).value);
		}
	}
	var id_champ_langue_recherche = document.getElementById("id_champ_langue_recherche").value;
	var id_champ_type_recherche = document.getElementById("id_champ_type_recherche").value;
	
	var xhr = getXMLHttpRequest();
	xhr.open("POST", "recherche_moteur_traitement_ajax.php?id_operation="+id_operation+"&id_recommandation="+id_recommandation, true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	xhr.send("id_texte_recherche="+id_texte_recherche+"&id_checkbox_pole_cv="+id_checkbox_pole_cv+"&id_checkbox_langue_cv="+id_checkbox_langue_cv+"&id_theme_cv1="+id_theme_cv1+"&id_theme_cv2="+id_theme_cv2+"&id_champ_langue_recherche="+id_champ_langue_recherche+"&id_champ_type_recherche="+id_champ_type_recherche);
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			callback(xhr.responseText);	
		}
	};
}
function readData_recherche_cv_op(sData) {
	document.getElementById("div_recherche").innerHTML = sData;
}

///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

function recherche_cv_front(callback, nb_pole, nb_theme_1, nb_theme_2, tips){	

	if(document.getElementById("id_personnalite_recherche") && document.getElementById("id_personnalite_recherche").value != ""){	
		var id_personnalite_recherche = document.getElementById("id_personnalite_recherche").value;	
		id_personnalite_recherche = escape(id_personnalite_recherche);
		window.location.replace("recherche.php?tips="+id_personnalite_recherche);
	}else{
		var id_personnalite_recherche = "";		
	}	
	
	if(tips && tips != ""){	
		var id_personnalite_recherche = tips;	
	}else{
		var id_personnalite_recherche = '';		
	}
	
	if(document.getElementById("id_texte_recherche") && document.getElementById("id_texte_recherche").value != ""){
		var id_texte_recherche = document.getElementById("id_texte_recherche").value;
	}else{
		var id_texte_recherche = '';		
	}
	
	var id_checkbox_pole_cv = new Array();
	for(i=0;i<nb_pole;i++){
		if (document.getElementById("id_checkbox_pole_cv"+i) && document.getElementById("id_checkbox_pole_cv"+i).checked != false){
			id_checkbox_pole_cv.push(document.getElementById("id_checkbox_pole_cv"+i).value);
		}
	}
	
	var id_theme_cv1 = new Array();
	for(i=0;i<nb_theme_1;i++){
		if (document.getElementById("id_theme_cv1"+i) && document.getElementById("id_theme_cv1"+i).checked != false){
			id_theme_cv1.push(document.getElementById("id_theme_cv1"+i).value);
		}
	}

	var id_theme_cv2 = new Array();
	for(i=0;i<nb_theme_2;i++){
		if (document.getElementById("id_theme_cv2"+i) && document.getElementById("id_theme_cv2"+i).checked != false){
			id_theme_cv2.push(document.getElementById("id_theme_cv2"+i).value);
		}
	}
	
	if(document.getElementById("id_express_exact") && document.getElementById("id_express_exact").checked){
		var id_express_exact = document.getElementById("id_express_exact").checked;	
	}else{
		var id_express_exact = '';		
	}
	
	if(document.getElementById("id_champ_type_recherche") && document.getElementById("id_champ_type_recherche").checked){
		var id_champ_type_recherche = document.getElementById("id_champ_type_recherche").checked;	
	}else{
		var id_champ_type_recherche = '';		
	}
	
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// SCRIPT qui limite le nombre de reqûête possible dans le moteur de recherche à trois, la 4ème redirige vers une page!
	var nb_view = new Number;
	if(id_texte_recherche != '' || id_checkbox_pole_cv != '' || id_theme_cv1 != '' || id_theme_cv2 != ''){
		if(!GetCookie("nb1")){
			nb_view = 1;	
			SetCookie("nb1", nb_view , 1);
			//alert(GetCookie("nb1"));
		}else{
			if(!GetCookie("nb2")){
				nb_view = 2;	
				SetCookie("nb2", nb_view , 1);
				//alert(GetCookie("nb2"));
			}else{
				if(!GetCookie("nb3")){
					nb_view = 3;	
					SetCookie("nb3", nb_view , 1);
					//alert(GetCookie("nb3"));
				}else{
					if(!GetCookie("nb4")){
						nb_view = 4;	
						SetCookie("nb4", nb_view , 1);
						window.location.replace("cv_redirection.php");
					}					
				}
			}
		}
	}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	var xhr = getXMLHttpRequest();
	xhr.open("POST", "recherche_traitement.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	xhr.send("id_texte_recherche="+id_texte_recherche+"&id_checkbox_pole_cv="+id_checkbox_pole_cv+"&id_theme_cv1="+id_theme_cv1+"&id_theme_cv2="+id_theme_cv2+"&id_champ_type_recherche="+id_champ_type_recherche+"&id_express_exact="+id_express_exact+"&id_personnalite_recherche="+id_personnalite_recherche);
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			callback(xhr.responseText);	
		}
	};
}
function readData_recherche_cv_front(sData) {
	document.getElementById("conteneur_recherche_form").style.display = "none"; 
	document.getElementById("conteneur_recherche_result").style.display = ""; 
	document.getElementById("div_recherche").innerHTML = sData;
}

function retour_moteur(){
	document.getElementById("conteneur_recherche_form").style.display = ""; 
	document.getElementById("conteneur_recherche_result").style.display = "none"; 	
}

function pagination_moteur(callback, tableau_id, nbr_profil){
	var tableau_id_split = tableau_id.split("_");
	var xhr = getXMLHttpRequest();
	xhr.open("GET", "recherche_traitement_pagination.php?id_cv1="+tableau_id_split[3]+"&id_cv2="+tableau_id_split[4]+"&id_cv3="+tableau_id_split[5]+"&nbr_profil="+nbr_profil, true);
	xhr.send(null);
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			callback(xhr.responseText);	
		}
	};
}
function readData_pagination(sData) {
	document.getElementById("div_recherche").innerHTML = sData;
}

///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

function request_modif_colla(callback,form,id_recommandation,id_operation){
	var form = form.value;
	var xhr = getXMLHttpRequest();		
	xhr.open("GET", "operation_modif_colla_ajax.php?id_colla="+form+"&id_operation="+id_operation+"&id_recommandation="+id_recommandation, true);
	xhr.send(null); 
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			callback(xhr.responseText);	
		}
	};
}
function readData_modif_colla(sData) {
	window.location.replace(sData);
}


function reload_send_media(callback, data){
	var data = data;
	var xhr = getXMLHttpRequest();		
	xhr.open("GET", "ajout_cv_media_ajax.php?data="+data, true);
	xhr.send(null); 
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			callback(xhr.responseText);	
		}
	};
	
}
function readData_send_media(sData) {
	document.getElementById("id_sub_video").disabled = false;
	document.getElementById("div_send_media").innerHTML = sData;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////// GESTION COOKIES ///////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function SetCookie(name, value, days) {
     var expire = new Date ();
     expire.setTime (expire.getTime() + (24 * 60 * 60 * 1000) * days);
     document.cookie = name + "=" + escape(value) + "; expires=" +expire.toGMTString();
}

function GetCookie(name) {
     var startIndex = document.cookie.indexOf(name);
     if (startIndex != -1) {
          var endIndex = document.cookie.indexOf(";", startIndex);
          if (endIndex == -1) endIndex = document.cookie.length;
          return unescape(document.cookie.substring(startIndex+name.length+1, endIndex));
     }
     else {
          return null;
     }
}

function DeleteCookie(name) {
     var expire = new Date ();
     expire.setTime (expire.getTime() - (24 * 60 * 60 * 1000));
     document.cookie = name + "=; expires=" + expire.toGMTString();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////


