// JavaScript Document
/*** FUNCIONES PARA VALIDAR DATOS ***/
/*** APLICAN LOS ESTILOS DEFINIDOS EN VALIDATOR.CSS**/

/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR CONTROLES VACIOS*/
function emptyValidator(control)
{
	if(document.getElementById(control).value == '')
	{
		return false;
	}
	else
	{
		return true;
	}
}

/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR COMBOS SIN SELECCIONAR*/
function selectValidator(control)
{
	
	if(document.getElementById(control).selectedIndex <= 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR COMBOS SIN SELECCIONAR SIN --Seleccione--*/
function selectValidatorNOSEL(control)
{
	
	if(document.getElementById(control).selectedIndex < 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR CONTROLES NUMERICOS*/
function numberemptyValidator(control)
{
	
	if(document.getElementById(control).value == '')
	{
		return false;
	}
	else
	{
		if (isNaN(document.getElementById(control).value)) 
	  	{ 
			return false;
     	}
	  	else
	 	{ 
			return true;
      	} 
		
	}
}

function numberValidator(control)
{
	
	if (isNaN(document.getElementById(control).value)) 
	{ 
		return false;
	}
	else
	{ 
		return true;
	} 
}

/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR QUE UN CHECBOX ESTE CHECKEADO*/
function checkValidator(control)
{
	if(!document.getElementById(control).checked)
	{
		return false;
	}
	else
	{
		return true;
	}
}

/* USADO EN EL SUBMIT PARA VALIDAR QUE FIRSTNAME Y LASTNAME NO ESTEN VACIOS*/
function nameValidator(controlFirst,controlLast)
{
	if(document.getElementById(controlFirst).value=='' && document.getElementById(controlLast).value=='')
	{
		return false;
	}
	else if(document.getElementById(controlFirst).value=='')
	{
		return false;
	}
	else if(document.getElementById(controlLast).value=='')
	{
		return false;
	}
	else
	{
		return true;
	}
}


/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR EMAIL*/
function emailValidator(control)
{
	if (document.getElementById(control).value!="")
	{
		var emailID = document.getElementById(control);
	
	
		if (emailCheck(emailID.value)==false)
		{
			return false;
			
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}

/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR EMAIL Y QUE NO ESTE VACIO*/
function emailEmptyValidator(control)
{
	var emailID = document.getElementById(control);
	
	if ((emailID.value==null)||(emailID.value==""))
	{
		return false;
	}
	else if (emailCheck(emailID.value)==false)
	{
		return false;
		
	}
	else
	{
		return true;
	}
}

function emailCheck (emailStr) 
{
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	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) 
	{
		/* Too many/few @'s or something; basically, this address doesn't
	even fit the general mould of a valid e-mail address. */
	
	/*alert("Email address seems incorrect (check @ and .'s)");*/
	return false;
	}
	
	var user=matchArray[1];
	var domain=matchArray[2];

	// Start by checking that only basic ASCII characters are in the strings (0-127).

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
		/*alert("Ths username contains invalid characters.");*/
		return false;
	   	}
	}
	
	for (i=0; i<domain.length; i++) {
	if (domain.charCodeAt(i)>127) {
	/*alert("Ths domain name contains invalid characters.");*/
	return false;
	   }
	}

	// See if "user" is valid 
	if (user.match(userPat)==null) {
	// user is not valid
	/*alert("The username doesn't seem to be valid.");*/
	return false;
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		// this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
			/*alert("Destination IP address is invalid!");*/
			return false;
		   }
		}
		return true;
	}

	// Domain is symbolic name.  Check if it's valid.
	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("The domain name does not seem to be valid.");*/
		return false;
		   }
	}


	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
	/*alert("The address must end in a well-known domain or two letter " + "country.");*/
	return false;
	}

	if (len<2) {
	/*alert("This address is missing a hostname!");*/
	return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
}

/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR UN IMPORTE SEA MAYOR QUE 0*/
function amountValidator(control)
{
	if(document.getElementById(control).value=="" || document.getElementById(control).value==0)
	{
		return false;
	}
	else
	{
		return true;
	}
}