JavaScript released under Creative Commons License - http://creativecommons.org/licenses/by-nc-sa/1.0/

function validar () {
	for (var i=0; i < document.forms[0].length;i++) {
		//miramos que elementos del formulario hay que checkear
		objVal = document.forms[0].elements[i];

		if (objVal.getAttribute("jsName") !=null && objVal.getAttribute("jsVal") !=null) {
			// primero desglosamos los distintos tipos de validacion

			//alert(document.forms[0].elements[i].getAttribute("jsName"));


			validaciones = objVal.getAttribute("jsVal").split (",");

			for (n=0; n<validaciones.length; n++) {
				if (validaciones[n] == 'not_empty') {
					if (objVal.value == '') {
						alert ('El campo "' + objVal.getAttribute("jsName") + '" no puede estar vacio');
						objVal.select();
						objVal.focus();
						return (false);
					}
				}
				if (validaciones[n] == 'is_date') {
					if (objVal.value != '') {
						if (check_date (objVal)==false) {
							alert ('El campo "' + objVal.getAttribute("jsName") + '" ha de ser una fecha valida dd/mm/aaaa');
							objVal.select ();
							objVal.focus ();
							return (false);
						}
					}

				}
				if (validaciones[n] == 'is_number') {
					if (objVal.value == '') {
						alert ('Debe introducir un numero de telefono');
						return (false);
					}

					if (check_number (objVal.value)==false) return (false);

				}
				if (validaciones[n] == 'is_select') {
					if (objVal.value == 0) {
						alert ('Debe seleccionar ' + objVal.getAttribute("jsName"));
						return (false);
					}

				}
				if (validaciones[n]=='not_zero_selected') {
					if (objVal.selectedIndex == 0) {
						alert ('Se ha de seleccionar un valor de "' + objVal.getAttribute("jsName") +'"');
						return (false);
					}

				}
				if (validaciones[n]=='not_empty_on_edit') {
					if (objVal.value=='' && on_edit) {
						alert ('El campo ' + objVal.getAttribute("jsName") + ' no puede estar vacio');
						return(false);
					}
				}
				if (validaciones[n]=='is_email') {
					if (objVal.value != '') {
						if (check_email (objVal.value)==false) {
							alert ('El campo ' + objVal.getAttribute("jsName") + ' ha de ser una direccion de email válida');
							return(false);
						}
					}
				}
				if (validaciones[n]=='is_url') {
					if (objVal.value != '') {
						if (check_url (objVal.value) == false) {
							alert ('El campo ' + objVal.getAttribute("jsName") + ' ha de ser una URL válida');
							return (false);
						}
					}
				}

			}
		}

	}
	return (true);
}
function check_date(field){
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "/";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = DateField.value;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
  // if (DateValue.length == 6) {
  //   DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }

	if (DateTemp=='' || DateTemp=='undefined') {
		err=27;
	}
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      DateField.value = day + seperator + month + seperator + year;
	  return (true);
   }
   /* Error-message if err != 0 */
   else {
      alert("Fecha incorrecta. Formato válido DD/MM/AAAA");
      DateField.select();
	  DateField.focus();
	  return (false);
   }

}

function check_number(str) {
		/* Comprobamos que tenga 9 digitos */
		var lstr=str.length
		var numeros = "0123456789 ";

		if (lstr < 9) {
			alert ("El numero de telefono debe tener 9 digitos");
			return false
		}

		/* Comprobamoas que todo los digitos sean numeros */
		for (i = 0; i < lstr; i++) {
			if (numeros.indexOf(str.charAt(i)) == -1) {
				alert ("Ha introducido un caracter que no es un numero.");
				return false
			}
		}

	 return true
}

function check_email(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)   return false
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)    return false
		if (str.indexOf(at,(lat+1))!=-1)  return false
	    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)   return false
	    if (str.indexOf(dot,(lat+2))==-1)    return false
	    if (str.lastIndexOf(dot) == 1) return false

	 return true
}
function check_url (str) {

	var myRegExp=/(http:\/\/)/gi;
	var myArray = myRegExp.exec(str);
	if ((myArray)  && (myRegExp.lastIndex == 7))
	{
	  return (true);
	} else {
		return (false);
	}


}