//mostrar capa
function mostrar(nombreCapa){
document.getElementById(nombreCapa).style.display="block";
}
//ocultar capa
function ocultar(nombreCapa){
document.getElementById(nombreCapa).style.display="none";
}

function kH(e) {
var pK = document.all? window.event.keyCode:e.which;
return pK != 13;
}
document.onkeypress = kH;
if (document.layers) document.captureEvents(Event.KEYPRESS);

//declaro mi objeto httpdRequest
var http = getXmlHttpObject();
/*
Este es nuestro manejador de eventos, lo que hace despues de recibir la info en el metodo sendQueryString

Estado del Objeto:
0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete
*/
function handleHttpResponse()
{
	
	var url= "";
	var destino = "";
//	results = http.responseText;
	var resultado_existe="";

	var existe_suscriptor= "";
	var nombre_suscriptor= "";
	var apellidos_suscriptor= "";
	var razon_social= "";		
	var mensaje_nit="";
	var caso="";


	
	if (http.readyState == 1)
	{
			document.getElementById('mensajes').style.display="block";
			document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#990000'>Verificando...</div>";	
	}
if (http.readyState == 4)
	{
			var vari=http.responseXML;
			caso=unescape(vari.getElementsByTagName('caso').item(0).firstChild.data); 
			switch(caso){
			case "0":
					resultado_existe= unescape(vari.getElementsByTagName('existe').item(0).firstChild.data); 	
					if(resultado_existe=='-1')
					{
						document.formulario_registro.correo_electronico.value="";
						//document.formulario_registro.correo_electronico.focus();
						document.getElementById('correo_electronico').style.border="1px solid #FF0000";
						document.getElementById('mensajes').style.display="block";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FF0000'><strong>Este correo ya esta registrado!.</strong></div>";				
					}else if(resultado_existe=='0'){
						document.getElementById('correo_electronico').style.background="#FFFFFF";
						document.getElementById('correo_electronico').style.border="1px solid #999999";
						//document.formulario_registro.clave.focus();
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#0000FF'><strong>Bienvenido al directorio, puede continuar el registro</strong></div>";	
					setTimeout("ocultar('mensajes')",3000);
						}
					else if(resultado_existe=='1'){
						document.getElementById('correo_electronico').style.background="#FFF1A8";
						document.formulario_registro.correo_electronico.value="";
						document.getElementById('correo_electronico').style.border="1px solid #FF0000";
						//document.formulario_registro.correo_electronico.focus();
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FF0000'><strong>Correo electrónico No Valido</strong></div>";	
						}
					break;
			case "1":
			if(vari.getElementsByTagName('mensaje_nit').item(0).firstChild.data=="")
				mensaje_nit="";
			else{
				mensaje_nit= unescape(vari.getElementsByTagName('mensaje_nit').item(0).firstChild.data); 
				if(mensaje_nit==0){
					document.getElementById('nit').style.background="#FFF1A8";
					document.getElementById('digito_verificacion').style.background="#FFF1A8";
					document.getElementById('digito_verificacion').style.border="1px solid #FF0000";
					document.getElementById('nit').style.border="1px solid #FF0000";
					document.formulario_registro.digito_verificacion.value="";
					document.formulario_registro.nit.value="";
					//document.formulario_registro.nit.focus();
					document.getElementById('mensajes').style.display="block";
					document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FF0000'><strong>Nit no valido</strong>. Verifica el Número o dígito de verificación</div>";	
				}else if(mensaje_nit==1){
					document.getElementById('nit').style.background="#FFFFFF";
					document.getElementById('digito_verificacion').style.background="#FFFFFF";
					document.getElementById('digito_verificacion').style.border="1px solid #AAAAAA";
					document.getElementById('nit').style.border="1px solid #AAAAAA";
					document.getElementById('mensajes').style.display="block";	
					/*CARGO DATOS DE PREREGISTRO*/
					document.formulario_registro.id_directorio.value=vari.getElementsByTagName('id_directorio').item(0).firstChild.data;
					document.formulario_registro.correo_e_local.value=vari.getElementsByTagName('correo_e_local').item(0).firstChild.data;
					document.formulario_registro.ciiu.value=vari.getElementsByTagName('descripcion_ciiu').item(0).firstChild.data;
					document.formulario_registro.test_id.value=vari.getElementsByTagName('codigo_ciiu').item(0).firstChild.data;
					document.formulario_registro.nombre_local.value=vari.getElementsByTagName('nombre_local').item(0).firstChild.data;
					document.formulario_registro.telefono_local.value=vari.getElementsByTagName('telefono_local').item(0).firstChild.data;
					document.formulario_registro.direccion_local.value=vari.getElementsByTagName('direccion_local').item(0).firstChild.data;
					document.formulario_registro.descripcion_local.value=vari.getElementsByTagName('descripcion_local').item(0).firstChild.data;
					/*CARGO MENSAJE*/
					document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#0000FF'><strong>Nit Valido. Preregistro Detectado!<br><span style='font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#FF0000'>Patrocinado por Cámara de Comercio de Pereira</span></strong></div>";
					setTimeout("ocultar('mensajes')",6000);
				}
				else if(mensaje_nit==2){
					document.getElementById('nit').style.background="#FFFFFF";
					document.getElementById('digito_verificacion').style.background="#FFFFFF";
					document.getElementById('digito_verificacion').style.border="1px solid #AAAAAA";
					document.getElementById('nit').style.border="1px solid #AAAAAA";
					document.getElementById('mensajes').style.display="block";
					document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#0000FF'><strong>Nit Valido.</strong></div>";	
					setTimeout("ocultar('mensajes')",3000);
				}
				}
				break;
				case "2":
			if(vari.getElementsByTagName('mensaje_clave').item(0).firstChild.data=="")
				mensaje_clave="";
			else{
				mensaje_clave= unescape(vari.getElementsByTagName('mensaje_clave').item(0).firstChild.data); 
				if(mensaje_clave==0){
					document.getElementById('clave').style.background="#FFF1A8";
					document.getElementById('clave').style.border="1px solid #FF0000";
					document.getElementById('confirmar_clave').style.background="#FFF1A8";
					document.getElementById('confirmar_clave').style.border="1px solid #FF0000";
					document.formulario_registro.clave.value="";
					document.formulario_registro.confirmar_clave.value="";
					//document.formulario_registro.clave.focus();
					document.getElementById('mensajes').style.display="block";
					document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FF0000'><strong>Claves diferentes</strong>.<br> Por favor escribe de nuevo la clave y su confirmación</div>";	
				}else if(mensaje_clave==1){
					document.getElementById('clave').style.background="#FFFFFF";
					document.getElementById('clave').style.border="1px solid #AAAAAA";
					document.getElementById('confirmar_clave').style.background="#FFFFFF";
					document.getElementById('confirmar_clave').style.border="1px solid #AAAAAA";
					document.getElementById('mensajes').style.display="block";
					document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#0000FF'><strong>Claves correctas!</strong></div>";	
					setTimeout("ocultar('mensajes')",3000);
				}
				else if(mensaje_clave==2){
					document.getElementById('clave').style.background="#FFF1A8";
					document.getElementById('clave').style.border="1px solid #FF0000";
					document.getElementById('confirmar_clave').style.background="#FFF1A8";
					document.getElementById('confirmar_clave').style.border="1px solid #FF0000";
					document.formulario_registro.clave.value="";
					document.formulario_registro.confirmar_clave.value="";
					//document.formulario_registro.clave.focus();
					document.getElementById('mensajes').style.display="block";
					document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FF0000'><strong>La clave debe ser de minimo 5 caracteres</strong>.<br> Por favor escribe de nuevo la clave y su confirmación</div>";	
				}
				}
				break;
				case "3":
					resultado_existe= unescape(vari.getElementsByTagName('existe').item(0).firstChild.data); 	
					if(resultado_existe=='-1')
					{
						document.formulario_registro.correo_e_local.value="";
						//document.formulario_registro.correo_e_local.focus();
						document.getElementById('correo_e_local').style.border="1px solid #FF0000";
						document.getElementById('correo_e_local').style.background="#FFF1A8";
						document.getElementById('mensajes').style.display="block";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FF0000'><strong>Este correo ya esta registrado a nombre de otra empresa!.</strong></div>";				
					}else if(resultado_existe=='0'){
						document.getElementById('correo_e_local').style.background="#FFFFFF";
						document.getElementById('correo_e_local').style.border="1px solid #999999";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#0000FF'><strong>Correo válido!</strong></div>";	
						setTimeout("ocultar('mensajes')",3000);
						}
					else if(resultado_existe=='1'){
						document.getElementById('correo_e_local').style.background="#FFF1A8";
						document.formulario_registro.correo_e_local.value="";
						document.getElementById('correo_e_local').style.border="1px solid #FF0000";
						//document.formulario_registro.correo_e_local.focus();
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FF0000'><strong>Correo electrónico No Valido</strong></div>";	
						}
					else if(resultado_existe=='2')
								ocultar('mensajes');
					break;
				case "4": 
					nombre_valido=unescape(vari.getElementsByTagName('nombre_valido').item(0).firstChild.data);
					if(nombre_valido==0){
						document.formulario_registro.nombre_local.value="";
						//document.formulario_registro.nombre_local.focus();
						document.getElementById('nombre_local').style.border="1px solid #FF0000";
						document.getElementById('nombre_local').style.background="#FFF1A8";
						document.getElementById('mensajes').style.display="block";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FF0000'><strong>El Nombre del Establecimiento debe tener mas de 4 carácteres</strong></div>";						
						}
					else{
						document.getElementById('nombre_local').style.background="#FFFFFF";
						document.getElementById('nombre_local').style.border="1px solid #999999";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#0000FF'><strong>Nombre del Establecimiento válido!</strong></div>";	
						setTimeout("ocultar('mensajes')",3000);						
						}
				break;
				case "5": 
					nombre_u_valido=unescape(vari.getElementsByTagName('nombre_u_valido').item(0).firstChild.data);
					if(nombre_u_valido==0){
						document.formulario_registro.nombre.value="";
						//document.formulario_registro.nombre.focus();
						document.getElementById('nombre').style.border="1px solid #FF0000";
						document.getElementById('nombre').style.background="#FFF1A8";
						document.getElementById('mensajes').style.display="block";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FF0000'><strong>El Nombre no es valido!</strong></div>";						
						}
					else{
						document.getElementById('nombre').style.background="#FFFFFF";
						document.getElementById('nombre').style.border="1px solid #999999";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#0000FF'><strong>Nombre válido!</strong></div>";	
						setTimeout("ocultar('mensajes')",3000);						
						}
				break;
				case "6": 
					nombre_a_valido=unescape(vari.getElementsByTagName('nombre_a_valido').item(0).firstChild.data);
					if(nombre_a_valido==0){
						document.formulario_registro.apellidos.value="";
						//document.formulario_registro.apellidos.focus();
						document.getElementById('apellidos').style.border="1px solid #FF0000";
						document.getElementById('apellidos').style.background="#FFF1A8";
						document.getElementById('mensajes').style.display="block";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FF0000'><strong>El Campo Apellidos debe tener mas de 4 carácteres</strong></div>";						
						}
					else{
						document.getElementById('apellidos').style.background="#FFFFFF";
						document.getElementById('apellidos').style.border="1px solid #999999";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#0000FF'><strong>Apellido(s) válido!</strong></div>";	
						setTimeout("ocultar('mensajes')",3000);						
						}
				break;
				case "7": 
					telefono=unescape(vari.getElementsByTagName('telefono').item(0).firstChild.data);
					if(telefono==0){
						document.formulario_registro.telefono.value="";
						//document.formulario_registro.telefono.focus();
						document.getElementById('telefono').style.border="1px solid #FF0000";
						document.getElementById('telefono').style.background="#FFF1A8";
						document.getElementById('mensajes').style.display="block";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FF0000'><strong>El Campo Teléfono debe tener mas de 4 carácteres</strong></div>";						
						}
					else{
						document.getElementById('telefono').style.background="#FFFFFF";
						document.getElementById('telefono').style.border="1px solid #999999";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#0000FF'><strong>Télefono válido!</strong></div>";	
						setTimeout("ocultar('mensajes')",3000);						
						}
				break;
				case "8": 
					direccion=unescape(vari.getElementsByTagName('direccion').item(0).firstChild.data);
					if(direccion==0){
						document.formulario_registro.direccion.value="";
						//document.formulario_registro.direccion.focus();
						document.getElementById('direccion').style.border="1px solid #FF0000";
						document.getElementById('direccion').style.background="#FFF1A8";
						document.getElementById('mensajes').style.display="block";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FF0000'><strong>El Campo Dirección no es válido</strong></div>";						
						}
					else{
						document.getElementById('direccion').style.background="#FFFFFF";
						document.getElementById('direccion').style.border="1px solid #999999";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#0000FF'><strong>Dirección válida!</strong></div>";	
						setTimeout("ocultar('mensajes')",3000);						
						}
				break;
				case "9": 
					telefono_local=unescape(vari.getElementsByTagName('telefono_local').item(0).firstChild.data);
					if(telefono_local==0){
						document.formulario_registro.telefono_local.value="";
						//document.formulario_registro.direccion.focus();
						document.getElementById('telefono_local').style.border="1px solid #FF0000";
						document.getElementById('telefono_local').style.background="#FFF1A8";
						document.getElementById('mensajes').style.display="block";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FF0000'><strong>El Campo Teléfono Local no es válido</strong></div>";						
						}
					else{
						document.getElementById('telefono_local').style.background="#FFFFFF";
						document.getElementById('telefono_local').style.border="1px solid #999999";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#0000FF'><strong>Teléfono válido!</strong></div>";	
						setTimeout("ocultar('mensajes')",3000);						
						}
				break;
				case "10": 
					direccion_local=unescape(vari.getElementsByTagName('direccion_local').item(0).firstChild.data);
					if(direccion_local==0){
						document.formulario_registro.direccion_local.value="";
						//document.formulario_registro.direccion.focus();
						document.getElementById('direccion_local').style.border="1px solid #FF0000";
						document.getElementById('direccion_local').style.background="#FFF1A8";
						document.getElementById('mensajes').style.display="block";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FF0000'><strong>El Campo Dirección Local no es válido</strong></div>";						
						}
					else{
						document.getElementById('direccion_local').style.background="#FFFFFF";
						document.getElementById('direccion_local').style.border="1px solid #999999";
						document.getElementById('mensajes').innerHTML= "<div style='width:90%; text-align:center; background-color:#FFF1A8; padding:2px; margin:2px; margin-left:10px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#0000FF'><strong>Dirección Local válida!</strong></div>";	
						setTimeout("ocultar('mensajes')",3000);						
						}
				break;
			default: break;
			}
		//innerHTML es para llenar el div resultado con info, recuerden javascript es casesensitive (una variable a != A)
		//document.getElementById('resultado').innerHTML = results;
	}
}

/*
aki es donde mandas el resquest del server de la pagina

Normalmenteo uno querie que el servidor procese la pagina enviada por XmlHttpRquest con objetos diferentes a lo normales.
Esto se hace reconociendo POST Y GET

Este script es asincronico -- sigue corriendo despues de aver enviado un request

lo de send(null) es lo normal todavia no se mas que esto asi que vamos por paso ^_^
*/
function sendQuerystring(url,caso)
{
	switch(caso){
		case 0: correo = document.getElementById("correo_electronico").value;
				url = url + correo;
				break;
		case 1: nit = document.getElementById("nit").value;
				digito_verificacion = document.getElementById("digito_verificacion").value;
				nit_completo=nit+"-"+digito_verificacion;
				url = url + nit_completo;
				break;
		case 2: clave = document.getElementById("clave").value;
				confirmar_clave = document.getElementById("confirmar_clave").value;
				clave_completa=clave+"-"+confirmar_clave;
				url = url + clave_completa;
				break;
		case 3: correo = document.getElementById("correo_e_local").value;
				nit = document.getElementById("nit").value;
				digito_verificacion = document.getElementById("digito_verificacion").value;
				datos=correo+"-"+nit+"-"+digito_verificacion;
				url = url + datos;
				break;
		case 4: nombre_local = document.getElementById("nombre_local").value;
				url = url + nombre_local;
				break;
		case 5: nombre = document.getElementById("nombre").value;
				url = url + nombre;
				break;
		case 6: apellidos = document.getElementById("apellidos").value;
				url = url + apellidos;
				break;
		case 7: telefono = document.getElementById("telefono").value;
				url = url + telefono;
				break;
		case 8: direccion = document.getElementById("direccion").value;
				url = url + direccion;
				break;
		case 9: telefono_local = document.getElementById("telefono_local").value;
				url = url + telefono_local;
				break;
		case 10: direccion_local = document.getElementById("direccion_local").value;
				url = url + direccion_local;
				break;
		default: break;
		}
	//abre la pagina
	http.open("GET", url , true);	
	//mando la informacion obtenido a la funcion handelHttpResponse
	http.onreadystatechange = handleHttpResponse;
	//send nada
	http.send(null);
}

/*
esta es la funcion principal, no topar solo copy y past y listo ^_^
*/
function getXmlHttpObject()
{
	var xmlhttp;

	/*@cc_on
	@if (@_jscript_version >= 5)
	try
	{
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
	try
	{
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (e)
	{
	xmlhttp = false;
	}
	}
	@else
	xmlhttp = false;
	@end @*/

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlhttp = false;
		}
	}

	return xmlhttp;
}