
// 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));
}
/*** Valida un email mediante expresiones regulares ***/
function validarTelefono(valor) {
        if (/^\d{9}$/.test(valor)){
                return false;
        } else {
                return true;
        }
}
function isTelefono(s){
	return (isWhitespace(s) || validarTelefono(s));
}


/*** Validación del formulario de Contacto ***/
function validarContacto(form, msg){
	var ok = true;
	// empresa
	if(isWhitespace(form.empresa.value)){ ok=false; form.empresa.style.backgroundColor='#FFCCCC'; }else{ form.empresa.style.backgroundColor=''; }
	// nombre
	if(isWhitespace(form.nombre.value)){ ok=false; form.nombre.style.backgroundColor='#FFCCCC'; }else{ form.nombre.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=''; }
	// telefono
	if(isTelefono(form.telefono.value)){ ok=false; form.telefono.style.backgroundColor='#FFCCCC'; }else{ form.telefono.style.backgroundColor=''; }

	if(ok==false){
		alert(msg);
		return false;
	}else{
		form.submit();
	}
}

/*** Función equivalente a urlencode() de PHP ***/
function urlencode(str) {
	return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

/*** Validar Buscador ***/
function validarBusqueda(form, url, msg){

	var ok = true;
	
	if(isWhitespace(form.buscar.value)){ ok=false; form.buscar.style.backgroundColor='#FF6666'; }else{ form.buscar.style.backgroundColor=''; }
	
	if(ok==false){
		alert(msg);
	}else{
		location.href=url + '/' + urlencode(form.buscar.value) + '.html';
	}
}

/*** Valida si se está escribiendo en el campo ***/
function isWritting(input){
	if(isWhitespace(input.value)){ ok=false; input.style.backgroundColor='#FF6666'; }else{ input.style.backgroundColor=''; }
}

