function $(id) {
	return document.getElementById(id);
}

function mostraTermos() {
	pulatela('http://web2.egestor.com.br/contrato_egestor.html');
}

function preenche_login(x) {
	if(x.value.indexOf(' ') == -1) document.getElementById('loginegec').value = x.value.toLowerCase();
}

function veriemail(x) {
	if ((x == "") ||
		(x.indexOf("@") < 1) ||
		(x.indexOf(".") < 1) ||
		(x.indexOf("@.") > -1) ||
		(x.indexOf(".@") > -1) ||
		(x.indexOf(" ") > -1) ||
		(x.indexOf(",") > -1)) {
		return false;
	} else {
		return true;
	}
}


function veri_form_contato(x) {
	if(x.nome.value == '') {
		alert('Por favor, preencha seu nome.');
		x.nome.select();
		return false;
	} else if(!veriemail(x.email.value)) {
		alert('Por favor, informe corretamente seu e-mail.');
		x.email.select();
		return false;
	} else if(x.mensagem.value == '') {
		alert('Por favor, escreva sua mensagem antes de clicar no botão.');
		x.mensagem.select();
		return false;
	} else {
		return true;
	}
}

function veri_form_cadastro(x) {
	if(!vericpfcgc(x.cpfeg)) {
		alert('CPF ou CNPJ inválido.');
		x.cpfeg.select();
		return false;
	} else if(x.razao.value == '') {
		alert('Por favor, preencha seu nome ou Razão Social de sua empresa.');
		x.razao.select();
		return false;
 	} else if(x.fone.value == '') {
		alert('Por favor, informe corretamente seu telefone com DDD.');
		x.fone.select();
		return false;
 	} else if(!veriemail(x.email1.value)) {
		alert('Por favor, informe corretamente seu e-mail.');
		x.email1.select();
		return false;
 	} else if(x.loginege.value == '') {
		alert('Por favor, preencha um login válido.');
		x.loginege.select();
		return false;
 	} else if(x.senhaege.value == '') {
		alert('Por favor, preencha uma senha válida.');
		x.senhaege.select();
		return false;
 	} else if(x.senhaege.value != x.senhaege2.value) {
		alert('A senha digitada não confere com a senha repetida.');
		x.senhaege.select();
		return false;
	} else if(x.aceitoTermos.checked == false) {
		alert('Para concluir seu cadastro, é obrigatório que você aceite os Termos de Utilização do sistema.');
		x.aceitoTermos.select();
		return false;
	}
	return true;
}


function vericpf(x) {
	var CPF = x.value;
	if(CPF=='') var faiado = true;
	else {
		if(CPF == "00000000000" || CPF == "11111111111" ||
			CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
			CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
			CPF == "88888888888" || CPF == "99999999999" || CPF == "00000000000") {
			var faiado = true;
		}
		soma = 0;
		for(i=0; i<9; i++) soma += parseInt(CPF.charAt(i)) * (10-i);
		resto = 11 - (soma % 11);
		if (resto == 10 || resto == 11) resto = 0;
		if (resto != parseInt(CPF.charAt(9))) var faiado = true;
		soma = 0;
		for(i=0; i<10; i++) soma += parseInt(CPF.charAt(i)) * (11-i);
		resto = 11-(soma % 11);
		if (resto == 10 || resto == 11) resto = 0;
		if (resto != parseInt(CPF.charAt(10))) var faiado = true;
	}
	if(faiado) return false;
	else return true;
}

function vericgc(x) {
	var s = x.value;
	var i;
	if(s.length == "15") s = s.substr(1,14);
	if(s == '00000000000000') var faiado = true;
	else {
		var c = s.substr(0,12);
		var dv = s.substr(12,2);
		var d1 = 0;
		for (i = 0; i < 12; i++) d1 += c.charAt(11-i)*(2+(i % 8));
		if (d1 == 0) var faiado = true;
		d1 = 11 - (d1 % 11);
		if (d1 > 9) d1 = 0;
		if(dv.charAt(0) != d1) var faiado = true;
		d1 *= 2;
		for (i = 0; i < 12; i++) d1 += c.charAt(11-i)*(2+((i+1) % 8));
		d1 = 11 - (d1 % 11);
		if (d1 > 9) d1 = 0;
		if (dv.charAt(1) != d1) var faiado = true;
	}
	if(faiado) return false;
	else return true;
}


function vericpfcgc(x) {
	if (x.value.length <= 13) return vericpf(x);
	else return vericgc(x);
}

//Limitador de Caracteres
//Tem como função limitar que as pessoas só possam digitar os caracteres permitidos pelo programador.
//por Vini L.Azev. - favor manter os créditos
function limitachars(z, y) {
    x = z.value;
    y = y.toUpperCase();
    y = y.replace('A-Z', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
    y = y.replace('0-9', '1234567890');
    var chars_permitidos = y;
    var resposta = '';
    for(ii=0; ii<x.length; ii++) {
        if ((chars_permitidos.indexOf(x.charAt(ii).toUpperCase()) > -1) || (chars_permitidos.indexOf(x.charAt(ii).toLowerCase()) > -1)) {
            resposta += x.charAt(ii);
        }
    }
    z.value = resposta;
}

function mostraBannerChat() {
//	if(getCookie('egestorhidechat')) {
//		$('#miniChatBox').animate({'left': '+=26px'}, 'slow');
//	} else {
//		$('#chatBox').animate({'top': '+=450px'}, 2000);
		$('#chatBox').animate({'bottom': '+=58px'}, 'slow');
//	}
}

function someBannerChat() {
	//$('#chatBox').animate({'left': '-=630px'}, 'slow', '', function() {
	//	$('#chatBox').hide();
	//	$('#miniChatBox').animate({'left': '+=26px'}, 'slow');
	//});
	//createCookie('egestorhidechat', true, 2);
}

//Teste para sumir o chat ao chegar no fim da página
//$(window).scroll(function(){
        //if  ($(window).scrollTop() == $(document).height() - $(window).height()){
           //lastPostFunc();
        //}
//});

function pulatela(url, w, h) {
	if(isNaN(w)) var w=750;
	if(isNaN(h)) var h=600;
	var y = screen.availHeight/2-h/2;
	var x = screen.availWidth/2-w/2;
	var aleatorio = Math.random();
	aleatorio = "a" + aleatorio;
	var ultimo = aleatorio.charAt(aleatorio.length-1);
	ultimo = ultimo + aleatorio.charAt(aleatorio.length-2);
	telapopup = window.open(url, 'tela'+ ultimo, 'width='+ w +', height='+ h +', left='+ x +', top='+ y +', scrollbars=1, resizable=1');
	if(!telapopup) alert('Atenção, o seu navegador está bloqueando as janelas popups, é necessário que você habilite a visualização de Popups.');
}

clicado = false;
function pulatelachat(w, h) {
	if(isNaN(w)) var w=486;
	if(isNaN(h)) var h=390;
	var y = screen.availHeight/2-h/2;
	var x = screen.availWidth/2-w/2;
	var aleatorio = Math.random();
	aleatorio = "a" + aleatorio;
	var ultimo = aleatorio.charAt(aleatorio.length-1);
	ultimo = ultimo + aleatorio.charAt(aleatorio.length-2);
	if (!clicado) {
		telapopup = window.open('http://zipline.com.br/zipchat/?system=ege&x='+ ultimo, 'tela'+ ultimo, 'width='+ w +', height='+ h +', left='+ x +', top='+ y +', scrollbars=1, resizable=1');
		if(telapopup) clicado = true;
		if(!telapopup) alert('Atenção, o seu navegador está bloqueando as janelas popups, é necessário que você habilite a visualização de Popups.');
	}
}

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

function pega_ref() {
	document.formulario.afiliado.value = getCookie("egestorref");
}

function mostraChat() {
	$('chatBox').className = 'chatVisible';
}
