﻿//Notificações (avisos de confirmação, falha e outros)
function ExibirNotificacao(pMensagem, pTipo) {
	var vHeader;
	switch (pTipo) {
		case "C":
			{
				vHeader = "Confirmação";
				if ((pMensagem == null) || (pMensagem == "")) {
					pMensagem = "<p>Procedimento efetuado com sucesso.</p>";
				}
				break;
			}
		case "F":
			{
				vHeader = "Falha não tratada";
				if ((pMensagem == null) || (pMensagem == "")) {
					pMensagem = "<p>Uma falha não tratada foi encontrada, uma mensagem de e-mail foi enviada ao núcleo de desenvolvimento responsável com as informações necessárias.</p>";
				}
				break;
			}
		case "M":
			{
				vHeader = "E-mail enviado com sucesso";
				if ((pMensagem == null) || (pMensagem == "")) {
					pMensagem = "<p>Mensagem enviada com sucesso, obrigado.</p><p>Se necessário estaremos entrando em contato o mais breve possível.</p>";
				}
				break;
			}
		case "SE":
			{
				vHeader = "Sessão expirada";
				break;
			}
		default:
			{
				vHeader = "Atenção";
				break;
			}
	}
	JGrowlNotificacao(pMensagem, vHeader);
}

function JGrowlNotificacao(pMensagem, vHeader) {
	$.jGrowl(pMensagem, { header: vHeader, sticky: true });
}

function ContarCaracteresRestantesDevExpress(Campo, Limite, Restante) {
	if (Campo.GetText().length > Limite) {
		Campo.SetText(Campo.GetText().substring(0, Limite));
	}
	else {
		Restante.SetText(Limite - Campo.GetText().length);
	}
}

var vJanelaAtendimentoOnLine;
function AbrirJanelaSOL(Url) {
	var vParametros;

	vParametros = "channelmode=no,"
				+ "directories=no,"
				+ "fullscreen=no,"
				+ "top=50,"
				+ "left=50,"
				+ "width=520,"
				+ "height=400,"
				+ "titlebar=no,"
				+ "menubar=no,"
				+ "toolbar=no,"
				+ "location=no,"
				+ "scrollbars=no,"
				+ "status=yes,"
				+ "resizable=yes";

	if (vJanelaAtendimentoOnLine == null) {
		vJanelaAtendimentoOnLine = window.open(Url, "AtendimentoOnline", vParametros);
	}
	else {
		if (!vJanelaAtendimentoOnLine.closed) {
			vJanelaAtendimentoOnLine.focus();
		}
		else {
			vJanelaAtendimentoOnLine = window.open(Url, "AtendimentoOnline", vParametros);
		}
	}

	return false;
}

//Validações dos componentes DevExpress.
function ValidarGrupo(Grupo) {
	var validationProcs = [ExecutarValidacaoDx, ExecutarValidacaoPadrao];
	var result = true;

	for (var index = 0; index < validationProcs.length; index++) {
		result = validationProcs[index](Grupo) && result;
	}

	if (!result) {
		ExibirNotificacao("Alguns dados não foram informados corretamente, verifique os campos em destaque.");
	}

	return result;
}

function ExecutarValidacaoDx(Grupo) {
	if (typeof (ASPxClientEdit) != "undefined" && typeof (ASPxClientEdit.ValidateGroup) == "function") {
		return ASPxClientEdit.ValidateGroup(Grupo);
	}
	else {
		return true;
	}
}

function ExecutarValidacaoPadrao(Grupo) {
	if (typeof (Page_IsValid) != "undefined" && Page_IsValid != null && typeof (Page_ClientValidate) == "function") {
		Page_ClientValidate(Grupo);
		return Page_IsValid;
	}

	return true;
}
//Fim das funções de validação.


function ImprimirPagina() {
	if (window.print) {
		window.print();
	}
	else {
		var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
		WebBrowser1.ExecWB(6, 2); //Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
	}
}

function IniciarImpressaoAutomaticamente(pMensagem) {
	if (navigator.appName == "Microsoft Internet Explorer") {
		ExibirNotificacao(pMensagem);
	}
	else {
		if (window.print) {
			ImprimirPagina();
		}
	}
}

function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
}

function ValidarEmail(Email) {
	var vReEmail = /^([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))$/;
	vRegex = new RegExp(vReEmail);
	if (vRegex.exec(Email) == null) {
		return false;
	}
	else {
		return true;
	}
}

function ValidarNumerico(Numero) {
	var vRegex = /^[1-9]+[0-9]*$/
	if (vRegex.exec(Numero) == null || !vRegex.test(Numero)) {
		return false;
	}
	else {
		return true;
	}
}

//Verifica a existência de um elemento no JS.
function VerificarExistenciaElemento(ElementoNome) {
	if (typeof (window[ElementoNome]) == "undefined") {
		return false;
	}
	else {
		return true;
	}
}

//Verifica a existência e acessibilidade de um elemento no JS.
function VerificarAcessibilideElemento(Elemento) {
	var thisObj = eval("document.getElementById('" + Elemento.name + "')")

	if (thisObj == null) {
		return false;
	}
	else {
		return true;
	}
}

//Verifica a existência de um elemento no JS.
function VerificarExistenciaElementoPopUp(ElementoNome) {
	if (typeof (parent.window[ElementoNome]) == "undefined") {
		return false;
	}
	else {
		return true;
	}
}

//Verifica a existência e acessibilidade de um elemento no JS.
function VerificarAcessibilideElementoPopUp(Elemento) {
	var thisObj = eval("parent.document.getElementById('" + Elemento.name + "')")

	if (thisObj == null) {
		return false;
	}
	else {
		return true;
	}
}

String.prototype.trim = function() {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}