// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com
function base64_encode(input) 
{
   var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

function base64_decode(input) 
{
   var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

/**
 * @param (Object) o : Yahoo Connecion responce object
 */
function ajax_error(o, isNoError)
{
	if(YAHOO.util.Connect.isCallInProgress(o))
	{
		YAHOO.util.Connect.abort(o);	
	}

	if(!isNoError)
	{
		if (o.status == 0)
		{
			//Serveur Error
	
			alert_web2({width: '210px', height: '110px', msg: 'Server error<br /><br />Refresh your browser or retry later...', alertTitle: 'Server/Connection Troubles', alertImg: 'lib/class/res_yui/container/assets/warn16_1.gif', buttonTitle: 'OK', handleYes: handleYes_loginweb2});
		}
		else if(o.status == -1)
		{
			//Time out...
			var handleYes_loginweb2 = function() { this.hide(); };
					
			alert_web2({width: '210px', height: '110px', msg: 'Connection Timeout<br /><br />Refresh your browser or retry later...', alertTitle: 'Server/Connection Troubles', alertImg: 'lib/class/res_yui/container/assets/warn16_1.gif', buttonTitle: 'OK', handleYes: handleYes_loginweb2});
		}
		else
		{
			//Unknow Error
			
			alert_web2({width: '210px', height: '110px', msg: 'Unknow error<br /><br />Refresh your browser or retry later...', alertTitle: 'Server/Connection Troubles', alertImg: 'lib/class/res_yui/container/assets/warn16_1.gif', buttonTitle: 'OK', handleYes: handleYes_loginweb2});
		}
	}
}


//Alerte JS en Web 2.0 avec soumission d'un form s'il n'y a pas d'erreur
function dialog_web2(e, o)
{
	windowsTitle = o.windowsTitle;
	buttonTXT = o.buttonTXT;	
	ID_form = o.ID_form;
	checkFct = o.checkFct;

	eval('data = ' + checkFct + '();');
	
	if(data === true)
	{
		document.getElementById(ID_form).submit();
	}
	else 
	{
		handleYes = function () { this.hide(); };
	
		
	
		var handleYes = function() { this.hide(); };
		simpledialog1 = new YAHOO.widget.SimpleDialog("simpledialog1", 
			{ 
				width: "400px", 
				fixedcenter: true, 
				visible: false, 
				draggable: true, 
				close: true, 
				text: "<div class=\"dialPopUpWeb2_Div\">" + data + "</div>",
				icon: 'lib/class/res_yui/container/assets/warn16_1.gif', 
				constraintoviewport: true, 
				buttons: [ { text: buttonTXT, handler: handleYes, isDefault: true } ]
			});
			
		simpledialog1.imageRoot = c_chemin;
		simpledialog1.setHeader(windowsTitle);
		simpledialog1.render(document.body);
		simpledialog1.show();
	}
}


//Alerte JS en Web 2.0 (plus simple que dialog_web2 qui fait un submit)
function alert_web2(o)
{
	var simpledialog_alertWeb2 = new YAHOO.widget.SimpleDialog("simpledialog_alertWeb2", 
		{ 
			width: o.width, 
			height: o.height,
			fixedcenter: true, 
			visible: false, 
			draggable: true, 
			close: true, 
			text: "<div class=\"dialPopUpWeb2_Div\">" + o.msg + "</div>",
			icon: o.alertImg, 
			constraintoviewport: true, 
			buttons: [ { text: o.buttonTitle, handler: o.handleYes, isDefault: true } ]
		});
		
	simpledialog_alertWeb2.imageRoot = c_chemin;
	simpledialog_alertWeb2.setHeader(o.alertTitle);
	simpledialog_alertWeb2.render(document.body);
	simpledialog_alertWeb2.show();
}

/**
 * Dit si un element en contient un autre (standard DOM2)
 * @param (String) a : objet conteneur (exemple : id de div)
 * @param (String) b : objet contenu (exemple : id de div / element)
 * @return (Bool) "True" si a contient b. "False" si a ne contient pas b
 * @author gmocquet 
 */   
function f_contains(p_a, p_b)
{
	while(p_b && (p_a!=p_b) && (p_b!=null)) 
	{
		p_b = p_b.parentNode;
	}
	return p_a == p_b;
}