function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function abrirVentana(theURL,winName,features, myWidth, myHeight, isCenter) { //v3.0
  if(window.screen)if(isCenter)if(isCenter=="true"){
    var myLeft = (screen.width-myWidth)/2;
    var myTop = (screen.height-myHeight)/2;
    features+=(features!='')?',':'';
    features+=',left='+myLeft+',top='+myTop;
  }
  window.open(theURL,winName,features+((features!='')?',':'')+'width='+myWidth+',height='+myHeight);
}


function cuadraIzquierda(str, n) {
// Posa els ceros a l'esquerra (quadrant amb el segon parametre)
  cadena='';

  for (i=0; i < n - str.length; i++)
	  cadena = cadena + '0';
  str = cadena + str;
  return(str);
			
}

function cuadraIzquierdaCE() {
// Posa els ceros a l'esquerra en el codi de l'empresa

  cadena='';
  codi = document.formEmpresa.elements[0].value

  for (i=0; i < 8 - codi.length; i++)
	  cadena = cadena + '0';
  document.formEmpresa.elements[0].value = cadena + codi;
			
}

var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

function makeArray(n) {
   for (var i = 1; i <= n; i++) {
      this[i] = 0;
   } 
   return this;
}

function daysInFebruary(whichYear) {
    return (whichYear % 4 == 0 && (!(whichYear % 100 == 0)
 || (whichYear % 400 == 0)) ? 29 : 28);
}

function isDate(mm, dd, yyyy)
 {
	if (mm !="" && !(mm > 0 && mm < 13))
	{
		alert ("Mes Inválido! Debe introducir un valor entre 1 y 12.");
		return false;
	}
	if (dd !="" && !(dd > 0 && dd < 32))
	{
		alert ("Día inválido! Debe introducir un valor entre 1 y 31.");
		return false;
	}
	if ((dd!="" && mm!="") && dd > daysInMonth[mm])
	{
		alert ("Día inválido para este mes.")
		return false;
	}
	if (yyyy !="" && !(yyyy > 1889 && yyyy < 2099))
	{
		alert ("Año incorrecto! Debe introducir un valor entre 00 y 99.");
		return false;
	}
	if ((mm=="2" || mm=="02" && dd!="" && yyyy!="") && dd > daysInFebruary(yyyy))
	{
		alert ("Día inválido para el mes de febrero del año indicado.!")
		return false;
	}
	return true;
}


/* Funció genérica per la validació de formularis.

* Camp obligatori
@ camp tipus e-mail
# camp tipus CIF (si el format és correcte però falla la lletra, ho corregeix al onChange)
& camp tipus NIF (ídem)
! camp tipus password (mirarà el següent camp per veure si són coincidents)
$ camp tipus data (Es posa als dies del mes d'una data i concatena amb els dos següents per veure
  si la data completa es una data vàlida

*/
var MesUnCop=false;
function validarForm(form) {

	//expresion regular para emails
	var er_email = /^(.+\@.+\..+)$/
	var retorno = true;
     
  // Obtenim els elements del formulari
  camposTexto = form.elements;
  for (x=0; x < camposTexto.length; x++) {
	// 1. Mirem si el camp és de tipus e-mail
	if (camposTexto[x].name) {
	  	if ((camposTexto[x].name).indexOf('@') != -1) {
	  	    if(!er_email.test(camposTexto[x].value)) {
			   camposTexto[x].style.border = "2px solid red";	      
			   retorno = false;    //no submit
			   alert ("El e-mail introducido no es correcto.");
			}
		}
		
		// Mirem si es tipus CIF
	  	/*else if ((camposTexto[x].name).indexOf('#') != -1) {
			 if (!CompruebaDatos(camposTexto[x])) {
	  	      	
				camposTexto[x].style.border = "2px solid red";	
				retorno = false;
				alert("El campo del CIF es incorrecto.");
			 }
		}*/
		// Mirem que si hi ha un camp password, el camp anterior sigui igual
	  	else if ((camposTexto[x].name).indexOf('!') != -1) {
	  	      if (camposTexto[x-1].value != camposTexto[x].value) {	  	      	
  	  	      	camposTexto[x-1].style.border = "2px solid red";
				camposTexto[x].style.border = "2px solid red";	
				alert ("Las contraseñas son distintas.");
				retorno = false; 
	  	      }
		}
		
		// Mirem si és un camp obligatori		
	  	else if ((camposTexto[x].name).indexOf('*') != -1) {
	  	      if (camposTexto[x].value == '') { 
				camposTexto[x].style.border = "2px solid red";	
				//camposTexto[x].value = "Campo obligatorio"; 	
				retorno = false;
	  	      }
		}
	
	}
  }
 if (!retorno) {
 	if (document.getElementById('zonaError'))
		document.getElementById('zonaError').style.display='block'; 
	else
	 	alert("Hay errores u omisiones en el rellenado del formulario. Debe corregirlos para poder enviarlo.");
 }
 else {
 	// Deixem els camps de manera que puguin ser ben processats al servidor:
 	for (x=0; x < camposTexto.length; x++) {	
		camposTexto[x].name=camposTexto[x].name.replace("@","");
		camposTexto[x].name=camposTexto[x].name.replace("&","");
		camposTexto[x].name=camposTexto[x].name.replace("#","");		
		camposTexto[x].name=camposTexto[x].name.replace("!","");
		camposTexto[x].name=camposTexto[x].name.replace("$","");
 	} 	
 }
  
 return retorno;
			
}



// Mirar aquesta funció per validar les dates en la funció general
function validarBanner () {
  if (document.ban.Dia.value == "" || document.ban.Mes.value == "" || document.ban.Any.value == "") {
	alert('Error: La fecha de contratación no es correcta.');	
	return false;
  }    
  if (document.ban.DiaD.value == "" || document.ban.MesD.value == "" || document.ban.AnyD.value == "") {
	alert('Error: La fecha de inicio no es correcta.');	
	return false;
  }  
  if (document.ban.DiaH.value == "" || document.ban.MesH.value == "" || document.ban.AnyH.value == "") {
	alert('Error: La fecha de final no es correcta.');	
	return false;
  }
  if (!isDate(document.ban.Mes.value, document.ban.Dia.value, '20' + cuadraIzquierda(document.ban.Any.value, 2)))
	   return false;    //no submit
  if (!isDate(document.ban.MesD.value, document.ban.DiaD.value, '20' + cuadraIzquierda(document.ban.AnyD.value, 2)))
	   return false;    //no submit
  if (!isDate(document.ban.MesH.value, document.ban.DiaH.value, '20' + cuadraIzquierda(document.ban.AnyH.value, 2)))
	   return false;    //no submit
  if (document.ban.ficheroremoto.value == '') {
    alert ('Utilice el botón Examinar para seleccionar el fichero gráfico, en su sistema de archivos, que desee usar como imagen a mostrar en el banner.');
    return false;
  }  	
}


function ActivaFechas() {
// S'usa en la pàgina de llistat de banners per activar/desactivar els camps de dates quan al combo es seleccion/deselecciona 
// l'opció de Entre Fechas
  if (document.FiltrosBanner.criterio.value == "EntreFechas") {
	document.FiltrosBanner.DiaD.disabled = false
	document.FiltrosBanner.MesD.disabled = false
	document.FiltrosBanner.AnyD.disabled = false	
	document.FiltrosBanner.DiaH.disabled = false
	document.FiltrosBanner.MesH.disabled = false	
	document.FiltrosBanner.AnyH.disabled = false
	document.FiltrosBanner.DiaD.focus() = false
  }
  else {
   	document.FiltrosBanner.DiaD.disabled = true
	document.FiltrosBanner.MesD.disabled = true
	document.FiltrosBanner.AnyD.disabled = true
	document.FiltrosBanner.DiaH.disabled = true
	document.FiltrosBanner.MesH.disabled = true
	document.FiltrosBanner.AnyH.disabled = true
  }
}




// Aquí posarem una crida al normal o potser algo especial (pq hi ha una dependència de camps)
function validarDatosOferta(form) {
  var er_email = /^(.+\@.+\..+)$/
  camposTexto = formOferta.elements;

  for (x=0; x < camposTexto.length; x++) {
    if (camposTexto[x].value == '') 
	{  	
	  	if ((camposTexto[x].name).indexOf('*') != -1) {
			alert("Debe cumplimentar los datos señalados mediante el asterisco en rojo.");
			return false; 
		}  	
    }
  }

  if(camposTexto[7].value != '') {
	if(!er_email.test(camposTexto[7].value)) {
	   alert('El e-mail ' + camposTexto[7].value + ' no es válido');
	   return false;    //no submit
	}
  }

  if (camposTexto[camposTexto.length - 3].value != '' && camposTexto[camposTexto.length - 2].value == '') {
	alert("Si introduce un salario mínimo, entonces debe introducir también un salario máximo.");
	return false; 
  }
  
}





function Mostrar(criteri) {
  blocTodos.style.visibility = 'hidden';  
  blocTrabajos.style.visibility = 'hidden';
  blocFecha.style.visibility = 'hidden';
  blocPoblacion.style.visibility = 'hidden';  
  blocProvincia.style.visibility = 'hidden';
  if (criteri == "Todos") 
  	  blocTodos.style.visibility = 'visible';  
  if (criteri == "Trabajos") 
  	  blocTrabajos.style.visibility = 'visible';  
  if (criteri == "PorFechas") 
  	  blocFecha.style.visibility = 'visible';  
  if (criteri == "PorPoblacion") 
  	  blocPoblacion.style.visibility = 'visible';  
  if (criteri == "PorProvincia") 
  	  blocProvincia.style.visibility = 'visible';  

}




function MarcarTodos(form) {
// Marca tots els checks del formulari passat com a paràmetre
  campos = form.elements;
  for (x=0; x < campos.length; x++) {
    if (campos[x].type == 'checkbox') 
	  campos[x].checked = true;
  }
  return false; // Perquè no es faci submit en FireFox
}

function DesmarcarTodos(form) {
// Desmarca tots els checks del formulari passat com a paràmetre
  campos = form.elements;
  for (x=0; x < campos.length; x++) {
    if (campos[x].type == 'checkbox') 
	  campos[x].checked = false;
  }
}



function adjustIFrameSize(iframeWindow)
{
  var h;
  var w;
  if (iframeWindow.document.height)
  {
    var iframeElement = parent.document.getElementById(iframeWindow.name);
    h = iframeWindow.document.height + 25 + 'px';
    w = iframeWindow.document.width + 25 + 'px';
  }
  else if (document.all)
  {
    var iframeElement = parent.document.all[iframeWindow.name];
    if (iframeWindow.document.compatMode &&
        iframeWindow.document.compatMode != 'BackCompat')
    {
      h =iframeWindow.document.documentElement.scrollHeight + 25 + 'px';
      w =iframeWindow.document.documentElement.scrollWidth + 25 + 'px';
    }
    else
    {
      h =iframeWindow.document.body.scrollHeight + 25 + 'px';
      w =iframeWindow.document.body.scrollWidth + 25 + 'px';
    }
  }
  
  
/*  if (parent.document)
  {
	 if (parent.document.getElementById && parent.document.createTextNode)
    {
      m=parent.document.getElementById("tabMain");
		  m.style.height=h;
		  m=parent.document.getElementById("tabIframeWrapper");
		  m.style.height=h;		
	 }
	} */
}



function calcHeight(iframeName)
{
  var h;
  var w;
  if (window.document.height)
  {
    // Aquí, com més avall, també he tingut que fer algun canvi respecte a lo que xuta bé en pi-sa 
    // Aquest bocí és on entra el Firefox i el de abaix és on entra IE.
    var iframeElement = document.getElementById(iframeName);
    h =frames[iframeName].document.height + 25 + 'px';
    w =frames[iframeName].document.width + 25 + 'px';
  }
  else if (document.all)
  {
    var iframeElement = document.all[iframeName];
    if (window.document.compatMode &&
        window.document.compatMode != 'BackCompat')
    {
      // h =frames[iframeName].document.documentElement.scrollHeight + 25 + 'px';
      // w =frames[iframeName].document.documentElement.scrollWidth + 25 + 'px';
      // Sembl que IE entra aquí quan ho hauria de fer al d'abaix, per això cópio les dos línies de abaix i deixo lo que hi havi comentat
      h =frames[iframeName].document.body.scrollHeight + 25 + 'px';
      w =frames[iframeName].document.body.scrollWidth + 25 + 'px';      
    }
    else
    {
      h =frames[iframeName].document.body.scrollHeight + 25 + 'px';
      w =frames[iframeName].document.body.scrollWidth + 25 + 'px';
    }
  }

 return (h);

  
	
/*  if (document)
  {
	 if (document.getElementById && document.createTextNode)
    {
      m=document.getElementById("tabMain");
		  m.style.height=h;
		  m=document.getElementById("tabIframeWrapper");
		  m.style.height=h;		
    }
	}
*/
}


 function CompruebaDatos(elCIF) 
 {
  
  var resul = false;
  // pasar a mayúsculas
  var temp = elCIF.value.toUpperCase(); 
  var aux = "";

  if (!/^[A-Za-z0-9]{9}$/.test(temp)) // Son 9 dígitos?
   alert ("Longitud incorrecta, un CIF consta de 9 dígitos");
  else if (!/^[ABCDEFGHKLMNPQS]/.test(temp)) 
   // Es una    letra de las admitidas ?
   alert("El primer dígito es  incorrecto, debe ser una letra de las siguientes: A,B,C,D,E,F,G,H,K,L,M,N,P,Q,S ");
  else {
   resul = true;
   aux = String(ValidaCIF(elCIF.value.toUpperCase()));
   if (aux.length != 1)
   	alert ("El CIF es incorrecto. Por favor corríjalo.")
   else
    elCIF.value = elCIF.value.substr(0,8) + ValidaCIF(elCIF.value.toUpperCase());
  }
   return resul;
  }

  // La función recibe el CIF completo: A58818501
  function ValidaCIF(F) 
  {

   var v1 = new Array(0,2,4,6,8,1,3,5,7,9); 
   var temp = 0;

   for( i = 2; i <= 6; i += 2 ) 
   {
    temp = temp + v1[ parseInt(F.substr(i-1,1))];
    temp = temp + parseInt(F.substr(i,1));
   };

   temp = temp + v1[ parseInt(F.substr(7,1))];
   temp = (10 - ( temp % 10));

   if( temp == 10 )
    return "J";
   else
    return temp;
    return true;
  }
















