/*******************************************************************
  ************************
  Copyright (c) 2008 Thiago Caetano - thiagocae@gmail.com
                     Microum
					 www.microum.com.br

********************************************************************/

if(!SUBPASTA) var SUBPASTA = "";

/********************************************************
* Lib
********************************************************/

var MyLib = Class.create();
MyLib.prototype = {
	initialize: function(){
		this.tempo_aguarde = false;
	},
	// encode
	encode: function(str){
		if(!str) return "";
		var hex_chars = "0123456789ABCDEF";
		var noEncode = /^([a-zA-Z0-9\_\-\.])$/;
		var n, strCode, hex1, hex2, strEncode = "";
		for(n = 0; n < str.length; n++) {
			if (noEncode.test(str.charAt(n))) {
				strEncode += str.charAt(n);
			} else {
				strCode = str.charCodeAt(n);
				hex1 = hex_chars.charAt(Math.floor(strCode / 16));
				hex2 = hex_chars.charAt(strCode % 16);
				strEncode += "%" + (hex1 + hex2);
			}
		}
		return strEncode;
	},
	// decode
	decode: function(str){
		if(!str) return "";
		var n, strCode, strDecode = "";
		for (n = 0; n < str.length; n++){
			if (str.charAt(n) == "%"){
				strCode = str.charAt(n + 1) + str.charAt(n + 2);
				strDecode += String.fromCharCode(parseInt(strCode, 16));
				n += 2;
			} else {
				strDecode += str.charAt(n);
			}
		}
		return strDecode; 
	},
	// Pega os valores do Form
	Forms: function(nome){
		var valor, chave, nome, tipo, linha = "";
		var form = document[nome];
		for(var i=0;i<form.elements.length;i++){
			chave = form.elements[i];
			tipo = chave.type;
			nome = chave.name;
			if(nome=='AJAX') continue;
			switch(tipo){
				case "select-one":
					valor = chave.options[chave.selectedIndex].value;
					break;
				case "radio":
					if(!chave.checked) continue;
					valor = chave.value;
					break;
				case "checkbox":
					if(!chave.checked) continue;
					valor = chave.value;
					break;
				default:
					valor = chave.value;
			}
			if(!valor) continue;
			if(!nome) continue;
			linha += "&"+this.encode(nome)+"="+this.encode(valor);
		}
		return linha;
	}
};
MyLib = new MyLib();


///////////////////////////////////////////////////////////////////////////////



// Verifica o formulario
function VerForm(){
	var cp = "";
	var erro = "";
	var val = "";
	var nome_erro = 'erros';
	// transforma em array
	var cp = arguments;
	var campos = Array();
	for(var i=0;i<cp.length;i++) campos[i] = cp[i];
	//
	var nomeform = campos[0];
	var form = document[nomeform];
	// nome do campo de erro
	if(campos[1].match(/NOME_ERRO:/)){
		var qb = campos[1].split(':');
		if(!qb[1]) nome_erro = "";
		else nome_erro = qb[1];
		campos[1] = '';
	}
	if(nome_erro) Element.hide(nome_erro);
	// coringa para atualizar o box de texto
	if(campos[1]=='TEXTO'){
		campos[1] = '';
		Wiki.updateTextArea();
	}
	// coringa para a noticias
	if(campos[1]=='NOTICIAS'){
		campos[1] = '';
		var total = campos.length;
		// nova editoria
		Element.removeClassName('ceditoria_nova','erro');
		if($F('editoria')=="-"){
			campos[total] = "editoria_nova";
			total++;
		}
		// resumo e imagem
		var tipo = $F('tipo_destaque');
		if(tipo!="normal"){
/*			campos[total] = "resumo";
			total++;*/
			if(tipo=="destaque2" || tipo=="super"){
				campos[total] = "imagem";
				total++;
			}
			if(tipo=="super"){
				campos[total] = "legenda";
				total++;
			}
		}
		Wiki.updateTextArea();
	}
	var verdefault = false;
	var versenha = 0;
	for(var i=0;i<campos.length;i++){
		if(!i) continue;
		cp = campos[i];
		if(!cp) continue;
		// coringa
		if(cp=='DEFAULTVALUES'){
			verdefault = true;
			continue;
		}
		if(cp=='senha' || cp=='resenha') versenha++;
		val = "";
		if(form[cp].type=="select-one"){
			val = form[cp].selectedIndex;
			if(val<1) val = 0;
		}
		else{
			val = form[cp].value;
			if(verdefault){
				if(form[cp].value==form[cp].defaultValue) val = '';
			}
		}
		if(!val || val==0){
			erro = 1;
			Element.addClassName('c'+cp,'erro');
		}
		else{
			Element.removeClassName('c'+cp,'erro');
		}
	}
	// tem senha
	if(versenha==2){
		var senha = form.senha.value;
		if(senha.length<3){
			erro = 1;
			Element.addClassName('csenha','erro');
			alert('A senha precisa ter no mínimo 3 caracteres.')
		}
		else if(senha!=form.resenha.value){
			alert('As duas senhas estão diferentes.')
			erro = 1;
			Element.addClassName('csenha','erro');
			Element.addClassName('cresenha','erro');
		}
	}
	if(erro){
		if(nome_erro) Element.show(nome_erro);
		else alert('Não foi possível enviar, verifique os campos destacados.')
		return false;
	}
	return true;
}


// cria o menu funcional
function MenuAtiva(){
	var li, ul, link, i, j, node, lis, navRoot, ull, ulltotal, ultimo;
	navRoot = document.getElementById("menu");
	for (i=0; i<navRoot.childNodes.length; i++) {
		node = navRoot.childNodes[i];
		if(node.nodeName!="UL") continue;
		lis = node.getElementsByTagName('li');
		for (j = 0; j<lis.length, li = lis[j]; j++){
			if(!li.getElementsByTagName('ul')[0]) continue;
			// evento do mouse
			li.onmouseover = function() {
				var cod = (document.all) ? 2 : 3;
				this.childNodes[cod].style.display = 'block';
			}
			li.onmouseout = function() {
				var cod = (document.all) ? 2 : 3;
				this.childNodes[cod].style.display = 'none';
			}
			link = li.getElementsByTagName('a')[0];
			link.onfocus = function(){
				this.parentNode.getElementsByTagName('ul')[0].style.display = 'block';
			}
			ul = link.parentNode.getElementsByTagName('ul')[0];
			if (ul){
				ull = ul.getElementsByTagName('a');
				ulltotal = ull.length-1;
				var ultimo = ull[ulltotal];
				if (ultimo){
					ultimo.onblur = function(){
						this.parentNode.parentNode.style.display='none';
					}
					ultimo.parentNode.onblur = function(){
						this.parentNode.style.display = '';
					}
				}
			}
		}
	}
}





