/* - - - - - - - - - - - - - 
Javascript para el sitio web internetamedida(c) 2004.
- - - - - - - - - - - - - */
mensajes = new Array;
mensajes["en"]=new Array("Please enter required fields.",
						 "Please enter correct e-mail."
						);
mensajes["es"]=new Array("Debe introducir los campos obligatorios.",
						 "Debe introducir una dirección de correo válida.",
						 "Debe introducir un CIF/NIF válido."
						);

lenguage = "es";

/* Patrones para validar campos */
var pt_email=/\w.+\@\w.+\.[a-z]{2,3}$/;
var pt_cif=/^[AaBbCcDdEeFfGgHhJjKkLlMmNnPpQqSsUuVvWwRr][0-9]{7}[AaBbCcDdEeFfGgHhIiJj0123456789]$/;
var pt_nif=/^([XxYyZz]|[0-9])[0-9]{7}([a-z]|[A-Z])$/;
var pt_digitos = /^\d*$/;

function controlContactar (form, modo) {
  if (modo == 'inf') {
	form.FtxEmail.className = 'FieldObl';
	form.FtxTelefono.className = 'FieldOpc';
  }
  else if (modo == 'cit') {
    form.FtxEmail.className = 'FieldObl';
    form.FtxTelefono.className = 'FieldObl';
  }
  else if (modo == 'com') {
	form.FtxEmail.className = 'FieldOpc';
    form.FtxTelefono.className = 'FieldOpc';
  }
  else {
  	form.FtxEmail.className = 'FieldOpc';
    form.FtxTelefono.className = 'FieldOpc';
  }

}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

/* Funciones rollover y rollout de imagenes */
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;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
/* Funciones rollover y rollout de imagenes */

function check_obli(form,lang)
{
	for(i=0;i < form.length;i++)
	{
		if(form.elements[i].className == 'FieldObl' && (form.elements[i].value == null || form.elements[i].value == ''))
		{
			alert (mensajes[lang][0]);
			return(false);
		}
	}
	return(true);
}

function checkEmail (objTxt,lang) {
  if (objTxt.value.length == 0)
  {
	return (true);
  }
  if (objTxt.value.indexOf("@") > -1) {
    // Partimos la cadena desde la posición de la "@"(esta no inclusive) hasta el final de la cadena 
	var CadAux  = objTxt.value.substr(objTxt.value.indexOf("@")+1 ,objTxt.value.length);
	// Comprobamos si existe alguna otra "@" en la cadena ya que si así fuese la dirección seria incorrecta 
	if (CadAux.indexOf("@")>-1) {
	  objTxt.focus();
	  objTxt.select();
	  return(false);
	}
	// Si no tiene más que una "@" podemos pasar a la siguiente consulta para su validación 
	else {
	  // Comprobamos que al menos entre la "@" y el "." tiene que haber al menos un caracter 
	  if (CadAux.indexOf(".")-CadAux.indexOf("@")>1) {
	    // Cortamos la cadena restante a partir del "." hasta el final de esta 
	    var CadAux2 = CadAux.substr(CadAux.indexOf(".")+1 ,CadAux.length);
		// Comprobamos que al menos tiene que haber 1 caracter despues del "." 
	    if (CadAux2.length<1)
	    {
		  objTxt.focus();
		  objTxt.select();
		  return(false);
	    }
	  } // fin if (CadAux.indexOf(".")-CadAux.indexOf("@")>1)
	  else
	  {
	    objTxt.focus();
		objTxt.select();
		return(false);
	  } // fin else if (CadAux.indexOf(".")-CadAux.indexOf("@")>1)
    }
  } // fin if (objTxt.value.indexOf("@") > -1)
  else
  {
    objTxt.focus();
	objTxt.select();
	return(false);
  }
	  
  return (true);

}

function validarCampo (obj, patron, lenguage) {
  var indice = "";
  var pt;
  if (patron == "pt_email") {
    indice = 1;
	pt = pt_email;
  }

  cadena = obj.value;
  if (cadena.length > 0) {
    if (cadena.search(pt) != 0) { 
      alert(mensajes[lenguage][indice]);
	  obj.focus();
	  obj.select();
    }
  }
}

function onblurCheckEmail (objTxt, lang) {
    if (checkEmail(objTxt,lang) == false)
		alert (mensajes[lang][1]);
}

function borrarFormContactar (form, modo) {
 
  form.FtxNombre.value = '';
  form.FtxEmpresa.value = '';
  form.FtxEmail.value = '';
  form.FtxTelefono.value = '';
  form.FtxDireccion.value = '';
  form.FtxProvincia.value = '';
  form.FtxPais.value = '';
  form.FtxWeb.value = '';
  form.FtxComentarioPre.value = '';
  form.FtxComentario.value = '';

  form.CbRazon[0].checked = true;
  controlContactar(form, 'com');
  
}

function onclickForm (objFrm, lang) {
  	
	if (check_obli(objFrm,lang) && comprobarCIF(objFrm.FtxCIF) && comprobarEmail(objFrm.FtxEmail, lang)){
    	objFrm.submit();
	}	
	else
	    return(false); 
}

function onclickFormContactar (objFrm, lang) {
  	
	if (check_obli(objFrm,lang) && comprobarEmail(objFrm.FtxEmail, lang)){
    	objFrm.submit();
	}	
	else
	    return(false);
}

function onclearForm (objFrm) {
	alert ('hola');
	//objFrm.reset();
	
}

function replaceEnterporBR (campo) {

  campo = campo.replace (/\r\n/gi,"<BR>");
  return campo;
}

function tratarComentarios(objOrigen, objDestino) {
	objDestino.value = replaceEnterporBR(objOrigen.value);
}

function RecordarCV(nombreForm) {

  objForm = parent.Oculto;
  //objForm = window.opener;
  
  if (nombreForm == 'frmCV1') {
    objForm = objForm.document.frmCV;
	document.frmCV1.FtxNombre.value    = objForm.FtxNombre.value;
	document.frmCV1.FtxEmail.value     = objForm.FtxEmail.value;
	document.frmCV1.FtxTelefono.value  = objForm.FtxTelefono.value;
	document.frmCV1.FtxDireccion.value = objForm.FtxDireccion.value;
	document.frmCV1.FtxProvincia.value = objForm.FtxProvincia.value;
	document.frmCV1.FtxPais.value      = objForm.FtxPais.value;
	document.frmCV1.FtxEdad.value	   = objForm.FtxEdad.value;
	document.frmCV1.FtxEstado.value	   = objForm.FtxEstado.value;	
  }
  else if (nombreForm == 'frmCV2') {
    objForm = objForm.document.frmCV;
	document.frmCV2.FtxFormacionAcadPre.value = objForm.FtxFormacionAcadPre.value;
	document.frmCV2.FtxFormacionSupPre.value  = objForm.FtxFormacionSupPre.value;
  }
  else if (nombreForm == 'frmCV3') {
    objForm = objForm.document.frmCV;
	document.frmCV3.FtxSoft1.value = objForm.FtxSoft1.value;
	document.frmCV3.FtxSoft2.value = objForm.FtxSoft2.value;
	document.frmCV3.FtxSoft3.value = objForm.FtxSoft3.value;
	if (objForm.FliNivelSoft1.value != "")
	  document.frmCV3.FliNivelSoft1.value = objForm.FliNivelSoft1.value; 
	if (objForm.FliNivelSoft2.value != "")
	  document.frmCV3.FliNivelSoft2.value = objForm.FliNivelSoft2.value;
	if (objForm.FliNivelSoft3.value != "")
	  document.frmCV3.FliNivelSoft3.value = objForm.FliNivelSoft3.value;    
	document.frmCV3.FtxLng1.value = objForm.FtxLng1.value;
	document.frmCV3.FtxLng2.value = objForm.FtxLng2.value;
	document.frmCV3.FtxLng3.value = objForm.FtxLng3.value;
	if (objForm.FliNivelLng1.value != "") 
	  document.frmCV3.FliNivelLng1.value = objForm.FliNivelLng1.value; 
	if (objForm.FliNivelLng2.value != "") 
	  document.frmCV3.FliNivelLng2.value = objForm.FliNivelLng2.value;
	if (objForm.FliNivelLng3.value != "") 
	  document.frmCV3.FliNivelLng3.value = objForm.FliNivelLng3.value;
  }
  else if (nombreForm == 'frmCV4') {
    objForm = objForm.document.frmCV;
	document.frmCV4.FtxFechaE1.value = objForm.FtxFechaE1.value;
	document.frmCV4.FtxFechaE2.value = objForm.FtxFechaE2.value;
	document.frmCV4.FtxFechaE3.value = objForm.FtxFechaE3.value;
	document.frmCV4.FtxEmpresa1.value = objForm.FtxEmpresa1.value;
	document.frmCV4.FtxEmpresa2.value = objForm.FtxEmpresa2.value;
	document.frmCV4.FtxEmpresa3.value = objForm.FtxEmpresa3.value;
	document.frmCV4.FtxCargo1.value = objForm.FtxCargo1.value;
	document.frmCV4.FtxCargo2.value = objForm.FtxCargo2.value;
	document.frmCV4.FtxCargo3.value = objForm.FtxCargo3.value;
	document.frmCV4.FtxTrabajo1.value = objForm.FtxTrabajo1.value;
	document.frmCV4.FtxTrabajo2.value = objForm.FtxTrabajo2.value;
	document.frmCV4.FtxTrabajo3.value = objForm.FtxTrabajo3.value;
	document.frmCV4.FtxLabor1.value = objForm.FtxLabor1.value;
	document.frmCV4.FtxLabor2.value = objForm.FtxLabor2.value;
	document.frmCV4.FtxLabor3.value = objForm.FtxLabor3.value;
  }
  else if (nombreForm == 'frmCV5') {
    objForm = objForm.document.frmCV;
	document.frmCV5.FtxPrExpectativasPre.value = objForm.FtxPrExpectativasPre.value;
	document.frmCV5.FtxPrInteresPre.value = objForm.FtxPrInteresPre.value;
	document.frmCV5.FtxPrConflictoPre.value = objForm.FtxPrConflictoPre.value;
	document.frmCV5.FtxPrSolucionPre.value = objForm.FtxPrSolucionPre.value;
	document.frmCV5.FtxPrCreatividadPre.value = objForm.FtxPrCreatividadPre.value;
  }
  
}

function MemorizarCV(nombreForm, numEtapa) {
  //objForm = window.opener;
  objForm = parent.Oculto;
  if (nombreForm == 'frmCV1') {
    objForm = objForm.document.frmCV;
	objForm.FtxNombre.value    = document.frmCV1.FtxNombre.value;
	objForm.FtxEmail.value 	   = document.frmCV1.FtxEmail.value;
	objForm.FtxTelefono.value  = document.frmCV1.FtxTelefono.value;
	objForm.FtxDireccion.value = document.frmCV1.FtxDireccion.value;
	objForm.FtxProvincia.value = document.frmCV1.FtxProvincia.value;
	objForm.FtxPais.value      = document.frmCV1.FtxPais.value;
	objForm.FtxEdad.value      = document.frmCV1.FtxEdad.value;
	objForm.FtxEstado.value    = document.frmCV1.FtxEstado.value;	
  }
  else if (nombreForm == 'frmCV2') {
    objForm = objForm.document.frmCV;
	objForm.FtxFormacionAcadPre.value    = document.frmCV2.FtxFormacionAcadPre.value;
	objForm.FtxFormacionSupPre.value 	   = document.frmCV2.FtxFormacionSupPre.value;
  }
  else if (nombreForm == 'frmCV3') {
    objForm = objForm.document.frmCV;
	objForm.FtxSoft1.value = document.frmCV3.FtxSoft1.value;
	objForm.FtxSoft2.value = document.frmCV3.FtxSoft2.value; 
	objForm.FtxSoft3.value = document.frmCV3.FtxSoft3.value;
	objForm.FliNivelSoft1.value = document.frmCV3.FliNivelSoft1.value;
	objForm.FliNivelSoft2.value = document.frmCV3.FliNivelSoft2.value;
	objForm.FliNivelSoft3.value = document.frmCV3.FliNivelSoft3.value;
	objForm.FtxLng1.value = document.frmCV3.FtxLng1.value;
	objForm.FtxLng2.value = document.frmCV3.FtxLng2.value;
	objForm.FtxLng3.value = document.frmCV3.FtxLng3.value;
	objForm.FliNivelLng1.value = document.frmCV3.FliNivelLng1.value; 
	objForm.FliNivelLng2.value = document.frmCV3.FliNivelLng2.value;
	objForm.FliNivelLng3.value = document.frmCV3.FliNivelLng3.value;
  }
  else if (nombreForm == 'frmCV4') {
    objForm = objForm.document.frmCV;
	objForm.FtxFechaE1.value = document.frmCV4.FtxFechaE1.value;
	objForm.FtxFechaE2.value = document.frmCV4.FtxFechaE2.value;
	objForm.FtxFechaE3.value = document.frmCV4.FtxFechaE3.value;
	objForm.FtxEmpresa1.value = document.frmCV4.FtxEmpresa1.value;
	objForm.FtxEmpresa2.value = document.frmCV4.FtxEmpresa2.value;
	objForm.FtxEmpresa3.value = document.frmCV4.FtxEmpresa3.value;
	objForm.FtxCargo1.value = document.frmCV4.FtxCargo1.value;
	objForm.FtxCargo2.value = document.frmCV4.FtxCargo2.value;
	objForm.FtxCargo3.value = document.frmCV4.FtxCargo3.value;
	objForm.FtxTrabajo1.value = document.frmCV4.FtxTrabajo1.value;
	objForm.FtxTrabajo2.value = document.frmCV4.FtxTrabajo2.value;
	objForm.FtxTrabajo3.value = document.frmCV4.FtxTrabajo3.value;
	objForm.FtxLabor1.value = document.frmCV4.FtxLabor1.value;
	objForm.FtxLabor2.value = document.frmCV4.FtxLabor2.value;
	objForm.FtxLabor3.value = document.frmCV4.FtxLabor3.value;
  }
  else if (nombreForm == 'frmCV5') {
    objForm = objForm.document.frmCV;
	objForm.FtxPrExpectativasPre.value = document.frmCV5.FtxPrExpectativasPre.value;
	objForm.FtxPrInteresPre.value = document.frmCV5.FtxPrInteresPre.value;
	objForm.FtxPrConflictoPre.value = document.frmCV5.FtxPrConflictoPre.value;
	objForm.FtxPrSolucionPre.value = document.frmCV5.FtxPrSolucionPre.value;
	objForm.FtxPrCreatividadPre.value = document.frmCV5.FtxPrCreatividadPre.value;
  }
  
  if (numEtapa == 1)
    window.location = 'popTrabajo_CV_online1.html';
  else if (numEtapa == 2)
    window.location = 'popTrabajo_CV_online2.html';
  else if (numEtapa == 3)
    window.location = 'popTrabajo_CV_online3.html'; 
  else if (numEtapa == 4)
    window.location = 'popTrabajo_CV_online4.html'; 
  else if (numEtapa == 5)
    window.location = 'popTrabajo_CV_online5.html';		
  
}

function EnviarCV(lang) {
  //objForm = window.opener;
  objForm = parent.Oculto;
  objForm = objForm.document.frmCV;
  
  tratarComentarios(objForm.FtxFormacionAcadPre, objForm.FtxFormacionAcad);
  tratarComentarios(objForm.FtxFormacionSupPre, objForm.FtxFormacionSup);
  tratarComentarios(objForm.FtxPrExpectativasPre, objForm.FtxPrExpectativas);
  tratarComentarios(objForm.FtxPrInteresPre, objForm.FtxPrInteres);
  tratarComentarios(objForm.FtxPrConflictoPre, objForm.FtxPrConflicto);
  tratarComentarios(objForm.FtxPrSolucionPre, objForm.FtxPrSolucion);
  tratarComentarios(objForm.FtxPrCreatividadPre, objForm.FtxPrCreatividad);
  
  if (check_obli(objForm,lang)) {
    	objForm.submit();
  }	
  else {
    return(false);
  }

}

function LimpiarCV() {
  //objForm = window.opener;
  objForm = parent.Oculto;
  objForm = objForm.document.frmCV;
  objForm.reset();
}


function abrirpop(){

	MM_openBrWindow('noticias_aniversario_2006.html','noticias','scrollbars=yes,scroll:yes,dialogLeft:1,dialogRight:1,dialogTop:1,width=480,height=400');

}

function comprobarCIF (obj) {

  cadena = obj.value;
 
  if (cadena.length == 0)
  	return (true);
  
  else if (cadena.length > 0) {
    if ( (cadena.search(pt_cif) != 0) && (cadena.search(pt_nif) != 0) ){ 
      alert(mensajes[lenguage][2]);
	  obj.focus();
	  obj.select();
	  return (false);
    }
  }
  
  if(obj.className == 'fieldCon')
  	return (true);

  else{
	  if(validarCIF(obj) || comprobarNIF(obj,false))
			return (true);
	  else{
			alert(mensajes[lenguage][2]);
			obj.focus();
			obj.select();
			return (false);
	  }
  }
}

function comprobarEmail (obj, lang) {
  cadena = obj.value;
  
  if (cadena.length == 0)
  	return (true);
  
  else {
    if (cadena.search(pt_email) != 0) { 
      alert(mensajes[lang][1]);
	  obj.focus();
	  obj.select();
	  return (false);
    }
	else{
	  return (true);
	}
  }
}

function validarCIF(obj){

	cadena = obj.value;
	
	var pares = 0;
    var impares = 0;
    var suma;
    var ultima;
    var unumero;
    var uletra = new Array("J", "A", "B", "C", "D", "E", "F", "G", "H", "I");
    var xxx;
	
    ultima = cadena.substr(8,1);
	cadena = cadena.toUpperCase();
	
    for (var cont = 1 ; cont < 7 ; cont ++){
        xxx = (2 * parseInt(cadena.substr(cont++,1))).toString() + "0";
        impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1));
        pares += parseInt(cadena.substr(cont,1));
    }

    xxx = (2 * parseInt(cadena.substr(cont,1))).toString() + "0";
    impares += parseInt(xxx.substr(0,1)) + parseInt(xxx.substr(1,1));
    
    suma = (pares + impares).toString();
	
    unumero = parseInt(suma.substr(suma.length - 1, 1));
    unumero = (10 - unumero).toString();
    if(unumero == 10) unumero = 0;
	
    if ((ultima == unumero) || (ultima == uletra[unumero]))
          return (true);
    else
		  return (false);

}

function comprobarNIF (obj,mostrar) {

  cadena = obj.value;
  
  if(mostrar==null)
  	mostrar=true;

  if (cadena.length == 0)
  	return (true);

  else if (cadena.length > 0) {
    if (cadena.search(pt_nif) != 0) {
	  if(mostrar){
      	alert(mensajes[lenguage][2]);
	  	obj.focus();
	  	obj.select();
	  }
	  return (false);
    }
  }
  
  if(obj.className == 'fieldCon')
  	return (true);
  
  else{
	  var lockup = 'TRWAGMYFPDXBNJZSQVHLCKE'; 

	  numero = cadena.substr(0,8);
	  numero = numero.toUpperCase();
	  numero = numero.replace(/X/,"0");
	  numero = numero.replace(/Y/,"1");
	  numero = numero.replace(/Z/,"2");
	  
	  letra = lockup.charAt(numero % 23);
	
	  if(letra == (cadena.substr(8,1)).toUpperCase())
		return (true);
		
	  else {
		  if(mostrar){
			alert(mensajes[lenguage][2]);
			obj.focus();
			obj.select();
		  }
		return (false);
	  }
  }	
}

function validar (obj, patron) {
	var subcadena = "";
	cadena = obj.value;
	if (cadena.search(patron) != 0) {
	  subcadena = cadena.charAt(0);
	  if (subcadena.search(patron) != 0)
	    subcadena = "";
	  for (irep=1;irep<cadena.length;irep++) {
	    auxiliar = subcadena + cadena.charAt(irep);
	    if (auxiliar.search(patron) == 0)  
			subcadena += cadena.charAt(irep);	
	  }
	  obj.value = subcadena; 
	}
    
}
