﻿function AdicionarLoadEvent(func) {
	try {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function () {
				oldonload();
				func();
			}
		}
	}
	catch (Error) {

	}
}

function ASPxComboBoxSites_SelectedIndexChanged(s, e) {
	var vUrl = ASPxComboBoxSites.GetValue();

	if ((vUrl != "") && (vUrl != null)) {
		if (vUrl.substring(7, 0).toLowerCase() == "http://") {
			//Sempre que tiver o http:// irá abrir em outra janela
			window.location = vUrl;
		}
	}

	if ((ASPxComboBoxSites.GetSelectedIndex() == 1) || (ASPxComboBoxSites.GetSelectedIndex() == 4)
    || (ASPxComboBoxSites.GetSelectedIndex() == 6) || (ASPxComboBoxSites.GetSelectedIndex() == 9)) {
		ASPxComboBoxSites.SetSelectedIndex(0);
	}
}

function ContarCaracteresRestantesDevExpress(Campo, Limite, Restante) {
	if (Campo.GetText().length > Limite) {
		Campo.SetText(Campo.GetText().substring(0, Limite));
	}
	else {
		Restante.SetText(Limite - Campo.GetText().length);
	}
}

function DefinirFundoConteudo(pImagem) {
	if (!document.getElementById) return false;

	var MeuComponente = "ConteudoPrincipal";
	var CaminhoImagens = "/App_Themes/Comercial2/Imgs/Conteudo/";

	if (!document.getElementById(MeuComponente)) {
		return false;
	}
	else {

		var Componente = document.getElementById(MeuComponente);
		var BackgroundImage = Componente.style.backgroundImage;
		var ImgURL = "url(" + CaminhoImagens + pImagem + ")";

		if (BackgroundImage != ImgURL) {
			Componente.style.backgroundImage = ImgURL;
		}
	}

}

function ExibirPopUpMapaMatriz() {
	ExibirPopUpUrl('Como chegar à nossa matriz', 'http://maps.google.com.br/maps?f=q&amp;source=s_q&amp;hl=pt-BR&amp;geocode=&amp;q=data+cempro+inform%C3%A1tica&amp;sll=-14.179186,-50.449219&amp;sspn=68.70707,113.027344&amp;ie=UTF8&amp;hq=data+cempro+inform%C3%A1tica&amp;hnear=&amp;cid=14456361553986490910&amp;ll=-29.947758,-51.107025&amp;spn=0.013015,0.018239&amp;z=15&amp;iwloc=A&amp;output=embed');
}

function ExibirPopUpUrl(pTitulo, pUrl) {
	if (VerificarExistenciaElemento("ASPxPopupControlGeral")) {
		ASPxPopupControlGeral.Hide();
		ASPxPopupControlGeral.SetContentHtml("");
		ASPxPopupControlGeral.SetHeaderText(pTitulo);
		ASPxPopupControlGeral.SetContentUrl(pUrl);
		ASPxPopupControlGeral.Show();
	}
}

function ExibirPopUpDownload(pTitulo, pUrl) {
	if (VerificarExistenciaElemento("ASPxPopupControlMensagem")) {
		ASPxPopupControlMensagem.Hide();
		ASPxPopupControlMensagem.SetContentHtml("");
		ASPxPopupControlMensagem.SetHeaderText(pTitulo);
		ASPxPopupControlMensagem.SetContentUrl(pUrl);
		ASPxPopupControlMensagem.Show();
	}
}

function ExibirPopUpMensagem(pMensagem, pTipo) {
	if ((pMensagem != "") && (pMensagem != null)) {
		ASPxPopupControlMensagem.Hide();

		vImagem = "<img class='MensagemIco' src='/App_Themes/Comercial2/Imgs/Ico/";
		if (pTipo == "C") {
			ASPxPopupControlMensagem.SetHeaderText("Confirmação");
			vImagem += "Info32.png' alt='Confirmação"
		}
		else if (pTipo == "F") {
			ASPxPopupControlMensagem.SetHeaderText("Falha!");
			vImagem += "Falha32.png' alt='Falha"
		}
		else if (pTipo == "D") {
			ASPxPopupControlMensagem.SetHeaderText("Download");
			vImagem += "Download32.png' alt='Download"
		}
		else {
			ASPxPopupControlMensagem.SetHeaderText("Atenção");
			vImagem += "Alert32.png' alt='Atenção"
		}

		ASPxPopupControlMensagem.SetContentHtml(vImagem + "' /><div class='MensagemTexto'>" + pMensagem + "</div>");
		ASPxPopupControlMensagem.Show();
	}
}

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") {
		window.alert(pMensagem);
	}
	else {
		if (window.print) {
			ImprimirPagina();
		}
	}
}

function OcultarPopUp() {
	if (VerificarExistenciaElemento("ASPxPopupControlGeral")) {
		ASPxPopupControlGeral.SetContentHtml("");
		ASPxPopupControlGeral.SetContentUrl("/LivrePopUp/Aguarde.htm");
		ASPxPopupControlGeral.SetHeaderText("Aguarde...");
		ASPxPopupControlGeral.Hide();
	}
}

function OcultarPopUpMensagem() {
	ASPxPopupControlMensagem.SetContentHtml("");
	ASPxPopupControlMensagem.SetContentUrl("/LivrePopUp/Aguarde.htm");
	ASPxPopupControlMensagem.SetHeaderText("Aguarde...");
	ASPxPopupControlMensagem.Hide();
}

// ------------ FUNCOES GERAIS -----------------//

//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;
	}
}

//Exibe o loadingPanel
function ExibirLoadingPanel(modal) {
	if (modal) {
		ASPxLoadingPanelModal.Show();
	}
	else {
		ASPxLoadingPanel.Show();
	}
}
//Oculta o loadingPanel
function OcultarLoadingPanel(modal) {
	if (modal) {
		ASPxLoadingPanelModal.Hide();
	}
	else {
		ASPxLoadingPanel.Hide();
	}
}

//Verifica a existência de um elemento no JS.
function VerificarExistenciaElemento(ElementoNome) {
	if (typeof (window[ElementoNome]) == "undefined") {
		return false;
	}
	else {
		return true;
	}
}

// ---------- FIM - FUNCOES GERAIS -------------------//

//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) {
		window.alert("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 ConfirmarRecuperarSenha() {
	return confirm('Deseja enviar a senha para o e-mail cadastrado?');
}

function ValidarNumerico(Numero) {
	var vRegex = /^[1-9]+[0-9]*$/
	if (vRegex.exec(Numero) == null || !vRegex.test(Numero)) {
		return false;
	}
	else {
		return true;
	}
}