<!-- @require yahoo/yahoo-min.js -->
<!-- @require event/event-min.js -->
<!-- @require connection/connection-min.js -->

/*
 * AjaxObjGetCodigo is a hypothetical object that encapsulates the transaction
 *     request and callback logic.
 *
 * handleSuccess( ) provides success case logic
 * handleFailure( ) provides failure case logic
 * processResult( ) displays the results of the response from both the
 * success and failure handlers
 * call( ) calling this member starts the transaction request.
 */
var AjaxObjGetCodigo = {

	tipo_via : '',
	nombre_via : '',
	altura : '',
	comuna : '',
	ciudad : '',
	este : '',
	norte : '',
	entorno : '',
	codigo : '',

	codigoEstaCargado : false,

	URL_SVC_GET_CODIGO: "./mapaCelular/svcGetCodigo.asp",
	IDDIV_CODIGO1: "id_block_celular_codigo1",
	IDDIV_CODIGO2: "id_block_celular_codigo2",
	IDDIV_CODIGO3: "id_block_celular_codigo3",
	IDDIV_CODIGO4: "id_block_celular_codigo4",
	IDDIV_CODIGO5: "id_block_celular_codigo5",
	MSG_ERROR : "En este momento no podemos atenderlo"
						 +"\n\n Por favor, intente más tarde ...",

	handleSuccess:function(o){
    var respuesta, hayError;
    hayError = false;
    try{
      respuesta = o.responseXML.documentElement;

    }catch(err){
      alert(this.MSG_ERROR);
      document.body.style.cursor = 'auto';
      hayError = true;
    }
    if (hayError==false)
    	this.processResult(respuesta);
	},

	handleFailure:function(o){
		//alert("Transacción id: " + o.tId + "-->HTTP código de estado: " + o.status + "-->Mensaje del código de estado: " + o.statusText);
    alert(this.MSG_ERROR);
    document.body.style.cursor = 'auto';
	},

	processResult:function(respuesta){

		//mostrare mensaje de error
		nodoError    = respuesta.getElementsByTagName('ERROR');
		if (nodoError[0] && nodoError[0].firstChild){
      var descripcion = nodoError[0].firstChild.data;
			if (descripcion!=''){
				alert(descripcion);
				document.body.style.cursor = 'auto';
				return;
      }
		}

		nodoCodigo    = respuesta.getElementsByTagName('CODIGO');
		this.codigo 	= nodoCodigo[0].firstChild.data;
		document.getElementById(this.IDDIV_CODIGO1).innerHTML =this.codigo;
		document.getElementById(this.IDDIV_CODIGO2).innerHTML =this.codigo;
		document.getElementById(this.IDDIV_CODIGO3).innerHTML =this.codigo;
		document.getElementById(this.IDDIV_CODIGO4).innerHTML =this.codigo;
		document.getElementById(this.IDDIV_CODIGO5).innerHTML =this.codigo;
		this.codigoEstaCargado=true;
	 	document.body.style.cursor = 'auto';
	},

	getPostDataArgs:function(){
		var postData="";
		postData += "tipo_via="+((this.tipo_via.length>0)?this.tipo_via:"");
		postData += "&nombre_via="+((this.nombre_via.length>0)?this.nombre_via:"");
		postData += "&altura="+((this.altura.length>0)?this.altura:"");
		postData += "&comuna="+((this.comuna.length>0)?this.comuna:"");
		postData += "&ciudad="+((this.ciudad.length>0)?this.ciudad:"");
		postData += "&este="+((this.este.length>0)?this.este:"");
		postData += "&norte="+((this.norte.length>0)?this.norte:"");
		postData += "&entorno="+((this.entorno.length>0)?this.entorno:"");
		return postData;
	},

	startRequest:function() {
		var postData =   this.getPostDataArgs();
		document.body.style.cursor = 'wait';
		rExp = / /gi;
		postData = postData.replace(rExp,"+");

		YAHOO.util.Connect.asyncRequest('POST', this.URL_SVC_GET_CODIGO, callbackGetCodigo, postData);
	},

	init:function(idForm){
		this.tipo_via 	= ""+document.forms[idForm]['TipoVia1'].value;
		this.nombre_via = ""+document.forms[idForm]['NombreVia1'].value;
		this.altura			= ""+document.forms[idForm]['Altura1'].value;
		this.comuna			= ""+document.forms[idForm]['Comuna1'].value;
		this.ciudad			= ""+document.forms[idForm]['ciudad'].value;
		this.este				= ""+document.forms[idForm]['Punto_X'].value;
		this.norte			= ""+document.forms[idForm]['Punto_Y'].value;
		try{
			this.entorno		= getObjFromFrame('id_entorno','DescEntorno').innerHTML;
		}catch(er){
			this.entorno = "NO PUDO SER OBTENIDO"
		}
	}
};

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var callbackGetCodigo =
{
	success:AjaxObjGetCodigo.handleSuccess,
	failure:AjaxObjGetCodigo.handleFailure,
  timeout: 100000,
	scope: AjaxObjGetCodigo
};