/*
*********** ASTECH ****************
LAMBSYSTEM
Design: Guzmán Pool
*/

function fTrim(xLiteral)
{
	var NewLiteral = "";
	var OldChar = " ";
	for (var i=0; i < xLiteral.length; i++)
	{
		if (xLiteral.charAt(i) != " " || OldChar != " ")
		{
			NewLiteral += xLiteral.charAt(i) 
		}
		OldChar = xLiteral.charAt(i)
	}
	return NewLiteral
}

function fValidarPatron(oElemento, sMensaje)
{
	var strCadena = new String(oElemento.value);
	var flag=false;
	
	if(strCadena == "")
		return (flag);
	
	var valido = " ABCDEFGHIJKLMNOPQRSTUVWYXZÑÁÉÍÓÚ";
	
	strCadena = strCadena.toUpperCase();
	for (i = 0 ; i <= strCadena.length - 1; i++)
	{	
		if (valido.indexOf (strCadena.substring(i,i+1),0) == -1)
		{
			valido = strCadena.substring(i,i + 1);
			flag=true;
			break;
		} 
	}
	
	if (flag)
		{
	  alert(sMensaje);
	  oElemento.select();
	  oElemento.focus();
	}

	return (flag);
}


function fVerificaEmail (emailStr){

var checkTLD=1;
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|gob|COM|NET|ORG|EDU|INT|MIL|GOV|ARPA|BIZ|AERO|NAME|COOP|INFO|PRO|MUSEUM|GOB)$/;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);

if (matchArray==null) {
  alert("El e-mail está incorrecto (posibles errores en el uso de caracteres @ y .)");
  return(false);
}

var user=matchArray[1];
var domain=matchArray[2];

for (i=0; i<user.length; i++){
  if (user.charCodeAt(i)>127) {
    alert("El e-mail contiene un nombre de usuario con errores");
    return(false);
  }
}

for (i=0; i<domain.length; i++) {
  if (domain.charCodeAt(i)>127) {
    alert("El dominio del e-mail contiene caracteres inválidos");
    return(false);
  }
}

if (user.match(userPat)==null) {
  alert("El e-mail contiene un nombre de usuario con errores");
  return(false);
}

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
  for (var i=1;i<=4;i++) {
    if (IPArray[i]>255) {
      alert("El e-mail contiene un IP destino inválido");
      return(false);
    }
  }
}

var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;

for (i=0;i<len;i++) {
  if (domArr[i].search(atomPat)==-1) {
    alert("El e-mail contiene un dominio inválido");
    return(false);
	 }
}

if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
  alert("El e-mail debe finalizar en un dominio conocido o codigo de país de dos letras");
   return(false);
  
}

if (len<2) {
  alert("El e-mail debe tener un nombre de host");
  return(false);
 
}

return(true);

}



function fValidaSelectVacio(oElementoSelect, iPrimerIndice, sMensaje)
{
		if (oElementoSelect.selectedIndex == iPrimerIndice)
		{
			alert(sMensaje);
			oElementoSelect.focus();
			return false
		}
		else
		{
			return true
		}
	
}


function fValidaTextoVacio(oElemento, sMensaje)
{
	sLiteral = new String(oElemento.value);	
	sNewLiteral = new String(fTrim(sLiteral));
	oElemento.value = sNewLiteral.toString();
	if (sNewLiteral.length == 0)
	{
		alert(sMensaje)
		oElemento.focus();
		return false
	}
	else
	{
		return true
	}
}



function fValidarNumero(oElemento, sMensaje)
{
	var strCadena = new String(oElemento.value);
	var flag=false
	
	if(strCadena == "")
		return (flag);
			
	var valido = "0123456789";
			
	strCadena = strCadena.toUpperCase();
	for (i = 0 ; i <= strCadena.length - 1; i++)
	{	
		if (valido.indexOf (strCadena.substring(i,i+1),0) == -1)
		{
			valido = strCadena.substring(i,i + 1);
			flag=true;
			break;
		} 
	}	

	if (flag)
	{
	  alert(sMensaje);
	  oElemento.select();
	  oElemento.focus();
	}

	return (flag);
}

function fValidaSexoVacio(radioObject,sMensaje)
{
	var value = null;
	
	if (radioObject == null){
		alert(sMensaje);
		return null;
	}
	if (radioObject[0].checked) {
			
			return true
	}else if(radioObject[1].checked) { 
			
			return true
	}else { 
			alert(sMensaje);
			return false
	}

} 

function fValidaDni(oElemento, sMensaje)
{
	sLongitud = new String(oElemento.value);	
	sNewLongitud = new String(fTrim(sLongitud));
	oElemento.value = sNewLongitud.toString();
	if (sNewLongitud.length != 8)
	{
		alert(sMensaje)
		oElemento.focus();
		return false
	}
	else
	{
		
		return true
	}
}

function fValidaLongitudClave(oElemento, sMensaje)
{
	sLongitud = new String(oElemento.value);	
	sNewLongitud = new String(fTrim(sLongitud));
	oElemento.value = sNewLongitud.toString();
	if (sNewLongitud.length < 6)
	{
		alert(sMensaje)
		oElemento.focus();
		return false
	}
	else
	{
		return true
	}
}


function fValidaClave(oClave,oReclave,sMensaje)
{   
	var strReclave = (oReclave.value);
	var strClave = (oClave.value);

   	 if ( strReclave == strClave)
	     	{
		    return true
			}
	    else
	     	{ 
	     	  alert(sMensaje);
			  oClave.select();
	        oClave.focus();
	        oReclave.value="";
	     	  return false
	     	}
}

function fValidaFechaIncompleta(oElemento, sMensaje)
{
	sLiteral = new String(oElemento.value);	
	if (sLiteral.length < 18 || sLiteral.length > 19)
	{
		alert(sMensaje)
		oElemento.focus();
		return false
	}else{
		return true
	}
}

function divWrite( divName, divValue ) {
	var div = document.getElementById( divName );
	div.innerHTML = divValue;
}
