function criaajax()
{
  try {
	    ajax = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e) {
	try {
	ajax = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(ex) {
	try {
	ajax = new XMLHttpRequest();
	}
	catch(exc) {
	alert("Esse browser não tem recursos para uso do Ajax");
	ajax = null;
	            }
	         }
	}
return ajax;//se tiver suporte ajax
}

var usando=false;
var fila = new Array();

function gera_url(objform)
{

  var parametros= new Array();
   for(var i=0;i<objform.elements.length;i++)
   {
    if((objform.elements[i].type!='radio' && objform.elements[i].type!='checkbox') ||  objform.elements[i].checked==true)
	{
	  parametro=encodeURIComponent(objform.elements[i].name);
      parametro+="=";
      parametro+=encodeURIComponent(objform.elements[i].value);
      parametros.push(parametro)
	}
   }
   return  parametros.join("&");
}
function limpa_form(objform)
{
   var parametros= new Array();
   for(var i=0;i<objform.elements.length-1;i++)
   {
      objform.elements[i].value="";
   }
}

function $_(id)
    {
       return document.getElementById(id);
    }
function $_tag(id)
	{
		return document.getElementsByTagName(id);	
	}
// JavaScript Document
function inpetora(dad,div,url)
{
  fila.push([dad,div,url]);
  if(fila.length > 0) chama_fila();
}
function chama_fila()
{
  if(usando==false)
  {
    var current = fila.shift();
    envia_dados(current[0],current[1],current[2]);
  }
  else if(fila.length > 0)
  {
   setTimeout(chama_fila, 100);
  }

}
function envia_dados(dad,div,url,retorno)
 {

   if(usando==false)
   {
    usando=true;
     var ajax=new criaajax();

	        if(ajax) {
                            r=$_(div);
                            this.ajax.open("POST", url, true);
                            this.ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded ;");
                            this.ajax.onreadystatechange = function() {
            			            if(ajax.readyState == 4 ) {

										if(ajax.status==200)
                                        {
                                           r.innerHTML=ajax.responseText;
                                           if(retorno)
                                           retorno.value=ajax.responseText.replace(" ","");
                                           esconde_processo();
                                        }
                                        usando=false;
                                        }
            					   }
			                 }
			       this.ajax.send(dad);
    }
    else
    {
  //   alert(usando+dad);
     if(retorno)
         setTimeout("envia_dados(dad,div,url,retorno)",10);
       else
         setTimeout("envia_dados(dad,div,url)",10);
    }
 }

function envia_dados_select(dad,div,url,retorno)
 {
 if(usando==false)
   {
    usando=true;
    var ajax=new criaajax();
	        if(ajax) {
                            r=$_(div);
                            this.ajax.open("POST", url, true);
                            this.ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded;");
                            this.ajax.onreadystatechange = function() {
            			            if(ajax.readyState == 4 ) {
										if(ajax.status==200)
                                        {
                                           values=ajax.responseText;
                                           texto=values.split(';')
                                           valores=texto;
                                           cria_select(div,valores,texto);
                                           if(retorno)
                                           retorno.value=ajax.responseText.replace(" ","");
                                           esconde_processo();
                                        }
                                      usando=false;
                                        }
            					   }
			                 }
			       this.ajax.send(dad);
    }
/*    else
    {

    if(retorno)
         setTimeout("envia_dados(dad,div,url,retorno)",100);
       else
        setTimeout("envia_dados(dad,div,url)",100);
    }
    */
 }

function cria_select(id,values,texto)
{
     a=document.getElementById(id);
     document.getElementById(id).length=0
   for(var i=0; i <values.length; i++)
        {
		   var oOption = document.createElement("OPTION");
		   oOption.value=values[i];
		   oOption.text=texto[i];
            a.options.add(oOption)
		}
    }
	
function transfereItem(idLis){
	var transferir = new Array();
	var contador = 0;
    	var oldList = document.getElementById(idLis);
	var opcoes = oldList.getElementsByTagName("option");
	// Pega as opcoes selecionadas
	for (var i = 0; i < opcoes.length; i++) {
		if (opcoes[i].selected == true) {
			transferir[contador] = opcoes[i].value;
			contador++;
		}
	}
  return transferir;
}

function limpa_selecao(id)
    {
  	  var oldList = document.getElementById(id);
      var opcoes = oldList.getElementsByTagName("option");
     for (var i = 0; i < opcoes.length; i++) {
		if (opcoes[i].selected == true) {
            opcoes[i].selected =false;
		}
}
}

function seleciona(id,dado)
    {
  	  var oldList = document.getElementById(id);
      var opcoes = oldList.getElementsByTagName("option");
     for (var i = 0; i < opcoes.length; i++) {
		if (opcoes[i].value== dado) {
            opcoes[i].selected =true;
		}
     }
}
//-----------------------------------class ajax--------------------------------------------------------------
function class_ajax()
{
  this.usando=false;// JavaScript Document
function busca_categorias()
{
	Ajax.url="categorias_produdos_xml.php";
	Ajax.susseso=function (){
	  processa_categoria(Ajax.getresposta('xml'));
	}
	Ajax.envia();
}
  this.fila=new Array();
  this.susseso=function (){};
  this.erro=null;
  this.ajax=null;
  this.rechama=false;
  this.url="";
  this.dados=null;
  this.metodo="POST";
  this.camafilad=function(rechama)
	   {
	   if(rechama==true)
		   {
			 //Ajax.rechama=false;
			 Ajax.camafilad();
  	       }
		   else
		   {

			if(this.ajax==null)
			   this.cria();
			  if(this.usando==false)
			  {
			    if(this.fila.length > 0){
				var current = this.fila.shift();
			    this.envia_ajax(current[0],current[1],current[2]);
			    }
			  }
			  else if(this.fila.length > 0)
			  {
				 this.rechama=true;
				 setTimeout("Ajax.camafilad(true)",100);
				 
			  }
		}//	   if(Ajax.rechama==true)			
	   }
  this.inpetora=function (dad,url,susseso)
		{
		  this.fila.push([dad,url,susseso]);
		  	  if(this.fila.length > 0)
		  	     this.camafilad();
	  }  
  this.getresposta=function (tipo){
    if(tipo=='xml')
     return this.ajax.responseXML;
    else
     return  this.ajax.responseText;
  };
  this.gera_dados=function(objform)
             {
               var parametros= new Array();
                for(var i=0;i<objform.elements.length;i++)
                    {
	  				if(objform.elements[i].type=="select-multiple")
					{
                    	  opcoes=objform.elements[i].options;
			       for (var j = 0; j< opcoes.length; j++) {
						if (opcoes[j].selected == true) {
											  parametro=encodeURIComponent(objform.elements[i].name);
											  parametro+="=";
											  parametro+=encodeURIComponent(opcoes[j].value);
											  parametros.push(parametro) 
						  }
						  
					}					  
					}else if((objform.elements[i].type!='radio' && objform.elements[i].type!='checkbox') ||  objform.elements[i].checked==true)
                     	{
                    	  parametro=encodeURIComponent(objform.elements[i].name);
                          parametro+="=";
                          parametro+=encodeURIComponent(objform.elements[i].value);
                          parametros.push(parametro)
                	    }
                    }
                     this.dados=parametros.join("&");
                 return  this.dados;
             }
  this.cria=function()
    {
		 try {
	   			this.ajax = new ActiveXObject("Microsoft.XMLHTTP");
	      	}
    	catch(e) {
    	try {
    			this.ajax = new ActiveXObject("Msxml2.XMLHTTP");
    	}
    	catch(ex) {
    	try {
    			this.ajax = new XMLHttpRequest();
    	}
    	catch(exc) {
    			alert("Esse browser não tem recursos para uso do Ajax");
    	this.ajax = null;
	            }
	         }
	}
	 this.usando=false;
    //this.ajax.susseso=this.susseso;
	 return this.ajax;//se tiver suporte ajax

    }

this.envia=function()
  {
    
	this.inpetora(this.dados,this.url,this.susseso)
  }
this.envia_ajax=function (dados,urls,susseso_ajax)
     {
	     if(this.ajax==null)
		   	this.cria();
		 if(this.usando==false)
           {
			  this.usando=true;
			 try{ 
					  $_('body').style.cursor="wait";
			   }catch(e)
			   		  {
					  }
    	        if(this.ajax) {
                               this.ajax.open(this.metodo,urls, true);
                               this.ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded;");
                               this.ajax.onreadystatechange = function() {
            			            if(Ajax.ajax.readyState == 4 ) {
		
                                        if(Ajax.ajax.status==200)
                                            {
											 if(susseso_ajax())
                                                 {
                                                    susseso_ajax();
                                                    
                                                 }
												Ajax.usando=false;//ajax.usando
										 try{ 
											  $_('body').style.cursor="";
										   }catch(e)
												  {
												  }

                                                return true;
                                            }
                                            else
                                            {
                                              Ajax.usando=false; //ajax.usando                                    
											  return false;
                                            }
							              }
            					   }
			                 }
			       this.ajax.send(dados);
            }
            else
            {
               this.inpetora(dados,urls);//fila.push([dad,div,url]); 
            }
     }
 
}
Ajax=new class_ajax();//istacia a classe ajax na memoria iss e ncessario
//-----------------------------------------------------fim class---------------------------------------------------

function get_value_xml(xml,nome,numero)
{
 try{
	     return xml.getElementsByTagName(nome)[numero].firstChild.nodeValue;
	}catch(e)
	{
	  alert('Erro no xml');	
	  return false;
	}	
}
function get_arvore_xml(xml,nome,numero)
{
  return xml.getElementsByTagName(nome)[numero];	
}
function get_length_xml(xml,nome)
{
    try{
	  return xml.getElementsByTagName(nome).length;
	}catch(e)
	{
	  alert('Erro no xml');	
	  return false;
	}
}
//----------------------------class------------------------------
//----------------------------------------------------
//propriendade dos movimentos na tela

var click=false;
var x=0;
var y=0;
var px=0;
var py=0;
function move(idd,minimize)
{
// document.getElementById(idd).style.position="";
function muda_div(e)
 {
    document.onmousemove=muda_div;
  if(!e) e = window.event;
   if(click)
   {
      document.getElementById(idd).style.left=((e.clientX-x)+px)+"px";
      document.getElementById(idd).style.top=((e.clientY-y)+py)+"px";
   }
   else
   {
     click=true;
     x=e.clientX;
     y=e.clientY;
     px=parseInt(document.getElementById(idd).style.left);
     py=parseInt(document.getElementById(idd).style.top);

   }
 }
 document.getElementById(idd).onmousedown=muda_div;
 document.onmouseup=function()
 {
    document.onmousemove=function()
    {
          click=false;
        //  x=0;
//          y=0;
    };
 }
function minimize(id_m)
{

   if(!$_(id_m).minimizado)
   {
     $_(id_m).style.height="20px";
     $_(id_m).style.overflow="auto";
     $_(id_m).minimizado=true;
   }
    else
    {
     //$_(id_m).style.height="100px";
     $_(id_m).style.overflow="";
     $_(id_m).minimizado=false;
    }
}
//minimize
if(minimize==true)
 {
	$_(idd).ondblclick=function ()
	{
	
	    minimize(idd)
	}
  }
}
function retira(pai,id) {
        div=$_(id);
		try{
            $_(pai).removeChild(div);
        }
        catch(e){}
    }
    
function Cria_div(pai,id,left,top,html,width,height)
{
  if($_(id)==null)
  {	     	
   div=document.createElement("div");
   opai=$_(pai);
   div.style.left=left+"px";
   div.style.top=top+"px";
   div.style.position="relative";
   if(height)
    {
	  div.style.height=height;	
	}
   if(width)
    {
	  div.style.width=width;	
	}	
   div.style.zIndex=100;
   div.innerHTML="<div id=\"fechar_pop\" style=\"position:absolut; left: 70%; top: 1px; width: 25px; background-color:#FF9933; cursor:pointer\"><a onClick=\"retira('"+pai+"','"+id+"');\">x</a></div>"+html;
   div.id=id;
   opai.appendChild(div);
   move(id);
  }
  else
  {
       alert('Não e possuvel abrir duas janelas ao mesmo tempo');	
  }
}
function getElementsByClassName(clase){
var x = document.getElementsByTagName('div');
var y = new Array();
for (i=0; i<x.length; i++)
  if (x[i].attributes['class'] && x[i].attributes['class'].value == clase)
    y.push(x[i]);
  return y;
}
