function $(id)
{
	return document.getElementById(id);
}

function change_page(page, form, file)
{
	confirm_action();
	
	document.getElementById('actual_page').value = page;
	document.getElementById(form).action = 'acoes.php?pag=' + file;
	document.getElementById(form).submit();
}

function change_tab(tab, form, file)
{
	confirm_action();
	
	document.getElementById('actual_tab').value = tab;
	document.getElementById(form).action = 'acoes.php?pag=' + file;
	document.getElementById(form).submit();
}

function change_qt_page(form, file)
{
	confirm_action();
	
	old_qt_per_pag = document.getElementById('old_qt_per_pag');
	qt_per_pag	   = document.getElementById('qt_per_pag');
	actual_page    = document.getElementById('actual_page');
	_form		   = document.getElementById(form);
	
	calc1 = ((parseInt(actual_page.value)+1) * parseInt(old_qt_per_pag.value));
	calc2 = (parseInt(old_qt_per_pag.value)-1);
	result = calc1 - calc2;	

	actual_page.value = 0;

/*	if(qt_per_pag.value == 'TODOS')
		actual_page.value = 0;
	else
		actual_page.value = parseInt(result/qt_per_pag.value);
		
	if(actual_page.value == 'NaN')
		actual_page.value = 0;*/

	_form.action = 'acoes.php?pag=' + file;
	_form.submit();
}

function changeStyle(title) 
{
	var i, a;
	for(i = 0;(a = document.getElementsByTagName('link')[i]); i++)
	{
		if(a.getAttribute('rel').indexOf('style') != -1	&& a.getAttribute('title')) 
		{
			a.disabled = true;
			if(a.getAttribute('title') == title) 
				a.disabled = false;
		}
	}
}

var last_menu = null;
var hide_timeout = null;

function display_menu_top10( target )
{
  if( target )
  {
    if( last_menu )
      hide_menu( );

    if( hide_timeout )
      clearTimeout( hide_timeout );

    var obj = document.getElementById( target );

    obj.style.visibility = "visible";

    last_menu = obj;
  }
  else
    hide_timeout = setTimeout( "hide_menu()", 300 );     
}

function display_menu( target )
{
  if( target )
  { 	
    if( last_menu )
      hide_menu( );

    if( hide_timeout )
      clearTimeout( hide_timeout );

    var obj = document.getElementById( target );
    obj.style.position = "absolute";
    obj.style.visibility = "visible";
    last_menu = obj;
  }
  else
	  hide_timeout = setTimeout( "hide_menu()", 300 );     
}

function hide_menu( )
{
	last_menu.style.visibility = "hidden";
	last_menu = null;
	clearTimeout( hide_timeout );
}

function limpar(formulario) 
{ 
	form = document.getElementById(formulario);
	for (i = 0; i <= form.elements.length-1; i++) 
	{ 
		if ((form.elements[i].type != 'button')&&(form.elements[i].type != 'submit')&&(form.elements[i].type != 'hidden')) 
		{ 
			if (form.elements[i].type == 'select-one') 
				form.elements[i].selectedIndex = 0; 
			else 
				form.elements[i].value = ''; 
		} 
	} 
	form.submit();
} 

function keypress(e) 
{
	var x = '';
	if (document.all)
	{
		var evnt = window.event;
		x = evnt.keyCode;
	}
	else
	{
		x = e.keyCode;
	}			
	return x;
}

function formataCPF(campo, event)
{
	var tecla = keypress(event);
	if(tecla != 8)
	{
		if (tecla != 194 && tecla != 190)
			if ((campo.value.length == 3) || (campo.value.length == 7))
				campo.value += '.';
		if (tecla != 109)
			if (campo.value.length == 11)
				campo.value += '-';
	}
}

function formataData(campo, event) 
{
	var tecla = keypress(event);
	if(tecla != 8)
	{
		if (tecla != 111 && tecla != 193 )
			if ((campo.value.length == 2) || (campo.value.length == 5))
				campo.value += '/';
	}
}

function formataCEP(campo, event) 
{
	var tecla = keypress(event);
	if(tecla != 8)
	{
		if (tecla != 109)
			if (campo.value.length == 5)
				campo.value += '-';
	}
}

function formataCNPJ(campo, event) 
{
	var tecla = keypress(event);
	if(tecla != 8)
	{
		if (tecla != 46)
			if ((campo.value.length == 2) || (campo.value.length == 6))
				campo.value += '.';
		if (tecla != 111 && tecla != 193 )
			if (campo.value.length == 10)
				campo.value += '/';
		if (tecla != 109)
			if (campo.value.length == 15)
				campo.value += '-';
	}
}

function formataHora(campo, event, tipo)
{
	var tecla = keypress(event);
	if(tecla != 8)
	{
		if(tipo == 2)
			if ((campo.value.length == 2))
				campo.value += ':';
		if(tipo == 3)
			if ((campo.value.length == 2) || (campo.value.length == 5))
				campo.value += ':';
	}
}

function countCharsTextArea(tamanho_maximo, campo) {

	var tecla = keypress(event);
	if(tecla != 13)
	{
	
		var text1 = campo.value;
		
		var totalChars = text1.length;
		window.status = "Quantidade de caracteres que faltam: " + (tamanho_maximo - totalChars);
		
		if (totalChars >= tamanho_maximo){ 
			alert ("Por favor, sua mensagem não deve ter mais de "+ tamanho_maximo +" caracteres.");
			campo.focus();
			return false;
		}
		return true;
	}
	else
		return false;
}

function Limpar(valor, validos) 
{
	// retira caracteres invalidos da string
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++) {
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0) {
			result += valor.substring(i, i+1);
		}
	}
	return result;
}

//Formata número tipo moeda usando o evento onKeyUp
function formataMoeda(campo, event) 
{
	var tecla = keypress(event);

	vr  = Limpar(campo.value,"0123456789");
	tam = vr.length;
	dec = 2;

	if (tam < 18 && tecla != 8)
	{ 
		tam = vr.length; 
	}

	campo.value = vr ;

	if ( (tam > dec) && (tam <= 5) )
	{
		campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; 
	}
	if ( (tam >= 6) && (tam <= 8) )
	{
		campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
	}
	if ( (tam >= 9) && (tam <= 11) )
	{
		campo.value = vr.substr( 0, tam - 8 ) + "." + vr.subprodustr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
	}
	if ( (tam >= 12) && (tam <= 14) )
	{
		campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
	}
	if ( (tam >= 15) && (tam <= 17) )
	{
		campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;
	}
}

function open_window(src, height, width, name, scrollbars, add)
{
	if(width < 250)
		width = 250;

	if(name == null)
		name = "nome";
	
	if(scrollbars == null)
		scrollbars = "no";

	if(add == null)
	{
		add = 70;
	}
	
		
	var janela = null;
	
	total_width = screen.availWidth;
	total_height = screen.availHeight;
	janela = window.open(src, name, 'height='+ (height+add) + ', width=' + width+',toolbar=no,copyhistory=no,location=no,status=no,menubar=no,scrollbars='+scrollbars+',resizable=no,top=0,left=0');
	
	if (!janela)
	{
		if( document.getElementById("conteudo") ) 
		{
			var var_form;
			
			var elemento_div = document.getElementById("conteudo");
			
			var_form = '\n	<div id="msg_popup">\n';
			var_form += '		<form style="background: #FFFFE1;">\n';
			var_form += '			<table width="100%" align="center">\n';
			var_form += '				<tr>\n';
			var_form += '					<td>\n';
			var_form += '						<img src="http://hydrus.videosoft.com.br/websites/templates/padrao/images/atencao_msg_popup.gif" >\n';
			var_form += '					</td>\n';
			var_form += '					<td style="font: 10px verdana;">\n';
			var_form += '						Seu navegador bloqueou uma janela popup do nosso site.<br>\n';
			var_form += '						Para visualizá-la, desabilite o bloqueador de popup do seu navegador.\n';
			var_form += '					</td>\n';
			var_form += '					<td>\n';
			var_form += '						<a href="#" onClick="\n';
			var_form += '														var popup;\n';
			var_form += '														popup = document.getElementById(\'msg_popup\');\n';
			var_form += '														if(popup)\n';
			var_form += '															popup.style.display = \'none\';\n';
			var_form += '											"\n';
			var_form += '						>\n';
			var_form += '							<img src="http://hydrus.videosoft.com.br/websites/templates/padrao/images/fechar_msg_popup.jpg">\n';
			var_form += '						</a>\n';
			var_form += '					</td>\n';
			var_form += '				</tr>\n';
			var_form += '			</table>\n';
			var_form += '		</form>\n';
			var_form += '\n</div><br>\n';
			
			elemento_div.innerHTML = var_form + elemento_div.innerHTML;	
		}
		
		bloqueou_popup = true;
	}
	else
	{
		janela.moveTo(((total_width-width)/2) , ((total_height-height)/2));
	}
}

function open_window_websites(src, height, width, name, scrollbars)
{
	if(name == null)
		name = "nome";

	if(scrollbars == null)
		scrollbars = "no";

	total_width = screen.availWidth;
	total_height = screen.availHeight;
	janela = window.open(src, name, 'height='+ height + ', width=' + width+',toolbar=no,copyhistory=no,location=no,status=no,menubar=no,scrollbars='+scrollbars+',resizable=no,top=0,left=0');
	
	if (!janela)
	{
		if( document.getElementById("conteudo") ) 
		{
			var var_form;
			
			var elemento_div = document.getElementById("conteudo");
			
			var_form = '\n	<div id="msg_popup">\n';
			var_form += '		<form style="background: #FFFFE1;">\n';
			var_form += '			<table width="100%" align="center">\n';
			var_form += '				<tr>\n';
			var_form += '					<td>\n';
			var_form += '						<img src="http://hydrus.videosoft.com.br/websites/templates/padrao/images/atencao_msg_popup.gif" >\n';
			var_form += '					</td>\n';
			var_form += '					<td style="font: 10px verdana;">\n';
			var_form += '						Seu navegador bloqueou uma janela popup do nosso site.<br>\n';
			var_form += '						Para visualizá-la, desabilite o bloqueador de popup do seu navegador.\n';
			var_form += '					</td>\n';
			var_form += '					<td>\n';
			var_form += '						<a href="#" onClick="\n';
			var_form += '														var popup;\n';
			var_form += '														popup = document.getElementById(\'msg_popup\');\n';
			var_form += '														if(popup)\n';
			var_form += '															popup.style.display = \'none\';\n';
			var_form += '											"\n';
			var_form += '						>\n';
			var_form += '							<img src="http://hydrus.videosoft.com.br/websites/templates/padrao/images/fechar_msg_popup.jpg">\n';
			var_form += '						</a>\n';
			var_form += '					</td>\n';
			var_form += '				</tr>\n';
			var_form += '			</table>\n';
			var_form += '		</form>\n';
			var_form += '\n</div><br>\n';
			
			elemento_div.innerHTML = var_form + elemento_div.innerHTML;	
		}
		
		bloqueou_popup = true;
	}
//	else
//	{
//		janela.moveTo(((total_width-width)/2) , ((total_height-height)/2));
//	}
}

function active_loading()
{
	var carregando;
	carregando = document.getElementById('carregando');
	if(carregando)
		carregando.style.display = 'block';
}

function deactive_loading()
{
	var carregando;
	carregando = document.getElementById('carregando');
	if(carregando)
		carregando.style.display = 'none';
}

function confirm_action(pergunta)
{
	if(pergunta)
		if(confirm(pergunta))
		{
			active_loading();
			return true;
		}
		else
		{
			deactive_loading();
			return false;
		}
	else
	{
		active_loading();
		return true;
	}
}

function janela_debug()
{
	if(document.getElementById('execute').height == 0)
	{
		document.getElementById('execute').height = "90%";
		document.getElementById('execute').width = "90%";
	}
	else
	{
		document.getElementById('execute').height = 0;
		document.getElementById('execute').width = 0;		
	}
}

function trim(str)
{
	s = str.replace(/^(\s)*/, '');
	s = s.replace(/(\s)*$/, '');
	return s;
}

var anima_label_flag;
var anima_label_texto;
var anima_label_count;
var anima_label_obj;
function anima_label(texto, obj, first)
{
	if(first)
	{
		anima_label_flag = true;
		anima_label_count = 0;
		anima_label_obj = obj;
		exec = true;
	}
	if(texto)
	{
		anima_label_texto = texto;
	}
	if(texto && !obj && !first)
	{
		exec = false;
	}
	else
	{
		exec = true;
	}
	if(anima_label_flag && anima_label_obj.innerHTML && anima_label_texto)
	{
		temp = anima_label_texto + ' ';
		for(i=0;i<anima_label_count;i++)
		{
			temp += '.';
		}
		anima_label_obj.innerHTML = temp;
		anima_label_count = ((anima_label_count+1)%4);
		if(exec)
		{
			setTimeout('anima_label();', 1000);
		}
	}
}
function stop_anima_label()
{
	anima_label_flag = false;
	anima_label_texto = '';
	anima_label_count = 0;
	anima_label_obj.innerHTML = '&nbsp;';
	anima_label_obj = null;
}

function createFlash(id, name, file, width, height, style)
{
	var text;
	text = '<embed src="' + file + '"';
	text += ' class="' + style + '"';
	text += ' id="' + name +'"';
	text += ' name="' + name + '"';
	
	if(width) 
		text += 'width="' + width + '"';

	if(height) 
		text += 'height="' + height + '"';

	text += ' quality="high"';
	text += ' pluginspage="http://www.macromedia.com/go/getflashplayer"';
	text += ' wmode="transparent"';
	text += ' allowScriptAccess="sameDomain"';
	text += ' type="application/x-shockwave-flash"'; 		
	text += ' pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';

	if(parent.document.getElementById(id))
	{
		parent.document.getElementById(id).innerHTML = text;
	}
	else
	{
		document.getElementById(id).innerHTML = text;
	}
}

function change_sub_menu(go_select)
{
	if (document.getElementById('selecao_atual').value == '-1')
	{
		var temp = 1;
	}
	else
		var temp = document.getElementById('selecao_atual').value;
	
	var id_selected_01 = document.getElementById(temp + '_01');
	var id_selected_02 = document.getElementById(temp + '_02');
	var id_selected_03 = document.getElementById(temp + '_03');
	
	var id_go_select_01 = document.getElementById(go_select + '_01');
	var id_go_select_02 = document.getElementById(go_select + '_02');
	var id_go_select_03 = document.getElementById(go_select + '_03');
	
	if(document.all)
	{		
		id_selected_01.setAttribute('className', 'vssubmenu_esquerda_nao');
		id_selected_02.setAttribute('className', 'vssubmenu_meio_nao');
		id_selected_03.setAttribute('className', 'vssubmenu_direita_nao');
		
		id_go_select_01.setAttribute('className', 'vssubmenu_esquerda');
		id_go_select_02.setAttribute('className', 'vssubmenu_meio');
		id_go_select_03.setAttribute('className', 'vssubmenu_direita');
	}
	else
	{
		id_selected_01.setAttribute('class', 'vssubmenu_esquerda_nao');
		id_selected_02.setAttribute('class', 'vssubmenu_meio_nao');
		id_selected_03.setAttribute('class', 'vssubmenu_direita_nao');
		
		id_go_select_01.setAttribute('class', 'vssubmenu_esquerda');
		id_go_select_02.setAttribute('class', 'vssubmenu_meio');
		id_go_select_03.setAttribute('class', 'vssubmenu_direita');
	}
	
	document.getElementById('selecao_atual').value = go_select;
}

function ajax (url)
{
	var xmlhttp = false;
	
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	else
	{
		if (window.ActiveXObject)
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	if (xmlhttp)
	{
		xmlhttp.open('POST', url, true);
		
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4)
			{
				eval(xmlhttp.responseText);
			}
		}
		xmlhttp.send(null);
	}
	
	return false;
}

function carrega_arquivo(url)
{
	var xmlhttp = false;
	var retorno = false;
	
	try 
	{ 
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
	} 
	catch (e) 
	{ 
		try 
		{ 
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch (E) 
		{ 
			xmlhttp = false; 
		} 
	} 

	if  (!xmlhttp && typeof  XMLHttpRequest != 'undefined' ) 
	{ 
		try  
		{ 
			xmlhttp = new  XMLHttpRequest(); 
		} 
		catch  (e) 
		{ 
			xmlhttp = false ; 
		} 
	} 

	if (xmlhttp)
	{
		xmlhttp.open('GET', url, true);
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState == 4)
			{				
				eval(xmlhttp.responseText);
				return retorno;
			}
		}
		xmlhttp.send(null);
	}	
	return retorno;
}

function carrega_arquivo_sincrono(url)
{
	var xmlhttp = false;
	var retorno = false;
	
	try 
	{ 
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
	} 
	catch (e) 
	{ 
		try 
		{ 
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch (E) 
		{ 
			xmlhttp = false; 
		} 
	} 

	if  (!xmlhttp && typeof  XMLHttpRequest != 'undefined' ) 
	{ 
		try  
		{ 
			xmlhttp = new  XMLHttpRequest(); 
		} 
		catch  (e) 
		{ 
			xmlhttp = false ; 
		} 
	} 

	if (xmlhttp)
	{
		xmlhttp.open('GET', url, false);
		xmlhttp.send(null);
		if (xmlhttp.readyState == 4)
		{				
			eval(xmlhttp.responseText);			
			return retorno;
		}		
	}	
	return retorno;
}
/* --- Data e Hora--- */
var dayarray=new Array("Domingo","Segunda-Feira","Terça-Feira","Quarta-Feira","Quinta-Feira","Sexta-Feira","Sábado")
var montharray=new Array("Janeiro","Fevereiro","Março","Abril","Maio","Junho","Julho","Agosto","Setembro","Outubro","Novembro","Dezembro")
function getthedate(){
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
var dn="AM"
if (hours>=12)
dn="PM"
if (hours>12){
hours=hours-12
}
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
//change font size here
var cdate="<small><font color='000000' face='Arial'><b>"+dayarray[day]+", "+daym+" de "+montharray[month]+" de "+year+" "+hours+":"+minutes+":"+seconds+" "+dn
+"</b></font></small>"
if (document.all)
document.all.clock.innerHTML=cdate
else if (document.getElementById)
document.getElementById("clock").innerHTML=cdate
else
document.write(cdate)
}
if (!document.all&&!document.getElementById)
getthedate()
function goforit(){
if (document.all||document.getElementById)
setInterval("getthedate()",1000)
}
	function addLoadEvent(func) {
	  var oldonload = window.onload;
	  if (typeof window.onload != 'function') {
		window.onload = func;
	  } else {
		window.onload = function() {
		  if (oldonload) {
			oldonload();
		  }
		  func();
		}
	  }
	}
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
addLoadEvent( function(){ add_print_link( 'nav' ) } );
function add_print_link( id ){
  if( !document.getElementById ||
      !document.getElementById( id ) ) return;
  // add extra functions to page tools list
  var print_page = document.getElementById( id );
  // create print link
  var print_function = document.createElement('p');
  print_function.className = 'print-link';
  print_function.onclick = function(){ print_preview(); return false; };
  print_function.appendChild( document.createTextNode( 'Print the Page' ) );
}
function print_preview() {
	// Switch the stylesheet
	setActiveStyleSheet('Print Preview');
	
	// Create preview message
	add_preview_message();
	// Print the page
	window.print();
}
function add_preview_message(){
var main_content = document.getElementById('content');
var main_body = main_content.parentNode;
	if (document.getElementById){
		
		var preview_message = document.createElement('div');
		preview_message.id = 'preview-message';
	
		// Create Heading
		var preview_header = document.createElement('h3');
		var preview_header_text = document.createTextNode('This is a print preview of this page');
		preview_header.appendChild(preview_header_text);
		
		// Create paragraph
		var preview_para = document.createElement('p');
		var preview_para_text = document.createTextNode('Without this message of course. ');
		
		var cancel_function_link = document.createElement('a');
			cancel_function_link.onclick = function(){ cancel_print_preview(); return false; };
			cancel_function_link.setAttribute('href', '#');	
		var cancel_function_link_text = document.createTextNode('Return to the existing page.');
		cancel_function_link.appendChild(cancel_function_link_text);
		preview_para.appendChild(preview_para_text); //
		preview_para.appendChild(cancel_function_link);
		
		// Put it all toegether
		preview_message.appendChild(preview_header); 
		preview_message.appendChild(preview_para);
		main_body.insertBefore(preview_message, main_content);
	}
}
function cancel_print_preview() {
	// Destroy the preview message
	var print_preview = document.getElementById('preview-message');
	var main_body = print_preview.parentNode;
	main_body.removeChild(print_preview);
	
	// Switch back stylesheet
	setActiveStyleSheet('default');
}
function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}
function criaXMLHttp() {
if (typeof XMLHttpRequest != "undefined")
return new XMLHttpRequest();
else if (window.ActiveXObject){
var versoes = ["MSXML2.XMLHttp.5.0",
"MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0",
"MSXML2.XMLHttp", "Microsoft.XMLHttp"
];
}
for (var i = 0; i < versoes.length; i++){
try{
return new ActiveXObject(versoes[i]);
}catch (e) {}
}
throw new Error("Seu browser nao suporta AJAX");
}

function FecharPopupFlash(){
    document.getElementById("div_splash").style.display = 'none';
}
