/*
 * FONCTIONS VT-OOLKIT COMPONENTS
 */

var MOD_RECHERCHE_SEARCH=null;

function init(e) {
	dlgSession = dojo.widget.manager.getWidgetsByType("Dialog")[0];
	
	for(var i=0; i<arrayCW.length;i++){
		var modInfos=arrayCW[i];
		WindowSystem.createWindow('cw_'+modInfos['mod'],'',modInfos['label']);
	}
		
	shortcut("backspace", function(){key_backspace();},{'propagate':true});
	shortcut("escape", function(){key_escape();});
	shortcut("tab", function(e){key_tab(e);},{'propagate':true});
	
	openCWindowMod('home');
}

function reloadIndex(){
	document.location.reload();
}

function showMessage(data){
	
	var obj=$("resultBlockContent");
	NewAC('').inner(unescape(data), "resultBlock", true);
	OPEN_MESSAGE++;
	obj.show();
	setTimeout(function(){hideMessage();},5000);	
}

function hideMessage(){
	var obj=$("resultBlockContent");
	OPEN_MESSAGE--;
	if(OPEN_MESSAGE==0) obj.hide();
}

function closeSession(){
	window.location='?module=Default&action=Logout';
}

function openCwindow(cwindow_, url_, obj_){
	if(cwindow_)
	{
		cwindow_.showExplode(obj_);
		cwindow_.loadGeneralLink(url_);
	}
}

function openCWindowMod(mod, strparam){
	var url_='?module='+mod+'&action=Index';
	if(strparam) url_+="&"+strparam;
	var obj_=document.getElementById("menuButton_"+mod);
	var cwindow_=WindowSystem.getWindowByModuleName(mod);
	openCwindow(cwindow_, url_, obj_);
}

function openModal(url_){
	WindowSystem.getCurrentWindow().openModal(url_);
}

function testDeconnexion(){ }

function alertNoRight(){
	alert("Vous n'avez pas les droits requis pour cette operation ou l'application ne le permet pas.");
}

function dbg(mess_,catched){
	if(typeof(console)!="undefined"){
		console.log(mess_)
	}else{
		if(catched) alert(mess_);
	}
}

function selectLink(linkNode){
	try
	{
		var tr= linkNode.parentNode.parentNode;
		var table=tr.parentNode;
		var arrayTr=table.getElementsByTagName("tr");
	
		//deselection des li
		for(var i=0; i<arrayTr.length; i++){
			arrayTr[i].className="menuLinkOut";
		}
		tr.className="menuLinkSel";
	}catch(er){}
}

function unSelectLink(linkNode){
	var tr= linkNode.parentNode.parentNode;
	tr.className="menuLinkOut";
}

function unSelectLinkTable(domNode){
	var arrayTr = domNode.getElementsByTagName("tr");
	for(var i=0; i<arrayTr.length; i++){
		arrayTr[i].className="menuLinkOut";
	}
}

function unSelectLinkDojoTree(domNode){
	var arraySpan = domNode.getElementsByTagName("span");
	for(var i=0; i<arraySpan.length; i++){
		if(arraySpan[i].className == 'dojoTreeNodeLabelTitle dojoTreeNodeLabelSelected')
			arraySpan[i].className="dojoTreeNodeLabelTitle";
	}
}

function checkIt(htmlTr, htmlRadio,idList,eval_){
	
	if(htmlRadio){
		if(!htmlRadio.checked){
			if(htmlTr.className.indexOf('tuple0')>=0)
				htmlTr.className="tuple0sel";
			else
				htmlTr.className="tuple1sel";
			htmlRadio.checked=true;
		}else{
			htmlRadio.checked=false;
			if(htmlTr.className.indexOf('tuple0')>=0)
				htmlTr.className="tuple0";
			else
				htmlTr.className="tuple1";
		}

		if(eval_!=null) eval(eval_);
	}
}

function selectAll(idlist_, bool_) {

	var checks = document.getElementsByName(idlist_+'_ido[]');

	for(var i=0; i<checks.length; i++) {
			
		checks[i].checked = bool_;
		var tr=checks[i].parentNode.parentNode;
			
		if(!bool_){		
			if(tr.className.indexOf('tuple0')>=0)
				tr.className="tuple0";
			else
				tr.className="tuple1";
		}else{
		
			if(tr.className.indexOf('tuple0')>=0)
				tr.className="tuple0sel";
			else
				tr.className="tuple1sel";
		}
	}
}

function getSelected(idlist_){
	var checks = document.getElementsByName(idlist_+'_ido[]');
	var checkeds = new Array();

	for(var i=0; i<checks.length; i++) {
		if(checks[i].checked){
			checkeds.push(checks[i].value);
		}
	}
	return checkeds;
}

function editSelected(idlist_){

	try{
		var checkeds = getSelected(idlist_);

		if(checkeds.length==1){
			var idSel=checkeds[0];
			return idSel;
		}else{
			throw "Veuillez s???lectionner un seul element!";
		}
	}catch(error){
		alert(error);
	}
	return null;
}

function selectLi(li){
	var ul= li.parentNode;
	var arrayLi=ul.getElementsByTagName("li");

	for(var i=0; i<arrayLi.length; i++){
		arrayLi[i].className="";
	}
	li.className="navlistlink";
}

function Dj_tooltip(objetHTML, caption_){
	
	objetHTML.onMouseOver=null;
	connectId_=objetHTML.id;
	if(!connectId_){
		var rand = Math.round(Math.random()*1000000);
		connectId_='srcTip_'+rand;
		objetHTML.id=connectId_;
	}
	if(!document.getElementById("tooltip_"+connectId_)){
		tooltip=dojo.widget.createWidget('tooltip',{caption:'', connectId:connectId_});
		tooltip.domNode.id="tooltip_"+connectId_;		

		document.body.appendChild(tooltip.domNode);
		inner('test',tooltip.domNode.id);
	}
}

function Dj_tooltipDyn(connectId_, href_){
	tooltip=dojo.widget.createWidget('tooltip',{href:href_, connectId:connectId_});
	tooltip.connectNode.parentNode.appendChild(tooltip.domNode);	
}

function openInputTree(id){
	var idDiv=id+'_div';
	var elHTML=document.getElementById(idDiv);
	if(elHTML.style.display!=''){
		elHTML.style.display='';
		dojo.event.connect(document.body, "onmousedown", function(){closeInputTree(id);});
	}else{ }
}

function closeInputTree(id){
	var idDiv=id+'_div';
	var elHTML=document.getElementById(idDiv);
	elHTML.style.display='none';
	dojo.event.disconnect(document.body, "onmousedown","closeInputTree");
}

function updateEditorFormValue(formId){
 
	for ( i = 0; i < parent.frames.length; i++ ){
		
		try{
			   if (parent.frames[i].FCK)
           		parent.frames[i].FCK.UpdateLinkedField();		    
		}catch(e){ }		
	}  
	
	//CKEditor
	if (formId != '' && typeof(CKEDITOR) != 'undefined') {
		var f = document.getElementById(formId);
		
		for(input in CKEDITOR.instances)
		{
			if(f[input] && f[input].type == 'textarea')
				f[input].value = CKEDITOR.instances[input].getData();
		}
		CKEDITOR.instances = {}; 
	} 	
}

function enableDateValidite(enable,field1,field2)
{	
	if(enable)
	{
		document.getElementById(field1).disabled=false;	
		document.getElementById(field1).style.color='';		
		document.getElementById(field2).disabled=false;		
		document.getElementById(field2).style.color='';		
	}
	else
	{
		document.getElementById(field1).disabled=true;	
		document.getElementById(field1).style.color='grey';	
		document.getElementById(field2).disabled=true;
		document.getElementById(field2).style.color='grey';	
	}
}


/*
 * FONCTIONS MOTEUR DE RECHERCHE
 */

function initSearchForm(modName){
	
	MOD_RECHERCHE_SEARCH=modName;
	openCWindowMod('recherche');	
	initSearchForm2();		
}

function initSearchForm2(){

	var formSearch=document.forms["form_search"];
	
	if(formSearch){
		
		var arrayCheckMod=formSearch.elements["f_module[]"];
		
		for(var i=0; i<arrayCheckMod.length;i++){
		
			var checkMod=arrayCheckMod[i];
			if(checkMod.value==MOD_RECHERCHE_SEARCH || MOD_RECHERCHE_SEARCH==null) checkMod.checked=true;
			else checkMod.checked=false;			
			eval(checkMod.getAttribute('onchange'));			
		}	
						
		MOD_RECHERCHE_SEARCH=null;
	}
}

/*
 * FONCTIONS RACCOURCIS CLAVIER
 */

function key_escape(){
        var closemod=true;
	if(VTAgendaPopup){
         if(VTAgendaPopup.isOpen(WindowSystem.getCurrentWindow().id)){
                VTAgendaPopup.maskPopup(VTAgendaPopup.getIdPopupOpen(WindowSystem.getCurrentWindow().id), true);
                closemod=false;
         }
        }
        if(closemod){
	var currentWindow=WindowSystem.getCurrentWindow();
	currentWindow.closeModal();
        }
}

function key_backspace(){
	
	var currentWindow=WindowSystem.getCurrentWindow();
	var callback=currentWindow.getShortCut('backspace');
	
	if(callback && !currentWindow.isOpenedModale()){
		
		callback();		
		
		e.cancelBubble = true;
		e.returnValue = false;
				
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
		return false;
	}
}	

function key_tab(e){
	
	var e = e || window.event;	
	var currentT=e.target;
	
	try{	
		if(currentT!='undefined' && currentT.tagName == "HTML"){
			
			displayMenu();
			
			e.cancelBubble = true;
			e.returnValue = false;
					
			if (e.stopPropagation) {
				e.stopPropagation();
				e.preventDefault();
			}
			return false;
		}
	}catch(er){}
}

function displayMenu(){

	var divMenu=document.getElementById("modBox");
	var divWindow=document.getElementById("contenercwsGbl");
	var divController=document.getElementById("menuController");
	
	var divOpenWidth=144;
	var divDemiWidth=42;
	var divCloseWidth=0;
	var marginWidth=6;
	
	if(divMenu.style.display=="none"){
		
		divMenu.style.display="";
		divController.style.borderRight="";
		divWindow.style.marginLeft=(divOpenWidth+marginWidth)+"px";
		divMenu.style.width=divOpenWidth+"px";
		
		changecss(".WindowTop", "left", (divOpenWidth+marginWidth)+"px");
		
		var arrayEls=document.getElementsByName("menuButton_label");
		for(var i=0; i<arrayEls.length; i++){
			var iEl=arrayEls[i];
			iEl.style.display='';
		}
		
		var arrayElGrps=document.getElementsByName("menuButton_grp");
		for(var i=0; i<arrayElGrps.length; i++){
			var iEl=arrayElGrps[i];
			iEl.style.display='';
		}
	
	}else	
	if(divMenu.style.width==(divDemiWidth+"px")){	
		
		divMenu.style.display="none";
		divController.style.borderRight="1px solid #CCCCCC";
		divWindow.style.marginLeft=marginWidth+"px";
		changecss(".WindowTop", "left", marginWidth+"px");
	}else{
	
		var arrayEls=document.getElementsByName("menuButton_label");
		
		for(var i=0; i<arrayEls.length; i++){
			var iEl=arrayEls[i];
			iEl.style.display='none';
		}
		
		var arrayElGrps=document.getElementsByName("menuButton_grp");
		for(var i=0; i<arrayElGrps.length; i++){
			var iEl=arrayElGrps[i];
			iEl.style.display='none';
		}
		
		divMenu.style.width=divDemiWidth+"px";
		divWindow.style.marginLeft=divDemiWidth+marginWidth+"px";
		
		
		
		changecss(".WindowTop", "left", divDemiWidth+marginWidth+"px");
		
	}	
}

/*
 * Divers
 */
function changecss(theClass,element,value) {
	//documentation for this script at http://www.shawnolson.net/a/503/
	 var cssRules;
	 if (document.all) {
	  cssRules = 'rules';
	 }
	 else if (document.getElementById) {
	  cssRules = 'cssRules';
	 }
	 for (var S = 0; S < document.styleSheets.length; S++){
	  for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
	   if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
	    document.styleSheets[S][cssRules][R].style[element] = value;
	   }
	  }
	 }	
	}
function popUp(url, titre, larg, haut){
	mapopup = window.open(url,titre,'width='+larg+',height='+haut+', resizable=no, status=no, directories=no, scrollbars=yes');
	hauteur = screen.availHeight-haut;
	largeur = screen.availWidth-larg;
	pointx = (largeur) / 2; //largeur = mapopup.width?
	pointy = (hauteur) / 2;
	mapopup.moveTo(pointx,pointy);
}

function byId(id_){
	return document.getElementById(id_);
}

/*
 * FONCTIONS POS TAILLE ELEMENT -> a remplacer par prototype
 */

function heightY(obj_){
	return obj_.offsetHeight;
}

function setHeight(node_, value_){
	node_.style.height=value_+"px";
}

function setWidth(node_,value_){
	node_.style.width=value_+"px";
}

function widthX(obj_){
	return obj_.offsetWidth;
}

function posX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}else if (obj.x){
		curleft += obj.x;
	}
	return curleft;
}

function posY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}else if (obj.y){
		curtop += obj.y;
	}
	return curtop;
}

function windowH(){
	var windowH=0;
	
	var ie4 = (document.all)? true:false;
	
	if (ie4) windowH=document.body.offsetHeight;
	else 	windowH=window.innerHeight;
	
	return windowH;
}

function windowW(){
	var windowX=0;
	
	var ie4 = (document.all)? true:false;
	
	if (ie4) windowX=document.body.clientWidth;
	else 	windowX=window.innerWidth;
	
	return windowX;
}

function percentX(percent_){
	windowW=0;
	if (getNav()=='ie') windowW=document.body.clientWidth;
	else 	windowW=window.innerWidth;
	return Math.round(windowW*percent_/100);
}

function percentY(percent_){
	var ie4 = (document.all)? true:false;
	var	windowH=0;
	if (ie4) windowH=document.body.clientHeight;
	else 	windowH=window.innerHeight;
	return Math.round(windowH*percent_/100);
}

function mouseX(e){
	var posx = 0;
	if (!e) var e = window.event;

	if (e.pageX || e.pageY)
		posx = e.pageX;
	else if (e.clientX || e.clientY)
		posx = e.clientX + document.body.scrollLeft;

	return posx;
}

function mouseY(e){

	var posy = 0;
	if (!e) var e = window.event;

	if (e.pageX || e.pageY)
		posy = e.pageY;
	else if (e.clientX || e.clientY)
		posy = e.clientY + document.body.scrollTop;

	return posy;
}

function isDisplayed(idEl_, boolean_){
	if(boolean_){
		document.getElementById(idEl_).style.display="";
	}else{
		document.getElementById(idEl_).style.display="none";
	}
}

function changeDisplay(idEl_){
	if(document.getElementById(idEl_).style.display=="none"){
		document.getElementById(idEl_).style.display="";		
	}else{	
		document.getElementById(idEl_).style.display="none";
	}
}

/*
 * FONCTIONS ARRAY
 */

function in_array(val_,array_){

	if(typeof(val_)=='object'){
		for(var i=0; i<array_.length; i++){
			if(val_==array_[i] || val_.isEqual(array_[i])) return true;
		}
		return false;
	}else{
		for(var i=0; i<array_.length; i++){
			if(array_[i]==val_) return true;
		}
		return false;
	}
}

function array_remove(val_, array_){

	var indice;

	for(var i=0; i<array_.length; i++){
		if(array_[i]==val_) indice=i;
	}

	var new_array_deb = array_.slice(0,indice);
	var new_array_fin = array_.slice(indice+1,array_.length);
	var new_array=new_array_deb.concat(new_array_fin);

	return new_array;
}

/**
 * Filtre la liste select la plus proche dans la suite du code
 * 
 */
function filterSelect(select,valuesString){
	 arrayValues = valuesString.split(',');
	 options = select.childElements();
	 if(typeof(select.globalArrayOptions)=='undefined'){
		 select.globalArrayOptions = new Array();
		 for(var i=0;i<options.length;i++){
			 select.globalArrayOptions[i] = options[i];
		 }
	 }
	 for(var i=0;i<options.length;i++){
		 options[i].remove();
	 }
	 for(var i=0;i<select.globalArrayOptions.length;i++){
		 if(in_array(select.globalArrayOptions[i].value,arrayValues) || valuesString=='-1'){
			 select.appendChild(select.globalArrayOptions[i]);
		 }
	 }
}


/*
 * A REMPLACER AUSSI
 */

function getNav(){
	if (navigator.appName=="Netscape"){
		return 'moz';
	}else
	 if (navigator.appName=="Microsoft Internet Explorer"){
		return 'ie';
	}else{
		return 0;
	}
}