// JavaScript Document

function openSub(idBtn){
	if(document.getElementById(idBtn).className=='closedSub'){
		document.getElementById(idBtn).className='open';
	}else{
		document.getElementById(idBtn).className='closedSub';
	}
	return false;
}
function swapArea(idBtn){
	document.getElementById('bodyId').className=idBtn.className;
	return false;
}

function blockStateSwapper(idBlock){
	if(document.getElementById(idBlock).className=='block openBlock'){
		document.getElementById(idBlock).className='block closedBlock';
	}else{
		document.getElementById(idBlock).className='block openBlock';
	}
}

function faqSwap(idFaq){
	if(idFaq.parentNode.className=='closedFaq'){
		idFaq.parentNode.className='';
	}else{
		idFaq.parentNode.className='closedFaq';
	}
}

function agenda_tipo_set(obj, id){
	uls=obj.parentNode.parentNode;
	for(i=0;i<uls.childNodes.length;i++){
		uls.childNodes[i].className="";
	}

	obj.parentNode.className="selected";
	agenda_tipo=id;
	agenda_days_selects=Array();


	agenda_select_days_clear_calendar();
	agenda_select_days_set();

	$("#tipo_name").html(obj.innerHTML);
	agenda_load();
}

function agenda_dia_set(day,ints){
	//alert(day);
	agenda_dia=day;
	agenda_select_days_set();
	agenda_load();

}
function agenda_dia_set_home(day,ints){
	//alert(day);
	agenda_dia=day;
	agenda_select_days_set();
	agenda_load_home();
}
function agenda_select_days_set(ano, mes, ints){

	if (ano!=undefined && mes!=undefined){
	      agenda_display_date=mes+"_"+ano;
	}
	agenda_select_days_clear_calendar();
	$.getJSON(url_site + "agenda_days.php?dia=" + agenda_display_date + "&tipo=" + agenda_tipo ,
		function(data){
			//alert("load");
			$.each(data, function(i,item){
				$("#datapicker_"+item).addClass("temEvento");
				//alert(i+"item: "+item);
				agenda_days_selects[item]=true;
			});
		});
}

function agenda_select_days_get(day){
	//alert("sdf"+day);
	data=day.getDate()+'_'+(day.getMonth()+1)+'_'+(1900+day.getYear());
	if (typeof(agenda_days_selects[data])=='boolean') {
		return true;
	}
	return false;
}
function agenda_select_days_clear_calendar(){
	for (i=1;i<=31;i++){
		$("#datapicker_" + i + "_" + agenda_display_date).removeClass("temEvento");
	}
}
function agenda_load(){
	$("#eventLists").load(url_site+"agenda.php?tipo="+agenda_tipo+"&dia="+agenda_dia+"");
}
function agenda_load_home(){
	$("#eventLists").load(url_site+"agenda_home.php?tipo="+agenda_tipo+"&dia="+agenda_dia+"");
}

function agenda_search(){
	nome=document.getElementById("nome_search").value;
	tipo=document.getElementById("tipo_search").value;
	dia=document.getElementById("dia_search").value;
	$("#eventLists").load(url_site+"agenda_search.php?tipo="+tipo+"&dia="+dia+"&nome="+nome);
}


function validacao_form(obri,campo,formato){
	ok=validacao_obrigatorio(obri);
	if (ok==false) {
		return false;
	}
	ok=validacao_formato(campo,formato);
	return ok;
}

function validacao_obrigatorio(campo){
	for (i=0 ;i<campo.length;i++) {
		if (typeof(document.getElementById(campo[i]))=="object") {
			if (document.getElementById(campo[i]).value=='') {
				alert("Tem que preencher os campos obrigatórios");
				return false;
			}
		}else{
			var campos=document.getElementById(campo[i]);
			var ok=false;
			if (campos.length>1){
				for (y=0;y<campos.length ;y++ ) {
					if (campos[y].checked==true) {
						ok=true;
					}
				}
			}else{
				if (campos.checked==true) {
					ok=true;
				}
			}
			if (ok==false){
				alert("Tem que preencher os campos obrigatórios");
				return false;
			}
		}
	}
	return true;
}

function validacao_formato(campo,formato){
	for (i=0 ;i<campo.length;i++) {
		valor=document.getElementById(campo[i]).value;
		if (valor!=''){
			switch (formato[i]) {
				case '2':
					var validformat=/^[0-9\.,]+$/;
					if (!validformat.test(valor)) {
						alert("O campo não esta correctamente preenchido.\nO campo tem que ser em formato numérico");
						return false;
					}
					break
				case '3':
					var validformat=/^[a-zA-Z0-9\-\._]+@[a-zA-Z0-9\-_]+(\.?[a-zA-Z0-9\-_]*)\.[a-zA-Z]{2,3}$/;
					if (!validformat.test(valor)) {
						alert("O campo não esta correctamente preenchido.\nO campo não tem um email válido");
						return false;
					}
					break
				case '4':
					var validformat=/^\d{2}-\d{2}-\d{4}$/;
					if (!validformat.test(valor)) {
						alert("O campo não esta correctamente preenchido.\nO campo não tem uma data válida (dd-mm-aaaa)");
						return false;
					}
					break
			}
		}
	}
	return true;
}

