var flag = 0;

function validarCampos(campo, vacio, formato, imagen){
	valor = document.getElementById(campo).value;
	if (vacio == "vacio"){
		if( valor == null || valor.length == 0 || /^\s+$/.test(valor) ) {	
			document.getElementById(campo).style.border="solid 2px red";
			document.getElementById(imagen).style.visibility = "visible";
			flag=1;
		}else{
			document.getElementById(campo).style.border="solid 2px #cccccc";
			document.getElementById(imagen).style.visibility = "hidden";
			flag=0;
		}
	}
	switch(formato) {
		case 'usuario':
		chk_libre('0','usuario','resultadoUsuario','1');
		break;
		case 'email':	
			if( !(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(valor)) ) {
				document.getElementById(campo).style.border="solid 2px red";
				//return false;
				document.getElementById(imagen).style.visibility = "visible";
				flag=1;
			}else{
			chk_libre('3','email','resultadoEmail','2');}
		break;
		case 'telefono': 
			b=0;
			if(!(/^\d{10}$/.test(valor))){b++;}
			if(!(/^\d{3}[-. ]\d{3}[-. ]\d{4}$/.test(valor))){b++;} 
			if(!(/^\d{3}\s\d{7}$/.test(valor))){b++;} 
			if(!(/^\(\d{3}\)\s\d{7}$/.test(valor))){b++;} 
			if(!(/^\+\d{2,3}\s\d{10}$/.test(valor))){b++;}
			if(!(/^\d{3}\s\d{3}\s\d{2}\s\d{2}$/.test(valor))){b++;} 
			if(!( valor == null || valor.length == 0 || /^\s+$/.test(valor) )){b++;} 
			if(b==7){
				document.getElementById(campo).style.border="solid 2px red";
				//return false;
				document.getElementById(imagen).style.visibility = "visible";
				flag=1;
			}else{
			document.getElementById(campo).style.border="solid 2px #cccccc";
			document.getElementById(imagen).style.visibility = "hidden";
			flag=0;
		}
		break;
		case 'nc':	
			if( !(/^\d{8}$/.test(valor)) ) {
				document.getElementById(campo).style.border="solid 2px red";
				//return false;
				document.getElementById(imagen).style.visibility = "visible";
				flag=1;
			}else{
			chk_libre('5','NC','resultadoControl','3');}
		break;
		case 'cp':	
			if( !(/^\d{5}$/.test(valor)) ) {
				document.getElementById(campo).style.border="solid 2px red";
				//return false;
				document.getElementById(imagen).style.visibility = "visible";
				flag=1;
			}
		break;
		case 'pass1':	
			if( valor != null || valor.length != 0 ){
			var p2 = document.getElementById("2").value;
			
			if (p2 != valor) {
				document.getElementById(campo).style.border="solid 2px red";
				//return false;
				document.getElementById(imagen).style.visibility = "visible";
				flag=1;
			}
			}
		break;
		case 'pass2':	
			
 
			if (p1 != valor) {
				document.getElementById(campo).style.border="solid 2px red";
				//return false;
				document.getElementById(imagen).style.visibility = "visible";
				flag=1;
			}
		break;
	}
}

function chk_libre(elemento, tipo, div, ocupado){
	var pos_url = 'inc/comprueba.php';
	var nombre = document.getElementById(elemento).value;
	var req = new XMLHttpRequest();
	if (req) {
		req.onreadystatechange = function() {
		if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
			document.getElementById(div).innerHTML = req.responseText;
		}
	}
	req.open('GET', pos_url +'?nombre='+nombre+'&tipo='+tipo+'&ocupado='+ocupado,true);
	req.send(null);
	}
}


function envia(formulario, primero, ultimo, boton, oculta, muestra, ina){
	cont=0;
	b=1;
	if(flag==1)
		alert('Hay errores en los datos favor de checar los campos en rojo');	
	if(ultimo!='null'){
	for(i=primero; i<=ultimo; i++){
		valor = document.getElementById(i).value;
		if( valor == null || valor.length == 0 || /^\s+$/.test(valor) ) 
			cont++;
	}
	}
	if(document.getElementById('Ocupado1').value!=null){
			if(document.getElementById('Ocupado1').value=='1'){alert("Usuario ya registrado");b=0;}
	}else{}
	if(document.getElementById('Ocupado2').value!=null){
		if(document.getElementById('Ocupado2').value=='2'){alert("Email ya registrado");b=0;}
	}else{}
	if(document.getElementById('Ocupado3').value!=null){
		if(document.getElementById('Ocupado3').value=='3'){alert("N&uacute; de Control ya registrado");b=0;}
	}else{}
	if(b==1){
	if(cont==0){
		if(flag!=1){
		if(ina!='1')
		document.getElementById(boton).disabled = true;
		//alert(oculta);
			
		if(document.getElementById(boton).value=="Guardar"){
			
			document.getElementById(formulario).submit();	
			//alert('submit');
			
		}
		else{
		muestra_oculta(muestra);
			muestra_oculta(oculta);
		}
		}
	}else{
		alert('Hay campos sin llenar');	
	}}
}

function enviaEgresado(){
	if(document.fRegistroEgresados.trabajando.value=="No"){
		document.fRegistroEgresados.action = 'egresados/abcEgresado.php';
		document.fRegistroEgresados.bPEgresado.value="Guardar";
		//document.fRegistroEgresados.bPEgresado.disabled=false;
		//muestra_oculta('boton1');
	}else{
		//document.fRegistroEgresados.action = 'laboralesEgresado.php';
		document.fRegistroEgresados.bPEgresado.value="Siguiente";
		//document.fRegistroEgresados.flag.value="0";
		//document.fRegistroEgresados.bPEgresado.disabled=true;
		//muestra_oculta('boton1');
	}
}

function vaciar()
{
  this.style.background="#FFF0F0";
}
function verificarEntrada()
{
   this.style.background="#4F4F4F";
}


function borrar(pagina, id, text){
	 var respuesta = confirm('¿Seguro qué desea borrar '+text+'')
	 if(respuesta){
	document.location=pagina+"?id="+id+"";
	 }else{}
}


//////tooltip

var theObj="";
function toolTip(text,me) {
       theObj=me;
       theObj.onmousemove=updatePos;
       document.getElementById('toolTipBox').innerHTML=text;
       document.getElementById('toolTipBox').style.display="block";
       window.onscroll=updatePos;
}
function updatePos() {
       var ev=arguments[0]?arguments[0]:event;
       var x=ev.clientX;
       var y=ev.clientY;
       diffX=170;
       diffY=-17;
       document.getElementById('toolTipBox').style.top  = y-diffY-document.body.scrollTop+ "px";
       document.getElementById('toolTipBox').style.left = x-diffX-document.body.scrollLeft+"px";
       theObj.onmouseout=hideMe;
}
function hideMe() {
       document.getElementById('toolTipBox').style.display="none";
}


function openpopup(){
var popurl="http://site.com/page.htm"
winpops=window.open(popurl,"","width=390,height=490,scrollbars,")
}

/////////////////////////////////////////////////////////////////

function muestra_oculta(id){
if (document.getElementById){ //se obtiene el id
var el = document.getElementById(id); //se define la variable "el" igual a nuestro div
el.style.display = (el.style.display == 'none') ? 'block' : 'none'; //damos un atributo display:none que oculta el div
}
}
window.onload = function(){/*hace que se cargue la función lo que predetermina que div estará oculto hasta llamar a la función nuevamente*/
muestra_oculta('Datos');muestra_oculta('Laborales');/* "contenido_a_mostrar" es el nombre que le dimos al DIV */
}

var contador=0;
function tooltip(campo, div){
	contador++;
	//alert(contador);
	var pos_url = 'inc/tooltip.php';
	//var campo = document.getElementById(elemento).value;
	var req = new XMLHttpRequest();
	if (req) {
		req.onreadystatechange = function() {
		if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
			document.getElementById(div).innerHTML = req.responseText;
		}
	}
	req.open('GET', pos_url +'?campo='+campo+'&limpia='+contador,true);
	req.send(null);
	}
	if(contador>1){contador=0;}
}

