FivecomUtil = {
	splitV:
		function (enable) {
			if (enable == false) {
				$("#sistemaPrincipalLeft").hide();
			}
			else {
				$("#sistemaPrincipalLeft").show();
			}
			FivecomUtil.setSize();
		},
	splitH:
		function (enable) {
			if (enable == false) {
				$("#conteudoPrincipalSplit").hide();
				$("#conteudoPrincipalBottom").hide();
			}
			else {
				$("#conteudoPrincipalSplit").show();
				$("#conteudoPrincipalBottom").show();
			}
			FivecomUtil.setSize();
		},
	bottonFullScreen:
		function (enable){
			if (enable==true) {
                $("#conteudoPrincipalSplit").html("<a href=\"javascript:FivecomUtil.bottonFullScreen(false);\"><img src=\"imagens/fig_min.gif\" alt=\"\" title=\"Recolher formulário\" border=\"0\" /></a>");
				$("#myGrid").hide();
				this.setSize(27);
			}
			else {
                $("#conteudoPrincipalSplit").html("<a href=\"javascript:FivecomUtil.bottonFullScreen(true);\"><img src=\"imagens/fig_max.gif\" alt=\"\" title=\"Expandir formulário\" border=\"0\" /></a>");
				$("#myGrid").show();
				this.setSize();
			}
		},
	setSize:
		function (heightTop){

			if (heightTop == undefined)
				heightTop = 200;
			
			var myWidth = 0, myHeight = 0;
			if( typeof( window.innerWidth ) == 'number' ) {
				//Non-IE
				myWidth = window.innerWidth;
				myHeight = window.innerHeight;
			} else {
				myWidth = document.documentElement.clientWidth;
				myHeight = document.documentElement.clientHeight;
			}
	
			if ($("#windowSistema").width()) {
				$("#windowSistema").width(myWidth);
				$("#windowSistema").height(myHeight);
				var windowSistema_w = parseInt($("#windowSistema").width().replace("px",""));
				var windowSistema_h = parseInt($("#windowSistema").height().replace("px",""));
			}
	
			if ($("#windowSistemaLogo").width()) {
				$("#windowSistemaLogo").width(myWidth);
				$("#windowSistemaLogo").height(50);
				var windowSistemaLogo_w = parseInt($("#windowSistemaLogo").width().replace("px",""));
				var windowSistemaLogo_h = parseInt($("#windowSistemaLogo").height().replace("px",""));
			}
			
			if ($("#windowSistemaMenu").width()) {
				$("#windowSistemaMenu").width(myWidth);
				$("#windowSistemaMenu").height(30);
				var windowSistemaMenu_w = parseInt($("#windowSistemaMenu").width().replace("px",""));
				var windowSistemaMenu_h = parseInt($("#windowSistemaMenu").height().replace("px",""));
			}
			
			if ($("#windowSistemaRodape").width()) {
				$("#windowSistemaRodape").width(myWidth);
				$("#windowSistemaRodape").height(30);
				var windowSistemaRodape_w = parseInt($("#windowSistemaRodape").width().replace("px",""));
				var windowSistemaRodape_h = parseInt($("#windowSistemaRodape").height().replace("px",""));		
			}
			
			if ($("#windowSistemaPrincipal").width()) {
				$("#windowSistemaPrincipal").width(myWidth);
				$("#windowSistemaPrincipal").height(windowSistema_h-(windowSistemaLogo_h+windowSistemaMenu_h+windowSistemaRodape_h));
				var windowSistemaPrincipal_w = parseInt($("#windowSistemaPrincipal").width().replace("px",""));
				var windowSistemaPrincipal_h = parseInt($("#windowSistemaPrincipal").height().replace("px",""));	
			}
	
			if ($("#sistemaPrincipal").width()) {
				$("#sistemaPrincipal").width(windowSistemaPrincipal_w);
				$("#sistemaPrincipal").height(windowSistemaPrincipal_h);
				var sistemaPrincipal_w = parseInt($("#sistemaPrincipal").width().replace("px",""));
				var sistemaPrincipal_h = parseInt($("#sistemaPrincipal").height().replace("px",""));	
			}
			if ($("#sistemaPrincipalLeft").width()) {
				$("#sistemaPrincipalLeft").width(200-2); 
				$("#sistemaPrincipalLeft").height(sistemaPrincipal_h-(10+2)); //diminuindo 2x5px cellspacing, 2x1px border
				var sistemaPrincipalLeft_w = parseInt($("#sistemaPrincipalLeft").width().replace("px",""));
				var sistemaPrincipalLeft_h = parseInt($("#sistemaPrincipalLeft").height().replace("px",""));
			}
			if ($("#sistemaPrincipalLeft").width()) {
				if ($("#sistemaPrincipalLeft").css("display") == "none") {
					sistemaPrincipalLeft_w = -7; //5px do space que divide - 2px da borda que nao possui
				}
			}
			if ($("#sistemaPrincipalRight").width()) {
				$("#sistemaPrincipalRight").width(sistemaPrincipal_w-(sistemaPrincipalLeft_w+15+2)); //diminuindo 200px do sistemaPrincipalLeft, 3x5px cellspacing, 2x1px border
				$("#sistemaPrincipalRight").height(sistemaPrincipal_h-(10)); //diminuindo 2x5px cellspacing
				var sistemaPrincipalRight_w = parseInt($("#sistemaPrincipalRight").width().replace("px",""));
				var sistemaPrincipalRight_h = parseInt($("#sistemaPrincipalRight").height().replace("px",""));	
			}
	
			if ($("#conteudoPrincipal").width()) {
				$("#conteudoPrincipal").width(sistemaPrincipalRight_w); 
				$("#conteudoPrincipal").height(sistemaPrincipalRight_h); 
				var conteudoPrincipal_w = parseInt($("#conteudoPrincipal").width().replace("px",""));
				var conteudoPrincipal_h = parseInt($("#conteudoPrincipal").height().replace("px",""));			
			}
	
			var conteudoPrincipalTop_h = 0;
			var conteudoPrincipalTop_w = 0;
			if ($("#conteudoPrincipalBottom").width()) {
				conteudoPrincipalTop_h = heightTop;	
				if ($("#conteudoPrincipalBottom").css("display") == "none")
					conteudoPrincipalTop_h = conteudoPrincipal_h;
			}
				
			if ($("#conteudoPrincipalTop").width()) {
				$("#conteudoPrincipalTop").width(conteudoPrincipal_w-2); //2x1px border
				$("#conteudoPrincipalTop").height(conteudoPrincipalTop_h-2); //2x1px border
				conteudoPrincipalTop_w = parseInt($("#conteudoPrincipalTop").width().replace("px",""));
				conteudoPrincipalTop_h = parseInt($("#conteudoPrincipalTop").height().replace("px",""));	
			}
			
			var conteudoPrincipalSplit_w = 0;
			var conteudoPrincipalSplit_h = 0;
			var conteudoPrincipalBottom_w = 0;
			var conteudoPrincipalBottom_h = 0;
			
			if ($("#conteudoPrincipalBottom").width()) {
				if ($("#conteudoPrincipalBottom").css("display") != "none") {
					$("#conteudoPrincipalSplit").width(conteudoPrincipal_w);
					$("#conteudoPrincipalSplit").height(5);
					conteudoPrincipalSplit_w = parseInt($("#conteudoPrincipalSplit").width().replace("px",""));
					conteudoPrincipalSplit_h = parseInt($("#conteudoPrincipalSplit").height().replace("px",""));			
			
					$("#conteudoPrincipalBottom").width(conteudoPrincipal_w-2); //2x1px border
					$("#conteudoPrincipalBottom").height(conteudoPrincipal_h-(conteudoPrincipalTop_h+conteudoPrincipalSplit_h+4));  //4x1px border
					conteudoPrincipalBottom_w = parseInt($("#conteudoPrincipalBottom").width().replace("px",""));
					conteudoPrincipalBottom_h = parseInt($("#conteudoPrincipalBottom").height().replace("px",""));			
				}
			}
	
			/*********************************************************************************/
			/*
			/* PARAMETRIZACAO DAS DIVs DE CONTEUDO MENU, GRID, FORM
			/*
			/*********************************************************************************/
		
			var frameCenterLeftBox_w = 0;
			var frameCenterLeftBox_h = 0;
			if ($("#frameCenterLeftBox").width()) {
				$("#frameCenterLeftBox").width(sistemaPrincipalLeft_w); 
				$("#frameCenterLeftBox").height(sistemaPrincipalLeft_h); 
				frameCenterLeftBox_w = parseInt($("#frameCenterLeftBox").width().replace("px",""));
				frameCenterLeftBox_h = parseInt($("#frameCenterLeftBox").height().replace("px",""));		
			}
			if ($("#frameCenterLeftBoxModuloTreeviewBox").width()) {
				$("#frameCenterLeftBoxModuloTreeviewBox").width(frameCenterLeftBox_w); 
				$("#frameCenterLeftBoxModuloTreeviewBox").height(frameCenterLeftBox_h - 59); 
			}
			if ($("#conteudoPrincipal #conteudoTop").width()) {
				$("#conteudoPrincipal #conteudoTop").width(conteudoPrincipalTop_w); 
				$("#conteudoPrincipal #conteudoTop").height(conteudoPrincipalTop_h); 
				var conteudoTop_w = parseInt($("#conteudoPrincipal #conteudoTop").width().replace("px",""));
				var conteudoTop_h = parseInt($("#conteudoPrincipal #conteudoTop").height().replace("px",""));	
			}
			
			if ($("#conteudoPrincipal #conteudoBottom").width()) {
				$("#conteudoPrincipal #conteudoBottom").width(conteudoPrincipalBottom_w); 
				$("#conteudoPrincipal #conteudoBottom").height(conteudoPrincipalBottom_h); 
				var conteudoBottom_w = parseInt($("#conteudoPrincipal #conteudoBottom").width().replace("px",""));
				var conteudoBottom_h = parseInt($("#conteudoPrincipal #conteudoBottom").height().replace("px",""));		
			}
			
			if ($("#conteudoPrincipal #myGrid").width()) {
				$("#conteudoPrincipal #myGrid").width(conteudoTop_w-8); 
				$("#conteudoPrincipal #myGrid").height(conteudoTop_h-(24+27+4)); 
			}
			
			if ($("#cssform").width()) {
				if (conteudoBottom_h > 0) {
					$("#conteudoPrincipal #cssform").width(conteudoBottom_w); 
					$("#conteudoPrincipal #cssform").height(conteudoBottom_h-(27)); 
				}
				else {
					$("#conteudoPrincipal #cssform").width(conteudoTop_w); 
					$("#conteudoPrincipal #cssform").height(conteudoTop_h-(27+24)); 
				}
				var cssform_w = parseInt($("#conteudoPrincipal #cssform").width().replace("px",""));
				var cssform_h = parseInt($("#conteudoPrincipal #cssform").height().replace("px",""));	
			}
			
			if ($("#conteudoPrincipal #cssformconteudo").width()) {
				$("#conteudoPrincipal #cssformconteudo").width(cssform_w-(17)); //- 17px da margem
				$("#conteudoPrincipal #cssformconteudo").height(cssform_h);
			}
			return;
			
		},
	submitForm:
		function(form, div) {
			var data = $(form).serialize();
			$.ajax( {
				url: $(form).attr("action"),
				type: $(form).attr("method"),
				data: data,
				cache: false,
				async: true,
				beforeSend: function(xmlhttprequest){
					$('#loading').show();
				},
				complete: function(xmlhttprequest) {
					$('#loading').hide();
					FivecomUtil.setSize();
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					$('#loading').hide();
					FivecomUtil.setSize();
				},
				success: function(html){
					$('#loading').hide();
					if (div == "#MsgBox")
						FivecomUtil.createMsgBox("MsgBox", 400, 300);
					$(div).html(html);
				}
			});
		},
	loadHtml:
		function(Url, div, resize) {
			if (resize == undefined)
				resize = true;
				
			$.ajax({
				url: Url,
				type: "GET",
				cache: false,
				async: true,
				beforeSend: function(xmlhttprequest){
					$('#loading').show();
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
				  
				},
				complete: function(xmlhttprequest){
					$('#loading').hide();
					if (resize == true)
						FivecomUtil.setSize();
				},
				success: function(html){
					$(div).html(html);
					if (resize == true)
						FivecomUtil.setSize();
					$('#loading').hide();
				}
			});
		},
	
	openModule:
		function(id, moduleUrl, topUrl, bottomUrl) {
			$("#sistemaPrincipalLeft").html("");
			$("#conteudoTop").html("");
			$("#conteudoBottom").html("");
			if (moduleUrl != "")
				this.splitV(true);
			else
				this.splitV(false);
				
			$('#navBar .ativo').removeClass();
			$('#navBar #' + id).addClass('ativo');
			
			if (moduleUrl != "") {
				this.loadHtml(moduleUrl, '#sistemaPrincipalLeft');
			}
			this.openFunction(topUrl, bottomUrl);
		},
	openSubModule:
	
		function(id, description, image, treeViewUrl, topUrl, bottomUrl) {
			$("#conteudoTop").html("");
			$("#conteudoBottom").html("");
			$('#frameCenterLeftBoxModuloTreeview').hide();
			$('#frameCenterLeftBoxModuloList').show();
			$('#frameCenterLeftBoxModuloToolBar').show();
			if (treeViewUrl != "") {
				$('#frameCenterLeftBoxModuloToolBar li').show();
				$('#frameCenterLeftBoxModuloToolBar #' + id).hide();
				$('#frameCenterLeftBoxModuloList').hide();
				$('#frameCenterLeftBoxModuloTreeview').show();
				this.loadHtml(treeViewUrl, '#frameCenterLeftBoxModuloTreeviewBox');
			}
			else
				$('#frameCenterLeftBoxModuloToolBar').hide();
			$('#frameCenterLeftBox .ativo').removeClass();
			$('#frameCenterLeftBox .cabecalho').html('<img src="' + image + '" alt="' + description + '" width="26" height="26" align="absmiddle" /> ' + description);
			$('#frameCenterLeftBoxModuloToolBar #' + id).addClass('ativo');
			$('#frameCenterLeftBoxModuloList #' + id).addClass('ativo');
			this.openFunction(topUrl, bottomUrl);
		},
	openFunction:
	
		function(topUrl, bottomUrl) {
			$("#conteudoTop").html("");
			$("#conteudoBottom").html("");
			if (bottomUrl != "")
				this.splitH(true);
			else
				this.splitH(false);
			if (topUrl != "")
				this.loadHtml(topUrl, '#conteudoTop');
			else
				$('#conteudoTop').html('');
			if (bottomUrl != "")
				this.loadHtml(bottomUrl, '#conteudoBottom');
			else
				$('#conteudoBottom').html('');
			
			//FivecomUtil.setSize();
		},
	openFunctionBottom:
	
		function(bottomUrl) {
			$("#conteudoBottom").html("");
			if (($("#conteudoPrincipalBottom").css("display") == "none") && (bottomUrl != "")){
				if (bottomUrl != "")
					this.splitH(true);
				else
					this.splitH(false);
			}
			if (bottomUrl != "") {
				this.loadHtml(bottomUrl, '#conteudoBottom');
			}
			//FivecomUtil.setSize();
		},
	getBrowser:
	
		function() {
			if( typeof( window.innerWidth ) == 'number' )
				return 0; //Firefox
			else
				return 1; //IE
		},
	setAllCheckBoxes:
		function (FormName, FieldName, CheckValue) {
			if(!document.forms[FormName])
				return;
		
		
			var objCheckBoxes = document.forms[FormName].elements[FieldName];
			if(!objCheckBoxes)
				return;
		
			var countCheckBoxes = objCheckBoxes.length;
			if(!countCheckBoxes)
				objCheckBoxes.checked = CheckValue;
			else
				// set the check value for all check boxes
				for(var i = 0; i < countCheckBoxes; i++)
					objCheckBoxes[i].checked = CheckValue;
		},
		
	format:
		function (src, mask) {
			var i = src.value.length;
			var saida = mask.substring(0,1);
			var texto = mask.substring(i)
		
			if (texto.substring(0,1) != saida) {
				src.value += texto.substring(0,1);
			}
		},
	searchFast:
		function () {
			this.submitForm("#frmSearch", "#conteudoTop");
			//this.setSize();
		},
	refreshFunction:
		function (url) {
			this.loadHtml(url,'#conteudoTop');
			//this.setSize();
		},
		
	refreshFunctionBottom:
		function (url) {
			this.loadHtml(url,'#conteudoBottom');
			//this.setSize();
		},
		
	clickToolBar:
		function (idForm, evento) {

			eventoOld = $(idForm).get(0).e.value;
			if (evento != "")
				$(idForm).get(0).e.value=evento;
			
			if((evento == "EG") || (evento == "E"))
				if(!confirm("Deseja Excluir o(s) Registro(s) Selecionado(s)?"))
					return false;
			this.submitForm(idForm, "#MsgBox");

			if (evento != "")
				$(idForm).get(0).e.value=eventoOld;


		},
		
	initEditor:
		function (campo) {
			editor = new HTMLArea(campo);
			editor.registerPlugin(SpellChecker);
			editor.generate();
			return false;
		},
	createMsgBox:
		function (id, width, height) {
			
			width = parseInt(width);
			height = parseInt(height);
			
			if (typeof _zindex == "undefined")
				_zindex=0;
			else
				_zindex++;
				
			$('#div' + id).html('');
			$('#div' + id).remove();
			
			var html = '';
			html += '<div id="div' + id + '" style="display:none;">\n';
			html += '        <div style="border:solid 1px #0f6189;z-index:' + (_zindex + 3) + ';position:absolute;background-color:#FFFFFF;overflow:auto;padding: 10px 10px 10px 10px;top:50%;left:50%;height:' + parseInt((parseInt(height) + 0)) + 'px;width:' + width + 'px;margin-left:-' + width/2 + 'px; margin-top:-' + parseInt(height + 0)/2 + 'px;">\n';
			html += '           <div style="position:absolute; width:' + width + 'px; text-align:center; padding:4px 0 4px 0;text-align:right;"><a href="javascript: return false;" onclick="javascript:$(\'#div' + id + '\').remove();"><img src="imagens/ics_exit.gif" alt="" title="Fechar janela"></a></div>\n';
			html += '        	<div id="' + id + '" style="position:absolute;left:10px;height:' + (height-32) + 'px;width:' + width + 'px; top:43px; overflow:auto;"></div>\n';
			html += '        </div>\n';
			html += '    <div style="z-index:50000;width:100%;position:absolute;height:5000px;background-color:#CCCCCC;opacity: .5;filter: Alpha(Opacity=50);-moz-opacity: 0,5;top:0;left:0;z-index:2;" onclick="javascript:$(\'#div' + id + '\').remove();"></div>\n';
			html += '</div>\n';
			html += '<script>\n';
			html += 'window.document.onkeydown = function (e) {\n';
			html += '	var code;\n';
			html += '	if (!e) var e = window.event;\n';
			html += '	if (e.keyCode) code = e.keyCode;\n';
			html += '	else if (e.which) code = e.which;\n';
			html += '	if (code == 27) {\n';
			html += '		$("#div' + id + '").remove();\n';
			html += '		window.document.onkeydown = void(0);\n';
			html += '	}\n';
			html += '}\n';
			html += '</script>\n';
			var div = jQuery(html);
			div.prependTo(document.body);
			
			$('#div' + id).show();
			//$('#div' + id).bgiframe();
		},    
	closeMsgBox:
		function (id) {
			$('#div' + id).html('');
			$('#div' + id).remove();
		},    

	openFloatMatia:
		function (url) {
			FivecomUtil.openFloat('matia', 'Conteudo', url, 800, 600);
		},
		
	openFloat:
		function (id, title, url, width, height) {
			if (typeof _zindex == "undefined")
				_zindex=0;
			else
				_zindex++;
				
			$('#div' + id).html('');
			$('#div' + id).remove();

			var html = '';
			html += '        <style>\n';
			html += '        .jqDnR {\n';
			html += '            position: absolute;\n';
			html += '            top:50%; \n';
			html += '            left:50%\n';
			html += '        }\n';
			html += '        </style>\n';
			html += '        <div id="div' + id + '" style="position:absolute" class="jqDnR">\n';
			html += '            <div style="overflow:hidden; z-index:' + (_zindex + 3) + '; position:absolute; height:' + parseInt(height) + 'px;width:' + parseInt(width) + 'px; margin-top:-' + parseInt(height)/2 + 'px; margin-left:-' + parseInt(width)/2 + 'px; background:#FFFFFF; border:solid 1px #CCCCCC; border-bottom:solid 2px #666666; border-right:solid 2px #666666;">\n';
			html += '                <div style="float:left; width:100%; height:30px; background:url(imagens/bg_icon.png); padding:1px 0 0 0; cursor:move;">\n';
			html += '                    <b style="float:left; padding:3px 0 0 10px;">' + title + '</b><a onClick="javascript:$(\'#div' + id + '\').remove();" href="javascript:%20return%20false;">\n';
			html += '                   	<img title="Fechar janela" alt="" src="imagens/ics_exit.gif" style="float:right; margin:3px 3px 0 0;" /></a>\n';
			html += '                </div>\n';
			height = height - 40;
			width = width - 0;
			html += '                <div style="overflow:auto; position:absolute;left:0px;height:' + height + 'px; width:' + width + 'px; top: 25px;" id="' + id + '">\n';
			html += '                </div>\n';
			html += '            </div>\n';
			html += '        </div>\n';
			/*
			html += '        <script>\n';
			html += '        $(document).ready(function (){\n';
			html += '                $(\'#div' + id + '\').jqDrag();\n';
			html += '            })\n';
			html += '        </script>\n';
			*/
			var div = jQuery(html);
			div.prependTo(document.body);
			
			$('#div' + id).show();

			this.loadHtml(url, '#' + id, false);
			
			/*
			height = height + 50;
			width = width - 200;

			if ($("#" + id + " " + "#myGrid").width()) {
				$("#" + id + " " + "#myGrid").width(width-8); 
				$("#" + id + " " + "#myGrid").height(height-(24+27+4)); 
			}
			var cssform_w = 0;
			var cssform_h = 0;
			
			if ($("#" + id + " " + "#cssform").width()) {
				$("#" + id + " " + "#cssform").width(width); 
				$("#" + id + " " + "#cssform").height(height-(27+24)); 

				cssform_w = parseInt($("#" + id + " " + "#cssform").width().replace("px",""));
				cssform_h = parseInt($("#" + id + " " + "#cssform").height().replace("px",""));	
			}
			
			if ($("#" + id + " " + "#cssformconteudo").width()) {
				$("#" + id + " " + "#cssformconteudo").width(cssform_w-(17)); //- 17px da margem
				$("#" + id + " " + "#cssformconteudo").height(cssform_h);
			}
			*/
		},    
		
	openMsgBox:
		function (id, url, width, height) {
			FivecomUtil.createMsgBox(id, width, height);
			FivecomUtil.loadHtml(url, "#" + id);
		},    
		
	previewFoto:
		function (id, campo) {
			$(id).css("background-image","url("+$(campo).get(0).value+")");	
		},
	onSubmitHtml:
		function (id) {
			window.document.frmSalvar.onsubmit();
		}
};
