// JavaScript Document
// Clevertext textbox javascript handlers.
// Gary Hockin .. gary --at-- garyhockin --dot-- co --dot-- uk
// June 2006


function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}


//Our XmlHttpRequest object to get the auto suggest
var http = getHTTPObject();
//Starts the AJAX request.

//var http;


// Funcao generica para montar combos select
function monta_combo( option, tipo, id ) {
	var wurl;

	// option = nome do componente que esta solicitando a geracao do combo
	//
	// tipo = uf ==> monta combo de uf
	// tipo = cy ==> monta combo de cidade (city)
	//
	// id ==> valor do id que devera ser utilizado para o filtro da montagem do combo
	
	wurl = "../components/" + option + "/mod_ajax_" + tipo + ".php?id=" + id;

	http.open( "GET", wurl, true );
	if( tipo == "uf" ) {
		http.onreadystatechange = handleMontaComboUF;
	} else if( tipo == "cy" ) {
		http.onreadystatechange = handleMontaComboCY
	}
	http.send(null);
}

function handleMontaComboUF() {
	combo = document.forms[0].id_uf;
	if (http.readyState == 4) {
		if(http.status == 200) {
			document.getElementById("tr_combouf").style.visibility = "visible";
			combo.options.length = 0;
			combo.options[0] = new Option( '*** Todos os Estados ***', 0 );
			results = http.responseText.split(",");
			jj = 1;
    		for( i = 0; i < results.length - 1; i++ ) {
				string = results[i].split( "|" );
				combo.options[jj] = new Option( string[0], string[1] );
				jj = jj + 1;
			}
			combo.options[0].selected = true;
		}
	}
}


function handleMontaComboCY() {
	combo = document.forms[0].id_cidade;
	if (http.readyState == 4) {
		if(http.status == 200) {
			document.getElementById("tr_combocity").style.visibility = "visible";
			combo.options.length = 0;
			combo.options[0] = new Option( '*** Todas as Cidades ***', 0 );
			results = http.responseText.split(",");
			jj = 1;
    		for( i = 0; i < results.length - 1; i++ ) {
				string = results[i].split( "|" );
				combo.options[jj] = new Option( string[0], string[1] );
				jj = jj + 1;
			}
			combo.options[0].selected = true;
		}
	}
}

var wspan;

function showlojaout( id ) {
	wspan = "dadosloja" + id;
	document.getElementById( wspan ).innerHTML = "";
	document.getElementById( wspan ).style.visibility = "hidden";
}


function showloja( id, option ) {
	var wurl;
	
	wspan = "dadosloja" + id;
	
	wurl = "../components/" + option + "/mod_ajax_loja.php?id=" + id;

	http.open( "GET", wurl, true );
	http.onreadystatechange = handleShowLoja;
	http.send(null);
}


function handleShowLoja() {
	if (http.readyState == 4) {			// indica concluido
		if(http.status == 200) {
			document.getElementById( wspan ).style.visibility = "visible";
			document.getElementById( wspan ).innerHTML = http.responseText;
        } else {
            alert("Houve um problema ao obter os (ajax_class.js ==> handleShowLoja) dados:\n" + http.statusText);
        }
	}
}


function repEstados( idUf ){
	var wurl;
	
	wurl = "http://www.sbk.com.br/representantes/repEstados.php?idUf=" + idUf;

	http.open( "GET", wurl, true );
	http.onreadystatechange = handlerepEstados;
	http.send(null);

}

function handlerepEstados() {
	if (http.readyState == 4 ) {
		if(http.status == 200) {
			document.getElementById( "repEstadoDiv" ).style.visibility = "visible";
			document.getElementById( "repEstadoDiv" ).innerHTML = http.responseText;
		} else {
			alert("Houve um problema ao obeter os (ajax_class.js ==> handlerepEstados) dados:/n" + http.statusText);
		}
	}
}

