/*
 * Interesa Forms v1.0
 * 
 * Interesa.es (http://www.interesa.es)
 *   
 */

/**************************************************************************************************
Función para validar formularios
	Campo Obligatorio: class="obligatorio"
	Campo mail: class="obligatorio mail"
	Nota: Primero te comprueba los campos de tipo input y despues los campos de tipo textarea
**************************************************************************************************/

function validar_formulario(){
	var pattern = /^[a-zA-Z0-9\-\._]+@[a-zA-Z0-9\-_]+(\.?[a-zA-Z0-9\-_]*){2,3}\.[a-zA-Z]{2,3}$/;
	var class_obligatorio = "obligatorio";
	var class_mail = "mail";
	
	var obj = document.getElementsByTagName("fieldset");
	
	for (i=0; i<obj.length; i++){//Bucle para los fieldset
		for (var b=0; b<obj[i].childNodes.length; b++) {//Bucle para los hijos de cada fieldset
			var campo = obj[i].childNodes[b];

			if (existe_cadena(campo.className,class_obligatorio)){//Comprobar si es obligatorio
				//Si es un input
				if (campo.tagName == "INPUT") {
					if (campo.type == "text") {
						if(campo.value=='' || campo.value==campo.getAttribute("title")){
							alert(campo.getAttribute("title"));
							campo.focus();
							return false;
						}
						
						//Si el campo es un mail
						if (existe_cadena(campo.className,class_mail)){
							if (!pattern.test(campo.value) ){//Si el formato es incorrecto
								alert(campo.getAttribute("title"));
								campo.focus();
								return false;
							}
						}
						
					}
					if (campo.type == "checkbox") {
					   if (!campo.checked) {
						  alert(campo.getAttribute("title"));
						  return false;
					   }
					}
		
				 }//Fin Si es input
				 
				 //Si es un textarea
				if (campo.tagName == "TEXTAREA") {
					var Str = campo.value;
						if (Str.length == 0 || campo.value==campo.getAttribute("title")){
							alert(campo.getAttribute("title"));
							campo.focus();
							return false;
						}
				}
				//Si es un select
				if (campo.tagName == "SELECT") {
					if(campo.options[campo.selectedIndex].text=='' || campo.options[campo.selectedIndex].text==campo.getAttribute("title")){
						alert(campo.getAttribute("title"));
						campo.focus();
						return false;
					}
				}
			}//Fin si es obligatorio
			
		}
	}
	
}

/*Mas de un formulario por pagina*/

function validar_formularios(objeto){
	var pattern = /^[a-zA-Z0-9\-\._]+@[a-zA-Z0-9\-_]+(\.?[a-zA-Z0-9\-_]*){2,3}\.[a-zA-Z]{2,3}$/;
	var class_obligatorio = "obligatorio";
	var class_mail = "mail";
	
	var obj = objeto.getElementsByTagName("fieldset");
	
	for (i=0; i<obj.length; i++){//Bucle para los fieldset
		for (var b=0; b<obj[i].childNodes.length; b++) {//Bucle para los hijos de cada fieldset
			var campo = obj[i].childNodes[b];

			if (existe_cadena(campo.className,class_obligatorio)){//Comprobar si es obligatorio
				//Si es un input
				if (campo.tagName == "INPUT") {
					if (campo.type == "text") {
						if(campo.value=='' || campo.value==campo.getAttribute("title")){
							alert(campo.getAttribute("title"));
							campo.focus();
							return false;
						}
						
						//Si el campo es un mail
						if (existe_cadena(campo.className,class_mail)){
							if (!pattern.test(campo.value) ){//Si el formato es incorrecto
								alert(campo.getAttribute("title"));
								campo.focus();
								return false;
							}
						}
						
					}
					if (campo.type == "checkbox") {
					   if (!campo.checked) {
						  alert(campo.getAttribute("title"));
						  return false;
					   }
					}
		
				 }//Fin Si es input
				 
				 //Si es un textarea
				if (campo.tagName == "TEXTAREA") {
					var Str = campo.value;
						if (Str.length == 0 || campo.value==campo.getAttribute("title")){
							alert(campo.getAttribute("title"));
							campo.focus();
							return false;
						}
				}
				//Si es un select
				if (campo.tagName == "SELECT") {
					if(campo.options[campo.selectedIndex].text=='' || campo.options[campo.selectedIndex].text==campo.getAttribute("title")){
						alert(campo.getAttribute("title"));
						campo.focus();
						return false;
					}
				}
			}//Fin si es obligatorio
			
		}
	}
	
}









function existe_cadena(cadena1,cadena2){

	if(cadena1==null){
			return false;
	}
	if(cadena1.indexOf(cadena2)!=-1){
		return true;
	}else{
		return false;
	}
}

/****************************************************/
//	Restaurar el valor de los inputs Y textarea
/****************************************************/

	function limpiar_valor_input(evnt) {
         ev = (evnt) ? evnt : event;
         input_actual = (ev.target) ? ev.target : ev.srcElement;
         if (input_actual.value == input_actual.defaultValue) {
            input_actual.value = "";
         }
      }

      function restaurar_valor_input(evnt) {
         ev = (evnt) ? evnt : event;
         input_actual = (ev.target) ? ev.target : ev.srcElement;
         if (input_actual.value == "") {
            input_actual.value = input_actual.defaultValue;
         }
      }
      
      function setAutoRestore() {
      
         function assignHandlerFF(obj) {//Asignar el evento en Firefox
            obj.addEventListener("focus", limpiar_valor_input, false);
            obj.addEventListener("blur", restaurar_valor_input, false);
         }
         
         function assignHandlerIE(obj) { //Asignar el evento en IE
            obj.attachEvent("onfocus", limpiar_valor_input);
            obj.attachEvent("onblur", restaurar_valor_input);
         }
         //añadir los eventos a los input
         var array_input = document.getElementsByTagName("INPUT");
         if (array_input.length == 0) return true;
         var assignHandlerFunc = (array_input[0].addEventListener) ? assignHandlerFF : assignHandlerIE;
         for (var i = 0; i < array_input.length; i++) {
			 if(array_input[i].type !="submit" && array_input[i].type !="hidden"){ //si no es un campo de tipo submit o hidden
               assignHandlerFunc(array_input[i]);
			 }
         }
		 
		 
		 //añadir los eventos a los textarea
         var array_textarea = document.getElementsByTagName("TEXTAREA");
         if (array_textarea.length == 0) return true;
         var assignHandlerFunc = (array_textarea[0].addEventListener) ? assignHandlerFF : assignHandlerIE;
         for (var i = 0; i < array_textarea.length; i++) {
               assignHandlerFunc(array_textarea[i]);
         }
         
      }
      
      if (window.addEventListener) {
         window.addEventListener("load", setAutoRestore, false);
      } else {
         window.attachEvent("onload", setAutoRestore);
      }


