// JavaScript Document

function flash(largura, altura, arquivo){
	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="menu" value="false" /><param name="wmode" value="transparent" />');
	document.write('<param name="movie" value="'+arquivo+'" /><param name="quality" value="best" /><param name="quality" value="best" /><embed src="'+arquivo+'" wmode="transparent" menu="false" quality="best" width="'+largura+'" height="'+altura+'" name="swf" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}

function popup(url,larg,alt){
	
  calcleft = (screen.width / 2) - (larg / 2);
  calctop = (screen.height / 2) - (alt / 2);
  
  window.open(url,"","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width="+larg+",height="+alt+",left="+calcleft+",top="+calctop);
  
}

function popup_scroll(url,larg,alt){
	
  calcleft = (screen.width / 2) - (larg / 2);
  calctop = (screen.height / 2) - (alt / 2);
  
  window.open(url,"","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width="+larg+",height="+alt+",left="+calcleft+",top="+calctop);
  
}

function SomenteNumero(e){
    var tecla=(window.event)?event.keyCode:e.which;
    if((tecla > 47 && tecla < 58)) return true;
    else{
    if (tecla != 8) return false;
    else return true;
    }
}

function SwitchMenu(div, obj){
	
	if(document.getElementById){
		
	var el = document.getElementById(obj);
	var ar = document.getElementById(div).getElementsByTagName("div");
	
		if(el.style.display == "none"){
			
			for (var i=0; i<ar.length; i++){
				ar[i].style.display = "none";
				
			}
			
			el.style.display = "block";
			
		}else{
			
			el.style.display = "none";
			
		}
	}
}

MaskInput = function(f, m){
	function mask(e){
		var patterns = {"1": /[A-Z]/i, "2": /[0-9]/, "4": /[À-ÿ]/i, "8": /[.]/ },
			rules = { "a": 3, "A": 7, "9": 2, "C":5, "c": 1, "*": 8};
		function accept(c, rule){
			for(var i = 1, r = rules[rule] || 0; i <= r; i<<=1)
				if(r & i && patterns[i].test(c))
					break;
				return i <= r || c == rule;
		}
		var k, mC, r, c = String.fromCharCode(k = e.key), l = f.value.length;
		(!k || k == 8 ? 1 : (r = /^(.)\^(.*)$/.exec(m)) && (r[0] = r[2].indexOf(c) + 1) + 1 ?
			r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : accept(c, r[1]) || r[0]
			: (l = (f.value += m.substr(l, (r = /[A|9|C|\*]/i.exec(m.substr(l))) ?
			r.index : l)).length) < m.length && accept(c, m.charAt(l))) || e.preventDefault();
	}
	for(var i in !/^(.)\^(.*)$/.test(m) && (f.maxLength = m.length), {keypress: 0, keyup: 1})
		addEvent(f, i, mask);
};

addEvent = function(o, e, f, s){
	var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
	r[r.length] = [f, s || o], o[e] = function(e){
		try{
			(e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
			e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
			e.target || (e.target = e.srcElement || null);
			e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
		}catch(f){}
		for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
		return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
	for(var i = (e = o["_on" + e] || []).length; i;)
		if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
			return delete e[i];
	return false;
};

function direcionar(url, alvo){
	
	var janela = window.open(url, alvo); 
	janela.window.focus();
	
}

function Contar(campo, conta, valor){

	if((valor-campo.value.length) <= 0){
	   alert('Atenção! Você atingiu o limite máximo de caracteres!');
	   campo.value = campo.value.substr(0,valor);
	}
	
	document.getElementById(conta).value = valor-campo.value.length
}

function Browser() {

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isOP    = false;  // Opera
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOP = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }

  s = "MSIE";
  if ((i = ua.indexOf(s))) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
}

var browser = new Browser();

var botaoAtivo = null;

var divFrame = null;

function clicaBotao(event, menuId) {

  var botao;

  if (browser.isIE)
    botao = window.event.srcElement;
  else
    botao = event.currentTarget;

  if (botao.menu == null) {
    botao.menu = document.getElementById(menuId);
  }
  
  if (botaoAtivo != null)
    fechaMenu(botaoAtivo);

  if (botao != botaoAtivo) {
    abreMenu(botao);
    botaoAtivo = botao;
  } else {
    botaoAtivo = null;
  }
  
  return false;

}

function abreMenu(botao) {

  botao.menu.style.zIndex = 4;

  if (browser.isIE) {

    x = getPageOffsetLeft(botao.menu);
    y = getPageOffsetTop(botao.menu);
    
    w = botao.menu.offsetWidth;
    h = botao.menu.offsetHeight;
  
    frameM = document.getElementById("iFrameMenu");
    
    frameM.style.left = x + "px";
    frameM.style.top  = y + "px";
    frameM.style.width = w + "px";
    frameM.style.height  = h + "px";
    
    frameM.style.zIndex = (botao.menu.style.zIndex-1);
    
    frameM.style.visibility = "visible";
  }

  botao.menu.style.visibility = "visible";
}

function fechaMenu(botao) {

  if (botao.menu != null) {

    if (browser.isIE) {

        frameM = document.getElementById("iFrameMenu");
  
        frameM.style.visibility = "hidden";
      }

    botao.menu.style.visibility = "hidden";
  }
}


function getPageOffsetLeft(el) {

  var x;

  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);

  return x;
}

function getPageOffsetTop(el) {

  var y;

  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
}

function conta_horas(){
	momentoAtual = new Date()
	hora = momentoAtual.getHours()
	minuto = momentoAtual.getMinutes()
		
	str_minuto = new String (minuto)
	if (str_minuto.length == 1) 
		minuto = "0" + minuto

	str_hora = new String (hora)
	if (str_hora.length == 1) 
		hora = "0" + hora
		
	horaImprimible = hora + ":" + minuto
	
	document.form_reloj.reloj.value = horaImprimible
	
	setTimeout("conta_horas()",1000)
}

function MM_jumpMenu(targ, selObj, restore){
	
	if(selObj.options[selObj.selectedIndex].value!=0){
	
	var janela = window.open(selObj.options[selObj.selectedIndex].value, targ,"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=800,height=570,top=0,left=0"); 
	janela.window.focus();
	
	}
}

//AJAX

function ajaxInit() {
 var req;
 try {
  req = new ActiveXObject("Microsoft.XMLHTTP");
 } catch(e) {
  try {
   req = new ActiveXObject("Msxml2.XMLHTTP");
  } catch(ex) {
   try {
    req = new XMLHttpRequest();
   } catch(exc) {
    alert("Esse browser não tem recursos para uso do Ajax");
    req = null;
   }
  }
 }
 
 return req;
}

function carregar(url, id, carregando){
	
	xmlhttp = ajaxInit();
	
    var conteudo = document.getElementById(id);

	   xmlhttp.open("GET", url, true);
	   
    xmlhttp.onreadystatechange=function() {
		
		if (xmlhttp.readyState == 1) {
	  		document.getElementById(id).innerHTML = carregando;    
	  	}	

        if (xmlhttp.readyState==4){	

            var texto=xmlhttp.responseText

            texto=texto.replace(/\+/g," ")
            texto=unescape(texto)
			
			if (xmlhttp.status == 200) {
	  			var conteudo=document.getElementById(id);
            	conteudo.innerHTML=texto;
	  		}	
        }
    }
    xmlhttp.send(null)
}

function carregar_lista(url, valor, id, carregando){
	
	xmlhttp = ajaxInit();
	
    var conteudo = document.getElementById(id);
	
	if(valor!=0){

	   xmlhttp.open("GET", url+valor, true);
	   
		xmlhttp.onreadystatechange=function() {
			
			if (xmlhttp.readyState == 1) {
				document.getElementById(id).innerHTML = carregando;    
			}	
	
			if (xmlhttp.readyState==4){	
	
				var texto=xmlhttp.responseText
	
				texto=texto.replace(/\+/g," ")
				texto=unescape(texto)
				
				if (xmlhttp.status == 200) {
					var conteudo=document.getElementById(id);
					conteudo.innerHTML=texto;
				}	
			}
		}
		
		xmlhttp.send(null)
	
	}
}

function Dados(valor, selected, caminho, text, id) {
	
  ajax = ajaxInit();
  
  if(ajax) {
	  
	 document.getElementById(selected).options.length = 1;
	 
	 idOpcao = document.getElementById(id);
	 
	 ajax.open("POST", caminho, true);
	 ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 
	 ajax.onreadystatechange = function() {
		 
		if(ajax.readyState == 1) {
		   idOpcao.innerHTML = "Carregando...!";   
		}
		
		if(ajax.readyState == 4 ) {
			
		   if(ajax.responseXML) {
			   
			  processXML(ajax.responseXML, selected, text, id);
			  
		   }else {

			   idOpcao.innerHTML = "Sem "+selected;
		   }
		}
	 }
	
	 var params = "id="+valor;
	 ajax.send(params);
	 
  }
}

function processXML(obj, selected, text, id){
	
  var dataArray   = obj.getElementsByTagName(selected);
  
  idOpcao = document.getElementById(id);
  

  if(dataArray.length > 0) {
	 
	 for(var i = 0 ; i < dataArray.length ; i++) {
		var item = dataArray[i];
		
		var codigo    =  item.getElementsByTagName("codigo")[0].firstChild.nodeValue;
		var descricao =  item.getElementsByTagName("descricao")[0].firstChild.nodeValue;
		
		idOpcao.innerHTML = "Selecione";
		 
		var novo = document.createElement("option");

			novo.setAttribute("id", id);
	
			novo.value = codigo;
	
			novo.text  = descricao;
	
			document.getElementById(selected).options.add(novo);
	 }
  }else {

	idOpcao.innerHTML = "Sem "+selected;
  }	  
}

function makePOSTRequest(url, parameters, div_status, carregando, url_erro) {
	
  http_request = ajaxInit();
  
  http_request.onreadystatechange = function(){
	  
	  if (http_request.readyState == 1) {
		  
	  		document.getElementById(div_status).innerHTML = carregando;    
			
	  }	
	  
	  if (http_request.readyState == 4) {
	  
		 if (http_request.status == 200) {
			 
			result = http_request.responseText;
			
			if (result==1) {
				
				direcionar(url_erro,'_parent');
				
			} else {
				
				result=result.replace(/\+/g," ");
            	result=unescape(result);
				
				document.getElementById(div_status).innerHTML = result; 
			
			}
	
		 } else {
			 
			alert('There was a problem with the request.');
			
		 }
	  }
	  
  };
  
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
  
}

