var _infos_ALL = new Object();

function InfosConnexes(a_id, a_options, a_initOnly) {
	this.id = a_id;
  this.options = a_options;
  a_options.requestParameters.push('connexes_id=' + a_id);
  this.ajaxHelper = new net.ContentLoader(this, ROOT_PATH + '/control', "POST", a_options.requestParameters);
 
 	_infos_ALL[a_id] = this;
 	
 	if (!a_initOnly) {
		this.display();
	}
}

InfosConnexes.prototype = {
	display: function() { 
		this.ajaxHelper.sendRequest('_action=show');
	},
	
	modify: function() { 
		this.ajaxHelper.sendRequest('_action=modifier');
	},
	
	close: function() { 
		this.ajaxHelper.sendRequest('_action=annuler');
	},
	
	save: function(a_oldIds, a_newIds, a_callback) { 
		this.callback = a_callback;
		this.ajaxHelper.sendRequest('_action=valider', 'old_ids=' + a_oldIds, 'new_ids=' + a_newIds);
	},
	
	dissocier : function(a_id) {
		this.ajaxHelper.sendRequest('_action=dissocier', 'node_id=' + a_id);
	},

	ajaxUpdate:  function(a_request) {
 		var l_div = xbGetElementById('cont_' + this.id);
 		xbSetInnerHTML(l_div, a_request.responseText); 
 		if (xbGetElementById('contenu')) {
 			l_div.style.display = 'block';
 		}
 		// on regarde si on doit exécuter du script
 		var l_script = xbGetElementById("div_code");
 		if (l_script) {
 			eval(l_script.innerHTML);
 			// on supprime complètement le div du DOM (pour ne pas interférer avec l'appel suivant)
 			l_script.parentNode.removeChild(l_script);
 		}
 		if (this.callback) {
 			this.callback();
 			this.callback = null;
 		}
  },

  handleError: function(request) {
		if (this.options.errorHandler)
			this.options.errorHandler(request);
	}

};


function openConnexesPopup(a_id) {
	_infos_ALL[a_id].modify();
}

/**************************************  FOLDERS / EVTS   ****************************************/
var _oldEvtIds;

function selectConnexesFolders(a_id) {
	_oldEvtIds = checkConnexesFolders(a_id);
}

function saveConnexesFolders(a_id) {
	_infos_ALL[a_id].save(_oldEvtIds, checkConnexesFolders(a_id));
}

function checkConnexesFolders(a_id) {
	var l_selectedIds = '';
	// on récupère la table des folders
	var l_table = xbGetElementById('popupTable');
	for (var r=1, l_nb = l_table.rows.length - 1; r < l_nb; r++) {
		if ((l_table.rows[r].cells.length > 1) && (l_table.rows[r].cells[1].childNodes[0].checked)) {
			l_selectedIds += '|' + l_table.rows[r].cells[2].childNodes[0].value;
		}
	}
	if (l_selectedIds.length > 0) {
		l_selectedIds = l_selectedIds.substring(1);
	}
	
	return l_selectedIds;
}

/**************************************  DOCUMENTS   **********************************************/
function openDocumentsPopup(a_elt, a_id) {
	var l_form = $(a_elt).up('form');
	new Ajax.Updater('popupContenu', l_form.action, {
		parameters : {process: l_form.process.value, key: l_form.key.value, _action: 'modifier', connexes_id: a_id},
		evalScripts: true
	});
}

function saveDocuments(a_id, a_docId) {
	_infos_ALL[a_id].save('', a_docId);
}

function closeDocuments(a_id) {
	_infos_ALL[a_id].close();
	closePopup();
}

function dissocierDocument(a_id, a_img) {
	_infos_ALL[a_id].dissocier(a_img.id.substring(4));
}

/**************************************  LISTES   ************************************************/
function openListesPopup(a_elt, a_id) {
	var l_form = $(a_elt).up('form');
	new Ajax.Updater('popupContenu', l_form.action, {
		parameters : {process: l_form.process.value, key: l_form.key.value, link_id : l_form.link_id.value, 
									_action: 'modifier', connexes_id: a_id},
		evalScripts: true
	});
}

function checkListes() {
  var l_listes = '';
  $$('#popupTable input.checkbox').each(function(a_ck){
    if (a_ck.checked) {
      var l_id = a_ck.id;
      l_id = l_id.substring(0, l_id.length - 3);
      l_listes += $(l_id + 'id').value + '|';
    }
  });
  return l_listes;
}

var _oldListeIds;

function saveConnexesListes(a_id) {
	_infos_ALL[a_id].save(_oldListeIds, checkListes());
	closePopup();
}


/**************************************  PARTAGE   ****************************************/
function saveConnexesPartage(a_id) {
	_infos_ALL[a_id].save('', 'yes');
	closePopup();
}

function reloadPartage() {
	_infos_ALL['neree_connexes'].display();
}

/**************************************  ACCUEIL / NEWSLETTER  *********************************/
function saveNewsletter(a_id) {
	var l_mail = xbGetElementById('mail_news').value;
	if (und(l_mail) || (l_mail == '')) {
		alert('Merci de renseigner votre adresse mail.');
		return;
	}
	_infos_ALL[a_id].save('', l_mail, alertOk);
}

function alertOk() {
	alert(message);
}

/**************************************  ENCADRANTS  *******************************************/
function openEncadrantsPopup(a_elt, a_id) {
	var l_form = $(a_elt).up('form');
	new Ajax.Updater('popupContenu', l_form.action, {
		parameters : {process: l_form.process.value, key: l_form.key.value, 
									link_id : l_form.link_id.value, str_id : l_form.str_id.value, 
									contrat_id : l_form.contrat_id.value, 
									_action: 'modifier', connexes_id: a_id},
		evalScripts: true
	});
}

function saveConnexesEncadrants(a_elt, a_id) {
	var l_basename = a_elt.id; 
	var l_encadrantIds = '|';
	var l_value = $(l_basename + '__cont_tut_id').getValue();
	if (l_value == '') {
		l_value = ' ';
	}
	l_encadrantIds += l_value + '|';
	l_value = $(l_basename + '__cont_maitre_id').getValue();
	if (l_value == '') {
		l_value = ' ';
	}
	l_encadrantIds += l_value + '|';
	l_value = $(l_basename + '__cont_repres_id').getValue();
	if (l_value == '') {
		l_value = ' ';
	}
	l_encadrantIds += l_value + '|';
	
	_infos_ALL[a_id].save('', l_encadrantIds);
	closePopup();
}


