var controleBox = 0;
try {
	xmlhttp = new XMLHttpRequest();
} catch(ee) {
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(E) {
			xmlhttp = false;
		}
	}
} 

function obj(id)
{
	return document.getElementById(id);
}

function preload() 
{
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=preload.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function inicializarEditor(tipo, elementos)
{
	if(!elementos)
		elementos="";
	
	tinyMCE.init({
		language: "pt", 
		mode : tipo,
		theme : "advanced",
		plugins : "inserirFigura,ibrowser,safari,pagebreak,style,layer,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
		
		relative_urls : false,
		
		theme_advanced_buttons1: "fontselect,fontsizeselect,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,forecolor",
		theme_advanced_buttons2: 
"backcolor,cut,copy,paste,pastetext,pasteword,removeformat,|,search,replace,|,undo,redo,|,bullist,numlist,link,unlink,image",
		theme_advanced_buttons3: 
"cleanup,media,charmap,code,|,preview,|,fullscreen,print,|,figura",
		theme_advanced_buttons4: "",
	
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		theme_advanced_resize_horizontal : false,
		elements: elementos
	});
}

function insertHtmlEditor(campo,html) {
	try{
		var content;
	   	content=tinyMCE.get(campo).getContent();
    	tinyMCE.get(campo).setContent(content.replace('[img]', html));   
	}catch(e){
		alert(e);
		}
}

function toogle(objt)
{
	if(objt.style.display=='')	
		objt.style.display='none';
	else
		objt.style.display='';
}

function flash(largura, altura, arquivo, transparencia,menu,pasta){
	document.write('<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="'+largura+'" height="'+altura+'" id="teste" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" /><param name="wmode" value="'+transparencia+'" /><param name="menu" value="'+menu+'" />');
	document.write('<param name="FlashVars" value="pasta='+pasta+'" />');
	document.write('<param name="movie" value="'+arquivo+'" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><embed src="'+arquivo+'" wmode="'+transparencia+'" menu="'+menu+'" quality="best" width="'+largura+'" height="'+altura+'" name="teste" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" flashvars="pasta='+pasta+'" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}
function flashInner(largura, altura, arquivo, transparencia,menu,pasta){
	document.getElementById('galeria').innerHTML = '<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="'+largura+'" height="'+altura+'" id="teste" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="wmode" value="'+transparencia+'" /><param name="menu" value="'+menu+'" /><param name="FlashVars" value="pasta='+pasta+'" /><param name="movie" value="'+arquivo+'" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><embed src="'+arquivo+'" wmode="'+transparencia+'" menu="'+menu+'" quality="best" width="'+largura+'" height="'+altura+'" name="teste" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" flashvars="pasta='+pasta+'" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
}

function popup(dest,p)
{
	exibeEfeito(p);
	iniciaAjax(dest,'','divResultado');
}

var resultados;
function iniciaAjax(linksql, vars, div, acaoPosterior,semCarregando){
	var args = iniciaAjax.arguments.length;
	xmlhttp.open("POST", linksql, true);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState==4)
		{
			t = unescape(xmlhttp.responseText);
			resultados=t;		
			if (args >= 3) 
			{
				if(div)
					if(document.getElementById(div))
						document.getElementById(div).innerHTML=unescape(resultados);	
			}
			if(acaoPosterior)
				setTimeout(acaoPosterior,1);
		}
	}

	if(div && !semCarregando)
	{
		if(document.getElementById(div))
		{
			document.getElementById(div).innerHTML = "<img src='../images/carregando.gif'/>";//peq	
		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(vars);
}
function iniciaAjaxEfeito(linksql, vars, div, efeito, acaoPosterior, semCarregando){
	var args = iniciaAjaxEfeito.arguments.length;
	var xmlhttp=inicializaXmlHttp();
	xmlhttp.open("POST", linksql, true);
	$("#"+div).fadeOut("fast",function()
	{
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState==4)
			{
				resultados=xmlhttp.responseText;
				if (args >= 3)
				{
					if(resultados.toString().toLowerCase().indexOf('javascript:')==0)
						setTimeout(resultados.replace(/javascript: /i,''),1);
					else
						gel(div).innerHTML=unescape(resultados);	
					if(efeito=='slide')
					{					
						$("#"+div).slideDown('medium');	
					}
					else
					{					
						$("#"+div).fadeIn('medium');
					}
					if(controleBox==1)
						setBox();
				}
				if(acaoPosterior)
					setTimeout(acaoPosterior,1);
			}
			else
			{
				if(div && !semCarregando)
					document.getElementById(div).innerHTML = strCarregando;//peq
			}
		}
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send(vars);
	});
}
function inicializaXmlHttp(){
	var xmlhttp;
  	try {
   		xmlhttp = new XMLHttpRequest();
		xmlhttp.overrideMimeType('text/plain');
  	} 
  	catch(ee){
   		try{
    		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   		} 
   		catch(e){
    		try{
     			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
    		catch(E){
     			xmlhttp = false;
			}
   		}
  }
  return xmlhttp;
}
function gel(id){
	return document.getElementById(id);
}
function gels(name){
	return document.getElementsByName(name);
}
function setBox(){
	var box = document.getElementById("divResultado");
	var x,y, xp, yp;
	if (box.scrollHeight > box.offsetHeight){// all but Explorer Mac
		x = box.scrollWidth;
		y = box.scrollHeight;
		xp = document.body.scrollWidth;
		yp = document.body.scrollHeight;
	}
	else{// Explorer Mac;
		x = box.offsetWidth;
		y = box.offsetHeight;
		xp = document.body.offsetWidth;
		yp = document.body.offsetHeight;
	}
	box.style.left = eval(xp)/2-eval(x)/2+"px";
	box.style.top = "20%";//eval(yp)/2-eval(y)/2+"px";
}
function iniciaAcao(linksql, vars){
	xmlhttp.open("POST", linksql, true);
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState==4)
		{
			setTimeout(unescape(xmlhttp.responseText.replace(/\+/g," ")),1);
		}		
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(vars);
}

function alerta(texto, titulo, naomostrarefeito)
{
	if(!naomostrarefeito)
		exibeEfeito();
	if(!titulo)
		titulo="";
	iniciaAjax('../page/_boxAlerta.asp','mensagem='+escape(texto.replace('\n','<BR>'))+'&titulo='+escape(titulo), 'divResultado');
}
function informar(texto, titulo, naomostrarefeito)
{
	if(!naomostrarefeito)
		exibeEfeito();
	if(!titulo)
		titulo="";
	iniciaAjax('../page/_boxInformacao.asp','mensagem='+escape(texto)+'&titulo='+escape(titulo), 'divResultado');
}

function iniciaPost(linksql,div,form,acaoPosterior)
{	
	try
	{				
		var vars="";

		for(var i = 0; i < form.elements.length; i++) 
		{
			if (form.elements[i].type == "checkbox"|| form.elements[i].type=="radio")
			{
				if(form.elements[i].checked)
					vars=vars + form.elements[i].name + "=" + escape(form.elements[i].value) + "&";
			}
			else
			{			
				vars=vars + form.elements[i].name + "=" + escape(form.elements[i].value) + "&";
				if (form.elements[i].type == "text") 
					form.elements[i].value = "";
			}			
		}
		while(vars.indexOf(" ")>=0)
		{
			vars=vars.replace(" ","%20");
		}			
		xmlhttp.open("POST", linksql, true);		
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState==4)		
			{
				if(div)
				{
					var t = xmlhttp.responseText;
					t = unescape(t.replace(/\+/g," "));
					document.getElementById(div).innerHTML=t;
				}
				if(acaoPosterior)
				{
					setTimeout(acaoPosterior,1);
				}
			}
		}
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send(vars);		
		if(div)
			document.getElementById(div).innerHTML = strCarregando;
	}
	catch(e)
	{
	}
}

var divResultado;
var strCarregando='<div align="center"><table width="99" height="56" border="0" align="center">    <tr><td width="252" align="center" valign="middle" bgcolor="#FFFFFF" class="titulo-alerta"><img src="../images/carregando.gif" width="32" height="32"></td> </tr></table></div>';
function exibeEfeito(margL,margT,texto){
	var div, div2;	
	var objSelect = document.getElementsByTagName("select");
	for (i=0;i<objSelect.length;i++){
		objSelect[i].style.visibility = "hidden";
	}
	var tudo=document.getElementsByTagName("body")[0];
	tudo.scrollTop=0;
	window.scrollTo(0,0);
	tudo.scroll="no";		
	div2=document.createElement("div");
 	div2.id=("divJanela");
	tudo.appendChild(div2);	
	div=document.createElement("div");
 	div.id=("divResultado");	
	if(margL)
		div.style.marginLeft=margL+'px';
	if(margT)
		div.style.marginTop=margT+'px';	
	
 	tudo.appendChild(div);	
	document.onkeydown=function(event){tecla(event)};
	divResultado=document.getElementById("divResultado");
	
	if(document.all)
		tudo.style.marginRight='17px';
	document.getElementById("divResultado").focus();
	document.getElementById("divResultado").innerHTML=strCarregando;
	
}
 
function apagaEfeito(){
	var objSelect = document.getElementsByTagName("select");
	for (i=0;i<objSelect.length;i++){
		objSelect[i].style.visibility = "visible";
	}
	var tudo=document.getElementsByTagName("body")[0]
  	var div1 = document.getElementById("divResultado");
	var div2 = document.getElementById("divJanela");	
	div1.innerHTML="";
  	tudo.removeChild(div1);
	tudo.removeChild(div2);
	document.getElementsByTagName("html")[0].style.overflow="";
	if(document.all)
		tudo.style.marginRight='';
  	tudo.scroll="yes";	
	document.onkeydown='';
}  

function tecla(e){
	if (!e) 
		e = window.event;	

	if (e.keyCode==27)
	{
		apagaEfeito();
		document.onkeydown='';
		return false;
	}
}

function fValidaCpf(idObjetoX)
{
	
	var vCpf
	vCpf = document.getElementById(idObjetoX).value;
	
	var vRetorno
	
	if ( (document.getElementById(idObjetoX).value == '') || (document.getElementById(idObjetoX).value.length != 11) ) 
	{
		vRetorno = false; 
   	}
	
	var vCpf
	var vDVInformados
	var vDVReais
	var vSoma
	var vArCpf
	var vPosicao
	
	vDVInformados = vCpf.substr(9, 2);
	vArCpf = new Array(10);

	for (I=0; I<=8; I++) 
	{
  		vArCpf[I] = vCpf.substr(I, 1);
	}

	vPosicao = 10;
	vSoma = 0;
   	for (I=0; I<=8; I++) 
	{
      vSoma = vSoma + vArCpf[I] * vPosicao;
      vPosicao = vPosicao - 1;
  	}
	vArCpf[9] = vSoma % 11;
   	if ( vArCpf[9] < 2 ) 
	{
    	vArCpf[9] = 0;
	}
   	else
	{
       vArCpf[9] = 11 - vArCpf[9];
	}
	
	vPosicao = 11;
	vSoma = 0;
   	for (I=0; I<=9; I++) 
	{
    	vSoma = vSoma + vArCpf[I] * vPosicao;
      	vPosicao = vPosicao - 1;
   }
   vArCpf[10] = vSoma % 11;
   if ( vArCpf[10] < 2 )
   {
        vArCpf[10] = 0;
   }
   else
   {
   		vArCpf[10] = 11 - vArCpf[10];
   }
   
    vDVReais = vArCpf[9] * 10 + vArCpf[10];
	if ( vDVReais != vDVInformados )
	{
		vRetorno = false;
   	}
	
	if (vRetorno == false)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function excluir(area,cod,destino){
	exibeEfeito();
	iniciaAjax('ajx_excluir.aspx','area='+area+'&cod='+cod+'&destino='+destino,'divResultado');	
}

function validaImagem(elem){

	var filePath=elem.value;
	
	//tipos validos
	var validExtensions = new Array();
	
	validExtensions[0] = 'jpg';
	validExtensions[1] = 'jpeg';
	validExtensions[2] = 'gif';
	validExtensions[3] = 'png';
	validExtensions[4] = 'bmp';
	validExtensions[5] = 'zip';
	
	//pega a extensao
	var ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();
	
	for(var i = 0; i < validExtensions.length; i++) {
		if(ext == validExtensions[i])
			return true;
	}	

	exibeEfeito();
 	iniciaAjax('ajx_erro_imagem.aspx?mensagem=1','' ,'divResultado');

	
	//limpa a FileUpload
	//firefox
  elem.value = '';

   //ie
   var elem2= elem.cloneNode(false);
   elem2.onchange= elem.onchange;
   elem.parentNode.replaceChild(elem2,elem); 
	 
	return false;

}

function validaMusica(elem)
{
	var filePath=elem.value;
	var validExtensions = new Array();
	
	validExtensions[0] = 'mp3';
	validExtensions[1] = 'wma';
	validExtensions[2] = 'acc';
	
	var ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();
	
	for(var i = 0; i < validExtensions.length; i++)
	{
		if(ext == validExtensions[i])
			return true;
	}	
	exibeEfeito();
 	iniciaAjax('ajx_erro_imagem.aspx?mensagem=2','' ,'divResultado');
	elem.value = '';

	var elem2= elem.cloneNode(false);
	elem2.onchange= elem.onchange;
	elem.parentNode.replaceChild(elem2,elem); 
	return false;
}
