/*
'--------------------------------------------------------------------
' Arquivo:		funcoes.js
' Autor:		Gilson Hoffmeister
' E-mail:		gilsonh@feevale.br
' Data:			09/02/2004
' Atualizado:	12/08/2004
' Descrição:	Arquivo que possui todas as funções JavaScript
'				utilizadas pela aplicação.
'--------------------------------------------------------------------
*/

/*
Função para maximizar a janela em qualquer resolução
*/
function pgMaximizar() //v1.0
{
	top.moveTo(0, 0);
	top.resizeTo(screen.availWidth, screen.availHeight);
	self.focus();
}

/*
--------------------------------------------------------------------
 Função para abrir uma janela pop-up com os parametros abaixo
 
 - vlsURL:		Endereço a ser aberto;
 - vlsJanela:	Nome do iframe ou frame a ser aberto;
 - vlnLargura:	Largura em pixels da janela a ser aberta;
 - vlnAltura:	Altura em pixels da janela a ser aberta;
--------------------------------------------------------------------
*/
function pgAbrirJanela(vlsURL,vlsJanela,vlnLargura,vlnAltura) //v3.0
{
	vlnPosX = (screen.width/2)-(vlnLargura/2)-5;
	vlnPosY = (screen.height/2)-(vlnAltura/2)-40;
	vlsFeatures = "status=no,menubar=no,scrollbars=no,resizable=no,width=" + vlnLargura + ",height=" + vlnAltura + ",top=" + vlnPosY + ",left=" + vlnPosX;
	window.open(vlsURL,vlsJanela,vlsFeatures);
}

/*
Função para imprimir a tela selecionada
*/
function fgImprimir()
{
	window.print()
}

/*
Função para voltar pra a página anterior
*/
function fgVoltar()
{
	window.history.back();
}

/*
Função para fechar uma jenela
*/
function fgFechar()
{
	window.close();
}

/*
Função para criar arrays
*/

function CriaArray (tam)
{
  this.length = tam;
}

/*
Função para eliminar os espaços no início e/ou final de uma string
*/
function pgTrim(txtString)
{
	if (txtString != "" && txtString != null)
	{
		while (((txtString.indexOf(" ") == 0) && (txtString.indexOf(" ") != -1)) || ((txtString.lastIndexOf(" ") == txtString.length-1) && (txtString.lastIndexOf(" ") != -1)))
		{
			txtString = txtString.replace(/^ /,"");
			txtString = txtString.replace(/ $/,"");
		}		
	}
	return(txtString);
}

function pgValidarEmail(txtEmail)
{
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]?&'%~#´`"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	var matchArray=txtEmail.match(emailPat)
	if (matchArray==null) {
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	if (user.match(userPat)==null) {
	    return false
	}
	
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
			return false
		    }
	    }
	    return true
	}
	
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
	    return false
	}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
	   return false
	}
	
	if (len<2) {
	   return false
	}
	
	return true;
}

function pgValidarContato(objForm) {

	var txtAviso = "Por favor, preencha o(s) campo(s):\n\n";
	var txtErro = ""

	if (pgTrim(objForm.txtMensagem.value) == "") {
		txtErro = "- Comentários\n" + txtErro;
		objForm.txtMensagem.focus();
	}

	if (pgTrim(objForm.txtEstado.value) == "") {
		txtErro = "- Estado\n" + txtErro;
		objForm.txtEstado.focus();
	}

	if (pgTrim(objForm.txtCidade.value) == "") {
		txtErro = "- Cidade\n" + txtErro;
		objForm.txtCidade.focus();
	}

	if (pgTrim(objForm.txtTelefone.value) == "") {
		txtErro = "- Fone\n" + txtErro;
		objForm.txtTelefone.focus();
	}

	if (pgTrim(objForm.txtEmail.value) == "") {
		txtErro = "- E-mail\n" + txtErro;
		objForm.txtEmail.focus();
	} else {
		if (!pgValidarEmail(objForm.txtEmail.value)) {
			txtErro = txtErro + "\n E verifique o(s) seguinte(s) erro(s):\n\n- E-mail incorreto";
			objForm.txtEmail.focus();
		}
	}

	if (pgTrim(objForm.txtNome.value) == "") {
		txtErro = "- Seu nome\n" +  txtErro;
		objForm.txtNome.focus();
	}

	if (txtErro != "") {
		txtAviso = txtAviso + txtErro;
		alert(txtAviso);
		return(false);
	}
}

function pgValidarContatoEnglish(objForm) {

	var txtAviso = "Please, fill the following:\n\n";
	var txtErro = ""

	if (pgTrim(objForm.txtMensagem.value) == "") {
		txtErro = "- Comments\n" + txtErro;
		objForm.txtMensagem.focus();
	}

	if (pgTrim(objForm.txtEstado.value) == "") {
		txtErro = "- State\n" + txtErro;
		objForm.txtEstado.focus();
	}

	if (pgTrim(objForm.txtCidade.value) == "") {
		txtErro = "- City\n" + txtErro;
		objForm.txtCidade.focus();
	}

	if (pgTrim(objForm.txtTelefone.value) == "") {
		txtErro = "- Phone\n" + txtErro;
		objForm.txtTelefone.focus();
	}

	if (pgTrim(objForm.txtEmail.value) == "") {
		txtErro = "- E-mail\n" + txtErro;
		objForm.txtEmail.focus();
	} else {
		if (!pgValidarEmail(objForm.txtEmail.value)) {
			txtErro = txtErro + "\n And verify the following errors(s):\n\n- Incorrect E-mail";
			objForm.txtEmail.focus();
		}
	}

	if (pgTrim(objForm.txtNome.value) == "") {
		txtErro = "- Your name\n" +  txtErro;
		objForm.txtNome.focus();
	}

	if (txtErro != "") {
		txtAviso = txtAviso + txtErro;
		alert(txtAviso);
		return(false);
	}
}