var ajax_loader = new Class({
	online:false,
	initialize: function() {
		this.online=false;
	},
	showURL: function(url,div_id) {
		if ($chk($(div_id))) {
			if (this.online==false) {
				var ajaxReq = new Request.HTML({
					'url' : url,
					initialize: function() {
						if ($(div_id).getStyle('display')=='none')
							$(div_id).setStyles({'display':'block'});
						this.addLoading($(div_id));
						this.online=true;
					}.bind(this),
					onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript) {
						$(div_id).empty();
						$(div_id).set('html',responseHTML);
						$exec(responseJavaScript);
						if (responseHTML=='')
							$(div_id).setStyles({'display':'none'});
						this.online=false;
					}.bind(this),
					evalScripts: false,
					evalResponse: false
				}).send();
			}
		}
	},
	addLoading: function(div) {
		div.empty();
		var div_img=Element('div',{'align':'center'});
		var img=Element('img',{'src':'../imagens/loading_p.gif'});
		div_img.appendChild(img);
		div.appendChild(div_img);
	}
});
var div_loader={};
window.addEvent('load', function() {
	div_loader=new ajax_loader();
});

function getHTTPObject() {
	var req;
	try {
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			if (req.readyState == null) {
				req.readyState = 1;
				req.addEventListener("load", function () { req.readyState = 4; if (typeof req.onReadyStateChange == "function") req.onReadyStateChange();   }, false);
			}
			return req;
		}
		if (window.ActiveXObject) {
			var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
			for (var i = 0; i < prefixes.length; i++) {
				try {
					req = new ActiveXObject(prefixes[i] + ".XmlHttp");
					return req;
				}
				catch (ex) {};
			}
		}
	}
	catch (ex) {}
	alert("XmlHttp Objects not supported by client browser");
}

function detalhe_evento(id,href) {
	objid = 'eventos_texto'+id;
	hrefid = 'hrefevento'+id;
	if ($(objid).getStyle('display').toLowerCase()=='none') {
		div_loader.showURL('../eventos/texto.php?id='+id,'eventos_texto'+id);		

		$(objid).setStyle('display','block');
		$(hrefid).innerHTML='&lt;&lt; menos';
	} else {
		$(objid).setStyle('display','none');
		$(hrefid).innerHTML='mais &gt;&gt;';
	}
}

function detalhe_faq(id,href) {
	objid = 'faq_texto'+id;
	hrefid = 'hrefevento'+id;
	if ($(objid).getStyle('display').toLowerCase()=='none') {
		div_loader.showURL('../faq/texto.php?id='+id,'faq_texto'+id);		

		$(objid).setStyle('display','block');
		$(hrefid).innerHTML='<img src="../imagens/img_menos_faq.jpg" class="img_faq">';
	} else {
		$(objid).setStyle('display','none');
		$(hrefid).innerHTML='<img src="../imagens/img_mais_faq.jpg" class="img_faq">';
	}
}

function detalhe_download(id,href) {
	objid = 'download_texto'+id;
	hrefid = 'hrefevento'+id;
	if ($(objid).getStyle('display').toLowerCase()=='none') {
		div_loader.showURL('../downloads/texto.php?id='+id,'download_texto'+id);		

		$(objid).setStyle('display','block');
		$(hrefid).innerHTML='<img src="../imagens/img_menos_dl.jpg" alt="Ocultar detalhes" title="Ocultar detalhes" border="0" class="img_dl">';
	} else {
		$(objid).setStyle('display','none');
		$(hrefid).innerHTML='<img src="../imagens/img_mais_dl.jpg" alt="Exibir detalhes" title="Exibir detalhes" border="0" class="img_dl">';
	}
}

var tamFonte=0;
function zoomfont(id,tipo) {
	var size=$(id).getStyle('fontSize').toUpperCase();
	size=parseFloat(size.replace('PX',''));
	if ((tipo=="+") && (tamFonte<=3)) {
		$(id).style.fontSize=(size+1)+'px';
		tamFonte++;
	}
	if ((tipo=="-") && (tamFonte>=-1)) {
		$(id).style.fontSize=(size-1)+'px';
		tamFonte--;
	}
}

//protector class
var dwProtector = new Class({
	//implements
	Implements: [Options],
	//options
	options: {
		image: 'blank.gif',
		elements: $$('img'),
		zIndex: 10
	},
	//initialization
	initialize: function(options) {
		//set options
		this.setOptions(options);
		//make it happen
		this.protect();
	},
	//a method that does whatever you want
	protect: function() {
		//for each image that needs be protected...
		this.options.elements.each(function(el) {
			//get the element's position, width, and height
			var size = el.getCoordinates();
			//create the protector
			var p = new Element('img', {
				src: this.options.image,
				width: size.width,
				height: size.height,
				styles: {
					'z-index': this.options.zIndex,
					'left': size.left + 'px',
					'top': size.top + 'px',
					'position': 'absolute'
				}
			}).inject($(document.body),'top');
		},this);
	}
});

Element.implement({  
	appendHTML: function(html,where){  
		return this.grab(new Element('text',{'html':html}),where);  
	}  
});

function input_limpar(id,text) {
	if ((id.value=='') || ((id.value=='') && (document.activeElement!=id)))
		 id.value=text;
	else
	if ((id.value!=text) || ((id.value==text) && (document.activeElement==id))) {
		id.value=id.value.replace(text,'');
	}
	return;
}

function cad_voltar(passo) {
	document.getElementById('cadastrando').value='0';
	document.getElementById('passo_cadastro').value=passo;
	document.getElementById('zone').submit();
}

function doDestacaTexto(Texto, termoBusca){
	inicioTag = "<b>";
	fimTag = "</b>";
	var novoTexto = "";
	var i = -1;
	var lcTermoBusca = termoBusca.toLowerCase();
	var lcTexto = Texto.toLowerCase();
	while (Texto.length > 0){
		i = lcTexto.indexOf(lcTermoBusca, i+1);
		if (i < 0){
			novoTexto += Texto;
			Texto = "";
		}
		else{
			if (Texto.lastIndexOf(">", i) >= Texto.lastIndexOf("<", i)){
				if (lcTexto.lastIndexOf("/script>", i) >= lcTexto.lastIndexOf("<script", i)){
					novoTexto += Texto.substring(0, i) + inicioTag + Texto.substr(i, termoBusca.length) + fimTag;
	  				Texto = Texto.substr(i + termoBusca.length);
					lcTexto = Texto.toLowerCase();
	  				i = -1;
				}
			}
		}
	}
	return novoTexto;
}

function doDestacaTextoBusca(textoBusca, textoObj, ehFrase){
	var textoObj=document.getElementById(textoObj);
	if (ehFrase){arrayBusca=[textoBusca];}
	else{arrayBusca = textoBusca.split(" ");}
	var Texto = textoObj.innerHTML;
	for (var i = 0; i < arrayBusca.length; i++){Texto = doDestacaTexto(Texto, arrayBusca[i]);}
	textoObj.innerHTML = Texto;
	return true;
}

function HPMAISconteudoProtegidoFF() {
	if ((document.getElementById || document.layers) && (!document.all)) {
	 document.oncontextmenu = function () { return false; }
	  document.onkeypress = function (evt) {
			return HPMAISconteudoProtegido(evt);
	  }
	}
}

function HPMAISconteudoProtegido(evt) {
	if ((document.getElementById || document.layers) && (!document.all)) {
		var r = '';
		var ctrl = 0;
		
		if (document.getElementById && (!document.all)) {
		  ctrl = evt.ctrlKey;
		}
		else if (document.layers) {
		  ctrl = Event.CONTROL_MASK;
		} 
		r = String.fromCharCode(evt.which).toUpperCase();    
		if (ctrl){
			if (r=='C'){
				alert('Não é permitido a cópia ou utilização deste conteúdo.\nPara obter mais informações entre em contato conosco!');
			}
		}
	} else 
		alert('Não é permitido a cópia ou utilização deste conteúdo.\nPara obter mais informações entre em contato conosco!');
	return false;
}

function ttipo_pessoa(troca,cpf,cnpj) {
	if (troca==1) {
		if (document.getElementById('fisica').checked==true) {
			document.getElementById('dados_pessoais').style.display='';
			document.getElementById('dados_empresa').style.display='none';
			var texto=cpf;
		}
		if (document.getElementById('juridica').checked==true) {
			document.getElementById('dados_pessoais').style.display='none';
			document.getElementById('dados_empresa').style.display='';
			var texto=cnpj;
		}
	} else
		var texto=cpf;
	document.getElementById('zone').onsubmit=new Function(texto);
}

function maxChar(obj,m) {
	obj=document.getElementById(obj);	
	if (obj.value.length>m) {
		obj.value=obj.value.substring(0,m);
	}
}

function vcep(id) {
	vr=''
	if (id.value.length>5) {
		vr=id.value
		if (vr.substr(5,1)!='-')
			id.value=vr.substr(0,5)+'-'+vr.substr(5,vr.lenght);
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0 // modificado por hpmais
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
// alteracoes hpmais
    if (val) { nm=val.title; if ((val=val.value)!="") {
// cpf cnpj
	  if (test.indexOf('isCpfCnpj')!=-1) {
		if ((TESTA(val,'CPF')=='invalido') && (TESTA(val,'CNPJ')=='invalido')) errors+='- '+nm+' inválido.\n';
// 2 campos iguais
	  } else if (test.indexOf('isIgual')!=-1) {
		val2=MM_findObj(args[i+1]);
		nm2=val2.title;
		val2=val2.value;
		if (val!=val2) errors+='- '+nm+' e '+nm2+' não conferem.\n';
// fim alteracoes hpmais
	  } else if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' precisa ser um e-mail.\n';
      } else if ((test.indexOf('isNum')!=-1) || (test.indexOf('inRange')!=-1)) { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' precisa ser um número. (Ex.: 9.89)\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' precisa ser um número entre '+min+' e '+max+'.\n';
    } }
// alteracoes hpmais	
// minimo de caracteres	
	  if (test.indexOf('minChar')!=-1) {
  	    p=test.indexOf('minChar:')+8;
	    minchar=test.substring(p);
	    ttchar=val.length;
	    if (ttchar<minchar) errors+='- '+nm+' precisa conter no mínimo '+minchar+' caracteres.\n';
	  }
//	fim alteracoes hpmais
	} else if (test.charAt(0) == 'R') errors += '- '+nm+' é obrigatório.\n'; }
  } if (errors) alert('Erro(s):\n'+errors);
  document.MM_returnValue = (errors == '');
}

function TESTA(CNUMB,CTYPE)
{
  if(Verify(CNUMB, CTYPE))
  {
    return("valido")
  }
  else
  {
    return("invalido")
  }
}

function ClearStr(str, char)
{
  while((cx=str.indexOf(char))!=-1)
  {		
    str = str.substring(0,cx)+str.substring(cx+1);
  }
  return(str);
}

function ParseNumb(c)
{
  c=ClearStr(c,'-');
  c=ClearStr(c,'/');
  c=ClearStr(c,',');
  c=ClearStr(c,'.');
  c=ClearStr(c,'(');
  c=ClearStr(c,')');
  c=ClearStr(c,' ');
  if((parseFloat(c) / c != 1))
  {
    if(parseFloat(c) * c == 0)
    {
      return(c);
    }
    else
    {
      return(0);
    }
  }
  else
  {
    return(c);
  }
}

function Verify(CNUMB,CTYPE)
{
  CNUMB=ParseNumb(CNUMB)
  if(CNUMB == 0)
  {
    return(false);
  }
  else
  {
    g=CNUMB.length-2;
    if(TestDigit(CNUMB,CTYPE,g))
    {
      g=CNUMB.length-1;
      if(TestDigit(CNUMB,CTYPE,g))
      {	
        return(true);
      }
      else
      {
        return(false);
      }
    }
    else
    {
      return(false);
    }
  }
}

function TestDigit(CNUMB,CTYPE,g)
{
  var dig=0;
  var ind=2;
  for(f=g;f>0;f--)
  {
    dig+=parseInt(CNUMB.charAt(f-1))*ind;
    if (CTYPE=='CNPJ')
    { if(ind>8) {ind=2} else {ind++} }
    else
    { ind++ }
  }
  dig%=11;
  if(dig<2)
  {
    dig=0;
  }
  else
  {
    dig=11-dig;
  }
  if(dig!=parseInt(CNUMB.charAt(g)))
  {
    return(false);
  }
  else
  {
    return(true);
  }
}

function fecha_falso_popup(div) {
	document.getElementById(div).style.display = 'none';
}

