// JavaScript Document
function lab(sel) {
if (document.getElementById('cond').checked)
	{
		document.getElementById('enviar').disabled=false;
	}
	else
	{
		document.getElementById('enviar').disabled=true;
	}
}

//FUNCION VALIDAR CAMPO DE TEXTO DE ENTRADA POR INPUT, USADA EN REGISTRO.PHP
function validar_ca(campo,checkOK){
	  //var checkOK = ;
	  var checkStr = campo;
	  var allValid = true;
	  for (i = 0; i < checkStr.length; i++) {
		ch = checkStr.charAt(i);
		for (j = 0; j < checkOK.length; j++)
		  if (ch == checkOK.charAt(j))
			break;
		  if (j == checkOK.length) {
		    allValid = false;
		    break;
		}
	  }
	  return allValid;

}
//CREAR NUEVO AJAX
function nuevoAjax(){
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 nuevoAjax()
{
        var xmlhttp=false;
        try
        {
                // Creacion del objeto AJAX para navegadores no IE
                xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
                try
                {
                        // Creacion del objet AJAX para IE
                        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e)
                {
                        if (!xmlhttp && typeof XMLHttpRequest!="undefined") xmlhttp=new XMLHttpRequest();
                }
        }
        return xmlhttp;
}*/

//CARGAR CONTENIDO USADA EN REGISTRO.PHP
function cargarContenido(){
	var contenedor,dummy;
	 var elemento = document.getElementById('input_registro');
	contenedor = document.getElementById('contenedor');
	ajax=nuevoAjax();
	 var nombre =document.getElementById('input_registro').value 
	 //Esta variable obtiene el valor de lo q puso el usuario en el textbox nombre
	ajax.open("GET", "ajax_hupa/valida.php?"+"&"+"username="+nombre,true); 
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			if(ajax.responseText =='OK'){
			//
			if(document.getElementById('input_registro').value.length > 2){
	
				
				if (!validar_ca(document.getElementById('input_registro').value,"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "@_")) {
					contenedor.innerHTML = 'El nombre sólo debe contener caracteres válidos: letras, números y signos @ _';
					document.getElementById('input_registro').focus();
					window.location.href="#error";	
					elemento.className = "error";
					 
				}else
				{			
					window.location.href="registro_1.php?username="+nombre+"&aprob=OK";
				}
			}else{
				document.getElementById('input_registro').focus();
				window.location.href="#error";				
				contenedor.innerHTML = "Debes ingresar 3 caracteres como mínimo"		
				elemento.className = "error";
			}
			//
			}else{
			if(document.getElementById('input_registro').value.length < 3){
				document.getElementById('input_registro').focus();
				window.location.href="#error";				
				contenedor.innerHTML = "Debes ingresar 3 caracteres como mínimo"	
				elemento.className = "error";
			}else{
				document.getElementById('input_registro').focus();
				window.location.href="#error";				
				contenedor.innerHTML = ajax.responseText
				elemento.className = "error";
			}
			}
		}
	}
	ajax.send(null)
}

//CAPTURAR TECLA ENTRER USADA EN REGISTRO.PHP
/*function pressed(e) {
//window.captureEvents(Event.KEYPRESS);
  if(e.which == 13){
  loginFG();
  }
}*/

/*function login(){
	var error_log;
	error_log = document.getElementById('error_log');
	ajax_log=nuevoAjax();
	 var nombre =document.getElementById('usuario').value ;
	 var pass =document.getElementById('pass').value; 
	 if(document.getElementById('sesion_duration').type =="checkbox"){
		if(document.getElementById('sesion_duration').checked){
			var session ='Permanent';
		}else{
			var session ='';
		}
	 }
	 var login_on = document.getElementById('CON_LOG');
	 //Esta variable obtiene el valor de lo q puso el usuario en el textbox nombre
	ajax_log.open("GET", "ajax_hupa/log.php?"+"&"+"usuario="+nombre+"&"+"pass="+pass+"&"+"sesion_duration="+session,true); 
	ajax_log.onreadystatechange=function() {
			if (ajax_log.readyState==1) {
				error_log.innerHTML = "";
			}
			if (ajax_log.readyState==4) {
			if(ajax_log.responseText =='OK1'){
				error_log.innerHTML = '';
				error_log.className ="ajax-loading";
				window.location.href="index.php";
				document.location.href="index.php";
				//login_on.innerHTML = "banner02";
			}else{
				error_log.innerHTML = ajax_log.responseText
			}
			}
	}
	ajax_log.send(null)
}

//presslog*/
function presslog(e) {
//window.captureEvents(Event.KEYPRESS);
  if(e.which == 13){
  //login();
  loginFG();
  }
}

//// AMIGOS ONLINE//////////////
/*function online(){
var online;
online = document.getElementById('online');
ajax_enc=nuevoAjax();
ajax_enc.open("GET", "ajax_hupa/online.php",true);
ajax_enc.onreadystatechange=function() {
if (ajax_enc.readyState==4) {
	online.innerHTML = ajax_enc.responseText
//refresh.contenedor.innerHTML(timer * 1000, this)
}
}
ajax_enc.send(null)
	setTimeout("online()",20000);	

}

//// AMIGOS ONLINE CON AVATAR//////////////
/*function online_av(){
var online_av;
online_av = document.getElementById('online_av');
ajax_av=nuevoAjax();
ajax_av.open("GET", "ajax_hupa/online_avatar.php?cachetime=0",true);
ajax_av.onreadystatechange=function() {
if (ajax_av.readyState==4) {
	online_av.innerHTML = ajax_av.responseText
}
}
ajax_av.send(null)
	setTimeout("online_av()",20000);
}
///// MENSAJES ////////////////////////
/*function mensajes(){
var priv_mensaje;
priv_mensaje = document.getElementById('priv_mensaje');
ajax_men=nuevoAjax();
ajax_men.open("GET", "ajax_hupa/mensajes_privados.php?cachetime=0",true);
ajax_men.onreadystatechange=function() {
if (ajax_men.readyState==1) {
	priv_mensaje.innerHTML = "<img src='/img_hupa/loading2.gif' align='center'>";
}
if (ajax_men.readyState==4) {
	priv_mensaje.innerHTML = ajax_men.responseText
}
}
ajax_men.send(null)
	setTimeout("mensajes()",40000);
}
///// COMENTARIOS ////////////////////////
function EvalSound(soundobj) {
  var thissound=document.getElementById(soundobj);
  thissound.Play();
}
/*function comentarios(){
var comentarios;
comentarios = document.getElementById('comentarios');
ajax_comm=nuevoAjax();
ajax_comm.open("GET", "ajax_hupa/comentarios.php?cachetime=0",true);
ajax_comm.onreadystatechange=function() {
if (ajax_comm.readyState==1) {
	comentarios.innerHTML = "<img src='/img_hupa/loading2.gif' align='center'>";
}
if (ajax_comm.readyState==4) {
	comentarios.innerHTML = ajax_comm.responseText
	if(ajax_comm.responseText !='0'){
	}
}
}
ajax_comm.send(null)
setTimeout("comentarios()",40000);
}*/
function loginFG(){
	var error_log;
	error_log = document.getElementById('error_log');
	ajax_logfg=nuevoAjax();
	 var nombre =document.getElementById('usuario').value ;
	 var pass =document.getElementById('pass').value; 
	 if(document.getElementById('sesion_duration').type =="checkbox"){
		if(document.getElementById('sesion_duration').checked){
			var session ='Permanent';
		}else{
			var session ='';
		}
	 }
	 var login_on = document.getElementById('CON_LOG');
	 //Esta variable obtiene el valor de lo q puso el usuario en el textbox nombre
	ajax_logfg.open("GET", "ajax_hupa/log.php?"+"&"+"usuario="+nombre+"&"+"pass="+pass+"&"+"sesion_duration="+session,true); 
	ajax_logfg.onreadystatechange=function() {
			if (ajax_logfg.readyState==1) {
				error_log.innerHTML = "";
			}
			if (ajax_logfg.readyState==4) {
			if(ajax_logfg.responseText =='OK1'){
				error_log.innerHTML = '';
				error_log.className ="ajax-loading";
				valorPAG = location.search
				top.location.href="index.php"+valorPAG;
				document.location.href="index.php"+valorPAG;
				//login_on.innerHTML = "banner02";
			}else if(ajax_logfg.responseText =='ADMINOK'){
				error_log.innerHTML = '';
				valorPAG1 = location.search
				error_log.className ="ajax-loading";
				top.location.href="admin/index.php"+valorPAG1;
				document.location.href="admin/index.php"+valorPAG1;
			
			}else{
				error_log.innerHTML = ajax_logfg.responseText
			}
			}
	}
	ajax_logfg.send(null)
}

/*function validar_ca(campo,checkOK){
	  //var checkOK = ;
	  var checkStr = campo;
	  var allValid = true;
	  for (i = 0; i < checkStr.length; i++) {
		ch = checkStr.charAt(i);
		for (j = 0; j < checkOK.length; j++)
		  if (ch == checkOK.charAt(j))
			break;
		  if (j == checkOK.length) {
		    allValid = false;
		    break;
		}
	  }
	  return allValid;

}

*/
//VALIDAR FORMILARIO
function formulario(){
	var online 		= document.getElementById('online');
	var code 		= document.getElementById('code').value;
	var pass 		= document.getElementById('pass').value;
	var confpass 	= document.getElementById('confpass').value;
	var nombre 		= document.getElementById('nombre').value;
	var username	= document.getElementById('username').value;
	var apellido	= document.getElementById('apellido').value;
	var email 		= document.getElementById('email').value;
	var confemail	= document.getElementById('confemail').value;
	var fone		= document.getElementById('fono').value;
	var dir			= document.getElementById('comuna').value;
	var desc		= document.getElementById('desc').value;
	var error 		= 0;
	//NOMBRE
	/*alert(nombre);
	if(nombre == ''){
		document.getElementById('nombre').focus();
		document.getElementById('nombre').className='cuadro_error';
		error=1;
	}else if(nombre.search(/[_a-zA-Z]/)){
			document.getElementById('nombre').className='cuadro_error';
			document.getElementById('nombre').focus();

			error=1;
			alert('a');
	}else{
			document.getElementById('nombre').className='';
			error=0;
		}
	*/
	if(nombre == ''){
		document.getElementById('nombre').focus();
		document.getElementById('nombre').className='cuadro_error';
		error=1;
		return false;
	}	
   else if (!validar_ca(document.getElementById('nombre').value,"ABCDEFGHIJKLMNOPQRSTUVWXYZÑ" + "abcdefghijklmnopqrstuvwxyzáéíóúñ" + "0123456789" + "@_ ")) {
	    alert ("El nombre debe sólo contener caracteres válidos: letras, números y signos @ _");
		document.getElementById('nombre').focus();
		document.getElementById('nombre').className='cuadro_error';
		error=1;		
		return (false); 
	}	else{
		document.getElementById('nombre').className='caja';
		error=0;
		
		}	
	if(apellido == ''){
		document.getElementById('apellido').focus();
		document.getElementById('apellido').className='cuadro_error';
		error=1;
		return false;
	}	
 else if (!validar_ca(document.getElementById('apellido').value,"ABCDEFGHIJKLMNOPQRSTUVWXYZÑ" + "abcdefghijklmnopqrstuvwxyzáéíóúñ" + "0123456789" + "@_ ")) {
	    alert ("El nombre debe sólo contener caracteres válidos: letras, números y signos @ _");
		document.getElementById('apellido').focus();
		document.getElementById('apellido').className='cuadro_error';
		error=1;		
		return (false); 
	}else{
		document.getElementById('apellido').className='caja';
		}
	if(email ==''){
		document.getElementById('email').focus();
		document.getElementById('email').className='cuadro_error';
		error=1;
		return false;
	}
	else if(email.search(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/ig)){
		alert('La cuenta no es válida, debes escribirla de forma: nombre@servidor.dominio');
		document.getElementById('email').focus();
		document.getElementById('email').className='cuadro_error';
		return false;
		error=1;
	}else{
		document.getElementById('email').className='caja';
		}
	if(confemail ==''){
		document.getElementById('confemail').focus();
		document.getElementById('confemail').className='cuadro_error';
		error=1;
		return false;
	}else{
		document.getElementById('confemail').className='caja';
		}
	if(email != confemail){
		alert ("Las cuentas de correo (E-mail) no coinciden, vuelve a ingresar tu E-Mail");
		document.getElementById('confemail').className='cuadro_error';
		document.getElementById('email').className='cuadro_error';
		document.getElementById('confemail').focus();
		error=1;
		return false;
	}else{
		document.getElementById('confemail').className='caja';
		document.getElementById('email').className='caja';
		}
	if(username == ''){
		document.getElementById('username').focus();
		document.getElementById('username').className='cuadro_error';
		error=1;
		return false;
	}	
 else if (!validar_ca(document.getElementById('username').value,"ABCDEFGHIJKLMNOPQRSTUVWXYZÑ" + "abcdefghijklmnopqrstuvwxyzáéíóúñ" + "0123456789" + "@_")) {
	    alert ("El nombre debe sólo contener caracteres válidos: letras, números y signos @ _");
		document.getElementById('username').focus();
		document.getElementById('username').className='cuadro_error';
		error=1;		
		return (false); 
	}else{
		document.getElementById('username').className='caja';
	}		
	if(pass == ''){
		document.getElementById('pass').focus();
		document.getElementById('pass').className='cuadro_error';
		error=1;
		return false;
	}else{
		document.getElementById('pass').className='caja';		
		error=0;
	}
	if(confpass == ''){
		document.getElementById('confpass').focus();
		document.getElementById('confpass').className='cuadro_error';
		error=1;
		return false;
	}else{
		document.getElementById('confpass').className='caja';		
		error=0;
	}
	if(confpass != pass){
		document.getElementById('pass').className='cuadro_error';
		document.getElementById('confpass').className='cuadro_error';
		document.getElementById('pass').focus();
		error=1;
		return false;
	}
	
	if(code ==''){
		document.getElementById('code').focus();
		document.getElementById('code').className='cuadro_error';
		error=1;
		return false;
		}
	if(error==0){
			form=nuevoAjax();
			form.open("POST", "registro.php",true);
			form.onreadystatechange=function() {
				if (form.readyState==4){
					if(form.responseText ==0){			
						alert('Codigo de verificación erroneo, vuelva a ingresarlo.');
						//document.location.reload();
						document.getElementById('code').className='cuadro_error';
					}else{
					online.innerHTML = form.responseText;
					valorPAG = location.search
					alert('Gracias por registrarte en El Fuerte. Nos pondremos en contacto');
					document.location.href='index.php' + valorPAG;
					}
				}
			}
		}	
	else if(error==1){
			form=nuevoAjax();
			form.open("POST", "#",true);
			form.onreadystatechange=function() {
				if (form.readyState==4){
				}
			}
		}	
    form.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	form.send("code="+code+"&nombre="+nombre+"&mail="+email+"&apellido="+apellido+"&username="+username+"&pass="+pass+"&fone="+fone+"&dir="+dir+"&desc="+desc); 	
	//form.send(null)
}

function userverf(){
var verf;
verf = document.getElementById('verf');
nombre =document.getElementById('username').value;

ver=nuevoAjax();
	ver.open("GET", "ajax_hupa/validafuerte.php?"+"&"+"username="+nombre,true); 
	ver.onreadystatechange=function() {
		if (ver.readyState==4) {
			if(ver.responseText =='OK'){
				//verf.innerHTML = ver.responseText;
				document.getElementById('username').className='caja';
				verf.innerHTML = "";

			}else{
				document.getElementById('username').focus();
				document.getElementById('username').className='cuadro_error';
				verf.innerHTML = ver.responseText;
				return false;
				
				}
}
}
ver.send(null)
}

function verres(){
var resadmin = document.getElementById('res_con');
var buscar   = document.getElementById('buscar').value;
var perfil   = document.getElementById('perfil').value;
var mod_id   = document.getElementById('mod_id').value;
admin=nuevoAjax();
	admin.open("GET", "../ajax_hupa/resadmin.php?"+"&"+"buscar="+buscar+"&perfil="+perfil+"&mod_id="+mod_id,true); 
	admin.onreadystatechange=function() {
		if (admin.readyState==4) {
				resadmin.innerHTML = admin.responseText;
		}
      }

admin.send(null)


}

function sendmail(){
var formulario 	= document.getElementById('formulario');
var nombre   	= document.getElementById('nombre').value;
var fono   		= document.getElementById('fono').value;
var mail   		= document.getElementById('mail').value;
var texto   	= document.getElementById('texto').value;
var mod_id   	= document.getElementById('mod_id').value;

sendmail=nuevoAjax();
	sendmail.open("GET", "ajax_hupa/sendmail.php?"+"&"+"nombre="+nombre+"&fono="+fono+"&mail="+mail+"&texto="+texto+"&mod_id="+mod_id,true); 
	sendmail.onreadystatechange=function() {
		if (sendmail.readyState==4) {
				formulario.innerHTML = sendmail.responseText;
		}
      }

sendmail.send(null)
}

function alerta(){
var formulario 		= document.getElementById('classid');
var formulario2 	= document.getElementById('classid2');
var newuserid   	= document.getElementById('newuserid').value;
var mod_id			= document.getElementById('mod_id').value;
var id_news			= document.getElementById('id_news').value;

alerti=nuevoAjax();
	alerti.open("GET", "../ajax_hupa/upd_usernota.php?"+"&"+"id_user="+newuserid+"&mod_id="+mod_id+"&id_news="+id_news,true); 
	alerti.onreadystatechange=function() {
		if (alerti.readyState==4) {
				formulario.innerHTML = alerti.responseText;
				formulario2.innerHTML = alerti.responseText;
		}
      }

alerti.send(null)
}
