//===============================================================
// XANATA core
//===============================================================
String.prototype.eolEncode = function() {
	return this.replace(/[']/g, '§!Q§').replace(/[&]/g, '§!E§').replace(/["]/g, '§!D§');
};
function SessionURL(url) {
	//if (url.indexOf('jsessionid') == -1) return url + ";jsessionid=" + sys_session;
	return url;
}
function VersPage(targetPage, param1, param2, param3, param4, param5) {
	GoSite( { page :targetPage, p1 :param1, p2 :param2, p3 :param3, p4 :param4, p5 :param5 });
}
function VersState(targetState, param1, param2, param3, param4, param5) {
	GoSite( { state :targetState, p1 :param1, p2 :param2, p3 :param3, p4 :param4, p5 :param5 });
}
function VersPageState(targetPage, targetState, param1, param2, param3, param4, param5) {
	GoSite( { page :targetPage, state :targetState, p1 :param1, p2 :param2, p3 :param3, p4 :param4, p5 :param5 });
}
function VersMode(targetMode, param1, param2, param3, param4, param5) {
	GoSite( { mode :targetMode, p1 :param1, p2 :param2, p3 :param3, p4 :param4, p5 :param5 });
}
function VersModeState(targetMode, targetState, param1, param2, param3, param4, param5) {
	GoSite( { mode :targetMode, state :targetState, p1 :param1, p2 :param2, p3 :param3, p4 :param4, p5 :param5 });
}
function VersUrl(targetUrl, param1, param2, param3, param4, param5, newWindow) {
	GoSite( { url :targetUrl, p1:param1, p2:param2, p3:param3, p4:param4, p5:param5, target:newWindow});
}
function VersUrlBlank(targetUrl, param1, param2, param3, param4, param5) {
	GoSite( { url :targetUrl, p1:param1, p2:param2, p3:param3, p4:param4, p5:param5, target:'_blank'});
}
function callUpdate(reference, newWindow, param1, param2, param3, param4, param5){
	GoSite( {url:'/Update/'+reference, target:newWindow, p1:param1, p2:param2, p3:param3, p4:param4, p5:param5});
}
// GoSite( {page:'site.index', state:'std', mode:'01'} );
// par défaut page=sys_page, state='std', mode='', target='_self';

function GoSite(params) {
	var args = new Hash(params);

	var sitePage = sys_page;
	var siteState = "start";
	var siteMode = null;
	var siteTarget = '_self';

	if (args.has('page')) sitePage = args.get('page');
	if (args.has('state')) siteState = args.get('state');
	if (args.has('mode')) siteMode = args.get('mode');
	if (args.has('target')) siteTarget = args.get('target');

	var siteAction = "";
	if (args.has('url')) siteAction=args.get('url');
	else siteAction='/Page/' + sitePage;

	// Probléme sur lecture de siteState
	if (siteState){ 
		if( siteState != "start") // valeur par défault
		{
			if( siteAction.contains("?")) siteAction+="&"; else siteAction+="?";
			siteAction+='_sp='+siteState; // Visible pour indexation (Google)
		}
	}
	
	var frmLink = new Element('form').setProperties({ id:'sys_frmlink',action :SessionURL(siteAction), method :'post', target :siteTarget });
	if (siteState) new Element('input').setProperties({ type :'hidden', name :'__sp', value :siteState }).injectInside(frmLink);
	if (siteMode) new Element('input').setProperties({ type :'hidden', name :'_ms', value :siteMode }).injectInside(frmLink);
	if (args.has('p1')) new Element('input').setProperties({ type :'hidden', name :'_P1_', value :args.get('p1') }).injectInside(frmLink);
	if (args.has('p2')) new Element('input').setProperties({ type :'hidden', name :'_P2_', value :args.get('p2') }).injectInside(frmLink);
	if (args.has('p2')) new Element('input').setProperties({ type :'hidden', name :'_P3_', value :args.get('p3') }).injectInside(frmLink);
	if (args.has('p3')) new Element('input').setProperties({ type :'hidden', name :'_P4_', value :args.get('p4') }).injectInside(frmLink);
	if (args.has('p4')) new Element('input').setProperties({ type :'hidden', name :'_P5_', value :args.get('p5') }).injectInside(frmLink);
	new Element('input').setProperties( { type :'hidden', name :'jsessionid', value :sys_session }).injectInside(frmLink);
	frmLink.injectInside(document.body);
	frmLink.submit();
	frmLink.destroy();
}

function gotoUrl(url) {
	alert('gotoUrl deprecié...( Voir VersUrl )');
	window.location = url;
}

function Admin(section, opt1, opt2, opt3, opt4, newWindow) {
	if (!section) section = 'start';
	var Destination = SessionURL('/AdminSite');
	VersUrl(Destination, section, opt1, opt2, opt3, opt4, newWindow);
}

function Update(refUpdateAction, message, opt1, opt2, opt3, opt4, opt5) {
	if ($defined(message)) {
		if (confirm(message)) {
			VersUpdate(refUpdateAction, opt1, opt2, opt3, opt4, opt5);
		}
	} else VersUpdate(refUpdateAction, opt1, opt2, opt3, opt4, opt5);
}
function UpdateAction(refUpdateAction, message, opt1, opt2, opt3, opt4, opt5) {
	if ($defined(message)) {
		if (confirm(message)) {
			VersUpdate(refUpdateAction, opt1, opt2, opt3, opt4, opt5);
		}
	} else VersUpdate(refUpdateAction, opt1, opt2, opt3, opt4, opt5);
}

function VersUpdate(refUpdateAction, p1, p2, p3, p4, p5) {
	// on nettoie un éventuel ancien frmLink
	var tagFrmLink = "sys_frmLink";
	var oldFrmLink = $(tagFrmLink);
	if( $chk(oldFrmLink)) oldFrmLink.dispose();
	// on crée le nouveau...
	var Destination = SessionURL('/Update');
	var frmLink = new Element('form').setProperties( { id:tagFrmLink, action :Destination, method :'post' });
	new Element('input').setProperties( { type :'hidden', name :'typ', value :'ua' }).injectInside(frmLink);
	new Element('input').setProperties( { type :'hidden', name :'ref', value :$pick(refUpdateAction, 'none') }).injectInside(frmLink);
	if ($chk(p1)) new Element('input').setProperties( { type :'hidden', name :'_P1_', value :p1 }).injectInside(frmLink);
	if ($chk(p2)) new Element('input').setProperties( { type :'hidden', name :'_P2_', value :p2 }).injectInside(frmLink);
	if ($chk(p3)) new Element('input').setProperties( { type :'hidden', name :'_P3_', value :p3 }).injectInside(frmLink);
	if ($chk(p4)) new Element('input').setProperties( { type :'hidden', name :'_P4_', value :p4 }).injectInside(frmLink);
	if ($chk(p5)) new Element('input').setProperties( { type :'hidden', name :'_P5_', value :p5 }).injectInside(frmLink);
	new Element('input').setProperties( { type :'hidden', name :'jsessionid', value :sys_session }).injectInside(frmLink);
	frmLink.injectInside(document.body);
	frmLink.submit();
}

function VersUpdateTarget(action, target, p1, p2, p3, p4, p5) {
	var Destination = SessionURL('/Update');
	var frmLink = new Element('form').setProperties( { action :Destination, method :'post', target :target });
	new Element('input').setProperties( { type :'hidden', name :'_action_', value :$pick(action, 'none') }).injectInside(frmLink);
	if ($chk(p1)) new Element('input').setProperties( { type :'hidden', name :'_P1_', value :p1 }).injectInside(frmLink);
	if ($chk(p2)) new Element('input').setProperties( { type :'hidden', name :'_P2_', value :p2 }).injectInside(frmLink);
	if ($chk(p3)) new Element('input').setProperties( { type :'hidden', name :'_P3_', value :p3 }).injectInside(frmLink);
	if ($chk(p4)) new Element('input').setProperties( { type :'hidden', name :'_P4_', value :p4 }).injectInside(frmLink);
	if ($chk(p5)) new Element('input').setProperties( { type :'hidden', name :'_P5_', value :p5 }).injectInside(frmLink);
	new Element('input').setProperties( { type :'hidden', name :'_session_', value :sys_session }).injectInside(frmLink);
	frmLink.injectInside(document.body);
	frmLink.submit();
}

function VersUpdateAjax(p1, p2, p3, p4, p5) {
	alert('VersUpdateAjax : Obsolete');
	var Destination = SessionURL('/Update');
	var frmLink = new Element('form').setProperties( { action :Destination, method :'post' });
	// new
	// Element('input').setProperties({type:'hidden',name:'_action_',value:$pick(action,'none')}).injectInside(frmLink);
	if ($chk(p1)) new Element('input').setProperties( { type :'hidden', name :'_P1_', value :p1 }).injectInside(frmLink);
	if ($chk(p2)) new Element('input').setProperties( { type :'hidden', name :'_P2_', value :p2 }).injectInside(frmLink);
	if ($chk(p3)) new Element('input').setProperties( { type :'hidden', name :'_P3_', value :p3 }).injectInside(frmLink);
	if ($chk(p4)) new Element('input').setProperties( { type :'hidden', name :'_P4_', value :p4 }).injectInside(frmLink);
	if ($chk(p5)) new Element('input').setProperties( { type :'hidden', name :'_P5_', value :p5 }).injectInside(frmLink);
	new Element('input').setProperties( { type :'hidden', name :'_session_', value :sys_session }).injectInside(frmLink);
	frmLink.injectInside(document.body);
	frmLink.sendEOL();
}

//OK, Version 1.2
function UpdateAjaxMessage(question, refUpdateAction, p1, p2, p3, p4, p5) {
	if ($defined(question)) {
		if (confirm(question)) {
			UpdateAjax(refUpdateAction, p1, p2, p3, p4, p5);
		}
	}
}

// OK, Version 1.2
function UpdateAjax(refUpdateAction, p1, p2, p3, p4, p5) {
	var options = "{}";
	var Destination = SessionURL('/Update');
	var frmLink = new Element('form').setProperties( { action :Destination, method :'post' });
	new Element('input').setProperties( { type :'hidden', name :'typ', value :'ux' }).injectInside(frmLink);
	new Element('input').setProperties( { type :'hidden', name :'ref', value :$pick(refUpdateAction, 'none') }).injectInside(frmLink);
	if ($chk(p1)) new Element('input').setProperties( { type :'hidden', name :'_P1_', value :p1 }).injectInside(frmLink);
	if ($chk(p2)) new Element('input').setProperties( { type :'hidden', name :'_P2_', value :p2 }).injectInside(frmLink);
	if ($chk(p3)) new Element('input').setProperties( { type :'hidden', name :'_P3_', value :p3 }).injectInside(frmLink);
	if ($chk(p4)) new Element('input').setProperties( { type :'hidden', name :'_P4_', value :p4 }).injectInside(frmLink);
	if ($chk(p5)) new Element('input').setProperties( { type :'hidden', name :'_P5_', value :p5 }).injectInside(frmLink);
	new Element('input').setProperties( { type :'hidden', name :'_session_', value :sys_session }).injectInside(frmLink);
	frmLink.injectInside(document.body);
	new AjaxEOL(frmLink.getProperty('action'), $merge({data: frmLink.toQueryString()}, options, {method: 'post'})).request();
	frmLink.destroy();
}

//OK, Version 1.2
function updateSelect(el, refAction, p1, p2, p3, p4, p5)
{ 
	var index=el.selectedIndex;
	UpdateAjax(refAction, el.options[index].value , p1, p2, p3, p4);
}

// ===============================================================
// ===============================================================
// EOLlogin
// ===============================================================
// ===============================================================
function login(formname, useritem, passitem) {
	var form = document.forms[formname];
	if (form[useritem].length == 0) {
		$(useritem).focus();
		return false;
	}
	if (form[passitem].length == 0) {
		$(passitem).focus();
		return false;
	}
	form.submit();
	return true;
}

function isEnter(event) {
	if (event.keyCode == 13) return true;
	return false;
}

// ==================================================================================

function OpenWin(windowName, url, width, height, resize) {
	if (!windowName) windowName = "WINDOWS";
	if (!url) url = "#";
	if (!width) width = "600";
	if (!height) height = "400";
	if (!resize) resize = "no";
	if (resize != "yes") resize = "no"

	var winX = (screen.availWidth - width) / 2;
	var winY = (screen.availHeight - height) / 2;

	var sizes = 'left=' + winX + ',top=' + winY + ',width=' + width + ',height=' + height;

	var myWin = window.open(url, windowName, sizes + ',directories=no,location=no,menubar=no,scrollbars=yes,status=0,toolbar=0,resizable=' + resize);
	myWin.focus();
}
// ==================================================================================
// ModalDialogs
// ==================================================================================
var Modal = new Class( { initialize : function(options) {
	this.wdialog = new Array();
	this.id_filtre = 'modalFiltre';
	this.filtreLevel = 999;
	this.startLevel = 999;
}, open : function(refAndId, p1, p2, p3, p4, p5) {
	this._filtreUp();
	var Panel = refAndId + 'Panel';
	var levelPanel = (this.filtreLevel + 1);
	var idPanel = levelPanel + 'Panel';

	var tBody = new Element('tbody');
	var trDialog = new Element('tr');
	var cellDialog = new Element('td').setProperty('id', refAndId);

	var newDialog = new Element('table').setProperties( { id :idPanel, cellpadding :'0', cellspacing :'0' });
	newDialog.setStyles( { top :'-2000px', left :'-2000px', position :'absolute', backgroundColor :'transparent' });
	newDialog.setStyle('z-index', this.filtreLevel);

	cellDialog.injectInside(trDialog);
	trDialog.injectInside(tBody);
	tBody.injectInside(newDialog);
	newDialog.inject(document.body);

	var Params = "";
	if ($chk(p1)) Params += "&_P1_=" + p1;
	if ($chk(p2)) Params += "&_P2_=" + p2;
	if ($chk(p3)) Params += "&_P3_=" + p3;
	if ($chk(p4)) Params += "&_P4_=" + p4;
	if ($chk(p5)) Params += "&_P5_=" + p5;

	var ajax = new AjaxEOL(SessionURL('/Update'), { onComplete : function() {
		this._drawModal(idPanel);
	}.bind(this), data :'typ=ux&ref=' + refAndId + Params });
	ajax.request();
	
}, _drawModal : function(id) {
	var dialog = $(id);
	var diagSizes = dialog.getCoordinates();

	var sizes = window.getSize();
	var scroll = window.getScroll();
	var dialogLeft = ((sizes.x - diagSizes['width']) / 2) + scroll.x;
	var dialogTop = ((sizes.y - diagSizes['height']) / 2) + scroll.y;

	dialog.setOpacity(0);
	dialog.setStyle('top', dialogTop + 'px');
	dialog.setStyle('left', dialogLeft + 'px');
	dialog.fade('in');

	//dialog.get('tween', {duration: 200,}).start('opacity', 0, 1);
	//dialog.get('tween', {duration: 1000,transition: Fx.Transitions.Sinus.easeIn}).start('top', -200, dialogTop);
	
}, close : function(id) {
	var levelPanel = (this.filtreLevel + 1);
	var idPanel = levelPanel + 'Panel';
	var dialog = $(idPanel);
		
	//dialog.effect('opacity', { duration :600, onComplete : function() {this._removeModal(idPanel);}.bind(this) }).start(1, 0);
	if ($chk(dialog)) dialog.get('tween', {duration: 100,onComplete : function() {this._removeModal(idPanel);}.bind(this)}).start('opacity', 1, 0);
	
	this._filtreDown();
}, _removeModal : function(id) {
	var dialog = $(id);
	if ($chk(dialog)) dialog.destroy();

}, _filtreUp : function() {
	this.filtreLevel += 2;
	var filtre = $(this.id_filtre);
	// si le filtre est déjà en place, on change juste sa position z-index,
	// sinon on le créé
	if ($chk(filtre)) {
		filtre.setStyle('z-index', this.filtreLevel);
	} else {
		var scrollSizes = window.getScrollSize();
		var sizeX = (scrollSizes.x) + "px";
		var sizeY = (scrollSizes.y) + "px";
		var filtre = new Element('div').setProperty('id', this.id_filtre).setStyles( { top :'0px', left :'0px', width :sizeX, height :sizeY, position :'absolute', 'background-color' :sys_filter_color });
		filtre.setStyle('z-index', this.filtreLevel);
		filtre.setOpacity(0);
		filtre.inject(document.body);

		//filtre.get('tween', {duration: 1500,transition: Fx.Transitions.Elastic.easeInOut}).start('opacity', 0, sys_filter_opacity);
		// ou
		filtre.set('tween', {duration: sys_filter_drawtime});
		filtre.tween( 'opacity', [0,sys_filter_opacity]);
	}
}, _filtreDown : function() {
	this.filtreLevel -= 2;
	var filtre = $(this.id_filtre);
	if ($chk(filtre)) {
		if (this.filtreLevel <= this.startLevel) {
			this._filtreHide();
		} else {
			filtre.setStyle('z-index', this.filtreLevel);
		}
	}
}, _filtreHide : function() {
	var filtre = $(this.id_filtre);
	if ($chk(filtre)) {
		filtre.get('tween', {duration: 100,onComplete : function() {this._filtreRemove();}.bind(this)}).start('opacity', 1, 0);
	}
}, _filtreRemove : function() {
	var filtre = $(this.id_filtre);
	if ($chk(filtre)) filtre.destroy();

	this.filtreLevel = 999;
}

});

var modal = new Modal();
// ==================================================================================
// fin ModalDialogs
// ==================================================================================

// ===============================================================================
var BoxTabs = new Class( { 
	initialize : function(options) {
		this.tab_id_current = '';
	}, 
	swap : function(id_tab, elLINK) {
		if( $chk(elLINK) )
		{
			var tdParent=elLINK.getParent();
			var trParent=tdParent.getParent();
			$$('td',trParent).each( function(baliseLink){
				baliseLink.removeClass('current');
			});			
			tdParent.addClass('current');
		}
		if (this.tab_id_current != '') {$(this.tab_id_current).style.display = 'none';}
		$(id_tab).style.display = '';
		this.tab_id_current = id_tab;
	}
});

/*
var mutli_tab_id_current = '';
function mtSwap(tabId) {
	if (mutli_tab_id_current != '') {
		$(mutli_tab_id_current).style.display = 'none';
	}
	$(tabId).style.display = '';
	mutli_tab_id_current = tabId;
}
*/
// ======================== Divers =======================================================
function noSelectOn(target)
{
if(!$chk(target)) return; 
if (typeof target.onselectstart!="undefined") target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") target.style.MozUserSelect="none"
else target.onmousedown=function(){return false}
target.style.cursor = "default"
}
// ======================= catchTab =======================================================
function catchTab(textInput, evt)
{
	var event = new Event(evt);
	if(event.code==9 && !event.shift)
	{
		textInput.insertAtCursor("\t", false);
		return false; // pour ne pas propager
	}
	return true;
}

// ========================== appearOn =====================================================
// Usage :
//	Element.appearOn(element, opacity, options);
// Example
//	$('myElement').appearOn($('anotherElement'));
//
Element.implement({
	appearOn: function(el, opacity, options){
		opacity = $type(opacity) == 'array' ? [opacity[0] || 1, opacity[1] || 0] : [opacity || 1, 0];
		
		this.set({
			opacity: opacity[1],
			tween: options || {duration: 200}
		});
		
		$(el).addEvents({
			mouseenter: this.fade.bind(this, opacity[0]),
			mouseleave: this.fade.bind(this, opacity[1])
		});
		return this;
	}
});

