/*


 *@Autor Andru


*/



/* Definimos Mensajes de Error */

	var FechaError = "Ingrese una fecha correcta (ej.: dd/mm/aaaa  - 21/09/1962)"

	var DiaError = "Ingrese un número de día correcto"

	var MesError = "Ingrese un número de mes correcto"

	var AnoError = "Ingrese un número de año correcto"

	var IntError = "Tiene que ser un número Entero"

	var MailError = "Ingrese un mail correcto (ej.: ejemplo@ejemplo.cl)"
	
	var RutError = "Ingrese un RUT correcto"
	
	var CelError = "Ingrese un Cel correcto (ej.: 12345678)"
	
	var TelError = "Ingrese un Tel correcto\n (Código de área - Teléfono Ej.: 02-XXXXXXX)"
	
	var FileError = "El nombre del archivo no cumple con la especificación pedida.\n(Ej: A-15555333-1.jpg)";

	var NumError = "Tiene que ser numero"

	var StrError = "Tiene que ser texto"
	
	var RepitError = "Datos no coinciden"

	var RqError = "Este campo es requerido"
	
	var PasswordError = "No coinciden las passwords"



/* Definimos las RegExp */

	var isMail  = /(^[0-9a-z]([0-9a-z_\-.]*)@([0-9a-z_\-.]*)([.][a-z]{3})$)|(^[0-9a-z]([0-9a-z_\-.]*)@([0-9a-z_\-.]*)([.][a-z]{2})$)|(^[a-z]([a-z_\-.]*)@([a-z_\-.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i

	var isFecha = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/

	var isNum = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/

	var isStr = /(^-?\D\D*\.\D*$)|(^-?\D\D*$)|(^-?\.\D\D*$)/

	var isInt = /(^-?\d\d*$)/
	
	var isRut = /(^\d{8})\-(\d|[k K])$/
	
	var isCel = /(^\d)(\d{7})$/
	
	var isTel = /(^\d{2,3})\-(\d{7})$/



function FormSubmit(idform){

	

	Form = document.getElementById(idform)

	

	if(Validar(Form)){

		Form.submit()

	}

	

	return false	

}

/*** Verifica el RUT mediante el algoritmo Modulo 11 ****/

function ValidaRut(strRut)
{
	if(!isRut.test(strRut))
	{
		return false;
	}

	arrayRut = strRut.split("-");
	
	strNumero1 = arrayRut[0];
	strDV = arrayRut[1];
	
	strNumero = strNumero1.split("");
	
	cantDigitos = strNumero.length;
	
	acum = 0;
	auxDV = 2;
	for( n=cantDigitos-1; n>=0; n--)
	{
		acum += auxDV * strNumero[n];
		
		auxDV++;
		if(auxDV == 8)
		{
			auxDV = 2;
		}
	}
	
	resto = acum % 11;
	
	DV = 11 - resto;


	
	
	switch(DV){
		case 11:
			if(strDV == 0)
			{
				return true;
			}
			else
			{
				return false;
			}
			break;
			
		case 10:
			if(strDV == "k" || strDV == "K")
			{
				return true;
			}
			else
			{
				return false;
			}
			break;
			
		default:
			if(strDV == DV)
			{
				return true;
			}
			else
			{
				return false;
			}
			break;
	}
	
	return true;
}

function validaPassword(str_pass)
{
	pass1 = document.getElementsByName('pass1')[0].value;
	
	if(pass1.match(str_pass))
	{
		return true;
	}
	return false;
}

function validaFile(str, rut, caracter)
{
	caracter = caracter.toLowerCase()
	
	ini = str.lastIndexOf("\\");
	fin = str.length;
	dif = fin - ini;
	
	arch_name = str.substr(ini+1, dif);
	
	aux = arch_name.substr(2, arch_name.length);
	
	caracter_file = arch_name.split("")[0];
	
	rut_numero = aux.split(".")[0];
	

	if( ( (caracter_file.search(caracter)>=0) || (caracter_file.search(caracter.toUpperCase())>=0) ) )
	{
		return true;
	}
	else
	{
		return false;
	}

}


function ValidaFecha(strFecha){



	  if(!isFecha.test(Value)){



	    return false



	  }else{

	

	    var strSeparator = strFecha.substring(2,3) 

	    var arrayDate = strFecha.split(strSeparator) 

	    

	    var arrayValidaMes = {  '01' : 31,'03' : 31, 

	                        	'04' : 30,'05' : 31,

	                        	'06' : 30,'07' : 31,

	                        	'08' : 31,'09' : 30,

	                        	'10' : 31,'11' : 30,

								'12' : 31 }



	    var intDia = parseInt(arrayDate[0],10) 

	

	    

	    if(arrayValidaMes[arrayDate[1]] != null) {

	      if(intDia <= arrayValidaMes[arrayDate[1]] && intDia != 0)

	        return true

	    }



	    var intMes = parseInt(arrayDate[1],10)

	    if (intMes == 2) { 

	       var intAno = parseInt(arrayDate[2])

	       if (intDia > 0 && intDia < 29) {

	           return true

	       }else if (intDia == 29) {

	         if ((intAno % 4 == 0) && (intAno % 100 != 0) || (intAno % 400 == 0)) {

	             return true

	         }   

	       }

	    }

	  }  

	  return false

	

}



function trimAll(strValue){

	

	var objRegExp = /^(\s*)$/

	

	    //Elimino espacios en blanco

	    if(objRegExp.test(strValue)) {

	       strValue = strValue.replace(objRegExp, '')

	       if( strValue.length == 0){

	          return strValue

	       }

	    }

	

	   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/

	   if(objRegExp.test(strValue)) {

	       strValue = strValue.replace(objRegExp, '$2')

	    }



  return strValue



}



function Validar(formulario){

	

	for (cont=0;cont<formulario.length;cont++){

		

		if(formulario.elements[cont].disabled == false){

			

			idarray = formulario.elements[cont].id.split("_")

			

			if(idarray[1] == "rq"){


				Value = formulario.elements[cont].value	


					

				Value = trimAll(Value)

				if(Value.length < 1){

	

					/*
					Si tiene definido el nombre alternativo para el campo lo muestra
					sino muestra el nombre original
					*/
					if(formulario.elements[cont].getAttribute('altName') == null)
					{
						nombre = formulario.elements[cont].name;
					}
					else
					{
						nombre = formulario.elements[cont].getAttribute('altName');
					}
						
					window.alert(RqError + '\n \"' + nombre + '\"' )
					
					if(formulario.elements[cont].type!="hidden")
					{
						formulario.elements[cont].focus()
					}

					return false;

				}

	

				Value = formulario.elements[cont].value

							

				switch(idarray[0]){

				

					case "mail":

							if(!isMail.test(Value)){

								window.alert(MailError)

								formulario.elements[cont].focus()

								return false;

							}

						break;

					case "int":

							if(!isInt.test(Value)){

								window.alert(IntError)

								formulario.elements[cont].focus()

								return false;

							}

						break;

					case "numeric":

							if(!isNum.test(Value)){

								window.alert(NumError)

								formulario.elements[cont].focus()

								return false;

							}

						break;

					case "string":

							if(!isStr.test(Value)){

								window.alert(StrError)

								formulario.elements[cont].focus()

								return false;

							}

						break;

					case "repitir":

							

							nameCompara = idarray[2]

							ValueCompara = formulario.elements[nameCompara].value

	

							if(Value!=ValueCompara){

								window.alert(RepitError)

								formulario.elements[cont].focus()

								return false;

							}

							

						break;

					case "dia":

							if(Value.length > 2 || Value < 1 || Value > 31){

								window.alert(DiaError)

								formulario.elements[cont].focus()

								return false;

							}

						break;

					case "mes":

							if(Value.length > 2 || Value < 1 || Value > 12){

								window.alert(MesError)

								formulario.elements[cont].focus()

								return false;

							}

						break;

					case "ano":

							if(Value.length != 4){

								window.alert(AnoError)

								formulario.elements[cont].focus()

								return false;

							}

						break;

					case "fecha":

					

							if(!ValidaFecha(Value)){

								window.alert(FechaError)

								formulario.elements[cont].focus()

								return false;	

							}

							

						break;
						
					case "rut":

					

							if(!ValidaRut(Value)){

								window.alert(RutError)

								formulario.elements[cont].focus()

								return false;	

							}

							

						break;
						
						
					case "password":

							if(Value.length < 4)
							{
								window.alert('Por favor elija una password de mas de 4 caracteres.')
								
								return false
							}					

							if(!validaPassword(Value)){
								
								window.alert(PasswordError)

								formulario.elements[cont].focus()

								return false;	

							}

							

						break;
						
					case "file":

							rut = document.getElementById('rut_rq').value;
							
							caracter = formulario.elements[cont].getAttribute('prefijo');
							
							if(!validaFile(Value, rut, caracter)){

								window.alert(FileError)

								formulario.elements[cont].focus()

								return false;	

							}

							

						break;
						
					case "tel":

					

							if(!isTel.test(Value)){

								window.alert(TelError)

								formulario.elements[cont].focus()

								return false;	

							}

							

						break;
						
					case "cel":

					

							if(!isCel.test(Value)){

								window.alert(CelError)

								formulario.elements[cont].focus()

								return false;	

							}

							

						break;

	

				}

			}else if(idarray[1] == "norq"){

				

				Value = formulario.elements[cont].value	

	

				Value = trimAll(Value)

				if(Value.length > 0){

	

					Value = formulario.elements[cont].value

								

					switch(idarray[0]){

					

						case "mail":

								if(!isMail.test(Value)){

									window.alert(MailError)

									formulario.elements[cont].focus()

									return false;

								}

							break;

						case "int":

								if(!isInt.test(Value)){

									window.alert(IntError)

									formulario.elements[cont].focus()

									return false;

								}

							break;

						case "numeric":

								if(!isNum.test(Value)){

									window.alert(NumError)

									formulario.elements[cont].focus()

									return false;

								}

							break;

						case "string":

								if(!isStr.test(Value)){

									window.alert(StrError)

									formulario.elements[cont].focus()

									return false;

								}

							break;

						case "repitir":

								

								nameCompara = idarray[2]

								ValueCompara = formulario.elements[nameCompara].value

		

								if(Value!=ValueCompara){

									window.alert(RepitError)

									formulario.elements[cont].focus()

									return false;

								}

								

							break;

						case "dia":

								if(Value.length > 2 || Value < 1 || Value > 31){

									window.alert(DiaError)

									formulario.elements[cont].focus()

									return false;

								}

							break;

						case "mes":

								if(Value.length > 2 || Value < 1 || Value > 12){

									window.alert(MesError)

									formulario.elements[cont].focus()

									return false;

								}

							break;

						case "ano":

								if(Value.length != 4){

									window.alert(AnoError)

									formulario.elements[cont].focus()

									return false;

								}

							break;

						case "fecha":

						

								if(!ValidaFecha(Value)){

									window.alert(FechaError)

									formulario.elements[cont].focus()

									return false;	

								}

								

							break;
							
						case "rut":
	
						
	
								if(!ValidaRut(Value)){
	
									window.alert(RutError)
	
									formulario.elements[cont].focus()
	
									return false;	
	
								}
	
								
	
							break;
							
						case "tel":
	
						
	
								if(!isTel.test(Value)){
	
									window.alert(TelError)
	
									formulario.elements[cont].focus()
	
									return false;	
	
								}
	
								
	
							break;
							
						case "cel":
	
						
	
								if(!isCel.test(Value)){
	
									window.alert(CelError)
	
									formulario.elements[cont].focus()
	
									return false;	
	
								}
	
								
	
							break;

		

					}

				}

	

			}

		}

	}

	

	return true;



}
