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');
			if(_tag == 'TEXTAREA' || _type == 'text'){
				if(e.value == '' || e.value == 'Campo de preenchimento obrigatório.'){
					e.style.border = '2px solid #D3853C';
					e.style.color  = '#D3853C';
					e.value = 'Campo de preenchimento obrigatório.';
					EVT.addListener(e, "focus", function(){ e.value = ''; });
					if(errors == false) errors = true;
				} else {
					e.style.border = '1px solid #EBEBEB';
					e.style.color  = '#544B3D';
				}
				if(_emai == 1){
					if(verify_email(e.value) == false){
						e.style.border = '2px solid #D3853C';
						e.style.color  = '#D3853C';
						e.value = 'Tem de introduzir um e-mail correcto.';
						EVT.addListener(e, "focus", function(){ e.value = ''; });
						if(errors == false) errors = true;
					} else {
						e.style.border = '1px solid #EBEBEB';
						e.style.color  = '#544B3D';
					}
				}
			} else if(_type == 'checkbox') {
				if(e.checked == false){
					e.checked = true;
					alert('Existem opções no formulário que foram activas.');
					if(errors == false) errors = true;
				}
			}
		}
	);
	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 cmd_hover(id) {
	var cmd = document.getElementById(id);
	cmd.src = cmd.src.replace('.jpg','_hover.jpg');
}

function cmd_out(id) {
	var cmd = document.getElementById(id);
	cmd.src = cmd.src.replace('_hover.jpg','.jpg');
}

function smn_hover(id) {
	var menu = document.getElementById(id);
	menu.className = menu.className + '_hover';
}

function smn_out(id) {
	var menu = document.getElementById(id);
	menu.className = menu.className.replace('_hover','');
}

function agenda_abrir(id) {
	var agenda = document.getElementById(id+'_mostra');
	var agenda_a = document.getElementById(id+'_esconde');
	agenda.className = 'agenda_cultural_esconde';
	agenda_a.className = 'agenda_cultural_mostra';
}

function agenda_fechar(id) {
	var agenda = document.getElementById(id+'_mostra');
	var agenda_a = document.getElementById(id+'_esconde');
	agenda_a.className = 'agenda_cultural_esconde';
	agenda.className = 'agenda_cultural_mostra';
}

function popup_abrir() {
	var div_traseira = document.getElementById('div_traseira');
	var div_popup = document.getElementById('div_popup');
	//var div_enviado = document.getElementById('div_enviado');
	div_traseira.style.display = 'block';
	div_popup.style.display = "block";
	//div_enviado.style.display = "none";
}

function popup_fechar() {
	var div_traseira = document.getElementById('div_traseira');
	var div_popup = document.getElementById('div_popup');
	//var div_enviado = document.getElementById('div_enviado');
	div_traseira.style.display = 'none';
	div_popup.style.display = "none";
	//div_enviado.style.display = "none";
}

function popup_enviado() {
	var div_traseira = document.getElementById('div_traseira');
	var div_popup = document.getElementById('div_popup');
	var div_enviado = document.getElementById('div_enviado');
	div_traseira.style.display = 'block';
	div_popup.style.display = "none";
	div_enviado.style.display = "block";
}

function contactos(id){
	var div_mapa = document.getElementById('div_mapa');
	var div_formulario = document.getElementById('div_formulario');
	var div_formulario_false = document.getElementById('div_formulario_false');
	var div_formulario_ok = document.getElementById('div_formulario_ok');
	var abrir = document.getElementById(id);
	div_formulario.className = 'contactos_esconde';
	div_formulario_false.className = 'contactos_esconde';
	div_formulario_ok.className = 'contactos_esconde';
	div_mapa.className = 'contactos_esconde';
	abrir.className = 'contactos_mostra';
}

function contactos2(idf,idm,idfa){
	var div_mapa = document.getElementById(idm);
	var div_formulario = document.getElementById(idf);
	
	//var div_formulario_false = document.getElementById('div_formulario_false');
	var div_formulario_ok = document.getElementById(idfa);
	
	var abrir = document.getElementById(idf);
	div_formulario.className = 'contactos_esconde';
	//div_formulario_false.className = 'contactos_esconde';
	div_formulario_ok.className = 'contactos_esconde';
	div_mapa.className = 'contactos_esconde';
	abrir.className = 'contactos_mostra';
}

function contactos2_close(idf,idm,idfa){
	var div_mapa = document.getElementById(idm);
	var div_formulario = document.getElementById(idf);
	
	//var div_formulario_false = document.getElementById('div_formulario_false');
	var div_formulario_ok = document.getElementById(idfa);
	
	var abrir = document.getElementById(idm);
	div_mapa.className = 'contactos_esconde';
	//div_formulario_false.className = 'contactos_esconde';
	div_formulario_ok.className = 'contactos_esconde';
	div_formulario.className = 'contactos_esconde';
	abrir.className = 'contactos_mostra';
}

function mudaautor(aut){
  var a = aut.value;
  location='index.php?id='+a+'&tbl=cal_autores';
}

function mudaTipoNot(t,m,a){
  var c = document.chk_cat.checkbox;
  var lnk = "";
  var aux=0;
  for (var i=0; i<c.length; i++){
    if (c[i].checked==true){
      lnk = lnk+"&tipo"+(i+1)+"="+c[i].value;
      aux++;
    }
  }
  
  if (lnk!=''){
    location="index.php?id=86&ma="+m+"&aa="+a+""+lnk;
  }
  
}

function mudaData()
{
  var nurl="";
  var mes = document.getElementById('selectmes').value;
  var ano = document.getElementById('selectano').value;
  
  var url = window.location.href;
  var urlp1 = url.split("?");
  var urlp2 = urlp1[1].split("&");
  for (var i=0;i<urlp2.length;i++){
    var urlp3 = urlp2[i].split("=");
    
    if (urlp3[0]=='aa'){
    } else if (urlp3[0]=='ma'){
    } else if(urlp3[0]!= 'undefined') {
      nurl = nurl+"&"+urlp2[i];
    }
  }
  location = "index.php?"+nurl+"&ma="+mes+"&aa="+ano;
}

function sel_aut(a)
{
  var aut = a.value;
  location="index.php?id="+aut+"&tbl=cal_autores";
}

var _xpannel;
function ModalPopUp(phpfile, _title, _mensagem, _width, xclose)
{
  _xpannel = new YAHOO.widget.Panel("xpannel", 
      {  
      width:_width, 
      fixedcenter:true, 
      close:false, 
      draggable:true, 
      zindex:99999, 
      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);
  
}

function ModalIframePopUp(phpfile, _title, _mensagem, _height, _width, xclose)
{
  _xpannel = new YAHOO.widget.Panel("xpannel", 
      {  
      width:_width, 
      fixedcenter:true, 
      close:false, 
      draggable:true, 
      zindex:99999, 
      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 validate_formP(){
  var txt = document.getElementById('T1').value;
  if (txt==''){
    alert('Tem de preencher o campo de pesquisa!');
    return false;
  } else {
    return true;
  }
}

function verComent()
{
  var nome = document.getElementById('Tnome');
  var com = document.getElementById('Tcoment');
  var erro = 0;
  
  if(nome.value == '' || nome.value == 'Campo de preenchimento obrigatório.'){
		nome.style.border = '2px solid #D3853C';
		nome.style.color  = '#D3853C';
		nome.value = 'Campo de preenchimento obrigatório.';
		EVT.addListener(nome, "focus", function(){ nome.value = ''; });
		erro = 1;
	} else {
		nome.style.border = '1px solid #EBEBEB';
		nome.style.color  = '#544B3D';
	}
  
  
  if(com.value == '' || com.value == 'Campo de preenchimento obrigatório.'){
		com.style.border = '2px solid #D3853C';
		com.style.color  = '#D3853C';
		com.value = 'Campo de preenchimento obrigatório.';
		EVT.addListener(com, "focus", function(){ com.value = ''; });
		erro = 1;
	} else {
		com.style.border = '1px solid #EBEBEB';
		com.style.color  = '#544B3D';
	}
  
  if (erro==1){
    return false;
  } else {
    return true;
  }
}  

function verEnviaAmi()
{
  var nome = document.getElementById('Tnome');
  var email = document.getElementById('Temail');
  var nome2 = document.getElementById('Tnome2');
  var email2 = document.getElementById('Temail2');
  var erro = 0;
  
  if(nome.value == '' || nome.value == 'Campo de preenchimento obrigatório.'){
		nome.style.border = '2px solid #D3853C';
		nome.style.color  = '#D3853C';
		nome.value = 'Campo de preenchimento obrigatório.';
		EVT.addListener(nome, "focus", function(){ nome.value = ''; });
		erro = 1;
	} else {
		nome.style.border = '1px solid #EBEBEB';
		nome.style.color  = '#544B3D';
	}
  
  if(email.value == '' || email.value == 'Campo de preenchimento obrigatório.'){
		email.style.border = '2px solid #D3853C';
		email.style.color  = '#D3853C';
		email.value = 'Campo de preenchimento obrigatório.';
		EVT.addListener(email, "focus", function(){ email.value = ''; });
		erro = 1;
	} else {
	  if(verify_email(email.value) == false){
			email.style.border = '2px solid #D3853C';
			email.style.color  = '#D3853C';
			email.value = 'Tem de introduzir um e-mail correcto.';
			EVT.addListener(email, "focus", function(){ email.value = ''; });
			if(errors == false) errors = true;
		} else {
			email.style.border = '1px solid #EBEBEB';
			email.style.color  = '#544B3D';
		}
	}
  
  if(nome2.value == '' || nome2.value == 'Campo de preenchimento obrigatório.'){
		nome2.style.border = '2px solid #D3853C';
		nome2.style.color  = '#D3853C';
		nome2.value = 'Campo de preenchimento obrigatório.';
		EVT.addListener(nome2, "focus", function(){ nome2.value = ''; });
		erro = 1;
	} else {
		nome2.style.border = '1px solid #EBEBEB';
		nome2.style.color  = '#544B3D';
	}
  
  if(email2.value == '' || email2.value == 'Campo de preenchimento obrigatório.'){
		email2.style.border = '2px solid #D3853C';
		email2.style.color  = '#D3853C';
		email2.value = 'Campo de preenchimento obrigatório.';
		EVT.addListener(email2, "focus", function(){ email2.value = ''; });
		erro = 1;
	} else {
	  if(verify_email(email2.value) == false){
			email2.style.border = '2px solid #D3853C';
			email2.style.color  = '#D3853C';
			email2.value = 'Tem de introduzir um e-mail correcto.';
			EVT.addListener(email2, "focus", function(){ email2.value = ''; });
			if(errors == false) errors = true;
		} else {
			email2.style.border = '1px solid #EBEBEB';
			email2.style.color  = '#544B3D';
		}
	}
  
  if (erro==1){
    return false;
  } else {
    return true;
  }
}

function verEncomenda()
{
  var nome = document.getElementById('Tnome');
  var morada = document.getElementById('Tmorada');
  //var contrib = document.getElementById('Tcontrib');
  var email = document.getElementById('Temail');
  var telef = document.getElementById('Ttelef');
  //var obs = document.getElementById('Tobs');
  
  var erro = 0;
  
  if(nome.value == '' || nome.value == 'Campo de preenchimento obrigatório.'){
		nome.style.border = '2px solid #D3853C';
		nome.style.color  = '#D3853C';
		nome.value = 'Campo de preenchimento obrigatório.';
		EVT.addListener(nome, "focus", function(){ nome.value = ''; });
		erro = 1;
	} else {
		nome.style.border = '1px solid #EBEBEB';
		nome.style.color  = '#544B3D';
	}
  
  if(morada.value == '' || morada.value == 'Campo de preenchimento obrigatório.'){
		morada.style.border = '2px solid #D3853C';
		morada.style.color  = '#D3853C';
		morada.value = 'Campo de preenchimento obrigatório.';
		EVT.addListener(morada, "focus", function(){ morada.value = ''; });
		erro = 1;
	} else {
		morada.style.border = '1px solid #EBEBEB';
		morada.style.color  = '#544B3D';
	}
  
  if(email.value == '' || email.value == 'Campo de preenchimento obrigatório.'){
		email.style.border = '2px solid #D3853C';
		email.style.color  = '#D3853C';
		email.value = 'Campo de preenchimento obrigatório.';
		EVT.addListener(email, "focus", function(){ email.value = ''; });
		erro = 1;
	} else {
	  if(verify_email(email.value) == false){
			email.style.border = '2px solid #D3853C';
			email.style.color  = '#D3853C';
			email.value = 'Tem de introduzir um e-mail correcto.';
			EVT.addListener(email, "focus", function(){ email.value = ''; });
			if(errors == false) errors = true;
		} else {
			email.style.border = '1px solid #EBEBEB';
			email.style.color  = '#544B3D';
		}
	}
  
  if(telef.value == '' || telef.value == 'Campo de preenchimento obrigatório.'){
		telef.style.border = '2px solid #D3853C';
		telef.style.color  = '#D3853C';
		telef.value = 'Campo de preenchimento obrigatório.';
		EVT.addListener(telef, "focus", function(){ telef.value = ''; });
		erro = 1;
	} else {
		telef.style.border = '1px solid #EBEBEB';
		telef.style.color  = '#544B3D';
	}
  
  if (erro==1){
    return false;
  } else {
    return true;
  }
}

