function anyadirEvento(evento, elemento, funcion){
	if (elemento.addEventListener){
		elemento.addEventListener(evento, funcion, false);
		return true;
	}else{
		r = elemento.attachEvent("on" + evento, funcion);
		return r;
	}
}

function activarValidacion(formulario){
	for (i=0;i<formulario.elements.length;i++){
		elemento = formulario.elements[i];
		if (elemento.style.display != "none" && elemento.style.visibility != "hidden" && !elemento.disabled){
			if (elemento.getAttribute("validacion")!= null && elemento.getAttribute("validacion")!= ""){
				anyadirEvento("blur",eval(elemento),eventBlurFormulario);
			}
		}   
	}
	anyadirEvento("submit",formulario, eventSubmitFormulario);
}

function eventSubmitFormulario(event){
	form = getTargetOfAnEvent(event);
	result = activarEventoSubmitFormulario(form);
	if (!result){
		if (event.preventDefault){
			event.preventDefault();
		}		
	}
	return result;
}
function activarEventoSubmitFormulario(form){
	idioma = form.O_Idioma.value;
	checkElementoAnterior = "";
	mensajes = "";
	primerElemento = null;
	for (i=0;i<form.elements.length;i++){
		valido = true;
		elemento = form.elements[i];
		if (elemento.style.display != "none" && elemento.style.visibility != "hidden" && !elemento.disabled && elemento.type != "hidden"){
			if (elemento.getAttribute("validacion")!= null && elemento.getAttribute("validacion")!= ""){
				sistemaValidacion = elemento.getAttribute("validacion").split(",");
				nombre = elemento.name.split("_")[2];
				for (j=0;j<sistemaValidacion.length;j++){
					if (valido){
						//miramos si pertenece a algun grupo
						hayQueValidar = false;
						if (elemento.getAttribute("grupo") != null){	
							elm = eval("form['" + elemento.getAttribute("grupo")+ "']");
							if ((elm[0].value=="si" && elm[0].checked == true) || (elm[1].value=="si" && elm[1].checked == true)){
								hayQueValidar =true;
							}
						}else{
							hayQueValidar = true;
						}
						if (hayQueValidar){
							if (elemento.type == 'radio'){
								if (checkElementoAnterior == "" || checkElementoAnterior != elemento.name){
									checkElementoAnterior  = elemento.name;
									elemChk = eval("form['" + elemento.name + "']");
									valor = "";
									for(k=0;k<elemChk.length;k++){
										if (elemChk[k].checked == true){
											valor = elemChk[k].value;
										}
									}
									valido = valido & isValid(valor, eval(sistemaValidacion[j]));
									if (!valido){
										mensajes = mensajes  + getValoresAceptados(nombre,eval(sistemaValidacion[j]),idioma) + "\n";
									}
								}
							}else if(elemento.type == 'checkbox'){
								valor = "";
								if (elemento.checked){
									valor = elemento.value;
								}
								valido = isValid(valor, eval(sistemaValidacion[j])) && valido;
								if (!valido){
									mensajes = mensajes  + getValoresAceptados(nombre,eval(sistemaValidacion[j]),idioma) + "\n";
								}
							}else{
								valido = isValid(elemento.value, eval(sistemaValidacion[j])) && valido;
								if (!valido){
									mensajes = mensajes  + getValoresAceptados(nombre,eval(sistemaValidacion[j]),idioma) + "\n";
								}
							}
							if (!valido && primerElemento == null){
								primerElemento = elemento;
							}
						}
					}
				}
			}
		}
	}
	if (mensajes != ""){
		alert(mensajes);
		primerElemento.select();
		primerElemento.focus();
		return false;
	}
	return true;
}

function parsearFormulario(formulario){
	valido = true;
	for (i=0;i<formulario.elements.length;i++){
		elemento = formulario.elements[i];
		if (true){
			//validamos name
			try{
				tipo = elemento.name.split("_")[0];
				if (tipo != 'O' && tipo != 'N'){
					alert("El elemento " + elemento.name + " tiene el tipo incorrecto");
					return false;
				}
				if (tipo != 'O'){
					if (isNaN(parseInt(elemento.name.split("_")[1])) || elemento.name.split("_")[1].length != 2){
						alert("El elemento " + elemento.name + " tiene el orden incorrecto");
						return false;
					}
				}
				if (tipo != 'O'){
					if (elemento.name.split("_")[2] == ""){
						alert("El elemento " + elemento.name + " tiene el nombre incorrecto");
						return false;
					}
				}else{
					if (elemento.name.split("_")[1] == ""){
						alert("El elemento " + elemento.name + " tiene el nombre incorrecto");
						return false;
					}
				}
			}catch(e){
				alert("El elemento " + elemento.name  + " tiene el altributo name incorrecto. Deberia ser: N/C_0x_nombre");
				return false;
			}
			//validamos atributo validacion
			try{
				if (elemento.getAttribute("validacion") != null){
					if (isNaN(eval(elemento.getAttribute("validacion")))){
						alert("El elemento " + elemento.name  + " tiene el altributo validacion incorrecto.");
						return false;
					}
				}
			}catch(e){
				alert("El elemento " + elemento.name  + " tiene el altributo validacion incorrecto.");
				return false;
			}
			//validamos el grupo
			try{
				if (elemento.getAttribute("grupo") != null){
					if (eval("formulario['" + elemento.getAttribute("grupo")+ "']") == null){
						alert("El elemento " + elemento.name  + " tiene el altributo grupo incorrecto.");
						return false;
					}
				}
			}catch(e){
				alert("El elemento " + elemento.name  + " tiene el altributo grupo incorrecto.");
				return false;
			}
		}
	}
	alert("formulario correcto");
}

function eventBlurFormulario(event){
	elemento = getTargetOfAnEvent(event);
	sistemaValidacion = elemento.getAttribute("validacion").split(",");
	for(j=0;j<sistemaValidacion.length;j++){
		eval("formatearCampo(elemento,\"" + eval(sistemaValidacion[j])  + "\");");
	}
}

//cros browser functions

	function getTargetOfAnEvent(event){
		if (event.target != null){
			return event.target;
		}
		return event.srcElement;
	}