function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function preguntarCCZ(id) {
    http.open('get', '/geo.php?action=ccz&id='+id);
    http.onreadystatechange = preguntarCCZResponse;
    http.send(null);
}

function preguntarCCZResponse() {
    if(http.readyState == 4){
        var response = http.responseText;

		if (response == 1) {
			document.getElementById('input_ccz_field').value = '';			
			document.getElementById('input_ccz').style.visibility = 'visible';
			}
		else {
			document.getElementById('input_ccz').style.visibility = 'hidden';
			document.getElementById('input_ccz_field').value = '-1';
			}
    }
}

function cambiarCiudad(id) {
    http.open('get', '/geo.php?action=ciudad&id='+id);
    http.onreadystatechange = cambiarCiudadResponse;
    http.send(null);
}

function cambiarCiudadResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
		document.getElementById('div_ciudad').innerHTML = response;
		id = document.getElementById('select_ciudad').value;
		preguntarCCZ(id);
    }
}


function verificar_form(f) {
	var errores = '';
		if (f.est_razonsocial.value.length == 0) { errores = errores+"\t - La Razón Social no puede estar vacía.\n"; }
		if (f.est_nombre.value.length == 0) { errores = errores+"\t - El Nombre Comercial no puede estar vacío.\n"; }
		if (f.est_ruc.value.length != 12) { errores = errores+"\t - El código de RUC es inválido.\n"; }
		if (f.est_direccion.value.length == 0) { errores = errores+"\t - La Direccion no puede estar vacía.\n"; }
		if (f.est_telefono.value.length == 0) { errores = errores+"\t - El Teléfono no puede estar vacío.\n"; }
		if (f.est_cajas.value.length == 0) { errores = errores+"\t - La Cantidad de Cajas no puede estar vacía.\n"; }
		if (f.est_cnt_nombre.value.length == 0) { errores = errores+"\t - El Nombre de Contacto no puede estar vacío.\n"; }
		if (f.est_cnt_email.value.length == 0) { errores = errores+"\t - El E-Mail de Contacto no puede estar vacío.\n"; }
		if (! ((f.est_cnt_ci.value.length >= 6) && (f.est_cnt_ci.value.length <= 10))) { errores = errores+"\t - CI de la Persona de Contacto inválido.\n"; }

	if (errores.length == 0) { return true; }
	else {
		alert("Errores en el formulario:\n"+errores);
		return false;
		}
	}



var time = 0;
function captcha_reload() {
	time++;
	var captcha_image = document.getElementById('captcha_image');
	captcha_image.src = captcha_image.src+'?iteration='+time;
	}