
function AbrirModal(flash_w, flash_h)
{
	$('#ModalInfo').modal({
		minHeight: flash_h,
		minWidth: flash_w,
		onClose: function(dialog){
		dialog.container.fadeOut('slow', function(){
			dialog.overlay.fadeOut('slow', function(){
				jQuery.modal.close();
				$('#ModalInfo').text('');
			});
		});
		},onOpen: function(dialog){
			dialog.container.fadeIn('slow', function(){
				dialog.overlay.fadeIn('slow', function(){
					dialog.data.slideDown('slow',function(){
						$('#simplemodal-container').css({backgroundImage:'none'});
					});
				});
			});
		}
	});
}
	
function validar_dni(_idioma)
{
	dni = document.getElementById("dni").value;

	numero = dni.substr(0,dni.length-1);
	let = dni.substr(dni.length-1,1);
	numero = numero % 23;
	letra='TRWAGMYFPDXBNJZSQVHLCKET';
	letra=letra.substring(numero,numero+1);
	if (letra!=let) {
		if (_idioma == "en") alert("Incorrect DNI - ID Card");
		else if (_idioma == "ca") alert("DNI incorrecte");
		else alert('DNI incorrecto');
		return false;
	}

	return true;
}

function validar_fecha(Cadena, _idioma){  
    var Fecha = new String(Cadena);   // Crea un string  
    var RealFecha = new Date();   // Para sacar la fecha de hoy  
    // Cadena A\u00f1o  
    var Anyo = new String(Fecha.substring(Fecha.lastIndexOf("/")+1,Fecha.length));
    // Cadena Mes  
    var Mes = new String(Fecha.substring(Fecha.indexOf("/")+1,Fecha.lastIndexOf("/")));
    // Cadena Día  
    var Dia = new String(Fecha.substring(0,Fecha.indexOf("/"))); 
  
    // Valido el a\u00f1o  
    if (isNaN(Anyo) || Anyo.length<4 || parseFloat(Anyo)<1900){  
		// alert('A\u00f1o incorrecto');
		if (_idioma == "en") alert("Incorrect date.\n\nRemember that the format should be dd/mm/year");
		else if (_idioma == "ca") alert("Data incorrecta.\n\nRecuerda que el format ha de ser dia/mes/any");
		else alert('Fecha incorrecta.\n\nRecuerda que el formato debe ser dia/mes/a\u00f1o');
		return false ;
    }  
    // Valido el Mes  
    if (isNaN(Mes) || parseFloat(Mes)<1 || parseFloat(Mes)>12){  
        // alert('Mes incorrecto');
		if (_idioma == "en") alert("Incorrect date.\n\nRemember that the format should be dd/mm/year");
		else if (_idioma == "ca") alert("Data incorrecta.\n\nRecuerda que el format ha de ser dia/mes/any");
		else alert('Fecha incorrecta.\n\nRecuerda que el formato debe ser dia/mes/a\u00f1o');
        return false;
    }  
    // Valido el Dia  
    if (isNaN(Dia) || parseInt(Dia, 10)<1 || parseInt(Dia, 10)>31){  
        // alert('Día incorrecto');
		if (_idioma == "en") alert("Incorrect date.\n\nRemember that the format should be dd/mm/year");
		else if (_idioma == "ca") alert("Data incorrecta.\n\nRecuerda que el format ha de ser dia/mes/any");
		else alert('Fecha incorrecta.\n\nRecuerda que el formato debe ser dia/mes/a\u00f1o');
        return false; 
    }  
    if (Mes==4 || Mes==6 || Mes==9 || Mes==11 || Mes==2) {  
        if (Mes==2 && Dia > 28 || Dia>30) {  
			// alert('Día incorrecto');
			if (_idioma == "en") alert("Incorrect date.\n\nRemember that the format should be dd/mm/year");
			else if (_idioma == "ca") alert("Data incorrecta.\n\nRecuerda que el format ha de ser dia/mes/any");
			else alert('Fecha incorrecta.\n\nRecuerda que el formato debe ser dia/mes/a\u00f1o');
			return false;
        }  
    }  
      
	return true;
}

function esemail(campo, nombre_campo, _idioma)
{
	str = new String(campo.value);
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported) return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{2,4})(\\]?)$");
	if ((!r1.test(str) && r2.test(str)) || (str == ""))
	{
		return true;
	} else {
		if (_idioma == "en") alert("The format of mail " + nombre_campo + " field is not valid");
		else if (_idioma == "ca") alert("El format de mail del camp " + nombre_campo + " no \u00E9s v\u00E0lid");
		else alert("El formato de mail del campo " + nombre_campo + " no es v\u00E1lido");
		campo.focus();
		return false;
	}
}

var mensaje = "";
var repeticion = "";
var idioma = "es";

function validar_formulario_trabaja(_idioma)
{
	problem = false;

	f = document.getElementById("formTrabaja");

	if ( (!problem) && ( f.nombre.value == "" ) ) {
		if (_idioma == "en") alert("Name field is mandatory");
		else if (_idioma == "ca") alert("El camp Nom \u00E9s obligatori");
		else alert("El campo Nombre es obligatorio");
		f.nombre.focus();
		problem = true;
	}
	if ( (!problem) && ( f.apellidos.value == "" ) ) {
		if (_idioma == "en") alert("Surnames field is mandatory");
		else if (_idioma == "ca") alert("El camp Cognoms \u00E9s obligatori");
		else alert("El campo Apellidos es obligatorio");
		f.apellidos.focus();
		problem = true;
	}
	if (!problem) {
		if ( f.dni.value == "" ) {
			if (_idioma == "en") alert("DNI - ID Card field is mandatory");
			else if (_idioma == "ca") alert("El camp DNI \u00E9s obligatori");
			else alert("El campo DNI es obligatorio");
			f.dni.focus();
			problem = true;
		} else if ( !validar_dni(_idioma) ) {
			problem = true;
		}
	}
	if (!problem) {
		if ( f.fecha.value == "" ) {
			if (_idioma == "en") alert("Date of birth field is mandatory");
			else if (_idioma == "ca") alert("El camp Data de naixement \u00E9s obligatori");
			else alert("El campo Fecha de nacimiento es obligatorio");
			f.fecha.focus();
			problem = true;
		} else if ( !validar_fecha(f.fecha.value, _idioma) ) {
			problem = true;
		}
	}	
	if ( (!problem) && ( f.direccion.value == "" ) ) {
		if (_idioma == "en") alert("Address field is mandatory");
		else if (_idioma == "ca") alert("El camp Direcci\u00f3 \u00E9s obligatori");
		else alert("El campo Direcci\u00f3n es obligatorio");
		f.direccion.focus();
		problem = true;
	}
	if ( (!problem) && ( f.cp.value == "" ) ) {
		if (_idioma == "en") alert("Postcode field is mandatory");
		else if (_idioma == "ca") alert("El camp Codi postal \u00E9s obligatori");
		else alert("El campo C\u00f3digo postal es obligatorio");
		f.cp.focus();
		problem = true;
	}
	if ( (!problem) && ( f.poblacion.value == "" ) ) {
		if (_idioma == "en") alert("Name field is mandatory");
		else if (_idioma == "ca") alert("El camp Poblaci\u00f3 \u00E9s obligatori");
		else alert("El campo Poblaci\u00f3n es obligatorio");
		f.poblacion.focus();
		problem = true;
	}
	if ( (!problem) && ( f.provincia.value == "" ) ) {
		if (_idioma == "en") alert("Town field is mandatory");
		else if (_idioma == "ca") alert("El camp Prov\u00EDncia \u00E9s obligatori");
		else alert("El campo Provincia es obligatorio");
		f.provincia.focus();
		problem = true;
	}
	if ( (!problem) && ( f.telefono.value == "" ) ) {
		if (_idioma == "en") alert("Telephone field is mandatory");
		else if (_idioma == "ca") alert("El camp Tel\u00E8fon \u00E9s obligatori");
		else alert("El campo Tel\u00e9fono es obligatorio");
		f.telefono.focus();
		problem = true;
	}
	if (!problem) {
		if ( f.email.value == "" ) {
			if (_idioma == "en") alert("E-mail field is mandatory");
			else if (_idioma == "ca") alert("El camp E-mail \u00E9s obligatori");
			else alert("El campo E-mail es obligatorio");
			f.email.focus();
			problem = true;
		} else if ( !esemail(document.getElementById("email"), "E-mail", _idioma) ) {
			problem = true;
		} 
	}
	if ( (!problem) && ( f.estudios.value == "" ) ) {
		if (_idioma == "en") alert("Level of education field is mandatory");
			else if (_idioma == "ca") alert("El camp Nivell d'estudis \u00E9s obligatori");
		else alert("El campo Nivel de estudios es obligatorio");
		f.estudios.focus();
		problem = true;
	}
	if ( (!problem) && ( f.cv.value == "" ) ) {
		if (_idioma == "en") alert("Curriculum field is mandatory");
			else if (_idioma == "ca") alert("El camp Curr\u00EDculum \u00E9s obligatori");
		else alert("El campo Curr\u00EDculum es obligatorio");
		f.cv.focus();
		problem = true;
	}
	if (!problem) {
		if ( !f.condiciones.checked ) {
			if (_idioma == "en") alert("You must accept the conditions before continuing");
			else if (_idioma == "ca") alert("Has d'acceptar les condicions abans de continuar");
			else alert("Debes aceptar las condiciones antes de continuar");
			problem = true;
		}
	}

	return !problem;
}

function validar_formulario_contacto(_idioma)
{
	problem = false;

	f = document.getElementById("formContacto");

	if ( (!problem) && ( f.nombre.value == "" ) ) {
		if (_idioma == "en") alert("Name field is mandatory");
		else if (_idioma == "ca") alert("El camp Nom \u00E9s obligatori");
		else alert("El campo Nombre es obligatorio");
		f.nombre.focus();
		problem = true;
	}
	if ( (!problem) && ( f.apellidos.value == "" ) ) {
		if (_idioma == "en") alert("Surnames field is mandatory");
		else if (_idioma == "ca") alert("El camp Cognoms \u00E9s obligatori");
		else alert("El campo Apellidos es obligatorio");
		f.apellidos.focus();
		problem = true;
	}
	if ( (!problem) && ( f.telefono.value == "" ) ) {
		if (_idioma == "en") alert("Telephone field is mandatory");
		else if (_idioma == "ca") alert("El camp Tel\u00E8fon \u00E9s obligatori");
		else alert("El campo Tel\u00e9fono es obligatorio");
		f.telefono.focus();
		problem = true;
	}
	if (!problem) {
		if ( f.email.value == "" ) {
			if (_idioma == "en") alert("E-mail field is mandatory");
		else if (_idioma == "ca") alert("El camp E-mail \u00E9s obligatori");
			else alert("El campo E-mail es obligatorio");
			f.email.focus();
			problem = true;
		} else if ( !esemail(document.getElementById("email"), "E-mail", _idioma) ) {
			problem = true;
		} 
	}
	if ( (!problem) && ( f.comentarios.value == "" ) ) {
		if (_idioma == "en") alert("Remarks field is mandatory");
		else if (_idioma == "ca") alert("El camp Comentaris \u00E9s obligatori");
		else alert("El campo Comentarios es obligatorio");
		f.comentarios.focus();
		problem = true;
	}

	return !problem;
}

$(document).ready(function(){

	if ( $(".flashpopup").length > 0 ) {

		var ahora = new Date().getTime();
		var flash_w = 0;
		var flash_h = 0;

		$.getScript('js/jquery.media.js?i=' + ahora, function(){ /* WMV */

			$.getScript('js/jquery.metadata.v2.js?i=' + ahora, function(){ /* WMV */

				$.getScript('js/flash.js?i=' + ahora, function(){ /* SWF */

					$.getScript('js/jquery.simplemodal.js?i=' + ahora, function(){ /* SWF */

						$.getScript('js/jquery.simplemodal.js?i=' + ahora, function(){

							$(".flashpopup").click(function(e){
								e.preventDefault();

								$(this).removeAttr("target");

								fichero = $(this).attr('href');

								if ( fichero.indexOf(".wmv") > -1 ) { /* WMV */
									flash_w = 400;
									flash_h = 290;

									AbrirModal(flash_w, flash_h);
									$('#ModalInfo').html("<a href='" + $(this).attr('href') + "'>&nbsp;</a>");
									$('#ModalInfo a').media({autoplay:1,width:flash_w, height:flash_h, bgColor:'#FFEA89'});
								
								} else { /* SWF */

									if ( $(".spots").length > 0 ) {
										flash_w = 400;
										flash_h = 290;
									} else {
										flash_w = 500;
										flash_h = 281;
									}

									if ( $.browser.msie ) {
										AbrirModal(flash_w, flash_h);
										$('#ModalInfo').flash({src: $(this).attr('href'),width:flash_w, height:flash_h});
									} else {
										$('#ModalInfo').flash({src: $(this).attr('href'),width:flash_w, height:flash_h});
										AbrirModal(flash_w, flash_h);
									}
								}
							});
						});
					});
				});
			});
		});
	}

	if ( $(".accordion").length > 0 ) {
		$(".accordion > li").each(function(index){
			$(this).attr("class", "fila" + (index%2));
		});

		$(".accordion > li a").click(function(e){
			e.preventDefault();
			$(this).next().toggle("slow");
		}).next().hide();
	}

	if ($("#web.home").length > 0) {
		heightTotal = $("#web.home").height() - $("#cabecera").height() - parseInt($("#cabecera").css("border-bottom-width"),10) - $(".push").height();
		if ($("#cuerpo").height() < heightTotal) $("#cuerpo").height(heightTotal);
	}	

	if ($(".topnav").length > 0) {

		$("ul.topnav > li > a").click(function(e){
			e.preventDefault();		
		}).hover(function(){ //When trigger is clicked...

			//Following events are applied to the subnav itself (moving subnav up and down)  
			$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click  
	  
			$(this).parent().hover(function(){ 
			}, function(){
				$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
			});
	
			//Following events are applied to the trigger (Hover events for the trigger)
		}); 
	}

	if ( $("#formTrabaja").length > 0 ) {

		/* DEBUG 
		$("#formTrabaja input").each(function(i){
			$(this).val($(this).attr("name") + " " + (i+1));
		});
		*/

		$("#formTrabaja").bind("submit", function(){
			return validar_formulario_trabaja($("html").attr("lang"));
		});

		$("#estudios").change(function(){

			if ( $(this).val() == "Otro" ) {
				$("#estudiosOtros").show();
				$("#estudiosOtros").focus();
			} else {
				$("#estudiosOtros").val("");
				$("#estudiosOtros").hide();
			}
		});
	}

	if ( $("#formContacto").length > 0 ) {

		/* DEBUG 
		$("#formTrabaja input").each(function(i){
			$(this).val($(this).attr("name") + " " + (i+1));
		});
		*/

		$("#formContacto").bind("submit", function(){
			return validar_formulario_contacto($("html").attr("lang"));
		});
	}

	if ( $("#preHome").length > 0 ) {
		$.getScript('js/jquery.media.js?i=' + ahora, function(){

			$.getScript('js/jquery.metadata.v2.js?i=' + ahora, function(){

				$.getScript('js/jquery.simplemodal.js?i=' + ahora, function(){

					flash_w = 400;
					flash_h = 290;

					AbrirModal(flash_w, flash_h);
					$("#ModalInfo").html($("#preHome").html());
					$('#ModalInfo a').media({autoplay:1,width:flash_w, height:flash_h, bgColor:'#FFEA89'});
				});
				
			});
				
		});
	}
});

