var xmlhttp;

function init(){
	if(window.XMLHttpRequest) xhr = new XMLHttpRequest();
	else if(window.ActiveXObject) xhr = new ActiveXObject("Microsoft.XMLHTTP");
	else alert("Su explorador no admite la tecnología usada en este formulario.");
	return xhr;	
}

function checkPais(e){
	document.form_emp.nom_emp.value = "";
	document.form_emp.nom_emp.disabled = false;
	var s = document.getElementById("filarut");
	if((e.value == "Chile") && (s == null)){
		var row = document.getElementById("tabla_1").insertRow(3);		
		row.id = "filarut";
		var c = row.insertCell(0);
		c.className = "input";
		c.innerHTML = "<input type='text' name='rut' onblur='checkRut(this)' maxlength='8' id='rut'></input>";
		var c = row.insertCell(0);
		c.className = "caption";
		c.innerHTML = "RUT empresa (sin ptos ni digito verificador):";		
		/*innerhtml = "<td class='caption'>RUT empresa (sin ptos ni digito verificador):</td><td class='input'>";
		innerhtml += "<input type='text' name='rut' onblur='checkRut(this)' maxlength='8' id='rut'></input></td>";
		row.innerHTML = innerhtml;*/
	}else if((e.value != "Chile") && (s != null)) var row = document.getElementById("tabla_1").deleteRow(3);
}
function checkRut(e){
	var xmlhttp = new init();
	xmlhttp.onreadystatechange = function(){		
		if(xmlhttp.readyState == 4){			
			if(xmlhttp.responseText != ""){
				document.getElementById("nom_emp").value = xmlhttp.responseText;
				document.form_emp.nom_emp.disabled = true;
				document.getElementById("nom_emp_hidden").value = xmlhttp.responseText;
			}else{
				document.getElementById("nom_emp").value = "";
				document.form_emp.nom_emp.disabled = false;
			}
		}else{
			document.getElementById("nom_emp").value = "Cargando datos...";
			document.form_emp.nom_emp.disabled = true;
		}
	}
	var URL = "scripts/checkRut.php?rut="+e.value;
	xmlhttp.open("GET", URL, true);
	xmlhttp.send(null);
}
function checkUser(e){
	if(e.value != ""){
		var xmlhttp = new init();
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4){
				if(xmlhttp.responseText > 0){
					alert("nombre de usuario '"+e.value+"' ya existe");	
					e.value = "";
					document.form_emp.botonEnviar.disabled = true;
				}else document.form_emp.botonEnviar.disabled = false;
			}
		}
		var URL = "scripts/checkUser.php?user="+e.value;
		xmlhttp.open("GET", URL, true);
		xmlhttp.send(null);
	}
}
function checkCaptcha(captcha, code, form, action_dir){
	if(captcha == "") captcha = "xxxx";
	var xmlhttp = new init();
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4){
			if(xmlhttp.responseText == 1){
				form.action = action_dir;
				form.submit();
			}else{
				alert("Codigo de verificacion incorrecto");
				var codigo = xmlhttp.responseText;
				var innerhtml = "<img src='scripts/captcha/?modo=imagen&codigo="+codigo+"' />";
				innerhtml += "<input name=code type='hidden' id='code' value='"+codigo+"' />";				
				document.getElementById("captcha_container").innerHTML = innerhtml;
			}
		}
	}
	var URL = "scripts/captcha/check.php?captcha="+captcha+"&code="+code;
	xmlhttp.open("GET", URL, true);
	xmlhttp.send(null);	
}
function checkCatalogos(marca){
	var xmlhttp = new init();
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4)	document.getElementById("lista_catalogos").innerHTML = xmlhttp.responseText;
		else document.getElementById("lista_catalogos").innerHTML = xmlhttp.responseText;
	}
	var URL = "scripts/checkCatalogos.php?marca="+marca+"&w="+window.innerWidth+"&h="+window.innerHeight;
	xmlhttp.open("GET", URL, true);
	xmlhttp.send(null);	
	}
	
/*function getFicha(e){
	var xmlhttp = new init();
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4)	document.getElementById("fp_container").innerHTML = xmlhttp.responseText;
	}
	var URL = "scripts/getProducto.php?producto="+e.value;
	xmlhttp.open("GET", URL, true);
	xmlhttp.send(null);	
	}*/