function nuevoAjax(){
	// Crea el objeto AJAX.
	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,langt){
	var selectAnterior=selectACargar-1;
	var valor=document.getElementById('CP_'+selectAnterior).options[document.getElementById('CP_'+selectAnterior).selectedIndex].value;
	var elemento;
	if(selectAnterior == 1){
		if(valor == 'MX'){
			document.form.E_Tel_Pais_1.value='52';
		}
		else{
			if(valor == 'US' || valor == 'CA'){
				document.form.E_Tel_Pais_1.value='001';
			}
			else{
				document.form.E_Tel_Pais_1.value='';
			}
		}
	}
	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);
			elemento2.replaceChild(newInput, elemento2.lastChild);
			y++;
		}
		cargaContenido_categorias(2,langt)
	}else{
		if(valor == 'US' || valor == 'CA'){
			y=2;
			elemento2 = document.getElementById('codigo_postal_'+y);
			newInput = document.createElement('select');
			newInput.setAttribute('id','CP_'+y);
			newInput.setAttribute('name','CP_'+y);
			if(elemento2.lastChild){
				elemento2.replaceChild(newInput, elemento2.lastChild);
			}
			y++;
			while(y < 6){
				elemento2 = document.getElementById('codigo_postal_'+y);
				newInput = document.createElement('input');
				newInput.setAttribute('type','text');
				newInput.setAttribute('id','CP_'+y);
				newInput.setAttribute('name','CP_'+y);
				newInput.onchange=function(){uppercase(this);};
				if(y!=5){newInput.setAttribute('size','30');}else{newInput.setAttribute('size','8');newInput.setAttribute('maxlength','8');}
				if(elemento2.lastChild){
					elemento2.replaceChild(newInput, elemento2.lastChild);
				}
				y++;
			}
			cargaContenido_categorias(2,langt)
		}else{
			if(valor != -1){
				y=2;
				while(y < 6){
					elemento2 = document.getElementById('codigo_postal_'+y);
					newInput = document.createElement('input');
					newInput.setAttribute('type','text');
					newInput.setAttribute('id','CP_'+y);
					newInput.setAttribute('name','CP_'+y);
					newInput.onchange=function(){uppercase(this);};
					if(y!=5){newInput.setAttribute('size',30);}else{newInput.setAttribute('size',8);newInput.setAttribute('maxLength',8);}
					if(elemento2.lastChild){
						elemento2.replaceChild(newInput, elemento2.lastChild);
					}
					y++;
				}
			}else{
				y=2;
				while(y < 6){
					elemento2 = document.getElementById('codigo_postal_'+y);
					newInput = document.createElement('input');
					newInput.setAttribute('type','text');
					newInput.setAttribute('id','CP_'+y);
					newInput.setAttribute('name','CP_'+y);
					newInput.onchange=function(){uppercase(this);};
					newInput.setAttribute('readOnly','true');
					if(y!=5){newInput.setAttribute('size',30);}else{newInput.setAttribute('size',8);newInput.setAttribute('maxLength',8);}
					if(elemento2.lastChild){
						elemento2.replaceChild(newInput, elemento2.lastChild);
					}
					y++;
				}
			}
		}
	}
}

function cargaContenido_categorias(selectACargar,langt){
	var elemento;
	var opcionCargando

	// 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;
	if(valor!=-1){
		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+'&langt='+langt, true);
		ajax.onreadystatechange=function(){
			if (ajax.readyState==1){
				elemento=document.getElementById('CP_'+selectACargar);
				elemento.length=0;
				if(selectACargar!=5){
					opcionCargando=document.createElement('option'); opcionCargando.value=-1;
					if(langt=='en'){
						opcionCargando.innerHTML='Downloading...';
					}else{
						opcionCargando.innerHTML='Cargando...';
					}
					elemento.appendChild(opcionCargando);
				}
				elemento.disabled=true;
			}
			if (ajax.readyState==4){
				document.getElementById('codigo_postal_'+selectACargar).innerHTML=ajax.responseText;
				if(selectACargar == '3' && valor=='D.F.'){
					if(langt=='en'){
						document.getElementById("t_E_Ciudad").innerHTML='<span class="requiered">*</span>Delegation:';
					}else{
						document.getElementById("t_E_Ciudad").innerHTML='<span class="requiered">*</span>Delegación:';
					}
				}else{
					if(langt=='en'){
						document.getElementById("t_E_Ciudad").innerHTML='<span class="requiered">*</span>City:';
					}else{
						document.getElementById("t_E_Ciudad").innerHTML='<span class="requiered">*</span>Ciudad/Municipio:';
					}
				}
			}
		}
		ajax.send(null);
	}
}
