// JavaScript Document
<!--

function redirect(page) {
	window.location = page;
}

function errMsg(field){
	alert("O campo "+ field +" deve ser preenchido!");
}

//*** Login ***//
function validaFrmLogin(){
	var f = document.frmLogin;
	
	if(f.login.value == ""){
		errMsg("login");
		f.login.focus();
		return false;
	}
	if(f.senha.value == ""){
		errMsg("senha");
		f.senha.focus();
		return false;
	}
return true;
}

//*** Fale Conosco ***//
function validaContato(){
	var f = document.formContato;
	
	if(f.nome.value == ""){
		errMsg("nome");
		f.nome.focus();
		return false;
	}
	if(f.assunto.value == ""){
		errMsg("assunto");
		f.assunto.focus();
		return false;
	}
	if(f.mensagem.value == ""){
		errMsg("mensagem");
		f.mensagem.focus();
		return false;
	}
return true;
}

//*** Admin - Cadastro Adms ***//
function validateAdmCadastro(){
	var f = document.frmAdmCadastro;
	
	if(f.nivel.value == "#"){
		errMsg('nível');
		f.nivel.focus();
		return false;
	}
	if(f.nome.value == ""){
		errMsg('nome');
		f.nome.focus();
		return false;
	}
	if(f.email.value == ""){
		errMsg('e-mail');
		f.email.focus();
		return false;
	}
	var str = f.email.value;
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    if(!filter.test(str)){
		alert("Formato de e-mail inválido!");
		f.email.select();
		return false;
    }
	if(f.login.value == ""){
		errMsg('nome de login');
		f.login.focus();
		return false;
	}
	var s1 = f.senha.value;
	var s2 = f.confirma_senha.value;
	if(s1 == ""){
		errMsg('senha');
		f.senha.focus();
		return false;
	}
	if(s2 == ""){
		errMsg('confirmação da senha');
		f.confirma_senha.focus();
		return false;
	}
	if(s1 != s2){
		alert("O campo senha e  confirma senha devem ter o mesmo valor!");
		f.confirma_senha.select();
		return false;
	}
	
return true;
}

//*** Admin - Cadastro de convênios ***//
function validaFrmCadConvenios(){
	var f = document.frmConvenios;
	
	if(f.categoria.value == ""){
		errMsg("categoria");
		f.categoria.focus();
		return false;
	}
	if(f.nome.value == ""){
		errMsg("nome");
		f.nome.focus();
		return false;
	}
	if(f.endereco.value == ""){
		errMsg("endereco");
		f.endereco.focus();
		return false;
	}
	if(f.cidade.value == ""){
		errMsg("cidade");
		f.cidade.focus();
		return false;
	}
	if(f.telefone1.value == ""){
		errMsg("telefone 1");
		f.telefone1.focus();
		return false;
	}
	
return true;
}

//*** Admin - Níveis Adm ***//
function validaFrmAdmNiv(){
	var f = document.frmAdmNiv.nivel;
	
	if(f.value == ""){
		errMsg("nível");
		f.focus();
		return false;
	}
return true;
}

//*** Admin - Noticias ***//
function validaFrmNoticias(){
	var f = document.frmNoticias;
	
	if(f.titulo.value == ""){
		errMsg("título");
		f.titulo.focus();
		return false;
	}
	if(f.descricao.value == ""){
		errMsg("descrição");
		f.descricao.focus();
		return false;
	}
	var arquivo = f.imagem.value;
	if(arquivo){
		ext = new Array(".jpg", ".gif", ".png");
		extensao = (arquivo.substring(arquivo.lastIndexOf("."))).toLowerCase();
		permitida = false;
		for (var i = 0; i < ext.length; i++){
			if (ext[i] == extensao) {
				permitida = true;
				break;
			}
		}
		if (!permitida) {
			alert("A extensão de arquivo "+ extensao +" não é permitida para upload.\nFormatos permitidos: .jpg, .gif e .png");
			return false;
		}
	}
	
return true;
}

//*** Admin - Documentos ***//
function validaFrmDocumentos(){
	var f = document.frmDocumentos;
	
	if(f.titulo.value == ""){
		errMsg("título");
		f.titulo.focus();
		return false;
	}
	var arquivo = f.upload.value;
	if(arquivo == ""){
		alert("Você deve anexar um arquivo.\nFormatos permitidos: .doc, .pdf, .xls e .zip");
		f.upload.focus();
		return false;
	}else{
		ext = new Array(".doc", ".pdf", ".xls", ".zip");
		extensao = (arquivo.substring(arquivo.lastIndexOf("."))).toLowerCase();
		permitida = false;
		for (var i = 0; i < ext.length; i++){
			if (ext[i] == extensao) {
				permitida = true;
				break;
			}
		}
		if (!permitida) {
			alert("A extensão de arquivo "+ extensao +" não é permitida para upload.");
			return false;
		}
	}
	
return true;
}
//*** Admin - Categ Convênios ***//
function validaCatConvenios(){
	var f = document.frmCatConvenios;
	
	if(f.cnv_categoria.value == ""){
		errMsg("categoria de convênio");
		f.cnv_categoria.focus();
		return false;
	}
return true;
}

//*** Recuperar Senha ***//
function validaFrmRecuperarSenha(){
	var f = document.frmRecuperarSenha;
	if(f.nome.value == ""){
		errMsg("nome");
		f.nome.focus();
		return false;
	}
	if(f.email.value == ""){
		errMsg("email");
		f.email.focus();
		return false;
	}
	var str = f.email.value;
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    if(!filter.test(str)){
		alert("Formato de e-mail inválido!");
		f.email.select();
		return false;
    }
return true;
}

function OpenWindow(page,w,h){
	popUpWidth  = w;
	popUpHeight = h;
	var sw = screen.width;
	var sh = screen.height;
	var centrHoriz	= (sw - popUpWidth) / 2;
	var centrVert	= (sh - popUpHeight) / 3;
	window.open(page, 'openWin','width='+ popUpWidth +',height='+ popUpHeight +',left='+ centrHoriz +',top='+ centrVert +',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no');
}

var enablepersist="on"
var collapseprevious="yes"

if (document.getElementById){
	document.write('<style type="text/css">')
	document.write('.switchcontent{display:none;}')
	document.write('</style>')
}

function getElementbyClass(classname){
	ccollect=new Array()
	var inc=0
	var alltags=document.all? document.all : document.getElementsByTagName("*")
	for (i=0; i<alltags.length; i++){
		if (alltags[i].className==classname)
		ccollect[inc++]=alltags[i]
	}
}

function contractcontent(omit){
	var inc=0
	while (ccollect[inc]){
		if (ccollect[inc].id!=omit)
		ccollect[inc].style.display="none"
		inc++
	}
}

function expandcontent(cid){
	if (typeof ccollect!="undefined"){
		if (collapseprevious=="yes")
		contractcontent(cid)
		document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none"
	}
}

function revivecontent(){
	contractcontent("omitnothing")
	selectedItem=getselectedItem()
	selectedComponents=selectedItem.split("|")
	for (i=0; i<selectedComponents.length-1; i++)
	document.getElementById(selectedComponents[i]).style.display="block"
}

function get_cookie(Name) { 
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
		if (offset != -1) { 
			offset += search.length
			end = document.cookie.indexOf(";", offset);
			if (end == -1) end = document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}
return returnvalue;
}

function getselectedItem(){
	if (get_cookie(window.location.pathname) != ""){
		selectedItem=get_cookie(window.location.pathname)
		return selectedItem
	}
	else
	return ""
}

function saveswitchstate(){
	var inc=0, selectedItem=""
	while (ccollect[inc]){
		if (ccollect[inc].style.display=="block")
		selectedItem+=ccollect[inc].id+"|"
		inc++
	}

document.cookie=window.location.pathname+"="+selectedItem
}

function do_onload(){
	uniqueidn=window.location.pathname+"firsttimeload"
	getElementbyClass("switchcontent")
	if (enablepersist=="on" && typeof ccollect!="undefined"){
		document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0" 
		firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0
		if (!firsttimeload)
		revivecontent()
	}
}

if (window.addEventListener)
window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
window.attachEvent("onload", do_onload)
else if (document.getElementById)
window.onload=do_onload

if (enablepersist=="on" && document.getElementById)
window.onunload=saveswitchstate

// -->