function swapToDiv(msg){
    if(document.getElementById("div_usuario"))
        document.getElementById("div_usuario").style.display = "none";
	
    if(document.getElementById("div_dj"))
        document.getElementById("div_dj").style.display = "none";

    if(document.getElementById("div_casa_noturna"))
        document.getElementById("div_casa_noturna").style.display = "none";

    var div = document.getElementById("div_message")
    div.style.display = "";
    div.innerHTML = msg+"<br/>&nbsp;";
    window.location.hash = "topo";
}
function resetCity(idcampo,divloding){
    if (divloding != '') {
        var span = document.getElementById(divloding);
        if(span)
            span.innerHTML = "&nbsp;&nbsp;&nbsp;Carregando...";
    }
    var s = document.getElementById(idcampo);
	
    for(x=s.length-1;x>=0;x--){
        s.remove(x);
    }
	
    var option = document.createElement('option');
    option.value = "";
    option.text = "Cidade";
	
    try {//outros navegadores
        s.add(option,null);
    } catch(ex) {//para IE
        s.add(option);
    }
	
    option = document.createElement('option');
    option.value = "";
    option.text = "------";
	
    try {//outros navegadores
        s.add(option,null);
    } catch(ex) {//para IE
        s.add(option);
    }
}
function loadCidade(id_estado,tmp_eval,idcampo,divloding){
    resetCity(idcampo,divloding);
    if(id_estado != ""){
        var a = new Ajax();
        a.onLoad = function(){
            var s = document.getElementById(idcampo);
            var arr = this.html.split("###");//separa por registro
            var arraux = new Array();
            for(x=0;x<arr.length;x++){
                arraux[x] = arr[x].split("...");
                var option = document.createElement('option');
                option.value = arraux[x][0];
                option.text = arraux[x][1];
				
                try {//outros navegadores
                    s.add(option,null);
                } catch(ex) {//para IE
                    s.add(option);
                }
            }
            if(tmp_eval){
                eval(tmp_eval);
            }
            if (divloding != '') {
                var span = document.getElementById(divloding);
                span.innerHTML = "";
            }
        }
        a.get('site/inc/routines/routines.php?routine=load_cidade&id_estado='+id_estado);
    }
}
function swapForm(tipo){
    document.getElementById("div_message").style.display = "none";
    switch(tipo) {
        case 1:
            document.getElementById("div_dj").style.display = "none";
            document.getElementById("div_casa_noturna").style.display = "none";
            document.getElementById("div_baresepub").style.display = "none";
            document.getElementById("div_usuario").style.display = "";
				
            break;
			
        case 2:
            document.getElementById("div_usuario").style.display = "none";
            document.getElementById("div_casa_noturna").style.display = "none";
            document.getElementById("div_baresepub").style.display = "none";
            document.getElementById("div_dj").style.display = "";
            break;
			
        case 3:
            document.getElementById("div_usuario").style.display = "none";
            document.getElementById("div_dj").style.display = "none";
            document.getElementById("div_baresepub").style.display = "none";
            document.getElementById("div_casa_noturna").style.display = "";
            break;	
			
        case 4:
            document.getElementById("div_usuario").style.display = "none";
            document.getElementById("div_dj").style.display = "none";
            document.getElementById("div_casa_noturna").style.display = "none";
            document.getElementById("div_baresepub").style.display = "";
            break;
    }
}
function swapAgencia(show){
    if(show){
        document.getElementById("div_agencia").style.display = "";
        document.getElementById("Seleciona a Agência_CMB0").selectedIndex = 0;
        document.getElementById("div_outraagencia").style.display = "none";
        document.getElementById("tr_bookings").style.display = "none";
    } else {
        document.getElementById("div_agencia").style.display = "none";
        document.getElementById("div_outraagencia").style.display = "none";
        document.getElementById("tr_bookings").style.display = "";
    }
}
function selectAgencia(id_agencia){
    if((id_agencia+"_") == "_"){
        document.getElementById("div_outraagencia").style.display = "none";
    } else if((id_agencia+"_") == "0_"){
        document.getElementById("div_outraagencia").style.display = "";
    } else {
        document.getElementById("div_outraagencia").style.display = "none";
    }
}
function enviarDj(){
    var nome = document.getElementById("Nome Artístico ( Sem DJ)_TXT1");
	
    if((nome.value != "Nome Artístico ( Sem DJ)") && (nome.value != "")){
        //testa genero
        if(!testaGenero()){
            alert("Selecione ao menos um gênero.");
            return false;
        }
    }
    var rep = representandoporagencia();
    if(rep != -1){
        if(rep == 1){//tem agencia
            var idagencia = document.getElementById("Seleciona a Agência_CMB0");
            if(idagencia.options[idagencia.selectedIndex].value == ""){
                alert("Selecione uma agência.");
                idagencia.focus();
                return false;
            } else if (idagencia.options[idagencia.selectedIndex].value == 0) {
                var nomea = document.getElementById("Nome da agência_TXT0");
                if((nomea.value == "Nome da agência") || (nomea.value == "")){
                    alert("Preencha o nome da sua agência.");
                    nomea.focus();
                    return false;
                }
				
                var tela = document.getElementById("Telefone da agência_TEL0");
                if((tela.value == "Telefone da agência") || (tela.value == "")){
                    alert("Preencha um telefone de contato da sua agência.");
                    tela.focus();
                    return false;
                }
            }
        } else {
            var tel = document.getElementById("Tefone de contato para bookings_TEL0");
            if((tel.value == "Tefone de contato para bookings") || (tel.value == "")){
                alert("Preencha um telefone de contato para bookings.");
                tel.focus();
                return false;
            }
			
            var mail = document.getElementById("E-mail de contato para bookings_EML0");
            if((mail.value == "E-mail de contato para bookings") || (mail.value == "")){
                alert("Preencha um E-mail de contato para bookings.");
                mail.focus();
                return false;
            }
        }
    }
    f.send('frm_dj');
}
function representandoporagencia(){
    var raidios = document.getElementsByName("representadoporagencia");
    for(var x=0;x<raidios.length;x++){
        if(raidios[x].checked){
            //alert("roçou:"+x);
            //alert(raidios[x].value);
            return raidios[x].value;
        } else {
    //alert("não roçou:"+x);
    }
    }
    return -1;
}
function testaGenero(){
    var total = document.getElementById("totalgenero");
    var selecionado = false;
	
    for(var x=0;x<total.value;x++){
        genero = document.getElementById("Generos"+x);
        if(genero){
            if(genero.checked){
                selecionado = true;
                break;
            }
        }
    }
    return selecionado;
}

jQuery(document).ready(function(){
    jQuery('#tdDjInputName input').change(function(){
        if(jQuery(this).val() == jQuery(this).attr('id') || jQuery(this).val() == '') {
            jQuery('#djFoundText').text('');
            return;
        }
		
        // console.log(jQuery(this).val());
        jQuery.get('site/inc/routines/routines.php?routine=check_dj', {
            name: jQuery(this).val()
            }, function(data) {
            jQuery('#djFoundText').removeClass();
            if(!data || data == '0') {
                jQuery('#djFoundText').addClass('green').html('O nome de DJ ainda não está cadastrado.');
                jQuery('#inputEnviarDj').removeAttr('disabled');				
            } else {
                jQuery('#djFoundText').html('<div class="red">O nome de DJ já está cadastrado.</div>' + 
                    '<div><a href="#" class="solicitar">Solicitar</a> dados para login.</div>');
                jQuery('#inputEnviarDj').attr('disabled', 'disabled');
            }
        });
    });
	
    jQuery('#djFoundText .solicitar').live('click', function(event) {
        event.preventDefault();
		
        var txt = 'Por favor, preencha seus dados. As informações de sua conta serão enviadas para seu email após aprovação do administrador:<br />' +
        '<form id="djDataSolicitar" action="site/inc/routines/routines.php?routine=solicitar_data_dj" method="post">' +
        '<div id="formPreencheImpromptu">' +
        '<label for="nomeImpromptu">Nome: </label><input id="nomeImpromptu_TXT1" type="text" name="nome" value="" /><br/>' + 
        '<label for="emailImpromptu">E-mail: </label><input id="emailImpromptu_TXT1" type="text" name="email" value="" /><br/>' + 
        '<label for="telefoneImpromptu">Telefone: </label><input id="telefoneImpromptu_TEL1" type="text" name="telefone" value="" />' +
        '</div></form>';

        jQuery.prompt(txt,{
            loaded: function(e) {
                f.maskFields(document.getElementById('djDataSolicitar'));
            },
            callback: function(v,m,f) {
                if(v == 'cancelar') {
                    return;
                }
                var $form = m.find('form#djDataSolicitar');
                jQuery.get('site/inc/routines/routines.php', 
                {
                    routine: 'solicitar_data_dj', 
                    name: $form.find('[name="nome"]').val(),
                    email: $form.find('[name="email"]').val(),
                    telefone: $form.find('[name="telefone"]').val()
                }, 
                function(data) {
                    jQuery.prompt(data, {
                        persistent: false
                    });
                }
                );
            },
            buttons: {
                Enviar: 'enviar', 
                Cancelar: 'cancelar'
            },
            persistent: false
        });
		
    });
});
