/**
 * Esse arquivo eh pra colocar funcoes JS do ICOX
 */


function ifListaValueExtra(alta, baixa, legenda) {
    var arHigh = document.getElementsByName(alta);
    var arLow = document.getElementsByName(baixa);
    var arLeg = document.getElementsByName(legenda);
    var t = arHigh.length;
    var ret = false;
    
    for(i = 0; i < t; i ++) { 
        if (arHigh[i].value != '' && arLow[i].value != '' && arLeg[i].value != '') {
            return true;
        }
    }
    
    return ret;
}

function ifListaChecked(nome) {
    var ar = document.getElementsByName(nome);
    var t = ar.length;
    var ret = false;
    
    for(i = 0; i < t; i ++) { 
        if (ar[i].checked == true) {
            return true;
        }
    }
    
    return ret;
}


function checar(id, bool) {
    document.getElementById(id).checked = bool;
}


function checarLista(nome) {
    var ar = document.getElementsByName(nome);
    var t = ar.length;
    var chk = false;
    
    for(i = 0; i < t; i ++) { if(ar[i].checked == false) { chk = true; } }
    
    for(i = 0; i < t; i ++) { ar[i].checked = chk; }
}


function checarListaBool(nome, bool) {
    var ar = document.getElementsByName(nome);
    var t = ar.length;
    
    for(i = 0; i < t; i ++) { ar[i].checked = bool; }
}

function checarListaLida(nome, ler) {
    var ar = document.getElementsByName(nome);
    var t = ar.length;
    
    for(i = 0; i < t; i ++) { ar[i].checked = false; }
    
    for(i = 0; i < t; i ++) { 
    	if(ar[i].title == ler) {
    		ar[i].checked = true; 
    	}
    }
}

function deletarSelecionados(nome, us, pg) {
    var ar = document.getElementsByName(nome);
    var t = ar.length;
    var ax = '';
    var ir = '';
    var ct = 0;
    
    for(i = 0; i < t; i ++) { 
        if (ar[i].checked == true) {
            ax += '&item['+i+']='+ar[i].value;
            ct ++; 
        }
    }    
    ir = 'icox.php?mdl=pagina&op=apagarrecado&lista=deletar&usuario='+us+'&pagina='+pg+ax;
    if (ct > 0) { window.location = ir; }
}




function trocaClass(c1, c2, id) {
    var ob = document.getElementById(id);
    
    if(ob.className == c1) {
        ob.className = c2;
    } else {
        ob.className = c1;
    }
}


function nomeClass(classe, id) {
   document.getElementById(id).className = classe;
}


function setaValue(ob, id) {
    if (ob.value == "") { document.getElementById(id).value = ""; }
    else { document.getElementById(id).value = ob.value+" \n\n"; }
}


function setaHtml(id, va) {
    document.getElementById(id).innerHTML = va;
}


function trocaValue(v1, v2, id) {    
    var ob = document.getElementById(id);
    
    if(ob.value == v1) {
        ob.value = v2;
    } else {
        ob.value = v1;
    }
}


function idValue(id, v) {    
    document.getElementById(id).value = v;
}

function idDisable(id, bool) {    
    document.getElementById(id).disabled = bool;
}

function popupMisIf(url, h, w, iOb) {
    if (document.getElementById(iOb).value != '') {
        popupMis(url, h, w);
        
    } else {
        document.getElementById(iOb).focus();
        alert('Preencha o campo para a busca.');
    }
}

function popupMis(url, h, w) {
	window.open(url, 'popup_mis', 'height='+ h +',width='+ w +',toolbar=0,menubar=0,scrollbars=1,resizable=0,location=0,directories=0,status=0');
	return false;
}

function sf() {	
	contador_caracteres('texto_conteudo', 'contador', 255);
      document.getElementById('texto_conteudo').focus();  
}

function contador_caracteres(opt_texto_conteudo, opt_contador, opt_maxSize) {
  var texto_conteudo = opt_texto_conteudo ?
    opt_texto_conteudo : "texto_conteudo";
  var contador = opt_contador ? opt_contador : "contador";
  var maxSize = opt_maxSize ? opt_maxSize : 255;
    
  var field = document.getElementById(texto_conteudo);
  if (field && field.value.length >= maxSize) {
    field.value = field.value.substring(0, maxSize);
  }
  var txtField = document.getElementById(contador);
  if (txtField) {  
    txtField.innerHTML = field.value.length;
  }
}

function qual_serie(v, id) {
    if (v == 0) {
        nomeClass('', id);
    } else {
        nomeClass('oculta', id);
    }
}

function check_espaco(v, id) {
    if (v == true) {
        nomeClass('', id);
    } else {
        nomeClass('oculta', id);
    }
}

function selecionarEvento() {
    window.opener.document.getElementById('').value='';
}

function fnPublicar() {
    if (podePublicarMis(1)) {        
        document.getElementById('id_ativo_00').value = 1;
        document.getElementById('form_event_id').submit();
    }
}

function fnRascunho() {
    if (podePublicarMis(0)) {        
        document.getElementById('id_ativo_00').value = 0;
        document.getElementById('form_event_id').submit();
    }
}

function g2Mis(id) {
    return document.getElementById(id);
}


function podePublicarMis(op) {
    txtErro = '';
    erro = false;
    foco = '';
    
    // op == 1, obrigatorio soh no publicar
    
    // titulo
    if (g2Mis('event_title').value == '') {
        txtErro += 'Preencha o campo Título.'+"\n";
        erro = true;
        if (foco == '') { foco = 'event_title'; }
    }
    
    // serie selecionada
    if (g2Mis('event_series1').checked == false && g2Mis('event_series0').checked == false) {
        txtErro += 'O evento faz parte de uma série?'+"\n";
        erro = true;
        if (foco == '') { foco = 'event_series1'; }
    }
    
    // serie
    if (g2Mis('event_series1').checked == true) {
        if (g2Mis('event_wich_serie').value == 0 && g2Mis('event_new_serie').value == '') {
            txtErro += 'Preencha o campo Série Nova.'+"\n";
            erro = true;
            if (foco == '') { foco = 'event_new_serie'; }
        }
    }
    
    // programacao selecionada
    if (g2Mis('event_programacao_sim').checked == false && g2Mis('event_programacao_nao').checked == false) {
        txtErro += 'Evento com programação?'+"\n";
        erro = true;
        if (foco == '') { foco = 'event_programacao_sim'; }
    }
    
    // programacao
    if (g2Mis('event_programacao_sim').checked == true) {
        if (g2Mis('event_detail').checked == true && g2Mis('id_event_principal').value == '0') {
            txtErro += 'Preencha o campo Programação.'+"\n";
            erro = true;
            if (foco == '') { foco = 'mean_54'; }
        }
    }
    
    // pai/mae
    if (! ifListaChecked('mae_filho')) {
        txtErro += 'Marque uma das opções mãe / filho.'+"\n";
        erro = true;
        if (foco == '') { foco = 'mae_filho_mis_1'; }
           
    } else if (g2Mis('mae_filho_mis_1').checked == true && g2Mis('id_event_pai').value == '0') {
        txtErro += 'Preencha o campo Evento mãe / filho.'+"\n";
        erro = true;
        if (foco == '') { foco = 'pai_mis_54'; }
    }
    
    if (! ifListaChecked('tipo_periodo')) {
        txtErro += 'Marque uma das opções do Período.'+"\n";
        erro = true;
        if (foco == '') { foco = 'tipo_periodo_1'; }
           
    }
    
    // tratar periodo */*/*/
    // aki
    // tratar periodo */*/*/
    
    // espaco 
   // if (op == 1 && ! ifListaChecked('espaco[]')) {
  /* if (g2Mis('select1').value == '') {
        txtErro += 'Marque pelo menos um Espaço.'+"\n";
        erro = true;
        if (foco == '') { foco = 'auditorio_pequeno'; }
        
    } else if (g2Mis('externo_outra_inst').checked == true || (g2Mis('fora_do_MIS') != undefined && g2Mis('fora_do_MIS').checked == true)) {
        if (g2Mis('externo_outra_inst').checked == true && g2Mis('em_outra_instituicao').value == '') {
            txtErro += 'Preencha o campo Em outra instituição.'+"\n";
            if (foco == '') { foco = 'em_outra_instituicao'; }
            erro = true;
        }
        
        if (g2Mis('fora_do_MIS').checked == true && g2Mis('endereco_fora_do_mis').value == '') {
            txtErro += 'Preencha o campo Endereço fora do MIS.'+"\n";
            if (foco == '') { foco = 'endereco_fora_do_mis'; }
            erro = true;
        }
    }  */
    
    // tipo do evento 
    if (g2Mis('select2').value == '') {
        txtErro += 'Marque pelo menos um Tipo de Evento.'+"\n";
        erro = true;
        if (foco == '') { foco = 'auditorio_pequeno'; }
        
    }  
    
    // publico_alvo
    //if (op == 1 && ! ifListaChecked('publico_alvo[]')) {
    if (g2Mis('select3').value == '') {
        txtErro += 'Marque pelo menos um Público Alvo.'+"\n";
        erro = true;
        if (foco == '') { foco = 'auditorio_pequeno'; }
    }
    
    // area
   // if (! ifListaChecked('area[]')) {
    if (g2Mis('select4').value == '') {
        txtErro += 'Marque pelo menos uma Área.'+"\n";
        erro = true;
        if (foco == '') { foco = 'auditorio_pequeno'; }
    } 
    
    // ingressosala_imprensa1 
    if (! ifListaChecked('ingresso_gratuito')) {
        txtErro += 'Marque uma das opções de Preço do ingresso.'+"\n";
        erro = true;
        if (foco == '') { foco = 'ingresso0'; }
        
    } else if (op == 1 && g2Mis('ingresso0').checked == true && g2Mis('ingresso_mis').value == '') {
        txtErro += 'Preencha o campo Preço do ingresso.'+"\n";
        erro = true;
        if (foco == '') { foco = 'ingresso_mis'; }
    }
    
    // Sumario
    /*if (op == 1 && g2Mis('texto_sumario').value == '') {
        txtErro += 'Preencha o campo Sumário.'+"\n";
        erro = true;
        if (foco == '') { foco = 'texto_sumario'; }
    }*/
    
    // sala imprensa  
    /*if (op == 1 && g2Mis('sala_imprensa1').checked == true) {
        if (g2Mis('doc_imprensa_1').value == '') {
            txtErro += 'Escolha o arquivo .doc.'+"\n";
            erro = true;
            if (foco == '') { foco = 'sala_imprensa1'; }
        }
        
        if (g2Mis('com_imagem1').checked == true) {
            if (! ifListaValueExtra('img_alta[]', 'img_baixa[]', 'legenda_imprensa[]')) {
                txtErro += 'Preencha pelo menos uma imagem de alta e baixa resolução com legenda.'+"\n";
                erro = true;
                if (foco == '') { foco = 'sala_imprensa1'; }
            }
        }
    }*/
    
    
    
    
    // fim
    if (erro) {
        alert("Erro !!!\n\n"+txtErro);
        if (g2Mis(foco) != undefined) { g2Mis(foco).focus(); }
        return false;
        
    } else {
        return true;
    }
}


function trocaImagemLegenda(idNova) {
	
	// a inserir "nova, thumb"
    var imagemNova = document.getElementById('imagem'+idNova).value;
    var thumbNova = document.getElementById('thumb'+idNova).src;
    var legendaNova = document.getElementById('legenda'+idNova).value;
    
    // a antiga "principal"
    var imagemPrincipal = document.getElementById('imagem_principal').src;
    var thumbPrincipal = document.getElementById('thumb_principal').value;
    var legendaPrincipal = document.getElementById('legenda').innerHTML;

    
    // troca tudo da nova
    document.getElementById('imagem_principal').src = imagemNova;
    document.getElementById('imagem_principal').title = legendaNova;
    document.getElementById('thumb_principal').value = thumbNova;
    document.getElementById('legenda_principal').value = legendaNova;
    document.getElementById('legenda').innerHTML = legendaNova;
    
    // troca tudo da antiga
    document.getElementById('imagem'+idNova).value = imagemPrincipal;
    document.getElementById('thumb'+idNova).src = thumbPrincipal;
    document.getElementById('thumb'+idNova).title = legendaPrincipal;
    document.getElementById('legenda'+idNova).value = legendaPrincipal;
    
    
    //alert(legendaPrincipal);
    //alert(imagemPrincipal);
    
}


function trocaImagemLegenda2(idNova) {
	
	// a inserir "nova, thumb"
    var imagemNova = document.getElementById('imagem'+idNova).value;
    var thumbNova = document.getElementById('thumb'+idNova).src;
    var legendaNova = document.getElementById('legenda'+idNova).value;
    
    // a antiga "principal"
    var imagemPrincipal = document.getElementById('imagem_principal').style.backgroundImage;
    var thumbPrincipal = document.getElementById('thumb_principal').value;
    var legendaPrincipal = document.getElementById('legenda').innerHTML;
    
    // troca tudo da nova
    document.getElementById('imagem_principal').style.backgroundImage = imagemNova;
    document.getElementById('imagem_principal').title = legendaNova;
    document.getElementById('thumb_principal').value = thumbNova;
    document.getElementById('legenda_principal').value = legendaNova;
    document.getElementById('legenda').innerHTML = legendaNova;
    
    // troca tudo da antiga
    document.getElementById('imagem'+idNova).value = imagemPrincipal;
    document.getElementById('thumb'+idNova).src = thumbPrincipal;
    document.getElementById('thumb'+idNova).title = legendaPrincipal;
    document.getElementById('legenda'+idNova).value = legendaPrincipal;
	
	/*document.getElementById('imagem_principal').style.backgroundImage = 'url("imagemPrincipal")';
	document.getElementById('imagem_principal').style.backgroundRepeat= 'no-repeat';*/

	
	//alert(legendaPrincipal);
	//alert(imagemPrincipal);
	
}

function selectAll(theField) {
	  var tempval=eval("document."+theField)
	  tempval.focus()
	  tempval.select()
	}

