// fonctions utiles (fichier fonctions.js)
function WindowOpenCentered(SiteUrl,l,h,resize,scroll)
{
	longueur=l;
	hauteur=h;
	haut = screen.height/2 - hauteur/2;
	dimx = screen.width/2 - longueur/2;
	window.open(SiteUrl,'popup', 'width='+longueur+' ,height='+hauteur+',left='+ dimx +',top='+ haut + ',resizable=' + resize + ',scrollbars='+scroll);
}

function WindowOpen(SiteUrl,l,h,x,y,resize,scroll)
{
	longueur=l;
	hauteur=h;
	window.open(SiteUrl,'popup','width='+longueur+',height='+hauteur+',left='+x+',top='+y+',resizable=' + resize + ',scrollbars='+scroll);
}

function swapImg(obj){
	var ch = obj.getElementsByTagName('img')[0].src;
	obj = obj.getElementsByTagName('img')[0];
	var finfin=ch.substring(ch.length-3,ch.length);
	var fin=ch.substring(ch.length-5,ch.length-4);
	ch=ch.substring(0,ch.length-5);
	
	if(fin == "0")
	   obj.src=ch+"1."+finfin;
	else if(fin == "1")
	   obj.src=ch+"0."+finfin;
}

function swapImage(obj, newUrl){
	obj.getElementsByTagName('img')[0].src = newUrl;	
}

function getObj(name){
	if (document.getElementById)
	{
  		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	else if (document.all)
	{
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
}

function getTop(objectId){
	var x = (new getObj(objectId)).obj;
	xTop = x.offsetTop;
	while(x.offsetParent!=null)
	{
		xParent = x.offsetParent;
		xTop += xParent.offsetTop;
		x = xParent;
	}
	return xTop;
}

function getAbsoluteTop(objectId){
	var x = objectId;
	xTop = x.offsetTop;
	while(x.offsetParent!=null)
	{
		xParent = x.offsetParent;
		xTop += xParent.offsetTop;
		x = xParent;
	}
	return xTop;
}

function getAbsoluteLeft(objectId){
	var x = objectId;
	xLeft = x.offsetLeft;
	while(x.offsetParent!=null)
	{
		xParent = x.offsetParent;
		xLeft += xParent.offsetLeft;
		x = xParent;
	}
	return xLeft;
}

function getWidth(objectId){
	var x = (new getObj(objectId)).obj;
	return x.offsetWidth;
}

function getHeight(objectId){
	var x = (new getObj(objectId)).obj;
	return x.offsetHeight;
}

function openPop(f,l,h,op)
{
	longueur=l;
	hauteur=h;
	haut = screen.height/2 - hauteur/2;
	dim = screen.width/2 - longueur/2;
	window.open(f,'','width='+longueur+',height='+hauteur+',left='+dim+',top='+haut+',resizable=no,'+op+'');
}

function openCenteredPopup(f,l,h,resize,scroll)
{
	longueur=l;
	hauteur=h;
	haut = screen.height/2 - hauteur/2;
	dimx = screen.width/2 - longueur/2;
	window.open(f,'','width='+longueur+',height='+hauteur+',left='+ dimx +',top='+ haut + ',resizable=' + resize + ',scrollbars='+scroll);
}

function openCustomPopup(f,l,h,x,y,resize,scroll)
{
	longueur=l;
	hauteur=h;
	window.open(f,'','width='+longueur+',height='+hauteur+',left='+x+',top='+y+',resizable=' + resize + ',scrollbars='+scroll);
}

function rollover(obj){
	obj.style.backgroundColor='#FD5A08';
	if(document.getElementById("text"+obj.id).className == 'tdSelect')
	{
		dateSelectionne = true;
	}
	else
	{
		dateSelectionne = false;
	}
	document.getElementById("text"+obj.id).className='txtblanc11';
}

function rollout(obj){
	obj.style.backgroundColor='';
	if (dateSelectionne == true)
	{
		document.getElementById("text"+obj.id).className='tdSelect';
	}
	else
	{
		document.getElementById("text"+obj.id).className='';
	}
	dateSelectionne = false;
}

function cmsHPBSlink(loc, target)
{
	if(target.indexOf(',')!=-1)
		var ch = target.substring(0,target.indexOf(','));
	else
		var ch = target;
	
	var prop = target.substring(target.indexOf(',')+1,target.length);

	lowtarget=ch.toUpperCase();
	
	if (lowtarget=="_SELF")
	{
		window.location=loc;
	}
	else
	{
		if (lowtarget=="_BLANK")
		{
			window.open(loc);
		}
		else 
		{
			if (lowtarget=="POPUP") { window.open(loc,'windowname',prop); }
		}
	}
}

//--  Ajoute la CSS d'édition a l'objet fourni : 1=Admin / 0=Normal
function modeAdmin(obj,inc)
{
	if (obj!='[object]') { obj = new getObj(obj).obj; }
		
	if (inc==1)
	{
		cName = obj.className;
		//obj.style.width = obj.offsetWidth;
		obj.className = cName+" selectin"; 
	}
	
	if (inc==0)
	{
		obj.className = cName;
	}
}

//--  Ajoute la CSS d'édition a l'objet fourni : 1=Admin / 0=Normal
function modeTopAdmin(obj,inc)
{
	if (obj!='[object]') { obj = new getObj(obj).obj; }

	if (inc==1)
	{
		cName = obj.className;
		//obj.style.width = obj.offsetWidth;
		obj.className = cName+" selectSousin"; 
	}
	
	if (inc==0)
	{
		obj.className = cName;
	}
}


//-- Context Menu

var display_url = 1;

function showContextMenu(obj)
{ 
	//var rightedge = 900;
	//var bottomedge = 900;
	var rightedge = document.body.clientWidth - event.x;
	var bottomedge = document.body.clientHeight - event.y;
	  
	if (rightedge < obj.offsetWidth) 
	{
		obj.style.left = document.body.scrollLeft + event.x - obj.offsetWidth;
	}
	else 
	{
		obj.style.left = document.body.scrollLeft + event.x - 10;
	}
	  
	if (bottomedge<obj.offsetHeight) 
	{
		//obj.style.top = document.body.scrollTop + event.y - obj.offsetHeight;
		obj.style.top = event.y;	 
	}
	else 
	{
		//obj.style.top = document.body.scrollTop + event.y - 20;
		obj.style.top = event.y - 20;
	}
	  
	obj.style.visibility = "visible";
	return false;
} 
  
function hideContextMenu(obj)
{ 
	obj.style.visibility="hidden";
} 
  
function over()
{ 
	if (event.srcElement.className=="menuitems")
	{ 
		event.srcElement.style.backgroundColor="highlight";
		event.srcElement.style.color="white";

		if (display_url==1) window.status = event.srcElement.url;
	} 
} 
  
function out(){ 
	if (event.srcElement.className=="menuitems")
	{ 
		event.srcElement.style.backgroundColor="";
		event.srcElement.style.color="black";
		window.status='';
	} 
} 
  
function linkTo()
{ 
	if (event.srcElement.className=="menuitems") 
		window.location=event.srcElement.url;
} 

//sim
function BaseComponentOpen(SiteUrl, path)
{	
	document.location = SiteUrl  + '/FileOpen.aspx?field=' + path ;
}

function BaseComponentOpenFile(SiteUrl, path)
{	
	window.open(SiteUrl  + '/FileOpen.aspx?field=' + path );
}

function openCenteredFilePopup(SiteUrl,path,l,h,resize,scroll)
{
	longueur=l;
	hauteur=h;
	haut = screen.height/2 - hauteur/2;
	dimx = screen.width/2 - longueur/2;
	window.open(SiteUrl  + '/FileOpen.aspx?field=' + path,'', 'width='+longueur+' ,height='+hauteur+',left='+ dimx +',top='+ haut + ',resizable=' + resize + ',scrollbars='+scroll);
}

function openCustomFilePopup(SiteUrl,path,l,h,x,y,resize,scroll)
{
	longueur=l;
	hauteur=h;
	window.open(SiteUrl  + '/FileOpen.aspx?field='+path,'','width='+longueur+',height='+hauteur+',left='+x+',top='+y+',resizable=' + resize + ',scrollbars='+scroll);
}
//endsim


// BaseComponentUpdate paramètres :
			// 1 Nom du BaseComponent
			// Largeur PopUp
			// Hauteur PopUp
			// Identifiant ClientID ??
			// ROOT chemin du XML juste avant le BaseComponent.
			// SelectID (Page)

function BaseComponentUpdate(SiteUrl, BaseComponentName, PopupWidth, PopupHeight, RootXML, SelectID, TextFormat, moduleID, versionID, siteAlias, TabID, TabVersion)
{	
	window.open(SiteUrl  + '/ContentEdition.aspx?SELECTID=' + SelectID + '&BASECOMPONENT=' + BaseComponentName + '&ROOT=' + RootXML+ '&FORMAT=' + TextFormat + '&MODULEID=' + moduleID + '&VERSIONID=' + versionID + '&TabID=' + TabID + '&Version=' + TabVersion + '&alias=' + siteAlias, 'WebFormEdition', 'top=50,left=50,width=' + PopupWidth + ',height=' + PopupHeight + ',status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes');
}

function BaseComponentRankUp(SiteUrl, RootXML, SelectID, Rank, moduleID, versionID, TabID, TabVersion)
{
	document.location = SiteUrl  + '/ComponentsMethods.aspx?SELECTID=' + SelectID + '&ROOT=' + RootXML + '&RANK=' + Rank + '&ACTION=UP' + '&MODULEID=' + moduleID + '&VERSIONID=' + versionID + '&TabID=' + TabID + '&Version=' + TabVersion ;
}

function BaseComponentRankDown(SiteUrl, RootXML, SelectID, Rank, moduleID, versionID, TabID, TabVersion)
{
	document.location = SiteUrl  + '/ComponentsMethods.aspx?SELECTID=' + SelectID + '&ROOT=' + RootXML + '&RANK=' + Rank + '&ACTION=DOWN' + '&MODULEID=' + moduleID + '&VERSIONID=' + versionID + '&TabID=' + TabID + '&Version=' + TabVersion ;
}

function BaseComponentRankInsert(SiteUrl, RootXML, SelectID, Rank, BaseComponent, moduleID, versionID, TabID, TabVersion)
{
	document.location = SiteUrl  + '/ComponentsMethods.aspx?SELECTID=' + SelectID + '&ROOT=' + RootXML + '&RANK=' + Rank + '&ACTION=INSERT&BASECOMPONENT=' + BaseComponent + '&MODULEID=' + moduleID + '&VERSIONID=' + versionID + '&TabID=' + TabID + '&Version=' + TabVersion ;
}

function BaseComponentRankDelete(SiteUrl, RootXML, SelectID, Rank, BaseComponent, moduleID, versionID, TabID, TabVersion)
{
	document.location = SiteUrl  + '/ComponentsMethods.aspx?SELECTID=' + SelectID + '&ROOT=' + RootXML + '&RANK=' + Rank + '&ACTION=DELETE&BASECOMPONENT=' + BaseComponent + '&MODULEID=' + moduleID + '&VERSIONID=' + versionID + '&TabID=' + TabID + '&Version=' + TabVersion ;
}

function BaseComponentTabViewURL(SiteUrl, RootXML, SelectID, Rank, BaseComponent, TABSID, TAB_ID, VIEWID, TABVIEWURL, web, moduleID, versionID, TabID, TabVersion)
{
	document.location = SiteUrl  + '/ComponentsMethods.aspx?SELECTID=' + SelectID + '&ROOT=' + RootXML + '&RANK=' + Rank + '&ACTION=CHANGETAB&BASECOMPONENT=' + BaseComponent + '&TABSID=' + TABSID + '&TAB_ID=' + TAB_ID +'&VIEWID=' + VIEWID +'&TABVIEWURL=' + TABVIEWURL + web + '&MODULEID=' + moduleID + '&VERSIONID=' + versionID + '&TabID=' + TabID + '&Version=' + TabVersion ;
}

/* nouvelles fonctions */

/*
 * Recharge la page parente de la pop-up d'édition,
 * suite à la validation d'une modification,
 * puis ferme la pop-up d'édition.
 * @documentDomain : domaine de la page parente
 */
function CloseEditWindow(documentDomain)
{
	try		
	{
		// on modifie le domaine de la pop-up pour pouvoir rafraîchir la page parente
		if (documentDomain != null && documentDomain != "")
		{
			document.domain = documentDomain;
		}
		// on recharge la page parente
		window.opener.location.reload();
	}
	catch (error)
	{
		//alert("error : " + error.message);
	}
	finally
	{
		// on ferme la pop-up d'édition
		window.close();	
	}
}

/*
 * Ouvre la Base Media et récupère les infos sur le Media sélectionné.
 * @urlBaseMedia : URL d'accès à la Base Media
 * @inputTextID : ID de la textbox dans laquelle sera stockée l'URL du média sélectionné
 * @inputHiddenID : ID du champ caché dans lequel sera stocké toute la chaîne d'information renvoyée par la Base Media
 */
function OpenBaseMedia(urlBaseMedia, inputTextID, inputHiddenID, windowStyle)
{
	var ressource = window.showModalDialog(urlBaseMedia,'',windowStyle);
	if (ressource != null)
	{
		var urlFile = ressource.substring(0, ressource.indexOf('///'));
		document.getElementById(inputTextID).value = urlFile.substring(0, ressource.indexOf('//'));
		document.getElementById(inputHiddenID).value = urlFile;
	}
}
