var xmlhttp; var CadastroInfo; var local; var acao='update'; var atualizado=false; 
try{xmlhttp=new XMLHttpRequest();}catch(ee){try{xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(E){xmlhttp=false;}}}
fila=[]; ifila=0;

function loader_show(str_msg) { 
	if (document.getElementById('loader')) {
		var l  = document.getElementById('loader');
		var lt = document.getElementById('loader_txt');
		if (str_msg && str_msg != "") {
			lt.innerHTML = str_msg;
		} else { document.getElementById('loader_txt').innerHTML = 'Aguarde...'; }
		l.style.visibility = "visible"; 
		l.style.left = ((document.body.offsetWidth)/2)-(75);
		l.style.top  = ((document.body.offsetHeight)/2)-(25);
	} else {
		return false;	
	}
}
function loader_hide() {
	if (document.getElementById('loader')) {
		document.getElementById('loader').style.visibility = "hidden";
	}
}

function ajaxHTML(id,url){ loader_show(); fila[fila.length]=[id,url];if((ifila+1)==fila.length)ajaxRun()}
function ajaxRun(){
    xmlhttp.open("GET",fila[ifila][1],true);
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
			loader_hide();
			retorno=unescape(xmlhttp.responseText);
			document.getElementById(fila[ifila][0]).innerHTML=retorno;
            ifila++
            if(ifila<fila.length)setTimeout("ajaxRun()",20)
        }
    }
    xmlhttp.send(null)
}
function getCadastroInfo(){
	for (var i in CadastroInfo) {
		document.getElementById('CadastroInfo_'+i).value=CadastroInfo[i];
	}
}
function setCadastroInfo(){for (var i in CadastroInfo) {document.getElementById('CadastroInfo_'+i).value='';}}
function ajaxLogin(x){
	var cPost='';
	document.getElementById('CadastroInfo').style.display='none';
	if (x==1) {
		getCadastroInfo();
		return true;
		usuario=senha=1;
	} else {
		usuario=document.getElementById('usuario').value;
		senha=document.getElementById('senha').value;
	}
	document.getElementById('resposta').innerHTML="Aguarde...";
	//url='getCadastroInfo.php?usuario='+usuario+'&senha='+senha;
	url='getCadastroInfo.php?acao=login';
	cPost='usuario='+usuario+'&senha='+senha;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
			retorno=unescape(xmlhttp.responseText);
			local=new Function("return "+retorno)();
			//document.getElementById('resposta').innerHTML=retorno;
			//alert("Status: "+local.status+' - '+local.status_text);
			document.getElementById('resposta').innerHTML=local.status_text;
			if (local.status==0) {
				alert(local.status_text);
				return false;
			}
			document.getElementById('CadastroInfo').style.display='list-item';
			document.getElementById('CadastroInfoNovo').style.display='none';
			document.getElementById('CadastroInfoSenha').style.display='none';
			CadastroInfo=local.CadastroInfo;
			getCadastroInfo();
		}
	}
    xmlhttp.send(cPost);
}

function ajaxLogoff(){
	var cPost='';
	//ajaxHTML('listaprodutos','cadastro,3.inc.php');
	document.getElementById('CadastroInfo').style.display='none';
	document.getElementById('CadastroLogin').style.display='list-item';
	document.getElementById('CadastroInfoNovo').style.display='list-item';
	document.getElementById('CadastroInfoSenha').style.display='list-item';
	document.getElementById('resposta').innerHTML="Aguarde...";
	url='getCadastroInfo.php?acao=logoff';
	xmlhttp.open("GET",url,true);
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
			retorno=xmlhttp.responseText;
			local=new Function("return "+retorno)();
			document.getElementById('resposta').innerHTML=local.status_text;
			//alert(local.status_text);
			return true;
		}
	}
    xmlhttp.send(null);
}

function ajaxTrocaSenha(){
	var cPost='';
	var senha=prompt('Digite a senha atual','');
	var novasenha=prompt('Digite a nova senha','');
	var novasenha2=prompt('Confirme a nova senha','');
	while (novasenha!=novasenha2) {
		alert('Senhas digitadas são diferentes!');
		novasenha=prompt('Digite a nova senha',novasenha);
		novasenha2=prompt('Confirme a nova senha',novasenha2);
	}
	if ((senha=='')||(novasenha=='')) {
		alert('Nenhuma das senhas pode ser vazia!');
		return false;
	}
	document.getElementById('resposta').innerHTML="Aguarde...";
	//url='getCadastroInfo.php?acao=senha&senha='+senha+'&novasenha='+novasenha;
	url='getCadastroInfo.php?acao=senha';
	cPost='senha='+senha+'&novasenha='+novasenha;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
			retorno=unescape(xmlhttp.responseText);
			local=new Function("return "+retorno)();
			document.getElementById('resposta').innerHTML=local.status_text;
			if (local.status==0) {
				alert(local.status_text);
				return false;
			}
			alert(local.status_text);
			return true;
		}
	}
    xmlhttp.send(cPost);
}

function ajaxLembraSenha(){
	var cPost='';
	document.getElementById('resposta').innerHTML="Aguarde...";
	usuario	= document.getElementById('senhaemail').value;
	url='getCadastroInfo.php?acao=lembrete';
	cPost='usuario='+usuario;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
			retorno=unescape(xmlhttp.responseText);
			local=new Function("return "+retorno)();
			document.getElementById('resposta').innerHTML=local.status_text;
			if (local.status==0) {
				alert(local.status_text);
				return false;
			}
			alert(local.status_text);
			return true;
		}
	}
    xmlhttp.send(cPost);
}

function ajaxCadastroInfoUpdate(form,dados,acao){
	var sDados='';
	document.getElementById('resposta').innerHTML="Aguarde...";
	elements=document.getElementById(form);
	for (var i=0; i < (elements.length); i++) {
		c=elements[i].name;
		v=elements[i].value;
		id=elements[i].id;
		if ((c.indexOf('!')==0)&&(v=='')) {
			erro=('Campo "'+c.substr(14)+'" de preenchimento obrigatório!');
			document.getElementById('resposta').innerHTML=erro;
			alert(erro);
			document.getElementById(id).focus();
			return false;
		}
		if (id.indexOf(dados)==0) {
			sDados += id + '=' + v + '&';
		}
	}
	url='getCadastroInfo.php?acao='+acao;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
			retorno=xmlhttp.responseText;
			local=new Function("return "+retorno)();
			document.getElementById('resposta').innerHTML=local.status_text;
			if (local.status==0) {
				alert(local.status_text);
				atualizado=false;
				return false;
			}
			alert(local.status_text);
			atualizado=true;
			return true;
		}
	}
    xmlhttp.send(sDados);
}

function ajaxEnquete(enquete,opcao) {
	document.getElementById('enqueteId').innerHTML="<span class='aguarde'>Aguarde...</span>";
	url='getEnqueteInfo.php?enquete='+enquete+'&opcao='+opcao;
	xmlhttp.open("GET",url,true);
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
			document.getElementById('enqueteId').innerHTML=xmlhttp.responseText;
			return true;
		}
	}
    xmlhttp.send(null);
}

function makeRequest(action, method, parameters, phpfile) {
	  //alert('makeRequest('+action+','+method+', '+parameters+')');
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
		 //Oculta('WaitWindow');
         return false;
      }
	  //alert("parameters: "+parameters);
	  //qs = "?acao="+action;
	  //qs = '?time='+Math.random();
	  qs = '';
	  if (method == 'GET') {
		if (parameters.substring(0,1)!='?')
		  	qs = '?' + parameters; 
		else 
		  qs = parameters;
	  }
	  if (!phpfile || phpfile == '') {
		  phpfile = gateway_url;
	  }
	  //alert('qs:'+phpfile+qs);
	  http_request.open(method, phpfile + qs, true);
	  http_request.onreadystatechange = function() {
		   if (http_request.readyState==4) {
				retorno=unescape(http_request.responseText);
				document.getElementById(action).innerHTML=retorno;
				//Oculta('WaitWindow');
				return true;
		   }
	  }
	  if (method == "POST") {
		 http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	  	 http_request.send(parameters);
	  } else {
		  http_request.send(null);
	  }
}
   
function SubmitForm(form, action, method, phpfile) {
	  //Mostra('WaitWindow');
	  if (!phpfile) phpfile='';
	  var enctype = form.enctype;
	  //alert(enctype);
	  var getstr = "";
      for (i=0; i<form.elements.length; i++) {
		 	var type = form.elements[i].type;
			if (type) {
				var name = form.elements[i].name.replace('!','');
				//if (type != "checkbox") alert('type: '+type);
				
				if (type == "text" || type == "hidden" || type == "password" || type == "textarea") {
				   getstr += name + "=" + form.elements[i].value + "&";
				}
				
				if (type == "checkbox") {
				   if (form.elements[i].checked) {
					  getstr += name + "=" + form.elements[i].value + "&";
				   }
				}
				
				if (type == "radio") {
				   if (form.elements[i].checked) {
					  getstr += name + "=" + form.elements[i].value + "&";
				   }
				}
			  
				if (type == "select" || type == "select-one") {
					getstr += name + "=" + form.elements[i].options[form.elements[i].selectedIndex].value + "&";
				}
			}
         
      }
	  //alert(action+', '+ method+', '+ getstr +', ' + enctype);
      makeRequest(action, method, getstr, phpfile);
}

function preenche_combo(combo, remote_action, post_vars, default_value) {
	
	var ajax_gw = '/webloja/includes/gw.cadastro.ajax.php';
	var cb = document.getElementById(combo);
	//var msg_carregando = document.getElementById('msg_carregando');
	
	 if (!cb) {
		 var cb = document.form1[cb];
	 }
	
	var str_url = ajax_gw+"?acao="+remote_action;
	
	if (post_vars) {
		for (var s in post_vars) {
			str_url += "&"+s+"="+post_vars[s];
		}
	}
	
	cb.length = 0;
	
	cb.options[0] = new Option('Carregando...','');
	
	if (!default_value) {
		default_value = false;
	}
	
	sel_index = false;
	
	//msg_carregando.innerHTML='Carregando...';
	
	xmlhttp.open("GET", str_url,true);
	
	xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState==4){
			
			retorno = xmlhttp.responseText;
			dataProvider = new Function("return "+retorno)();
			
			//alert('TotalRows: '+dataProvider.totalRows);
			
			for (var i=0;i<dataProvider.totalRows;i++) {
				
				index = dataProvider.registros[i][1];
				valor = dataProvider.registros[i][0];
				
				if (default_value) {
					
					if ((valor.toLowerCase() == default_value.toLowerCase()) || (index.toLowerCase() == default_value.toLowerCase())) 
					{
						sel_index = i;
					}
				}
				cb.options[i] = new Option(valor,index);
			}
			if (sel_index != false) 
			{
				cb.selectedIndex = sel_index;
			}
			
		}
		
		//msg_carregando.innerHTML='Carregando...ok';
		
	}
	
	xmlhttp.send(null);
	
}
function ValidaEmail(email){
	with (document) {
		if (email!='') {
			url='/webloja/includes/gw.cadastro.ajax.php?acao=ValidaEmail&email='+email;
			xmlhttp.open("GET",url,true);
			xmlhttp.onreadystatechange=function() {
				if (xmlhttp.readyState==4){
					retorno=xmlhttp.responseText;
					local=new Function("return "+retorno)();
					if(local.status == 0){
						alert(local.status_text);
						document.location.href='?secao=cadastro';
						return false;
					}
					return true;
				}
			}
			xmlhttp.send(null);
			}
	}
	return true;
}
function validaLogin(){
	usuario=document.getElementById('USUARIO').value;
	if (usuario=='') { alert('Favor preencher o campo Usuario!'); document.getElementById('USUARIO').focus(); return false; }
	senha=document.getElementById('SENHA').value;
	if (senha=='') { alert('Favor preencher o campo Senha!'); document.getElementById('SENHA').focus(); return false; }
	return true;
}
function ajaxLogin(){
	var cPost='';
	usuario=document.getElementById('USUARIO').value;
	senha=document.getElementById('SENHA').value;
	url = 'login.php';
	cPost='USUARIO='+usuario+'&SENHA='+senha;
	//Mostra('WaitWindow');
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
			retorno=xmlhttp.responseText;
			local=new Function("return "+retorno)();
			if (local.status==0) {
				//Oculta('WaitWindow');
				alert(local.status_text);
				document.getElementById('USUARIO').value='';
				document.getElementById('SENHA').value='';
				document.getElementById('USUARIO').focus();
				return false;
			} else {
				//Oculta('WaitWindow');
				//alert(local.status_text);
				document.location='index.php?secao=informacoes&titulo=Informacoes/Seu+Sistema&id=188';
				return true;
			}
		}
	}
    xmlhttp.send(cPost);
}
function ajaxLembraSenha(){
	var cPost='';
	//Mostra('WaitWindow');
	usuario	= document.getElementById('USUARIO').value;
	if (usuario=='') { 
		//Oculta('WaitWindow');
		alert('Favor preencher o campo Usuario!'); document.getElementById('USUARIO').focus(); 
		return false; 
	}
	url='login.php?acao=lembrete';
	cPost='USUARIO='+usuario;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
			//Oculta('WaitWindow');
			retorno=unescape(xmlhttp.responseText);
			local=new Function("return "+retorno)();
			if (local.status==0) {
				alert(local.status_text);
				return false;
			}
			alert(local.status_text);
			return true;
		}
	}
    xmlhttp.send(cPost);
}
function prod_check_dupl(codigo){
	url='ajax/gw.webloja.ajax.php?acao=prod_check_dupl&codigo='+codigo;
	xmlhttp.open("GET",url,true);
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
			retorno=new Function("return "+xmlhttp.responseText)();
			if (retorno.status == "erro") {
				window.alert(retorno.msg);
				document.getElementById('CODIGO').value='';
				document.getElementById('CODIGO').focus();
				return false;
			} else {
				return true;
			}
		}
	}
    xmlhttp.send(null);
}
