/**
 * since update	- 2009-03-06
 */

	function setVisibility(id, visibility) {
		if(document.getElementById(id)) {
			document.getElementById(id).style.display = visibility;
		}
	}


	function validaForm(form) {
		var pre = '  * ';
		var txt = '';

		if(form.entidade_nome.value=='') { txt += pre+'Nome da entidade\n'; }

		if(form.codigo_postal_escola.value=='') { txt += pre+'Código Postal\n'; }
		else if(ValidaCP(form.codigo_postal_escola.value,false)!=true) { txt += pre+'Verifique o Código Postal\n'; }

/*		if(form.localidade_escola.value=='') { txt += pre+'Localidade da escola\n' }*/

		if(form.contacto_escola.value=='') { txt += pre+'Contacto da entidade\n'; }
/* else if(checkStringLength(form.contacto.value,9)!=true) { txt += pre+'O contacto tem de ter 9 caracteres\n'; }*/

/*		if(form.email_escola.value=='') { txt += pre+'e-Mail da escola\n'; } else if(isValidEmail(form.email_escola.value)==false) { txt += pre+'e-Mail da escola inválido\n'; }*/

		if(form.email_escola.value!='') { if(isValidEmail(form.email_escola.value)==false) { txt += pre+'e-Mail da escola inválido\n'; } }

		if(form.nif.value=='') { txt += pre+'NIF\n'; }

/*		if(form.ano.value=='') { txt += pre+'Ano\n'; }*/

		if(form.nome_requisitante.value=='') { txt += pre+'Nome do requisitante\n'; }

		if(form.contacto_professor.value=='') { txt += pre+'Contacto do requisitante\n'; }

		if(txt=='') { form.submit(); }
		else {
			txt = 'Campos por preencher:\n'+txt;
			alert(txt);
			return false;
		}


		return false;
	}

	function hideDivBox(segundos) {self.setTimeout("setVisibility('box_apresentacao', 'none')", 1000*segundos); }

	function trim(str, chars) {
		return ltrim(rtrim(str, chars), chars);
	}

	function ltrim(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	}

	function rtrim(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	}

	function ValidaCP(cp,comment) {
		pos = cp.indexOf('-');
		if (pos < 0) {
			if(comment==true) { alert("Introduza o Código Postal no formato NNNN-NNN!"); }
			return false;
		}
		else {
			first = trim(cp.substr(0, pos));
			if (first.length != 4) {
				if(comment==true) { alert("Introduza o Código Postal no formato NNNN-NNN!"); }
				return false;
			}
			last = trim(cp.substr(pos+1));
			if (last.length != 3) {
				if(comment==true) { alert("Introduza o Código Postal no formato NNNN-NNN!"); }
				return false;
			}
			if ((isNaN(first)) || (isNaN(last)) || (pos!=4)) {
				if(comment==true) { alert("Introduza o Código Postal no formato NNNN-NNN!"); }
				return false;
			}
		}
		return true;
	}

	function getKey(e) {
		if (window.event)
			return window.event.keyCode;
		else if (e)
			return e.which;
		else
			return null;
	}

	function validChars(e, valid) {
		var key = getKey(e);
		if (key == null) return true;

		// get character
		var keychar = String.fromCharCode(key);
		keychar = keychar.toLowerCase();
		valid = valid.toLowerCase();

		// check valid keys
		if (valid.indexOf(keychar) != -1)
			return true;

		// control keys
		if (key == null || key == 0 || key == 8 || key == 9 || key == 13 || key == 27)
			return true;

		// else return false
		return false;
	}

	function checkLength(string,size) {
		if(string.length!=size) {
			alert("Tem de preencher "+size+" caracteres neste campo.");
		}
	}

	function checkStringLength(string,size) {
		if(string.length!=size) { return false; }
		return true;
	}

	function isValidEmail(str) {
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	}

	function checkMail(str) {
		if(isValidEmail(str)==false) { alert("e-Mail inválido"); }
	}

	function valida_data_ddmmaaaa(data) { // 24/01/2007
		data = data.value;

		// aqui está assim por causa da postagem do Wordpress 8^S
		if(data!='dd/mm/aaaa' && !data.match(/^((([0][1-9]|[12][0-9])\/02\/(19|20)([13579][26]|[02468][048]))|(([0][1-9]|[1][0-9]|[2][0-8])\/02\/(19|20)([02468][12356]|[013579][13579]))|((([0][1-9]|[12][0-9]|30)\/(0[469]|11)|([0][1-9]|[12][0-9]|3[01])\/(0[13578]|1[02]))\/((19|20)[0-9][0-9])))$/)) {
			alert("A data inserida "+data+" é inválida!");
			return false;
		}
		return true;
	}


