
if (typeof($) != "undefined") $(function(){
	updateReadonlyClass();
	
	$('table.list tr[class!=nodata]').hover(function(){
		$(this).addClass('highlight');
	}, function(){
		$(this).removeClass('highlight');
	});
});

function updateReadonlyClass(){
	$('input:not([disabled]), input:not([readonly])').removeClass('readonly');
	$('input[disabled], input[readonly]').addClass('readonly');
}

function submitClear(form){
	var f = form.getElementsByTagName('input');
	for (var i = 0; i < f.length; i++) if (f[i].type != 'hidden') f[i].value = '';
	form.submit();
}

function validateNotEmpty(campo, nombre){
	campo.value = campo.value.replace(/^\s+|\s+$/g,'');
	var testEmpty = /^.+$/m;
	if (!testEmpty.test(campo.value)){
		alert("The field " + nombre + " is compulsory.");
		campo.focus();
		campo.select();
		return false;
	}
	return true;
}

function validateSelect(campo, nombre){
	if (campo.value == 0){
		alert("You must select an item from the list for the field " + nombre + ".");
		campo.focus();
		return false;
	}
	return true;
}

function validateEmail(campo){
	campo.value = campo.value.replace(/ /g, "");
	var testEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
	if (!testEmail.test(campo.value)){
		alert("Please, fill in the field e-mail with a valid address.");
		campo.focus();
		campo.select();
		return false;
	}
	return true;
}

function validatePassword(pass, confirmacion){
	var testPass = /^.{6,}$/;
	if (!testPass.test(pass.value)){
		alert("The password must be at least 6 characters.");
		pass.focus();
		pass.select();
		return false;
	}
	
	if (confirmacion && pass.value != confirmacion.value){
		alert("Incorrect password confirmation.");
		confirmacion.focus();
		confirmacion.select();
		return false;
	}
	
	return true;
}

function validatePostal(campo){
	var testCP = /^[0-9]{5}$/;
	if (!testCP.test(campo.value)){
		alert("Incorrect post code.");
		campo.focus();
		campo.select();
		return false;
	}
	
	return true;
}

function validatePhone(campo, nombre){
	campo.value = campo.value.replace(/[ -]/g, "");
	if (!validateNotEmpty(campo, nombre)) return false;
	var testPhone = /^(00|\+)?\d{9,16}$/;
	if (!testPhone.test(campo.value)){
		alert("The field " + nombre + " has an invalid format.");
		campo.focus();
		campo.select();
		return false;
	}
	
	return true;
}

function validateCLI(campo){
	campo.value = campo.value.replace(/ /g, "");
	var testCLI = /^(6|9)\d{8}$/;
	if (!testCLI.test(campo.value)){
		alert("The field Phone Number (Caller Id) has an invalid format.");
		campo.focus();
		campo.select();
		return false;
	}
	
	return true;
}

function validateProvince(campo){
	if (campo.value <= 0){
		alert("You must select a province.");
		campo.focus();
		return false;
	}
	
	return true;
}

function validateCIF(campo){
	campo.value = campo.value.toUpperCase();
	campo.value = campo.value.replace(/ /g, "");
	campo.value = campo.value.replace(/-/g, "");
	var testCIF = /^[ABCDEFGHKLMNPQS]\d\d\d\d\d\d\d[0-9,A-J]$/;
	if (!testCIF.test(campo.value)){
		alert("The CIF number has an invalid format.");
		campo.focus();
		campo.select();
		return false;
	}

	return true;
}

function validateTotal(campo){
	campo.value = campo.value.replace(/ /g, "");
	campo.value = campo.value.replace(/,/g, ".");
	var testTotal = /^[0-9]{1,6}(\.[0-9]{1,2})?$/;
	if (!testTotal.test(campo.value) || campo.value <= 0){
		alert("Invalid Amount!");
		campo.focus();
		campo.select();
		return false;
	}

	return true;
}

function validateNumeric(campo, nombre){
	var testNumeric = /^[0-9]+$/;
	if (!testNumeric.test(campo.value)){
		alert("The field " + nombre + " must be a number.");
		campo.focus();
		campo.select();
		return false;
	}

	return true;
}

function validateNotZero(campo, nombre){
	var testZero = /^[1-9]{1}[0-9]*$/;
	if (!testZero.test(campo.value)){
		alert("The field " + nombre + " must be a number and cannot be zero.");
		campo.focus();
		campo.select();
		return false;
	}
	return true;
}

function validateDate(campo){
	var ymd = campo.value.split('-');
	var y = ymd[0];
	var m = ymd[1];
	var d = ymd[2];
	var ok = false;
	if (y != null && m != null && d != null){
		if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
			var fecha = new Date(y, m-1, d);
			if (fecha.getDate() == d) return true;
		}
	}
	alert("The date (or date format) selected is invalid.");
	campo.focus();
	campo.select();
	return false;
}
