function mudaTipo(tipo){
  
  if(tipo==4 || tipo==6){
    document.getElementById("propano_butano").style.display="block";
    document.getElementById("gasoleo").style.display="none";
    document.getElementById("propano_canalisado").style.display="none";
  }else if(tipo==7){
    document.getElementById("propano_butano").style.display="none";
    document.getElementById("gasoleo").style.display="block";
    document.getElementById("propano_canalisado").style.display="none";
  }else if(tipo==3){
    document.getElementById("propano_butano").style.display="none";
    document.getElementById("gasoleo").style.display="none";
    document.getElementById("propano_canalisado").style.display="block";
  }
  document.getElementById("mostra_result").style.display="none";
  var btn_sim = document.getElementById('btn_sim');
  var btn_sim2 = document.getElementById('btn_sim2');
  
  btn_sim.style.display="block";
  btn_sim2.style.display="none";
  
}

function validaCampos(val){
  
  if(IsNumeric(val)==false){
    erros_sim+=1;
  }else {
    if(erros_sim>0){
      erros_sim-=1;
    }
  }
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
var erros_sim = 0;
function calcula_e(form){
if (xmlhttp)
  {
    // tenta estabelecer ligação
    try
    {
      
      var tipo = document.getElementById('tenergia');
      var l_gl = document.getElementById('l_gl');
      var preco_gl = document.getElementById('preco_gl');
      var qt_g = document.getElementById('qt_g');
      var preco_g = document.getElementById('preco_g');
     
      var m_g = document.getElementById('m_g');
      var preco_g2 = document.getElementById('preco_g2');
      //var termo = document.getElementById('termo');
      
      
      
      
      //se não tiver sido escolhido tipo de energia apresenta mensagem de erro      
      if(tipo.value==0){ alert("Escolha o tipo de energia a comparar"); return false;}
      
      //se for gasoleo      
      if(tipo.value==7 && (l_gl.value==0 || preco_gl.value==0) ){ alert("Preencha os campos com valores válidos"); return false;}
      
      //se for propano de garrafa      
      if(tipo.value==4 && (qt_g.value==0 || preco_g.value==0) ){ alert("Preencha os campos com valores válidos"); return false;}
      
      //se for propano canalizado    
      if(tipo.value==3 && (m_g.value==0 || preco_g2.value==0 ) ){ alert("Preencha os campos com valores válidos"); return false;}
      
      //se for butano garrafa
      if(tipo.value==6 && (qt_g.value==0 || preco_g.value==0) ){ alert("Preencha os campos com valores válidos"); return false;}
      
              
      //se não existirem erros de preenchimento  
      if(erros_sim>0){ alert("Preencha os campos com valores numéricos válidos."); return false;}
      
      var serverPage="calculo_sim.php";
      var params = "";
      
      var elems = form.elements;
      //alert(elems.length);
      for(i=0;i<=(elems.length-2);i++){
        if(elems[i].type!="submit"){
         // alert(elems[i].type);
          params += elems[i].name + "=" + elems[i].value ;
          if(i<(elems.length-2)){
            params += "&" ;
          }
        }
      }
      
      //alert(params);
      
      
      
     
      xmlhttp.open("POST", serverPage,true);
      
      //Send the proper header information along with the request
      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp.setRequestHeader("Content-length", params.length);
      xmlhttp.setRequestHeader("Connection", "close");
      
      xmlhttp.onreadystatechange = function() {//Chama a função quando o estado mudar
        if(xmlhttp.readyState == 1 ) {
              
        }else if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            if(xmlhttp.responseText){
                var resultado = xmlhttp.responseText;
                resultado = resultado.split("|*|");
                //alert(resultado);
                var obj = document.getElementById('mostra_result');
                
                var resA = document.getElementById('res_anual');
                var resM = document.getElementById('res_mens');
                var resP = document.getElementById('res_perc');
                var btn_sim = document.getElementById('btn_sim');
                var btn_sim2 = document.getElementById('btn_sim2');
                var frase = document.getElementById('frase');
                
                if(obj!=null){
                  
                  resA.innerHTML = resultado[0];
                  resM.innerHTML = resultado[1];
                  resP.innerHTML = resultado[2];
                  
                  obj.style.display = 'block';
                  btn_sim.style.display = 'none';
                  btn_sim2.style.display = 'block';
                  frase.style.display = 'block';
                
                }
                
            }
        }
      }
     
      xmlhttp.send(params);
      return false;
    
    }catch (e){
      alert("Não foi possivel efectuar o calculo.Por favor tente novamente.\n Descrição do erro:" + e.toString());
      return false;
    }
  }else {
    return true;
  }
}


function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}

function vericaForm(frm,valida){
  
  var mensagem = document.getElementById('mensagem');
  if(mensagem!=null){
    mensagem.value = mensagem.value.replace("€",'');
  }
  return verify_form_data(frm,valida,formErros);
}


function tipoEntidade(obj){
  
  var obj1 = document.getElementById("rua");
  var obj2 = document.getElementById("npol");
  var obj3 = document.getElementById("cp1");
  var obj4 = document.getElementById("cp2");
  var obj5 = document.getElementById("localidade");
  var obj6 = document.getElementById("tel");
  var obj7 = document.getElementById("email");
  var obj8 = document.getElementById("cui");
  var obj9 = document.getElementById("nome");
  
  
  var obj10 = document.getElementById("ast_rua");
  var obj11 = document.getElementById("ast_npol");
  var obj12 = document.getElementById("ast_cp");
  
  
  var obj15 = document.getElementById("ast_tel");
  var obj16 = document.getElementById("ast_email");
  var obj17 = document.getElementById("ast_cui");
  var obj18 = document.getElementById("ast_nome");
  
  if(obj.value=="1"){
   
    
    obj1.disabled = true;
    obj1.readonly = true;
    obj1.readOnly = true;
    obj1.className="textbox2 disabled";
    obj10.className="asterisco2";
    
    obj2.disabled = true;
    obj2.readonly = true;
    obj2.readOnly = true;
    obj2.className="textbox2 disabled";
    obj11.className="asterisco2";
    
    obj3.disabled = true;
    obj3.readonly = true;
    obj3.readOnly = true;
    obj3.className="textbox2 disabled";
    obj12.className="asterisco2";
    
    obj4.disabled = true;
    obj4.readonly = true;
    obj4.readOnly = true;
    obj4.className="textbox2 disabled";
    
    obj5.disabled = true;
    obj5.readonly = true;
    obj5.readOnly = true;
    obj5.className="textbox2 disabled";
    
    obj6.disabled = false;
    obj6.readonly = false;
    obj6.readOnly = false;
    obj6.className="textbox2";
    obj15.className="asterisco";
    
    obj7.disabled = false;
    obj7.readonly = false;
    obj7.readOnly = false;
    obj7.className="textbox2";
    obj16.className="asterisco";
    
    obj8.disabled = false;
    obj8.readonly = false;
    obj8.readOnly = false;
    obj8.className="textbox2";
    obj17.className="asterisco";
    
    obj9.disabled = false;
    obj9.readonly = false;
    obj9.readOnly = false;
    
    obj9.className="textbox2";
    obj18.className="asterisco";
   
  }else if(obj.value=="2"){
    
    
    obj1.disabled = false;
    obj1.readonly = false;
    obj1.readOnly = false;
    obj1.className="textbox2";
    obj10.className="asterisco";
    
    obj2.disabled = false;
    obj2.readonly = false;
    obj2.readOnly = false;
    obj2.className="textbox2";
    obj11.className="asterisco";
    
    obj3.disabled = false;
    obj3.readonly = false;
    obj3.readOnly = false;
    obj3.className="textbox2";
    obj12.className="asterisco";
    
    obj4.disabled = false;
    obj4.readonly = false;
    obj4.readOnly = false;
    obj4.className="textbox2";
    
    obj5.disabled = false;
    obj5.readonly = false;
    obj5.readOnly = false;
    obj5.className="textbox2";
    
    obj6.disabled = false;
    obj6.readonly = false;
    obj6.readOnly = false;
    obj6.className="textbox2";
    obj15.className="asterisco";
    
    obj7.disabled = false;
    obj7.readonly = false;
    obj7.readOnly = false;
    obj7.className="textbox2";
    obj16.className="asterisco";
    
    obj8.disabled = true;
    obj8.readonly = true;
    obj8.readOnly = true;
    obj8.className="textbox2 disabled";
    obj17.className="asterisco2";
    
    obj9.disabled = false;
    obj9.readonly = false;
    obj9.readOnly = false;
    obj9.className="textbox2";
    obj18.className="asterisco";
    
    
  }

}

function escolheTipo(obj){

if (xmlhttp)
  {
    
  
    // tenta estabelecer ligação
    try
    {
      
      var tipo = obj.value;
     
      var serverPage="mudaAssunto.php";
      var params = "tipo=" + tipo;
      
      xmlhttp.open("POST", serverPage,true);
      
      //Send the proper header information along with the request
      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp.setRequestHeader("Content-length", params.length);
      xmlhttp.setRequestHeader("Connection", "close");
      
      xmlhttp.onreadystatechange = function() {//Chama a função quando o estado mudar
        if(xmlhttp.readyState == 1 ) {
              
        }else if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            if(xmlhttp.responseText){
                var resultado = xmlhttp.responseText;
                var obj1 = document.getElementById("sel_assunto");
                if(obj1!=null){
                  obj1.innerHTML = resultado;
                }
                
            }
        }
      }
     
      xmlhttp.send(params);
      return false;
    
    }catch (e){
      alert("Não foi possivel efectuar o pedido.Por favor tente novamente.\n Descrição do erro:" + e.toString());
      return false;
    }
  }

}
function escolheDistrito(obj){

if (xmlhttp)
  {
    
  
    // tenta estabelecer ligação
    try
    {
      
      var distrito = obj.value;
     
      var serverPage="mudaConcelhos.php";
      var params = "distrito=" + distrito;
      
      xmlhttp.open("POST", serverPage,true);
      
      //Send the proper header information along with the request
      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp.setRequestHeader("Content-length", params.length);
      xmlhttp.setRequestHeader("Connection", "close");
      
      xmlhttp.onreadystatechange = function() {//Chama a função quando o estado mudar
        if(xmlhttp.readyState == 1 ) {
              
        }else if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            if(xmlhttp.responseText){
                var resultado = xmlhttp.responseText;
                var obj1 = document.getElementById("sel_concelho");
                if(obj1!=null){
                  obj1.innerHTML = resultado;
                }
                
            }
        }
      }
     
      xmlhttp.send(params);
      return false;
    
    }catch (e){
      alert("Não foi possivel efectuar o pedido.Por favor tente novamente.\n Descrição do erro:" + e.toString());
      return false;
    }
  }

}




function validaCUI(){

var obj = document.getElementById('cui');
var entidade = document.getElementById('entidade');

if(entidade.value==2){
  return true;
}



if (xmlhttp)
  {
    
  
    // tenta estabelecer ligação
    try
    {
      
      
      
      var cui = obj.value;
     
      
      var serverPage="validaCUI.php";
      var params = "cui=" + cui;
      
     
      
      
     
      xmlhttp.open("POST", serverPage,true);
      
      //Send the proper header information along with the request
      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp.setRequestHeader("Content-length", params.length);
      xmlhttp.setRequestHeader("Connection", "close");
      
      xmlhttp.onreadystatechange = function() {//Chama a função quando o estado mudar
        if(xmlhttp.readyState == 1 ) {
              
        }else if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            if(xmlhttp.responseText){
                var resultado = xmlhttp.responseText;
                var obj2 = document.getElementById('cui_valido');
                if(resultado==0){
                    obj2.value=0;
                }else{
                     obj2.value=1;
                     
                }
                
            }
        }
      }
     
      xmlhttp.send(params);
     
    
    }catch (e){
     
      return false;
    }
  }

}


var myWidth;
var myHeight;

function getSize() {

  
  if( typeof( window.innerWidth ) == 'number' ) { 
  
  //Non-IE 
  
  myWidth = window.document.body.scrollWidth;
  myHeight = window.document.body.scrollHeight; 
  
  } else if( document.documentElement && 
  
  ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { 
  
  //IE 6+ in 'standards compliant mode' 
  
  myWidth = document.documentElement.scrollWidth; 
  myHeight = document.documentElement.scrollHeight 
  
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { 
  
  //IE 4 compatible 
  
  myWidth = document.body.scrollWidth; 
  myHeight = document.body.scrollHeight; 
  
  }
  var tamanho = new Array();
  tamanho[0] = myWidth;
  tamanho[1] = myHeight; 
  
  return  tamanho;
}

function fechavideo(lg,id) {
  
  for(j=1;j<blc.length;j++){
    var obj1=document.getElementById("flash3_Mov"+blc[i]);
    var obj2=document.getElementById("btn_flash"+blc[i]);
    if(obj1!=null){
      obj1.style.display='none';
    }
    if(obj2!=null){
      obj2.style.display='none';
    }
  }
 
  
  var btn = document.getElementById("btn_flash"+id);
  var flsh = document.getElementById("flash3_Mov"+id);
  
  if(flsh!=null){
    flsh.style.display='none';
    flsh.innerHTML = "";
  }
  if(btn!=null){
    btn.style.display='none';
  }
  var fundo_o = document.getElementById("fundo_opaco");
  if(fundo_o!=null){
    fundo_o.style.display='none';
  }
   
}

function montavideo(lg,id,w,h) {
  
  

    
      
    for(j=1;j<blc.length;j++){
      var obj1=document.getElementById("flash3_Mov"+blc[i]);
      var obj2=document.getElementById("btn_flash"+blc[i]);
      if(obj1!=null){
        obj1.style.display='none';
      }
      if(obj2!=null){
        obj2.style.display='none';
      }
    }
 
  
  var btn = document.getElementById("btn_flash"+id);
  var flsh = document.getElementById("flash3_Mov"+id);
  
  if(flsh!=null){
    flsh.style.display='';
  }
  if(btn!=null){
    btn.style.display='';
  }
  //altera o tamanho do fundo opaco para as dimensões totais da página
  var tam = getSize();
  var fundo_o = document.getElementById("fundo_opaco");
  if(fundo_o!=null){
    fundo_o.style.display='';
    fundo_o.style.width = tam[0]+"px";
    fundo_o.style.height = tam[1]+"px";
  }
  
  
   
  
  
	var fo2 = new FlashObject("flash/video2.swf", "movie"+id,533,300, "9", "000000");
	fo2.addParam("wmode", "transparent");
	fo2.addParam("allowFullScreen", "true");
	fo2.addVariable("servidor", "http://www.edpgasdistribuicao.pt");
  fo2.addVariable("videoID", lg+"_"+id);
  fo2.addVariable("typePlay", true);
	fo2.write("flash3_Mov"+id);
	// ]]>
}

function montavideo2(lg,id,flash,w,h) {
  
  
//alert(flash + "" + w + "" + h)
    
      
    for(j=1;j<blc.length;j++){
      var obj1=document.getElementById("flash3_Mov"+blc[i]);
      var obj2=document.getElementById("btn_flash"+blc[i]);
      if(obj1!=null){
        obj1.style.display='none';
      }
      if(obj2!=null){
        obj2.style.display='none';
      }
    }
 
  
  var btn = document.getElementById("btn_flash"+id);
  var flsh = document.getElementById("flash3_Mov"+id);
  
  if(flsh!=null){
    flsh.style.display='';
  }
  if(btn!=null){
    btn.style.display='';
  }
  //altera o tamanho do fundo opaco para as dimensões totais da página
  var tam = getSize();
  var fundo_o = document.getElementById("fundo_opaco");
  if(fundo_o!=null){
    fundo_o.style.display='';
    fundo_o.style.width = tam[0]+"px";
    fundo_o.style.height = tam[1]+"px";
  }
  
  
   
  
  
	var fo2 = new FlashObject(flash, "movie"+id,w,h, "9", "000000");
	fo2.addParam("wmode", "transparent");
	fo2.addParam("allowFullScreen", "true");
	fo2.addVariable("servidor", "http://www.edpgasdistribuicao.pt");
  //fo2.addVariable("videoID", lg+"_"+id);
  //fo2.addVariable("typePlay", true);
	fo2.write("flash3_Mov"+id);
	// ]]>
}

var numbr=1;
var tipo="Appear";
var randDest;
var Dest=new Array();
function random_dest(){


randDest = self.setInterval(function (){

if(numbr!=Dest.length-1){
  numbr++;
}else{
  numbr=1;
}
//alert(numbr);


//Effect.BlindDown("not_dest", { duration: 1.0,from:0,to:1,afterFinishInternal:function (){Effect.Appear("not_dest", { duration: 14.0,from:1,to:1});}});
Effect.Appear("flash_home", { duration: 5.0,from:1,to:1,afterFinishInternal:function (){Effect.Fade("flash_home", { duration: 5.0,from:1,to:1});}});




muda_img(Dest[numbr]);
},"10000");
}

function muda_img(id){


var obj = document.getElementById("flash_home");

var serverPage ="random_img.php";
var params = "id="+id;


xmlhttp.open("GET",serverPage+"?"+params);
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 1 ) {
//obj.innerHTML="<img src='./sysimages/loader2.gif' border='0' class='border_img'>";
}else if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  obj.innerHTML = xmlhttp.responseText;
  
  /*var nr = document.getElementById("nr_" + id);
  var seta = document.getElementById("seta" + id);
  var td = document.getElementsByTagName("td");
  
  for(i = 0; i <td.length; i++){
    	
        	if(td[i].className=="nr_sel"){
    				  
              td[i].className="nr";
      				
          }
          if(td[i].className=="seta_sel"){
    				  
              td[i].className="seta";
      				
          }
    	}
  
  if(nr!=null){
    nr.className='nr_sel';
  }
  if(seta!=null){
    seta.className='seta_sel';
  }*/
  
}
}
xmlhttp.send(null);
}

var OBJ;
var total;
var elem = new Array();
 function formErros(obj)
{
OBJ=obj;
total=obj.length;

var frmObj=OBJ.form;
objID=obj.id;

  
 elem = frmObj.elements;
 
  for(i = 0; i < elem.length; i++){
  
  if(elem[i].id==objID){
    document.getElementById(objID).style.borderColor="#FF0000";
    
    var lbl=document.getElementById("lbl_" + objID);
    
    if(lbl!=null){
       //lbl.style.color="#FF0000";
    }
    
  }
  
  }
}
// SUBMENU
function showSub(id)
{ 
  
  document.getElementById("subp_" + id).style.display = "block";
  var mnT= document.getElementById("mnT" + id);
  
  var mnW = mnT.clientWidth;
  //alert(mnW);
  var sub_tbl= document.getElementById("tbl_subp_" + id);
  var stW = sub_tbl.clientWidth;
 
  if(sub_tbl!=null){
    if(stW<mnW){
      sub_tbl.style.width=mnW+'px';
    }
    //alert((mnW+28));
    if(mnW<stW){
      //alert(mnW+""+stW);
      sub_tbl.style.width=(mnW+52)+'px';
    }
    
  }
  
}

function hideSub(id){
  document.getElementById(id).style.display = "none";
}

var jpesq;

function verifica_pesq(form) {

var formulario = document.getElementById(form)
var txt=formulario.valor_pesq;
var texto = txt.value;

var error;

  if((txt.value=="") || (txt.value==unescape(jpesq[1]))){
      alert(unescape(jpesq[2]));
      error=1;
  }else if(txt.value!="" &&  texto.length<5){
      alert(unescape(jpesq[3]));
      error=2;
      
  }

  if((txt.value!=unescape(jpesq[1])) && (texto.value!="") && (texto.length>=5)){
     error=0;
  }
  
  if(error>0){
    return false;
  }else {
    return document.getElementById(form).submit();
  }
  
  
}

/* IFRAME POP-UP */
var _xpannel;

function ModalIframePopUp(phpfile, _title, _mensagem, _height, _width)
{
  _xpannel = new YAHOO.widget.Panel("xpannel", 
      {  
      width:_width, 
      fixedcenter:true, 
      close:false, 
      draggable:true, 
      zindex:32200, 
      modal:true, 
      visible:false
      } );

    var HTML_BODY = '<iframe src="'+ phpfile +'" style="width:100%;height:' + _height + ';background-color:transparent;margin: 0px !important; padding: 0px !important;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame" width="100%" height="100%" hspace="0"></iframe>';
    /*_xpannel.setHeader(_title);*/
    _xpannel.setBody(HTML_BODY);
    _xpannel.render(document.body);
    _xpannel.show();
}

function ModalPopUp(phpfile, _title, _mensagem, _width)
{
  _xpannel = new YAHOO.widget.Panel("xpannel", 
      {  
      width:_width, 
      fixedcenter:true, 
      close:false, 
      draggable:true, 
      zindex:32200, 
      modal:true, 
      visible:false
      } );

  var abrejanela = {
          success: function(oResponse) {
          var oResults = oResponse.responseText;
          _xpannel.setHeader(_title);
          _xpannel.setBody(oResults);
          _xpannel.render(document.body);
          _xpannel.show();
          },
          
          failure: function(oResponse) {
            alert('(1) Erro ao carregar informação. Por favor volte a tentar!');
          },
          timeout: 20000
  };

  YAHOO.util.Connect.asyncRequest('GET', phpfile, abrejanela);
}

