/*

 *

 *

 *@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.com.ch)"

	var NumError = "Tiene que ser numero"

	var StrError = "Tiene que ser texto"

	var RepitError = "Datos no coinciden"

	var RqError = "Este campo es requerido"



/* 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*$)/



function FormSubmit(idform){

	

	Form = document.getElementById(idform)

	

	if(Validar(Form)){

		Form.submit()

	}

	

	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 Validar2(formulario){


	
	if(!CompararEmail())
	{
		window.alert('Deben coincidir los emails');
		
		return false;
	}
	if(!CompararPassword())
	{
		window.alert('Deben coincidir los passwords');
		
		return false;
	}

	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){

	

					window.alert(RqError)

					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;

	

				}

			}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;

		

					}

				}

	

			}

		}

	}

	

	return true;



}