function mostra( elemento ) { mostraObjeto( $(elemento) ); }
function oculta( elemento ) { ocultaObjeto( $(elemento) ) }
function mostraObjeto( objeto ) {
	if ( objeto != undefined )
		objeto.style.display = "block";
}
function ocultaObjeto( objeto ) {
	if ( objeto != undefined )
		objeto.style.display = "none";
}
function getRequestAJAX() {
	if ( window.XMLHttpRequest )
		return new XMLHttpRequest();
	else if ( window.ActiveXObject )
		return new ActiveXObject( "Microsoft.XMLHTTP" );
	else {
		alert( "Seu browser n\xE3o suporta AJAX!" );
		return null;
	}
}

function neverNull( str ) {
	return str == undefined || str == null ? "" : str;
}

function isInt( n ) {
	return ! isNaN( parseInt(n) );
}
function possuiVirgula( str ) {
	return str.indexOf( "," ) >= 0;
}
function extraiValor( campo ) {
	var valor = campo.value;
	if ( isInt(valor) )
		return campo.options[campo.selectedIndex].text;
	else
		return valor;
}

function removeAcentos( str ) {
	str = str.replace( /[áàãâä]/g, 'a' );
	str = str.replace( /[ÁÀÃÂÄ]/g, 'A' );
	str = str.replace( /[éèêë&]/g, 'e' );
	str = str.replace( /[ÉÈÊË&]/g, 'E' );
	str = str.replace( /[íìîï]/g, 'i' );
	str = str.replace( /[ÍÌÎÏ]/g, 'I' );
	str = str.replace( /[óòõôö]/g, 'o' );
	str = str.replace( /[ÓÒÕÔÖ]/g, 'O' );
	str = str.replace( /[úùûü]/g, 'u' );
	str = str.replace( /[ÚÙÛÜ]/g, 'U' );
	str = str.replace( /[ç]/g, 'c' );
	str = str.replace( /[Ç]/g, 'C' );
	return str;
}

function ocultarSugestaoDeRuas( n ) {
	var x = $( "enderecos"+n ).firstChild;
	if ( x != undefined ) {
		if( x.getAttribute( "status" ) == "erro" ) {
			oculta( "enderecos"+n );
		}
	}
}

var ajaxSugestaoDeRuas = null;
function mostrarSugestaoDeRuas( event, campo, n ) {
		
	var tecla = event.keyCode;
	if ( tecla == 27 || tecla == 9 ) {
		oculta( "enderecos"+n );
	} else {
		var endereco = campo.value;
		if ( endereco.length >= 4 ) {
			if ( possuiVirgula(endereco) ) {
				oculta( "enderecos"+n );
			} else {
			
				var campoUf = HagahMapas.campos[n].uf;
				var campoCidade = HagahMapas.campos[n].cidade;
				var ufAtual = extraiValor( campoUf );
				var cidadeAtual = removeAcentos( extraiValor(campoCidade) );
	
				var url = HagahMapas.proxy+"?url="+escape("http://www.hagah.com.br/mapas/jsp/sugestao_de_ruas.jsp?endereco="+escape(endereco)+"&cidade="+escape(cidadeAtual)+"&estado="+ufAtual+"&n="+n);
				ajaxSugestaoDeRuas = getRequestAJAX();
				ajaxSugestaoDeRuas.open( "GET", url, true );
				ajaxSugestaoDeRuas.onreadystatechange = function() {
				    if ( ajaxSugestaoDeRuas.readyState == 4 ) {
					    if ( ajaxSugestaoDeRuas.status == 200 ) {
							var op = ajaxSugestaoDeRuas.responseText;
							if ( op != "" ) {
								mostra( "enderecos"+n );
								$( "enderecos"+n ).innerHTML = op;
							} else {
								oculta( "enderecos"+n );
							}
					    } else {
							alert( "Erro ("+ajaxSugestaoDeRuas.status+")." );
						}
					}
				}
				ajaxSugestaoDeRuas.send( null );
			}
		} else {
			oculta( "enderecos"+n );
		}
	}
}
function setText( tipo, logradouro, n ) {
	var numeroNoMesmoCampo = HagahMapas.campos[n].numeroNoMesmoCampo;
	if ( numeroNoMesmoCampo ) {
		HagahMapas.campos[n].campo.value = tipo+" "+logradouro+", ";
		HagahMapas.campos[n].campo.focus();
	} else {
		HagahMapas.campos[n].campo.value = tipo+" "+logradouro;
		if ( HagahMapas.campos[n].proximoCampo != undefined ) {
			HagahMapas.campos[n].proximoCampo.focus();
		}
	}
	oculta( "enderecos"+n );
}

function moveRow( event, id, n ) {
	var tecla = event.keyCode;
	if ( tecla == 40 && $("a"+n+""+(id+1)) != null ) { 
		id++;
		$( "a"+n+""+id ).focus();
	} else if ( tecla == 38 ) {
		if ( id == 1 ) {
			HagahMapas.campos[n].focus();
		} else {
			id--;
			$( "a"+n+""+id ).focus();
		}
	} else if ( tecla == 27 ) {
		oculta( "enderecos"+n );
		HagahMapas.campos[n].focus();
	}
}

function move( event, n ) {	
	var len = HagahMapas.campos[n].campo.value.length;
	if ( len > 0 ) {
		var tecla = event.keyCode;
		if ( tecla == 40 ) {
			if ( $( "a"+n+"1" ) != null ) {
				$( "a"+n+"1" ).focus();
			}
		} else if ( tecla == 27 ) {
			oculta( "enderecos"+n );
			HagahMapas.campos[n].focus();
		}
	}
}

//
function HagahMapas() {
}
//
HagahMapas.atributosToString = function( atributos ) {
	var atributosStr = "";
	if ( atributos != undefined ) {
		var t = atributos.length;
		var a;
		for ( var i = 0 ; i < t ; i++ ) {
			a = atributos[i]; 
			atributosStr += a.nome+"='"+a.valor+"' ";
		}
	}
	return atributosStr;
}
//
HagahMapas.setProxy = function( proxy ) {
	HagahMapas.proxy = proxy;
}
//
HagahMapas.campos = new Array();
//
HagahMapas.CampoEndereco = function( n ) {
	this.campo = new HagahMapas.CampoEndereco.Campo();
	this.sugestao = new HagahMapas.CampoEndereco.Sugestao( this.campo.n );
	//
	this.getString = function() {
		return this.campo.getString()+"<br />"+this.sugestao.getString();
	}
	this.toString = function( id ) {
		$( id ).innerHTML = this.getString();
		HagahMapas.campos[this.campo.n] = { campo: $("endereco"+this.campo.n), numeroNoMesmoCampo: this.campo.numeroNoMesmoCampo, proximoCampo: this.campo.proximoCampo, uf: this.campo.uf, cidade: this.campo.cidade };
	}
}
//
HagahMapas.CampoEndereco.Campo = function() {
	this.n = HagahMapas.campos.length;
	this.uf;
	this.cidade;
	this.numeroNoMesmoCampo;
	this.proximoCampo;
	this.atributos;
	//
	this.setNome = function( nome ) {
		this.nome = nome;
	}
	this.setClasse = function( classe ) {
		this.classe = classe;
	}
	this.setUf = function( uf ) {
		this.uf = uf;
	}
	this.setCidade = function( cidade ) {
		this.cidade = cidade;
	}
	this.setNumeroNoMesmoCampo = function( numeroNoMesmoCampo ) {
		this.numeroNoMesmoCampo = numeroNoMesmoCampo;
	}
	this.setProximoCampo = function( proximoCampo ) {
		this.proximoCampo = proximoCampo;
	}
	this.setValor = function( valor ) {
		this.valor = valor;
	}
	this.setAtributos = function( atributos ) {
		this.atributos = atributos;
	}
	//
	this.getString = function() {
		var atributosStr = HagahMapas.atributosToString( this.atributos );
		return "<input "+atributosStr+" id='endereco"+this.n+"' onkeyup='mostrarSugestaoDeRuas( event, this, "+this.n+" );' onkeydown='move( event, "+this.n+" );' onBlur='ocultarSugestaoDeRuas( "+this.n+" );' autocomplete='off' />";
	};
	this.toString = function( id ) {
		$( id ).innerHTML = this.getString();
		HagahMapas.campos[this.n] = { campo: $("endereco"+this.n), numeroNoMesmoCampo: this.numeroNoMesmoCampo, proximoCampo: this.proximoCampo, uf: this.uf, cidade: this.cidade };
	}
}
//
HagahMapas.CampoEndereco.Sugestao = function( n ) {
	this.n = n;
	this.atributos;
	//
	this.setClasse = function( classe ) {
		this.classe = classe;
	}
	this.setAtributos = function( atributos ) {
		this.atributos = atributos;
	}
	//
	this.getString = function() {
		var atributosStr = HagahMapas.atributosToString( this.atributos );
		return "<div "+atributosStr+" class='"+neverNull( this.classe )+"' id='enderecos"+this.n+"'></div>";
	};
	this.toString = function( id ) {
		$( id ).innerHTML = this.getString();
	}
}
//

