// JavaScript Document

/* VALIDAÇÃO DE FORMULÁRIOS */
var DOM = YAHOO.util.Dom;
var EVT = YAHOO.util.Event;

function validate_form(_obj,lg){
  var errors = false;
  if (lg=='pt'){
    var txt_erro = "Preenchimento obrigatório.";
    var txt_erro_email = "Email inválido.";
  } else {
    var txt_erro = "Mandatory field.";
    var txt_erro_email = "Invalid Email";
  }
  
  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 == txt_erro)){
            var _varid = e.getAttribute('id');
            e.style.border = '1px solid #FF0000';
  					e.style.color  = '#FF0000';
  					e.value = txt_erro;
            
  					EVT.addListener(e, "focus", function(){ e.value = ''; });
  					
  					if(errors == false) errors = true;
  					
  				} else {
  				  var idinput =  e.getAttribute('id');
  				  if (idinput=='confpass') {
  				    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 #FF0000';
      					e.style.color  = '#FF0000';
      					e.value = txt_erro;
      					
      					document.getElementById('password').style.border = '1px solid #FF0000';
      					document.getElementById('password').style.color  = '#FF0000';
      					document.getElementById('password').value = txt_erro;
                
      					EVT.addListener(e, "focus", function(){ e.value = ''; });
      					
      					if(errors == false) errors = true;
              } else {
                var _varid = e.getAttribute('id');
      				  e.style.border = '1px solid #9E9E9E';
      					e.style.color  = '#000000';
      					
      					document.getElementById('password').style.border = '1px solid #9E9E9E';
      					document.getElementById('password').style.color  = '#000000';
      					
              }
  				  
            } else {
    				  var _varid = e.getAttribute('id');
    				  e.style.border = '1px solid #9E9E9E';
    					e.style.color  = '#000000';
    				}
  				}
  				
  				if(_emai == 1){
  					if(verify_email(e.value) == false){
  						var _varid = e.getAttribute('id');
              e.style.border = '1px solid #FF0000';
    					e.style.color  = '#FF0000';
    					e.value = txt_erro_email;
              
  						EVT.addListener(e, "focus", function(){ e.value = ''; });
  						
  						if(errors == false) errors = true;
  						
  					} else {
  					  var _varid = e.getAttribute('id');
      				e.style.border = '1px solid #9E9E9E';
    					e.style.color  = '#000000';
  					}
  				}
  			} else if(_fich == 1){
  			  if(e.value == ''){
            var _varid = e.getAttribute('id');
            e.style.border = '1px solid #FF0000';
  					e.style.color  = '#FF0000';
  					e.value = txt_erro;
  					EVT.addListener(e, "focus", function(){ e.value = ''; });
  					if(errors == false) errors = true;
          } else {
  					var _varid = e.getAttribute('id');
  				  e.style.border = '1px solid #9E9E9E';
  					e.style.color  = '#000000';
  				}
  			} else if(_sel == 1) {
          if ((e.value=="") || (e.value==0)){
            var _varid = e.getAttribute('id');
            e.style.border = '1px solid #FF0000';
  					e.style.color  = '#FF0000';
						EVT.addListener(e, "focus", function(){ e.value = ''; });
						if(errors == false) errors = true;
          } else {
            var _varid = e.getAttribute('id');
  				  e.style.border = '1px solid #9E9E9E';
  					e.style.color  = '#000000';
          }
  			} 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(document.getElementById('nib1')) {
    if (valida_nib()==false){
      if(errors == false) errors = true;
    }
  }
	
	if(errors == true){
		return false;
	} else {
		return true;
	}
	
}

function IsNumeric(cmp)
//  check for valid numeric strings	
{
  var strString = cmp.value;
  var strValidChars = "0123456789.-";
  var strChar;
  var blnResult = true;
  
  if (strString.length == 0) return false;
  
  //  test strString consists of valid characters listed above
  for (i = 0; i < strString.length && blnResult == true; i++)
  {
  strChar = strString.charAt(i);
  if (strValidChars.indexOf(strChar) == -1)
     {
     blnResult = false;
     }
  }
  
  if(blnResult == false){
    alert('Campo numérico.');
    cmp.value='';
  }
}

function valida_nib() {
  var erro = 0;
  var n1 = document.getElementById('nib1');
  var n2 = document.getElementById('nib2'); 
  var n3 = document.getElementById('nib3');
  var n4 = document.getElementById('nib4');
  if (n1.value.length <= 3) {
    //alert('NIB Inválido.');
    n1.style.border = '1px solid #FF0000';
  	n1.style.color  = '#FF0000';
    document.getElementById('nib1').focus();
    erro=1;
  } else if (n2.value.length <= 3) {
    //alert('NIB Inválido.');
    n2.style.border = '1px solid #FF0000';
  	n2.style.color  = '#FF0000';
    document.getElementById('nib2').focus();
    erro=1;
  } else if (n3.value.length <= 10) {
    //alert('NIB Inválido.');
    n3.style.border = '1px solid #FF0000';
  	n3.style.color  = '#FF0000';
    document.getElementById('nib3').focus();
    erro=1;
  } else if (n4.value.length <= 1) {
    //alert('NIB Inválido.');
    n4.style.border = '1px solid #FF0000';
  	n4.style.color  = '#FF0000';
    document.getElementById('nib4').focus();
    erro=1;
  }
  
  if(erro==0) {
    var _varid = n1.getAttribute('nnome');
    document.getElementById(_varid).value = n1.value+' '+n2.value+' '+n3.value+' '+n4.value;
    return true;
  } else {
    return false;
  }
}

/* VALIDAÇÃO DE FORMULÁRIOS */


function RenderTTF()
{
  DATAFUNCS.RenderFonts('myconfimed',{
   fontSize         : '15',
   backgroundColor  : 'C5E1EF',
   color            : '446DA3',
   fontFile         : 'fonts/ROSESF_B.TTF',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('partners',{
   fontSize         : '15',
   backgroundColor  : 'FFFFFF',
   color            : 'CCCCCC',
   fontFile         : 'fonts/ROSESF_B.TTF',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('titulo',{
   fontSize         : '30',
   backgroundColor  : 'FFFFFF',
   color            : '446DA3',
   fontFile         : 'fonts/ROSESAEB.TTF',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('subtitulo',{
   fontSize         : '16.5',
   backgroundColor  : 'FFFFFF',
   color            : '9AB4D6',
   fontFile         : 'fonts/ROSESA.TTF',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('h1texto',{
   fontSize         : '14.25',
   backgroundColor  : 'FFFFFF',
   color            : '446DA3',
   fontFile         : 'fonts/ROSESAEB.TTF',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('parceiros',{
   fontSize         : '16.5',
   backgroundColor  : 'FFFFFF',
   color            : '9AB4D6',
   fontFile         : 'fonts/ROSESA.TTF',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('bannerleft',{
   fontSize         : '12',
   backgroundColor  : 'FFFFFF',
   color            : '666666',
   fontFile         : 'fonts/ROSESF_B.TTF',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('acidentes_tit',{
   fontSize         : '14',
   backgroundColor  : 'FFFFFF',
   color            : '666666',
   fontFile         : 'fonts/ROSESF_B.TTF',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('ferias',{
   fontSize         : '26.25',
   backgroundColor  : 'FFFFFF',
   color            : '446DA3',
   fontFile         : 'fonts/ROSESF_B.TTF',
   transparent      : '1'
  });
  
  DATAFUNCS.RenderFonts('ferias2',{
   fontSize         : '22.5',
   backgroundColor  : 'FFFFFF',
   color            : '9AB4D6',
   fontFile         : 'fonts/ROSESF_B.TTF',
   transparent      : '1'
  });
}

/* ADICIONAR AOS FAVORITOS */
function addFav(){
    var url        = "http://amerika/confimed";
    var title    = "Confimed";
    if (window.sidebar) window.sidebar.addPanel(title, url,"");
    else if(window.opera && window.print){
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',url);
        mbm.setAttribute('title',title);
        mbm.click();
    }
    else if(document.all){window.external.AddFavorite(url, title);}
}
/* ADICIONAR AOS FAVORITOS */

/* VALIDAÇÃO DE LOGIN ENEWSLETTER */
String.prototype.trim = function()
{
  return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function valida_log(){
  var u = document.getElementById('L1').value.trim();
  var p = document.getElementById('L2').value.trim();
  if ((u!='') && (p!='')) {
    return true;
  } else {
    return false;
  }
}

function valida_newsl(txtu,txtp){
  var u = document.getElementById('N1').value.trim();
  var p = document.getElementById('N2').value.trim();
  
  if ((u!='') && (p!='') && (u!=txtu) && (p!=txtp) && (verify_email(document.getElementById('N2').value)==true)) {
    return 1;
  } else {
    return 0;
  }
}

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;
  }
}
/* VALIDAÇÃO DE LOGIN ENEWSLETTER */



/* *************************************************************************** */

function showimage(id,idbarr)
{
  
  if (document.getElementById(id).style.display == "block") {
    document.getElementById(id).style.display = "none";
    idbarr.className='procedimento';
  } else {
    document.getElementById(id).style.display = "block";
    idbarr.className='procedimentosel';
  }
}

function hideimage(id)
{
  document.getElementById(id).style.display = "none";
}

function showinfo(idtd,id,aux) {
  var i = 0;
  var el = document.getElementById(id);
  var el2 = document.getElementById("titinfo"+aux);
  var xclose = document.getElementsByTagName("tr");
  var tds = document.getElementsByTagName("td");
  var xclose2 = document.getElementsByTagName("div");
  var tit1 = document.getElementById("tit1_"+aux+"");
  var tit2 = document.getElementById("tit2_"+aux+"");
  
  idtd.className='textofaqsel2';
  
  for (var i=0; i<xclose.length; i++) {
    if (xclose[i].className == "fechar"){
      xclose[i].style.display = "none";
    }
  }
  
  for (var i=0; i<xclose2.length; i++) {
    if (xclose2[i].className == "teste1"){
      xclose2[i].style.display = "block";
    } else if (xclose2[i].className == "teste2"){
      xclose2[i].style.display = "none";
    }
  }
    
  for (i=0; i<tds.length; i++) {
    if(tds[i].className == "textofaqsel2"){
      tds[i].className = "textofaq";
      tds[i].onmouseover = function (){this.className='textofaqsel';};
      tds[i].onmouseout = function (){this.className='textofaq';};
    }
    
  }
  
  
  el.style.display = "block";
  
  el2.className='textofaqsel2';
  el2.onmouseout= this.className='textofaqsel2';
  el2.onmouseover= this.className='textofaqsel2';
  tit1.style.display='none';
  tit2.style.display='block';
  
}


/* DESTAQUES HOME */

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("img_dest_home", { duration: 1.0,from:0,to:1,afterFinishInternal:function (){Effect.Appear("img_dest_home", { duration: 14.0,from:1,to:1});}});
    Effect.Appear("img_dest_home", { duration: 3.0,from:0,to:1,afterFinishInternal:function (){Effect.Fade("img_dest_home", { duration: 5.0,from:1,to:1});}});
    muda_img(Dest[numbr]);
  },"30000");
}

function muda_img(id){
  var obj = document.getElementById("img_dest_home");
  var serverPage ="dest_home_load.php";
  var params = "id="+id;
  
  xmlhttp.open("GET",serverPage+"?"+params);
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 1 ) {
    } else if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      obj.innerHTML = xmlhttp.responseText;
    }
  }
  xmlhttp.send(null);
}

/* DESTAQUES HOME */

/* TIPO DE CAMPO MULTI */
/* Cálculo de Totais */

function calcTot(lins,cols,xcount) {
  var tot = new Array();
  var totl = new Array();
  
  for (var i=1;i<=lins;i++){
    for (var j=1;j<=cols;j++){
      if(totl[i]>0) {
        totl[i] = parseFloat(totl[i]) + parseFloat(document.getElementById('T['+xcount+']['+i+']['+j+']').value);
      } else {
        totl[i] = parseFloat(document.getElementById('T['+xcount+']['+i+']['+j+']').value);
      }
      if(tot[j]>0) {
        tot[j] = parseFloat(tot[j]) + parseFloat(document.getElementById('T['+xcount+']['+i+']['+j+']').value);
      } else {
        tot[j] = parseFloat(document.getElementById('T['+xcount+']['+i+']['+j+']').value);
      }
    }
  }
  
  var tot_geral = 0.00;
  for (var i=1;i<=lins;i++){
    if(document.getElementById('totl_'+i)){
      document.getElementById('totl_'+i).value = parseFloat(totl[i]).toFixed(2);
      tot_geral = parseFloat(tot_geral)+parseFloat(totl[i]);
    }
  }
  for (var j=1;j<=cols;j++){
    //alert(tot[j]);
    if(document.getElementById('tot_'+j)){
      document.getElementById('tot_'+j).value = parseFloat(tot[j]).toFixed(2);
    }
  }
  //alert(tot_geral)
  if(document.getElementById('tot_total')){
  document.getElementById('tot_total').value = parseFloat(tot_geral).toFixed(2);
  }
  
}

function dm(inp_val) 
{
  var amount = inp_val.value;
  string = "" + amount;
  dec = string.length - string.indexOf('.');
  
  if(string.length>11) {
  } else {
    if (string.indexOf('.') == -1) {
      inp_val.value = string + '.00';
    } else if (dec == 1) {
      inp_val.value = string + '00';
    } else if (dec == 2) {
      inp_val.value = string + '0';
    } else if (dec > 3) {
      inp_val.value = string.substring(0,string.length-dec+3);
    } else {
      inp_val.value = string;
    }
  }
}



