function blindToggle(elmt, closeAll) {
	status = elmt.className;

	if (elmt.value == 0 || status == 'closed') {
		divs = $$('div.open');
		if (closeAll == 'true') {
			for (i = 0; i < divs.length; i++) {
				new Effect.BlindUp(divs[i],{scaleTo:5});
				Element.removeClassName(divs[i], 'open');
				Element.addClassName(divs[i], 'closed');
				divs[i].value = 0;
			}
		}

		new Effect.BlindDown(elmt,{scaleFrom:5});
		Element.removeClassName(elmt, 'closed');
		Element.addClassName(elmt, 'open');
		elmt.value = 1;
	}
	else {
		new Effect.BlindUp(elmt,{scaleTo:5});
		Element.removeClassName(elmt, 'open');
		Element.addClassName(elmt, 'closed');
		elmt.value = 0;
	}
}

function DisplayPngByBrowser ( browser, img_path, width, height ) {
	var png_path;
	if (browser == 'Microsoft Internet Explorer') {
		$('date').innerHTML = '<img src="/images/blank.gif" style="width:'+width+'px; height:'+height+'px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+img_path+'\', sizingMethod=\'scale\');" >';
	}
	else if (browser == 'Netscape') $('date').innerHTML = "<img src='"+img_path+"' />";
	else $('date').innerHTML = "<img src='"+img_path+"' />";
}

function checkform(formid) {
	var ereg = new RegExp('^[a-z-_]+$');
	var needs = new Array();
	var regexp = new Array();

	switch (formid) {
		case 'new_entity':
			var needs = new Array('new_code', 'new_description');
			var regexp = new Array('new_code');
			break;
		case 'edit_entity':
			var needs = new Array('edit_code', 'edit_description');
			var regexp = new Array('edit_code');
			break;
		case 'new_shop' :
			var needs = new Array('new_description');
			break;
		case 'edit_shop' :
			var needs = new Array('edit_description');
			break;
		case 'new_user' :
			var needs = new Array('new_login', 'new_description', 'new_email', 'new_passwd');
			break;
		default :
			return false;
	}
	for (i=0; i < needs.length; i++) {
		if ($(needs[i]).value == '') return false;
	}
	for (i=0; i < regexp.length; i++) {
		if (!$(regexp[i]).value.match(ereg)) return false;
	}
	$(formid).submit();
}

function ajaxSelect(dest, object) {
	separator = document.location.search ? '&' : '?';
	switch (object.id) {
		case 'list_entite' :
			url = document.location+separator+'id_entite='+object.value;
			break;
		case 'list_shop' :
			url = document.location+separator+'id_entite='+$('list_entite').value+'&id_magasin='+object.value;
			break;
		case 'list_user' :
			url = document.location+separator+'id_entite='+$('list_entite').value+'&id_magasin='+$('list_shop').value+'&id_utilisateur='+object.value;
			break;
	}
	if (object.value != '0' && object.value != '00') {
		new Ajax.Updater(
			$(dest),
			url,
			{method: 'get'}
		);
	} else {
		$(dest).innerHTML = '';
	}
}

function changeUserStatus(id) {
	var ajax = new Ajax.Request(
		'/administration/utilisateur/',
		{
			method: 'post',
			parameters: 'activate='+id,
			onComplete: function(t) {
				sta = $('status_'+id).innerHTML;
				if (sta.indexOf('Désactiver') != -1) {
					sta = '<img alt="Activer" title="Activer" border="0" src="/images/user_innactive.png" />';
				} else {
					sta = '<img alt="Désactiver" title="Désactiver" border="0" src="/images/user_active.png" />';
				}
				$('status_'+id).innerHTML = sta;
			}
		}
	);
}

function checkUnicity(object,dest, field) {
	params = "unicity="+object.value;
	if (field != undefined) params += "&field=" + field;
	new Ajax.Request(
		document.location,
		{
			method: "post",
			parameters: params,
			onComplete: function(t) {
				if (t.responseText != '0') {
					if ($(dest).innerHTML == '') $('unicity_count').value = eval(parseInt($('unicity_count').value) + 1);
					$(dest).innerHTML = t.responseText > 0 
							? '<br />Cette valeur est déjà présente dans la base de données.'
							: '<br />Vous ne pouvez pas utiliser des caractères spéciaux autre que - ou _ pour ce champ.';
					$('submit').disabled = 'disabled';
				} else {
					if ($(dest).innerHTML != '') eval($('unicity_count').value = $('unicity_count').value - 1);
					$(dest).innerHTML = '';
					if ($('unicity_count').value == 0) {
						$('submit').disabled = '';
					}
				}
			}
		}
	);
}

function assignForm(formid, shopid, object) {
	object.value = object.value == 1 ? 0 : 1;
	assign = object.value == 1 ? 1 : 0;
	new Ajax.Request(
		document.location,
		{
			method: "post",
			parameters: "assign="+assign+"&form="+formid+"&shop="+shopid
		}
	);
}

function checkPdfForm(){
	fields = $$('input.compulsory');

	// Si un champ est vide on quitte la fonction
	for (i = 0; i < fields.length ; i++) {
		if ($(fields[i].value).value == '') {
			$('submit').disabled = 'disabled';
			return false;
		}
	}
	// Si tous les champs sont renseignés on active le bouton
	$('submit').disabled = '';
	
	document.getElementById('blockeur').value=0;
}

function checkUserPasswd() {
	var ereg = new RegExp('^[a-zA-Z1-9]{6,15}$');
	if (!$('new_passwd').value.match(ereg)) {
		if ($('erreur_passwd').innerHTML == '') $('unicity_count').value = eval(parseInt($('unicity_count').value) + 1);
		$('erreur_passwd').innerHTML = '<br />Le mot de passe doit conporter entre 6 et 15 caractères parmis a~z, A~Z et 1~9.';
		$('submit').disabled = 'disabled';
	} else {
		if ($('erreur_passwd').innerHTML != '') eval($('unicity_count').value = $('unicity_count').value - 1);
		$('erreur_passwd').innerHTML = '';
		if ($('unicity_count').value == 0) $('submit').disabled = '';
	}
}

function showTestPane(ob) {
	st = ob.style.display;
	newSt = st == 'none' ? 'block' : 'none';
	$('pane_form').style.display = newSt;
}

function selectForm(select) {
	if (select.value != '0' && select.value != '00') {
		document.location = document.location.pathname + '?form=' + select.value;
	} else {
		document.location = document.location.pathname;
	}
}

//-----------------------------------------------------------------------------//

var xpPanel_slideActive = true;	// Slide down/up active?
var xpPanel_slideSpeed = 20;	// Speed of slide
var xpPanel_onlyOneExpandedPane = false;	// Only one pane expanded at a time ?

var menu;
var paneIndex;

var savedActivePane = false;
var savedActiveSub = false;

var xpPanel_currentDirection = new Array();

var cookieNames = new Array();


var currentlyExpandedPane = false;

function initMenu(panelTitles,panelDisplayed,cookieArray)
{
	menu = document.getElementById('menu');
	var divs = menu.getElementsByTagName('DIV');
	paneIndex=0;
	cookieNames = cookieArray;
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='panel'){
			
			var outerContentDiv = document.createElement('DIV');
			var contentDiv = divs[no].getElementsByTagName('DIV')[0];
			outerContentDiv.appendChild(contentDiv);	
		
			outerContentDiv.id = 'paneContent' + paneIndex;
			outerContentDiv.className = 'panelContent';
			var topBar = document.createElement('DIV');
			topBar.onselectstart = cancelXpWidgetEvent;
			var span = document.createElement('SPAN');
			span.innerHTML = panelTitles[paneIndex];
			topBar.appendChild(span);
			topBar.onclick = showHidePaneContent;
			if(document.all)topBar.ondblclick = showHidePaneContent;
			topBar.onmouseover = mouseoverTopbar;
			topBar.onmouseout = mouseoutTopbar;
			topBar.style.position = 'relative';

			var img = document.createElement('IMG');
			img.id = 'showHideButton' + paneIndex;
			img.src = '/images/arrow_up.gif';
			topBar.appendChild(img);
			
			/*if(cookieArray[paneIndex]){
				cookieValue = Get_Cookie(cookieArray[paneIndex]);
				if(cookieValue)panelDisplayed[paneIndex] = cookieValue==1?true:false;
				
			}*/
			
			if(!panelDisplayed[paneIndex]){
				outerContentDiv.style.height = '0px';
				contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px';
				if(document.all)outerContentDiv.style.display='none';
				img.src = '/images/arrow_down.gif';
			}

			topBar.className='topBar';
			divs[no].appendChild(topBar);
			divs[no].appendChild(outerContentDiv);
			paneIndex++;
		}
	}
}

/*
These cookie functions are downloaded from 
http://www.mach5.com/support/analyzer/manual/html/General/CookiesJavaScript.htm
*/	
function Get_Cookie(name) { 
   var start = document.cookie.indexOf(name+"="); 
   var len = start+name.length+1; 
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
   if (start == -1) return null; 
   var end = document.cookie.indexOf(";",len); 
   if (end == -1) end = document.cookie.length; 
   return unescape(document.cookie.substring(len,end)); 
} 
// This function has been slightly modified
function Set_Cookie(name,value,expires,path,domain,secure) { 
	expires = expires * 60*60*24*1000;
	var today = new Date();
	var expires_date = new Date( today.getTime() + (expires) );
	var cookieString = name + "=" +escape(value) + 
	   ( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 
	   ( (path) ? ";path=" + path : "") + 
	   ( (domain) ? ";domain=" + domain : "") + 
	   ( (secure) ? ";secure" : ""); 
	document.cookie = cookieString; 
}

function cancelXpWidgetEvent()
{
	return false;	
	
}

function showHidePaneContent(e,inputObj)
{
	if(!inputObj)inputObj = this;
	
	var img = inputObj.getElementsByTagName('IMG')[0];
	var numericId = img.id.replace(/[^0-9]/g,'');
	var obj = document.getElementById('paneContent' + numericId);
	if(img.src.toLowerCase().indexOf('up')>=0){
		currentlyExpandedPane = false;
		img.src = img.src.replace('up','down');
		if(xpPanel_slideActive){
			obj.style.display='block';
			xpPanel_currentDirection[obj.id] = (xpPanel_slideSpeed*-1);
			slidePane((xpPanel_slideSpeed*-1), obj.id);
		}else{
			obj.style.display='none';
		}
		//if(cookieNames[numericId])Set_Cookie(cookieNames[numericId],'0',100000);
	}else{
		if(this){
			if(currentlyExpandedPane && xpPanel_onlyOneExpandedPane)showHidePaneContent(false,currentlyExpandedPane);
			currentlyExpandedPane = this;	
		}else{
			currentlyExpandedPane = false;
		}
		img.src = img.src.replace('down','up');
		if(xpPanel_slideActive){
			if(document.all){
				obj.style.display='block';
				//obj.style.height = '1px';
			}
			xpPanel_currentDirection[obj.id] = xpPanel_slideSpeed;
			slidePane(xpPanel_slideSpeed,obj.id);
		}else{
			obj.style.display='block';
			subDiv = obj.getElementsByTagName('DIV')[0];
			obj.style.height = subDiv.offsetHeight + 'px';
		}
		//if(cookieNames[numericId])Set_Cookie(cookieNames[numericId],'1',100000);
	}	
	return true;	
}



function slidePane(slideValue,id)
{
	if(slideValue!=xpPanel_currentDirection[id]){
		return false;
	}
	var activePane = document.getElementById(id);
	if(activePane==savedActivePane){
		var subDiv = savedActiveSub;
	}else{
		var subDiv = activePane.getElementsByTagName('DIV')[0];
	}
	savedActivePane = activePane;
	savedActiveSub = subDiv;
	
	var height = activePane.offsetHeight;
	var innerHeight = subDiv.offsetHeight;
	height+=slideValue;
	if(height<0)height=0;
	if(height>innerHeight)height = innerHeight;
	
	if(document.all){
		activePane.style.filter = 'alpha(opacity=' + Math.round((height / subDiv.offsetHeight)*100) + ')';
	}else{
		var opacity = (height / subDiv.offsetHeight);
		if(opacity==0)opacity=0.01;
		if(opacity==1)opacity = 0.99;
		activePane.style.opacity = opacity;
	}			
	
				
	if(slideValue<0){			
		activePane.style.height = height + 'px';
		subDiv.style.top = height - subDiv.offsetHeight + 'px';
		if(height>0){
			setTimeout('slidePane(' + slideValue + ',"' + id + '")',10);
		}else{
			if(document.all)activePane.style.display='none';
		}
	}else{			
		subDiv.style.top = height - subDiv.offsetHeight + 'px';
		activePane.style.height = height + 'px';
		if(height<innerHeight){
			setTimeout('slidePane(' + slideValue + ',"' + id + '")',10);				
		}		
	}
	
	
	
	
}

function mouseoverTopbar()
{
	var img = this.getElementsByTagName('IMG')[0];
	var src = img.src;
	img.src = img.src.replace('.gif','_over.gif');
	
	var span = this.getElementsByTagName('SPAN')[0];
	span.style.color='#428EFF';		
	
}
function mouseoutTopbar()
{
	var img = this.getElementsByTagName('IMG')[0];
	var src = img.src;
	img.src = img.src.replace('_over.gif','.gif');
	
	var span = this.getElementsByTagName('SPAN')[0];
	span.style.color='';
}

function goToPage(url, dest) {
	new Ajax.Updater(
		$(dest),
		url,
		{method: 'get'}
	);
}

function submiteLeForm(leform,leGetForm)
{
	if(document.getElementById('blockeur').value==1)
	{
		alert('Attention, vous venez déjà de valider ce formulaire.');
		document.getElementById('blockeur').value=2;
	}
	else
	{
		document.getElementById('formpdf').action = './?form='+leGetForm+'&valid=1';
		document.getElementById('blockeur').value=1;
	}
}

function move(entity,shop,contrat,page)
{
	if( confirm('Etes-vous sûr de vouloir mettre le contrat à la corbeille ?'))
	{
		goToPage('./?delete='+contrat+'&entite='+entity+'&magasin='+shop,'contract_list');
		goToPage("/administration/contrat/?num_page="+page+"&id_entite="+entity+"&id_magasin="+shop,'contract_list');
	}
}