/*
	Funções para pop-ups
*/

var pWin = null;

function focus_window()
{
	if (pWin != null) 
	{
		pWin.focus();
		pWin = null;
		return;
	}
	setTimeout("focus_window()", 1000);
}

function abre_popup_local( url, nome, largura, altura, posleft, postop )
{
	var features = 'fullscreen=0,toolbar=0,statusbar=0,directories=0,'
	
	features += 'location=0,menubar=0,resizable=1,width=' + largura + ','
	features += 'height=' + altura + ',top=' + postop + ',left=' + posleft;
	
	var newWin = window.open(url, nome, features);

	newWin.resizeTo( largura, altura );
	newWin.moveTo( posleft, postop );

	pWin = newWin;
	setTimeout("focus_window()", 10);			
}

function abre_popup_local_com_scrollbars( url, nome, largura, altura, posleft, postop )
{
	var features = 'fullscreen=0,scrollbars=1,toolbar=0,statusbar=0,directories=0,'
	
	features += 'location=0,menubar=0,resizable=1,width=' + largura + ','
	features += 'height=' + altura + ',top=' + postop + ',left=' + posleft;
	
	var newWin = window.open(url, nome, features);

	newWin.resizeTo( largura, altura );
	newWin.moveTo( posleft, postop );

	pWin = newWin;
	setTimeout("focus_window()", 10);			
}


function abre_popup_mouse( url, nome, largura, altura )
{
	var posleft = event.screenX;
	var postop = event.screenY;

	abre_popup_local(url, nome, largura, altura, posleft, postop);
}

function abre_popup_centro( url, nome, largura, altura )
{
	var posleft = (screen.availWidth - largura) / 2;
	var postop = (screen.availHeight - altura) / 2;
	
	abre_popup_local(url, nome, largura, altura, posleft, postop);
}


function abre_popup_centro_com_scrollbars( url, nome, largura, altura )
{
	var posleft = (screen.availWidth - largura) / 2;
	var postop = (screen.availHeight - altura) / 2;
	
	abre_popup_local_com_scrollbars(url, nome, largura, altura, posleft, postop);
}










/*
	Funções Comuns
	Desenvolvedor: Juliano Nunes
*/


/*
*************************************************************************
Função para verificação da Data
Verifica quantidade de dias, se o ano é bissexto e se a data está no formato dd/mm/aaaa
Juliano Nunes
*************************************************************************
*/

function ValidacaoData(strData)
{
	if( (strData.length != 10) || (isNaN(strData.substring(0,2))) || (strData.substring(2,3) != "/") || (isNaN(strData.substring(3,5))) || (strData.substring(5,6) != "/") || (isNaN(strData.substring(6,10))) )
	{	
		MostraMensagemData();
		return false;
	}
	else
	{
		if((strData.substring(3,5) > 0) && (strData.substring(3,5) <= 12))
		{
			switch(strData.substring(3,5))
			{
				case "01":
				case "03":
				case "05":
				case "07":
				case "08":
				case "10":
				case "12":
				{
					if(strData.substring(0,2) < 1)
					{
						alert("O primeiro dia do mês é 01.");
						return false;
					}
					else if(strData.substring(0,2) > 31)
					{
						alert("O mês de " + RetornaMes(strData.substring(3,5)) + " tem 31 dias.");
						return false;
					}
					break;
				}
				default:
				{
					if(strData.substring(3,5) == "02")
					{
						if(strData.substring(6,10) % 2 == 0)
						{
							if(strData.substring(0,2) < 1)
							{
								alert("O primeiro dia do mês é 01.");
								return false;
							}
							else if(strData.substring(0,2) > 29)
							{
								alert("O ano de " + strData.substring(6,10) + " é um ano bissexto, portanto o mês de fevereiro tem 29 dias.");
								return false;
							}
						}
						else
						{
							if(strData.substring(0,2) < 1)
							{
								alert("O primeiro dia do mês é 01.");
								return false;
							}
							else if(strData.substring(0,2) > 28)
							{
								alert("O mês de " + RetornaMes(strData.substring(3,5)) + " tem 28 dias.");
								return false;
							}
						}
					}
					else
					{
						if(strData.substring(0,2) < 1)
						{
							alert("O primeiro dia do mês é 01.");
							return false;
						}
						else if(strData.substring(0,2) > 30)
						{
							alert("O mês de " + RetornaMes(strData.substring(3,5)) + " tem 30 dias.");
							return false;
						}
					}
					break;
				}
			}
			return true;
		}
		else
		{
			alert("O mês deve ser entre 01 e 12.");
			return false;
		}
	}
}
/*
*************************************************************************
*/
			
			
/*
*************************************************************************
Função que retorna o nome do Mês em português
Juliano Nunes
*************************************************************************
*/
function RetornaMes(strMes)
{
	switch(strMes)
	{
		case "01":
		{
			return "Janeiro";
			break;
		}
		case "02":
		{
			return "Fevereiro";
			break;
		}
		case "03":
		{
			return "Março";
			break;
		}
		case "04":
		{
			return "Abril";
			break;
		}
		case "05":
		{
			return "Maio";
			break;
		}
		case "06":
		{
			return "Junho";
			break;
		}
		case "07":
		{
			return "Julho";
			break;
		}
		case "08":
		{
			return "Agosto";
			break;
		}
		case "09":
		{
			return "Setembro";
			break;
		}
		case "10":
		{
			return "Outubro";
			break;
		}
		case "11":
		{
			return "Novembro";
			break;
		}
		default:
		{
			return "Dezembro";
			break;
		}
	}
}
/*
*************************************************************************
*/
			
function MostraMensagemData()
{
	alert("O formato da data deve ser dd/mm/aaaa." + String.fromCharCode(13) + String.fromCharCode(13) + "dd -> dia com 2 dígitos" + String.fromCharCode(13) + "mm -> mês com 2 dígitos" + String.fromCharCode(13) + "aaaa -> ano com 4 dígitos" + String.fromCharCode(13) + String.fromCharCode(13) + "Exemplo: " + String.fromCharCode(13) + "                      01/01/2004" + String.fromCharCode(13) + String.fromCharCode(13) + "É importante manter os zeros.");
}


/*
*************************************************************************
Função validação hora no formato hh:mm
Juliano Nunes
*************************************************************************
*/
function ValidacaoHora(strHora)
{
	if(strHora.length == 5)
	{
		if(isNaN(strHora.substring(0,2)))
		{
			MostraMensagemHora();
			return false;
		}
		else if(strHora.substring(2,3) != ":")
		{
			MostraMensagemHora();
			return false;
		}
		else if(isNaN(strHora.substring(3,5)))
		{
			MostraMensagemHora();
			return false;
		}
		else if((strHora.substring(0,2) < 00) || (strHora.substring(0,2) > 23))
		{
			alert("As horas vão de 00 a 23.");
			return false;
		}
		else if((strHora.substring(3,5) < 00) || (strHora.substring(3,5) > 59))
		{
			alert("Os minutos vão de 00 a 59.");
			return false;
		}
		return true;
	}
	else
	{
		MostraMensagemHora();
		return false;
	}
}
/*
*************************************************************************
*/


function MostraMensagemHora()
{
	alert("O formato da hora deve ser hh:mm." + String.fromCharCode(13) + String.fromCharCode(13) + "hh -> hora com 2 dígitos" + String.fromCharCode(13) + "mm -> minutos com 2 dígitos" + String.fromCharCode(13) + "Exemplo: " + String.fromCharCode(13) + "                      06:05" + String.fromCharCode(13) + String.fromCharCode(13) + "É importante manter os zeros.");
}


	function ltrim(strTexto) {
		if(strTexto.substr(0,1) == " ") {
			strTexto = strTexto.substr(1,strTexto.length-1);
			strTexto = ltrim(strTexto);
		}
		return strTexto;
	}
	
	function rtrim(strTexto) {
		if(strTexto.substr(strTexto.length-1,1) == " ") {
			strTexto = strTexto.substr(0,strTexto.length-1);
			strTexto = rtrim(strTexto);
		}
		return strTexto;
	}
	
	function trim(strTexto) {
		strTexto = rtrim(ltrim(strTexto));
		return strTexto;
	}
	
	function verifica_espaco_esquerda(strTexto) {
		if(strTexto.substr(0,1) == " ") {
			return false;	
		}
		return true; //Ok
	}
	
	function verifica_espaco_direita(strTexto) {
		if(strTexto.substr(strTexto.length-1,1) == " ") {
			return false;
		}
		return true; //Ok
	}
	
	function verifica_espaco(strTexto) {
		if(verifica_espaco_esquerda(strTexto) == false || verifica_espaco_direita(strTexto) == false)
			return false;
		
		return true; //Ok
	}
	
	function verifica_espaco_toda_string(strTexto) {
		if(strTexto.indexOf(" ",0) >= 0) 
			return false; //Contém espaços
		else
			return true; //Não contém espaços
	}
	
	function ConfirmarExclusao(){
		return confirm('Deseja realmente excluir o(s) registro(s) em questão?')		
	}
	

//***************************************************************************************
// Função: muda_campo																	*
// Parâmetros:																			*
//	- formulario																		*
//  - campo_atual																		*
//  - proximo_campo																		*
//  - tamanho																			*
//***************************************************************************************
// Data de desenvolvimento: 03/03/2005													*
// Desenvolvedor: Juliano Nunes															*
//***************************************************************************************
function muda_campo(formulario,campo_atual,proximo_campo,tamanho) {
	if((event.keyCode != 16) && (event.keyCode != 46) && (event.keyCode != 8) && (event.keyCode != 36) && (event.keyCode != 35) && (event.keyCode != 9)) {
		if(document.forms(formulario).item(campo_atual).value.length == tamanho) {
			document.forms(formulario).item(proximo_campo).focus();
		}
	}
}
//***************************************************************************************

/*
*************************************************************************
Função validação hora no formato hh:mm
Juliano Nunes
*************************************************************************
*/
function ValidacaoHora(strHora)
{
	if(strHora.length == 5)
	{
		if(isNaN(strHora.substring(0,2)))
		{
			MostraMensagemHora();
			return false;
		}
		else if(strHora.substring(2,3) != ":")
		{
			MostraMensagemHora();
			return false;
		}
		else if(isNaN(strHora.substring(3,5)))
		{
			MostraMensagemHora();
			return false;
		}
		else if((strHora.substring(0,2) < 00) || (strHora.substring(0,2) > 23))
		{
			alert("As horas vão de 00 a 23.");
			return false;
		}
		else if((strHora.substring(3,5) < 00) || (strHora.substring(3,5) > 59))
		{
			alert("Os minutos vão de 00 a 59.");
			return false;
		}
		return true;
	}
	else
	{
		MostraMensagemHora();
		return false;
	}
}
/*
*************************************************************************
*/


function MostraMensagemHora()
{
	alert("O formato da hora deve ser hh:mm." + String.fromCharCode(13) + String.fromCharCode(13) + "hh -> hora com 2 dígitos" + String.fromCharCode(13) + "mm -> minutos com 2 dígitos" + String.fromCharCode(13) + "Exemplo: " + String.fromCharCode(13) + "                      06:05" + String.fromCharCode(13) + String.fromCharCode(13) + "É importante manter os zeros.");
}


function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
     var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

     if(document.all) { // Internet Explorer
       nTecla = evtKeyPress.keyCode; }
     else if(document.layers) { // Nestcape
       nTecla = evtKeyPress.which;
     }

     sValue = objForm[strField].value;

     // Limpa todos os caracteres de formatação que
     // já estiverem no campo.
     
     sValue = sValue.toString().replace( "/", "" );
     sValue = sValue.toString().replace( "/", "" );
    
     fldLen = sValue.length;
     mskLen = sMask.length;

     i = 0;
     nCount = 0;
     sCod = "";
     mskLen = fldLen;

     while (i <= mskLen) {
       bolMask = (sMask.charAt(i) == "/")
       if (bolMask) {
         sCod += sMask.charAt(i);
         mskLen++; }
       else {
         sCod += sValue.charAt(nCount);
         nCount++;
       }

       i++;
     }

     objForm[strField].value = sCod;

     if (nTecla != 8) { // backspace
       if (sMask.charAt(i-1) == "9") { // apenas números...
         return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
       else { // qualquer caracter...
         return true;
       } }
     else {
       return true;
     }
   }
//Fim da Função Máscaras Gerais

function SomenteNumeros(e) {
	if (window.event) //IE
	{
		tecla = e.keyCode;
	}
	else if (e.which) //FF
	{
	tecla = e.which;
	}
	//techa==8 é para permitir o backspace funcionar para apagar
	if ((tecla >= 48 && tecla <= 57) || (tecla == 8) || (tecla == 44)) {
		return true;
	}
	else {
		return false;
	}
}
