function NumReal(field,milSep,decSep,dec) { 
				var numCaract = 0;
				var newNum = '';
				var jn = /[^[0-9]]*/gi;
				var num = field.value.replace(jn,'').replace(/^0/,'');
				if(num.length <= dec)
					for(var cont = 0; num.length <= dec; cont++)
						num = '0' + num;
				for(cont = num.length - 1; cont >= 0; cont--) {
					newNum = num.substr(cont,1) + newNum;
					if(dec > 0 && numCaract == dec - 1)
						newNum = decSep + newNum;
					else if(((numCaract - dec) % 3 == 2) && (numCaract < num.length-1))
						newNum = milSep + newNum;
					numCaract++;
				}
				if(newNum.substr(0,1) == ',')
					newNum = '0' + newNum;
				field.value = newNum;
}
/*
	Tipo:
		1 - CPF
		2 - CNPJ
		3 - CEP
*/
function tiraMask(tipo,campo){
	if(tipo==1){
		$('#'+campo).mask("99999999999");
	}else if(tipo==2){
		$('#'+campo).mask("99999999999999");
	}else if(tipo==3){
		$('#'+campo).mask("99999999");
	}
}
String.prototype.replaceAll = function(token, newtoken) {

                var str = this;

                while (str.indexOf(token) != -1)

                               str = str.replace(token, newtoken);

                return str;

}
function getValidCNPJCPF(valor,tipo){
	if(tipo==1){
		var ok = validarCPF(valor);
		if(ok==false){
			alert("CPF inválido");
		}
	}else{
		var ok = validarCNPJ(valor);
		if(ok==false){
			alert("CNPJ inválido");
		}
	}
	return ok;
}

function validarCPF(cpf) {

                if (cpf.indexOf(".")!=-1) {
                               cpf = cpf.replaceAll(".","");
                               cpf = cpf.replaceAll("-",""); 
                }
                var numeros, digitos, soma, i, resultado, digitos_iguais;
                digitos_iguais = 1;
                if (cpf.length < 11)
                    return false;
                for (i = 0; i < cpf.length - 1; i++)
                           if (cpf.charAt(i) != cpf.charAt(i + 1)) {
                                digitos_iguais = 0;
                                break;
                           }
                if (!digitos_iguais) {
								numeros = cpf.substring(0,9);
								digitos = cpf.substring(9);

                               soma = 0;
                               for (i = 10; i > 1; i--)
                                               soma += numeros.charAt(10 - i) * i;
                               resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
                               if (resultado != digitos.charAt(0))
                                               return false;
                               numeros = cpf.substring(0,10);
                               soma = 0;
                               for (i = 11; i > 1; i--)
                                            soma += numeros.charAt(11 - i) * i;
                               resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
                               if (resultado != digitos.charAt(1))
                                               return false;
                               return true;
                } else
                    return false;
}

 

function validarCNPJ(cnpj) {

                if (cnpj.indexOf(".")!=-1) {
                               cnpj = cnpj.replaceAll(".","");
                               cnpj = cnpj.replaceAll("-",""); 
                               cnpj = cnpj.replaceAll("/",""); 
                }
				var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
                digitos_iguais = 1;
                if (cnpj.length < 14 && cnpj.length < 15)
                               return false;
                for (i = 0; i < cnpj.length - 1; i++)
                               if (cnpj.charAt(i) != cnpj.charAt(i + 1)) {
                                               digitos_iguais = 0;
                                               break;
                               }
                if (!digitos_iguais) {
                               tamanho = cnpj.length - 2
                               numeros = cnpj.substring(0,tamanho);
                               digitos = cnpj.substring(tamanho);
                               soma = 0;
                               pos = tamanho - 7;
                               for (i = tamanho; i >= 1; i--) {
                                               soma += numeros.charAt(tamanho - i) * pos--;
                                               if (pos < 2)
                                                pos = 9;
                               }
                               resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
                               if (resultado != digitos.charAt(0))
                                    return false;
                               tamanho = tamanho + 1;
                               numeros = cnpj.substring(0,tamanho);
                               soma = 0;
                               pos = tamanho - 7;
                               for (i = tamanho; i >= 1; i--) {
                                    soma += numeros.charAt(tamanho - i) * pos--;
                                    if (pos < 2)
										pos = 9;
                               }
                               resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
                               if (resultado != digitos.charAt(1))
                                    return false;
                               return true;
                } else
                    return false;

}

function ajaxPagina(url,div) {
	load_div("#"+div);
	$.ajaxSetup ({ 
		cache: false 
	});
	$("#"+div).load(url,function(){$(":text:enabled:first").focus()});
}
function load_div(div) {

	$(div).html("<center><img src=\"http: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() {
	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;
}

