// JavaScript Document
function IsMail(YourMail)
{
	var Template = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; //Formato de direccion de correo electronico
	
	if(YourMail.value!="") 
	{
		if (Template.test(YourMail.value)) 
		{							}
		else
		{   alert("Ingrese un email válido");
			YourMail.focus();
			YourMail.select();
		}		
	}
	return false;
}

function IsPhone(YourPhone)
{
	var Template = /^[0-9- ]+$/i //Formato de alfanumerico
	
	if(YourPhone.value!="") 
	{
		if (Template.test(YourPhone.value)) 
		{							}
		else
		{   alert("El Teléfono que ha Ingresado tiene Caracteres Inválidos");
			YourPhone.focus();
			YourPhone.select();
		}		
	}
	return false;
}

function IsPassword(pass)
{	var Template = /^[0-9a-zA-Z]+$/i //Formato de alfanumerico
	
	if(pass.value!="") 
	{
		if (Template.test(pass.value)) 
		{							}
		else
		{   alert("Contraseña errónea, vuelvalo a intentar");
			pass.focus();
			pass.select();
		}		
	}
	return false;
}

function IsTexto(texto)
{
	var Template = /^[a-zA-ZáéíóúÁÉÍÓÚÑñ, ]+$/i //Formato de alfanumerico
		if(texto.value!="") 
	{
		if (Template.test(texto.value)) 
		{							}
		else
		{   alert("Solo puede ingresar texto");
			texto.focus();
			texto.select();
		}		
	}		
	return false;
}

function IsTexto1(texto)
{
	var Template = /^[a-zA-ZáéíóúÁÉÍÓÚÑñ, ]+$/i //Formato de alfanumerico
		if(texto.value!="") 
	{
		if (Template.test(texto.value)) 
		{							}
		else
		{   alert("Solo puede ingresar texto");
			texto.focus();
			texto.select();
		}		
	}		
	return false;
}

function IsDay(day)
{
	if(day.value!="") 
	{
	if ( (isNaN(day.value)) || (day.value<1) || (day.value >31) )
		{
			alert("No es un Dia Válido");
			day.focus();
			day.select();			
		}
	}
	
	return false;	
}

function IsMonth(month)
{
	if(month.value!="") 
	{
	if ( (isNaN(month.value)) || (month.value<1) || (month.value >12) )
		{
			alert("No es un Mes Válido");
			month.focus();
			month.select();
		}
	}	
	return false;	
}

function IsYear(year)
{
	if(year.value!="") 
	{
	if ( (isNaN(year.value)) || (year.value<1990) || (year.value >2020) )
		{
			alert("No es un año Válido. Año mínimo 1990");
			year.focus();
			year.select();
		}		
	}	
	return false;	
}

function IsYearNac(year)
{
	if(year.value!="") 
	{
	if ( (isNaN(year.value)) || (year.value<1910) || (year.value >1990) )
		{
			alert("No es un año Válido.");
			year.focus();
			year.select();
		}		
	}	
	return false;	
}

function IsNumerico(numero)
{
	var Template = /^[0-9]+$/i //Formato de alfanumerico
	
	if(numero.value!="") 
	{
		if (Template.test(numero.value)) 
		{							}
		else
		{   alert("El Número que ha Ingresado tiene Caracteres Inválidos");
			numero.focus();
			numero.select();
		}		
	}
	return false;
}

function roundOff(value, precision,fieldName)
{
        value = "" + value //convert value to string
        precision = parseInt(precision);

        var whole = "" + Math.round(value * Math.pow(10, precision));

        var decPoint = whole.length - precision;

        if(decPoint != 0)
        {
                result = whole.substring(0, decPoint);
                result += ".";
                result += whole.substring(decPoint, whole.length);
        }
        else
        {
                result = 0;
                result += ".";
                result += whole.substring(decPoint, whole.length);
        }
		fieldName.value=result;
        //return result;
}

function checkDecimals(fieldName, fieldValue) 
{	decallowed = 2;  // how many decimals are allowed?
	if (isNaN(fieldValue)) 
	{	alert("Ingresa solo números y/o decimales");
		fieldName.select();
		fieldName.focus();
	}
	else 
	{	if (fieldValue.indexOf('.') == -1) fieldValue += ".";
			dectext = fieldValue.substring(fieldValue.indexOf('.')+1, fieldValue.length);
		if (dectext.length > decallowed)
		{	roundOff(fieldValue, decallowed,fieldName)
			/*alert ("OJO!! Introduce un numero con " + decallowed + " decimales.  Intentalo de nuevo.");
			fieldName.select();
			fieldName.focus();*/
		}
	}
}

function validarPasswd(pass) 
{	var espacios = true;
 	var cont = 0;
	
	// Este bucle recorre la cadena para comprobar que no todo son espacios
	while (espacios && (cont < pass.length)) 
	{  	if (pass.charAt(cont) != " ") 
		{	alert ("La contraseña no puede ser todo espacios en blanco");
			pass.focus();
			pass.select();
    	}
	    cont++;
	}
}

function anyoBisiesto(anyo)
{	if (anyo < 100)
		var fin = anyo + 1900;
	else
		var fin = anyo ;

	if (fin % 4 != 0)
		return false;
	else
	{	if (fin % 100 == 0)
		{	if (fin % 400 == 0)
				return true;
			else
				return false;
		}
		else
		{	return true;	}
	}
}

