// JavaScript Document AJAX
var ajax = Load_Ajax();

//////////////////////////////////////////////////////////////////////////////// load ajax
function Load_Ajax() {
	var ajax;
  	try { ajax = new ActiveXObject("Microsoft.XMLHTTP"); }
    catch(e) {
       try { ajax = new ActiveXObject("Msxml2.XMLHTTP"); }
	   catch(ex) {
          try { ajax = new XMLHttpRequest(); }
	      catch(exc) {
            alert("Browser não tem recursos para Ajax");
            ajax = null;
          }
       }
    }
	return ajax;
}

//////////////////////////////////////////////////////////////////////////////// função menu
function menuCadastro(id1, id2, id3, id4) { 
	ID = document.getElementById(id1); 
    if (ID.style.display == "") {
	    ID.style.display = "none"; 
	} else {
        ID.style.display = ""; 
    }
	ID = document.getElementById(id2); 
    if (ID.style.display == "") {
	    ID.style.display = "none"; 
	} else {
        ID.style.display = ""; 
    }
	ID = document.getElementById(id3); 
    if (ID.style.display == "") {
	    ID.style.display = "none"; 
	} else {
        ID.style.display = ""; 
    }
	ID = document.getElementById(id4); 
    if (ID.style.display == "") {
	    ID.style.display = "none"; 
	} else {
        ID.style.display = ""; 
    }
}
function menuRelatorio(id1, id2, id3, id4) { 
	ID = document.getElementById(id1); 
    if (ID.style.display == "") {
	    ID.style.display = "none"; 
	} else {
        ID.style.display = ""; 
    }
	ID = document.getElementById(id2); 
    if (ID.style.display == "") {
	    ID.style.display = "none"; 
	} else {
        ID.style.display = ""; 
    }
	ID = document.getElementById(id3); 
    if (ID.style.display == "") {
	    ID.style.display = "none"; 
	} else {
        ID.style.display = ""; 
    }
	ID = document.getElementById(id4); 
    if (ID.style.display == "") {
	    ID.style.display = "none"; 
	} else {
        ID.style.display = ""; 
    }
}
function menuManutencao(id1, id2) { 
	ID = document.getElementById(id1); 
    if (ID.style.display == "") {
	    ID.style.display = "none"; 
	} else {
        ID.style.display = ""; 
    }
	ID = document.getElementById(id2); 
    if (ID.style.display == "") {
	    ID.style.display = "none"; 
	} else {
        ID.style.display = ""; 
    }
}

//////////////////////////////////////////////////////////////////////////////// efetua logon
function Efetua_Logon(valor1, valor2) {
	if (Valida_Login() == true) {
		if (ajax) {
			mensagem = document.getElementById("mensagem_login");
			ajax.open("POST", "../admin/xml_logon.php", true);
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			ajax.onreadystatechange = function() {
				if (ajax.readyState == 1) { mensagem.innerHTML = "Logando...<img src=\"../img/loading.gif\">"; }
				if (ajax.readyState == 4) { if (ajax.responseXML) { xml_Logon(ajax.responseXML); } }
			}
			var params1 = "login="+valor1;
			var params2 = "senha="+valor2;
			var params = params1+"&"+params2;
			ajax.send(params);
		}
	}
}
function xml_Logon(obj) {
	var dataArray = obj.getElementsByTagName("logon");
	if (dataArray.length > 0) {
        var item = dataArray[0];
		dados = item.getElementsByTagName("mensagem")[0].firstChild.nodeValue;
		if (dados == "Erro") {
			dados = "Não é possível efetuar Logon e(ou) Login ou Senha Incorreto(s).";
			mensagem.innerHTML = dados;
		} else {
			for (var i = 0; i < dataArray.length; i++) {
				var item = dataArray[i];
    	        dados = item.getElementsByTagName("contato")[0].firstChild.nodeValue;
	    	}
			mensagem.innerHTML = "Bem vindo "+dados;
			location.reload(true);
		}
   	}
}

//////////////////////////////////////////////////////////////////////////////// valida logon
function Valida_Login() {
	var login = document.formLogin.txtLogin.value;
	var senha = document.formLogin.txtSenha.value;
	var mensagem = document.getElementById("mensagem_login");
	if (login.length < 3) {
		mensagem.innerHTML = "<b>Preenche o Login Corretamente.</b>";
		document.formLogin.txtLogin.focus();
		return false;
	}
	if (senha.length < 3) {
		mensagem.innerHTML = "<b>Preenche a Senha Corretamente.</b>";
		document.formLogin.txtSenha.focus();
		return false;
	}
	return true;
}

//////////////////////////////////////////////////////////////////////////////// habilita cliente
function Habilitar_Cliente(valor) {
	var mensagem = document.getElementById("id_mensagem_manut_cliente");
	mensagem.innerHTML = "";
	if (ajax) {
		ajax.open("POST", "../admin/xml_habilita_cliente.php", true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 1) { mensagem.innerHTML = "Habilitando...<img src=\"../img/loading.gif\">"; }
           	if (ajax.readyState == 4) { 
				if (ajax.status == 200) { if (ajax.responseText) { mensagem.innerHTML = "Cliente habilitado com sucesso."; } }
			}
       	}
	   	var params = "codigo="+valor;
	   	ajax.send(params);
    }
}

//////////////////////////////////////////////////////////////////////////////// valida string
function htmlEntities(texto) {
    var i, carac, letra, novo='';
    for (i = 0; i < texto.length; i++) {
        carac = texto[i].charCodeAt(0);
        if ( (carac > 47 && carac < 58) || (carac > 62 && carac < 127) ) {

            novo += texto[i];
        } else {
            novo += "&#" + texto[i].charCodeAt(0) + ";";
        }
    }
    return novo;
}
function html2entities(str)
    {var keys=new Array("&", " ", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "«", "¬", "­", "®", "¯", "°", "±", "²", "³", "´", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½", "¾", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "ÿ", "\"", "<", ">", "");
    var values=new Array("&amp;", "&nbsp;", "&iexcl;", "&cent;", "&pound;", "&curren;", "&yen;", "&brvbar;", "&sect;", "&uml;", "&copy;", "&ordf;", "&laquo;", "&not;", "&shy;", "&reg;", "&macr;", "&deg;", "&plusmn;", "&sup2;", "&sup3;", "&acute;", "&micro;", "&para;", "&middot;", "&cedil;", "&sup1;", "&ordm;", "&raquo;", "&frac14;", "&frac12;", "&frac34;", "&iquest;", "&Agrave;", "&Aacute;", "&Acirc;", "&Atilde;", "&Auml;", "&Aring;", "&AElig;", "&Ccedil;", "&Egrave;", "&Eacute;", "&Ecirc;", "&Euml;", "&Igrave;", "&Iacute;", "&Icirc;", "&Iuml;", "&ETH;", "&Ntilde;", "&Ograve;", "&Oacute;", "&Ocirc;", "&Otilde;", "&Ouml;", "&times;", "&Oslash;", "&Ugrave;", "&Uacute;", "&Ucirc;", "&Uuml;", "&Yacute;", "&THORN;", "&szlig;", "&agrave;", "&aacute;", "&acirc;", "&atilde;", "&auml;", "&aring;", "&aelig;", "&ccedil;", "&egrave;", "&eacute;", "&ecirc;", "&euml;", "&igrave;", "&iacute;", "&icirc;", "&iuml;", "&eth;", "&ntilde;", "&ograve;", "&oacute;", "&ocirc;", "&otilde;", "&ouml;", "&divide;", "&oslash;", "&ugrave;", "&uacute;", "&ucirc;", "&uuml;", "&yacute;", "&thorn;", "&yuml;", "&quot;", "&lt;", "&gt;", "");
    for (var i = 0; i < keys.length; i++)
        { if (values[i]!="&nbsp;") { str=str.replace(keys[i], values[i]); } }
    return str;
}

//////////////////////////////////////////////////////////////////////////////// tabulaçao nos formularios
function TABEnter(oEvent){
 	var oEvent = (oEvent)? oEvent : event;
  	var oTarget = (oEvent.target)? oEvent.target : oEvent.srcElement;
  	if (oEvent.keyCode == 13)
    	oEvent.keyCode = 9;
  	if (oTarget.type == "text" && oEvent.keyCode == 13)
	    oEvent.keyCode = 9;
  	if (oTarget.type == "radio" && oEvent.keyCode == 13)
    	oEvent.keyCode = 9;
}