///////////////////////////////////////////////////////////////////
//                                                               //
//        geral.js : Contém as funções javascript do site        //
//                                                               //
//            FIEVEL NETWORK - http://www.fievel.com.br          //
//                                                               //
///////////////////////////////////////////////////////////////////


var vEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
var vCampos = /^(.){1,}/;

//Função para abrir uma janela popup
function popupx(url,target,parametros, largura, altura, center) {

	if (parametros != '') {
		parametros += ',';
	}
	if (center == 1) {
		var vLeft = (screen.width-largura)/2;
		var vTop = (screen.height-altura)/2;

		parametros += 'left='+vLeft+',top='+vTop;
	}
	if (parametros != '') {
		parametros += ',';
	}
	parametros += 'width='+largura+',height='+altura;
	window.open(url, target, parametros)
}


/*
// Validar os campos do formulário
// Tipos: (1) E-mail ; (2) Numérico
// v|CAMPO|TIPO{1, 2}
// 
//  by Élison Gomes (2005)
// < elison@fievel.com.br >
*/
function checkForm(form) {
	var campo;
	var campo_valor;
	var campo_nome;
	var msg = '';
	var erro = 0;
	var icampo = -1;
	var pEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;

	for (var i = 0; i < form.length; i++) {
		try {
			campo = form[i].id.split('|');
			campo_valor = form[i].value;
			campo_nome = campo[1].toUpperCase();

			if (campo[0] == "v") {
				/* CAMPO VAZIO */
				if (campo_valor.length < 1) {
					erro++;
					msg += '- O campo ' + campo_nome + ' deve ser preenchido.\n';
					if (icampo == -1) {
						icampo = i;
					}
				}
				/* VALIDAÇÃO ESPECÍFICA */
				else if (campo.length > 2) {
					/* E-MAIL */
					if ( (campo[2] == 1) && (!pEmail.test(campo_valor)) ) {
						erro++;
						msg += '- O campo ' + campo_nome + ' deve ser preenchido com um e-mail válido.\n';
						if (icampo == -1) {
							icampo = i;
						}
					}
					/* NUMÉRICO */
					else if ( (campo[2] == 2) && (isNaN(campo_valor)) ) {
						erro++;
						msg += '- O campo ' + campo_nome + ' deve conter somente números.\n';
						if (icampo == -1) {
							icampo = i;
						}
					}
				}
			}
		} catch(e) {}
	}
	if (erro > 0)
	{
		alert('Atenção, foi detectado '+erro+' erro(s) no preenchimento do formulário:\n\n'+msg);
		form[icampo].focus();
		return false;
	} else
	{
		return true;
	}
}

function sMsg(str)
{
  window.status = str;
}

function vEnquete(id) {
  var nm_opcoes = document.enquete.opcao.length;
  var resposta = nm_opcoes;
  var respostas = "";
  for(i=0;i<nm_opcoes;i++) {
	if(document.enquete.opcao[i].checked) {
		resposta = i;
		respostas=respostas + "voto=" + document.enquete.opcao[i].value;
	}
  }
  if(resposta==nm_opcoes) {
	alert("Selecione uma opção antes de votar.");
  }
  else {
	location.href = 'enquete.php?op=votar&amp;id='+id+'&amp;' + respostas;
  }
}

function vBusca() {
	var txt = escape(document.busca.txtPalavra.value);
	var ec = document.busca.cbEstCivil[document.busca.cbEstCivil.selectedIndex].value;
	var sx = "";
	if (document.getElementById('rbSexoM').checked) {
		sx = document.getElementById('rbSexoM').value;
	}
	else if (document.getElementById('rbSexoF').checked) {
		sx = document.getElementById('rbSexoF').value;
	}
	var uf = document.busca.cbEstado[document.busca.cbEstado.selectedIndex].value;
	var bBusca = false;
	var sBusca = ""
	
	if (txt.length > 0) {
		sBusca += "&amp;txt="+escape(txt);
		bBusca = true;
	}
	if (ec > 0) {
		sBusca += "&amp;ec="+ec;
		bBusca = true;
	}
	if (sx.length > 0) {
		sBusca += "&amp;sx="+sx;
		bBusca = true;
	}
	if (uf > 0) {
		sBusca += "&amp;uf="+uf;
		bBusca = true;
	}

	if (bBusca) {
		location.href = "vcard.php?op=busca"+sBusca;
	}
	else {
		alert('Informe algo para efetuar a busca nos AtivaCards!');
	}
	return false;
}

function ShowHideLayer(layerNome)
{

  var elemLayerID = document.getElementById(layerNome);

  if (elemLayerID.style.display == 'none')
  {
    elemLayerID.style.display = '';
  }
  else {
    elemLayerID.style.display = 'none';
  }
}

//Função para limitar o tamanho da textarea num formulário
function textMax(obj, objcount, max)
{
  str = obj.value;
  if( str.length > max )
  {
    obj.value = str.substring( 0, max );
  } else
  {
    objcount.innerText = max - str.length;
  }
}

function ir(url) {
	location.href = url;
}

function abreChat()
{
	popupx('./chat','ativa_chat','menubar=no, scrollbars=no, resizable=no','350','260','1');
}

function showFlash(pArquivo, pLargura, pAltura, pBgcolor, pId, pQualidade, pAlinhamento, pTransparente) {
	obj = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+pLargura+'" height="'+pAltura+'" id="'+pId+'" align="'+pAlinhamento+'">';
	obj+= '<param name="allowScriptAccess" value="sameDomain" />';
	obj+= '<param name="menu" value="false" />';
	obj+= '<param name="movie" value="'+pArquivo+'" />';
	obj+= '<param name="quality" value="'+pQualidade+'" />';
	if(pTransparente) {
		obj+= '<param name="wmode" value="transparent" />';
	}
	obj+= '<param name="bgcolor" value="'+pBgcolor+'" />';
	obj+= '<embed src="'+pArquivo+'" '+((pTransparente)?'wmode="transparent"':'')+'" quality="'+pQualidade+'" bgcolor="'+pBgcolor+'" width="'+pLargura+'" height="'+pAltura+'" name="'+pId+'" align="'+pAlinhamento+'" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	obj+= '</object>';
	document.write(obj);
}