function outraProfissao(){
	var obj1 = document.getElementById("outraProfissao");
	var obj2 = document.getElementsByName("dados_profissoes_id")[0];
	if(obj1)
		obj1.style.display = ((obj2.selectedIndex + 1) == obj2.options.length ? '' : 'none');
}
outraProfissao();



function outraInstituicao(){
        var obj1 = document.getElementById("outraInstituicao");
        var obj2 = document.getElementsByName("dados_instituicao_id")[0];
        var obj3 = document.getElementsByName("dados_estado_instituicao")[0];
        if(obj1){
	        if(obj2.value == '9999' && obj3.value == "")
    	            obj2.options[0].selected = true;
        	//obj1.style.display = ((obj2.selectedIndex + 1) == obj2.options.length ? '' : 'none');
        	obj1.style.display = (obj2.value == '9999' ? '':'none');
        }
 
 
}
outraInstituicao();

function verificaEmail()
{
var campo1='',campo2='';

	campo1= document.getElementsByName('dados_repita_email')[0].value;
	campo2= document.getElementsByName('dados_email')[0].value;
	if(campo1 != campo2)
	{
		
		alert("você digitou um email diferente em Repita Email!");
		document.getElementsByName('dados_repita_email')[0].value = '';
		document.getElementsByName('dados_repita_email')[0].focus();
	
		
	}
	
}

function mostraCampos(valor){
	var cpf = document.getElementById('cpf');
	var cnpj2 = document.getElementById('cnpj2');
	var doc = document.getElementById('documento');

	var label_cpf = document.getElementById('label_cpf');
	var label_cnpj2 = document.getElementById('label_cnpj2');
	var label_doc = document.getElementById('label_documento');
	
	
	if (valor == 'F')
	{
		cnpj2.style.display='none';
		label_cnpj2.style.display='none';
		Obrigatorios.removeObrigatorio('dados_CNPJ2');
		cnpj2.value='';
		
		doc.style.display='none';
		label_doc.style.display='none';
		Obrigatorios.removeObrigatorio('dados_DOCUMENTO');
		doc.value='';
		
		cpf.style.display='';
		label_cpf.style.display='';
		Obrigatorios.adicionaObrigatorio('dados_CPF',"CPF");
	}else{
	
		if(valor == 'J')
		{
			cpf.style.display='none';
			label_cpf.style.display='none';
			cpf.value='';
			Obrigatorios.removeObrigatorio('dados_CPF');
			
			doc.style.display='none';
			label_doc.style.display='none';
			Obrigatorios.removeObrigatorio('dados_DOCUMENTO');
			doc.value='';
			
			cnpj2.style.display='';
			label_cnpj2.style.display='';
			Obrigatorios.adicionaObrigatorio('dados_CNPJ2',"CNPJ");
		}
		else
		{
			cpf.style.display='none';
			label_cpf.style.display='none';
			cpf.value='';
			Obrigatorios.removeObrigatorio('dados_CPF');
			
			cnpj2.style.display='none';
			label_cnpj2.style.display='none';
			Obrigatorios.removeObrigatorio('dados_CNPJ2');
			cnpj2.value='';
			
			doc.style.display='';
			label_doc.style.display='';
			Obrigatorios.adicionaObrigatorio('dados_DOCUMENTO',"DOCUMENTO");
		}
	}	
}

/////////////////////

function HTTPClient() {};

// Add methods and properties as array.
HTTPClient.prototype = {
    url: null,

    // Instance of XMLHttpRequest.
    request: null,

    // Used to make sure multiple calls are not placed with the same
    // client object while another in progress.
    callInProgress: false,

    // The user defined handler - see MyHandler below.
    userhandler: null,

    init: function(url)
    {
        this.url = url;

        
        try {
            // Mozilla, Safari.
           this.request = new XMLHttpRequest();
        } catch (e) {
            // IE.
            var MSXML_XMLHTTP_PROGIDS = new Array(
                "MSXML2.XMLHTTP.4.0",
                "MSXML2.XMLHTTP.3.0",
                "MSXML2.XMLHTTP",
                "Microsoft.XMLHTTP"
                );
            var success = false;
            for (var i = 0; i < MSXML_XMLHTTP_PROGIDS.length && !success; i++) {
                try {
                    this.request = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]);
                    success = true;
		    
                } catch (e) {}
            }
            if (!success) {
	    	
		document.forms['form_cadastro'].submit();
		throw "Unable to create XMLHttpRequest.";
		
            }
        }
    },

    // Handler argument is a user defined object to be called.
    asyncGET: function(handler)
    {
        // Degrade or some such.
        if (!this.request) {
            return false;
        };

        // Prevent multiple calls
        if (this.callInProgress) {
            throw "Call in progress";
        };

        this.callInProgress = true;

        this.userhandler = handler;

        // Open an async request - third argument makes it
        // asynchronous.
        this.request.open('GET', this.url, true);

        // Have to assign "this" to a variable.
        var self = this;

        // Assign a closure to the onreadystatechange callback.
        this.request.onreadystatechange = function() {
            self.stateChangeCallback(self);
        }

        this.request.send(null);
    },

    stateChangeCallback: function(client)
    {
        switch (client.request.readyState) {
            // Request not yet made.
            case 1:
            try {
                client.userhandler.onInit();
            } catch (e) { /* Handler method not defined. */ }
            break;

            // Contact established with server but nothing downloaded
            // yet.
            case 2:
            try {
                status = client.request.status;
                // Check for HTTP status 200.
                if (status != 200) {
                    client.userhandler.onError(
                        status,
                        client.request.statusText
                        );

                    // Abort the request.
                    client.request.abort();

                    // Call no longer in progress.
                    client.callInProgress = false;
                }
            } catch (e) {
                /* MSXMLHTTP 3.x+ doesn't populate status until
                 * readyState 4. */
            }
            break;

            // Called multiple times while download is in progress.
            case 3:
            // Notify user handler of download progress.
            try {
                // Get the total content length (useful to work
                // out how much has been downloaded).
                try {
                    var contentLength =
                        client.request.getResponseHeader("Content-Length");
                } catch (e) {
                    var contentLength = NaN;
                }

                // Call the progress handler with what we've got.
                client.userhandler.onProgress(
                    client.request.responseText,
                    contentLength
                    );

            } catch (e) { /* Handler method not defined. */ }
            break;

            // Download complete.
            case 4:
            try {
                client.userhandler.onLoad(client.request.responseText);
            } catch (e) {
                /* Handler method not defined. */
            } finally {
                // Call no longer in progress.
                client.callInProgress = false;
            }
            break;
        }
    }
}

var Select = function (obj){
	this.obj = obj;
	this.options = [];
	this.cont = 1;
	this.options[0] = new Option("", "", false, true);
	this.adiciona = function(val, text, sele){
		this.options[this.cont] = new Option(text, val, false, sele);
		this.cont++;
	};
	this.monta = function(emBranco){
		this.obj.options.length = this.options.length;
		for (var i=0;i<this.cont;i++)
			this.obj.options[i] = this.options[i];
		if (typeof emBranco == "undefined" || !emBranco || this.obj.name.indexOf("multiplos_select") != -1)
			this.obj.options[0] = null;
	};
	this.limpa = function(){
		this.obj.options.length = 0;
	};
	this.limpa();
};


var MenuUpdateHandler = {
    onLoad: function(result)
    {
        // Run the javascript we got back.
        eval(result);
    }
};

function update(ufies)
{
   
    var client = new HTTPClient();
    client.init("/scripts/preenche_ies.php?uf_ies="+ufies)

    try {
 	client.asyncGET(MenuUpdateHandler);      
    } catch (e) {
        // Don't do anything on errors; probably a transient failure,
        // and we'll try again after the next interval.
    }
}



