
function troca() {
    if( $('imagem').style.display == 'none' )
        $('imagem').style.display = 'block';
    else
        $('imagem').style.display = 'none';

    if( $('file').style.display == 'none' ){
        $('file').style.display = 'block';
        $('img').disabled = false;
    }
    else{
        $('file').style.display = 'none';
        $('img').disabled = true;
    }
}

var upload_number = 2;

function addFileInput() {

    var d = document.createElement("div");
    d.setAttribute("id", "file"+upload_number);

    var table = document.createElement("table");
    var tbody = document.createElement("tbody");
    var tr = document.createElement("tr");
    var td1 = document.createElement("td");
    var td2 = document.createElement("td");
    td2.setAttribute("valign", "top");


    var textarea = document.createElement("textarea");
    textarea.setAttribute("name", "descricao_img"+upload_number);
    textarea.setAttribute("id", "descricao_img"+upload_number);
    textarea.setAttribute("rows", "3");
    textarea.setAttribute("cols", "40");
    td1.appendChild(textarea);

    var file = document.createElement("input");
    file.setAttribute("type", "file");
    file.setAttribute("name", "img"+upload_number);
    file.setAttribute("id", "img"+upload_number);
    td2.appendChild(file);

    tr.appendChild(td1);
    tr.appendChild(td2);
    tbody.appendChild(tr);
    table.appendChild(tbody);
    d.appendChild(table);

    $("moreUploads").appendChild(table);
    upload_number++;

}

function del_img_proj(base, id) {

    var resp = confirm("Deseja realmente deletar esta imagem?");
    if (resp)
    {
        $('mostra_img'+id).style.display = 'none';
        var url = base + 'admin/index.php?c=projetos&m=deletar_imagem&id=' + id;
        var parametros = '';
        var myAjax = new Ajax.Request( url, { method: 'post', parameters: parametros, onComplete: delok} );
    }

}

function delok(){
    alert('Imagem deletada com sucesso');
}

function del_proj(base, id) {
    var resp = confirm("Deseja realmente deletar este projeto?");
    if (resp) window.location = base + 'index.php?c=projetos&m=deletar_projeto&id=' + id;
}

function del_chat(base, id)
{
    var resp = confirm("Deseja realmente deletar este chat e todas as conversas dele?");
    if (resp) window.location = base + 'index.php?c=chats&m=delete&id=' + id;
}

function del_cliente(base, id)
{
    var resp = confirm("Deseja realmente deletar este cliente?");
    if (resp) window.location = base + '&m=deletar&id=' + id;
}

function del_nov(base, id)
{
    var resp = confirm("Deseja realmente deletar esta novidade?");
    if (resp) window.location = base + 'index.php?c=novidades&m=deletar_novidade&id=' + id;
}

function del_usuario(base, id)
{
    var resp = confirm("Deseja realmente deletar este usuário?");
    if (resp) window.location = base + 'index.php?c=usuarios&m=deletar&id=' + id;
}

/* poderia utilizar função acima :: todo */
function del_img_not(base, id)
{
    var resp = confirm("Deseja realmente deletar esta imagem?");
    if (resp)
    {
        $('imagem').style.display = 'none';
        $('file').style.display = 'block';
        $('img').disabled = false;
        var url = base + 'admin/index.php?c=novidades&m=deletar_imagem&id=' + id;
        var parametros = '';
        var myAjax = new Ajax.Request( url, { method: 'get', parameters: parametros, onComplete: delok} );
    }
}

/* poderia utilizar função acima :: todo */
function del_img_home(base, id, num)
{
    var resp = confirm("Deseja realmente deletar esta imagem?");
    if (resp)
    {
        $('img'+num).style.display = 'none';
        $('file'+num).style.display = 'block';
        var url = base + 'admin/index.php?c=home&m=deletar_imagem&img=img' + num + '&id=' + id;
        var parametros = '';
        var myAjax = new Ajax.Request( url, { method: 'get', parameters: parametros, onComplete: delok} );
    }
}

function troca_img(img) {
    $("img_projeto").src = '../img/projetos/'+img;
    /*<br />[ <a href="../img/projetos/download.php?arquivo=' + img + '">download</a> ]*/
}

function troca_txtimg(texto){
    $("texto_imagem").innerHTML = texto;
}

function negrito(el, tot){
    for (i=1;i<=tot; i=i+1)
    {
        $("link"+i).style.fontWeight = 'normal';
        $("link"+i).style.color = '#999';
    }
    el.style.fontWeight = 'bold';
    el.style.color = '#000000';
}

function negrito2(el, tot){
    for (i=1;i<=tot; i=i+1)
    {
        $("link"+i).style.fontWeight = 'normal';
        $("link"+i).style.color = '#999';
    }
    el.style.fontWeight = 'bold';
    el.style.color = '#ffffff';
}

function mostra_proj(pos, id)
{
    var url = document.getElementById("url").value + 'index.php?c=projetos&m=get_projeto';
    var parametro = null;
    parametro = 'id=' + id;

    var objAjax = null;
    objAjax = new Ajax.Request(url, {method: 'post', parameters: parametro, onComplete: resultado});

	for (i = 0; i < document.getElementById("total_menu").value; i++) {
		document.getElementById("menu_"+i).className = "menu_projeto";
	}

	document.getElementById("menu_"+pos).className = "menu_projeto_hover";

}

function resultado(resposta){
    $('toda_pagina_projeto').innerHTML = resposta.responseText;
}

function buscaProjetos(base, idCliente, idProjeto)
{
    var url = base + 'index.php?c=chats&m=projetosCliente';
    var parametro = null;
    parametro = {'id': idCliente, 'idProjeto': idProjeto};

    var objAjax = null;
    objAjax = new Ajax.Request(url, {method: 'post', parameters: parametro, onComplete: resultadoBuscaProjetos});
}

function resultadoBuscaProjetos(resposta){
    $('projetos').innerHTML = resposta.responseText;
}

function enableSubmit()
{
    var idProjeto = 0;
    var eSelecao = document.getElementById('projeto_id');
    idProjeto = eSelecao.value;

    if( idProjeto != 0 )
    {
        document.getElementById('Submit').disabled = false;
    }
    else
    {
        document.getElementById('Submit').disabled = true;
    }
}

function altera_ordem_img(id_arquivo, ordem_arquivo)
{
    var url = document.getElementById("url").value + 'index.php?c=projetos&m=altera_ordem_arquivos&id=' + id_arquivo + '&ordem=' + ordem_arquivo;
    var parametro = null;
    parametro = {'id': id_arquivo, 'ordem': ordem_arquivo};

    var objAjax = null;
    objAjax = new Ajax.Request(url, {method: 'post', parameters: parametro, onComplete: resultadoAlteraOrdemImg});
}

function resultadoAlteraOrdemImg(resposta){
    alert('Ordem alterada');
}

/* CHAT */
function enviar()
{
    //var texto = encodeURIComponent($('texto').value);
    var texto = document.getElementById('texto').value;
    $('texto').value = '';

    if(texto.length > 0)
    {
        var url = document.getElementById("url").value + 'index.php?c=chats&m=gravaMsg';
        var parametro = null;
        parametro = 'texto=' + texto;

        var objAjax = null;
        objAjax = new Ajax.Request(url, {method: 'post', parameters: parametro, onComplete: resultadoEnviar});
    }
}

function resultadoEnviar(resposta){
    //$('textarea').innerHTML = resposta.responseText;
}

function colaTexto(texto)
{
    $('chat').value = texto;
    $('chat').scrollTop = scrollTamanho;
}

function atualizarFormulario()
{
    var url = document.getElementById("url").value + '/index.php?c=chats&m=atualizaMsg';
    var parametro = null;

    var objAjax = null;
    objAjax = new Ajax.Request(url, {method: 'post', parameters: parametro, onComplete: resultadoAtualizarFormulario});
}

var scrollTamanho = 1000000;

function resultadoAtualizarFormulario(resposta){
    //$('textarea').innerHTML = resposta.responseText;
    //alert(resposta.responseText);
    $('chat').value = resposta.responseText;
    scrollTamanho = scrollTamanho + 500;
    $('chat').scrollTop = scrollTamanho;
    atualizar = setTimeout("atualizarFormulario()", 3000);// alterei para 3 segundos, tava com 0.5 => 500
    $('chat').scrollTop = scrollTamanho;
    //colaTexto(resposta.responseText);
}

function enviarChat(event) {

    var charCode = (event.which) ? event.which : event.keyCode;

    if (charCode == 13) {
        enviar();
    }

}