
function changeMonth(obj, f, LG) {
	var sel = obj.selectedIndex;
	if(sel) {
		var val = obj.options[sel].value;
		clearSelect("periodo"); 
		if(val == 0) return;
		dn("/?it=advertising_date_sel&LG="+LG+"&cop="+val, dnSelect2, f+".periodo");
	} else {
		clearSelect("periodo"); 
	}
}

function dnSelect2(s,idcampo) {
	if(s){
		s = s.substr(0,s.length-1);
		var linhas = s.split(/;/);
		var tam = linhas.length;
		var sel = eval("document."+idcampo); 
		for(var i = 0; i < tam; i++) {
			var campos = linhas[i].split(/\|/);
			sel.options[i+1] = new Option(campos[1], campos[0]);
		}
		
	}
}

function tstNum2(evt)
{
 var charCode = (evt.which) ? evt.which : evt.keyCode
 if (charCode > 31 && ((charCode < 48 && charCode!=45)|| charCode > 57))
	return false;
 return true;
}


function dnALA(s) {
	document.getElementById('ALMWKAREA').innerHTML = s;
	try{generate_wysiwyg('editor');} catch(e) {}
}

function tttAA(ce) { 
	var f = document.getElementById('FME'+ce);
	dn(_ub+"it=adalt&op=1&co="+f.co.value+"&SID="+SID+"&ce="+f.ce.value+"&dialt="+f.dialt.value+"&meslt="+f.meslt.value+"&anolt="+f.anolt.value+"&horalt="+f.horalt.value+"&msgalt="+msgalt.value, dnALA); 
}

function ld(tp,co,SID,ce) {
	switch(tp) {
		case 1:  // Form novo evento
			if(vv_browser == 2) dn(_ub+"it=novoevt&LG="+LG+"&co="+co+"&SID="+SID, dnALA);
			else window.location = _ub+"it=eventos&LG="+LG+"&op=17&co="+co+"&SID="+SID; 
			break;
		case 2: // Form altera almanaque
			if(vv_browser == 2) dn(_ub+"it=formalmanaque&LG="+LG+"&co="+co+"&SID="+SID, dnALA);
			else window.location = _ub+"op=11&co="+co+"&LG="+LG+"&SID="+SID; 
			break; 
		case 3: // Form altera evento
			if(vv_browser == 2) dn(_ub+"it=formevento&LG="+LG+"&co="+co+"&ce="+ce+"&SID="+SID, dnALA);
			else window.location = _ub+"it=alteraevt&LG="+LG+"&op=21&co="+co+"&ce="+ce+"&SID="+SID;
			break; 
		case 4: 
			if(vv_browser == 2) dn(_ub+"it=rmvevt&LG="+LG+"&op=22&co="+co+"&ce="+ce+"&SID="+SID, dnALA);
			else window.location = _ub+"it=eventos&LG="+LG+"&op=22&co="+co+"&ce="+ce+"&SID="+SID; 
			break; 
		case 5: 
			if(vv_browser == 2) dn(_ub+"it=novo_facto&LG="+LG+"&op=21&co="+co+"&SID="+SID, dnFAC);
			else window.location = _ub+"it=factos&op=21&LG="+LG+"&co="+co+"&SID="+SID; 
			break; 
		case 6: 
			if(vv_browser == 2) {
				dn(_ub+"it=listagem_factos&LG="+LG+"&co="+co+"&SID="+SID, dnFAC);
				window.scroll(0,0);
			} else window.location= _ub+"it=factos&LG="+LG+"&co="+co+"&SID="+SID; 
			break; 
		case 7: // Remove facto 
			if(vv_browser == 2)dn(_ub+"it=rmvfct&LG="+LG+"&co="+co+"&cf="+ce+"&SID="+SID, dnFAC);
			else window.location = _ub+"it=factos&LG="+LG+"&co="+co+"&cf="+ce+"&op=15&SID="+SID; 
			break; 
		case 8: //Edita facto
			if(vv_browser == 2) dn(_ub+"it=edita_facto&LG="+LG+"&co="+co+"&cf="+ce+"&SID="+SID, dnFAC);
			else window.location = _ub+"it=factos&LG="+LG+"&co="+co+"&cf="+ce+"&op=18"+"&SID="+SID; 
			break; 
		case 9: 
			if(vv_browser == 2) dn(_ub+"it=convidautilizadores&LG="+LG+"&co="+co+"&SID="+SID, dnUSR);
			else window.location = _ub+"it=utilizadores&LG="+LG+"&op=26&co="+co+"&SID="+SID; 
			break; 
		case 10: 
			dn(_ub+"it=addal&op=35&LG="+LG+"&co="+co+"&SID="+SID, dnFACAD);
			break;
		case 11:  // Form novo evento
			if(vv_browser == 2) dn(_ub+"it=novoevt2&LG="+LG+"&co="+co+"&SID="+SID, dnALA);
			else window.location = _ub+"it=eventos1&LG="+LG+"&op=17&co="+co+"&SID="+SID; 
			break;
		case 12:  // Form novo evento
			if(vv_browser == 2) dn(_ub+"it=novoevt3&LG="+LG+"&co="+co+"&SID="+SID, dnALA);
			else window.location = _ub+"it=efemerides1&LG="+LG+"&op=17&co="+co+"&SID="+SID; 
			break;
	}
}

function changeField(obj) {
	switch(obj.selectedIndex) {
		case 1:
			document.getElementById("field1").style.display = '';
			document.getElementById("field2").style.display = 'none';
		break;
		case 2:
			document.getElementById("field1").style.display = 'none';
			document.getElementById("field2").style.display = '';
		break;
		default:
			document.getElementById("field1").style.display = 'none';
			document.getElementById("field2").style.display = 'none';
		break;
	}
}

function glTTA(f,id) {
   f.action=id;
}

// PARA O BACKOFFICE
var guarda = 0;
function mostraback(id){
	var el = document.getElementById(id);
	var i = document.getElementById("i"+id);
	if(el.style.display == 'none'){
		el.style.display = '';
		i.src="/i/open.png";
	} else{ 
		el.style.display = 'none';
		i.src="/i/close.png";
	}

	if(guarda && guarda != id) {
		var el = document.getElementById(guarda);
		var i = document.getElementById("i"+guarda);
		el.style.display = 'none';
		i.src="/i/close.png";
	}
	guarda = id;
}



function checkedAll (id, checked) {
		var el = document.getElementById(id);
        for (var i = 0; i < (el.elements.length); i++) {
          if (el.elements[i].checked == true)
		  		  el.elements[i].checked = false;
		  else el.elements[i].checked = true;
        }
}


function mudaCaixa(id){
	var c1 = document.getElementById('caixa1');
	var c2 = document.getElementById('caixa2');
	var c3 = document.getElementById('caixa3');

	if(id=='caixa1'){
		c1.style.display = '';
		c2.style.display = 'none';
		c3.style.display = 'none';
	}
	if(id=='caixa2'){
		c1.style.display = 'none';
		c2.style.display = '';
		c3.style.display = 'none';
	}
	if(id=='caixa3'){
		c1.style.display = 'none';
		c2.style.display = 'none';
		c3.style.display = '';
	}
	
}

function mudaCaixaB(id){
	var c1 = document.getElementById('caixaB1');
	var c2 = document.getElementById('caixaB2');
	var c3 = document.getElementById('caixaB3');
	if(id=='caixaB1'){
		c1.style.display = '';
		c2.style.display = 'none';
		c3.style.display = 'none';
	}
	if(id=='caixaB2'){
		c1.style.display = 'none';
		c2.style.display = '';
		c3.style.display = 'none';
	}
	if(id=='caixaB3'){
		c1.style.display = 'none';
		c2.style.display = 'none';
		c3.style.display = '';
	}
}

function loadTo(u) {
	dn(u, dn100); 
}
function dn100(s) {
document.getElementById('PRESSDES').innerHTML = s;
}

function AddFavorito(title,url) 
{
	if (window.external){
		window.external.AddFavorite(url,title);
	}else  if(window.sidebar){
    	window.sidebar.addPanel(title, url,"");
	} else {return true;}
}

function owN(url, w, h) {
	var wd = window.open(url, "Newsletter", "width="+w+",height="+(h+30)+",location=no,menubar=no,personalbar=no,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=no");
	if(wd) wd.focus();
}

function PrintPopup(url) {
 var wd = window.open(url, "IMP", "width=647,height=600,location=no,menubar=no,personalbar=no,resizable=no,scrollbars=yes,status=no,titlebar=yes,toolbar=no");
 wd.focus();
}

function mudamarca(s,f) {
	clearSelect("modelo"); 
	if(s.value == 0) return;
	dn("/?it=modelo_lst2&LG="+LG+"&marca="+s.value, dnSelect, f+".modelo");
}

function mudacategoria(s,f) {
	clearSelect("subcategoria"); 
	if(s.value == 0) return;
	dn("/?it=subcategoria_lst2&LG="+LG+"&categoria="+s.value, dnSelect, f+".subcategoria");
}

function manda(id){
	document.fpesq.od.value = document.getElementById(id).selectedIndex;
	document.fpesq.submit();
}

function mudaRange(s,f) {
	clearSelect("codcat1"); 
	clearSelect("CATEGORYCODE");
	if(s.value == 0) return; 
	dn("/?it=produto_frm_lst_c2O&LG="+LG+"&codrange="+s.value, dnSelect, f+".codcat1");  
}
function mudaCat1(s,f) {
	clearSelect("CATEGORYCODE");
	if(s.value == 0) return; 
	dn("/?it=produto_frm_lst_c3O&LG="+LG+"&codcat1="+s.value, dnSelect, f+".CATEGORYCODE");  
}
function mostra(co){
	var op = document.getElementById(co);
	if(op.style.display == "none")
		op.style.display = "";
	else
		op.style.display = "none";
}

campos=new Array();
function fc(campo,ivalue){
	var v1=campos[campo.form.name];
	if(v1 && v1.length)for(i=0;i<v1.length;i++)if((v1[i][0]==campo.name)&&(campo.value==v1[i][1]))campo.value="";
}

function lfc(campo){
 	var v1=campos[campo.form.name];
 	if(v1 && v1.length)for(i=0;i<v1.length;i++)if((v1[i][0]==campo.name)&&(campo.value==""))campo.value=v1[i][1];
}

function initField(form,campo,inicial,obrigatorio,texto) {
	if(!campos[form])campos[form]=new Array();
	campos[form][campos[form].length]=new Array(campo,inicial,obrigatorio,texto);
	if(document.forms[form].elements[campo].value=="")document.forms[form].elements[campo].value=inicial;
}

function tstForm(f){
	var form = f.name; 
 	var v1=campos[form];
 	if(v1 && v1.length)for(i=0;i<v1.length;i++){
  		c=v1[i];
  		if(c[2] && (document.forms[form].elements[c[0]].value=="") || (document.forms[form].elements[c[0]].value==c[1]))return e(c[3],document.forms[form].elements[c[0]]);
 	}
 	return 1;
}

function submitenter(myfield,e) {
 	var keycode;
 	if(window.event)keyCode=window.event.keyCode;
 	else if(e)keyCode=e.which;
 	else return true;
 	if(keyCode == 13){
  		if(tstForm(myfield.form))myfield.form.submit();
  		return false;
 	} else return true;
}

function validadata(data){
	if(data.value.length != 10) return false;
	if(!(res = data.value.split(/-/)) || res.length!=3) return false;
	
	var dia = data.value.substring(0,2);
	var mes = data.value.substring(3,5);
	var ano = data.value.substring(6,10);

	var hoje = new Date();
	var anoh = hoje.getYear();
	
	if (ano > anoh || ano < 1900) return false;
	
	if(mes == '01' || mes == '03' || mes == '05' || mes == '07' || mes == '08' || mes == '10' || mes == '12'){
		if (dia > 31) return false;
	}
	else if( mes == '04' || mes == '06' || mes == '09' || mes == '11'){
		if(dia > 30) return false;
	}
	else if((ano % 4) != 0 && mes == '02' && dia > 28)
		return false;
	else if((ano % 4) == 0 && mes == '02' && dia > 29)
		return false;
	return true;
}

function lmp() {document.fnews.email.value = ''}
function goEm() {if(tstNews(document.fnews))document.fnews.submit()}
function tstEmail(email) {
	if(!email.value.match(/.*?@.*?\.[a-zA-Z][a-zA-Z]/) || email.value.match(/www/)) return false;
	return true;
}
function voltaTopo(id){
	var objDiv = document.getElementById(id);
	objDiv.scrollTop = "0px";
}
function e(mess, obj) {
	alert(mess);
	if(obj) obj.focus();
	return false;
}
function tstNum() {
	var tc = event.keyCode;
	if(event.ctrlKey || event.altKey) return;
	if(tc == 38 || tc == 35 || tc == 37 || tc == 36 || tc == 39 || tc==40 || tc==41 || tc==43 || tc==45 || tc==46 || tc==127 || tc <= 31 || (tc >= 48 && tc <= 57) || (tc>=96 && tc<=105)) return;
	event.returnValue=false;
}
function tstMoney(campo) {
	var tc = event.keyCode;
	if(event.ctrlKey || event.altKey) return;
	if(tc == 110) tc = 188;
	if(tc == 188 && campo.value.lastIndexOf(',')!=-1) {
		event.returnValue=false;
		return;
	}
	if(tc == 109 || tc == 189 || tc==188 || tc==45 || tc==127 || tc <= 31 || tc==144 || (tc >= 35 && tc <= 40) || (tc >= 46 && tc <= 57) || (tc>=96 && tc<=105)) return;
	event.returnValue=false;
}
function ow(url, w, h) {
	var wd = window.open(url, "IMG", "width="+w+",height="+(h+30)+",location=no,menubar=no,personalbar=no,resizable=no,scrollbars=no,status=no,titlebar=yes,toolbar=no");
	if(wd) wd.focus();
}

function tstDataFinal(data) {
	var res = null;
	if(!(res = data.value.split(/-/)) || res.length!=3) return e("Data Inv�lida", data);
	var dia = parseInt(res[0], 10);
	var mes = parseInt(res[1], 10);
	var ano = parseInt(res[2], 10);
	if(dia > 31) return 1; //Indique correctamente o dia da data
	if(mes > 12) return 2; //Indique correctamente o mês da data
	if(ano < 1900) return 3; //Indique correctamente o ano da data
	return true;
}

function tstsnd(f,rd) {
	if (rd.checked) return true;
	else for (x = 0; x < rd.length; x++) if (rd[x].checked) return true;
	return e("Por favor, selecione uma resposta");
}
function tstCB(rd) {
	if(rd.checked) return true;
	else for (x = 0; x < rd.length; x++) if (rd[x].checked) return true;
	return false;
}

function dn(url, func, p) {
	var xd = null;
	try {
		xd = (vv_browser == 2?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest());
		if(xd) {
			xd.open("GET",url,true);
			xd.onreadystatechange=function() {
				if(xd.readyState == 4 && xd.status == 200){
					if(p && func)func(xd.responseText,p);
					else{
						if(func)func(xd.responseText);
						else dn1(xd.responseText);
					}
				}
			}
    		xd.send(null);
  	 	}
    } catch(e) {}
}
function dnSelect(s,idcampo) {
	if(s){
		var linhas = s.split(/\n/);
		var tam = linhas.length;
		var sel = eval("document."+idcampo); 
		for(var i = 0; i < tam; i++) {
			var campos = linhas[i].split(/\|/);
			sel.options[i+1] = new Option(campos[1], campos[0]);
		}
	}
}
function clearSelect(idcampo){
	var obj = document.getElementById(idcampo);
	if(!obj || !obj.options) return; 
	var tot = obj.options.length; 
	for(var i = 0; i<tot; i++) obj.options[1] = null;
}
var vv_browser = 0;
var vv_version = 0;
var vv_cookies = 0;
getVersion();

function getVersion() {
	var agt=navigator.userAgent.toLowerCase();
	var is_major = parseInt(navigator.appVersion);
	var is_minor = parseFloat(navigator.appVersion);

	var is_nav = is_ie = 0;
	var res = null;
	if((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
		&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
		&& (agt.indexOf('webtv')==-1)) {
		vv_browser = 1;
		if(is_major == 2) vv_version = 2;
		else if(is_major == 3) vv_servion = 3;
		else if(is_major == 4) vv_servion = 4;
		else if(is_major == 5) vv_version = 5;
		else if(is_major >= 5) vv_version = 6;
  	} else if(res = agt.match(/msie (\d+)\.(\d+)/)) {
		vv_browser = 2;
		if(is_major < 4) vv_version = 6;
		else if(res[1] == 5 && res[2] == 0) vv_version = 7;
		else if(res[1] == 5 && res[2]) vv_version = 8;
		else if(res[1] == 6 && !res[2]) vv_version = 9;
		else if(res[1] == 6 && res[2]) vv_version = 10;
	} else if(agt.indexOf("aol") != -1) {
		vv_browser = 4;
		if(is_major < 4) vv_version = 11;
		else if(is_major == 4 && agt.indexOf("msie 5.0")==-1) vv_version = 12;
	} else if(agt.indexOf("opera") != -1) vv_browser = 5;
	else if(agt.indexOf("webtv") != -1) vv_browser = 3;
	vv_cookies = (window.navigator.cookieEnabled?1:0);
}

function tstDataErro(data) {
	var res = null;
	if(!(res = data.value.split(/-/)) || res.length!=3) return false;
	var dia = parseInt(res[0], 10);
	var mes = parseInt(res[1], 10);
	var ano = parseInt(res[2], 10);
	if(dia > 31) return false;
	if(mes > 12) return false;
	if(ano < 1900) return false;
	return true;
}

