﻿var mensagensVG = new Array();

var VG = {

	'guardaMensagens' : function (objeto, tipo, codigo) {
		mensagensVG[mensagensVG.length] = eval (elementosVG + "." + objeto + "." + tipo);
	},

	'validaMaximo' : function (objeto, tamanho) {
		if ( objeto.value.length > tamanho ) {
			VG.guardaMensagens (objeto.id, "msg_maximo");
			return false;
		}
		return true;
	},

	'validaER' : function (objeto, er) {
		if ( objeto.value.match (er) == null ) {
			VG.guardaMensagens (objeto.id, "msg_er");
			return false;
		}
		return true;
	},

	'validaRequerido' : function (objeto) {
		valor = lib.trim ( objeto.value );
		if ( valor == "" ) {
			VG.guardaMensagens (objeto.id, "msg_requerido");
			return false;
		}
		return true;
	},

	'controlaClasses' : function (objeto, tipo, classe) {
		if ( !document.getElementById(objeto) )
			return;
		if ( !tipo)
			return;
		if ( !tipo.match ( /(adicionar|remover)/gim ) )
			return;
		if ( !classe )
			return;
		if ( classe.match ( /^\s*?$/gim ) )
			return;

		objeto = document.getElementById(objeto);

		if (tipo == "adicionar")
			objeto.className += " " + classe;
		else
			objeto.className = (" " + objeto.className + " ").replace (" " + classe + " ", "");

	},

	'insereIconeCaixa' : function (objeto, icone) {
		if ( !document.getElementById(objeto) )
			return;

		objeto = document.getElementById(objeto);

		if ( objeto.previousSibling                                              &&
			   objeto.previousSibling.tagName                                      &&
			   objeto.previousSibling.tagName.toLowerCase() == "span"              &&
				 objeto.previousSibling.childNodes.length >= 1                       &&
				 objeto.previousSibling.childNodes[0].tagName                        &&
				 objeto.previousSibling.childNodes[0].tagName.toLowerCase() == "img" )
			return;

		var span = document.createElement ("span");

		var img = document.createElement ("img");
		img.src = "http://uolk.img.uol.com.br/" + icone;

		span.appendChild (img);

		objeto.parentNode.insertBefore ( span , objeto );
	},

	'removeIconeCaixa' : function (objeto) {
		if ( !document.getElementById(objeto) )
			return;

		objeto = document.getElementById(objeto);

		if ( !objeto.previousSibling )
			return;

		if ( objeto.previousSibling.tagName                         &&
			   objeto.previousSibling.tagName.toLowerCase() != "span" )
			return;

		objeto.parentNode.removeChild (objeto.previousSibling);

	},

	'iconesCaixas' : function () {
		for (var o in eval (elementosVG)) {
			if ( eval (elementosVG + '.' + o + '.temErros == true') ) {
				VG.insereIconeCaixa (o, "ico-alert.gif");
				VG.controlaClasses (o, "adicionar", "inerror");
                if (document.getElementById(o)                                              &&
                        document.getElementById(o).onkeyup                                    &&
                            document.getElementById(o).onkeyup.toString().match(/validar/gim)   )
                    validar(document.getElementById(o), false, false);
			}
			else {
				VG.removeIconeCaixa (o);
				VG.controlaClasses (o, "remover", "inerror");
			}
		}
	},

	'validaTextArea' : function ( t, limite ) {

		if ( window.event )
			t = window.event.srcElement;

		if ( !t )
			return;

		if ( !limite )
			limite = 512;

		VG.contaToques ( t, limite );

	},

	'visibilidadeContador' : function ( e ) {

		var t = window.event ? window.event.srcElement : e ? e.target : null;

		if ( !t )
			return;

		if ( !t.nextSibling                                                ||
		      t.nextSibling.nodeType != 1                                  ||
		      t.nextSibling.tagName.toLowerCase() != "span"                ||
		      t.nextSibling.className.match (/contador-toques/gim) == null )
			return;

		if ( t.nextSibling.style.display == "" )
				t.nextSibling.style.display = "none";
		else
			t.nextSibling.style.display = "";

		if ( t.nextSibling.className.match (/toques-demais/gim) != null )
			t.nextSibling.style.display = "";

	},

	'contaToques' : function ( alvo, limite ) {

		if ( !alvo )
			return;

		if ( !alvo.nextSibling                                                  ||
		      alvo.nextSibling.nodeType != 1                                    ||
		      alvo.nextSibling.tagName.toLowerCase() != "span"                  ||
		      alvo.nextSibling.className.match (/contador-toques/gim) == null ) {

			var span       = document.createElement ("span");
			span.className = "contador-toques";
			alvo.parentNode.insertBefore ( span, alvo.nextSibling );

		}

		var span           = alvo.nextSibling;
		span.innerHTML     = "";
		span.style.display = "";

		if ( alvo.value.length > limite )
			alvo.value = alvo.value.substr ( 0, limite );

		var t = document.createTextNode ( ( limite - alvo.value.length ) + " caracter" + ( ( ( limite - alvo.value.length ) > 1 ) ? "es" : "" ) + " restante" +  ( ( ( limite - alvo.value.length ) > 1 ) ? "s" : "" ) + " - este campo não aceita código html" );

		if ( limite == alvo.value.length )
			t = document.createTextNode ( "Nenhum caracter restante." );

		span.appendChild ( t );
		span.className = (" " + span.className + " ").replace (/\stoques-demais/gim, "");

		alvo.className = (" " + alvo.className + " ").replace (/\sinerror/gim, "");

		/*if ( alvo.value.length <= 512 ) {

			var t = document.createTextNode ( ( 512 - alvo.value.length ) + " caracter" + ( ( ( 512 - alvo.value.length ) > 1 ) ? "es" : "" ) + " restante" +  ( ( ( 512 - alvo.value.length ) > 1 ) ? "s" : "" ) + "." );

			if ( 512 == alvo.value.length )
				t = document.createTextNode ( "Nenhum caracter restante." );

			span.appendChild ( t );
			span.className = (" " + span.className + " ").replace (/\stoques-demais/gim, "");

			alvo.className = (" " + alvo.className + " ").replace (/\sinerror/gim, "");

		}
		else {

			var img = document.createElement ("img");
			img.src = "http://uolk.img.uol.com.br/alert-mini.gif";
			span.appendChild ( img );

			var t = document.createTextNode ( " Você excedeu o limite máximo em " + ( alvo.value.length - 512 ) + " caracter" + ( ( ( alvo.value.length - 512 ) > 1 ) ? "es" : "" ) + "." );
			span.appendChild ( t );

			if ( span.className.match (/\stoques-demais/gim) == null )
				span.className += " toques-demais";

			alvo.className += " inerror";

		}*/

	},

	'instalaTextAreas' : function() {

		if ( typeof textareasVG != "object" )
			return;

		for ( var o = 0; o < textareasVG.length; o++ ) {

			var tid = textareasVG[o].split("|")[0];

			if ( textareasVG[o].split("|").length > 1 )
				var tamanho = textareasVG[o].split("|")[1];
			else
				var tamanho = 512;

			if ( document.getElementById ( tid ) ) {
				var c = 'lib.addEvent ( document.getElementById ( tid ), "keyup", function() { VG.validaTextArea ( this, ' + tamanho.toString() + ' ) }, false );';
				eval ( c );
				// lib.addEvent ( document.getElementById ( textareasVG[o] ), "blur", VG.visibilidadeContador, false );
				// lib.addEvent ( document.getElementById ( textareasVG[o] ), "focus", VG.visibilidadeContador, false );
			}

		}

	},

	'init' : function() {

		for (var o in eval (elementosVG)) {

			erros = false;

			if ( eval (elementosVG + '.' + o + '.maximo') ) {
				if ( VG.validaMaximo (document.getElementById(o), eval (elementosVG + '.' + o + '.maximo') ) == false ) {
					erros = true;
				}
			}
			if ( eval (elementosVG + '.' + o + '.er') ) {
				if ( VG.validaER (document.getElementById(o), eval (elementosVG + '.' + o + '.er') ) == false ) {
					erros = true;
				}
			}
			if ( eval (elementosVG + '.' + o + '.requerido') ) {
				if ( VG.validaRequerido (document.getElementById(o)) == false ) {
					erros = true;
				}
			}

			if (erros == true)
				eval (elementosVG + "." + o + ".temErros = true");
			else
				eval (elementosVG + "." + o + ".temErros = false");

		}

		if ( document.getElementById("dentro") )
			lib.exibeMensagens (mensagensVG, "alert", "dentro", "mensagens-cadastro");
		else if ( document.getElementById("cadastro") )
			lib.exibeMensagens (mensagensVG, "alert", "cadastro", "mensagens-cadastro");

		if (mensagensVG.length == 0) {
			return true;
		}
		else {
			mensagensVG.length = 0;
			VG.iconesCaixas();
			return false;
		}

	}

}

if ( document.getElementById )
	lib.addEvent ( window, "load", VG.instalaTextAreas, false );
