
var urlSite = 'http://www.infravision.com.br/site';

/**
* Classe Ajax
* Por Thiago Genuino - Coldvector
* http://www.thiagogenuino.com.br
* Email: thiagogenuino@coldvector.com
* -----------
* Exemplo de uso:
* var ajax = new AjaxClass(retorno, msg); // Retorno sendo o local aonde será retornado o resultado e msg a mensagem que será exibido enquanto é carregado os dados.
* ajax.setParam('chave', 'valor'); // Parametros que serão enviados para o script, setParam seta 1 parametro por vez
* ajax.setParams({'chave' : 'valor'}); // Parametros que serão enviados para o script, em setParams deverá ser passado os parametros por Object {'chave' : 'valor', 'chave' : 'valor'}
* - Modos de retorno:
* ajax.update(); ou ajax.atualizar(); -> Altera o conteúdo do campo de retorno pelo resultado do script.
* ajax.hide(); ajax.esconder(); -> Esconde o retorno após executar o script.
* ajax.adicionar(); -> Adiciona o resultado do script no final do conteúdo do retorno.
* ajax.load(); ou ajax.execute(); -> Carrega o resultado em uma string para tratamento ou captura.
* ajax.redirect(url); -> Redireciona para a página após a execução do script.
* ajax.sendAlert(); -> Exibe um alert() com o resultado do script.
* ajax.getEnd(); -> Retorna se o script foi finalizado ou não. True = finalizado
* ajax.getResult(); -> Captura o retorno do script
* ajax.getError(); -> Captura o erro do script caso exista
*/
function AjaxClass(retorno, msg) {
	var xhttp = null;
	var params = {};
	var _params = {};
	var ativo = false;
	var script = urlSite+'/ajax.php';
	var msg = msg || '...';
	//this.retorno = retorno || null;
	var result = null;
	var error = null;
	
	try {
		xhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			try {
				xhttp = new XMLHttpRequest();
			} catch (e3) {
				xhttp = false;
			}
		}
	}
	
	this.setScript = function(scr) {
		script = scr;
	}
	
	this.setParams = function(parametros) {
		parametros = parametros || {};
		
		for (key in parametros) {
			_params[key] = parametros[key];
		}
	}
	
	this.setParam = function(chave, valor) {
		_params[chave] = valor;
	}
	
	this.montaParametros = function() {
		for (key in _params) {
			params += '&'+key+'='+_params[key];
		}
	}
	
	this.start = function() {
		this.montaParametros();
		ativo = true;
		xhttp.open("POST", script, true);
		xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhttp.setRequestHeader("Content-length", params.length);
		xhttp.setRequestHeader("Connection", "close");
		
		xhttp.send(params);
	}
	
	// Chamadas
	
	this.adicionar = function(local) {
		retorno = local || retorno;
		var isSelect 	= (document.getElementById(retorno).type ? (document.getElementById(retorno).type == 'select-one' ? true : false) : false);
		
		if (isSelect) {
			xhttp.onreadystatechange = this.funAddSelect;
		} else {
			xhttp.onreadystatechange = this.funAdicionar;
		}
		
		this.start();
	}
	
	this.atualizar = function(local) {
		retorno 		= local || retorno;
		var isSelect 	= (document.getElementById(retorno).type ? (document.getElementById(retorno).type == 'select-one' ? true : false) : false);
		
		if (isSelect) {
			xhttp.onreadystatechange = this.funUpdateSelect;
		} else{
			xhttp.onreadystatechange = this.funUpdate;
		}
		
		this.start();
	}
	
	this.update = function(local) {
		retorno = local || retorno;
		return this.atualizar();
	}
	
	this.hide = function(local) {
		retorno = local || retorno;
		xhttp.onreadystatechange = this.funHide;
		this.start();
	}
	
	this.esconder = function(local) {
		retorno = local || retorno;
		return this.hide(retorno);
	}
	
	this.load = function() {
		xhttp.onreadystatechange = this.funExecute;
		this.start();
	}
	
	this.execute = function() {
		return this.load();
	}
	
	this.redirect = function(url) {
		retorno = url || retorno;
		xhttp.onreadystatechange = this.funRedirect;
		this.start();
	}
	
	this.sendAlert = function() {
		xhttp.onreadystatechange = this.funAlert;
		this.start();
	}
	
	this.getEnd = function() {
		return (ativo == true ? false : true);
	}
	
	this.getResult = function() {
		return result;
	}
	
	this.getError = function() {
		return error;
	}
	
	
	// Funções
	
	this.funRedirect = function() {
		if (xhttp.readyState == 4 && xhttp.status == 200) { 
			result = xhttp.responseText;  
			ativo = false;
			
			window.location = retorno;
		}
	}
	
	this.funAlert = function() {
		if (xhttp.readyState == 4 && xhttp.status == 200) { 
			result = xhttp.responseText;  
			ativo = false;
			
			alert(result);
		}
	}
	
	this.funAdicionar = function () {
		ajx = document.getElementById(retorno);
		if (xhttp.readyState == 4 && xhttp.status == 200) { 
			result = xhttp.responseText;  
			ajx.value += result;
			ajx.innerHTML += result;
			
			ativo = false;
		}
	}
	
	this.funAddSelect = function() {
		ajx = document.getElementById(retorno);
		var isSelect 	= (ajx.type ? (ajx.type == 'select-one' ? true : false) : false);
		
		if (!isSelect) {
			ativo = false;
			error = 'Elemento não é um Select';
			return false;
		}
		
		if (xhttp.readyState == 4 && xhttp.status == 200) { 
			res = xhttp.responseText;  
			
			var itens = res.slice(0,-1).split('|');
			
			for (i=0;i<itens.length;i++) {
				var opt = document.createElement("OPTION");
				split = itens[i].split('=');
				
				opt.text = split[0];
				opt.value = split[1];
				
				ajx.options.add(opt);
			}
			
			ativo = false;
			
		}
	}
	
	this.funUpdate = function () {
		ajx = document.getElementById(retorno);
		if (xhttp.readyState == 1) {
			if (ajx.innerHTML) ajx.innerHTML = msg;
			if (ajx.value) ajx.value = msg;
		}
		if (xhttp.readyState == 4 && xhttp.status == 200) { 
			result = xhttp.responseText;
			ajx.value = result;
			ajx.innerHTML = result;
			
			ativo = false;
		}
	}
	
	this.funUpdateSelect = function() {
		ajx 			= document.getElementById(retorno);
		var isSelect 	= (ajx.type ? (ajx.type == 'select-one' ? true : false) : false);
		
		if (!isSelect) {
			ativo = false;
			error = 'Elemento não é um Select';
			return false;
		}
		
		ajx.options = [];
		
//		for (i=ajx.options.length;i>=0;i--) {
//			ajx.remove(i);
//		}
		
		if (xhttp.readyState == 1) {
			var opt = document.createElement("OPTION");
			opt.text = msg;
			opt.value = '';
			
			ajx.options.add(opt);
		}
		
		if (xhttp.readyState == 4 && xhttp.status == 200) { 
			result = xhttp.responseText;
			var res = result;
		
			if (res.substr(res.length-1,1) == '|') {
				var itens = res.slice(0,-1).split('|');
			}
			
			for (i=0;i<itens.length;i++) {
				var opt = document.createElement("OPTION");
				split = itens[i].split('=');
				
				opt.text = split[0];
				opt.value = split[1];
				
				ajx.options.add(opt);
			}
			
			ativo = false;
			
		}
	}
	
	this.funHide = function () {
		ajx = document.getElementById(retorno);
		if (xhttp.readyState == 4 && xhttp.status == 200) { 
			ajx.style.display = 'none';
			
			ativo = false;
		}
	}
	
	this.funExecute = function () {
		if (xhttp.readyState == 4 && xhttp.status == 200) { 
			result = xhttp.responseText;
			ativo = false;
		}
	}
};

Tools = {
	divAjuda : document.createElement('div'),
	getPosition : function(e){
		var left = 0;
		var top  = 0;
	
		while (e.offsetParent){
			left += e.offsetLeft;
			top  += e.offsetTop;
			e     = e.offsetParent;
		}
	
		left += e.offsetLeft;
		top  += e.offsetTop;
	
		return {x:left, y:top};
	},
	
	scan : function() {
		var elements = document.body.getElementsByTagName("*");
		for(i = 0 ; i < elements.length ; i++) {
			if (elements[i].getAttribute('tools')) {
				switch(elements[i].getAttribute('tools')) {
					case 'moeda':
						Tools.setMoeda(elements[i]);
					break;
					case 'permitido':
						Tools.setPermitido(elements[i]);
					break;
					case 'ajuda':
						Tools.setAjuda(elements[i]);
					break;
					case 'formatar':
						Tools.setFormatar(elements[i]);
					break;
				}
			}
		}
	},
	
	setMoeda : function(campo) {
		var separador_milhar = campo.getAttribute('separador_milhar') || '.';
		var separador_decimal = campo.getAttribute('separador_decimal') || ',';
		
		campo.setAttribute('onkeypress',"return Tools.formatarMoeda(this,'"+separador_milhar+"','"+separador_decimal+"',event);");
	},
	
	formatarMoeda : function(campo, separador_milhar, separador_decimal, tecla) {
		var sep = 0;
		var key = '';
		var i = j = 0;
		var len = len2 = 0;
		var strCheck = '0123456789';
		var aux = aux2 = '';
		var ev = window.event || tecla;
		var whichCode = ev.which || ev.keyCode;
		
		if (whichCode == 13) return true; // Tecla Enter
		if (whichCode == 8) return true; // Tecla Delete
		if (whichCode == 0) return true; // Tecla Tab
		key = String.fromCharCode(whichCode); // Pegando o valor digitado
		if (strCheck.indexOf(key) == -1) return false; // Valor inválido (não inteiro)
		len = campo.value.length;
		for(i = 0; i < len; i++)
		if ((campo.value.charAt(i) != '0') && (campo.value.charAt(i) != separador_decimal)) break;
		aux = '';
		for(; i < len; i++)
		if (strCheck.indexOf(campo.value.charAt(i))!=-1) aux += campo.value.charAt(i);
		aux += key;
		len = aux.length;
		if (len == 0) campo.value = '';
		if (len == 1) campo.value = '0'+ separador_decimal + '0' + aux;
		if (len == 2) campo.value = '0'+ separador_decimal + aux;
	
		if (len > 2) {
			aux2 = '';
	
			for (j = 0, i = len - 3; i >= 0; i--) {
				if (j == 3) {
					aux2 += separador_milhar;
					j = 0;
				}
				aux2 += aux.charAt(i);
				j++;
			}
	
			campo.value = '';
			len2 = aux2.length;
			for (i = len2 - 1; i >= 0; i--)
			campo.value += aux2.charAt(i);
			campo.value += separador_decimal + aux.substr(len - 2, len);
		}
	
		return false;
	},
	
	setAjuda : function(campo) {
		var msg = campo.getAttribute('ajuda') || null;
		
		campo.setAttribute('onfocus',"Tools.mostrarAjuda(this,'"+msg+"');");
		campo.setAttribute('onblur',"Tools.escondeAjuda();");
	},
	
	mostrarAjuda : function(campo,msg) {
		var pos = Tools.getPosition(campo);
		
		if (!document.getElementById('toolsAjuda')) {
			Tools.divAjuda.id = 'toolsAjuda';
			Tools.divAjuda.style.position = 'absolute';
			Tools.divAjuda.style.backgroundColor = '#fff';
			Tools.divAjuda.style.border = '1px solid #000';
			Tools.divAjuda.style.padding = '3px';
			Tools.divAjuda.style.width = '200px';
			Tools.divAjuda.style.textAlign = 'center';
			Tools.divAjuda.style.display = 'none';
			Tools.divAjuda.style.fontFamily = 'verdana';
			Tools.divAjuda.style.fontSize = '12px';
			
			document.body.appendChild(Tools.divAjuda);
		}
		
		Tools.divAjuda.innerHTML = msg;
		Tools.divAjuda.style.display = 'block';
		Tools.divAjuda.style.top = pos['y']+'px';
		Tools.divAjuda.style.left = (pos['x'] + campo.offsetWidth + 5) + 'px';
	},
	
	escondeAjuda : function() {
		Tools.divAjuda.style.display = 'none';
	},
	
	setFormatar : function(campo) {
		var formatar = campo.getAttribute('formatar') || null;
		var permitido = campo.getAttribute('permitido') || null;
		
		campo.setAttribute('onkeypress',"return Tools.formatar(this,event,'"+formatar+"','"+permitido+"')");
	},
	
	formatar : function(campo, ev, mask, permitido) {
		if (permitido != 'null') {
			permitido = permitido.toLowerCase();
		} else {
			permitido = false;
		}
		
		if (window.event) {
			key = window.event.keyCode;
		} else {
			key = ev.which;
		}
	
		if ((key == 8) || (key == 9) || (key == 13) || (key == 0)) {
			return true;
		}
	
		var string = campo.value;  
		var i = string.length;
		
		if (permitido) {
			var keychar = String.fromCharCode(key).toLowerCase();
			if ((permitido).indexOf(keychar.toLowerCase()) == -1) {
				return false;
			}
		}
		
		if (i < mask.length) {
			if (mask.charAt(i) == '?') {
				return (key > 47 && key < 58);
			} else {
				if (mask.charAt(i) == '!') {
					return true;
				}
				for (c = i; c < mask.length; c++) {
					if (mask.charAt(c) != '?' && mask.charAt(c) != '!') {
						campo.value = campo.value + mask.charAt(c);
					} else if (mask.charAt(c) == '!') {
						return true;
					} else {
						return (key > 47 && key < 58);
					}
				}
			}
		} else {
			return false;
		}
	},
	
	setPermitido : function(campo) {
		var permitido = campo.getAttribute('permitido') || null;
		
		campo.setAttribute('onkeypress',"return Tools.permitido(this,event,'"+permitido+"');");
	},
	
	permitido : function(field,ev,permitido) {
		var key;
		var keychar;
		
		if (window.event) {
		   key = window.event.keyCode;
		} else if (ev) {
		   key = ev.which;
		} else {
		   return true;
		}
		
		var numpad_chaves = new Array(48,49,50,51,52,53,54,55,56,57);
		var numpad_valores = new Array(0,1,2,3,4,5,6,7,8,9);
		
		if ((key >= 48) && (key <= 57)) {
			for(tecla in numpad_chaves) {
				if (key == numpad_chaves[tecla]) {
					keychar = numpad_valores[tecla];
				}
			}
		} else {
			keychar = String.fromCharCode(key).toLowerCase();
		}
		
		if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27)) {
		   return true;
		} else if (((permitido).indexOf(keychar) > -1)) {
			return true;
		} else {
			return false;
		}
	}
};

// Helper Object by Thiago Genuino
// contato: thiagogenuino@coldvector.com
// Forma de Utilização:
// Adicione aos campos que deseja ter o Helper o atributo helper ex:
// <input type="text" name="nome" helper="Digite seu Nome">
// Depois adicione ao evento onload do body: Helper.start();
// Está função irá percorrer os elementos e adicionar aos que possuem o atributo helper um painel para ajuda com a mensagem previamente configurada
// Caso queira estilizar o painel, adicione na função Helper.start(PainelClass,ContadorClass) o nome das classes CSS para estilização
Helper = {
	painel : null,
	contador : null,
	onBlurTemp : [],
	onFocusTemp : [],
	onKeyUpTemp : [],
	getPosition : function(e){
		var left = 0;
		var top  = 0;
	
		while (e.offsetParent){
			left += e.offsetLeft;
			top  += e.offsetTop;
			e     = e.offsetParent;
		}
	
		left += e.offsetLeft;
		top  += e.offsetTop;
	
		return {x:left, y:top};
	},
	start : function(painelClass,contadorClass) {
		var c = document.getElementsByTagName("body")[0].getElementsByTagName("*");
		for(i=0;i<c.length;i++) {
		if (c[i].hasAttribute('helper')) {
			var id = c[i].id;
			
			if (c[i].onfocus) {
				Helper.onFocusTemp[id] = c[i].onfocus.toString().replace('function onfocus(event) {','');
				Helper.onFocusTemp[id] = Helper.onFocusTemp[id].replace('}','');
			} else {
				Helper.onFocusTemp[id] = null;
			}
			
			c[i].onfocus = function() {
				if (Helper.onFocusTemp[this.id]) {
					eval(Helper.onFocusTemp[this.id]);
				}
				
				if (!Helper.painel) {
					Helper.painel = document.createElement('div');
					document.body.appendChild(Helper.painel);
					
					Helper.contador = document.createElement('div');
					Helper.contador.setAttribute('id','_cntChars');
					if (contadorClass) {
						Helper.contador.className = contadorClass;
					} else {
						Helper.contador.style.textAlign = 'center';
						Helper.contador.style.color = '#cc0000';
						Helper.contador.style.fontWeight = 'bolder';
					}
					
					if (painelClass) {
						Helper.painel.className = painelClass;
					} else {
						Helper.painel.style.backgroundColor = '#f0f0f0';
						Helper.painel.style.border = '1px solid #000';
						Helper.painel.style.padding = '3px';
						Helper.painel.style.width = '150px';
						Helper.painel.style.fontSize = '10px';
						Helper.painel.style.fontFamily = 'verdana';
						Helper.painel.style.textAlign = 'center';
//						if (document.all) {
//							Helper.painel.style.filter = "alpha(opacity=90)";
//						} else {
//							Helper.painel.style.opacity = 0.9;
//						}
					}
				}
				
				var coords = Helper.getPosition(this);
				
				Helper.painel.setAttribute('id','_painel');
				Helper.painel.style.position = 'absolute';
				Helper.painel.style.display = 'block';
				Helper.painel.style.zIndex = '2000';
				Helper.painel.style.top = (coords.y + this.offsetHeight) + 'px';
				Helper.painel.style.left = coords.x + 'px';
				Helper.painel.innerHTML = this.getAttribute('helper');
				if (this.type == 'text') {
					Helper.painel.appendChild(Helper.contador);
					Helper.contador.innerHTML = this.value.length + ' / ' + (this.getAttribute('maxlength') || '*') + ' caracteres';
				}
			}
			
			if (c[i].onkeyup) {
				Helper.onKeyUpTemp[id] = c[i].onkeyup.toString().replace('function onkeyup(event) {','');
				Helper.onKeyUpTemp[id] = Helper.onKeyPressTemp[id].replace('}','');
			} else {
				Helper.onKeyUpTemp[id] = null;
			}
			
			c[i].onkeyup = function () {
				if (Helper.onKeyUpTemp[this.id]) {
					eval(Helper.onKeyUpTemp[this.id]);
				}
				if (this.type == 'text') {
					Helper.contador.innerHTML = this.value.length + ' / ' + (this.getAttribute('maxlength') || '*') + ' caracteres';
				}
			}
			
			if (c[i].onblur) {
				Helper.onBlurTemp[id] = c[i].onblur.toString().replace('function onblur(event) {','');
				Helper.onBlurTemp[id] = Helper.onBlurTemp[id].replace('}','');
			} else {
				Helper.onBlurTemp[id] = null;
			}
			
			c[i].onblur = function() {
				if (Helper.onBlurTemp[this.id]) {
					eval(Helper.onBlurTemp[this.id]);
				}
				
				Helper.painel.style.display = 'none';
			}
		}
	}
	}
};

if (typeof($) !== 'function') {
	function $(element) {
		return Util.$(element);
	}
}

Util = {
	$ : function(element) {
		return document.getElementById(element);
	},
	
	display : function(id, state) {
		state = state || 'block';
		
		Util.$(id).style.display = state;
	},
	
	checkIE : function () {
		return /*@cc_on!@*/false;
	},
	
	callOnLoad : function () {
		//Util.mostrarRelogio('relogio');
	},
	
	rand : function(n) {
		return ( Math.floor ( Math.random ( ) * n + 1 ) );
	},
	
	getScrollXY : function() {
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
	  return { x : scrOfX, y : scrOfY };
	},
	
	mouseCoords : function(ev) {
		ev = ev || window.event;
		if(ev.pageX || ev.pageY) {
			return {x:ev.pageX, y:ev.pageY};
		}
		return {
			x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
			y:ev.clientY + document.body.scrollTop  - document.body.clientTop
		};
	},

	removeChars : function(string, permitido) {
		var retorno = '';
		for (i=0;i<string.length;i++) {
			if (permitido.indexOf(string.charAt(i)) > -1) {
				retorno += string.charAt(i);
			}
		}
		
		return retorno;
	},
	
	mostrarRelogio : function (container) {
		if ($(container)) {
			var interval = setInterval(function() {
				var split = horario.split(' ');
				var date = split[0];
				var time = split[1];
			
				var date = date.split('-');
				var time = time.split(':');
				
				var ano = date[0];
				var mes = date[1];
				var dia = date[2];
				
				var hora = time[0];
				var minuto = time[1];
				var segundo = time[2];
				
				var retorno = dia + '/' + mes + '/' + ano + ' ' + hora + ':' + minuto + ':' +segundo;
				
				$(container).innerHTML = retorno;
				$(container).value = retorno;
			},1000);
		}
	},
	
	getPosition : function(e){
		var left = 0;
		var top  = 0;
	
		while (e.offsetParent){
			left += e.offsetLeft;
			top  += e.offsetTop;
			e     = e.offsetParent;
		}
	
		left += e.offsetLeft;
		top  += e.offsetTop;
	
		return {x:left, y:top};
	},
	
	empty : function(string) {
		if ((string == '') || (string == false) || (string == undefined) || 
			(string == 0) || (string == null) || (string == '0')) {
				return true;
			} else {
				return false;
			}
	},
	
	formatar : function (valor, mask, permitido) {
		if (permitido) {
			permitido = permitido.toLowerCase()
		}
	
		var string = '';  
		var retorno = '';
		
		if (permitido) {
			for(x=0;x<valor.length;x++) {
				if ((permitido).indexOf(valor.charAt(x).toLowerCase()) > -1) {
					string += valor.charAt(x);
				}
			}
		} else {
			string = valor;
		}
		
		var y = 0;
		for (c = 0; c < mask.length; c++) {
			if (mask.charAt(c) != '?' && mask.charAt(c) != '!') {
				retorno += mask.charAt(c);
				continue;
			}
			
			if (mask.charAt(c) == '?') {
				if (('1234567890').indexOf(string.charAt(y)) > -1) {
					retorno += string.charAt(y);
				}
			} else {
				retorno += string.charAt(y);
			}
			
			y = y + 1;
		}
		
		return retorno;
	},
	
	formatarCampo : function(campo, ev, mask, permitido) {
		permitido = permitido.toLowerCase();
		if (window.event) {
			key = window.event.keyCode;
		} else {
			key = ev.which;
		}
	
		if ((key == 8) || (key == 9) || (key == 13) || (key == 0)) {
			return true;
		}
	
		var string = campo.value;  
		var i = string.length;
		
		if (permitido) {
			var keychar = String.fromCharCode(key).toLowerCase();
			if ((permitido).indexOf(keychar.toLowerCase()) == -1) {
				return false;
			}
		}
		
		if (i < mask.length) {
			if (mask.charAt(i) == '?') {
				return (key > 47 && key < 58);
			} else {
				if (mask.charAt(i) == '!') {
					return true;
				}
				for (c = i; c < mask.length; c++) {
					if (mask.charAt(c) != '?' && mask.charAt(c) != '!') {
						campo.value = campo.value + mask.charAt(c);
					} else if (mask.charAt(c) == '!') {
						return true;
					} else {
						return (key > 47 && key < 58);
					}
				}
			}
		} else {
			return false;
		}
	},
	
	redirecionar : function(url) {
		window.location = url;
	},
	
	go : function(url) {
		window.location = url;
	},
	
	blank : function(url,target) {
		target = target || null;
		
		window.open(url,target);
	},
	
	permitido : function(field, ev, permitido) {
		var key;
		var keychar;
		
		if (window.event) {
		   key = window.event.keyCode;
		} else if (ev) {
		   key = ev.which;
		} else {
		   return true;
		}
		
		var numpad_chaves = new Array(48,49,50,51,52,53,54,55,56,57);
		var numpad_valores = new Array(0,1,2,3,4,5,6,7,8,9);
		
		if ((key >= 48) && (key <= 57)) {
			for(tecla in numpad_chaves) {
				if (key == numpad_chaves[tecla]) {
					keychar = numpad_valores[tecla];
				}
			}
		} else {
			keychar = String.fromCharCode(key).toLowerCase();
		}
		
		if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27)) {
		   return true;
		} else if (((permitido).indexOf(keychar) > -1)) {
			return true;
		} else {
			return false;
		}
	},
	
	formatar_moeda : function(campo, separador_milhar, separador_decimal, tecla) {
		var sep = 0;
		var key = '';
		var i = j = 0;
		var len = len2 = 0;
		var strCheck = '0123456789';
		var aux = aux2 = '';
		var whichCode = (window.Event) ? tecla.which : tecla.keyCode;
		
		if (whichCode == 13) 	return true; // Tecla Enter
		if (whichCode == 8) 	return true; // Tecla Delete
		if (whichCode == 0) 	return true; // Tecla Tab
		key = String.fromCharCode(whichCode); // Pegando o valor digitado
		if (strCheck.indexOf(key) == -1) return false; // Valor inválido (não inteiro)
		len = campo.value.length;
		for(i = 0; i < len; i++)
		if ((campo.value.charAt(i) != '0') && (campo.value.charAt(i) != separador_decimal)) break;
		aux = '';
		for(; i < len; i++)
		if (strCheck.indexOf(campo.value.charAt(i))!=-1) aux += campo.value.charAt(i);
		aux += key;
		len = aux.length;
		if (len == 0) campo.value = '';
		if (len == 1) campo.value = '0'+ separador_decimal + '0' + aux;
		if (len == 2) campo.value = '0'+ separador_decimal + aux;
	
		if (len > 2) {
			aux2 = '';
	
			for (j = 0, i = len - 3; i >= 0; i--) {
				if (j == 3) {
					aux2 += separador_milhar;
					j = 0;
				}
				aux2 += aux.charAt(i);
				j++;
			}
	
			campo.value = '';
			len2 = aux2.length;
			for (i = len2 - 1; i >= 0; i--)
			campo.value += aux2.charAt(i);
			campo.value += separador_decimal + aux.substr(len - 2, len);
		}
	
		return false;
	}
	
};

var horario = '2012-02-22 17:34:57';
var relogioInterval = setInterval(function() {
	
	var split 	= horario.split(' ');
	var date 	= split[0];
	var time 	= split[1];
	
	var date 	= date.split('-');
	var time 	= time.split(':');
	
	var ano 	= date[0] * 1;
	var mes 	= date[1] * 1;
	var dia 	= date[2] * 1;
	
	var hora 	= time[0] * 1;
	var minuto 	= time[1] * 1;
	var segundo = time[2] * 1;
	
	segundo = segundo + 1;
	
	if (segundo >= 60) {
		segundo = 0;
		minuto 	= minuto + 1;
	}
	
	if (minuto >= 60) {
		minuto 	= 0;
		hora 	= hora + 1;
	}
	
	if (hora >= 24) {
		hora = 0;
		dia = dia + 1;
	}
	
	segundo = (segundo.toString().length == 1 ? '0'+segundo : segundo);
	minuto 	= (minuto.toString().length == 1 ? '0'+minuto : minuto);
	hora 	= (hora.toString().length == 1 ? '0'+hora : hora);
	
	mes 	= (mes.toString().length == 1 ? '0'+mes : mes);
	dia 	= (dia.toString().length == 1 ? '0'+dia : dia);
	
	horario = ano.toString() + '-' + mes.toString() + '-' + dia.toString() + ' ' + hora.toString() + ':' + minuto.toString() + ':' + segundo.toString();
	
},1000);

window.onload = function() {
	Util.callOnLoad();
}



if (typeof($) != 'function') {
	function $(element) {
		return document.getElementById(element);
	}
}

Drag = {
	drags : [],
	drops : [],
	funs : {},
	back : null,
	ondrag : false,
	dragObj : null,
	dropTarget : null,
	bkpClass : null,
	lastObj : null,
	staticid : 0,
	mouseOffset : null,
	cnt : function () {
		return Drag.staticid++;
	},
	
	coords : function(ev) {
		ev           = ev || window.event;
		if(ev.pageX || ev.pageY){
			return {x:ev.pageX, y:ev.pageY};
		}
		return {
			x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
			y:ev.clientY + document.body.scrollTop  - document.body.clientTop
		};
	},
	
	mousemove : function(ev) {
		ev           = ev || window.event;
		var mousePos = Drag.coords(ev);
		var aim   = ev.target || ev.srcElement;
		
		if (Drag.ondrag == true) {
			if (Drag.lastObj != null) {
				if (Drag.lastObj !== aim) {
					Drag.lastObj.className = Drag.bkpClass;
					Drag.lastObj = null;
					Drag.bkpClass = null;
				}
			}
			if (aim.getAttribute('container') == 'true') {
				Drag.dropTarget = aim.id;
				if (aim.getAttribute('onmouseeffect')) {
					if (Drag.bkpClass == null) {
						Drag.bkpClass = aim.className;
						Drag.lastObj = aim;
					}
					aim.className = aim.getAttribute('onmouseeffect');
				}
			}
			
			Drag.dragObj.style.top      = (mousePos.y + 2) + 'px';
			Drag.dragObj.style.left     = (mousePos.x + 2) + 'px';
//			Drag.dragObj.style.top      = (mousePos.y - Drag.mouseOffset.y) + 'px';
//			Drag.dragObj.style.left     = (mousePos.x - Drag.mouseOffset.x) + 'px';
		}
	
		return false;
	},
	
	scan : function() {
		var c = document.body.getElementsByTagName("*");
		for(i=0;i<c.length;i++) {
			if (c[i].getAttribute('drag') == 'true') {
				Drag.makeDrag(c[i]);
			}
			
			if (c[i].getAttribute('container') == 'true') {
				Drag.makeDrop(c[i]);
			}
		}
	},
	
	makeDrag : function(me) {
		if (!me.id) {
			me.id = 'drag' + Drag.cnt();
			Drag.drags.push(me.id);
		}
		
		me.onmousedown = function(ev) { return Drag.startDrag(this, ev); }
		document.onmouseup = function(ev) { if (Drag.ondrag == true) { return Drag.stopDrag(Drag.dragObj, ev); } }
	},
	
	getMouseOffset : function(target, ev) {
		ev = ev || window.event;
	
		var docPos    = Drag.getPosition(target);
		var mousePos  = Drag.coords(ev);
		return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
	},
	
	getPosition : function(e){
		var left = 0;
		var top  = 0;
	
		while (e.offsetParent){
			left += e.offsetLeft;
			top  += e.offsetTop;
			e     = e.offsetParent;
		}
	
		left += e.offsetLeft;
		top  += e.offsetTop;
	
		return {x:left, y:top};
	},
	
	makeDrop : function(me) {
		if (!me.id) {
			me.id = 'drop' + Drag.cnt();
			Drag.drops.push(me.id);
		}
	},
	
	startDrag : function(obj, ev) {
		ev         = ev || window.event;
		
		Drag.ondrag = true;
		Drag.dragObj = obj;
		Drag.dragObj.style.position = 'absolute';
		Drag.mouseOffset = Drag.getMouseOffset(Drag.dragObj, ev);
		
//		var mousePos = Drag.coords(ev);
//		Drag.dragObj.style.top      = (mousePos.y + 2) + 'px';
//		Drag.dragObj.style.left     = (mousePos.x + 2) + 'px';
//		Drag.dragObj.style.top      = (mousePos.y - Drag.mouseOffset.y) + 'px';
//		Drag.dragObj.style.left     = (mousePos.x - Drag.mouseOffset.x) + 'px';
		
		return false;
	},
	
	stopDrag : function(obj, ev) {
		ev         = ev || window.event;
		var mousePos = Drag.coords(ev);
		var aim = ev.target || ev.srcElement;
		
		if (aim.getAttribute('container') == 'true') {
			Drag.dropTarget = aim.id;
		}
		
		while(aim.offsetParent) {
			if (aim.id) {
				if (Drag.checkCallBack(aim.id) == true) {
					break;
				}
			}
			
			aim = aim.offsetParent;
		}
		
		if (Drag.bkpClass != null) {
			aim.className = Drag.bkpClass;
		}
		Drag.bkpClass = null;
		Drag.lastObj = null;
		
		Drag.dragObj.style.position = 'static';
		Drag.dragObj.style.top = '';
		Drag.dragObj.style.left = '';
		Drag.ondrag = false;
		Drag.dragObj = null;
		
		return false;
	},
	
	checkCallBack : function(id) {
		var aim = $(id);
		
		if (aim.getAttribute('callback')) {
			var fun = aim.getAttribute('callback') + '(\''+Drag.dragObj.id+'\', \'' + aim.id + '\');';
			eval(fun);
			return true;
		}
	}
	
};

/** Função de Retorno Callback
function teste(dragid, dropid) {
	var drag = $(dragid);
	var drop = $(Drag.dropTarget);
	
	drag.parentNode.removeChild(drag);
	
	drop.appendChild(drag);
}
**/

// Title: Tigra Color Picker
// URL: http://www.softcomplex.com/products/tigra_color_picker/
// Version: 1.1
// Date: 06/26/2003 (mm/dd/yyyy)
// Note: Permission given to use this script in ANY kind of applications if
//    header lines are left unchanged.
// Note: Script consists of two files: picker.js and picker.html

var TCP = new TColorPicker();

function TCPopup(field, palette) {
	this.field = field;
	this.initPalette = !palette || palette > 3 ? 0 : palette;
	var w = 194, h = 240,
	move = screen ? 
		',left=' + ((screen.width - w) >> 1) + ',top=' + ((screen.height - h) >> 1) : '', 
	o_colWindow = window.open('/scripts/picker.html', null, "help=no,status=no,scrollbars=no,resizable=no" + move + ",width=" + w + ",height=" + h + ",dependent=yes", true);
	o_colWindow.opener = window;
	o_colWindow.focus();
}

function TCBuildCell (R, G, B, w, h) {
	return '<td bgcolor="#' + this.dec2hex((R << 16) + (G << 8) + B) + '"><a href="javascript:P.S(\'' + this.dec2hex((R << 16) + (G << 8) + B) + '\')" onmouseover="P.P(\'' + this.dec2hex((R << 16) + (G << 8) + B) + '\')"><img src="pixel.gif" width="' + w + '" height="' + h + '" border="0"></a></td>';
}

function TCSelect(c) {
	this.field.value = '#' + c.toUpperCase();
	this.win.close();
}

function TCPaint(c, b_noPref) {
	c = (b_noPref ? '' : '#') + c.toUpperCase();
	if (this.o_samp) 
		this.o_samp.innerHTML = '<font face=Tahoma size=2>' + c +' <font color=white>' + c + '</font></font>'
	if(this.doc.layers)
		this.sample.bgColor = c;
	else { 
		if (this.sample.backgroundColor != null) this.sample.backgroundColor = c;
		else if (this.sample.background != null) this.sample.background = c;
	}
}

function TCGenerateSafe() {
	var s = '';
	for (j = 0; j < 12; j ++) {
		s += "<tr>";
		for (k = 0; k < 3; k ++)
			for (i = 0; i <= 5; i ++)
				s += this.bldCell(k * 51 + (j % 2) * 51 * 3, Math.floor(j / 2) * 51, i * 51, 8, 10);
		s += "</tr>";
	}
	return s;
}

function TCGenerateWind() {
	var s = '';
	for (j = 0; j < 12; j ++) {
		s += "<tr>";
		for (k = 0; k < 3; k ++)
			for (i = 0; i <= 5; i++)
				s += this.bldCell(i * 51, k * 51 + (j % 2) * 51 * 3, Math.floor(j / 2) * 51, 8, 10);
		s += "</tr>";
	}
	return s	
}
function TCGenerateMac() {
	var s = '';
	var c = 0,n = 1;
	var r,g,b;
	for (j = 0; j < 15; j ++) {
		s += "<tr>";
		for (k = 0; k < 3; k ++)
			for (i = 0; i <= 5; i++){
				if(j<12){
				s += this.bldCell( 255-(Math.floor(j / 2) * 51), 255-(k * 51 + (j % 2) * 51 * 3),255-(i * 51), 8, 10);
				}else{
					if(n<=14){
						r = 255-(n * 17);
						g=b=0;
					}else if(n>14 && n<=28){
						g = 255-((n-14) * 17);
						r=b=0;
					}else if(n>28 && n<=42){
						b = 255-((n-28) * 17);
						r=g=0;
					}else{
						r=g=b=255-((n-42) * 17);
					}
					s += this.bldCell( r, g,b, 8, 10);
					n++;
				}
			}
		s += "</tr>";
	}
	return s;
}

function TCGenerateGray() {
	var s = '';
	for (j = 0; j <= 15; j ++) {
		s += "<tr>";
		for (k = 0; k <= 15; k ++) {
			g = Math.floor((k + j * 16) % 256);
			s += this.bldCell(g, g, g, 9, 7);
		}
		s += '</tr>';
	}
	return s
}

function TCDec2Hex(v) {
	v = v.toString(16);
	for(; v.length < 6; v = '0' + v);
	return v;
}

function TCChgMode(v) {
	for (var k in this.divs) this.hide(k);
	this.show(v);
}

function TColorPicker(field) {
	this.build0 = TCGenerateSafe;
	this.build1 = TCGenerateWind;
	this.build2 = TCGenerateGray;
	this.build3 = TCGenerateMac;
	this.show = document.layers ? 
		function (div) { this.divs[div].visibility = 'show' } :
		function (div) { this.divs[div].visibility = 'visible' };
	this.hide = document.layers ? 
		function (div) { this.divs[div].visibility = 'hide' } :
		function (div) { this.divs[div].visibility = 'hidden' };
	// event handlers
	this.C       = TCChgMode;
	this.S       = TCSelect;
	this.P       = TCPaint;
	this.popup   = TCPopup;
	this.draw    = TCDraw;
	this.dec2hex = TCDec2Hex;
	this.bldCell = TCBuildCell;
	this.divs = [];
}

function TCDraw(o_win, o_doc) {
	this.win = o_win;
	this.doc = o_doc;
	var 
	s_tag_openT  = o_doc.layers ? 
		'layer visibility=hidden top=54 left=5 width=182' : 
		'div style=visibility:hidden;position:absolute;left:6px;top:54px;width:182px;height:0',
	s_tag_openS  = o_doc.layers ? 'layer top=32 left=6' : 'div',
	s_tag_close  = o_doc.layers ? 'layer' : 'div'
		
	this.doc.write('<' + s_tag_openS + ' id=sam name=sam><table cellpadding=0 cellspacing=0 border=1 width=181 align=center class=bd><tr><td align=center height=18><div id="samp"><font face=Tahoma size=2>sample <font color=white>sample</font></font></div></td></tr></table></' + s_tag_close + '>');
	this.sample = o_doc.layers ? o_doc.layers['sam'] : 
		o_doc.getElementById ? o_doc.getElementById('sam').style : o_doc.all['sam'].style

	for (var k = 0; k < 4; k ++) {
		this.doc.write('<' + s_tag_openT + ' id="p' + k + '" name="p' + k + '"><table cellpadding=0 cellspacing=0 border=1 align=center>' + this['build' + k]() + '</table></' + s_tag_close + '>');
		this.divs[k] = o_doc.layers 
			? o_doc.layers['p' + k] : o_doc.all 
				? o_doc.all['p' + k].style : o_doc.getElementById('p' + k).style
	}
	if (!o_doc.layers && o_doc.body.innerHTML) 
		this.o_samp = o_doc.all 
			? o_doc.all.samp : o_doc.getElementById('samp');
	this.C(this.initPalette);
	if (this.field.value) this.P(this.field.value, true)
}


Div = {
	
	create : function(divid, className, styles) {
		if (!$(divid)) {
			
			var div 		= document.createElement('div');
			div.id 			= divid;
			div.className 	= className;
			
			for(key in styles) {
				eval('div.styles.'+key+' = '+styles[key]+';');
			}
			
			document.body.appendChild(div);
			
			return div;
		}
		
		return $(divid);
	}
	
}


