var Dialogbox =
{
	alert: function(message, myParams)
	{
		this.popup(320, 130);
		var params = {
			title: ( typeof(myParams) == 'object' && typeof(myParams.title) != 'undefined' && myParams.title != '' ) ? myParams.title : 'Information',
			content: '&nbsp;<br /><strong>'+message+'</strong>',
			close_button: '1',
			closeLabel: ( typeof(myParams) == 'object' && typeof(myParams.closeLabel) != 'undefined' && myParams.closeLabel != '' ) ? myParams.closeLabel : 'Fermer'
		}
		this.popup_set_params(params);
	},
	
	debug: function(message, varName)
	{
		if (typeof(astussDebug) == 'undefined' || astussDebug == false) return false;
		myDocument = document;
		if (window.parent.document) myDocument = window.parent.document;
		
		if (typeof(message) == 'object')
		{
			var txt = "Objet : \n";
			if (typeof(varName) != 'undefined')
				var txt = varName+" : \n";
			t_attributs = [];
			for (var n in message)
			{
				if (typeof(message[n]) != 'function' && typeof(message[n]) != 'undefined' && typeof(message[n]) != 'unknown')
				{
					t_attributs.push(n);
				}
			}
			t_attributs = t_attributs.sort();
			for (var i = 0 ; i < t_attributs.length ; i++)
			{
				txt += t_attributs[i]+' -> '+message[t_attributs[i]]+"\n";
			}
			message = txt;
		}
		else if (typeof(message) == 'undefined')
		{
			message = 'undefined';
			if (typeof(varName) != 'undefined')
				message = '"'+varName+'" : '+message;
		}
		else if (typeof(varName) != 'undefined')
			message = varName+' : "'+message+'"';
		else message = ""+message+"";
		
		this.popup_debug(700, 200, myDocument);
		d = new Date();
		var params = {
			title: 'Debug',
			content_concatened: "<strong>Debug</strong> - "+d.toLocaleString()+" : <br />"+message.replace(/\n/g,"<br />")+"<br />",
			close_button: '1',
			overflow: 'scroll'
		}
		this.popup_set_params(params, myDocument);
		
		myDocument.getElementById("div_popup").scrollTop = myDocument.getElementById("div_popup").scrollHeight;
	},
	
	confirm: function(message)
	{
		this.popup(320, 130);
		
		var params = {
			title: 'Question',
			content: '&nbsp;<br /><strong>'+message+'</strong>',
			close_button: '1',
			button:
				{
					callback: 'Dialogbox.onConfirm();', 
					value: 'Ok'
				}
		}
		this.popup_set_params(params);
	},
	
	requete: function(url, params, message, reload, onsuccess, onfailure)
	{
		if (typeof(message) == 'undefined') message = '';
		if (typeof(reload) == 'undefined') reload = '1';
		
		this.bg();
		if ($('div_popup'))
			document.body.removeChild($('div_popup'))
		// Message d'attente
		//popup_width = 450;
		popup_height = 60;
		popup_left = (document.body.offsetWidth) * 3 / 10;
		popup_top = (document.body.offsetHeight - popup_height) / 2;
		d = document.createElement('div');
		d.id = 'div_requete';
		d.className = 'div_attente';
		d.style.width = '40%';
		d.style.height = popup_height + 'px';
		d.style.zIndex = '2000';
		d.style.top = popup_top + 'px';
		d.style.left = popup_left + 'px';
		d.style.position = 'absolute';

		str_div = '<div style="display:inline; width:40%; padding-top:15px; text-align:right;"><img src="../images/indicator.gif" /></div>';
		//str_div += '<div style="display:inline; width:50%; padding-top:15px; margin-left:15px;">'+message+'</div>';

		d.innerHTML = str_div;
		document.body.appendChild(d);
		//alert("Ajax.Request")
		// Requete
		var popup = new Ajax.Request(url, {
			onSuccess:function(req)
			{
				if (typeof(onsuccess) != 'undefined')
				{
          document.body.removeChild($('div_requete'));
					eval(onsuccess);
				}
				else
				{
					if (reload=='1') self.location='index.php';
					else
					{
						//resp = req.responseText.evalJSON(true);
						document.body.removeChild($('bg_popup'));
						document.body.removeChild($('div_requete'));
					}
				}
			},
			onFailure:function(req)
			{
				if (typeof(onfailure) != 'undefined')
				{
					eval(onfailure);
				}
				else
				{
					document.body.removeChild($('bg_popup'));
					document.body.removeChild($('div_requete'));
					Dialogbox.alert('La requete a échoué<br /></strong>Status code : '+req.status+'<br /><font color="#cc0000">'+req.responseText+'</font>');
				}
			},
			method:'post',
			parameters:params
		});
	},
	
	
	onCancel: function()
	{
		this.closePopup();
	},
	
	
	closePopup: function()
	{
		if ($('bg_popup')) document.body.removeChild($('bg_popup'));
		if ($('div_popup')) document.body.removeChild($('div_popup'));
		if ($('div_requete')) document.body.removeChild($('div_requete'));
		this.cacheSelectIE('visible');
	},
	
	
	miniform: function(titre, champs, url, params, close, reload, onsuccess)
	{
		if (typeof(close)=='undefined') close = '1';
		if (typeof(reload)=='undefined') reload = '0';
		
	
		str_form = '<table class="paragraphe" width="100%" cellspacing="2" cellpadding="2">';
		str_params = '';
		
		champs.each(function(champ) {
			// Rajouter select, date ...
			if (!champ['type']) champ['type'] = 'text';
			
			str_form += '<tr><td width="40%" class="DialogBoxLabel">'+champ['libelle']+' ';
			if (champ['libelle'] != '') str_form += ':';
			str_form += '</td>';
			str_form += '<td width="60%">';
			if (!champ['value']) champ['value'] = '';
			
			switch(champ['type'])
			{
				case 'html':
					str_form += champ['value'];
				break;
			
				case 'date':
					str_form += '<input type="text" value="'+champ['value']+'" id="'+champ['champ']+'" size="9" maxlength="10" />';
					str_form += '&nbsp;<input type="button" class="bouton_calendrier" ';
					str_form += 'onclick="popUpCalendar(this,\''+champ['champ']+'\', \'yyyy-mm-dd\');" />'
				break;			
			
				case 'datetime':
					str_form += '<input type="text" value="'+champ['value']+'" id="'+champ['champ']+'" size="9" maxlength="19" />';
					str_form += '&nbsp;<input type="button" class="bouton_calendrier" ';
					str_form += 'onclick="popUpCalendar(this,\''+champ['champ']+'\', \'yyyy-mm-dd 00:00:00\');" />'
				break;
			
				case 'text':
					str_form += '<input type="text" value="'+champ['value']+'" id="'+champ['champ']+'" />';
				break;
				
				case 'password':
					str_form += '<input type="password" value="'+champ['value']+'" id="'+champ['champ']+'" />';
				break;
				
				case 'textarea':
					str_form += '<textarea id="'+champ['champ']+'" />'+champ['value']+'</textarea>';
				break;
				
				case 'select':
					str_form += '<select id="'+champ['champ']+'">';
					if (champ['selection'] && champ['selection'].length>0)
					{
						for (i=0; i<champ['selection'].length; i++)
						{
							selected = (champ['value'] == champ['selection'][i][0]) ? ' selected' : '';
							if (champ['selection'][i][0])
							{
								str_form += '<option value="'+champ['selection'][i][0]+'"'+selected+'>'+champ['selection'][i][1]+'</option>';
							}
							else
							{
								str_form += '<option value="'+champ['selection'][i]+'"'+selected+'>'+champ['selection'][i]+'</option>';
							}
						}
					}
					str_form += '</select>';
				break;
				
				case 'select_checkbox':
					str_form += '<input type="hidden" id="'+champ['champ']+'" />';
					//document.getElementById(champ['champ']).value
					if (champ['selection'] && champ['selection'].length>0)
					{
						for (i=0; i<champ['selection'].length; i++)
						{
							//selected = (champ['value'] == champ['selection'][i][0]) ? ' selected' : '';
							if (champ['selection'][i][0])
							{
								str_form += '<input type="checkbox" id="'+champ['champ']+'_'+champ['selection'][i][0]+'" onclick="Dialogbox.updateCheckboxValue(\''+champ['champ']+'\' ,this);"/>';
								str_form += '&nbsp;' + champ['selection'][i][1] + '<br />';
							}
						}
					}
				break;
			}
			str_form += '</td></tr>';
			if (champ['type'] != 'html')
			{
				str_params += champ['champ']+"='+document.getElementById('"+champ['champ']+"').value+'&";
			}
		});
		
		str_form += '</table>';
		
		this.popup(340, 160);
		var myParams = {
			title: titre,
			content: '&nbsp;<br /><strong>'+str_form+'</strong>',
			close_button: close,
			button:
				{
					callback: "Dialogbox.requete('"+url+"', '"+str_params+''+params+"', '', '"+reload+"');",
					value: ( typeof(params.validerLabel) != 'undefined' && params.validerLabel != '' ) ? params.validerLabel : 'Valider'
				}
		}
		if (typeof(params.closeLabel) != 'undefined')
		{
			myParams.closeLabel = params.closeLabel;
		}
		if (typeof(onsuccess) != 'undefined')
		{
      myParams.button.callback = "Dialogbox.requete('"+url+"', '"+str_params+''+params+"', '', '"+reload+"', '"+onsuccess+"');";
		}
		
		this.popup_set_params(myParams);
	
	},
	
	updateCheckboxValue: function(f, obj)
	{
		fselected = document.getElementById(f).value.split("@");
		separateur = '';
		newvalue = '';
		fd = false;
		for (i = 0; i < fselected.length; i++)
		{
			if (obj.id == fselected[i])
			{
				fd = true;
			}
			else
			{
				newvalue += separateur + fselected[i];
				separateur = '@';
			}
		}
		if (fd == false && obj.checked == true)
		{
			newvalue += separateur + obj.id;
		}
		document.getElementById(f).value = newvalue;
	},
	
	
	bg: function(myDocument)
	{
		if (typeof(myDocument) == 'undefined') myDocument = document;
		if (myDocument.getElementById('bg_popup')) return;
		d = myDocument.createElement('div');
		d.id = 'bg_popup';
		d.style.width = (document.viewport.getWidth())+'px';
		d.style.height = (document.viewport.getHeight()+document.viewport.getScrollOffsets().top)+'px';
		d.style.backgroundColor = '#000';
		d.style.position = 'absolute';
		d.style.zIndex = '1001';
		d.style.top = '0px';
		d.style.left = '0px';
		
		d.style.opacity = '0.4';
		d.style.filter='alpha(opacity=40)';
		myDocument.body.appendChild(d);
		this.cacheSelectIE('hidden', myDocument);
	},
	
	cacheSelectIE: function(visibility)
	{
		if (typeof(myDocument) == 'undefined') myDocument = document;
		// Parcours des elements pour cacher les select
		if (myDocument.all && navigator.appVersion.indexOf("MSIE 7.0") == -1)
		{
			for (f = 0; f < myDocument.forms.length; f++)
			{
				for (i = 0; i < myDocument.forms[f].elements.length; i++)
				{
					
					if (visibility == 'hidden')
					{
						if (myDocument.forms[f].elements[i].type == 'select' ||
							myDocument.forms[f].elements[i].type == 'select-one')
						{
							myDocument.forms[f].elements[i].style.visibility = 'hidden';
							myDocument.forms[f].elements[i].cache = '1';
						}
					}
					else if(visibility == 'visible')
					{
						if (myDocument.forms[f].elements[i].cache == '1')
						{
							myDocument.forms[f].elements[i].style.visibility = 'visible';
							myDocument.forms[f].elements[i].cache = '0';
						}
					}
				}
			}
		}
	},
	
	popup:function(popup_width, popup_height, myDocument)
	{
		if (typeof(myDocument) == 'undefined') myDocument = document;
		this.bg(myDocument);
		if (document.all)
		{
			dw = myDocument.body.offsetWidth;
			dh = myDocument.body.offsetHeight;
		}
		else
		{
			dw = window.innerWidth;
			dh = window.innerHeight;
		}
		popup_left = ((dw - popup_width) / 2)+document.viewport.getScrollOffsets().left;
		popup_top = ((dh - popup_height) / 2)+document.viewport.getScrollOffsets().top;
		d = myDocument.createElement('div');
		d.id = 'div_popup';
		d.style.width = popup_width + 'px';
		d.style.height = popup_height + 'px';
		d.style.zIndex = '1002';
		d.style.top = popup_top + 'px';
		d.style.left = popup_left + 'px';
		d.style.position = 'absolute';
		
		str_div = '<div id="popup_conteneur_titre" style="background-color:#2760a3;">';//'<div style="padding:2px 10px;">';
			str_div += '<div class="titre" id="popup_titre"></div>';
			str_div += '<div style="background-color:#2760a3; position:relative;"></div>';
			str_div += '<div id="div_close_popup" style="visibility:hidden; padding:2px 10px; position:absolute; right:0%; background-color:#2760a3;">';
			str_div += '<a href="javascript:Dialogbox.closePopup();">X</a>';
			str_div += '</div>';
		str_div += '</div>';
		
		str_div += '<div class="contenu" id="popup_contenu"></div>';
		str_div += '<div class="boutons" id="popup_boutons"></div>';
		str_div += '<div class="boutons" style="display:none;" id="popup_footer"></div>';
		
		d.innerHTML = str_div;
		myDocument.body.appendChild(d);
	},
	
	popup_debug:function(popup_width, popup_height, myDocument)
	{
		if (typeof(myDocument) == 'undefined') myDocument = document;
		if (myDocument.getElementById('div_popup')) return;
		this.popup(popup_width, popup_height, myDocument);
		d = myDocument.getElementById('div_popup');
		d.style.top = null;
		d.style.bottom = '0px';
		if (myDocument.getElementById('bg_popup')) myDocument.getElementById('bg_popup').style.display = 'none';
	},
	
	popup_onSuccess: function(req)
	{
		resp = req.responseText.evalJSON(true);
		Dialogbox.popup_set_params(resp)
	},
	
	
	ajax_popup: function(width, height, url, params)
	{
		this.popup(width, height);
		var dbpopup = new Ajax.Request(url, {
			onSuccess:this.popup_onSuccess,
			onFailure:function()
			{
				document.body.removeChild($('bg_popup'));
				document.body.removeChild($('div_popup'));
				Dialogbox.alert('La requete a échoué<br /></strong>Status code : '+req.status+'<br /><font color="#cc0000">'+req.responseText+'</font>');
			},
			method:'post',
			parameters:params
		});
	},

	
	popup_set_params: function(obj, myDocument)
	{
		if (typeof(myDocument) == 'undefined') myDocument = document;
		if (obj['title']) myDocument.getElementById('popup_titre').innerHTML = obj['title'];
		if (typeof(obj.closeLabel) == 'undefined' || obj.closeLabel == '') obj.closeLabel = 'Fermer';
		if (obj['content']) myDocument.getElementById('popup_contenu').innerHTML = obj['content'];
		else if (obj['content_concatened']) myDocument.getElementById('popup_contenu').innerHTML += obj['content_concatened'];
		if (obj['overflow']) myDocument.getElementById('div_popup').style.overflow = obj['overflow'];
		if (obj['close_button'])
		{
			myDocument.getElementById('popup_boutons').innerHTML = '<input class="popup_button" type="button" onclick="Dialogbox.onCancel();" value="'+obj.closeLabel+'" />';
			myDocument.getElementById('div_close_popup').style.visibility = 'visible';
		}
		if (obj['footer'])
		{
			document.getElementById('popup_footer').innerHTML = obj['footer'];
			document.getElementById('popup_footer').style.display = 'block';
			//document.getElementById('popup_footer').style.visibility = 'visible';
		}
		if (obj['button'])
		{
			myDocument.getElementById('popup_boutons').innerHTML += '&nbsp;&nbsp;&nbsp;';
			myDocument.getElementById('popup_boutons').innerHTML += '<input class="popup_button" type="button" onclick="'+obj['button']['callback']+'" value="'+obj['button']['value']+'" />';
		}
		w_c = myDocument.getElementById('popup_contenu').offsetWidth;
		w_p = parseInt(myDocument.getElementById('div_popup').style.width);
		if (w_c > w_p)
		{
			myDocument.getElementById('div_popup').style.left = ((myDocument.body.offsetWidth - w_c) / 2) + 'px';
			myDocument.getElementById('popup_conteneur_titre').style.width = w_c + 'px';
			myDocument.getElementById('popup_boutons').style.width = w_c + 'px';
		}
	}

}
