function ajaxPagina(url,div) {
	$.ajaxSetup ({ 
		cache: false 
	});
	$("#"+div).load(url,function(){$(":text:enabled:first").focus()});
}
function load_div(div) {
	$(div).html("<center><img src=\"imagens/carregando.gif\" width=\"95\" height=\"20\" /></center>");	
}

function abrePopup(url, altura, largura) {
	window.open(url,'','height='+altura+',width='+largura+',top=0,left=0,status=yes,scrollbars=yes,resizable=yes');	
}

function startValidar(pCampos) {
	if (campos=="")
		return true;
	var campos = pCampos.split(",");
	var tot = campos.length;
	var error=false;

	var n = navigator.appVersion;
	var i = n.indexOf("MSIE")
	var nav = n.substr(i,6);

	for (a=0; a < tot; a++) {
		if (campos[a]=="") 
			continue;
		objCampo = document.getElementById(campos[a]);
		
		if (objCampo.value==null || objCampo.value=="" || objCampo.value=="0,00") {
			
			objCampo.style.border="1px solid #FF0000";
			if ( (nav=="MSIE 7"||nav=="MSIE 6") && objCampo.type.indexOf("select")!=-1) {
				objCampo.style.backgroundColor="#FF0000";
			}
			error = true;
		
		} else {
			
			objCampo.style.border="1px solid #999999";
			if ( (nav=="MSIE 7"||nav=="MSIE 6") && objCampo.type.indexOf("select")!=-1)  {
				objCampo.style.backgroundColor="#FFF";
			}
		}
	
	}	
	if (error==true) {
		alert("Preencha todos os campos destacados em vermelho.");
		return false;
	}
	return true;
}

/***********************************************************************************
* Função de Salvar
*
* parametros: caminhoSalvar -> url onde está a função salvar
*			  dados -> parametros que vão ser inseridos
*			  caminhoRetorno -> url onde vai retornar a pagina depois de salvo
*			  msg -> [true] mostrar msg, [false] não mostra mensagem
*
*  retorno: retorna variavel de retorno
************************************************************************************/
function salvarForm(caminhoSalvar, dados, caminhoRetorno, msg) {
	
	var retorno="";
	var confere=0;
	var erro="";
	
	$.ajax({ 
			type:"POST"
			, cache: false 
			, url:caminhoSalvar
			, datatype:"html"
			, data:dados
			, async:false
			, success:function(data) { 
						confere=1;		  	
						if (caminhoRetorno!="" && caminhoRetorno!="undefined") {
							$("#conteudo").load(caminhoRetorno);
						}
						if (data==-1) {
							confere=0;
						}
						retorno=data;
						
			  		  }
			 ,error : function(XMLHttpRequest) {
						 erro = XMLHttpRequest.responseText;
			 		  }
	});

	if (confere==1 && msg==true)
		alert("Salvo com Sucesso!");
	else if (confere==0 && erro!="")
		alert("Houve erro ao Salvar!\n------------------------------------\n: "+erro);
	return retorno;
	
}

function limparForm(idForm) {
	$("#"+idForm).find(':input').each(function() {
		switch(this.type) {
			case 'password':
			case 'select-multiple':
			case 'select-one':
			case 'text':
			case 'textarea':
				$(this).val('');
				break;
			case 'checkbox':
			case 'radio':
				this.checked = false;
		}
    });
	$(":text:enabled:first").focus();
}

function excluirForm(caminho, dados) {
	$.ajax({ 
		type:"GET"
		, cache: false 
		, url:caminho
		, datatype:"html"
		, data:dados
		, success :function( data ) { 
			if (data==1)
				return 1;
		  }
	});
}


function getValor(caminho, dados) {

	$.ajax({ 
			type:"GET"
			, cache: false 
			, url:caminho
			, datatype:"html"
			, data:dados
			, async:false
			, success : function(txt) { 
							retorno = txt;
			  			}
			 , error : function(data) {
				 	   		alert("Houve erro ao Buscar!");
			 		   }
		  });
	return retorno;
}

function abreRelatorio(url) {
	var largura = screen.width;
	var altura = screen.availHeight;
	var dominio_interno = "www.satc.edu.br";
	var dominio_externo = "www.satcweb.edu.br"
	if (getDominio()==dominio_interno)
		window.open("http://"+dominio_interno+"/portais/satcedu_desenv/lib/relatorio/relatorio.asp?url="+url,'','height='+altura+',width='+largura+',top=0,left=0,status=yes,scrollbars=yes,resizable=yes');	
	else if (getDominio()==dominio_externo)
		window.open("http://"+dominio_externo+"/satcedu/lib/relatorio/relatorio.asp?url="+url,'','height='+altura+',width='+largura+',top=0,left=0,status=yes,scrollbars=yes,resizable=yes');	
}

String.prototype.replaceAll = function(token, newtoken) {
	var str = this;
	while (str.indexOf(token) != -1)
 		str = str.replace(token, newtoken);
	return str;
}

/****************************************************************
*	Formata valor para salvar no banco de dados
*
*	@parametros: tipo	1 - transforma de 1.000,00 p/ 1000.00
*				 
****************************************************************/
String.prototype.formataMoeda = function(tipo) {
	var valor = this.replaceAll(".","");
	return valor.replace(",",".")
}

function removerDivs() {
	var bgBody = document.getElementById('bgBody');
	if (bgBody)
		bgBody.parentNode.removeChild(bgBody);
}

function getPageSize(){

	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	

	if(yScroll < windowHeight)
		pageHeight = windowHeight;
	else
		pageHeight = yScroll;

	if(xScroll < windowWidth)
		pageWidth = windowWidth;
	else
		pageWidth = xScroll;

	return arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
}

function loading(opt, path) {
	if (opt == true) {
		if (path=="undefined" || path==null) 
			path = "";
		var refer = document.getElementById('bgBody');
		var referHeight = refer.offsetHeight;
		refer.style.textAlign = 'center';
		var img = document.createElement('img');
		img.setAttribute('src',path+'img/load.gif');
		img.setAttribute('id','loading');
		//img.style.marginTop = (referHeight /2) + 'px';
		img.style.marginTop = "20%";
		if (!document.getElementById('loading')) {
			refer.insertBefore(img, refer.firstChild);
		}
	} else if (opt == false) {
		var imgLoading = document.getElementById('loading');
		if (imgLoading) {
			imgLoading.parentNode.removeChild(imgLoading);
		}
	}
}

function exibirBgBody() {
	var tagBody = document.getElementsByTagName("body").item(0);
	var sizesPage = getPageSize();
	var bgBody = document.createElement('div');
	bgBody.setAttribute("id","bgBody");
	bgBody.style.height = arrayPageSize[1] + "px";
	bgBody.style.width = arrayPageSize[0] + "px";
	if (!document.getElementById("bgBody"))
		tagBody.insertBefore(bgBody, tagBody.firstChild);
}

/****************************************************************
*	Pega dominio da URL
****************************************************************/
function getDominio() {
	var url = location.href;
	url = url.split("/");
	return url[2];
}

/****************************************************************
*	Tras pagina dentro do div conteudo
****************************************************************/
function getPagina(caminho) {
	alert("caminho = "+caminho);
	return false;
	var retorno = "";
	$.ajax({ 
			type:"GET"
			, cache: false 
			, url:caminho
			, datatype:"html"
			, async:false
			, success : function(XMLHttpRequest) { 
							retorno = XMLHttpRequest.responseText;
			  			}
			 , error : function(data) {
				 	   		alert("Houve erro ao Buscar!");
			 		   }
		  });
	return retorno;
}

