function ValidarCorreo()
   { 
    var pos1, pos2, bOk = true; 
	var rpta;
    var sDir = document.fregistro.email.value; 
	rpta ='1';
    if (sDir == ""){ 
     alert("Porfavor, ingrese la dirección de correo electrónico"); 
     document.fregistro.email.focus(); 
	 rpta = '-1';
     return rpta; 
    } 
    pos1 = sDir.indexOf('@', 0); 
    pos2 = sDir.indexOf('.', 0); 
    bOk = bOk && (pos1 > 0); 
    bOk = bOk && (pos2 != -1); 
    bOk = bOk && (pos1 < pos2 - 1); 
    bOk = bOk && (pos2 < sDir.length - 1); 
    if (!bOk){ 
     alert("Porfavor, ingrese una dirección de correo válida"); 
     document.fregistro.email.focus(); 
	 rpta ='-1';
	 }
     return rpta; 

   } 

	function numerico(numero){
  		cad = numero.toString();
  		for (var i=0; i<cad.length; i++) {
   			var caracter = cad.charAt(i);
			if (caracter<"0" || caracter>"9")
	 		return false;
  		}
  		return true;
	}	
	
	function letras(cadena){
		//char caracter;
  		cad = cadena.toString();
  		for (var i=0; i<cad.length; i++) {
   			var caracter = cad.charAt(i);
			if ((caracter >='A' && caracter <='Z')  || (caracter >='a' && caracter <='z'))
	 		return true;
  		}
  		return false;
	}

function validarcaracteres(valor) {
   var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú" + "0123456789";
  var checkStr = valor; //document.fregistro.datos.value;
  var allValid = true; 
  for (i = 0; i < checkStr.length; i++) {
    ch = checkStr.charAt(i); 
    for (j = 0; j < checkOK.length; j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length) { 
      allValid = false; 
      break; 
    }
  }
  if (!allValid) { 
    alert("Sólo se aceptan letras, porfavor verifique"); 	
    //document.fregistro.datos.focus(); 
	return (false);
    //return -1; 
  } 
   
}

//---------------------------------------------

function getXMLHTTPRequest(){
	var req;
	try{
		req= new XMLHttpRequest();
	}catch (err1){
							try{
									req = new ActivexObject("Msxml2.XMLHTTP");
							}catch (err2){
													try{
														req = new ActiveXObject("Microsoft.XMLHTTP");
													}
													catch (err3){
														req = false;
													}
												}			
						}
	return req;
}

  obj = getXMLHTTPRequest(); 
	
  function ValidarLogin() {
	
  var estado = 1;
  var u;
  var p;
  
    if ( document.fregistro.txtusuario.value== '' && estado == 1)
	  	{
				alert("Porfavor, ingresar el usuario")
				estado = -1;
				document.fregistro.txtusuario.focus();
      	}

	    if (estado == 1)
		{		
				u = validarcaracteres(document.fregistro.txtusuario.value);
				if (u == false) estado = -1;
	 			document.fregistro.txtusuario.focus(); 
		}

    if ( document.fregistro.txtclave.value== '' && estado == 1)
	  	{
				alert("Porfavor, ingresar la clave")
				estado = -1;
				document.fregistro.txtclave.focus();
      	}

	    if (estado == 1)
		{		p = validarcaracteres(document.fregistro.txtclave.value);
				if (p == false) estado = -1;
	 		document.fregistro.txtclave.focus(); 
		}

       if (estado == 1) {
		 
		user = document.fregistro.txtusuario.value;	
		pass = document.fregistro.txtclave.value;	  
		url = 'LoginUsuario.php?username='+user+'&password='+pass;			
		obj.open('GET',url,true); 
		obj.onreadystatechange = ProcesarAccion;
		obj.send(null);				 
        }
}

	function ProcesarAccion()
	{
		if (obj.readyState == 4)
			{
				if (obj.status == 200)
					{			
						
						if (obj.responseText == 0)
							{
								document.getElementById('mensajer').innerHTML = "Datos válidos...";									
								 setTimeout("document.location.href='Principal.php';",500); 	
							}
						if (obj.responseText == 1)
							{
								document.getElementById('mensajer').innerHTML = "Usuario o Contraseña incorrecto, vuelva a intentarlo"
							}					
						if (obj.responseText == 2)
							{
								document.getElementById('mensajer').innerHTML = "Porfavor Ingrese el Usuario y/o Contraseña"
							}
						
					}
			}
		else
			{
				document.getElementById('mensajer').innerHTML = "<img src='images/procesando.gif' width='16' height='16' /> Procesando...";
			}		
	}

function cerrar() 
{
	window.close();
}

//---------------- I example -------------------

function Enviar(_pagina,capa) {
var ajax;
ajax = ajaxFunction();

ajax.open("POST", _pagina, true);

ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
ajax.onreadystatechange = function()
{

if (ajax.readyState == 4)
{
if (ajax.status==200)
{
document.getElementById(capa).innerHTML = ajax.responseText;

}}}
ajax.send(null);
}
//---------------- F example ---------------------
	
//------------- Enviar Saludo ------------------

  function EnviarSaludo(FechaNac,Nombres,Apellidos,Correo,i)  
  {
	
		fnac = FechaNac;	
		name = Nombres;	
		app = Apellidos;	  
		mail = Correo;			
		myRand = parseInt(Math.random()*9999999999999999999);		
		url = 'EnviarSaludo.php?fecha='+fnac+'&nombres='+name+'&apellidos='+app+'&correo='+mail+'&rand='+myRand;			
		obj.open('GET',url,true); 
		obj.onreadystatechange = function()
		{
			if (obj.readyState == 4)
			{
				if (obj.status==200)
				{
						if (obj.responseText == 0)
						{
							document.getElementById(i).innerHTML = "<strong>OK. Correo enviado</strong>";															
						}						
						if (obj.responseText == 1)
						{
							document.getElementById(i).innerHTML = "<strong>Error. Correo no enviado</strong>";
						}						
				}
			}
			else
			{
				document.getElementById(i).innerHTML = "<strong>Enviando...</strong>"; 
			}		
			
		}
		obj.send(null);
	}

//-----------------

function creaAjax() {
var req;
try {
	req = new XMLHttpRequest();
} catch(err1) {
	try {
		req = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (err2) {
		try {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (err3) {
				req = false;
			}
		}
	}
	return req;
}

oAjax = creaAjax();

function fAjax(archivo,vars,divid){
	myRand = parseInt(Math.random()*999999999999999);
	var modurl = archivo +"?rand=" + myRand + vars; 
	oAjax.open("GET", modurl, true);
	mydiv = divid;
	oAjax.onreadystatechange = rAjax;
	oAjax.send(null);
}

function rAjax() {
	if (oAjax.readyState == 4) {
		if(oAjax.status == 200) {
			var miTexto = oAjax.responseText;
			document.getElementById(mydiv).innerHTML = (miTexto);
		}
	}
	else
	{
			document.getElementById(mydiv).innerHTML = "<img src='images/procesando.gif' width='16' height='16' /> <strong>Procesando...</strong>"; 
	}	
}

function fAjax2(archivo,vars,campo){
	myRand = parseInt(Math.random()*999999999999999);
	var modurl = archivo +"?rand=" + myRand + vars; 
	oAjax.open("GET", modurl, true);
	mycampo = campo;
	oAjax.onreadystatechange = rAjax2;
	oAjax.send(null);
}

function rAjax2() {
	if (oAjax.readyState == 4) {
		if(oAjax.status == 200) {
			var miTexto = oAjax.responseText;
			//document.form.getElementByTagName(campo).value = miTexto;
			document.getElementById(mycampo).value = miTexto;	// el formulario se llama form1, no es necesario poner
		}
	}
}
//- no es suficiente
function mostrar(nombreCapa){ 
document.getElementById(nombreCapa).style.visibility="visible"; 
} 
function ocultar(nombreCapa){ 
document.getElementById(nombreCapa).style.visibility="hidden"; 
} 

//--- 2 funciones en 1
function mostrar_ocultar(campo,capa){ 
	if (document.getElementById(campo).value.length == 0)
	{
		//document.getElementById(nombreCapa).style.visibility="hidden"; 
		ocultar(capa);
	}
	else
	{
		//document.getElementById(nombreCapa).style.visibility="visible"; 
		mostrar(capa);		
	}
} 

