/* -------------------------------------- */
/* Funciones de validación del formulario */
/* -------------------------------------- */
$.fn.mandatory = function(name) {
	if ($(this).size() == 1) {
		if ($(this).is("select") && $(this).attr("multiple") == "multiple") {
			if ($(this).val() == null) {
				txt = (window.labels != null) ? labels["a_es obligatorio"] : "es obligatorio";
				$(this).formError(name + ' ' + txt);
				return false;
			} else {
				$(this).formClearError();
			}
		} else if ($(this).is(".scroll_checkboxes")) {
			if ($(this).children("input:checked").size() == 0) {
				txt = (window.labels != null) ? labels["a_es obligatorio"] : "es obligatorio";
				$(this).formError(name + ' ' + txt);
				return false;
			} else {
				$(this).formClearError();
			}
		} else if ($(this).is("input[type='text']") | $(this).is("select") | $(this).is("input[type='password']") | $(this).is("textarea") | $(this).is("file")) {
				if ($(this).val() == "") {
					txt = (window.labels != null) ? labels["a_es obligatorio"] : "es obligatorio";
					$(this).formError(name + ' ' + txt);
					return false;
				} else {
					$(this).formClearError();
				}
		} else if ($(this).is("input[type='checkbox']") | $(this).is("input[type='radio']")) {
			if (!$(this).is(":checked")) {
				txt = (window.labels != null) ? labels["a_es obligatorio"] : "es obligatorio";
				$(this).formError(name + ' ' + txt);
				return false;
			} else {
				$(this).formClearError();
			}
		}
	} else {
		// es un array. Uno de los campos de la lista tiene que ser obligatorio
		var field;
		var ret = false;
		$(this).each(function(i) {
			if (i == 0) field = $(this);
			if ($(this).is("input[type='text']") | $(this).is("select") | $(this).is("input[type='password']") | $(this).is("textarea") | $(this).is("file")) {
				if ($(this).val() != "") {
					$(this).formClearError();
					ret = true;
				}
			} else if ($(this).is("input[type='checkbox']") | $(this).is("input[type='radio']")) {
				if ($(this).is(":checked")) {
					$(this).formClearError();
					ret = true;
				}
			}
		});
		if (!ret) {
			txt = (window.labels != null) ? labels["a_es obligatorio"] : "es obligatorio";
			field.formError(name + ' '  + txt);
			return false;
		}
	}
	
	return true;
}

$.fn.formError = function(msg) {
	$(this).addClass("error");
	alert(msg);
	$(this).focus();
}

$.fn.formClearError = function () {
	$(this).removeClass("error");
}

$.fn.validateEmail = function(mandatory) {
	if (mandatory == null) mandatory = false;
	
	var email = $(this).val();
	if (!mandatory && email == "") {
		return true;
	}
	
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!email.match(emailRegEx)) {
		txt = (window.labels != null) ? labels["a_Direcci&oacute;n de email no v&aacute;lida"] : "Dirección de email no válida";
		$(this).formError(txt);
		return false;
	}
	return true;
}

$.fn.validatePassword = function(password2) {
	if ($(this).val() != password2.val()) {
		txt = (window.labels != null) ? labels["a_Las dos contrase&ntilde;as entradas no son las mismas"] : "Las dos contraseñas entradas no son las mismas";
		$(this).formError(txt);
		return false;
	}
	
	return true;
}

$.fn.validateDate = function(name) {
	
	var checkstr = "0123456789";
	var Datevalue = "";
	var DateTemp = "";
	var separator = "/";
	var day ="";
	var month ="";
	var year = "";
	var leap = 0;
	var err = 0;
	var i;
	err = 0;
	
	var datevalue = $(this).val();       /* sets the content of the field for valuation */
	/* 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 (err == 0) {
	  $(this).formClearError();
	} else {
		txt = (window.labels != null) ? labels["a_no v&aacute;lida"] : "no válida";
		$(this).formError(name + ' ' + txt);
	}
	
	return (err == 0);
}

$.fn.validateDateRange = function(dateInf, dateSup, msg) {
	var strDate = $(this).val().substr(3,2) + '/' + $(this).val().substr(0,2) + '/' + $(this).val().substr(6,4);
	var date = new Date(strDate);
	var error = false;
	if ((dateInf != null) && (date < dateInf)) error = true;
	if ((dateSup != null) && (date > dateSup)) error = true;
	
	if (error) {
		$(this).formError(msg);
		return false;
	} else {
		$(this).formClearError();
	}
	
	return true;
}

$.fn.validateInteger = function(name) {
	var s = $(this).val();
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!((c >= "0") && (c <= "9"))) {
			txt = (window.labels != null) ? labels["a_no v&aacute;lido"] : "no válido";
			$(this).formError(name + ' ' + txt);

			return false;
		}
	}
	
	return true;
}

$.fn.validateFloat = function(name) {
	var s = $(this).val();
	var cpt = 0;
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!((c >= "0") && (c <= "9"))) {
			if (c == '.') {
				cpt++;
				if (cpt > 1) {
					txt = (window.labels != null) ? labels["a_no v&aacute;lido"] : "no válido";
					$(this).formError(name + ' ' + txt);
		
					return false;
				}
			} else {
				txt = (window.labels != null) ? labels["a_no v&aacute;lido"] : "no válido";
				$(this).formError(name + ' ' + txt);
	
				return false;
			}
		}
	}
	
	return true;
}
