/***************************************************************************************
 *
 *    WEBGeo - Sistema de Informação Geográfica do Município de Guarulhos
 *
 ***************************************************************************************
 *
 *    Copyright (C) 2005, Divisão Técnica de Geoprocessamento - Prefeitura de Guarulhos
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    e-mail: marcelovieira@guarulhos.sp.gov.br
 *
 ***************************************************************************************/

/**********************************************************************

/******************************************************************************
 * kaSearch - 
 *
 * To use kaSearch:
 * 
 * 1) add a script tag to your page:
 * 
 *   <script type="text/javascript" src="tools/kaSearch.js"></script>
 * 
 * 2) create a new instance of kaSearch
 * 
 *   var toolTip = new kaSearch( oMap);
 * 
 *
 *****************************************************************************/
 
function kaSearch( oKaMap ){
    this.kaMap = oKaMap;
    this.image = null;
    this.domObj = null;
    this.tooltip = new kaToolTip(oKaMap);
    this.init();
};
 

kaSearch.prototype.init = function(){
  this.tooltip.setTipImage('images/tip-yellow.png',-6,-19);
};


kaSearch.prototype.search=function(search_query,search_query1,search_query2,search_query3,chv){ 

	if (search_query.length > 0 || search_query1.length > 0 || search_query2.length > 0  || search_query3.length > 0) {
		var agt = navigator.userAgent.toLowerCase();
		var is_ie = (agt.indexOf('msie') != -1);
		var is_ie5 = (agt.indexOf('msie 5') != -1);
       
		element = document.getElementById ('searchres');
		element.innerHTML = "<p><font class=\"titulo\">Localizando objeto, aguarde...</p></font><hr>";
		element.className = "visible";

		function handle_do_search () {
			if (xmlhttp.readyState == 4) { //request completed 
				if (xmlhttp.status == 200) { //request successful
					element.innerHTML = xmlhttp.responseText;
				} else {
					alert ('Ocorreu erro com o servidor!');
					}
				}
			}
			var xmlhttp = null;
			if (is_ie) {
				var control = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
				try {
					xmlhttp = new ActiveXObject(control);
					xmlhttp.onreadystatechange = handle_do_search;
				} catch(e) {
					alert("Controles de JavaScript e ActiveX de seu navegador estão desativados!");
					}
			} else {
				xmlhttp = new XMLHttpRequest();
				xmlhttp.onload = handle_do_search;
				xmlhttp.onerror = handle_do_search;
				}

			//call for xsearch.php results - sending link
			// CODIFICA AS INFORMAÇÕES PARA MANDAR PELO LINK
			if (search_query.length > 0) {
				rua = encodeURIComponent(search_query);
				} else {
					rua = 'Empty';
					}
			if (search_query1.length > 0) {
				numantigo = encodeURIComponent(search_query1);
				} else {
					numantigo = 'Empty';				
					}
			if (search_query2.length > 0) {
				numnovo = encodeURIComponent(search_query2);
				} else {
					numnovo = 'Empty';
					}
			if (search_query3.length > 0) {
				cep = encodeURIComponent(search_query3);
				} else {
					cep = 'Empty';
					}
			//alert(search_query);alert(search_query1);alert(search_query2);
			xmlhttp.open('GET', "tools/search/kaSearch.php?xmlRequest=true&map="+this.kaMap.getCurrentMap().name+
						"&rua="+rua+"&numantigo="+numantigo+"&numnovo="+numnovo+"&cep="+cep+"&chv="+chv, true);
			xmlhttp.send(null);
		}
};


kaSearch.prototype.searchObj=function( tipo , nome ){ 

	if (tipo.length > 0 && nome.length > 0 ) {

		var agt = navigator.userAgent.toLowerCase();
		var is_ie = (agt.indexOf('msie') != -1);
		var is_ie5 = (agt.indexOf('msie 5') != -1);
       
		element = document.getElementById ('searchresObj');
		element.innerHTML = "<p><font class=\"titulo\">Localizando objeto, aguarde...</p></font><hr>";
		element.className = "visible";

		function handle_do_search () {
			if (xmlhttp.readyState == 4) { //request completed 
				if (xmlhttp.status == 200) { //request successful
					element.innerHTML = xmlhttp.responseText;
				} else {
					alert ('Ocorreu erro com o servidor!');
					}
				}
			}
			var xmlhttp = null;
			if (is_ie) {
				var control = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
				try {
					xmlhttp = new ActiveXObject(control);
					xmlhttp.onreadystatechange = handle_do_search;
				} catch(e) {
					alert("Controles de JavaScript e ActiveX de seu navegador estão desativados!");
					}
			} else {
				xmlhttp = new XMLHttpRequest();
				xmlhttp.onload = handle_do_search;
				xmlhttp.onerror = handle_do_search;
				}

			xmlhttp.open('GET', "tools/search/kaSearchObj.php?xmlRequest=true&map="+this.kaMap.getCurrentMap().name+
						"&tipo="+tipo+"&nome="+nome, true);
			xmlhttp.send(null);
		}
};

kaSearch.prototype.searchLO=function( empresa , nome ){ 

	if (empresa.length > 0 && nome.length > 0 ) {

		var agt = navigator.userAgent.toLowerCase();
		var is_ie = (agt.indexOf('msie') != -1);
		var is_ie5 = (agt.indexOf('msie 5') != -1);
       
		element = document.getElementById ('searchresLO');
		element.innerHTML = "<p><font class=\"titulo\">Localizando linha, aguarde...</p></font><hr>";
		element.className = "visible";

		function handle_do_search () {
			if (xmlhttp.readyState == 4) { //request completed 
				if (xmlhttp.status == 200) { //request successful
					element.innerHTML = xmlhttp.responseText;
				} else {
					alert ('Ocorreu erro com o servidor!');
					}
				}
			}
			var xmlhttp = null;
			if (is_ie) {
				var control = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
				try {
					xmlhttp = new ActiveXObject(control);
					xmlhttp.onreadystatechange = handle_do_search;
				} catch(e) {
					alert("Controles de JavaScript e ActiveX de seu navegador estão desativados!");
					}
			} else {
				xmlhttp = new XMLHttpRequest();
				xmlhttp.onload = handle_do_search;
				xmlhttp.onerror = handle_do_search;
				}

			xmlhttp.open('GET', "tools/search/kaSearchLO.php?xmlRequest=true&map="+this.kaMap.getCurrentMap().name+
						"&empresa="+empresa+"&nome="+nome, true);
			xmlhttp.send(null);
		}
};

kaSearch.prototype.searchMT=function( tipo, campo, numeroClasse ){ 

	if (tipo.length > 0 && numeroClasse.length > 0 ) {

		var agt = navigator.userAgent.toLowerCase();
		var is_ie = (agt.indexOf('msie') != -1);
		var is_ie5 = (agt.indexOf('msie 5') != -1);
       
		element = document.getElementById ('searchresMT');
		element.innerHTML = "<p><font class=\"titulo\">Concluindo, aguarde...</p></font><hr>";
		element.className = "visible";

		function handle_do_search () {
			if (xmlhttp.readyState == 4) { //request completed 
				if (xmlhttp.status == 200) { //request successful
					element.innerHTML = xmlhttp.responseText;
				} else {
					alert ('Ocorreu erro com o servidor!');
					}
				}
			}
			var xmlhttp = null;
			if (is_ie) {
				var control = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
				try {
					xmlhttp = new ActiveXObject(control);
					xmlhttp.onreadystatechange = handle_do_search;
				} catch(e) {
					alert("Controles de JavaScript e ActiveX de seu navegador estão desativados!");
					}
			} else {
				xmlhttp = new XMLHttpRequest();
				xmlhttp.onload = handle_do_search;
				xmlhttp.onerror = handle_do_search;
				}

			xmlhttp.open('GET', "tools/search/kaSearchMT.php?xmlRequest=true&map="+this.kaMap.getCurrentMap().name+
						"&tipo="+tipo+"&campo="+campo+"&numeroClasse="+numeroClasse+"&xy="+this.kaMap.getGeoExtents(), true);
			xmlhttp.send(null);
		}
};

kaSearch.prototype.searchMTCampo=function( tipo ){ 

	if (tipo.length > 0) {

		var agt    = navigator.userAgent.toLowerCase();
		var is_ie  = (agt.indexOf('msie') != -1);
		var is_ie5 = (agt.indexOf('msie 5') != -1);
       
		element           = document.getElementById ('searchMTcampo');
		element.innerHTML = "<font class=\"titulo\">Carregando, aguarde...</font>";
		element.className = "visible";

		function handle_do_search () {
			if (xmlhttp.readyState == 4) { //request completed 
				if (xmlhttp.status == 200) { //request successful
					element.innerHTML = xmlhttp.responseText;
				} else { alert ('Ocorreu erro com o servidor!'); }
			}
		}

		var xmlhttp = null;
		if (is_ie) {
			var control = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
			try {
				xmlhttp = new ActiveXObject(control);
				xmlhttp.onreadystatechange = handle_do_search;
			} catch(e) { alert("Controles de JavaScript e ActiveX de seu navegador estão desativados!"); }
		} else {
			xmlhttp         = new XMLHttpRequest();
			xmlhttp.onload  = handle_do_search;
			xmlhttp.onerror = handle_do_search;
			}

		xmlhttp.open('GET', "tools/search/kaSearchMT.php?xmlRequest=true&tipo="+tipo+"&listarCampo=1", true);
		xmlhttp.send(null);

	}
};

kaSearch.prototype.searchBrr=function( nome ){ 

	if (nome.length > 0 ) {

		var agt = navigator.userAgent.toLowerCase();
		var is_ie = (agt.indexOf('msie') != -1);
		var is_ie5 = (agt.indexOf('msie 5') != -1);
       
		element = document.getElementById ('searchresBairro');
		element.innerHTML = "<p><font class=\"titulo\">Localizando bairro, aguarde...</p></font><hr>";
		element.className = "visible";

		function handle_do_search () {
			if (xmlhttp.readyState == 4) { //request completed 
				if (xmlhttp.status == 200) { //request successful
					element.innerHTML = xmlhttp.responseText;
				} else {
					alert ('Ocorreu erro com o servidor!');
					}
				}
			}
			var xmlhttp = null;
			if (is_ie) {
				var control = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
				try {
					xmlhttp = new ActiveXObject(control);
					xmlhttp.onreadystatechange = handle_do_search;
				} catch(e) {
					alert("Controles de JavaScript e ActiveX de seu navegador estão desativados!");
					}
			} else {
				xmlhttp = new XMLHttpRequest();
				xmlhttp.onload = handle_do_search;
				xmlhttp.onerror = handle_do_search;
				}

			xmlhttp.open('GET', "tools/search/kaSearchBairro.php?xmlRequest=true&map="+this.kaMap.getCurrentMap().name+"&nome="+nome, true);
			xmlhttp.send(null);
		}
};
