///////////////////////////////////////////////////////////////////
//                                                               //
//        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)
}


function popupret(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;
	return window.open(url, target, parametros);
}

//Função para validar todos campos do formulário
function checkForm(form)
{
  var msg;
  var erro = 0;
  var ii = 0;
  var icampo;
  
  msg = "Faltam os seguintes campos:\n\n";

  for (var i = 0; i < form.length; i++)
  {
    if ((form[i].value.length < 1) && (form[i].nome != ""))
    {
      msg = msg + "- "+form[i].nome+"\n";
      erro++;
      if (ii == 0)
      {
        icampo = i;
        ii++;
      }
    }
  }
  if (erro > 0)
  {
    alert(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()
{

  if (!checkForm(document.busca))
  {
    return false;
  } else
  {
    var palavra = escape(document.busca.txtPalavra.value);
	location.href = "vcard.php?op=busca&amp;txt="+palavra;
    return false;
  }
}

function vCartao()
{

  if (!checkForm(document.cartao))
  {
    return false;
  } else
  {
    if (vEmail.test(document.cartao.txtEmail.value) == false)
    { 
      alert("Informe seu E-Mail VÁLIDO.");
      document.cartao.txtEmail.focus();
      return false;
    }
    if (vEmail.test(document.cartao.txtEmailAmigo.value) == false)
    { 
      alert("Informe o E-MAIL VÁLIDO da pessoa que receberá o cartão");
      document.cartao.txtEmailAmigo.focus();
      return false;
    }
    else {
      popupx('about:blank','cartao','status=no,scrollbars=yes','440','500','1');
      return true;
    }
  }
}

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 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="true" />';
	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);
}

/////////// 05/06/2008

function $() {
	var lElements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var lElement = arguments[i];
		if (typeof lElement == 'string') {
			lElement = document.getElementById(lElement);
		}
	    if (arguments.length == 1) {
			return lElement;
		}
		lElements.push(lElement);
	}
	return lElements;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}



function bgBlock() {
	lDiv = $('bgBlock');
	if (lDiv == null) {
		lDiv = document.createElement("div");
		lDiv.id = "bgBlock";
		lDiv.style.position = "absolute";
		lDiv.style.left = "0";
		lDiv.style.top = "0";
		document.body.appendChild(lDiv);
	}
	lDiv.style.display = "none";

	var lWidth = (document.body.clientWidth*1);
	var lHeight = (document.body.clientHeight*1);
	lPageSize = getPageSize();

	lDiv.style.width = lPageSize[0]+'px';
	lDiv.style.height = lPageSize[1]+'px';
	lDiv.style.zIndex = 10000;
	lDiv.style.display = "block";
}
function bgUnBlock() {
	lDiv = $('bgBlock');
	lDiv.style.display = "none";
}
