var DOM = YAHOO.util.Dom;
var REG = YAHOO.util.Region;
/*
function latHeight(){
	try{
		var alt = REG.getRegion(document.getElementById('engloba'));
		
		var alt2 = alt.height;
		var valor2 = 28;
		
		var valor = alt2-valor2;
		var latmenu = DOM.getElementBy(function(obj) {
				  return (obj.className == "menulat") ? true : false;
				  }, 'div');
				  
		var latReg = REG.getRegion(latmenu);
		
		latRegH = latReg.height;
		
		if (alt2 >= latRegH){
			var minH = valor.toString().concat("px");
		
			latmenu.style.minHeight = minH;
		}
		else{
			var minH = latRegH.toString().concat("px");
			
			document.getElementById('engloba').style.minHeight = minH;
		}
	}
	catch(err){
	}
}

try{
	window.onload = latHeight();
}
catch(err){
}
 */
function selectLink(idlink){
	try{
		var link = document.getElementById(idlink);
		var tagsA = DOM.getElementsByClassName('asel', 'a', 'divtopo');
		
		tagsA[0].className = 'anormal';
		link.className = 'asel';
	}
	catch(err){
	}
}

function selectLinkLat(idlink){
	try{
		var link = document.getElementById(idlink);
		var tagsA = DOM.getElementsByClassName('asel', 'a', 'divlat');
		
		tagsA[0].className = 'anormal';
		link.className = 'asel';
	}
	catch(err){
	}	
}
var temp = 0;
function openPop(idpop, srcimg){
	try{

     var id = document.getElementById(idpop);
		  var idimg = id.getElementsByTagName("div")[1].getElementsByTagName("img");
		 
		  if (idimg && idimg.length > 0) {
			  var childImg = DOM.getChildrenBy(id.getElementsByTagName("div")[1], 
								function(obj){
									return (obj.nodeName == 'IMG') ? true : false;
								});
			
			  id.getElementsByTagName("div")[1].removeChild(childImg[0]);  
		  }
		  
		  var imgnova = document.createElement('img');
		  imgnova.src = srcimg;
		  id.getElementsByTagName("div")[1].appendChild(imgnova);

		  id.style.display = "block";
		  id.style.height = DOM.getDocumentHeight() + "px";

      window.scrollTo(0,0);
	}
	catch(err){
	}
}






function closePop(idpop){
	try{
		var id = document.getElementById(idpop);  
	   id.style.display = "none";
	}
	catch(err){
	}
}

function selectPage(idlink){
	try{
		var tagsA = DOM.getElementsByClassName('asel', 'a', 'divpage');
		
		tagsA[0].className = 'anormal';
		idlink.className = 'asel';
	}
	catch(err){
	}
}

function openPop2(idpop){
	try{
		var id = document.getElementById(idpop);  
		id.style.display = "block"; 
	}
	catch(err){
	}	
}

function openForm(that, idfrm){
	try{
		that.parentNode.style.display = 'none';
		
		document.getElementById(idfrm).style.display = 'block';
		
		/* A div do menu laretal tem que acompanhar o crescimento da página */
		var valor = 28;
		var _menu = document.getElementById('menulat');
		var _regmenu = REG.getRegion(_menu);
	  
		var _total = document.getElementById("inMain");
		var _reg = REG.getRegion(_total);
		
		if(_reg.height >= _regmenu.height){
			_menu.style.height = _reg.height - 3 + "px";
		}
		else{
			document.getElementById("engloba").style.height = _regmenu.height - valor + "px";
	  
			var marca = DOM.getElementBy(function(obj){
							return (obj.className == 'btmarca') ? true : false;
						}, 'div');
	  
			if (marca != ""){
	  
				document.getElementById("menulat").style.borderBottom = '0px';
				document.getElementById("inMain").style.minHeight = _regmenu.height - 3 + "px";
			}
			else{
				document.getElementById("inMain").style.minHeight = _regmenu.height - 2 + "px";
			}
		}
	}
	catch(err){
	}
}

function clickImgSlide(that, idgrd,src){
  try{
		var childs = DOM.getChildrenBy(that, function(obj){
						return (obj.nodeName == 'IMG') ? true : false;
					})				
	  
	   var divdoc = document.getElementsByTagName("div");
  
    for (var i=0; i < divdoc.length; i++){
     if (divdoc[i].className=="imgpeqviasel"){
      divdoc[i].className='imgpeqvia';
      }
    }
		that.className = 'imgpeqviasel';
    if (childs.length >= 1){
			document.getElementById(idgrd).src = src;
        }		
	}
	catch(err){
	}
}

function seeInfo(info){
	try{
		document.getElementById(info).style.display = 'block';
	}
	catch(err){
	}
}
function unInfo(info){
	try{
		document.getElementById(info).style.display = 'none';
	}
	catch(err){
	}
}


function RenderTTF()
{
  DATAFUNCS.RenderFonts('tit_h1bloco',{
   fontSize         : '22.5',
   backgroundColor  : 'FFFFFF',
   color            : '333333',
   fontFile         : 'fonts/Citroen Regular.ttf',
   transparent      : '1'
  });
  DATAFUNCS.RenderFonts('tit_h1nots',{
   fontSize         : '18.5',
   backgroundColor  : 'FFFFFF',
   color            : '333333',
   fontFile         : 'fonts/Citroen Regular.ttf',
   transparent      : '1'
  });
  DATAFUNCS.RenderFonts('tit_h2bloco',{
   fontSize         : '12',
   backgroundColor  : 'FFFFFF',
   color            : '333333',
   fontFile         : 'fonts/Citroen Regular.ttf',
   transparent      : '1'
  });
  DATAFUNCS.RenderFonts('preco_tit',{
   fontSize         : '15',
   backgroundColor  : 'FFFFFF',
   color            : '333333',
   fontFile         : 'fonts/Citroen Bold.ttf',
   transparent      : '1'
  });
 
}
function escolhe_data_entrada() {
 
  var arrDates = cal2.getSelectedDates(); 
  for (var i = 0; i < arrDates.length; ++i) { 
    var date = arrDates[i]; 
  
      
    var mes = date.getMonth() + 1; 
    var ano = date.getFullYear(); 
    var dia = date.getDate(); 
    
    
    document.getElementById("dia").value=dia;
    cal2.hide();
  }
}

var DOM = YAHOO.util.Dom;
var EVT = YAHOO.util.Event;

function validate_form(_obj){
  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.className  = 'obrigaclick required';
  					//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.className  = 'obrigaclick required';
      					//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.className  = 'obriga required';
      					
      					document.getElementById('password').style.border = '1px solid #D3D3D3';
      					document.getElementById('password').style.color  = '#7F7F7F';
      					
              }
  				  
            } else {
    				  var _varid = e.getAttribute('id');
    				  e.className  = 'obriga required';
    				}
  				}
  				
  				if(_emai == 1){
  					if(verify_email(e.value) == false){
  						var _varid = e.getAttribute('id');
              e.className  = 'obrigaclick required';
    					//e.value = 'Email inválido.';
              
  						//EVT.addListener(e, "focus", function(){ e.value = ''; });
  						
  						if(errors == false) errors = true;
  						
  					} else {
  					  var _varid = e.getAttribute('id');
      				e.className  = 'obriga required';
  					}
  				}
  			} else if(_fich == 1){
  			  if(e.value == ''){
            var _varid = e.getAttribute('id');
            e.className  = 'obrigaclick required';
  					//e.value = 'Preenchimento obrigatório.';
  					//EVT.addListener(e, "focus", function(){ e.value = ''; });
  					if(errors == false) errors = true;
          } else {
  					var _varid = e.getAttribute('id');
  				  e.className  = 'obriga required';
  				}
  			} else if(_sel == 1) {
          if ((e.value=="") || (e.value==0)){
            var _varid = e.getAttribute('id');
            e.className  = 'obrigaclick required';
						//EVT.addListener(e, "focus", function(){ e.value = ''; });
						if(errors == false) errors = true;
          } else {
            var _varid = e.getAttribute('id');
  				  e.className  = 'obriga required';
          }
  			} 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(document.getElementById('obriga')){
	   document.getElementById('obriga').src='sysimages/obrigaclick.jpg';
   }
   alert("Verificar os campos obrigatórios");
		return false;
	} else {
	 if(document.getElementById('obriga')){
	   document.getElementById('obriga').src='sysimages/obriga.jpg';
   }
		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){
      alert("Email inválido.");
      erro=1;
    }
  } else {
    alert("Tem que introduzir um email válido.");
    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 verificaAno(valor) {
  var preco1 = document.getElementById("ano").value;
  var preco2 = document.getElementById(valor).value;
  
  if(parseInt(preco1) > 0 && parseInt(preco2) != "") {
    
    if(parseInt(preco1) > parseInt(preco2)) {
      
      document.getElementById("ano2").value = parseInt(preco1);
    
    }
    
  }
  
}

var intervalo = 0;
var variavel = 0;

function verificaPreco(valor) {
  var preco1 = document.getElementById("preco").value;
  var preco2 = document.getElementById(valor).value;
  
  
  //alert(parseInt(preco2));
  
  if(parseInt(preco1) > 0 && parseInt(preco2) != "") {
    
    if(parseInt(preco1) > parseInt(preco2)) {
      
      variavel = parseInt(preco1);
      intervalo = setInterval(func2, 200);
    
    }
    
  }
  
  
  
}





function verificaKil(valor) {
  var preco1 = document.getElementById("quil").value;
  var preco2 = document.getElementById(valor).value;
  
  if(parseInt(preco1) > 0 && parseInt(preco2) != "") {
    
    if(parseInt(preco1) > parseInt(preco2)) {
      
      variavel = parseInt(preco1);
      intervalo = setInterval(func1, 200);
    
    }
    
  }
  
}


function func1() {
  clearInterval(intervalo);
  document.getElementById("quil2").value = variavel;
  variavel = "";
}



function func2() {
  clearInterval(intervalo);
  document.getElementById("preco2").value = variavel;
  variavel = "";
}


/* Picker calendário */
function escolhe_data_entrada(type,args,obj) {
 
  var dates = args[0];   
  var date = dates[0];   
  var ano = date[0], mes = date[1], dia = date[2];
  //obj = obj.split(','); 
  if(dia<10)
    dia = "0"+dia;
  if(mes<10)
    mes = "0"+mes;
  
  document.getElementById("T["+obj+"]").value=dia+'-'+mes+'-'+ano;
  
  document.getElementById("cal2Container_"+obj).style.display='none';
}
/* Picker calendário */













