function objetoAjax(){
	var xmlhttp=false;
	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 ValidadTexto(control){
	if(document.getElementById(control).value==""){
		document.getElementById(control).style.backgroundColor="#E4E4E4";
		estado=0;
		document.getElementById(control).focus();
		return estado;
	}else{
		document.getElementById(control).style.backgroundColor="#FFF";
		estado=1;
	}
	return estado;
}

function ValidaClave(control,contro2){
	if(document.getElementById(control).value!=""){
		if(document.getElementById(contro2).value!=""){
			if(document.getElementById(control).value==document.getElementById(contro2).value){
				document.getElementById(contro2).style.backgroundColor="#FFF";
				estado=1;
				return estado;
			}else{
				document.getElementById(contro2).style.backgroundColor="#E4E4E4";
				estado=0;
				document.getElementById(contro2).focus();
				document.getElementById(contro2).select();
				return estado;
			}

		}else{
			document.getElementById(control).style.backgroundColor="#FFF";
			document.getElementById(contro2).style.backgroundColor="#E4E4E4";
			estado=0;
			document.getElementById(contro2).focus();
			return estado;
		}
	}else{
		document.getElementById(control).style.backgroundColor="#E4E4E4";
		estado=0;
		document.getElementById(control).focus();
	}
	return estado;
}


function ValidaCorreo(control){
	if(document.getElementById(control).value==""){
		document.getElementById(control).style.backgroundColor="#E4E4E4";
		estado=0;
		document.getElementById(control).focus();
		return estado;
	}else{
		 if(document.getElementById(control).value.indexOf("@")==-1 || document.getElementById(control).value.indexOf(".")==-1){
			document.getElementById(control).style.backgroundColor="#E4E4E4";
			estado=0;
			document.getElementById(control).focus();
			document.getElementById(control).select();
			return estado;
		}else{
		document.getElementById(control).style.backgroundColor="#FFF";
		estado=1;
		}
	}
	return estado;
}

function ValidaNumeroTelefono(control){
	if(document.getElementById(control).value==""){
		document.getElementById(control).style.backgroundColor="#E4E4E4";
		estado=0;
		document.getElementById(control).focus();
		return estado;
	}else{

		var valid = "()*-0123456789" 
		var ok = "yes"; 
		var temp; 
		for (var i=0; i<document.getElementById(control).value.length; i++) { 
			temp = "" + document.getElementById(control).value.substring(i, i+1); 
			if (valid.indexOf(temp) == -1) ok = "no"; 
		} 
		if (ok == "no") { 
			document.getElementById(control).style.backgroundColor="#E4E4E4";
			estado=0;
			document.getElementById(control).focus();
			document.getElementById(control).select();		
			return estado;
		 }else{
			document.getElementById(control).style.backgroundColor="#FFF";
			estado=1
		}		
	}
	return estado;
}


function ValidaNumero(control){
	if(document.getElementById(control).value==""){
		document.getElementById(control).style.backgroundColor="#E4E4E4";
		estado=0;
		document.getElementById(control).focus();
		return estado;
	}else{

		var valid = "0123456789" 
		var ok = "yes"; 
		var temp; 
		for (var i=0; i<document.getElementById(control).value.length; i++) { 
			temp = "" + document.getElementById(control).value.substring(i, i+1); 
			if (valid.indexOf(temp) == -1) ok = "no"; 
		} 
		if (ok == "no") { 
			document.getElementById(control).style.backgroundColor="#E4E4E4";
			estado=0;
			document.getElementById(control).focus();
			document.getElementById(control).select();		
			return estado;
		 }else{
			document.getElementById(control).style.backgroundColor="#FFF";
			estado=1
		}		
	}
	return estado;
}

function ValidadSelecion(control){
	if(document.getElementById(control).value=="0"){
		document.getElementById(control).style.backgroundColor="#E4E4E4";
		estado=0;
		document.getElementById(control).focus();
		return estado;
	}else{
		document.getElementById(control).style.backgroundColor="#FFF";
		estado=1;
	}
	return estado;
}

function Validad2Opcion(control){
	if(document.getElementsByName(control)[0].checked || document.getElementsByName(control)[1].checked){
		document.getElementsByName(control)[0].style.backgroundColor="#FFF";
		estado=1;
	}else{
		document.getElementsByName(control)[0].style.backgroundColor="#E4E4E4";
		document.getElementsByName(control)[0].focus();
		estado=0;
	}
	return estado;
}


function Validad3Opcion(control){
	if(document.getElementsByName(control)[0].checked || document.getElementsByName(control)[1].checked || document.getElementsByName(control)[2].checked){
		document.getElementsByName(control)[0].style.backgroundColor="#FFF";
		estado=1;
	}else{
		document.getElementsByName(control)[0].style.backgroundColor="#E4E4E4";
		document.getElementsByName(control)[0].focus();
		estado=0;
	}
	return estado;
}

function carga_ing_centro(){
	if(document.getElementById("slt_centrop_e_otro").value=="01"){
		document.getElementById("imgr_txt_centro").style.display="block";	
		document.getElementById("imgr_txt_centro_2").style.display="block";	
		document.getElementById("nuevo_centro").value=2;	
	}else{
		document.getElementById("imgr_txt_centro").style.display="none";	
		document.getElementById("imgr_txt_centro_2").style.display="none";	
		document.getElementById("nuevo_centro").value=1;	
	}

}


/*---------------*/

function EnviaEncuesta(id){
	var Contenido, PrecargaEncuesta;
	var EstadoEncuesta=0;
	Contenido = document.getElementById('Encuesta');
	PrecargaEncuesta = document.getElementById('PrecargaEncuesta');
	
	for(a=0;a<document.getElementById('CantidadRespuestas').value;a++){
		if(document.getElementsByName('OptEncuesta')[a].checked){
			document.getElementsByName('OptEncuesta')[a].style.backgroundColor="#FFF";
			EstadoEncuesta=1;
			break;
		}
	}
	if(EstadoEncuesta==0){
		alert("Selecciona un respuesta");
		document.getElementsByName('OptEncuesta')[0].style.backgroundColor="#E4E4E4";
		document.getElementsByName('OptEncuesta')[0].focus();
		document.getElementsByName('OptEncuesta')[0].select();
		return;
	}

		ajax=objetoAjax();
		ajax.open("POST", "Includes/InsertaEncuesta.php",true);		
        ajax.onreadystatechange=function(){
                if(ajax.readyState==1){
                        PrecargaEncuesta.innerHTML = "Cargando...";
	               }else if(ajax.readyState==4){
                        if(ajax.status==200){
							if(ajax.responseText==0){
								PrecargaEncuesta.innerHTML = "No se pudo regisrtar la encuesta";
							}else{
								Contenido.innerHTML = ajax.responseText;
							}	
                        }else if(ajax.status==404){
                                PrecargaEncuesta.innerHTML = "La página no existe";
                        }else{
                                //mostramos el posible error
                                PrecargaEncuesta.innerHTML = "Error:".ajax.status; 
                        }
                }
        }

		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	  ajax.send("IdRes="+id);
}


//------------------

function JsIniciarSesion(){
	var Contenido, PrecargaIniciarSesion;
	var EstadoEncuesta=0;
	
	Contenido = document.getElementById('IniciarSesion');
	PrecargaIniciarSesion = document.getElementById('PrecargaIniciarSesion');
	
		ajax=objetoAjax();
		ajax.open("POST", "Includes/IniciarSesion.php",true);		
        ajax.onreadystatechange=function(){
                if(ajax.readyState==1){
                        PrecargaIniciarSesion.innerHTML = "Cargando...";
	               }else if(ajax.readyState==4){
                        if(ajax.status==200){
							switch(ajax.responseText){
								case "0":
								PrecargaIniciarSesion.innerHTML = "No se pudo iniciar sesión,\n los datos son incorrectos";
								break;
								case "1":
								PrecargaIniciarSesion.innerHTML = "No se pudo iniciar sesión, \n su cuenta esta desactivada";
								break;
								case "2":
								PrecargaIniciarSesion.innerHTML = "No se pudo iniciar sesión, \n Su cuenta ha expirado";
								break;
								default:
								Contenido.innerHTML = ajax.responseText;	
								document.getElementById('DivMenuAreas').style.display='none';
								break;		
							}	
                        }else if(ajax.status==404){
                                PrecargaIniciarSesion.innerHTML = "La página no existe";
                        }else{
                                //mostramos el posible error
                                PrecargaIniciarSesion.innerHTML = "Error:".ajax.status; 
                        }
                }
        }

	if(ValidaCorreo('Txt_Email')==1)
	if(ValidadTexto('Txt_Clave')==1)
	
	if(estado==1){
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send("Email="+document.getElementById('Txt_Email').value+"&Clave="+document.getElementById('Txt_Clave').value);
	}
}

function JsCerrarSession(){
	var Contenido, PrecargaIniciarSesion;
	var EstadoEncuesta=0;
	
	Contenido = document.getElementById('IniciarSesion');
	PrecargaIniciarSesion = document.getElementById('PrecargaIniciarSesion');
	
		ajax=objetoAjax();
		ajax.open("POST", "Includes/CerrarSession.php",true);		
        ajax.onreadystatechange=function(){
                if(ajax.readyState==1){
                        PrecargaIniciarSesion.innerHTML = "Cargando...";
	               }else if(ajax.readyState==4){
                        if(ajax.status==200){
							if(ajax.responseText==0){
								PrecargaIniciarSesion.innerHTML = "No se pudo cerrar la sesión";			
							}else{
								Contenido.innerHTML = ajax.responseText;		
								document.getElementById('DivMenuAreas').style.display='block';
							}	
                        }else if(ajax.status==404){
                                PrecargaIniciarSesion.innerHTML = "La página no existe";
                        }else{
                                //mostramos el posible error
                                PrecargaIniciarSesion.innerHTML = "Error:".ajax.status; 
                        }
                }
        }
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send("Estado=1");
}



function JsTestCapituloUniversidad(){
var EstadoRespuestaEnvia=1;
var EstadoValidacion = document.getElementById('DivEstadoValidacion');
	for(var a=1;a<=document.getElementById('CantidadPreguntasCapitulo').value;a++){	
		for(var b=1;b<=document.getElementById('CantidadRespuestaCapitulo').value+1;b++){	
			if(document.getElementById("Respuesta_"+a+"_"+b)!=null){
				if(document.getElementById("Respuesta_"+a+"_"+b).checked){
					document.getElementById("Respuesta_"+a+"_1").style.backgroundColor="#FFF";
					EstadoRespuestaEnvia=1;
					break;
				}else{
					document.getElementById("Respuesta_"+a+"_1").style.backgroundColor="#E4E4E4";
					document.getElementById("Respuesta_"+a+"_1").focus();	
					EstadoRespuestaEnvia=0;
				}
			}else{
				break;
			}
		}
	}	
	if(EstadoRespuestaEnvia==1){
		EstadoValidacion.innerHTML = "Cargando...";						
		document.getElementById('activa_envio').value=1;
		document.TestUniversidad.submit();
	}else{
		EstadoValidacion.innerHTML = "Maque todas las opciones";	
	}

}


//-------------------------------------
//Consulta de curso


function JsMuestraAgregarRespuesta(id){

	if(TempIdRespuesta==0){
		document.getElementById("DivAgregaPregunta").style.display="none";
	}

	if(TempIdRespuesta != "" && TempIdRespuesta != 0){
			document.getElementById("DivAgregaRespuesta_"+TempIdRespuesta).style.display="none";	
	}

	TempIdRespuesta = id;
	document.getElementById("DivAgregaRespuesta_"+id).style.display="block";	
	document.getElementById("DivAgregaRespuesta_"+id).focus();
	document.getElementById('EstadoGraba').value=id;


}

function JsMuestraAgregarPregunta(){

	if(TempIdRespuesta!="" && TempIdRespuesta!=0){
		document.getElementById("DivAgregaRespuesta_"+TempIdRespuesta).style.display="none";	
	}
	
	TempIdRespuesta=0;
	document.getElementById("DivAgregaPregunta").style.display="block";	
	document.getElementById('EstadoGraba').value=0;
	document.getElementById("DivAgregaPregunta").focus();
	document.getElementById("DivAgregaPregunta").select();
}


	
function JsGrabaPreguntaConsulta(){	
	if(TempIdRespuesta==0){

		if(ValidadTexto('TxtPregunta')==1);
	
	}
	
	if(TempIdRespuesta!="" && TempIdRespuesta!=0){

		if(ValidadTexto('TxtRespuesta_'+TempIdRespuesta)==1);
	
	}
	
	
	if(estado==1){
		document.getElementById('activa_envio').value=1;
		document.FrmConsultaCurso.action="elearning-mis-consultas.php?accion=registra";
		document.FrmConsultaCurso.submit();
	}
}


function enviamail(){
	if(ValidaCorreo('txcorreo')==1)
	if(estado ==1){
		document.EnviaEmail.submit();}
	
	}
function setpais(){
	if(document.getElementById("cbpais").value!="Peru"){
		document.getElementById("divdepartamento").style.display="none";
	}else{
		document.getElementById("divdepartamento").style.display="block";	
	}
}