function nuevoAjax(){
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	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) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp=new XMLHttpRequest(); }

	return xmlhttp;
}


function verificaPais(selectACargar){
var selectAnterior=selectACargar-1;
var valor=document.getElementById("CP_"+selectAnterior).options[document.getElementById("CP_"+selectAnterior).selectedIndex].value;
var elemento;



	if (valor == 'MX'){
		y = 2;
		while (y < 6){
				elemento2 = document.getElementById("codigo_postal_"+y);
				newInput = document.createElement('select');
				newInput.setAttribute("id","CP_"+y);
				newInput.setAttribute("name","CP_"+y);
				newInput.setAttribute("class","txtTable");
				newInput.setAttribute("disabled","true");
				newInput.setAttribute("onChange","javascript:this.value=this.value.toUpperCase();");
				if(y == 5)
					newInput.setAttribute("maxlength","9");
				elemento2.replaceChild(newInput, elemento2.lastChild);
				y++;
			}
		cargaContenido_categorias(2)
	}
	else{
		if (valor == 'US' || valor == 'CA'){
			ajax1=nuevoAjax();
			ajax1.open("GET", "postales_slave.php?seleccionado="+valor+"&categ=10", true);
			ajax1.onreadystatechange=function()
			{
				if (ajax1.readyState==1){
					// Mientras carga elimino la opcion "Elige" y pongo una que dice "Cargando"
					elemento=document.getElementById("codigo_postal_2");
					elemento.length=0;
					var opcionCargando=document.createElement("option"); opcionCargando.value=0; opcionCargando.innerHTML="Cargando...";
					elemento.appendChild(opcionCargando); elemento.disabled=true;
				}
				if (ajax1.readyState==4){
					var i = selectACargar;
					var x=0, y=null, flag=true;
					document.getElementById("codigo_postal_2").innerHTML=ajax1.responseText;
					//document.getElementById("codigo_postal_txt_1").innerHTML=document.getElementById("CP_txt_1").value;
				}
			}
			ajax1.send(null);
			y = 2;
			while (y < 6){
				elemento2 = document.getElementById("codigo_postal_"+y);
				elemento2.removeChild(elemento2.lastChild);
				y++;
			}
			y  = 3;
			while (y < 6){
				newInput = document.createElement('input');
				newInput.setAttribute('type',"text");
				newInput.setAttribute("id","CP_"+y);
				newInput.setAttribute("name","CP_"+y);
				newInput.setAttribute("class","txtTable");
				newInput.setAttribute("onChange","javascript:this.value=this.value.toUpperCase();");
				if(y == 5)
					newInput.setAttribute("maxlength","9");
				elemento2 = document.getElementById("codigo_postal_"+y);
				elemento2.appendChild(newInput);
				y++;
			}
		}
		else{
			if(valor != 0){
				y = 3;
				elemento2 = document.getElementById("codigo_postal_2");
				elemento = document.getElementById("CP_2");
				elemento2.removeChild(elemento2.lastChild);
				while (y < 6){
					elemento2 = document.getElementById("codigo_postal_"+y);
					elemento2.removeChild(elemento2.lastChild);
					y++;
				}
				y  = 2;
				while (y < 6){
					newInput = document.createElement('input');
					newInput.setAttribute('type',"text");
					newInput.setAttribute("id","CP_"+y);
					newInput.setAttribute("name","CP_"+y);
					newInput.setAttribute("class","txtTable");
					newInput.setAttribute("onChange","javascript:this.value=this.value.toUpperCase();");
					if(y == 5)
						newInput.setAttribute("maxlength","9");
					elemento2 = document.getElementById("codigo_postal_"+y);
					elemento2.appendChild(newInput);
					y++;
				}
			}
			else{
				y =2;
				while (y < 6){
				elemento2 = document.getElementById("codigo_postal_"+y);
				newInput = document.createElement('select');
				newInput.setAttribute("id","CP_"+y);
				newInput.setAttribute("name","CP_"+y);
				newInput.setAttribute("class","txtTable");
				newInput.setAttribute("disabled","true");
				newInput.setAttribute("onChange","javascript:this.value=this.value.toUpperCase();");
				elemento2.replaceChild(newInput, elemento2.lastChild);
				y++;
			}
			}
		}
	}

}

function cargaContenido_categorias(selectACargar){
	// Recibo el número correspondiente al combo que se debe llenar de datos
	var selectAnterior=selectACargar-1; // Obtengo el número del combo que activó el evento onChange
	// Extraigo el valor del combo que se ha cambiado
	var valor=document.getElementById("CP_"+selectAnterior).options[document.getElementById("CP_"+selectAnterior).selectedIndex].value;
	//var rows_query=document.getElementById("registro_query").value;
	var elemento;

	if(valor!=0){
		ajax=nuevoAjax();

		// Envio al servidor el valor seleccionado y el combo al cual se le deben poner los datos
		ajax.open("GET", "postales_slave.php?seleccionado="+valor+"&categ="+selectACargar, true);
		ajax.onreadystatechange=function()
		{
			if (ajax.readyState==1){
				// Mientras carga elimino la opcion "Elige" y pongo una que dice "Cargando"
				elemento=document.getElementById("CP_"+selectACargar);
				elemento.length=0;
				var opcionCargando=document.createElement("option"); opcionCargando.value=0; opcionCargando.innerHTML="Cargando...";
				elemento.appendChild(opcionCargando); elemento.disabled=true;
			}
			if (ajax.readyState==4){
				var i = selectACargar;
				var x=0, y=null, flag=true;
				document.getElementById("codigo_postal_"+selectACargar).innerHTML=ajax.responseText;
				if(selectACargar == '3')
					document.getElementById("codigo_postal_txt_3").innerHTML=document.getElementById("CP_txt_2").value;
			}
		}
		ajax.send(null);
	}

	/* Colocamos mediante los whiles los selects en "Selecciona opción..." cuando el select anterior
	ha quedado en estado "Elige" */
	var x=2, y=null;
	while(x<=4){
		valor=document.getElementById("CP_"+x).options[document.getElementById("CP_"+x).selectedIndex].value;
		if(valor==0){
			while(x<=4) {
				y=x+1;
				elemento=document.getElementById("CP_"+y);
				elemento.length=0;
				if (elemento.tagName == "select"){
					var opcionSelecciona=document.createElement("option"); opcionSelecciona.value=0;
					opcionSelecciona.innerHTML="Selecciona opci&oacute;n...";
					elemento.appendChild(opcionSelecciona);
				}
				elemento.disabled=true;
				x++;
			}//end while
		}//end if
		x++;
	}//end while
}