/* ver 0.2 */

/* Definimos Mensajes de Error */
	var FechaError = "Ingrese una fecha correcta (ej.: dd/mm/aaaa)"
	var DiaError = "Ingrese un nmero de da correcto"
	var MesError = "Ingrese un nmero de mes correcto"
	var AnoError = "Ingrese un nmero de ao correcto"
	var IntError = "El dato ingresado es invalido."
	var CuilError = "El dato ingresado es invalido."
	var MailError = "Ingres un mail correcto (ej.: ejemplo@ejemplo.com.ar)"
	var NumError = "Tiene que ser numero"
	var StrError = "Tiene que ser texto"
	var RepitError = "Datos no coinciden"
	var RqGrupoChek = "Por favor respondé todas las preguntas."
	var RqError = "Este dato 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 Validar(form){
	
	for (cont=0;cont<form.length;cont++){
		
		if(form.elements[cont].disabled == false){
			
			idarray = form.elements[cont].id.split("_")

			if(idarray[1] == "rq"){
				
				Value = form.elements[cont].value	
	
				Value = trimAll(Value)
				
				if(Value.length < 1){
					window.alert(RqError)
					form.elements[cont].focus()
					return false;
				}
	
				Value = form.elements[cont].value;
				Checked = form.elements[cont].checked;
							
				switch(idarray[2]){
				
					case "mail":
							if(!isMail.test(Value)){
								window.alert(MailError)
								form.elements[cont].focus()
								return false;
							}
						break;
					case "cuil":
							if(!isInt.test(Value)){
								window.alert(CuilError)
								form.elements[cont].focus()
								return false;
							}
							if(Value.length != 11){
								window.alert(CuilError)
								form.elements[cont].focus()
								return false;
							}
						break;
					case "int":
							if(!isInt.test(Value)){
								window.alert(IntError)
								form.elements[cont].focus()
								return false;
							}
						break;
					case "numeric":
							if(!isNum.test(Value)){
								window.alert(NumError)
								form.elements[cont].focus()
								return false;
							}
						break;
					case "string":
							if(!isStr.test(Value)){
								window.alert(StrError)
								form.elements[cont].focus()
								return false;
							}
						break;
					case "grupocheck":
							
							grupoName = idarray[3]
							
							OpcionElegida = false;
							
							for (contAux=0;contAux<form.length;contAux++){
		
								if(form.elements[contAux].disabled == false){
									
									idarrayAux = form.elements[contAux].id.split("_")
									
									Checked = form.elements[contAux].checked;
									
									if(grupoName == idarrayAux[3]){
										if(Checked == true){
											OpcionElegida = true;
										}
									}
								}
							}
								
							if(!OpcionElegida){
								window.alert(RqGrupoChek);
								form.elements[cont].focus()
								return false;
							}
							
						break;
					case "repitir":
							
							nameCompara = idarray[3]
							ValueCompara = form.elements[nameCompara].value
	
							if(Value!=ValueCompara){
								window.alert(RepitError)
								form.elements[cont].focus()
								return false;
							}
							
						break;
					case "dia":
							if(!isNum.test(Value)){
								window.alert(DiaError)
								form.elements[cont].focus()
								return false;
							}else{
								if(Value.length > 2 || Value < 1 || Value > 31){
									window.alert(DiaError)
									form.elements[cont].focus()
									return false;
								}
							}
						break;
					case "mes":
							if(!isNum.test(Value)){
								window.alert(MesError)
								form.elements[cont].focus()
								return false;
							}else{
								if(Value.length > 2 || Value < 1 || Value > 12){
									window.alert(MesError)
									form.elements[cont].focus()
									return false;
								}
							}
						break;
					case "ano":
							if(!isNum.test(Value)){
								window.alert(AnoError)
								form.elements[cont].focus()
								return false;
							}else{
								if(Value.length != 4){
									window.alert(AnoError)
									form.elements[cont].focus()
									return false;
								}
							}
						break;
					case "fecha":
					
							if(!ValidaFecha(Value)){
								window.alert(FechaError)
								form.elements[cont].focus()
								return false;	
							}
							
						break;
	
				}
			}else if(idarray[1] == "norq"){
				
				Value = form.elements[cont].value	
	
				Value = trimAll(Value)
				if(Value.length > 0){
	
					Value = form.elements[cont].value
								
					switch(idarray[2]){
					
						case "mail":
								if(!isMail.test(Value)){
									window.alert(MailError)
									form.elements[cont].focus()
									return false;
								}
							break;
						case "int":
								if(!isInt.test(Value)){
									window.alert(IntError)
									form.elements[cont].focus()
									return false;
								}
							break;
						case "numeric":
								if(!isNum.test(Value)){
									window.alert(NumError)
									form.elements[cont].focus()
									return false;
								}
							break;
						case "string":
								if(!isStr.test(Value)){
									window.alert(StrError)
									form.elements[cont].focus()
									return false;
								}
							break;
						case "repitir":
								
								nameCompara = idarray[3]
								ValueCompara = form.elements[nameCompara].value
		
								if(Value!=ValueCompara){
									window.alert(RepitError)
									form.elements[cont].focus()
									return false;
								}
								
							break;
						case "grupocheck":
								
								grupoName = idarray[3]
								
								OpcionElegida = false;
								
								for (contAux=0;contAux<form.length;contAux++){
			
									if(form.elements[contAux].disabled == false){
										
										idarrayAux = form.elements[contAux].id.split("_")
										
										Checked = form.elements[contAux].checked;
										
										if(grupoName == idarrayAux[3]){
											if(Checked == true){
												OpcionElegida = true;
											}
										}
									}
								}
									
								if(!OpcionElegida){
									window.alert(RqGrupoChek);
									form.elements[cont].focus()
									return false;
								}
								
							break;
						case "dia":
								if(Value.length > 2 || Value < 1 || Value > 31){
									window.alert(DiaError)
									form.elements[cont].focus()
									return false;
								}
							break;
						case "mes":
								if(Value.length > 2 || Value < 1 || Value > 12){
									window.alert(MesError)
									form.elements[cont].focus()
									return false;
								}
							break;
						case "ano":
								if(Value.length != 4){
									window.alert(AnoError)
									form.elements[cont].focus()
									return false;
								}
							break;
						case "fecha":
						
								if(!ValidaFecha(Value)){
									window.alert(FechaError)
									form.elements[cont].focus()
									return false;	
								}
								
							break;
		
					}
				}
	
			}
		}
	}
	
	return true;

}
