var _tabElement = new Array();
function _gel(element)
{
	if(!_tabElement[element])
	{
		_tabElement[element] = document.getElementById(element);
	}
	
	return _tabElement[element];
}

function _del(element)
{
	if(_gel(element).parentNode.removeChild)
	{
		_gel(element).parentNode.removeChild(_gel(element));
		_tabElement[element] = null;
		return true;
	}
	
	return false;
}


/*
//Permet d ajouter simplement des elements dans le dom
//@param e type de noeud a crée
//@param c contenu texte du noeud
//@param a tableau d'attributs
//@param p noeud dom dans lequel on crée le nouveau noeud
//@param x remplacer les noeuds ou crée un nouveau noeud
//http://pawel.saikko.com/domel-easier-dom-manipulation,20060325
*/
var domEl = function(e,c,a,p,x) 
{
	if(e||c) 
	{
	     c=(typeof c=='string'||(typeof c=='object'&&!c.length))?[c]:c;   
	     e=(!e&&c.length==1)?document.createTextNode(c[0]):e;   
	     var n = (typeof e=='string')?document.createElement(e) : !(e&&e===c[0])?e.cloneNode(false):e.cloneNode(true);   
	     if(e.nodeType!=3) 
		  {
	         c[0]===e?c[0]='':'';
	         for(var i=0,j=c.length;i<j;i++) typeof c[i]=='string'? n.appendChild(document.createTextNode(c[i]))  : n.appendChild(c[i].cloneNode(true));
	         if(a) {for(var i=(a.length-1);i>=0;i--) a[i][0]=='class' ? n.className=a[i][1] : n.setAttribute(a[i][0],a[i][1]);}
	     }
	}
	
	if(!p)return n;
	p=(typeof p=='object'&&!p.length)?[p]:p;
	for(var i=(p.length-1);i>=0;i--) 
	{
	   if(x) { while(p[i].firstChild) p[i].removeChild(p[i].firstChild);
	       if(!e&&!c&&p[i].parentNode) p[i].parentNode.removeChild(p[i]);}
	   if(n) p[i].appendChild(n.cloneNode(true));
	}    
}


function inArray(arr,elt)
{
    for(var i=0;i<arr.length;i++)
    {
        if(arr[i] == elt)
            return true;
    }
    return false;
}



function buildColorPicker(elt_color,elt_txt)
{
	id = Math.round(100*Math.random());
	html = '<table border="0" id="'+id+'" style="background:#dfdfdf;border:1px solid silver;position:absolute;" cellpadding="0" cellspacing="0"><tr>';
	h=new Array('00','33','66','99','cc','ff');
	u=0;
	for(var r=0;r<h.length;r++) 
	{
		if(u==2)
		{
			html += '</tr><tr>';
			u=0;
		}
		u++;
		html += '<td>';
		html += '<table border="0">';
		for(var g=0;g<h.length;g++) 
		{ 
			html += '<tr>';
			for(var b=0;b<h.length;b++) 
			{
				html += '<td><div style="width:20px;height:20px;background:#'+h[r]+h[g]+h[b]+'" onclick="ColorPicker_Click('+id+',\'#'+h[r]+h[g]+h[b]+'\',\''+elt_color+'\',\''+elt_txt+'\');" onmouseover=""></div></td>';			
			}
			html += '</tr>';
		}
		
		html += '</table>';
		html += '</td>';
	
		
	}
	html += '</tr></table>';
	
	return html;
}

function ColorPicker_Click(id,color,elt_color,elt_txt)
{
	if(_gel(elt_color))
	{
		_gel(elt_color).style.backgroundColor = color;
	}
	
	if(_gel(elt_txt))
	{
		if(_gel(elt_txt).nodeName=='INPUT')
		{
			_gel(elt_txt).value = color.toUpperCase().replace('#','');
		}else{
			_gel(elt_txt).innerHTML = color.toUpperCase().replace('#','');
		}
	}
	
	_del(id);
}


function tableTrColorOver(id,color)
{
    mousedown = false;
    table = document.getElementById(id);
    if(table)
    {
        trs = table.getElementsByTagName('TR');
        for(var i=0;i<trs.length;i++)
        {
            trs[i].oldcolor = trs[i].style.background;
            trs[i].clicked = false;
            trs[i].onmouseover = function(){
                    if(!this.clicked)
                    {
                        if(mousedown)
                        {
                            this.onclick();
                        }else{
                            this.style.background = color;
                        }
                    }
                }
            trs[i].onmouseout = function(){
                if(!this.clicked)
                {
                    this.style.background = this.oldcolor;
                    if(mousedown)
                    {
                       this.onclick(); 
                    }
                }
            }
            
            trs[i].onclick = function(){
                if(!this.clicked)
                {
                    this.style.background = color;
                    this.clicked = true;
                }else{
                    this.clicked = false;
                    this.style.background = this.oldcolor;
                }
            }
            
            trs[i].onmousedown = function(){
                mousedown = true;   
            }
            
            trs[i].onmouseup = function(){
                mousedown = false;   
            }
        }
    }
}

function XHRObj(url, callback) {
    var req = init();
    req.onreadystatechange = processRequest;
        
    function init() {
      if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    
    function processRequest () {
      if (req.readyState == 4) {
        if (req.status == 200) {
          if (callback) callback(req.responseText,req.responseXML);
        }
      }
    }

    this.doGet = function() {
      req.open("GET", url, true);
      req.send(null);
    }
    
    this.doPost = function(body) {
      req.open("POST", url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send(body);
    }
}

function close_all()
{
  document.getElementById('conteneur').parentNode.removeChild(document.getElementById('conteneur'));
  document.getElementById('fondtranslucide').parentNode.removeChild(document.getElementById('fondtranslucide'));
}
    


function phpads_pop(titre,elem,width,height,but,redirect,param,email,url)
{
    height2=height/2;
    width2=width/2;

    var fenetre = document.createElement("div");
		

    function getInfo() {
    
        this.init = function () {
        
            if ((document.compatMode) && (document.compatMode != 'BackCompat')) {
		        this.docHTML = document.documentElement;
		    }
		    else {
		        this.docHTML = document.body;
		    }
        };
        this.init();
        
        this.getAvailWidth = function () {
        
            if (this.docHTML.clientWidth <= this.docHTML.scrollWidth) {
                return this.docHTML.scrollWidth;
            }
            else {
                return this.docHTML.clientWidth;
            }
        };
        
        this.getAvailHeight = function () {
        
            if (this.docHTML.clientHeight <= this.docHTML.scrollHeight) {
                return this.docHTML.scrollHeight;
            }
            else {
                return this.docHTML.clientHeight;
            }
        };
        
    };
    if (typeof this.tt == 'undefined') {
    
        this.tt = new getInfo();
    }
    
    fenetre.style.left='0px';
    fenetre.style.top='0px';
		var max_width=this.tt.getAvailWidth();
		var max_height=this.tt.getAvailHeight();
    fenetre.style.width=max_width; //'100%';
    fenetre.style.height=max_height; //'100%';
    fenetre.id="fondtranslucide";
		
  //  fenetre.setAttribute("onclick",function(){fenetre.parentNode.removeChild(fenetre);fenetre2.parentNode.removeChild(fenetre2);});
	//	fenetre.onclick=function(){fenetre.parentNode.removeChild(fenetre);fenetre2.parentNode.removeChild(fenetre2);};

		var fenetre2 = document.createElement("div");
		fenetre2.style.width=width+'px';
	
		height3=height*1.05;
    fenetre2.style.height=height3+'px';
    fenetre2.style.marginTop=-height2;
    fenetre2.style.marginLeft=-width2;
		//fenetre2.style.overflow='auto';
    fenetre2.id="conteneur";

    if(redirect=='no')
    {
      ajout='<td style="text-align:center;"><input class="bt_login" style="width:'+width2+'" type="button" value="OK" onclick="close_all();" ></td>';
    }
		else
		{
	    ajout='<td style="width:'+width2+';text-align:left;"  colspan=2><input class="bt_login" style="width:'+width2+'" type="button" value="OK" onclick="window.open(\''+redirect+'\',target=\'_self\')" ></td>';
		}
		
    if(but==2)
    {
      ajout2='<td><input class="bt_login" style="width:100%;" type="button" value="ANNULER" onclick="close_all();"></td>'
    }
		else
		{
		  ajout2='';
		}
		if(but==1)
		{
		  rajout_but='<td>&nbsp;&nbsp;</td>';
		}
		else
		{
			rajout_but='<td width="16px;" style="vertical-align:middle;cursor:pointer;" onclick="close_all();"><img src="http://adsmarketplace.com/admin/images/error_16.png"></td>';
		}
		var browserName=navigator.appName; 
		height_div=height-50;
		height_tab=height-40;
		width_tab=width-7;
		
    if (browserName=="Microsoft Internet Explorer")
		{
		  width_div=width-10;
      var tab_recup = document.getElementsByTagName("select"); 
      for(i=0;i<tab_recup.length;i++)
			{
			  tab_recup[i].style.visibility='hidden';
			} 
		}
		else
		{
		  width_div=width-2;
		}

		if(titre=='Inscription Validée')
		{
			
			var conversion='<IFRAME frameborder=0 width=340 height=30  src="http://adsmarketplace.advertstream.com/test_conv.php?param='+param+'&email='+email+'&url='+url+'"></IFRAME>';
			
		}
		else
		{
		  var conversion='';
		}

		
    fenetre2.innerHTML='<table id="fond_pop" style="overflow-x:scroll;overflow-y:auto;width:'+width_tab+';height:'+height_tab+';border-collapse:collapse;padding:2px;background-image:url(http://adsmarketplace.com/admin/interfaces/images/fond_pop.gif);">'
                			   +'<tr valign="top" style="height:20px;background-image:url(http://adsmarketplace.com/admin/interfaces/images/bg_bt_go2.jpg);background-repeat:repeat-x;">'
                				 +'<td align="left" colspan=2 style="color:#fff;font-weight: bold;">&nbsp;'+titre+'</td>'+rajout_but
                				 +'</tr>'
                				 +'<tr>'
                				 +'<td colspan=3><div style="overflow:auto;height:'+height_div+';width:'+width_div+';">'+elem+'</div></td>'
                				 +'</tr>'
                				 +'<tr valign="bottom">'
												 +ajout
												 +ajout2													 										 
												 +'</tr>'
												 +'<table id="moi"><tr><td>'+conversion+'</td></tr></table>'
                			 +'</table>'
											 
											 	
											 
    		
    document.body.appendChild(fenetre);
		document.body.appendChild(fenetre2);

}
