function validarMail(nom){
	var est=false;
	control = document.getElementById(nom);
	var mail=control.value;
	
	control.style.backgroundColor="#ffffff";
	cad=control.name + '_span';
	document.getElementById(cad).innerHTML="";
	
	var re=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/;
	if(!re.exec(mail)) {
		control.style.backgroundColor="#ff0000";
		document.getElementById(cad).innerHTML="<font color='#ff0000'>*</font>";
		est=true;
	}
	
	return est;
}

function validarEnter(evento,control){
	var key; 
         
    if(window.event){ 
	    key = evento.keyCode; 
        if (key == 13) {
			var btn1 = document.getElementById(control);
	        btn1.focus();
        } 
    } else if(evento.which){ 
	    key = evento.which; 
        if (key == 13){
			var btn1 = document.getElementById(control);
	        btn1.focus();
        } 
    } 
}

function soloNumerosFijo(evento){
	var key; 
         
    if(window.event){ 
	    key = evento.keyCode; 
        if ((key < 48 || key > 57) && key!=8 && key!=45 && key!=65 && key!=88 && key!=97 && key!=120) { 
	        window.event.keyCode=0; 
            return false; 
        } 
    } else if(evento.which){ 
	    key = evento.which; 
        if ((key < 48 || key > 57) && key!=8 && key!=45 && key!=65 && key!=88 && key!=97 && key!=120) { 
	        return false; 
        } 
    } 
    return true; 
}


// AJAX DOCUMENT
function nuevoAjax(xmlhttp){
	
	   try {
		   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
	   catch (e) {
		   try {
			 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		  } 
		  catch (E) {
			  xmlhttp = false;
		  }
	   }
	   if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		  xmlhttp = new XMLHttpRequest();
	   }
	   return xmlhttp
	}

function cargarSWF(direccion,ancho,alto)
{ 
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+ancho+'" height="'+alto+'">'); 
    document.write('<param name="movie" value="'+direccion+'" /><param name="quality" value="high"><param name="menu" value="false" /><param name="wmode" value="transparent" /><embed src="'+direccion+'" quality="high" wmode=transparent pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+ancho+'" height="'+alto+'"></embed><noembed> <div align="center">Su navegador no dipone de una versión apropiada de flash player. <br> Puede obtenerla gratuitamente <a href="http://www.adobe.com/es/products/flashplayer/">aqui </a> </div> </noembed></object>'); 
}

//functiones propias y validaciones
function cargarContenido(pagina,destino){
	   var contenedor;
	   var ajax;
	   
	   contenedor = document.getElementById(destino);
	   ajax = nuevoAjax(ajax);
	   ajax.open("GET", pagina, true);
	   ajax.onreadystatechange=function() {
		  if (ajax.readyState==4) {
			 contenedor.innerHTML = ajax.responseText;
		  }
	   }
	   ajax.send(null);
	}
	
function valDatoRegistro(tipo, documento){
	if(tipo.value==0){
		alert('Seleccione un tipo de documento');
		tipo.focus();
		return false;
	}
	if(tipo.value==1 && documento.value.length<8){
		alert('Escriba correctamente su DNI');
		return false;
	}
	if(tipo.value==2 && documento.value.length<4){
		alert('Escriba correctamente su Nro CIP');
		return false;
	}
	return true;
}

function selProvincia(dpto){
	cargarContenido('Cvista/pp_ubigeo.php?departamento='+dpto,'ubigeo');
}

function selDistrito(dpto,prov){
	cargarContenido('Cvista/pp_ubigeo.php?departamento='+dpto+'&provincia='+prov,'ubigeo');
}

function validarTipoArchivo(control,tipo){
	if(tipo==1) array=new Array(".doc",".docx");
	if(tipo==2) array=new Array(".gif",".jpg",".png");
	var texto=control.value;
	var ext = (texto.substring(texto.lastIndexOf("."))).toLowerCase(); 
	
	var permitida = false;
	for (var i = 0; i < array.length; i++) {
		if (array[i] == ext) {
			permitida = true;

			break;
		}
	}
	
	var error=document.getElementById('error');
	error.value=0;
	control.style.backgroundColor="#ffffff";
	if(!permitida){
		alert("Usted solo puede subir archivos con extensiones " + (array.join(" ")) + "\nPor favor seleccione un nuevo archivo");
		error.value=1;
		control.value="";
		control.style.backgroundColor="#ff0000";
		control.focus();
	}
}
function validarTexto(array){
	var control,est=false,cad='';
	for(i=0;i<array.length;i++){
		control = document.getElementById(array[i]);
		control.style.backgroundColor="#ffffff";
		cad=control.name + '_span';
		document.getElementById(cad).innerHTML="";
		if(control.value.length==0){
			control.style.backgroundColor="#ff0000";
			//cad=control.name + '_span';
			document.getElementById(cad).innerHTML="<font color='#ff0000'>*</font>";
			est=true;
		}
	}
	return est;
}

// Funcion registrado tambien en bolsa_trabajo.js
function soloNumeros(evento){
	var key; 
         
    if(window.event){ 
	    key = evento.keyCode; 
        if ((key < 48 || key > 57) && key != 8) { 
	        window.event.keyCode=0; 
            return false; 
        } 
    } else if(evento.which){ 
	    key = evento.which; 
        if ((key < 48 || key >  57) && key != 8){ 
	        return false; 
        } 
    } 
    return true; 
}

function validarCombo(array){
	var control,est=false,cad='';
	for(i=0;i<array.length;i++){
		control = document.getElementById(array[i]);
		control.style.backgroundColor="#ffffff";
		cad=control.name + '_span';
		document.getElementById(cad).innerHTML="";
		if(control.value==0){
			control.style.backgroundColor="#ff0000";
			//cad=control.name + '_span';
			document.getElementById(cad).innerHTML="<font color='#ff0000'>*</font>";
			est=true;
		}
	}
	return est;
}

function validarCheckBox(frm){
	var est=false;
	var elem;
	for(var i=0;i<frm.elements.length;i++){
		if(frm.elements[i].type=="checkbox"){
			elem=frm.elements[i];
			if(elem.checked==true) est=true;
		}
	}
	return est;
}

function opcTrabajo(opc){
	var bb=true;
	if(opc==2) bb=false;
	else{
		document.getElementById("empresa").style.backgroundColor="#E5E5E5";
		document.getElementById("empresa_span").innerHTML="";
		
		document.getElementById("cargo").style.backgroundColor="#E5E5E5";
		document.getElementById("cargo_span").innerHTML="";
	}
	document.getElementById("btnBuscarEmpresa").disabled=bb;
	document.getElementById("btnBuscarCargo").disabled=bb;
	document.getElementById("telOficina").disabled=bb;
	document.getElementById("contacto").disabled=bb;
}

function selRegistro(nom,tipo,evento){
	var strFeatures = "dialogTop=100px; dialogLeft=100px; dialogWidth=560px;" +
					"dialogHeight=400px; center=yes; help=no; font-family:Arial; font-size:12px"
					
		var valor = window.showModalDialog('Cvista/buscarRegistro.php?tipo='+tipo,'ventana1',strFeatures);
		valor += "";
		//alert(valor);
		if(valor.length>0){
			var tmp=valor.split(';');
			if(tmp.length==2){
				document.getElementById(nom).value = tmp[1];
				if(tipo==1) document.getElementById('idEmpresa').value = tmp[0];
				if(tipo==2) document.getElementById('idCargo').value = tmp[0];
				if(tipo==3) document.getElementById('idProfesion').value = tmp[0];
				if(tipo==4) document.getElementById('idEspecialidad').value = tmp[0];
			}
			
		}	
}

function verDetalle(){
	var doc = document.getElementById('documento').value;
	var tipo=3;
	var dato="doc="+doc+"&tipo="+tipo;
	cargarContenido('Cvista/pp_Detalle.php?'+dato,'detalle');
}

function cargarTematica(cod){
	var dato="cod="+cod;
	cargarContenido('Cvista/ppTematica.php?'+dato,'detTematica');
}

function temRegistrar(){
	if(!valDatoDetalle()){
		document.frmDetalle.submit();
	}
}

//modificado 14-02-2011
function valDatoDetalle(){
	var est=true;
	var array=new Array("apellido","nombre","especialidad","email_1","direccion");
	if (validarTexto(array)==true) est=false;
	
	var array2=new Array("departamento","provincia","distrito","sede");
	if (validarCombo(array2)==true) est=false;

	//Validar Centro de trabajo
	var opc=document.frmDetalle.opcion[1].checked;
	if(opc==true){
		var array3=new Array("empresa","cargo");
		if (validarTexto(array3)==true) est=false;
	}
	
	//Asignar titulo al participante
	document.getElementById('titulo1').value=document.getElementById('tit1').value;
	
	//Validacion de email
	if(validarMail('email_1')==true){
		est=false;
		alert("email obligatorio invalido!!!");
	}
	
	var mail2=document.getElementById('email_2').value;
	if(mail2.length>0){
		if(validarMail('email_2')==true){
			est=false;
			alert("email opcional invalido!!!");
		}	
	}
	
	//validar nroColegiatura
	var colegio=document.getElementById('colProf').value;
	if(colegio>0){
		var nroCole=document.getElementById('nroCole').value;
		nroCole = nroCole.replace(/^\s*|\s*$/g,"");
		if(nroCole.length<=0){
			alert('Escriba su numero de colegiatura!!!');
			est=false;
		}else{
			document.getElementById('nroColegiatura').value=nroCole;
			document.getElementById('colProfesional').value=colegio;
		}
		
	}
	
	//Validar telefonos
	var tel1=document.getElementById('telPersonal').value;
	var tel2=document.getElementById('claroMovil').value;
	var tel3=document.getElementById('claroRadio').value;
	var tel4=document.getElementById('movistarMovil').value;
	var tel5=document.getElementById('movistarRadio').value;
	var tel6=document.getElementById('nextelRadio').value;
	
	tel3 = tel3.replace(/^\s*|\s*$/g,"");
	tel5 = tel5.replace(/^\s*|\s*$/g,"");
	tel6 = tel6.replace(/^\s*|\s*$/g,"");
	
	var ccant=0;
	if(tel1.length>0) ccant++;
	if(tel2.length>0) ccant++;
	if(tel3.length>0) ccant++;
	if(tel4.length>0) ccant++;
	if(tel5.length>0) ccant++;
	if(tel6.length>0) ccant++;
	if(ccant==0){
		alert("Escriba algun numero telefonico!!!");
		est=false;
	}
	
	if(est==true){
		var frm=document.frmDetalle;
		var bb=validarCheckBox(frm);
		if(bb==false){
			alert('seleccione una tematica');
			est=false;
		}else{
			var elem,cad="";
			for(var i=0;i<frm.elements.length;i++){
				if(frm.elements[i].type=="checkbox"){
					elem=frm.elements[i];
					if(elem.checked==true) cad+=","+elem.id.substr(3);
				}
			}
			cad=cad.substr(1);
			document.getElementById('chk').value=cad;
		}
	}
	
	var msg="";
	var errores=document.getElementById('errores');
	if(est==false) msg="Faltan datos<br>";
	
	var error=document.getElementById('error');
	if(error.value==1){est=false; msg+="Error de formato en curriculum vitae y/o foto";}
	
	if(est==false) errores.innerHTML="<font color='#ff0000' style='font-size:10px;'>" + msg + "</font>";
	
	return est;
}

function ppActivarNroColegiatura(cod){
	if(cod==0){
		document.getElementById('nroCole').value='';
		document.getElementById('nroCole').disabled=true;
		document.getElementById('colProfesional').value=0;
	}else{
		document.getElementById('nroCole').disabled=false;
		document.getElementById('colProfesional').value=cod;
	}
}

//Forum
function chkForum(control){
	var nom=control.name;
	var opcPiura=document.getElementById('opcPiura').value;
	if(opcPiura.length > 0){
		if(nom=="chkCDPiura"){
			control.checked=true;
			return;
		}else{
			var est=control.checked;
			control.checked= est;
			return;
		}		
	}else{
		control.checked=true;
	}
}
function verUsuario(){
	if(!valForumIns()) return;
	
	var usu = document.getElementById('usuario').value;
	var id=document.getElementById('idIntegrante').value;
	var dato="usuario="+usu+"&id="+id;
	cargarContenido('Cvista/forumUsuario.php?'+dato,'errores');
	setTimeout("this.forumUPD()", 500);
}
function forumUPD(){
	var err = document.getElementById('error').value;
	if(err==1) return;
	document.frmDato.submit();
}
function valForumIns(){
	var est=true;
	var array=new Array("apellido","nombre","dni","telefono","celular","email_1","usuario");
	if (validarTexto(array)==true) est=false;
	
	var array2=new Array("idSede");
	if (validarCombo(array2)==true) est=false;

	
	//Validacion de email
	if(validarMail('email_1')==true){
		est=false;
		alert("email obligatorio invalido!!!");
	}
	
	var mail2=document.getElementById('email_2').value;
	document.getElementById('email_2').style.backgroundColor="#ffffff";
	document.getElementById('email_2_span').innerHTML="";
	if(mail2.length>0){
		if(validarMail('email_2')==true){
			est=false;
			alert("email opcional invalido!!!");
		}	
	}

	return est;
}
function verDetalleForum(){
	var doc = document.getElementById('documento').value;
	var fNac = document.getElementById('fechaNac').value;
	if(doc.length<8){
		alert('Ingrese un DNI valido!');
		return;
	}
	var fecha=new Date();
	var ff=fNac.split('-');
	if(ff[2] >= fecha.getFullYear()-15){
		alert('Ingrese una fecha de nacimiento valida');
		return;
	}
	document.getElementById('fNac').value=fNac;
	
	var dato="doc="+doc+"&fNac="+fNac;
	cargarContenido('Cvista/forumDetalle.php?'+dato,'detalle');
}

function forumUsuSug(){
	var ape=document.getElementById('apellido').value;
	var nom=document.getElementById('nombre').value;
	
	apeA=ape.split(' ');
	nomA=nom.split(' ');
	
	var cad=nomA[0]+'.'+apeA[0];
	document.getElementById('usuario').value=cad.toLowerCase();
}

function eveReporte(){
	var tipo=document.getElementById('tipoEvento').value;
	var anio=document.getElementById('anio').value;
	var mes=document.getElementById('mes').value;
	var evento=document.getElementById('evento').value;
	
	var dato="carga=1&tipo="+tipo+"&anio="+anio+"&mes="+mes+"&evento="+evento;
	cargarContenido('Cvista/eveReporte.php?'+dato,'contenido');
}


