// JavaScript Document
function CreateObjetoAjax() {
	var Objeto;
		var browser = navigator.appName;
		if(browser == "Microsoft Internet Explorer"){
			Objeto = new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			Objeto = new XMLHttpRequest();
		}
			return Objeto
 }
 /* function transaccion(action) {
	var HTTP = CreateObjetoAjax();
	 HTTP.open("get", "server.php?action="+action);
     HTTP.onreadystatechange=function() { 
   				if (HTTP.readyState==4) { 
   						recibe(HTTP.responseText); 
   				} 
  		} 
       HTTP.send(null);
}*/
 function transaccion(sw,parametros) {
	var HTTP = CreateObjetoAjax();
	 HTTP.open("POST", "ajax/server_registrarse.php?valor=Yasser",true);
	                                                   //aparte de mandar con el Post
	                                                   //tambien mandamos en la url una 
													   //variable que puede ser tratada como get 
	 HTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
	 //HTTP.send('variable='+action);//asi se manda una sola variable
	 HTTP.send('sw=' + sw + '&parametros=' + parametros);//asi se manda varias variables
	 HTTP.onreadystatechange=function() { 
	            
   				if (HTTP.readyState==4) { 
   						recibe(HTTP.responseText); //aqui recibo el mensaje
						
				}
  		} 
       
}
/***********************************************************************************************
A partir de aqui construyo todas mis funciones
/***********************************************************************************************/

////////////////////////////////////////////
//Esta es la unica funcion que recibira 
//todo los regresos de las funciones manda
/////////////////////////////////////////////
function recibe(action){
	  
	 	var descriminador =	action.substring(0,1);//PORQUE LA POSICION 0 TRAE EL NUMERO
		
		                                         //EN LA POSICION 1 VIENE EL MICHI
		   
	    var cadena_total =	action.substring(2,action.length); //PORQUE A PARTIR DE LA 
		                                                       //POSICION 2 TRAE LO DEMAS
			 
	    switch (descriminador){
			
			case "1": //VALIDAMOS QUE EL USUARIO NO EXISTA
						
						if(cadena_total=="0"){
							var men="<span style='color:#339932;'>";
							    men+="El Usuario esta Disponible.</span>";
							document.getElementById("div_disponible").innerHTML=men;
						}else{
							var men="<span style='color:#FF0000;'>";
							    men+="Usuario NO Disponible.</span>";
							document.getElementById("div_disponible").innerHTML=men;
						
						}
						
						break;
					
					
												
		   case  "2": //REGRESA DESPUES DE MANDAR A GRABAR
		                
						
						
						document.getElementById("sp_gif_graba").innerHTML="";
						
						if(cadena_total=="email"){
							var men="El email ya esta en uso, por favor intente con otro.";
							    alert(men);
								document.getElementById("txt_email").focus();
								
						}else if(cadena_total=="telefono"){
							
							var men="El Telefono ya esta en uso, por favor intente con otro.";
							    alert(men);
								document.getElementById("txt_telefono1").focus();
						
						
						}else if(cadena_total=="fecha_nac"){
							
							var men="La fecha de nacimiento ingresada no es válida, "; 
							    men+=" por favor corrijalo."
							    alert(men);
								document.getElementById("txt_dia").focus();
								
						}else if(cadena_total=="usuario"){
							
								var men="El Usuario ya esta en uso, por favor intente con otro."
							    alert(men);
								document.getElementById("txt_usuario").focus();
								
						}else if(cadena_total=="ok"){
							
								/*
								document.getElementById("div_titulo").style.display="none";
								document.getElementById("tb_registrate").style.display="none";
								
								document.getElementById("tb_mensaje").style.display="block";
								
								*/
								
								window.location.href="registrate.php?grabo=1";
						}//endif
						
						break;
						
						
		  	case  "3": 
			
		               alert(cadena_total);
						break;
						
		  	case  "4": 
			
		                alert(cadena_total);
						break;
						
						
								
		  	case  "5": 
		                
						alert(cadena_total);
						break;				
					    
		
	   }//end switch

}//end function 


function disponible(){

		var txt_usuario=document.getElementById("txt_usuario").value;
	        txt_usuario=trim(txt_usuario);
		
		if(txt_usuario.length==0){
			var men="Por favor ingrese su Usuario.";
			    alert(men);
				document.getElementById("txt_usuario").focus();
				
		}else{
			
			transaccion("1", txt_usuario);
			gif_ajax("div_disponible")
			
		}//endif
	
}


function registrate(){
	
	
	
	
	
	
	
	var txt_nombres=document.getElementById("txt_nombres").value;
	    txt_nombres=trim(txt_nombres);
		txt_nombres=manda_transformacion(txt_nombres);
		
	var txt_apellidos=document.getElementById("txt_apellidos").value;
	    txt_apellidos=trim(txt_apellidos);
		txt_apellidos=manda_transformacion(txt_apellidos);
		
	var txt_email=document.getElementById("txt_email").value;
	    txt_email=trim(txt_email);
		
	var txt_dia=document.getElementById("txt_dia").value;
	    txt_dia=trim(txt_dia);	
		
	var txt_mes=document.getElementById("txt_mes").value;
	    txt_mes=trim(txt_mes);
		
	var txt_anio=document.getElementById("txt_anio").value;
	    txt_anio=trim(txt_anio);	
		
	var txt_telefono1=document.getElementById("txt_telefono1").value;
	    txt_telefono1=trim(txt_telefono1);
		
	var txt_telefono2=document.getElementById("txt_telefono2").value;
	    txt_telefono2=trim(txt_telefono2);
		
    var txt_postal=document.getElementById("txt_postal").value;		
	    txt_postal=trim(txt_postal);
		txt_postal=manda_transformacion(txt_postal);
		
    var txt_poblacion=document.getElementById("txt_poblacion").value;
	    txt_poblacion=trim(txt_poblacion);
		txt_poblacion=manda_transformacion(txt_poblacion);
		
	var txt_direccion=document.getElementById("txt_direccion").value;
	    txt_direccion=trim(txt_direccion);
		txt_direccion=manda_transformacion(txt_direccion);
		
	var txt_obs=document.getElementById("txt_obs").value;
	    txt_obs=trim(txt_obs);
		txt_obs=manda_transformacion(txt_obs);	
	
	//EL txt_usuario, txt_pass1, txt_pass2 no necesitan validación porque 
	//estan validados desde el ingreso
	var txt_usuario=document.getElementById("txt_usuario").value;
	    txt_usuario=trim(txt_usuario);
		
		
	var txt_pass1=document.getElementById("txt_pass1").value;
	    txt_pass1=trim(txt_pass1);
		
		
	var txt_pass2=document.getElementById("txt_pass2").value;
	    txt_pass2=trim(txt_pass2);	
		
		
	var chk_terminos=document.getElementById("chk_terminos").checked;
	
	var chk_promociones=document.getElementById("chk_promociones").checked;
	
	
	//LIMPIAMOS LOS MENSAJITOS DE VALIDACION EN CASO 
	//QUE ESTEN LLENOS PARA UNA NUEVA VALIDACION
	limpiar_div_mensajitos();
	
		
		if(txt_nombres.length==0){
			var men="Por favor ingrese su Nombre.";
			    
				document.getElementById("div_men_nombres").innerHTML=men;
				document.getElementById("txt_nombres").focus();
				return false;
		}//endif
		
		if(txt_apellidos.length==0){
			var men="Por favor ingrese su Apellido.";
			    document.getElementById("div_men_apellidos").innerHTML=men;
				document.getElementById("txt_apellidos").focus();
				return false;
		}//endif
		
		if(txt_email.length==0){
			var men="Por favor ingrese su E-mail.";
			   document.getElementById("div_men_email").innerHTML=men;
				document.getElementById("txt_email").focus();
				return false;
		}//endif
		
		//VALIDAMOS LA FECHA (DIA, MES Y AÑO)
		if(txt_dia.length==0){
			var men="Por favor complete la Fecha de Nacimiento.";
			   document.getElementById("div_men_fecha_nac").innerHTML=men;
				document.getElementById("txt_dia").focus();
				return false;
		}//endif
		
		if(txt_mes.length==0){
			var men="Por favor complete la Fecha de Nacimiento.";
			    document.getElementById("div_men_fecha_nac").innerHTML=men;
				document.getElementById("txt_mes").focus();
				return false;
		}//endif
		
		if(txt_anio.length==0){
			var men="Por favor complete la Fecha de Nacimiento.";
			    document.getElementById("div_men_fecha_nac").innerHTML=men;
				document.getElementById("txt_anio").focus();
				return false;
		}//endif
		
		
		
		if(txt_telefono1.length==0){
			var men="Por favor ingrese su Tel&eacute;fono.";
			    document.getElementById("div_men_telefono").innerHTML=men;
				document.getElementById("txt_telefono1").focus();
				return false;
		}//endif
		
		if(txt_postal.length==0){
			var men="Por favor ingrese C&oacute;digo Postal.";
			   document.getElementById("div_men_postal").innerHTML=men;
				document.getElementById("txt_postal").focus();
				return false;
		}//endif
		
		if(txt_poblacion.length==0){
			var men="Por favor ingrese su Poblaci&oacute;n.";
			   document.getElementById("div_men_poblacion").innerHTML=men;
				document.getElementById("txt_poblacion").focus();
				return false;
		}//endif
		
		if(txt_direccion.length==0){
			var men="Por favor ingrese su Direcci&oacute;n.";
			    document.getElementById("div_men_direccion").innerHTML=men;
				document.getElementById("txt_direccion").focus();
				return false;
		}//endif
		
		if(txt_usuario.length==0){
			var men="Por favor ingrese su Usuario.";
			   document.getElementById("div_men_usuario").innerHTML=men;
				document.getElementById("txt_usuario").focus();
				return false;
		}//endif
		
		
		if(txt_pass1.length==0){
			var men="Por favor ingrese su Contrase&ntilde;a.";
			   document.getElementById("div_men_pass1").innerHTML=men;
				document.getElementById("txt_pass1").focus();
				return false;
		}//endif
		
		if(txt_pass2.length==0){
			var men="Por favor ingrese su Retipeo de Contrase&ntilde;a.";
			    document.getElementById("div_men_pass2").innerHTML=men;
				document.getElementById("txt_pass2").focus();
				return false;
		}//endif
	
	
	   //VALIDAMOS QUE EL PASS1 Y EL PASS2 SEAN IGUALES
	   if(txt_pass1!=txt_pass2){
		   
		   var men="La contrase&ntilde;a y el retipeo de contrase&ntilde;a no coinciden.";
		   document.getElementById("div_men_valida").innerHTML=men;
		   document.getElementById("txt_pass1").focus();
		   return false;
		   
	   }
	   
	   
	   	
		var terminos="0";
		if(chk_terminos==true){
		
		 	terminos="1";
		
		}else{
			
			var men="Para registrarse tiene que aceptar con un check los t&eacute;rminos y condiciones.";
			document.getElementById("div_men_terminos").innerHTML=men;
			document.getElementById("chk_terminos").focus();
			
			return false;
		}//endIF
	   
	   
	   var promociones="0";
		if(chk_promociones==true){
		
		 	promociones="1";
		
		}//endIF
	   
	   
	   //CREAMOS LA FECHA
	    var fecha_nac=txt_anio+"-"+txt_mes+"-"+txt_dia;
        
		
		var cadena=txt_nombres+"|"+txt_apellidos+"|"+txt_email+"|"+fecha_nac+"|";
		    cadena+=txt_telefono1+"|"+txt_telefono2+"|"+txt_postal+"|"+txt_poblacion+"|";
			cadena+=txt_direccion+"|"+txt_obs+"|"+txt_usuario+"|"+txt_pass1+"|"+terminos+"|"+promociones;
		
			//alert(cadena);
			transaccion("2", cadena);
			gif_ajax("sp_gif_graba");
			
}

function limpiar_div_mensajitos(){
	document.getElementById("div_men_nombres").innerHTML="";
	document.getElementById("div_men_apellidos").innerHTML="";
	document.getElementById("div_men_email").innerHTML="";
	document.getElementById("div_men_fecha_nac").innerHTML="";
	document.getElementById("div_men_telefono").innerHTML="";
	document.getElementById("div_men_postal").innerHTML="";
	document.getElementById("div_men_poblacion").innerHTML="";
	document.getElementById("div_men_direccion").innerHTML="";
	document.getElementById("div_men_usuario").innerHTML="";
	document.getElementById("div_men_pass1").innerHTML="";
	document.getElementById("div_men_pass2").innerHTML="";
	document.getElementById("div_men_valida").innerHTML="";
	document.getElementById("div_men_terminos").innerHTML="";
}


function gif_ajax(id_object){
	
	 var strhtml="<img src='imagenes_administracion/indicator.gif'>";
	document.getElementById(id_object).innerHTML=strhtml;
	
}