// *********************************************
// FUNCIONES PARA VALIDACIÓN FORMULARIO REGISTRO
// *********************************************
function trim(inputString) {
   // Elimina los espacios iniciales y finales de la cadena que recibe. También elimina
   // espacios consecutivos y los sustituye por uno solo. Si se recibe algo que no sea una 
   //cadena (null, objeto personalizado, ...), devuelve la entrada.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Busca espacios al principio de la cadena
	  retValue = retValue.substring(1, retValue.length);
	  ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Busca espacios al final de la cadena
	  retValue = retValue.substring(0, retValue.length-1);
	  ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Comprueba si hay espacios consecutivos
	  retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
   }
   return retValue; // Devuelve la cadena sin espacios
} // Final de la función "trim"

function validarEmail(valor) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
		return (true)
	} else {
		return (false);
	}
}

// Función Auxiliar Validación NIE
function str_replace(search, replace, subject) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
 
    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
 
    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };
 
    return sa ? s : s[0];
}

//Retorna: 1 = NIF ok, 2 = CIF ok, 3 = NIE ok, -1 = NIF error, -2 = CIF error, -3 = NIE error, 0 = ??? error
function valida_nif_cif_nie(a) 
{
	var temp=a.toUpperCase();
	var cadenadni="TRWAGMYFPDXBNJZSQVHLCKE";
 
	if (temp!==''){
		//si no tiene un formato valido devuelve error
		if ((!/^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$/.test(temp) && !/^[T]{1}[A-Z0-9]{8}$/.test(temp)) && !/^[0-9]{8}[A-Z]{1}$/.test(temp))
		{
			return 0;
		}
 
		//comprobacion de NIFs estandar
		if (/^[0-9]{8}[A-Z]{1}$/.test(temp))
		{
			posicion = a.substring(8,0) % 23;
			letra = cadenadni.charAt(posicion);
			var letradni=temp.charAt(8);
			if (letra == letradni)
			{
			   	return 1;
			}
			else
			{
				return -1;
			}
		}
 
		//algoritmo para comprobacion de codigos tipo CIF
		suma = parseInt(a.charAt(2))+parseInt(a.charAt(4))+parseInt(a.charAt(6));
		for (i = 1; i < 8; i += 2)
		{
			temp1 = 2 * parseInt(a.charAt(i));
			temp1 += '';
			temp1 = temp1.substring(0,1);
			temp2 = 2 * parseInt(a.charAt(i));
			temp2 += '';
			temp2 = temp2.substring(1,2);
			if (temp2 == '')
			{
				temp2 = '0';
			}
 
			suma += (parseInt(temp1) + parseInt(temp2));
		}
		suma += '';
		n = 10 - parseInt(suma.substring(suma.length-1, suma.length));
 
		//comprobacion de NIFs especiales (se calculan como CIFs)
		if (/^[KLM]{1}/.test(temp))
		{
			if (a.charAt(8) == String.fromCharCode(64 + n))
			{
				return 1;
			}
			else
			{
				return -1;
			}
		}
 
		//comprobacion de CIFs
		if (/^[ABCDEFGHJNPQRSUVW]{1}/.test(temp))
		{
			temp = n + '';
			if (a.charAt(8) == String.fromCharCode(64 + n) || a.charAt(8) == parseInt(temp.substring(temp.length-1, temp.length)))
			{
				return 2;
			}
			else
			{
				return -2;
			}
		}
		
		//comprobacion de NIEs
		if (/^[T]{1}/.test(temp))
		{
			if (a.charAt(8) == /^[T]{1}[A-Z0-9]{8}$/.test(temp))
			{
				return 3;
			}
			else
			{
				return -3;
			}
		}
		
		//XYZ
		if (/^[XYZ]{1}/.test(temp))
		{
			pos = str_replace(['X', 'Y', 'Z'], ['0','1','2'], temp).substring(0, 8) % 23;			
			if (a.charAt(8) == cadenadni.substring(pos, pos + 1))
			{
				return 3;
			}
			else
			{
				return -3;
			}
		}
	}
 
	return 0;
}

function estiloCampoError(thename) {
	document.getElementById(thename).style.background = "#FFFF99";
}

function estiloCampoOk(thename) {
	document.getElementById(thename).style.background = "#FFFFFF";
}

// *****************************************************************
// FUNCIONES PARA VALIDACIÓN FORMULARIO DATOS USUARIO (/panel/datos)
// *****************************************************************

function validate_datos_usuario (fobj) {
	var mensaje = "";
	var nowcont = true;
	
	// Vacio todos los mensajes de error
	jQuery("#mensajes_form_datos_usuario").removeClass("mensaje_ajax");
	jQuery("#mensajes_form_datos_usuario").empty();
	
	// Recorro todos los campos del formulario para validarlos
	for(var i = 0; i < fobj.elements.length; i++) {
		var thename = (fobj.elements[i].name == undefined || fobj.elements[i].name.length == 0) ? '' : fobj.elements[i].name;
		var thevalue = fobj.elements[i].value;	
		
		if (thename == "nombre"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su nombre. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
	
		if (thename == "apellidos"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir sus apellidos. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "telefono"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su n&uacute;mero de tel&eacute;fono. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "email"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su e-mail. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else {
				if(validarEmail(thevalue) == false) {
					mensaje = "Debe introducir un e-mail correcto. <br />";
					estiloCampoError(thename);
					nowcont = false;
				} else
					estiloCampoOk(thename);
			}
		}
		
		if (thename == "password"){
			if (trim (thevalue) == "" && (fobj["password_new"].value != "" || fobj["password_confirm"].value != "")){
				mensaje += "Debe introducir su contraseña actual. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else {
				if(trim (thevalue) != "" && fobj["ca"].value != sha1(thevalue)) {
					mensaje += "La constraseña introducida no coincide con su contraseña actual. <br />";
					estiloCampoError(thename);
					nowcont = false;
				} else {
					estiloCampoOk(thename);
				}
			}
		}
		
		if (thename == "password_new" && (fobj["password"].value != "" || fobj["password_confirm"].value != "")){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su nueva contraseña. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "password_confirm" && (fobj["password"].value != "" || fobj["password_new"].value != "")){
			if (trim (thevalue) == ""){
				mensaje += "Debe confirmar su contraseña. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else {
				if(fobj["password_new"].value != thevalue) {
					mensaje += "Debe repetir la misma contraseña. <br />";
					estiloCampoError(thename);
					nowcont = false;
				} else {
					estiloCampoOk("password_new");
					estiloCampoOk("password_confirm");
				}
			}
		}		
	}
	
	if(nowcont == false) {
		// document.getElementById(thename).focus();
		jQuery("#mensajes_form_datos_usuario").html(mensaje);
		jQuery("#mensajes_form_datos_usuario").addClass("mensaje_ajax");
	}
		
	return nowcont;
}

// **********************************************
// FUNCIONES PARA VALIDACIÓN CATEGORIAS USUARIO
// **********************************************
function validate_categorias_usuario (fobj) {
	var mensaje = "";
	var nowcont = true;
	var validar_subcategorias = true;
	
	// Vacio todos los mensajes de error
	jQuery("#mensajes_form_categorias_usuario").removeClass("mensaje_ajax");
	jQuery("#mensajes_form_categorias_usuario").empty();
	
	for(var i = 0; i < fobj.elements.length; i++) {
		var thename = (fobj.elements[i].name == undefined || fobj.elements[i].name.length == 0) ? '' : fobj.elements[i].name;
		var thevalue = fobj.elements[i].value;
		
		if (thename.indexOf("subcategoria__") != -1 && validar_subcategorias){
			if(fobj[thename].checked != false)
				validar_subcategorias = false;
		}
	}
	
	if (validar_subcategorias) {
		mensaje = "Debe elegir una subcategoria profesional. <br />" + mensaje;
		nowcont = false;
	}
	
	if(nowcont == false) {
		// document.getElementById(thename).focus();
		jQuery("#mensajes_form_categorias_usuario").html(mensaje);
		jQuery("#mensajes_form_categorias_usuario").addClass("mensaje_ajax");
	}
		
	return nowcont;
}

// *******************************************************************
// FUNCIONES PARA VALIDACIÓN FORMULARIO DATOS EMPRESA (/panel/empresa)
// *******************************************************************
function validate_datos_empresa (fobj){
	
	var mensaje = "";
	var nowcont = true;
	
	// Vacio todos los mensajes de error
	jQuery("#mensajes_form_datos_empresa").removeClass("mensaje_ajax");
	jQuery("#mensajes_form_datos_empresa").empty();
	
	for(var i = 0; i < fobj.elements.length; i++) {
		var thename = (fobj.elements[i].name == undefined || fobj.elements[i].name.length == 0) ? '' : fobj.elements[i].name;
		var thevalue = fobj.elements[i].value;
		
		if (thename == "empresa"){
			if (thevalue == ""){
				mensaje += "Debe introducir el nombre de su empresa. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		/*
		if (thename == "ingresos"){
			if (trim (thevalue) == 0){
				mensaje += "Debe introducir los ingresos anuales de su empresa. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		*/
		
		if (thename == "nif"){
			if (thevalue == ""){
				mensaje += "Debe introducir su CIF/NIF. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else {
				// valida_nif_cif_nie = 1: NIF Ok; valida_nif_cif_nie = 2: CIF Ok; valida_nif_cif_nie = 3: NIE Ok  
				var validar_documento = valida_nif_cif_nie(thevalue);
				if(validar_documento != 1 && validar_documento != 2 && validar_documento != 3) {
					mensaje += "Debe introducir un CIF/NIF/NIE correcto. <br />";
					estiloCampoError(thename);
					nowcont = false;
				} else
					estiloCampoOk(thename);
			}
		}
		
		if (thename == "provincia"){
			if (trim (thevalue) == 0){
				mensaje += "Debe introducir sus provincia. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "localidad"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su localidad. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "codigo_postal"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su codigo postal. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "direccion"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su direccion. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
	}
	
	if(nowcont == false) {
		// document.getElementById(thename).focus();
		jQuery("#mensajes_form_datos_empresa").html(mensaje);
		jQuery("#mensajes_form_datos_empresa").addClass("mensaje_ajax");
	}
		
	return nowcont;
}

// ********************************************************
// FUNCIONES PARA VALIDACIÓN FORMULARIO REGISTRO SOLICITUD
// ********************************************************

function validate_registro_solicitud(fobj){
	var mensaje = "";
	var nowcont = true;
	var validar_subcategorias = true;

	// Vacio todos los mensajes de error
	jQuery("#errores_form_registro_solicitud").removeClass("mensaje_ajax");
	jQuery("#errores_form_registro_solicitud").empty();
			
	// Recorro todos los campos del formulario para validarlos
	for(var i = 0; i < fobj.elements.length; i++) {
		var thename = (fobj.elements[i].name == undefined || fobj.elements[i].name.length == 0) ? '' : fobj.elements[i].name;
		var thevalue = fobj.elements[i].value;		
		
		if (thename.indexOf("subcategoria__") != -1 && validar_subcategorias){
			if(fobj[thename].checked != false)
				validar_subcategorias = false;
		}
		
		if (thename == "empresa"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su nombre de empresa. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}

		if (thename == "nombre"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su nombre. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
	
		if (thename == "apellidos"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir sus apellidos. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "nif"){
			if (thevalue == ""){
				mensaje += "Debe introducir su CIF/NIF/NIE. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else {
				// valida_nif_cif_nie = 1: NIF Ok; valida_nif_cif_nie = 2: CIF Ok; valida_nif_cif_nie = 3: NIE Ok  
				var validar_documento = valida_nif_cif_nie(thevalue);
				if(validar_documento != 1 && validar_documento != 2 && validar_documento != 3) {
					mensaje += "Debe introducir un CIF/NIF/NIE correcto. <br />";
					estiloCampoError(thename);
					nowcont = false;
				} else
					estiloCampoOk(thename);
			}
		}
		
		if (thename == "provincia"){
			if (trim (thevalue) == 0){
				mensaje += "Debe introducir sus provincia. </br >";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "localidad"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su localidad. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "telefono"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su n&uacute;mero de tel&eacute;fono. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "email"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su e-mail. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else {
				if(validarEmail(thevalue) == false) {
					mensaje = "Debe introducir un e-mail correcto. <br />";
					estiloCampoError(thename);
					nowcont = false;
				} else
					estiloCampoOk(thename);
			}
		}
		
		if (thename == "email_rep"){
			if (trim (thevalue) == ""){
				mensaje += "Debe repetir su e-mail.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else {
				if(validarEmail(thevalue) == false) {
					mensaje += "Debe introducir un e-mail correcto.<br />";
					estiloCampoError(thename);
					nowcont = false;
				} else {
					if(fobj["email"].value != thevalue) {
						mensaje += "Debe repetir el mismo e-mail.<br />";
						estiloCampoError(thename);
						nowcont = false;
					} else
						estiloCampoOk(thename);
				}
			}
		}
				
		if (thename == "password"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir un password. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "password_confirm"){
			if (trim (thevalue) == ""){
				mensaje += "Debe repetir su password. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else {
				if(fobj["password"].value != thevalue) {
					mensaje += "Debe escribir el mismo password. <br />";
					estiloCampoError(thename);
					nowcont = false;
				} else
					estiloCampoOk(thename);
			}
		}
		
		if (thename == "terminos"){
			if (trim (thevalue) == "0"){
				mensaje += "Debe aceptar los terminos y condiciones. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
	}
	
	if (validar_subcategorias) {
		mensaje = "Debe elegir una subcategoria profesional. <br />" + mensaje;
		nowcont = false;
	}
			
	if(nowcont == false) {
		// document.getElementById(thename).focus();
		jQuery("#errores_form_registro_solicitud").html(mensaje);
		jQuery("#errores_form_registro_solicitud").addClass("mensaje_ajax");
	}
		
	return nowcont;
}

// **********************************************
// FUNCIONES PARA VALIDACIÓN FORMULARIO PROYECTOS
// **********************************************

var extra_radio_aux = '';

function validate_form_datos_proyecto_1 (fobj){
	var mensaje = "";
	var nowcont = true;

	// Vacio todos los mensajes de error
	jQuery("#mensajes_form_datos_proyecto").removeClass("mensaje_ajax");
	jQuery("#mensajes_form_datos_proyecto").empty();
			
	// Recorro todos los campos del formulario para validarlos
	for(var i = 0; i < fobj.elements.length; i++) {
		var thename = (fobj.elements[i].name == undefined || fobj.elements[i].name.length == 0) ? '' : fobj.elements[i].name;
		var thevalue = fobj.elements[i].value;
	
		// Campos específicos
		if(thename != undefined && (thename.indexOf("extra_")) != -1 && (thename.indexOf("_obligatorio")) != -1) {
			
			var campo_texto_otros_obligatorio = thename.replace("obligatorio", "text");
			if( trim (thevalue) == "false" || trim (thevalue) == "0" || trim (thevalue) == "") {
				
				/*
				// Si existe el campo campo_texto_otros_obligatorio se trata de radiobuttons o checkbox array
				if(document.getElementById(campo_texto_otros_obligatorio) != null) {
					mensaje += "Debe seleccionar una opción.<br />";
					estiloCampoError(campo_texto_otros_obligatorio);
				} else {
					// Para el resto de campos
					mensaje += "Este campo es obligatorio.<br />";
					estiloCampoError(thename);
				}
				*/
				
				// En lugar de resaltar todos los campos obligatorios se muestra un único mensaje de error
				nowcont = false;
			} else {
				if(document.getElementById(campo_texto_otros_obligatorio) != null)
					document.getElementById(campo_texto_otros_obligatorio).style.background = "#FFFFFF";
				//else
				//	estiloCampoOk(thename);
			}					
		}		
	}
	
	if(nowcont == false) {
		mensaje = "Por favor, revise los campos obligatorios.<br />";
		jQuery("#mensajes_form_datos_proyecto").html(mensaje);
		jQuery("#mensajes_form_datos_proyecto").addClass("mensaje_ajax");
	} else {
		// Registro el seguimiento del formulario de registro de proyecto enviado correctamente en Google Analytics
		url_pagetracker = document.location.href.replace("http://www.icontrata.com","") + "/paso_1";
		pageTracker._trackPageview(url_pagetracker);
	}
		
	return nowcont;
}

function validate_form_datos_proyecto_2 (fobj){
	var mensaje = "";
	var nowcont = true;

	// Vacio todos los mensajes de error
	jQuery("#mensajes_form_datos_proyecto").removeClass("mensaje_ajax");
	jQuery("#mensajes_form_datos_proyecto").empty();
			
	// Recorro todos los campos del formulario para validarlos
	for(var i = 0; i < fobj.elements.length; i++) {
		var thename = (fobj.elements[i].name == undefined || fobj.elements[i].name.length == 0) ? '' : fobj.elements[i].name;
		var thevalue = fobj.elements[i].value;
		
		// Datos genéricos del proyecto
		if (thename == "presupuesto_proyecto"){
			if (trim (thevalue) == "0"){
				mensaje += "Debe seleccionar el presupuesto para el proyecto.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "ofertas_proyecto"){
			if (trim (thevalue) == "0"){
				mensaje += "Debe seleccionar cuántas ofertas desea recibir.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		// Información de contacto
		if (thename == "nombre"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su nombre.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "apellidos"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir sus apellidos.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "provincia"){
			if (trim (thevalue) == "0"){
				mensaje += "Debe introducir sus provincia.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "localidad"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su localidad.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "telefono"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su n&uacute;mero de tel&eacute;fono.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "email"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su e-mail.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else {
				if(validarEmail(thevalue) == false) {
					mensaje += "Debe introducir un e-mail correcto.<br />";
					estiloCampoError(thename);
					nowcont = false;
				} else
					estiloCampoOk(thename);
			}
		}
		
		if (thename == "email_rep"){
			if (trim (thevalue) == ""){
				mensaje += "Debe repetir su e-mail.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else {
				if(validarEmail(thevalue) == false) {
					mensaje += "Debe introducir un e-mail correcto.<br />";
					estiloCampoError(thename);
					nowcont = false;
				} else {
					if(fobj["email"].value != thevalue) {
						mensaje += "Debe repetir el mismo e-mail.<br />";
						estiloCampoError(thename);
						nowcont = false;
					} else
						estiloCampoOk(thename);
				}
			}
		}
		
		if (thename == "terminos"){
			if (trim (thevalue) == "0"){
				mensaje += "Debe aceptar los terminos y condiciones. <br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
	}
	
	if(nowcont == false) {
		jQuery("#mensajes_form_datos_proyecto").html(mensaje);
		jQuery("#mensajes_form_datos_proyecto").addClass("mensaje_ajax");
	} else {
		// Cambio la imagen de los pasos por la del paso 3
		jQuery(".pasos-proyecto").attr("id", "pasos-03");
		// Oculto el action_proyecto
		jQuery("#action_proyecto").hide();
		
		// Registro el seguimiento del formulario de registro de proyecto enviado correctamente en Google Analytics
		url_pagetracker = document.location.href.replace("http://www.icontrata.com","") + "/paso_2";
		pageTracker._trackPageview(url_pagetracker);
	}
		
	return nowcont;
}

// **********************************************************************
// FUNCIONES PARA VALIDACIÓN FORMULARIO SUSCRIPCIONES (panel/suscripcion)
// **********************************************************************
function validate_new_suscripcion (fobj){
	
	var nowcont = true;

	// Recorro todos los campos del formulario para validarlos
	for(var i = 0; i < fobj.elements.length; i++) {
		var thename = (fobj.elements[i].name == undefined || fobj.elements[i].name.length == 0) ? '' : fobj.elements[i].name;
		var thevalue = fobj.elements[i].value;

		if (thename == "form_new_suscripcion_categoria"){
			if (trim (thevalue) == 0){
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
	
		if (thename == "form_new_suscripcion_subcategorias"){
			if (trim (thevalue) == 0){
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "max_hojas_pedido"){
			if (trim (thevalue) == 0){
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		/*
		if (thename == "presupuesto_proyecto"){
			if (trim (thevalue) == 0){
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "provincias"){
			if (trim (thevalue) == "0"){
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		*/		
	}
	
	return nowcont;
}

// **********************************************************************
// FUNCIONES PARA VALIDACIÓN FORMULARIOS FILTROS (panel/filtros)
// **********************************************************************
function validate_filtro (fobj){
	var nowcont = true;
	/*
	var nowcont_01 = true;
	var nowcont_02 = true;
	
	
	estiloCampoOk("presupuesto_" + fobj.name);
	estiloCampoOk("provincia_" + fobj.name);
	
	// Recorro todos los campos del formulario para validarlos
	for(var i = 0; i < fobj.elements.length; i++) {
		var thename = (fobj.elements[i].name == undefined || fobj.elements[i].name.length == 0) ? '' : fobj.elements[i].name;
		var thevalue = fobj.elements[i].value;
		
		if (thename == "presupuesto_" + fobj.name){
			if (trim (thevalue) == 0) {
				nowcont_01 = false;
			}
		}
		
		if (thename == "provincia_" + fobj.name) {
			if (trim (thevalue) == 0) {
				nowcont_02 = false;
			}
		}
		
	}
	
	// Sólo se devuelve false si no se ha seleccionado ninguna opción de ninguna lista
	if(nowcont_01 == false && nowcont_02 == false) {
		estiloCampoError("presupuesto_" + fobj.name);
		estiloCampoError("provincia_" + fobj.name);
		nowcont = false;
	}
	
	*/
	return nowcont;
}

// **********************************************
// FUNCIONES PARA VALIDACIÓN FORMULARIOS CONTACTO
// **********************************************
function validate_form_contacto (fobj){
	var mensaje = "";
	var nowcont = true;

	// Vacio todos los mensajes de error
	jQuery("#mensajes_form_contacto").removeClass("mensaje_ajax");
	jQuery("#mensajes_form_contacto").empty();
			
	// Recorro todos los campos del formulario para validarlos
	for(var i = 0; i < fobj.elements.length; i++) {
		var thename = (fobj.elements[i].name == undefined || fobj.elements[i].name.length == 0) ? '' : fobj.elements[i].name;
		var thevalue = fobj.elements[i].value;
	
		// Información de contacto
		if (thename == "nombre"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su nombre.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "telefono"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su n&uacute;mero de tel&eacute;fono.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "email"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su e-mail.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else {
				if(validarEmail(thevalue) == false) {
					mensaje += "Debe introducir un e-mail correcto.<br />";
					estiloCampoError(thename);
					nowcont = false;
				} else
					estiloCampoOk(thename);
			}
		}
		
		if (thename == "consulta"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su consulta.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
	}
	
	if(nowcont == false) {
		jQuery("#mensajes_form_contacto").html(mensaje);
		jQuery("#mensajes_form_contacto").addClass("mensaje_ajax");
	} else {
		// Registro el seguimiento del envío de la recomendación correctamente en Google Analytics
		url_pagetracker = "/contacto_ok";
		pageTracker._trackPageview(url_pagetracker);
	}
		
	return nowcont;
}

// **********************************************************
// FUNCIONES PARA VALIDACIÓN FORMULARIO OPINIONES PROVEEDORES
// **********************************************************
function validate_form_opiniones (fobj){
	var mensaje = "";
	var nowcont = true;

	// Vacio todos los mensajes de error
	jQuery("#mensajes_form_opiniones_proveedor").removeClass("mensaje_ajax");
	jQuery("#mensajes_form_opiniones_proveedor").empty();
			
	// Recorro todos los campos del formulario para validarlos
	for(var i = 0; i < fobj.elements.length; i++) {
		var thename = (fobj.elements[i].name == undefined || fobj.elements[i].name.length == 0) ? '' : fobj.elements[i].name;
		var thevalue = fobj.elements[i].value;
	
		// Información de contacto
		if (thename == "nombre"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su nombre.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "empresa"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir el nombre de su empresa.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "opinion"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su opini&oacute;n.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
	}
	
	if(nowcont == false) {
		jQuery("#mensajes_form_opiniones_proveedor").html(mensaje);
		jQuery("#mensajes_form_opiniones_proveedor").addClass("mensaje_ajax");
	}
		
	return nowcont;
}

// **********************************************
// FUNCIONES PARA VALIDACIÓN FORMULARIOS CONTACTO
// **********************************************
function validate_form_contacto (fobj){
	var mensaje = "";
	var nowcont = true;

	// Vacio todos los mensajes de error
	jQuery("#mensajes_form_contacto").removeClass("mensaje_ajax");
	jQuery("#mensajes_form_contacto").empty();
			
	// Recorro todos los campos del formulario para validarlos
	for(var i = 0; i < fobj.elements.length; i++) {
		var thename = (fobj.elements[i].name == undefined || fobj.elements[i].name.length == 0) ? '' : fobj.elements[i].name;
		var thevalue = fobj.elements[i].value;
		
		// Información de contacto
		if (thename == "nombre"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su nombre.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "telefono"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su n&uacute;mero de tel&eacute;fono.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
		
		if (thename == "email"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su e-mail.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else {
				if(validarEmail(thevalue) == false) {
					mensaje += "Debe introducir un e-mail correcto.<br />";
					estiloCampoError(thename);
					nowcont = false;
				} else
					estiloCampoOk(thename);
			}
		}
		
		if (thename == "consulta"){
			if (trim (thevalue) == ""){
				mensaje += "Debe introducir su consulta.<br />";
				estiloCampoError(thename);
				nowcont = false;
			} else
				estiloCampoOk(thename);
		}
	}
	
	if(nowcont == false) {
		jQuery("#mensajes_form_contacto").html(mensaje);
		jQuery("#mensajes_form_contacto").addClass("mensaje_ajax");
	}
		
	return nowcont;
}

// ***********************************************************
// FUNCIÓN PARA VALIDACIÓN FORMULARIO RECOMENDAR icontrata.com
// ***********************************************************
function validate_form_recomendar (fobj){
	var nowcont = true;
			
	// Recorro todos los campos del formulario para validarlos
	for(var i = 0; i < fobj.elements.length; i++) {
		var thename = (fobj.elements[i].name == undefined || fobj.elements[i].name.length == 0) ? '' : fobj.elements[i].name;
		var thevalue = fobj.elements[i].value;
		
		if(thename != "submit" && thename != "mensaje") {		
			estiloCampoOk(thename);			
			if (thename == "su_email" || thename == "email_amigo"){
				if (trim (thevalue) == ""){
					fobj.elements[i].value = "Debe introducir un e-mail";
					estiloCampoError(thename);
					nowcont = false;
				} else {
					if(validarEmail(thevalue) == false) {
						fobj.elements[i].value = "Debe introducir un e-mail correcto";
						estiloCampoError(thename);
						nowcont = false;
					} else
						estiloCampoOk(thename);
				}
			} else {
				if (trim (thevalue) == ""){
					fobj.elements[i].value = "Este campo es obligatorio";
					estiloCampoError(thename);
					nowcont = false;
				}
			}
		}
	}
	
	if(nowcont == true) {
		// Registro el seguimiento del envío de la recomendación correctamente en Google Analytics
		url_pagetracker = "/recomendar_ok";
		pageTracker._trackPageview(url_pagetracker);
	}
	
	return nowcont;
}
