//_______________+++++++++++++++++++++++++++++++++
// JavaScript Document
var form = document.forms[0];
var send = true;
var alerted = false;

//************************************************************************************************//
function submitForm() {
	var frm = submitForm.arguments[0];
	if (frm == "" || frm == undefined) {//*/
		document.forms[0].submit();
	} else {
		document.getElementById(frm).submit();
	}
}

function mudarAction() {
	var frm 	= mudarAction.arguments[0];
	var action  = mudarAction.arguments[1];
	if (frm == "" || frm == undefined) {
		if (action != "" || action == undefined) {
			document.forms[0].action = action;
		}
	} else {
		if (action != "" || action == undefined) {
			document.getElementById(frm).action = action;
		}
	}
}

// Funções Trim para Javascript
// JavaScript Document
function Trim(TRIM_VALUE) {
	if (TRIM_VALUE.length < 1) {
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if (TRIM_VALUE=="") {
		return "";
	} else {
		return TRIM_VALUE;
	}
} //End Function
function RTrim(VALUE) {
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if (v_length < 0) {
		return"";
	}
	var iTemp = v_length -1;
	
	while(iTemp > -1) {
		if(VALUE.charAt(iTemp) == w_space) {
		} else {
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
	
	} //End While
	return strTemp;	
} //End Function

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if (v_length < 1) {
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";
	
	var iTemp = 0;
	
	while(iTemp < v_length) {
		if (VALUE.charAt(iTemp) == w_space) {
		} else {
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function

// Exibe erros
function isValid(result, input, msg) {
   if (result == false && msg != "" && alerted == false) {
      alert(msg);
	  alerted = true;
      document.getElementById(input).focus();
   } else { 
   	return true;
   }
}
// Exibe erros

// Validação para não vazio
function noNull(input, msg) {
	var result = false;
	var input2 = noNull.arguments[2];
	
    if (Trim(document.getElementById(input).value) == "") result = false; else result = true;
	
	if (input2 == "" || input2 == undefined || input2 == null){
    	isValid(result, input, msg);
	} else {
		isValid(result, input2, msg);
	}

   return result;
}
// Validação para não vazio

function equalTo(inputA,inputB,msg) {
	var result = false;

	if (document.getElementById(inputA).value == document.getElementById(inputB).value) {
		result = true;
	} else {
		result = false;
	}

	isValid(result, inputA, msg);
	
	return result;
}

function enviar_cotato(id) {
	alerted = false;  

	var id = id.id;
    document.getElementById(id).disabled = true;
    
	send = noNull("nome", "Por favor, informe o Nome.");
    //send = noNull("telefone", "Telefone precisa ser informado.");
    send = noNull("email", "Por favor, informe o E-mail.");
    //send = noNull("assunto", "Por favor, informe o Assunto.");
    send = noNull("comentario", "Por favor, digite o Comentário."); 
	
	if ( !send ) {
	    if ( document.getElementById(id).disabled == false ) {
			document.getElementById(id).disabled = true;
		} else {
			document.getElementById(id).disabled = false;
		}
	}
    
	if (!alerted && send) {
		//ajaxPost("validacao.php","frm_login","alerta");
		//getResponse("alerta","validacao.php","","Processando...");	
		document.getElementById("btnEnviar").disabled = false;
		mudarAction("frmContato","sendMail.php");
		submitForm("frmContato");
    }
}

function verifica_tamanho_tel(valor, tipo) {	
	var vfone = valor;  
	if ( tipo == "f" ) {
		var tipo_num = "Telefone";	
		var nome_campo = "fone";
	} else {
		var tipo_num = "Celular";	
		var nome_campo = "celular";
	}
	//alert(vfone.length);
	if ( vfone.length > 0 && vfone.length <= 8 ) {
		alert(tipo_num+" incompleto");
		document.getElementById(nome_campo).focus();
		return false;
	} else {
		return true;
	}
}

function fmtTel(campo, e) {
    myVal = campo.value;
    
    if (myVal.length > 4 && !myVal.match(/\-/)) {
       myVal = '';
    } else {
        if (window.event) {
           keycode = window.event.keyCode;
        } else if (e) {
            keycode = e.which;
        }
        
        if ( (keycode < 48 || keycode > 57) && (keycode < 96 || keycode > 105) ) {
            myVal = myVal.substr(0, (myVal.length - 1));
        }
        
        if (myVal.length == 4) {
            myVal += '-';
        }
    }

    campo.value = myVal;
}

function soNumeros(v){
    return v.replace(/\D/g,"")
}

function muda_cor_fundo_campo(valor) {
	var campo_id = valor.id;
	//alert(campo_id);
	document.getElementById(campo_id).style.backgroundColor = "#FFFFFF";
}

function volta_cor_fundo_campo(valor) {
	var campo_id = valor.id;
	document.getElementById(campo_id).style.backgroundColor = "#F9F9F9";
}

function habilita_butao() {
	document.getElementById("btnEnviar").disabled = false;
}

function envia_login() {
	alerted = false;  

	send = noNull("nome_login", "Por favor, informe o Nome.");
	send = noNull("senha_login", "Por favor, informe a Senha.");
	
	if (!alerted && send) {
		mudarAction("frmLogin","inc/valida_login.php");
		submitForm("frmLogin");
    }
}

function valida_login() {
	if ( document.getElementById("nome_login").value == "login" ) {
		alert("Informe o Login.");
		document.getElementById("nome_login").focus();
		return false;
	} else if ( document.getElementById("senha_login").value == "senha" ) {
		alert("Informe a Senha.");
		document.getElementById("senha_login").focus();
		return false;
	}
	else {
		envia_login();	
	}
}

function goURL(url) {
	window.location.href = url;	
}

function escolheu_editar_materia() {
	goURL("editar_materia.php");
}

function escolheu_carregar_arquivo() {
	goURL("carregar_arquivo.php");
}

function escolheu_usuario() {
	goURL("editar_usuario.php");
}

function escolheu_consultar_arquivo() {
	goURL("consultar_arquivo.php");	
}

function recarrega_editar_materia(valor_sel) {
	//alert(valor_sel.value);
	submitForm("frm_Editar_Materia");
}

function enviar_editar_materia() {
	alerted = false;  
	var textarea = document.getElementsByTagName('textarea');
	alert(textarea.length);
	//send = noNull("materia", "Texto do menu, precisa ser informado.");
	
	if (!alerted && send) {
		mudarAction("frm_Editar_Materia","editar_materia.php");
		submitForm("frm_Editar_Materia");
    }
}

function radioNoNull(radioName,msg) {
	var radios = document.getElementsByName(radioName);
	var result = false;
	var cont   = 0;
	for (var i = 0; i < radios.length; i++) {
		if (radios[i].checked) {
			result = true;
			break;
		} else {
			result = false;
		}
	}
	isValid(result, radios[0].id, msg);
	
	return result;
}


function enviar_frm_Carregar_Arquivo() {
	alerted = false;  

	send = noNull("nome_arquivo", "Por favor, escolha o arquivo.");
	send = radioNoNull("tipo", "Por favor, informe o tipo.");	
	if (!alerted && send) {
		//mudarAction("frm_Carregar_Arquivo","carregar_arquivo.php");
		document.getElementById("enviou").value = "enviado";
		submitForm("frm_Carregar_Arquivo");
    }
	
}

function muda_qtd_reg_page() {
	if ( document.getElementById("qtd_reg_page").value == "" ) {
		alert("Informe o número de Registros");
		document.getElementById("qtd_reg_page").focus();
		return false;
	} else {
		goURL("carregar_arquivo.php?n="+document.getElementById("qtd_reg_page").value);	
	}
}

function muda_qtd_reg_page_nome() {
	if ( document.getElementById("qtd_reg_page").value == "" ) {
		alert("Informe o número de Registros");
		document.getElementById("qtd_reg_page").focus();
		return false;
	} else {
		goURL("carregar_arquivon.php?n="+document.getElementById("qtd_reg_page").value);	
	}
}

function muda_qtd_reg_page_tipo() {
	if ( document.getElementById("qtd_reg_page").value == "" ) {
		alert("Informe o número de Registros");
		document.getElementById("qtd_reg_page").focus();
		return false;
	} else {
		goURL("carregar_arquivot.php?n="+document.getElementById("qtd_reg_page").value+"&s="+document.getElementById("sel_tipo").value);	
	}
}

function exibe_campo_pesquisa(valor) {
	//alert(valor.value);
	if ( valor.value == "nome" ) {
		goURL("carregar_arquivon.php");	
	} else if ( valor.value == "tipo" ) {
		goURL("carregar_arquivot.php");	
	} else {
		goURL("carregar_arquivo.php");	
	}
}

function recarrega_con_por_nome() {
	alerted = false;  
	  
    send = noNull("param_nome_arquivo", "Nome precisa ser informado.");

	if (!alerted && send) {
		//alert(document.getElementById("sel_consulta").value);
		switch ( document.getElementById("sel_consulta").value ) {
			case "nome":
				mudarAction("frmPesquisa","carregar_arquivon.php");
				break;
			case "tipo":
				mudarAction("frmPesquisa","carregar_arquivot.php");
				break;
			default:
				mudarAction("frmPesquisa","carregar_arquivo.php");
				break;
		}
		submitForm("frmPesquisa");
	}
}

function recarrega_con_por_tipo() {
	alerted = false;  
	  
    send = noNull("sel_tipo", "Selecione o tipo.");

	if (!alerted && send) {
		//alert(document.getElementById("sel_consulta").value);
		mudarAction("frmPesquisa","carregar_arquivot.php");
		submitForm("frmPesquisa");
	}
}

function excluir_arq(valor) {
	//alert(valor);
	if ( confirm("Tem certeza que deseja excluir o arquivo ?") ) {
		goURL("deletar_arquivo.php?i="+valor);
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function exibir_arq(valor) {
	//alert(valor);
	//goURL("exibe_arquivo.php?i="+valor);
	MM_openBrWindow('exibe_arquivo.php?i='+valor,'','width=750,height=673')
}

function recarrega_page() {
	submitForm("frm_editar_materia");
}

function submeter_form_materia() {
	alerted = false;  
	 
    send = noNull("texto_materia", "Informe o texto da matéria.");

	if (!alerted && send) {
		//mudarAction("frm_editar_materia","inc/editar_materia_process.php");
		submitForm("frm_editar_materia");
	}
}

// Coloca o foco no editor.
function setFocus() {
  textEdit.focus()
}

// Executa um comando no editor.
// Pelo menos um argumento é requerido. Alguns comandos
// requerem um segundo argumento opicional:
// ex., ("formatblock","<H1>") para colocar uma marca <H1>.
function Formatacao(comando) {
  if (formato=="HTML") {
	if (arguments[1]=="CustomFont") {
  arguments[1] = prompt("Com qual fonte você deseja formatar o texto?","Verdana, Arial, Helvetica, sans-serif")
}
	if ((arguments[1]=="") && (arguments[0]=="forecolor")) {
	  arguments[1] = prompt("Qual a cor que você deseja?\n(Em inglês ou código hexadecimal)","Black")
	}
	var edit = textEdit.document.selection.createRange()
	if (arguments[1]==null)
	  edit.execCommand(comando)
	else
	  edit.execCommand(comando,false, arguments[1])
	edit.select()
	textEdit.focus()
  }
}

function deleta_imagem(id_img) {
	if ( confirm("Tem certeza que deseja excluir a imagem ?") ) {
		goURL("inc/deletar_img.php?i="+id_img);
	}
}

function exibir_edicao_bloco(bloco) {
	var campo_edicao_texto = "campo_edicao_texto_bloco_"+bloco;
	if ( document.getElementById(campo_edicao_texto).style.display == "none" ) {
		if ( document.getElementById("campos_novo_bloco").style.display != "none" ) {
			document.getElementById("campos_novo_bloco").style.display = "none";
		}
		document.getElementById(campo_edicao_texto).style.display = "inline";	
	} else {
		document.getElementById(campo_edicao_texto).style.display = "none";	
	}
}

function exibir_edicao_novo_bloco() {
	if ( document.getElementById("campos_novo_bloco").style.display == "none" ) {
		/*if ( document.getElementById("campo_edicao_texto").style.display != "none" ) {
			document.getElementById("campo_edicao_texto").style.display = "none";
		}  //   */
		document.getElementById("campos_novo_bloco").style.display = "inline";	
	} else {
		document.getElementById("campos_novo_bloco").style.display = "none";	
	}
}

function excluir_bloco(valor_id_materia) {
	if ( confirm("Tem certeza que deseja excluir esse bloco ?") ) {
		goURL("inc/excluir_bloco.php?i="+valor_id_materia);
	}
}

function envia_novo_usu() {
	alerted = false;  
	 
    send = noNull("txt_nome", "Informe o nome do usuário.");
	send = radioNoNull("radio_tipo", "Por favor, informe o tipo.");	
    send = noNull("txt_login", "Informe o login do usuário.");
  	if ( !equalTo("txt_confirm_senha","txt_senha", "A confirmação da senha não confere com a senha digitada.") ) {
		send = false;  
  	}

	if (!alerted && send) {
		submitForm("frm_novo_usu");
	}
}