var DOM = YAHOO.util.Dom;
var REG = YAHOO.util.Region;

function addEventMenuHeader(){
	try{
		DOM.getElementsByClassName('ulmenuheader', 'ul', document.body, function(obj){
		
			DOM.getElementsBy(function(obja){
			
				return (obja.nodeName == 'A') ? true : false;
				
			}, 'a', obj, function(obja){
			
				obja.onclick = function(){
				
					//Esconder eventuais listas abertas
					DOM.getElementsBy(function(objul){
						return (objul.nodeName == 'UL') ? true : false;
					}, 'ul', obj, function(objul){
						objul.style.display = 'none';
					});
				
					var ulchild = DOM.getFirstChildBy(this.parentNode, function(objul){
									return (objul.nodeName == 'UL') ? true : false;
								});
								
					if (ulchild){
						ulchild.style.display = 'block';
					}
					
				};
				
			});
			
		});
	}
	catch(err){
	}
}

function addEventInnerMenuHeader(){
	try{
		DOM.getElementsByClassName('ulinnermenu', 'ul', document.body, function(obj){
		
			DOM.getElementsBy(function(obja){
			
				return (obja.nodeName == 'LI') ? true : false;
				
			}, 'li', obj, function(obja){
			
				obja.onmouseover = function(){
				
					//Esconder eventuais listas abertas
					DOM.getElementsBy(function(objul){
						return (objul.nodeName == 'UL') ? true : false;
					}, 'ul', obj, function(objul){
						objul.style.display = 'none';
					});
				
					var ulchild = DOM.getFirstChildBy(this, function(objul){
									return (objul.nodeName == 'UL') ? true : false;
								});
								
					if (ulchild){
						ulchild.style.display = 'block';
					}
					
				};
				
				obja.onmouseout = function(){
				
					var ulchild = DOM.getFirstChildBy(this, function(objul){
									return (objul.nodeName == 'UL') ? true : false;
								});
								
					if (ulchild){
						ulchild.style.display = 'none';
					}
					
				};
				
			});
			
		});
	}
	catch(err){
	}
}

function addEventInnerInnerMenuHeader(){
	try{
		DOM.getElementsByClassName('ulinnerinnermenu', 'ul', document.body, function(obj){
		
			obj.onmouseover = function(){
			
				var anchorparent = DOM.getElementBy(function(obja){
					return (obja.nodeName == 'A') ? true : false;
				}, 'a', this.parentNode);
				
				anchorparent.className = 'asel';
				
			};
			
			obj.onmouseout = function(){
			
				var anchorparent = DOM.getElementBy(function(obja){
					return (obja.nodeName == 'A') ? true : false;
				}, 'a', this.parentNode);
				
				anchorparent.className = 'anormal';
				
			};
			
		});
	}
	catch(err){
	}
}

function addEventTabs(){
	try{
		//Tabs Produtos
		DOM.getElementsByClassName('ultab', 'ul', document.body, function(obj){
		
			DOM.getElementsBy(function(obja){
			
				return (obja.nodeName == 'A') ? true : false;
				
			}, 'a', obj, function(obja){
			
				obja.onclick = function(){
				
					var divchild = DOM.getElementBy(function(objd){
									return (objd.nodeName == 'DIV') ? true : false;
								}, 'div', this.parentNode);		
					
					if (DOM.getStyle(divchild, 'display') == 'block'){
						
						divchild.style.display = 'none';
						this.className = 'anormal';
						
					}
					else{
						
						//Esconder eventuais divs abertas
						DOM.getElementsBy(function(objd){
							return (objd.nodeName == 'DIV') ? true : false;
						}, 'div', obj, function(objd){
							objd.style.display = 'none';
						});
						
						//Alterar a classe da tab seleccionada
						var asel = DOM.getElementBy(function(objsel){
									return (objsel.className == 'asel') ? true : false;
								 }, 'a', obj);		
						
						asel.className = 'anormal';
						
						divchild.style.display = 'block';
						this.className = 'asel';
						
					}

				};
				
			});
			
		});
		//Tabs Produtos
		
		//Tabs Contactos
		DOM.getElementsByClassName('ultab2', 'ul', document.body, function(obj){
		
			DOM.getElementsBy(function(obja){
			
				return (obja.nodeName == 'A') ? true : false;
				
			}, 'a', obj, function(obja){
			
				obja.onclick = function(){
				
					var divchild = DOM.getElementBy(function(objd){
									return (objd.nodeName == 'DIV') ? true : false;
								}, 'div', this.parentNode);		
					
					if (DOM.getStyle(divchild, 'display') == 'block'){
						
						divchild.style.display = 'none';
						this.className = 'anormal';
						this.parentNode.getElementsByTagName('span')[0].style.visibility = 'hidden';
					}
					else{
						//Simular onmouseout
						var spanimg = DOM.getElementBy(function(objsel){
										return (objsel.nodeName == 'SPAN') ? true : false;
									 }, 'span', obja.parentNode);	
							
						this.style.background = '#F5F5F5';
						spanimg.style.visibility = 'hidden';
						
						//Esconder eventuais divs abertas
						DOM.getElementsBy(function(objd){
							return (objd.nodeName == 'DIV') ? true : false;
						}, 'div', obj, function(objd){
							objd.style.display = 'none';
						});
						
						//Esconder eventuais imagens a serem mostradas
						DOM.getElementsBy(function(objd){
							return (objd.nodeName == 'SPAN') ? true : false;
						}, 'span', obj, function(objd){
							objd.style.visibility = 'hidden';
						});
						
						//Alterar a classe da tab seleccionada
						var asel = DOM.getElementBy(function(objsel){
									return (objsel.className == 'asel') ? true : false;
								 }, 'a', obj);		
						
						if (asel != ''){
							asel.className = 'anormal';
							asel.style.background = '#F5F5F5';
						}
						
						divchild.style.display = 'block';
						this.className = 'asel';
						this.style.background = 'url(sysimages/atabsel.png) no-repeat left top';
						this.parentNode.getElementsByTagName('span')[0].style.visibility = 'visible';
						
					}
					
				};
				
				obja.onmouseover = function(){	
					
					if (this.className == 'anormal'){ 
						var spanimg = DOM.getElementBy(function(objsel){
										return (objsel.nodeName == 'SPAN') ? true : false;
									 }, 'span', obja.parentNode);	
							
						this.style.background = 'url(sysimages/atab_hover.png) no-repeat left top';
						spanimg.style.visibility = 'visible';
					}
					
				};
				
				obja.onmouseout = function(){
					
					if (this.className == 'anormal'){
						var spanimg = DOM.getElementBy(function(objsel){
										return (objsel.nodeName == 'SPAN') ? true : false;
									 }, 'span', obja.parentNode);	
							
						this.style.background = '#F5F5F5';
						spanimg.style.visibility = 'hidden';
					}
					
				};
				
			});
			
		});
		//Tabs Contactos
	}
	catch(err){
	}
}

function addEventNews(){
	try{
		DOM.getElementsByClassName('noticia', 'div', document.body, function(obj){
		
			obj.onmouseover = function(){
				
				try{
					var ptit = DOM.getElementBy(function(objn){
									return (objn.className == 'tit') ? true : false;
							   }, 'p', obj);
				
					var ptxt = DOM.getElementBy(function(objn){
									return (objn.className == 'txt') ? true : false;
							   }, 'p', obj);
		
					ptit.style.textDecoration = 'underline';
					
					ptxt.style.textDecoration = 'underline';
				}
				catch(err){
				}
				
			};
			
			obj.onmouseout = function(){
				
				try{
					var ptit = DOM.getElementBy(function(objn){
									return (objn.className == 'tit') ? true : false;
							   }, 'p', obj);
				
					var ptxt = DOM.getElementBy(function(objn){
									return (objn.className == 'txt') ? true : false;
							   }, 'p', obj);
		
					ptit.style.textDecoration = 'none';
					
					ptxt.style.textDecoration = 'none';
				}
				catch(err){
				}
				
			};
			
		});
	}
	catch(err){
	}
}

YAHOO.util.Event.onDOMReady(function(){
	try{
		addEventMenuHeader();
		addEventInnerMenuHeader();
		addEventInnerInnerMenuHeader();
		addEventTabs();
		addEventNews();
		
		if(/firefox/.test(navigator.userAgent.toLowerCase()))
		{
			var sepbt = document.getElementById('sepbt');
			sepbt.style.left = '155px';
			sepbt.style.top = '2px';
		}
		
		var imgPre = new Image();
		imgPre.src="sysimages/saiba_mais_hover.png";
	}
	catch(err){
	}
}, document.body, true);

function openCloseMapa(){
	try{
		if(DOM.getStyle(document.getElementById('mapacont'), 'visibility') == 'hidden'){
			
			document.getElementById('mapacont').style.visibility = 'visible';
			document.getElementById('wrapperleftright').style.display = 'none';
			
		}
		else{
		
			document.getElementById('mapacont').style.visibility = 'hidden';
			document.getElementById('wrapperleftright').style.display = 'block';
			
		}
	}
	catch(err){
	}
}

function heightHome(){
	try{
		var hHome = DOM.getDocumentHeight();
		
		document.getElementById('home').style.height = hHome + 'px';
	}
	catch(err){
	}
}




var DOM = YAHOO.util.Dom;
var EVT = YAHOO.util.Event;

function validate_form(_obj,lg){
  var errors = false;

	DOM.getElementsByClassName(
		'required',
		null,
		_obj,
		function(e){
  			var _tag  = e.tagName;
  			var _type = e.getAttribute('type');
  			var _emai = e.getAttribute('email');
  			var _fich = e.getAttribute('fich');
        var _sel  = e.getAttribute('selecta');
        
        
  			if(_tag == 'TEXTAREA' || _type == 'text' || _type == 'password'){
  				if ((e.value == '') || (e.value == 'Preenchimento obrigatório.')){
            var _varid = e.getAttribute('id');
            e.style.border = '1px solid #D20000';
  					e.style.color  = '#D20000';
  					//e.value = 'Preenchimento obrigatório.';
            
  					//EVT.addListener(e, "focus", function(){ e.value = ''; });
  					
  					if(errors == false) errors = true;
  					
  				} else {
  				  var idinput =  e.getAttribute('id');
  				  if (idinput=='confpassword') {
  				    var pass1 = document.getElementById('password').value;
  
  				    if (pass1 != e.value){
                //alert("As passwords introduzidas não são iguais.")
                
                var _varid = e.getAttribute('id');
                e.style.border = '1px solid #D20000';
      					e.style.color  = '#D20000';
      					//e.value = 'Preenchimento obrigatório.';
      					
      					document.getElementById('password').style.border = '1px solid #D20000';
      					document.getElementById('password').style.color  = '#D20000';
      					//document.getElementById('password').value = 'Preenchimento obrigatório.';
                
      					//EVT.addListener(e, "focus", function(){ e.value = ''; });
      					
      					if(errors == false) errors = true;
              } else {
                var _varid = e.getAttribute('id');
      				  e.style.border = '1px solid #D3D3D3';
      					e.style.color  = '#7F7F7F';
      					
      					document.getElementById('password').style.border = '1px solid #D3D3D3';
      					document.getElementById('password').style.color  = '#7F7F7F';
      					
              }
  				  
            } else {
    				  var _varid = e.getAttribute('id');
    				  e.style.border = '1px solid #D3D3D3';
    					e.style.color  = '#7F7F7F';
    				}
  				}
  				
  				if(_emai == 1){
  					if(verify_email(e.value) == false){
  						var _varid = e.getAttribute('id');
              e.style.border = '1px solid #D20000';
    					e.style.color  = '#D20000';
    					//e.value = 'Email inválido.';
              
  						//EVT.addListener(e, "focus", function(){ e.value = ''; });
  						
  						if(errors == false) errors = true;
  						
  					} else {
  					  var _varid = e.getAttribute('id');
      				e.style.border = '1px solid #D3D3D3';
    					e.style.color  = '#7F7F7F';
  					}
  				}
  			} else if(_fich == 1){
  			  if(e.value == ''){
            var _varid = e.getAttribute('id');
            e.style.border = '1px solid #D20000';
  					e.style.color  = '#D20000';
  					//e.value = 'Preenchimento obrigatório.';
  					//EVT.addListener(e, "focus", function(){ e.value = ''; });
  					if(errors == false) errors = true;
          } else {
  					var _varid = e.getAttribute('id');
  				  e.style.border = '1px solid #D3D3D3';
  					e.style.color  = '#7F7F7F';
  				}
  			} else if(_sel == 1) {
          if ((e.value=="") || (e.value==0)){
            var _varid = e.getAttribute('id');
            e.style.border = '1px solid #D20000';
  					e.style.color  = '#D20000';
						//EVT.addListener(e, "focus", function(){ e.value = ''; });
						if(errors == false) errors = true;
          } else {
            var _varid = e.getAttribute('id');
  				  e.style.border = '1px solid #D3D3D3';
  					e.style.color  = '#7F7F7F';
          }
  			} else if(_type == 'checkbox') {
  				if(e.checked == false){
  					//alert('Tem de dizer se aceita os termos e condições de funcionamento do site.');
  					if(errors == false) errors = true;
  				}
  			}
  		
		}
	);
	
	if(errors == true){
	  if(lg== "pt"){
      alert("Verificar os campos obrigatórios"); 
    }else if(lg== "gb"){
      alert("Check required fields");
    }else if(lg== "fr"){
      alert("Vérifiez les champs obligatoires");
    }
	  
		return false;
	} else {
	
		return true;
	}
	
}

function verify_email(_mail){
	
  var status = false;     
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
  if (_mail.search(emailRegEx) == -1) {
		status = false;
	} else {
		status = true;
	}
	return status;
}

function valida_email(email){
  var e = email.value;
  var erro = 0;
  if (e!=''){
    if (verify_email(e) == false){

      erro=1;
    }
  } else {

    erro=1;
  }
  if(erro==1){  
    return false;
  } else {
    return true;
  }
}

function verify_number(field){
  if (isNaN(field.value)) 
  {
    alert("Só pode introduzir caracteres numéricos.");
    field.value='';
    field.focus();
  }
}
function ver_pesquisa2(p,lg){

    var _p = document.getElementById(p);
    if(_p.value.length<3){
      if(lg == "pt"){
      alert("Deve preencher a sua pesquisa com no minimo 3 letras");
      }else if(lg == "gb"){
      alert("Must fill in your search with at least 3 letters");
      }else if(lg == "fr"){
      alert("Devez remplir votre recherche avec au moins 3 lettres");
      }
      return false;
    } else {
      return true;
    } 
  
}


var contador = 0;
function ver_pesquisa(p,lg){
  var _p = document.getElementById(p);
  
  if(contador == 1){
    if(_p.value.length<3){
      if(lg == "pt"){
      alert("Deve preencher a sua pesquisa com no minimo 3 letras");
      }else if(lg == "gb"){
      alert("Must fill in your search with at least 3 letters");
      }else if(lg == "fr"){
      alert("Devez remplir votre recherche avec au moins 3 lettres");
      }
      return false;
    } else {
      return true;
    } 
  } else if(contador == 0){
    
      if(lg == "pt"){
      alert("Deve preencher a sua pesquisa com no minimo 3 letras");
      }else if(lg == "gb"){
      alert("Must fill in your search with at least 3 letters");
      }else if(lg == "fr"){
      alert("Devez remplir votre recherche avec au moins 3 lettres");
      }
      return false;
   

    } 
  return false;
}

function mudar_glider_next(){
  my_glider.next();
}


