/*******************************************************************/
/*    
/*	QXFRAMEWORK  by QwantiX
/*	
/*******************************************************************/


/*******************************************************/
//						STRING
/////////////////////////////////////////////////////////

String.prototype.nl2br = function ()
{
	regex = /\n/g;
	return this.replace(regex,"<br />");
}
String.prototype.br2nl = function ()
{
	regex = /<br[^>]*>/gi;
	return this.replace(regex,"\n");
}
String.prototype.trim = function ()
{
	return this.replace(/(^\s*)|(\s*$)/g,'');
}
String.prototype.escapeExpReg = function()
{
	var chars = "[]().+*\$^|?";
	var out = this;
	for(var i=0;i<chars.length;i++)
	{
		rx = new RegExp('\\'+chars[i],'g');
		out = out.replace(rx,'\\'+chars[i]); 
	}
	return out;
}
String.prototype.getNbLignes = function()
{
	str = this;
	var regex = /<br[^>]*>|\r\n/gi;
	var tRetour = str.match(regex);
	var nbLgnEstim = Math.floor(str.length/80);
	var out = tRetour?(tRetour.length>nbLgnEstim?tRetour.length+1:nbLgnEstim):nbLgnEstim+1;
	if(out)
		return out;
	return 1;
}

String.prototype.encode64 = function() {
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var input = this;
   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;
}

String.prototype.decode64 = function() {
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var input = this;
   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;
}
String.prototype.encodeHex = function()
{
	var out = '';
	for(var i = 0; i<this.length; i++)
	{
		if(this.charCodeAt(i).dec2hex().length>2)
			out += '#'+this.charCodeAt(i).dec2hex().length;
		out += this.charCodeAt(i).dec2hex();
	}
	return out;
}
String.prototype.decodeHex = function()
{
	var out = '';
	for(var i = 0; i<this.length; i=i+2)
		out += String.fromCharCode((this[i]+this[i+1]).hex2dec());
	return(out);
}
String.prototype.hex2dec = function()
{
	return parseInt(this,16);
}

Number.prototype.dec2hex = function()
{
	var dec = this;
	var hexa="0123456789ABCDEF";
	var hex="";
	while (dec>15) {
		tmp=dec-(Math.floor(dec/16))*16;
		hex=hexa.charAt(tmp)+hex;
		dec=Math.floor(dec/16);
	}
	hex=hexa.charAt(dec)+hex;
	return(hex);
}

Array.prototype.search = function(mixed,opt)
{
	var a = this;
	for(i in a)
	{
		if(opt)
		{
			if(eval('a[i]'+opt+' == mixed'))
				return i;
		}
		else if(a[i] == mixed)
			return i;	

	}
	return false;
}

////////////////////////////////////////////////////////
//Les evenement de souris ne sont pas pris en compte => trop lent
function desallocateEvents()
{
	var elts = document.getElementsByTagName('*');
	events = new Array();
	for(i in elts)
	{
		if(elts[i].nodeName == 'A' || elts[i].nodeName == 'a')
		{
			events.push([elts[i],elts[i].getAttribute('href')]);
			elts[i].removeAttribute('href');
		}
	}
	return events
}
function realocateEvents(events)
{
	var elts = document.getElementsByTagName('*');
	for(i in elts)
	{
		if(elts[i].nodeName == 'A' || elts[i].nodeName == 'a')
		{
			var j = events.search(elts[i], '[0]');
			if(j != false)
				elts[i].setAttribute('href',events[j][1] )
		}
	}	
}

function disableLinks(node)
{
	var elts = node.getElementsByTagName('a');

	for(var i = 0; i<elts.length; i++)
	{
		elts[i]._href = elts[i].getAttribute('href');	
		elts[i].removeAttribute('href');
	}
	return elts;
}
function enabledLinks(elts)
{
	for(var i = 0; i<elts.length; i++)
		elts[i].setAttribute('href',elts[i]._href);
}
function deleteNode(node)
{
	node.parentNode.removeChild(node);
	delete node;
}


////////////////////////////////////////////////////////
function findPos(obj) {
	var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
	return [curleft,curtop];
}

/*******************************************************/
//						AJAX
/////////////////////////////////////////////////////////

function Ajax(callback,o)
{

  if(!o)
  	o = window;
  var xmlhttp = false;
		
  /* Compilation conditionnelle d'IE */
  /*@cc_on
  @if (@_jscript_version >= 5)
     try
     {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e)
     {
        try
        {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
           xmlhttp = false;
        }
     }
  @else
     xmlhttp = false;
  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
  {
     try
     {
        xmlhttp = new XMLHttpRequest();
     }
     catch (e)
     {
        xmlhttp = false;
     }
  }

  if (xmlhttp)
  {
     xmlhttp.onreadystatechange=function()
     {
        if (xmlhttp.readyState == 4) /* 4 : état "complete" */
        {
           if (xmlhttp.status == 200 && callback) /* 200 : code HTTP pour OK */
           {
			if(typeof(callback) == 'function')
			 	callback(xmlhttp.responseXML,xmlhttp.responseText)
			else	
				o[callback](xmlhttp.responseXML,xmlhttp.responseText);
           }else if(callback)
		   {
			if(typeof(callback) == 'function')
			 	callback();
			else	
				o[callback]();
		   }
        }
     }
  }
  return xmlhttp;
}
///////////////////////////////////////////////////
// Classe table
//////////////////////////////////////////////////
function Node(type)
{
	return document.createElement(type);
}

function Table(rows,cols)
{
	this.cells = new Array();
	this.rows = new Array();
	this.table = document.createElement('table');
	for(var i = 0; i<rows; i++)
	{
		var row =  this.appendRow(cols);
		this.table.appendChild(row[0]);
		this.cells.push(row[1]);
		this.rows.push(row[0]);
	}	
}
Table.prototype = {
	appendRow : function(cols)
	{
		var row = document.createElement('tr');
		var cells = new Array();
		for(var i = 0; i<cols; i++)
		{
			var cell = this.createCell();
			row.appendChild(cell);
			cells.push(cell)
		}
		return [row,cells];
	},
	createCell : function()
	{
		return document.createElement('td');
	}


}
//////////////////////////////////////////////////
function Input(type, name, id, value)
{
	var input = document.createElement('input');
	input.setAttribute('type',type);
	input.setAttribute('name',name);
	input.setAttribute('id',name);
	input.setAttribute('value',value);
	return input;
}
