
// funciones front

/* FUNCIONES VALIDACIÓN FORMULARIOS */

var whitespace = " \t\n\r";
var reWhitespace = /^\s+$/

/** Verifica que no este vacio **/
function isEmpty(s){
	return ((s == null) || (s.length == 0)) 
}
 
/*** Verifica que no sean espacios en blanco o vacio ***/
function isWhitespace (s){
    return (isEmpty(s) || reWhitespace.test(s));
}
 
/*** corta espacios en blanco al principio y al final de una variable ***/
function trimAll(sString) 
{
    while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	};
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

/*** Valida un email mediante expresiones regulares ***/
function validarEmail(valor) {
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(valor)){
                return false;
        } else {
                return true;
        }
}
function isEmail(s){
	return (isWhitespace(s) || validarEmail(s));
}


/*** Validación del formulario de Contacto ***/
function validarContacto(form, msg){
	var ok = true;
	// nombre
	if(isWhitespace(form.nombre.value)){ ok=false; form.nombre.style.backgroundColor='#FFCCCC'; }else{ form.nombre.style.backgroundColor=''; }
	// apellidos
	if(isWhitespace(form.apellidos.value)){ ok=false; form.apellidos.style.backgroundColor='#FFCCCC'; }else{ form.apellidos.style.backgroundColor=''; }
	// email
	if(isEmail(form.email.value)){ ok=false; form.email.style.backgroundColor='#FFCCCC'; }else{ form.email.style.backgroundColor=''; }
	// mensaje
	if(isWhitespace(form.comentario.value)){ ok=false; form.comentario.style.backgroundColor='#FFCCCC'; }else{ form.comentario.style.backgroundColor=''; }

	if(ok==false){
		alert(msg);
		return false;
	}else{
		form.submit();
	}
}
// muestra el email (para evitar SPAM)
function montaEmail(adress, domain, extension){
	var email = adress+'@'+domain+'.'+extension;
	var result = '<a href="mailto:'+email+'">'+email+'</a>';
	document.write(result);
}

// abre un popUp ccon los resultados del examen
function showResultado(url){
	var form = document.getElementById('examens');
	var data = form.data.value;
	var dni = form.dni.value;
	url = url + "?data=" + data + "&dni=" + dni;
	// centramos el popup
	var left = (screen.width - 130) / 2;
	var top = (screen.height - 70) / 2;
	
	window.open(url, '', 'width=260,height=140,left='+ left +',top=' + top + ',location=no,menubar=no,scrollbars=no,status=no');
}
