// JavaScript Document

/* VALIDAÇÃO DE FORMULÁRIOS */
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.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 #A8A8A8';
      					e.style.color  = '#3D3938';
      					
      					document.getElementById('password').style.border = '1px solid #A8A8A8';
      					document.getElementById('password').style.color  = '#3D3938';
      					
              }
  				  
            } else {
    				  var _varid = e.getAttribute('id');
    				  e.style.border = '1px solid #A8A8A8';
    					e.style.color  = '#3D3938';
    				}
  				}
  				
  				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 #A8A8A8';
    					e.style.color  = '#3D3938';
  					}
  				}
  			} 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 #A8A8A8';
  					e.style.color  = '#3D3938';
  				}
  			} else if(_sel == 1) {
          if ((e.value=="") || (e.value==0) || (e.value=="Seleccione uma Opção")){
            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 #A8A8A8';
  					e.style.color  = '#3D3938';
          }
  			} 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('txt_erro')){
	   document.getElementById('txt_erro').style.display='block';
   }
    alert("Tem de preencher todos os campos obrigatórios.")
		return false;
	} else {
	 if(document.getElementById('txt_erro')){
	   document.getElementById('txt_erro').style.display='none';
   }
		return true;
	}
	
}

function validate_form_peq(_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');
		  
      if(_type == 'text'){
  				if (e.value == ''){
            
            var _varid = e.getAttribute('id');
  					e.style.color  = '#D20000';
  					if(errors == false) errors = true;
  					
  				} else {
  				  var _varid = e.getAttribute('id');
  					e.style.color  = '#999999';
  				}
  				
  				if(_emai == 1){
  					if(verify_email(e.value) == false){
  						var _varid = e.getAttribute('id');
    					e.style.color  = '#D20000';
  						if(errors == false) errors = true;
  						
  					} else {
  					  var _varid = e.getAttribute('id');
    					e.style.color  = '#999999';
  					}
  				}
  			}
		}
	);
	
	if(errors == true){
		return false;
	} else {
		return true;
	}
}

function valida_log(){
  var errors = false;
  var l = document.getElementById('T1');
  var p = document.getElementById('T2');
  
  if(l.value=='') {
    l.style.color  = '#D20000';
    if(errors == false) errors = true;
  } else {
    l.style.color  = '#999999';
  }
  
  if(verify_email(l.value) == false){
    l.style.color  = '#D20000';
    if(errors == false) errors = true;
  } else {
    l.style.color  = '#999999';
  }
  
  if (p.value=='') {
    p.style.color  = '#D20000';
    if(errors == false) errors = true;
  } else {
    p.style.color  = '#999999';
  }
  
  if(errors == true){
		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){
      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();
  }
}


/* VALIDAÇÃO DE FORMULÁRIOS */

/* Picker calendário */
function escolhe_data_entrada(type,args,obj) {
  //alert(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(obj[0]).value=dia+'-'+mes+'-'+ano;
    
  cal2.hide();
}
/* Picker calendário */

/*

function openSubmenu(submenu){
  var Submenu = document.getElementById(submenu);  
  Submenu.style.display = "block";  
}

function hideSubmenu(submenu){
  var Submenu = document.getElementById(submenu);  
  Submenu.style.display = "none";  
}
*/
function escondeMostra(id){
  var tabSel = document.getElementById(id);
  var cls = (tabSel.className == 'subSel') ? 'sub' : 'subSel';
  tabSel.className = cls;
}





function toggle (obj, cls1, cls2)
{
obj=document.getElementById(obj);
  obj.className = (obj.className == cls1) ? cls2 : cls1;
}


function showFamilia(x,y,z,a){
  if(document.getElementById(y).style.display=='block'){ 
  
      document.getElementById(y).style.display='none';
      document.getElementById(x).style.backgroundColor='#e9e9e9';
      document.getElementById(x).style.color='#a7a7a7';
      
      
    } else { 
      document.getElementById(y).style.display='block'; 
      document.getElementById(x).style.backgroundColor='#cacaca'; 
      document.getElementById(x).style.color='#000000';
      document.getElementById(z).style.display='none';
      document.getElementById(a).style.backgroundColor='#e9e9e9';
      document.getElementById(a).style.color='#a7a7a7';
    }
}






function showFamilia2(x,y,z,a){
  if(document.getElementById(y).style.display=='block'){ 
  
      document.getElementById(y).style.display='none';
      document.getElementById(x).style.backgroundColor='#e9e9e9';
      document.getElementById(x).style.color='#a7a7a7';
      
      
    } else { 
      document.getElementById(y).style.display='block'; 
      document.getElementById(x).style.backgroundColor='#cacaca'; 
      document.getElementById(x).style.color='#000000';
      document.getElementById(z).style.display='none';
      document.getElementById(a).style.backgroundColor='#e9e9e9';
      document.getElementById(a).style.color='#a7a7a7';
    }
}














/* caixa selecção pesquisa */
function showOption(y, a, option){
  var z = document.getElementById(y);
  var x = document.getElementById(a);
  x.style.display = "none";
  z.innerHTML = option;
}

function showLatEsq(y,x,k,j){
  var z = document.getElementById(x);
  var zy = document.getElementById(y);
  var kk = document.getElementById(k);
  var jj = document.getElementById(j);
  zy.src = "sysimages/menos.jpg";
  if(z.style.display == "block"){
      z.style.display = "none";
      zy.src = "sysimages/mais_esq.png";
        kk.style.backgroundColor = "";
        jj.style.backgroundColor = "";
         kk.style.color="#F5B600";
      
  }
  else{
  z.style.display = "block";
  kk.style.backgroundColor = "#c6e2f0";
  jj.style.backgroundColor = "#c6e2f0";
  zy.src = "sysimages/menos.jpg";
  kk.style.color="#0075b7";
  }
}
/* caixa selecção pesquisa */

function changeContactos(bt, cont){
  var button = document.getElementById(bt);
  var content = document.getElementById(cont);
    
  
  var subs = document.getElementsByTagName("div");
  
  for (var i=0; i<subs.length; i++) {
  
    if (subs[i].className == "zona"){
      subs[i].className = "zona_h";
    }
    
    if (subs[i].className == "bt_cont_sel"){
      subs[i].className = "bt_cont";
    }
  }
  
  button.className = "bt_cont_sel";
  content.className = "zona";
    
}
/*
function load_image(obj)
{
  var img_place = document.getElementById('img_placeholder');
  
  if(img_place)
  {
    img_place.src = obj.href;
  }

  return false;
}*/

function load_image(obj,imgG,elem)
{
  var img_place = document.getElementById('img_placeholder');
  var _elem = document.getElementById(elem);
  if(img_place)
  {
    img_place.src = obj.href;
  }
  document.getElementById('lnkmais').href = imgG;
   var subs = document.getElementsByTagName("img");
  
  for (var i=0; i<subs.length; i++) {
    if (subs[i].className == "sel_img"){
      subs[i].className = "nada";
    } 
  }
  _elem.className='sel_img';
  //alert(_elem.className);
  return false;
}

function load_image_novo(obj,imgG,elem)
{
  var img_place = document.getElementById('img_placeholder');
  var _elem = document.getElementById(elem);
  if(img_place)
  {
    img_place.src = obj.href;
  }
  
  //document.getElementById('lnkmais').href = imgG;
  document.getElementById('lnkmais').onclick = function(){ void(viewer.show(elem)); };
  var subs = document.getElementsByTagName("img");
  
  for (var i=0; i<subs.length; i++) {
    if (subs[i].className == "sel_img"){
      subs[i].className = "nada";
    } 
  }
  _elem.className='sel_img';
  //alert(_elem.className);
  return false;
}


function getXmlHttpRequest() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function mudaModelo()
{
  var marca = document.getElementById('Pmarca').value;
 
  var url ="mudaMOD.php"; 	
  var post_data="marca="+marca;
  var cmsajaxhttp = getXmlHttpRequest();
 	cmsajaxhttp.open("GET", url+'?'+post_data, true);
  cmsajaxhttp.onreadystatechange = function(){
		if (cmsajaxhttp.readyState==4){
			var resultado = cmsajaxhttp.responseText;
      document.getElementById("div_list_menu_02MOD").innerHTML=resultado;
		}
	}
 	cmsajaxhttp.send(null);
}


function RenderTTF()
{
  DATAFUNCS.RenderFonts('topo_bemvindo',{
   fontSize         : '11.25',
   backgroundColor  : 'FFFFFF',
   color            : '829299',
   fontFile         : 'fonts/Univers CE 75 Black.ttf',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('tit_b_fp',{
   fontSize         : '9.75',
   backgroundColor  : '3D3938',
   color            : 'FFFFFF',
   fontFile         : 'fonts/Univers CE 75 Black.ttf',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('tit_bt',{
   fontSize         : '8.25',
   backgroundColor  : '666666',
   color            : 'FFFFFF',
   fontFile         : 'fonts/Univers CE 75 Black.ttf',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('tit_h2',{
   fontSize         : '9',
   backgroundColor  : 'FFFFFF',
   color            : '3D3938',
   fontFile         : 'fonts/Univers CE 75 Black.ttf',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('tit_h2_amarelo',{
   fontSize         : '10',
   backgroundColor  : 'FFFFFF',
   color            : '829299',
   fontFile         : 'fonts/Univers CE 75 Black.ttf',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('tit_h2_famar',{
   fontSize         : '9',
   backgroundColor  : '829299',
   color            : 'FFFFFF',
   fontFile         : 'fonts/Univers CE 75 Black.ttf',
   transparent      : '1'
  });
  /*color            : '3D3938',*/
  
  DATAFUNCS.RenderFonts('tit_h3',{
   fontSize         : '13.5',
   backgroundColor  : 'FFFFFF',
   color            : '5F5C5C',
   fontFile         : 'fonts/Univers CE 75 Black.ttf',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('tit_h3_norm',{
   fontSize         : '13.5',
   backgroundColor  : 'FFFFFF',
   color            : '5F5C5C',
   fontFile         : 'fonts/univers55.ttf',
   transparent      : '1'
  });
 
 DATAFUNCS.RenderFonts('t_links_ttf',{
   fontSize         : '9',
   backgroundColor  : 'FFFFFF',
   color            : '3D3938',
   fontFile         : 'fonts/Univers CE 75 Black.ttf',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('det_veic1',{
   fontSize         : '10.5',
   backgroundColor  : '716F6E',
   color            : 'FFFFFF',
   fontFile         : 'fonts/Univers CE 75 Black.ttf',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('det_veic2',{
   fontSize         : '10.5',
   backgroundColor  : '716F6E',
   color            : 'FFFFFF',
   fontFile         : 'fonts/univers55.ttf',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('preco_H',{
   fontSize         : '8.75',
   backgroundColor  : 'F3F3F3',
   color            : '716F6E',
   fontFile         : 'fonts/Helvetica LT 95 Black.ttf',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('preco_H2',{
   fontSize         : '15',
   backgroundColor  : 'F3F3F3',
   color            : 'E20A16',
   fontFile         : 'fonts/Helvetica LT 95 Black.ttf',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('tit_camp_home',{
   fontSize         : '7.5',
   backgroundColor  : '666666',
   color            : 'FFFFFF',
   fontFile         : 'fonts/Univers CE 75 Black.ttf',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('tit_h2_dest',{
   fontSize         : '10',
   backgroundColor  : 'F3F3F3',
   color            : '3D3938',
   fontFile         : 'fonts/Univers CE 75 Black.ttf',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('tit_h2_dest2',{
   fontSize         : '10',
   backgroundColor  : 'F3F3F3',
   color            : '3D3938',
   fontFile         : 'fonts/univers55.ttf',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('euro_H',{
   fontSize         : '8.75',
   backgroundColor  : 'F3F3F3',
   color            : '716F6E',
   fontFile         : 'fonts/Eursnb__.ttf',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('euro_H2',{
   fontSize         : '15',
   backgroundColor  : 'F3F3F3',
   color            : 'CA892C',
   fontFile         : 'fonts/Eursnb__.ttf',
   transparent      : '1'
  });
}
function muda_ord(lnk,val,cmp){
  location='index.php?'+lnk+'&'+cmp+'='+val.value;
}

function irPagina(lim,ord,nr,lnk_ger,box){
  var npag = document.getElementById(box).value;
  var nlnk = 'index.php?'+lnk_ger;
  if (npag>lim){
    npag = lim;
  } else if(npag<=0){
    npag = 1;
  }
  if(ord>0){
    nlnk = nlnk+"&ord="+ord;
  }
  
  if(nr>0) {
    nlnk = nlnk+"&nr="+nr;
  }
  location=nlnk+"&pag="+npag;
}

function mostra_divs(show){
  for (var i=1; i<=5; i++){
    document.getElementById('orc_tint_div'+i).style.display = 'none';
  }
  document.getElementById('orc_tint_div'+show).style.display = 'block';
  
  if(show==1){
    document.getElementById('orc_tint_tprev').className = 'selofic required';
  } else {
    document.getElementById('orc_tint_tprev').className = 'selofic';
  }
  
  if(show==2){
    document.getElementById('or_tint_intmec').className = 'areagama textcaixas required';
  } else {
    document.getElementById('or_tint_intmec').className = 'areagama textcaixas';
  }
  
  if(show==3){
    document.getElementById('or_tint_intcol').className = 'areagama textcaixas required';
  } else {
    document.getElementById('or_tint_intcol').className = 'areagama textcaixas';
  }  
  
  if(show==5){
    document.getElementById('or_tint_outro').className = 'areagama textcaixas required';
  } else {
    document.getElementById('or_tint_outro').className = 'areagama textcaixas';
  }
}

function form_pa() {
  var pmrc = document.getElementById('Pmarcaa');
  var pmdl = document.getElementById('Pmodeloo');
  var ptp = document.getElementById('Ptipo');
  var pprt = document.getElementById('Pportas');
  var pcld = document.getElementById('Pcilind');
  var pc = document.getElementById('Pcor');
  var pcmb = document.getElementById('Pcombuss');
  var pcvl = document.getElementById('Pcavalos');
  var panoi = document.getElementById('Panoini');
  var panof = document.getElementById('Panofim');
  var pvini = document.getElementById('Pvalini');
  var pvmax = document.getElementById('Pvalmax');
  var pqlm = document.getElementById('Pquilom');
  var plcl = document.getElementById('Plocal');
  var nextra = document.getElementById('Nextras');
  var ext = "";
  for (var i=0;i<=nextra.value;i++){
    if(document.getElementById('Pextras['+i+']').checked == true){
      var ext = ext+"|"+document.getElementById('Pextras['+i+']').value;
    }
  }
  if(ext!=''){
    ext = ext+"|";
  }
  
  var url ="pa_values.php";
  
  var post_data = "&marca="+pmrc.value;
  post_data = post_data+"&modelo="+pmdl.value;
  post_data = post_data+"&tipo="+ptp.value;
  post_data = post_data+"&portas="+pprt.value;
  post_data = post_data+"&cilindrada="+pcld.value;
  post_data = post_data+"&cor="+pc.value;
  post_data = post_data+"&combustivel="+pcmb.value;
  post_data = post_data+"&cavalos="+pcvl.value;
  post_data = post_data+"&anoini="+panoi.value;
  post_data = post_data+"&anofim="+panof.value;
  post_data = post_data+"&valini="+pvini.value;
  post_data = post_data+"&valmax="+pvmax.value;
  post_data = post_data+"&quilom="+plcl.value;
  post_data = post_data+"&local="+plcl.value;
  post_data = post_data+"&extras="+ext;

  var cmsajaxhttp = getXmlHttpRequest();
 	cmsajaxhttp.open("GET", url+'?pa=1'+post_data, true);
  cmsajaxhttp.onreadystatechange = function(){
		if (cmsajaxhttp.readyState==4){
			var resultado = cmsajaxhttp.responseText;
			//alert(resultado);
      location='index.php?id=37';
		}
	}
 	cmsajaxhttp.send(null);
}
