function valueField (event, obj) {
	e = event.type;
	
	if(!obj.save)
	{
		obj.save = obj.value;		
	}
	
	if(e=="blur")
	{
		if(obj.value=="")
		{
			obj.value = obj.save;
		}
	}
	else if(e=="focus")
	{
		if(obj.value=="")
		{
			obj.value = obj.save;
		}
		else if(obj.value==obj.save)
		{
			obj.value = "";
		}
	}
}

function addValueField(obj) {
	$('#' + obj).focus( function(event) { valueField(event, this); } );
	$('#' + obj).blur( function(event) { valueField(event, this); } );
}

function in_array(needle,haystack) {
	return new RegExp('(^|\,)'+needle+'(\,|$)','gi').test(haystack);
}

var Utils = {
	
	abrir: true,
	
	pop:function(pagina, janela, width, height) {
	   var left = (screen.width - width)/2;
	   var top = (screen.height - height)/2;;   
	   var janela = window.open(pagina,janela, 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
	   janela.focus();
	},
	
	getPageSize:function() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	},
	
	getPageScroll:function () {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		arrayPageScroll = new Array(xScroll,yScroll);
		return arrayPageScroll;
	},
	
	resizeWindow:function(width, height, mode) {
		var arrPageSizes = Utils.getPageSize();
		var arrPageScroll = Utils.getPageScroll();		
		
		if(!mode) { mode = 'fast'; }
		
		$('#utils-window-content,#utils-window-content-ajax').animate({
			width: width,
			height: height
		}, mode);		
	},
		
	openWindow:function(pagina, params, wWindow, hWindow) {
		
		Utils.abrir = true;
		
		if(!params) { params = {}; }
		if(!wWindow) { wWindow = 500; }
		if(!hWindow) { hWindow = 450; }
		
		/* HTML		
		<div id="utils-window">
			<div id="utils-window-background"></div>
			<div id="utils-window-content">
				<div id="utils-window-loading"><img src="images/loading.gif"/></div>
				<div id="utils-window-content-ajax"></div>
			</div>
		</div>
		*/
		
		if($('#utils-window').length > 0) {
			$('#utils-window').remove();
		}		
		$('body').append('<div id="utils-window"><div id="utils-window-background"></div><div id="utils-window-content"><div id="utils-window-content-bg"><div id="utils-window-close">[x] Fechar&nbsp;&nbsp;</div><div id="utils-window-loading"><img src="images/loading.gif"/></div><div id="utils-window-content-ajax"></div></div></div></div>');
		
		var arrPageSizes = Utils.getPageSize();
		$('#utils-window-background').css({
			opacity:'0.5',
			display:'block',
			width: arrPageSizes[0],
			height: arrPageSizes[1]
		});
		
		$('#utils-window-content-ajax').css({
			width: wWindow,
			height: hWindow
		});

		var arrPageScroll = Utils.getPageScroll();		
		var heightContentWindow = $('#utils-window-content').height()/2;
		var widthContentWindow = $('#utils-window-content').width()/2;

		$('#utils-window-content').css({
			top: (arrPageSizes[3]/2 + arrPageScroll[1]) - heightContentWindow,
			left: (arrPageSizes[2]/2 + arrPageScroll[0]) - widthContentWindow
		});

		$('#utils-window-background').fadeIn();
		setTimeout( function() { $('#utils-window-content').fadeIn(); }, 100);
		setTimeout(
			function() {
				$('#utils-window-content-ajax').load(pagina, params, function() {
					if(Utils.abrir) {
						$('#utils-window-loading').hide();
						$('#utils-window-content').animate({
							top: (arrPageSizes[3]/2 + arrPageScroll[1]) - hWindow/ 2,
							left: (arrPageSizes[2]/2 + arrPageScroll[0]) - wWindow/2,
							width: wWindow,
							height: hWindow
						}, '', 'swing', function() { $('#utils-window-content-ajax').fadeIn(); });										
					}
				});
			},
		400);
		$('#utils-window-background,#utils-window-close').click(function() { Utils.closeWindow() });
	},
	
	closeWindow:function() {
		
		Utils.abrir = false;
		
		$('#utils-window-content, #utils-window-background, #utils-window-content-ajax').stop();

		$('#utils-window-content').fadeOut('fast');
		
		setTimeout(function() { $('#utils-window-background').fadeOut('fast'); }, 150);

	}
}

function addFocus(campo) {
	$('#' + campo).focus();
}

// FUNÇÃO PARA CARREGAR AS OPÇÕES DO SELECT
function loadOptions(value, field, page, msg)
{
	$("#" + field).removeOption(/./);
	
	if(value != 0) {
		$("#" + field).ajaxAddOption(page + '?value=' + value, '', false);
	} else {
		var myOptions = { '0' : msg }
		$("#" + field).addOption(myOptions);
	}
}
function limpaEspacos(palavra) {
	var partes = palavra.split(' ');
	var nPalavra = partes.join('-');
	return nPalavra;
}

function removeAcentos(text) {
  text = text.replace(new RegExp('[ÁÀÂÃ]','gi'), 'a');
  text = text.replace(new RegExp('[ÉÈÊ]','gi'), 'e');
  text = text.replace(new RegExp('[ÍÌÎ]','gi'), 'i');
  text = text.replace(new RegExp('[ÓÒÔÕ]','gi'), 'o');
  text = text.replace(new RegExp('[ÚÙÛ]','gi'), 'u');
  text = text.replace(new RegExp('[Ç]','gi'), 'c');
  return text;
}

function teclaEnter(e, fct)  {
	if(e.keyCode==13) {
		eval(fct);	
	}
}
