// Roshan's Ajax dropdown code with php
// This notice must stay intact for legal use
// Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
// If you have any problem contact me at http://roshanbh.com.np

function getXMLHTTP() { //fuction to return the xml http object
	var xmlhttp=false;	
	try{
		xmlhttp=new XMLHttpRequest();
	}
	catch(e) {		
		try {			
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e) {
			try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e1) {
				xmlhttp=false;
			}
		}
	}
		
	return xmlhttp;
}

function rodaAjax(div,strURL) {
	var req = getXMLHTTP();
	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 1) { // aqui é  linha do preloader
				document.getElementById(div).innerHTML = '<div style="margin:auto; text-align:center;"><img src="img/carregando.gif"></div>';
			} else if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {						
					// coloca o valor no objeto requisitado
					texto=unescape(req.responseText.replace(/\+/g," "));
					
					document.getElementById(div).innerHTML = req.responseText;						
					
					// executa scripts
					extraiScript(texto);
				} else {
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		}			
		req.open("GET", strURL, true);
		req.send(null);
	}
}

function getCidades(sigla,url) {
	if(url != '') {
		var strURL=url+"/ajaxCidades.php?sigla="+sigla;
	} else {
		var strURL="ajaxCidades.php?sigla="+sigla;
	}
	rodaAjax('cidadeDiv',strURL);
}

function getFormularios(idTipo) {
	var strURL="ajaxFormularios.php?idTipo="+idTipo;
	rodaAjax('formDiv',strURL);
}
	
function extraiScript(texto){
    // inicializa o inicio ><
    var ini = 0;
    // loop enquanto achar um script
    while (ini!=-1){
        // procura uma tag de script
        ini = texto.indexOf('<script', ini);
        // se encontrar
        if (ini >=0){
            // define o inicio para depois do fechamento dessa tag
            ini = texto.indexOf('>', ini) + 1;
            // procura o final do script
            var fim = texto.indexOf('</script>', ini);
            // extrai apenas o script
            codigo = texto.substring(ini,fim);
            // executa o script
            eval(codigo);
        }
    }
}
