/*
	gaiia v3.1
	
	Sripts js pour les objets
	
	objets.js
*/


// Définition des variables
var NAV=''; // Type de navigateur

// Pour le drag & drop
var dragging = false;
var drag_obj=null;
var drag_x=0;
var drag_y=0;
var obj_x=0;
var obj_y=0; 


// Retourne le pointeur sur l'id de l'objet
function $(id)
{
	if (document.getElementById(id))
		return document.getElementById(id);
	else
		return false;
}


// Retourne le pointeur sur l'id du parent
function $p(id)
{
	return $(id).parentNode;
}

	
// Retourne le pointeur sur l'id de l'objet dans le parent
function $$(id)
{
	if (parent.document.getElementById(id))
		return parent.document.getElementById(id);
	else
		return false;
}


// Affiche un objet
function Show(id)
{
	$(id).style.display='block';
}


// Cache un objet
function Hide(id)
{
	$(id).style.display='none';
}


// Affiche un objet
function Parent_Show(id)
{
	$$(id).style.display='block';
}


// Cache un objet
function Parent_Hide(id)
{
	$$(id).style.display='none';
}



// Renvoie la valeur d'un objet
function Value(id)
{
	return $(id).value;
}


// Supprime un objet
function Delete(id)
{
	o=$(id);
	p=o.parentNode;
	p.removeChild(o);
}


// Supprime un objet dans le parent
function Parent_Delete(id)
{
	o=$$(id);
	p=o.parentNode;
	p.removeChild(o);
}


// Envoie vers le lien  lnk
function Location(lnk)
{
	document.location=lnk;
}


// Retourne la valeur v sans "px"
function Val(v)
{
	return parseInt(v.substr(0,v.length-2));
}


// Retourne le nom du navigateur
function Get_Navigator()
{
	if (document.all)
		NAV='ie';
	else
		NAV='ff';
		
	return NAV;
}


// Retourne un tableau avec les valeurs délimitées par 1 et 2
function Gaiia_Explode(delimiter1,delimiter2,st)
{
	var tab = new Array();
	tab_1=st.split('|');
	for (i=0; i<tab_1.length; i++)
	{
		tab_2=tab_1[i];
		tab[tab_2.split(':')[0]]=tab_2.split(':')[1];
	}
	return tab;
}


// Teste la validité d'un email
function Test_Email(email)
{
	var expr = /^([a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw|biz|cat|com|edu|gov|int|mil|net|org|pro|aero|arpa|coop|info|jobs|name|museum|travel)\b)$/;
	return email.match(expr);
}


// Initialisation du drag & drop
function init_drag(e,obj)
{
	dragging=true;
	drag_obj=$(obj + '_cadre');

	drag_x=(NAV=='ie') ? event.clientX : e.clientX;
	drag_y=(NAV=='ie') ? event.clientY : e.clientY;

	obj_x=parseInt(drag_obj.style.left);
	obj_y=parseInt(drag_obj.style.top);
}


// Gestion des évènements souris
function mouse_handler(e)
{
	if (NAV=='ie' && dragging==true)
	{
		drag_obj.style.left=obj_x + event.clientX - drag_x+'px'
		drag_obj.style.top=obj_y + event.clientY - drag_y+'px'
		return false
	}
	else 
	if (NAV=='ff' && dragging==true)
	{
		drag_obj.style.left=obj_x + e.clientX - drag_x+'px'
		drag_obj.style.top=obj_y + e.clientY - drag_y+'px'
		return false
	}
}


// Teste la valeur en fonction du type et l'obligation de saisie - Peut déclancher message d'alerte
function Object_Test_Value(o)
{
	type=o.getAttribute("object_type");
	required=o.getAttribute("required");
	name=o.getAttribute("object_name");
	switch (type)
	{
		case 'button' :
			break;
			
		case 'text' :
				value=$(name).value;
				if (required)
				{
					if (value=='')
					{
						alert($('label_'+name).innerHTML + ' : ' + $('req_'+name).getAttribute("message"));
						return false;
					}
				}
			break;
			
		case 'email' :
				value=$(name).value;
				if (required)
				{
					if (value=='')
					{
						alert($('label_'+name).innerHTML + ' : ' + $('req_'+name).getAttribute("message"));
						return false;
					}
					if (!Test_Email(value)) // Teste la validité de l'email
					{
						alert($('label_'+name).innerHTML + ' : adresse e-mail invalide !');
						return false;						
					}
				}
			break;
			
		case 'num' :
			break;
			
		case 'combobox' :
				value=$(name).value; //selectedIndex;
				if (required)
				{
					if (value==' ')
					{
						alert($('label_'+name).innerHTML + ' : ' + $('req_'+name).getAttribute("message"));
						return false;
					}
				}			
			break;
	}
	return true;
}


//  Valide le formulaire après avoir testé les valeurs
function Submit_Form(id)
{
	// Parcourt tous les fils du formulaire à la recherche des champs
	var TabChild = $(id).childNodes;
	for (var i = 0; i < TabChild.length; i++)
	{
		var o = TabChild[i];
		try
		{
			field=o.getAttribute("object_name");
			//alert(o.id);
			if (!Object_Test_Value(o)) return false; // Teste la valeur du champ
		}
		catch(ex)
		{
		}
	}
	// Soumission du formulaire
	$(id).submit(); 
	return true;
}


// Dessine un cadre autour de l'objet
function Cadre(id,pere,x,y,xx,yy,z,css)
{
	// Récupération infos du père
	if (pere=='body')
		p=document.body;
	else
		p=$(pere);

	// Création du cadre hg
	var o=document.createElement("div");
	o.id = id + '_hg';
	o.style.position="absolute";
	o.style.left= x + 'px';
	o.style.top= y + 'px';
	o.style.width= '20px';
	o.style.height= '20px';
	o.style.zIndex=z;
	o.style.backgroundImage= 'url(' + chm + 'libs/images/wnd/' + css + '_hg.png)';
	p.appendChild(o);

	// Création du cadre hd
	o=document.createElement("div");
	o.id = id + '_hd';
	o.style.position="absolute";
	o.style.left= x + (xx-20) + 'px';
	o.style.top= y + 'px';
	o.style.width= '20px';
	o.style.height= '20px';
	o.style.zIndex=z;
	o.style.backgroundImage= 'url(' + chm + 'libs/images/wnd/' + css + '_hd.png)';
	p.appendChild(o);
	
	// Création du cadre g
	o=document.createElement("div");
	o.id = id + '_g';
	o.style.position="absolute";
	o.style.left= x + 'px';
	o.style.top= (y+20) + 'px';
	o.style.width= '20px';
	o.style.height= (yy-40) + 'px';
	o.style.zIndex=z;
	o.style.backgroundImage= 'url(' + chm + 'libs/images/wnd/' + css + '_g.png)';
	p.appendChild(o);

	// Création du cadre d
	o=document.createElement("div");
	o.id = id + '_d';
	o.className=css + '_d';
	o.style.position="absolute";
	o.style.left= (x+xx-20) + 'px';
	o.style.top= (y+20) + 'px';
	o.style.width= '20px';
	o.style.height= (yy-40) + 'px';
	o.style.zIndex=z;
	o.style.backgroundImage= 'url(' + chm + 'libs/images/wnd/' + css + '_d.png)';
	p.appendChild(o);
	
	// Création du cadre h
	o=document.createElement("div");
	o.id = id + '_h';
	o.className=css + '_h';
	o.style.position="absolute";
	o.style.left= (x+20) + 'px';
	o.style.top= y + 'px';
	o.style.width= (xx-40) + 'px';
	o.style.height= '20px';
	o.style.zIndex=z;
	o.style.backgroundImage= 'url(' + chm + 'libs/images/wnd/' + css + '_h.png)';
	p.appendChild(o);

	// Création du cadre bg
	o=document.createElement("div");
	o.id = id + '_bg';
	o.style.position="absolute";
	o.style.left= x + 'px';
	o.style.top= y + (yy-20) + 'px';
	o.style.width= '20px';
	o.style.height= '20px';
	o.style.zIndex=z;
	o.style.backgroundImage= 'url(' + chm + 'libs/images/wnd/' + css + '_bg.png)';
	p.appendChild(o);
	
	// Création du cadre b
	o=document.createElement("div");
	o.id = id + '_b';
	o.style.position="absolute";
	o.style.left= (x+20) + 'px';
	o.style.top= y+(yy-20) + 'px';
	o.style.width= (xx-40) + 'px';
	o.style.height= '20px';
	o.style.zIndex=z;
	o.style.backgroundImage= 'url(' + chm + 'libs/images/wnd/' + css + '_b.png)';
	p.appendChild(o);

	// Création du cadre bd
	o=document.createElement("div");
	o.id = id + '_bd';
	o.className=css + '_bd';
	o.style.position="absolute";
	o.style.left= x + (xx-20) + 'px';
	o.style.top= y + (yy-20) + 'px';
	o.style.width= '20px';
	o.style.height= '20px';
	o.style.zIndex=z;
	o.style.backgroundImage= 'url(' + chm + 'libs/images/wnd/' + css + '_bd.png)';
	p.appendChild(o);
}


// Edition du contenu d'un block (Modification du texte)
function Block_Edit(id)
{
	var F_Edit_Block=window.open(chm + '/libs/block_edit.php?id=' + id,id,
			'width=800,height=600,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=yes');
}


// Affiche une fenêtre
function Window(id,titre,lnk,props)
{
	prop=Gaiia_Explode('|',':',props);
	
	if (!prop["css"]) prop["css"]='std';
	if (!prop["z"]) prop["z"]=100;
	if (prop["pere"]) 
		p=$(prop["pere"]); 
	else 
	{
		prop["pere"]='body';
		p=document.body;
	}
	x=parseInt(prop["x"]);
	y=parseInt(prop["y"]);
	xx=parseInt(prop["xx"]);
	yy=parseInt(prop["yy"]);
	
	// Paramètres pour la page appelée
	if (prop["params"])
	{
		params=prop["params"];
	}
	else params='';
	
	// On utilise le navigateur ou le gestionnaire de fenêtres de gaiia ?
	if (prop["nav"]=="true")
	{
		if (prop["no_popup"]=="true")
			wnd=window.open(lnk+'?'+params,id,'');
		else
			wnd=window.open(lnk+'?'+params,id,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no,width='+xx+',height='+yy);
		return;
	}
	
	// Création du cadre div
	oc=document.createElement("div");
	oc.id = id + '_cadre';
	oc.className= 'wnd_' + prop["css"];
	oc.style.position="absolute";
	oc.style.left= (x) + 'px';
	oc.style.top= (y) + 'px';
	oc.style.width= (xx) + 'px';
	oc.style.height= (yy) + 'px';
	oc.style.zIndex=prop["z"];
	p.appendChild(oc);	
	
	// Création de l'objet - iframe
	o=document.createElement("iframe");
	o.id = id + '_iframe';
	o.className= 'wnd_' + prop["css"];
	o.style.position="absolute";
	o.frameBorder="0";
	o.scrolling='no';
	o.style.left= (20) + 'px';
	o.style.top= (40) + 'px';
	o.style.width= (xx-40) + 'px';
	o.style.height= (yy-60) + 'px';
	o.src= lnk + '?css=' + prop["css"] + '&x= ' + x + '&y=' + y + '&xx=' + xx + '&yy=' + yy + params;
	o.style.zIndex=prop["z"];
	oc.appendChild(o);
	
	// Ajout du titre de la fenêtre
	o=document.createElement("div");
	o.id = id;	
	o.className='wnd_titre_' + prop["css"];
	o.style.position="absolute";
	o.style.left= (20) + 'px';
	o.style.top= (20) + 'px';
	o.style.width= (xx-45) + 'px';
	o.style.height= '20px';
	o.style.zIndex=prop["z"];
	txt_close='';
	if (prop["close"]=='true')
	{
		txt_close='<img src="' + chm + '/libs/icones/16/close.png" class="click" onclick="Hide(\'fond_message\'); Delete(\'' + id + '_cadre\');">';
	}
	o.innerHTML='<table width="100%"><td valign="middle">' + titre + '</td><td valign="middle" align="right">' + txt_close + '</td></table>';
	o.onmousedown = function(e)
			{
				init_drag(e,this.id);
			}
	o.onmouseup = function(e)
			{
				dragging=false;		
			}
	oc.appendChild(o);	
	
	// Création du cadre
	Cadre(id,id + '_cadre',0,0,xx,yy,prop["z"],prop["css"]);
	
	// Fenêtre modale ? - Affichage d'un fond
	if (prop["modal"]=='true') Show('fond_message');
}



// Parcourir - Appel de l'outil qui permet de parcourir la base documentaire
function Parcourir(lnk,obj,props)
{
	prop=Gaiia_Explode('|',':',props);
	opt='';
	if (prop["ext"]) opt+='&ext='+prop["ext"];
	if (prop["root_folder"]) opt+='&ext='+prop["root_folder"];
	Window('wnd_parcourir','Parcourir',chm + 'outils/parcourir/parcourir.php?lnk=' + lnk + '&obj=' + obj + opt,'xx:1000|yy:750|nav:true')
}


// initialisation
function initialisation()
{
	// Infos sur le navigateur
	Get_Navigator();

	// Taille de la zone de navigation
	if (NAV=="ie")
	{
		XX_SCREEN = (document.documentElement.clientWidth);
		YY_SCREEN = (document.documentElement.offsetHeight );
	}
	else
	{
		XX_SCREEN = (window.innerWidth);
		YY_SCREEN = (window.innerHeight);
	}
					
	// Chargement du gestionnaire d'évènements pour la souris
	document.onmousemove=mouse_handler;
	
	// Création du fond grisé pour les messages
	p=document.body;
	o=document.createElement("div");
	o.id = 'fond_message';
	o.className= 'fond_message';
	o.style.position="absolute";
	o.style.left= '0px';
	o.style.top= '0px';
	o.style.width= XX_SCREEN+'px';
	o.style.height= YY_SCREEN+'px';
	o.style.zIndex=99;
	p.appendChild(o);		
}


// -------------------------- Exécuté au chargement 
initialisation();
