/******************************************************************/
/*                   Validar formulario 1.0                       */
/*           Script que valida el formulario de admision          */
/*           de los masters executive y muestra div ocultos       */
/*         por Santi Rivas, santi.rivas [at] gmail.com            */
/******************************************************************/
/*               http://www.santirivas.info                       */
/******************************************************************/
/*                                                                */
/* MIT style license:                                             */
/* http://en.wikipedia.org/wiki/MIT_License                       */
/*                                                                */
/******************************************************************/


/******************************************************************/
/*                                                                */
/*			MUESTRA BLOQUES OCULTOS EN FORMULARIO                 */
/*																  */
/******************************************************************/


//muestra bloque 1
function mostrarReferencia(){
//Si la opcion con id CARGOEMP (dentro del documento > formulario con name form1 >     y a la vez el valor CARGOEMP) igual valor establecido
if (document.form1.Cargo_empresa.value== 'OTROS') {
//muestra (cambiando la propiedad display del estilo) el div con id 'desdeotro'
document.getElementById('desdeotro').style.display='block';
//por el contrario, el valor de CARGOEMP no es el establecido
} else {
//oculta el div con id 'desdeotro'
document.getElementById('desdeotro').style.display='none';
}
}
//muestra bloque 2
function mostrarReferencia2(){
if (document.form1.Area_empresa.value== 'OTRAS') {
document.getElementById('desdeotro2').style.display='block';
} else {
document.getElementById('desdeotro2').style.display='none';
}
}
//muestra bloque 3
function mostrarReferencia3(){
if (document.form1.Empresa_Club_Foro_europeo.value== 'No') {
document.getElementById('desdeotro3').style.display='block';
} else {
document.getElementById('desdeotro3').style.display='none';
}
}
//muestra bloque 4
function mostrarReferencia4(){
if (document.form1.CLUBEMPCONTAC.value== 'Si') {
document.getElementById('desdeotro4').style.display='block';
} else {
document.getElementById('desdeotro4').style.display='none';
}
}



/******************************************************************/
/*                                                                */
/*			FUNCION QUE VALIDA LOS DATOS QUE ENVIA EL USUARIO     */
/*																  */
/******************************************************************/

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

return (true)
} else {

return (false);
}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


//FUNCION QUE VALIDA EL FORMULARIO
function valida_envia(){

//comprueba que ha selecionado un programa
if (document.form1.Programa[0].checked == false && document.form1.Programa[1].checked == false )
{
alert ("Debe seleccionar un programa en la solicitud");
document.form1.Programa[0].focus();
return 0;
}


//comprobamos si es nacional, si lo es comprobamos si a selecciona provincia
var listaPais = document.getElementById("Pais");
var valorPaisSeleccionado = listaPais.options[listaPais.selectedIndex].value;

if (valorPaisSeleccionado == 'ESPAÑA') {
	var listaProvincia = document.getElementById("Provincia");
	var valorProvinciaSeleccionado = listaProvincia.options[listaProvincia.selectedIndex].value;
	if (valorProvinciaSeleccionado == 'INTERNACIONAL') {
		alert("Debe seleccionar una provincia en la solicitud");
		document.form1.Provincia.focus();
		return false;
	}

}

//comprobamos direcciones de correo minimo una obliglatoria
if(document.form1.E_mail_personal.value =="" && document.form1.E_mail_laboral.value ==""){
	alert("Debe introducir almenos una dirección de correo electrónico");
	return false;
}else if(document.form1.E_mail_personal.value != ""){
	if(validarEmail(document.form1.E_mail_personal.value) == false){
		alert("La direccion de correo personal introducida no es correcta");
		document.form1.E_mail_personal.focus();
		return false;
	}
}else if(document.form1.E_mail_laboral.value != ""){
	if(validarEmail(document.form1.E_mail_laboral.value) == false){
		alert("La direccion de correo laboral introducida no es correcta");
		document.form1.E_mail_laboral.focus();
		return false;
	}
}





if (document.form1.Programa[0].checked == true || document.form1.Programa[1].checked == true || document.form1.Programa[2].checked == true)
				   {
MM_validateForm('Nombre','','R','Apellidos','','R','Fecha_nacimiento','','R','Lugar_de_Nacimiento','','R','Pais','','R','Provincia','','R','Poblacion','','R','Codigo_Postal','','R','Telefono','','RisNum','Movil','','RisNum','DNI','','R','Sexo','','R','Institucion_1','','R','Titulo_1','','R','Nivel_estudios_1','','R','Ano_estudios_1','','R','Nivel_ingles','','RisNum','Nivel_frances','','RisNum','Nivel_aleman','','RisNum','Experiencia_total_acumulada','','R','Empresa','','R','Area_empresa','','R','Tiempo_empleado','','R','Cargo_empresa','','R','CNAE','','R','Empresa_Club_Foro_europeo','','R');
				   }

}



function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' debe introducir una direccion valida de correo electronico.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' debe introducir un numero.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' es un campo requerido.\n'; }
  } if (errors)
  {
  alert('Su formulario contiene los siguientes errores:\n'+errors);
  document.MM_returnValue = (errors == '');
  } else {
document.form1.submit();
  }
}
