/* * * * * * * * * * * * * * * * * * * * * * * 
 *	NETUAL-Multimédia e Telecomuncações
 *
 *	Scripts Genéricos usados em Validações/cálculos de Formulários
 *	V1	
 *  14-07-2003
 */

/////////////////////////////////////////////////////
//	function isEmpty(textcontrol)
//	Retorna true, se o campo estiver vazio. Se estiver só com espaços em branco conta como vazio.
function size(textcontrol){
	if(textcontrol!=null){
		str = textcontrol.value;
		return str.length;
	}
	return 0;
}
 
/////////////////////////////////////////////////////
//	function isEmpty(textcontrol)
//	Retorna true, se o campo estiver vazio. Se estiver só com espaços em branco conta como vazio.
function isEmpty(textcontrol){
	if(textcontrol!=null){
		str = textcontrol.value;
		for (i = 0; i <  str.length; i++){
			chr = str.substring(i, i + 1);
			if (chr != ' '){ return false; }
		}
	}
	return true;
}
/////////////////////////////////////////////////////
//	function isNumber(textcontrol)
//	Retorna true, se o campo estiver vazio. Se estiver só com espaços em branco conta como vazio.
function isNumber(textcontrol){
	str = textcontrol.value;
	if(str==null){
		return false;
	}else{
		for (i = 0; i <  str.length; i++){
			chr = str.substring(i, i + 1);
			if ((chr>'9' || chr<'0')){
				if(!(chr==',' || chr=='.')){ 
					return false; 
				}
			}
		}
	}
	return true;
}

/////////////////////////////////////////////////////
//	function IsEmailValid
//  DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
//
function IsEmailValid(str) {
	var at   = "@";
	var dot  = ".";
	var lat  = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	if (str.indexOf(at)==-1){	/*alert("Invalid E-mail ID")*/						return false;	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){		return false;	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){		return false;	}
	if (str.indexOf(at,(lat+1))!=-1){												return false;	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){			return false;	}
	if (str.indexOf(dot,(lat+2))==-1){												return false;	}
	if (str.indexOf(" ")!=-1){														return false;	}
 	return true;
}

/////////////////////////////////////////////////////
//	function submitonce(theform)
//		Desabilita os botoes de submit nos forms após o submit
//		ONSUBMIT="submitonce(document.formname);"
function submitonce(theform){
	if (document.all || document.getElementById){
		for (i=0;i<theform.length;i++){
			var tempobj = theform.elements[i];
			if(tempobj.type.toLowerCase()=="submit") tempobj.disabled = true;
		}
	}
}
/////////////////////////////////////////////////////
//	Já vem do webmail
//	function ToggleAll(theForm)
//		Valida o correcto preenchimento do form para inserir recursos humanos
function ToggleAll(theForm) {
	//checkbox adý allbox olacak ve onClick="CheckAll();" 
	for (var i=0;i<theForm.elements.length;i++) {
		var e = theForm.elements[i];
  		//alert(e.name);
		if (e.name != 'allbox')   e.checked = theForm.allbox.checked;
	}
}

/////////////////////////////////////////////////////////////////////////////////
//	ValidateFormRegisto1
//	Exemplo de validaçãod e um form
//		onclick = "return ValidateFormRegisto1(document.formname)"
/////////////////////////////////////////////////////////////////////////////////
/*
function ValidateFormRegisto1(theForm){
	var nome    = theForm.nome;
	var emailID = theForm.email;
	var passID  = theForm.password;
	if ((nome.value==null)||(nome.value=="")){
		nome.focus();
		alert("Por favor, insira o nome.");
		return false;
	}
	if ((emailID.value==null)||(emailID.value=="")){
		emailID.focus();
		alert("Por favor, insira o endereço de email.");
		return false;
	}
	if(!IsEmailValid(emailID.value)){
		emailID.value = "";
		emailID.focus();
		alert("Por favor, insira um endereço de email válido.")
		return false;
	}
	if ((passID.value==null)||(passID.value=="")){
		passID.focus();
		alert("Por favor, insira a password.");
		return false;
	}
	if ((passID.value.length<4)||(passID.value.length>10)){
		passID.focus();
		alert("Por favor, insira uma password válida, com 4 a 10 caractéres.");
		return false;
	}
	return true;
}
*/


/////////////////////////////////////////////////////
//	pagina:form_recursos.asp
//	function Validate_UpdateProjecto(theForm)
//		Valida o correcto preenchimento do form sobre projecto
function Validate_UpdateProjecto(theForm){
	var campo1	= theForm.titulo;
	var campo2	= theForm.acronimo;
	
	// Campos que não existem no formulário, ou estão replicados
	if (campo1==null ||campo2==null){
		return false;
	}
	if (isEmpty(campo1)){
		campo1.focus();
		alert("Por favor, preencha o Título do Projecto.");
		return false;
	}
	if (isEmpty(campo2)){
		campo2.focus();
		alert("Por favor, preencha o Acrónimo.");
		return false;
	}
	return true;
}


/////////////////////////////////////////////////////
//	pagina:form_recursos.asp
//	function Validate_InsertRecursos(theForm)
//		Valida o correcto preenchimento do form para inserir recursos humanos
function Validate_InsertRecursos(theForm){
	var campo1	= theForm.recurso;
	var campo2	= theForm.meses;
	var campo3  = theForm.mensal;
	
	// Campos que não existem no formulário, ou estão replicados
	if (campo1==null ||campo2==null ||campo3==null){
		return false;
	}
	if (isEmpty(campo1)){
		campo1.focus();
		alert("Por favor, preencha o nome do recurso humano.");
		return false;
	}
	if (isEmpty(campo2)){
		campo2.focus();
		alert("Por favor, preencha o número de meses.");
		return false;
	}
	if (!isNumber(campo2)){
		campo2.focus();
		alert("Por favor, preencha um valor numérico no número de meses.");
		return false;
	}
	
	var pos = campo2.value.lastIndexOf(",");
	if (pos!=-1){
		if(campo2.value.length-pos>3){
			campo2.focus();
			alert("Por favor, preencha um valor utilizando até duas casas decimais.");
			return false;
		}
	}
	var pos = campo2.value.lastIndexOf(".");
	if (pos!=-1){
		if(campo2.value.length-pos>3){
			campo2.focus();
			alert("Por favor, preencha um valor utilizando até duas casas decimais.");
			return false;
		}
	}
	if (isEmpty(campo3)){
		campo3.focus();
		alert("Por favor, preencha o custo mensal.");
		return false;
	}
	if (!isNumber(campo3)){
		campo3.focus();
		alert("Por favor, preencha um valor numérico no custo mensal.");
		return false;
	}
	return true;
}


/////////////////////////////////////////////////////
//	pagina:form_recursos.asp
//	function Validate_InsertServicos(theForm)
//		Valida o correcto preenchimento do form para lista de serviços de desenvolvimento
function Validate_InsertServicos(theForm){
	var nome	= theForm.recurso;
	var mes		= theForm.mes;
	var quant	= theForm.quantidade;
	var total	= theForm.total;
	if (isEmpty(nome)){
		nome.focus();
		alert("Por favor, preencha os serviços a adquirir.");
		return false;
	}
	if (isEmpty(mes)){
		mes.focus();
		alert("Por favor, preencha o mês.");
		return false;
	}
	if (isEmpty(quant)){
		quant.focus();
		alert("Por favor, preencha o valor a quantidade.");
		return false;
	}
	if (!isNumber(quant)){
		quant.focus();
		alert("Por favor, preencha um valor numérico no campo da quantidade.");
		return false;
	}
	if (isEmpty(total)){
		total.focus();
		alert("Por favor, preencha o valor total em €.");
		return false;
	}
	if (!isNumber(total)){
		total.focus();
		alert("Por favor, preencha um valor numérico no campo do total em €.");
		return false;
	}
	return true;
}


/////////////////////////////////////////////////////
//	pagina:form_recursos.asp
//	function Validate_InsertEquipamento(theForm)
//		Valida o correcto preenchimento do form para lista de equipamento
function Validate_InsertEquipamento(theForm){
	var nome	= theForm.recurso;
	var mes	= theForm.mes;
	var quant	= theForm.quantidade;
	var total	= theForm.total;
	if (isEmpty(nome)){
		nome.focus();
		alert("Por favor, preencha o equipamento a adquirir.");
		return false;
	}
	if (isEmpty(mes)){
		mes.focus();
		alert("Por favor, preencha a mês.");
		return false;
	}
	if (isEmpty(quant)){
		quant.focus();
		alert("Por favor, preencha o valor a quantidade.");
		return false;
	}
	if (!isNumber(quant)){
		quant.focus();
		alert("Por favor, preencha um valor numérico no campo da quantidade.");
		return false;
	}
	if (isEmpty(total)){
		total.focus();
		alert("Por favor, preencha o valor total em €.");
		return false;
	}
	if (!isNumber(total)){
		total.focus();
		alert("Por favor, preencha um valor numérico no campo do total em €.");
		return false;
	}
	return true;
}

/////////////////////////////////////////////////////
//	pagina:site_contas.asp
//	function Validate_InsertCandidatura(theForm)
//		Valida o correcto preenchimento do form para inserir candidaturas
function Validate_InsertCandidatura(theForm){
	var cp1	= theForm.acronimo;
	var cp2	= theForm.titulo;
	var cp3 = theForm.id_area;
	if (isEmpty(cp1)){
		cp1.focus();
		alert("Por favor, preencha o acrónimo da candidatura a inserir.");
		return false;
	}
	if (isEmpty(cp2)){
		cp2.focus();
		alert("Por favor, preencha o título da candidatura a inserir.");
		return false;
	}
	return true;
}
/////////////////////////////////////////////////////
//	pagina:form_resultados.asp
//	function Validate_InsertTarefa(theForm)
//		Valida o correcto preenchimento do form para inserir recursos humanos
function Validate_InsertTarefa(theForm){
	var nome	= theForm.tarefa;
	var inicio	= theForm.inicio;
	var fim	    = theForm.fim;
	if (isEmpty(nome)){
		nome.focus();
		alert("Por favor, preencha o nome da tarefa a inserir.");
		return false;
	}
	if (isEmpty(inicio)){
		inicio.focus();
		alert("Por favor, preencha o mês de início.");
		return false;
	}
	if (isEmpty(fim)){
		fim.focus();
		alert("Por favor, preencha o mês do fim.");
		return false;
	}
	if (parseInt(inicio.value) > parseInt(fim.value)){
		fim.focus();
		alert("Atenção, a data do fim deve ser posterior à data de início.");
		return false;
	}
	return true;
}
/////////////////////////////////////////////////////
//	pagina:form_tarefas.asp
//	function Validate_InsertResultado(theForm)
//		Valida o correcto preenchimento do form para inserir recursos humanos
function Validate_InsertResultado(theForm){
	var resultado	= theForm.resultado;
	var designacao	= theForm.designacao;
	var mes			= theForm.mes;
	//if (isEmpty(resultado)){
	//	resultado.focus();
	//	alert("Por favor, preencha o campo resultado.");
	//	return false;
	//}
	if (isEmpty(designacao)){
		designacao.focus();
		alert("Por favor, preencha o campo designação.");
		return false;
	}
	if (isEmpty(mes)){
		mes.focus();
		alert("Por favor, preencha o campo com um mês.");
		return false;
	}
	return true;
}

/////////////////////////////////////////////////////
//	pagina:form_projectos.asp
//	function Validate_InsertCorrelacao(theForm)
//		Valida o correcto preenchimento do form para inserir recursos humanos
function Validate_InsertCorrelacao(theForm){
	var nome	= theForm.acronimo;
	if (isEmpty(nome)){
		nome.focus();
		alert("Por favor, preencha o acrónimo do projecto correlacionado.");
		return false;
	}
	return true;
}

/////////////////////////////////////////////////////
//	pagina:form_projectos.asp
//	function SetAllChecked(theForm, value)
//		Coloca o valor "value" em todos os checkbox do form
function SetAllChecked(theForm, value, val) {
	for (var i=0;i<theForm.elements.length;i++) {
		var e = theForm.elements[i];					//alert(e.name);
		if (e.type.toLowerCase()=="checkbox"){			//alert (e.type.toLowerCase())	//"submit"
			if (e.name  != 'allbox')	 e.checked = value;
			if (e.value != val)			 e.checked = value;		//alert(e.value);
			else e.checked = true;
			//else alert(val)
		}
	}
}


/////////////////////////////////////////////////////
//	pagina:
//	function Validate_TextAreas(theForm)
//		Valida as dimensoes dos inputs do tipo textarea
function Validate_TextAreas(theForm) {
	if (theForm.elements==null) return false;
	for (var i=0;i<theForm.elements.length;i++) {
		var e = theForm.elements[i];
		if (e.type.toLowerCase()=="textarea"){			//alert (e.type.toLowerCase())	//"submit" "text" "textarea" "hidden"
			var i1  = i+1;
			if(i1<theForm.elements.length){
				var e1 = theForm.elements[i1];
				if (e1.type.toLowerCase()=="hidden" && e1.name=="maxlength"){
					var maxLen = parseInt(e1.value);
					if(e.value.length > maxLen){
						e.focus();
						alert("Atenção, a dimensão do texto excede o limite máximo permitido: "+maxLen+" caracteres.");
						return false;
					}
				}
			}
			//str = textcontrol.value;
			//for (i = 0; i <  str.length; i++){
			//	chr = str.substring(i, i + 1);
			//	if (chr != ' '){ return false; }
			//if (e.name  != 'allbox')	 e.checked = value;
			//if (e.value != val)		 e.checked = value;		//alert(e.value);
		}
	}
	return true;
}

/////////////////////////////////////////////////////
//	pagina:form_detalhada.asp
//	function Validate_Detalhada(theForm)
//		Valida as dimensoes dos textos
function Validate_Detalhada(theForm) {
	return Validate_TextAreas(theForm);
}
