///////////////////////////////////////////////

function formCheckCarreiraDetalhe(form){

	// name of mandatory fields
	var fieldRequired = Array(
				"name",
				"lastname",
				"email",
				"cellphone",
				"gender",
				"birthday_day",
				"birthday_month",
				"birthday_year"
			);
	// field description to appear in the dialog box
	var fieldDescription = Array(
				"Nome",
				"Apelido",
				"E-Mail",
				"Telemóvel",
				"Sexo",
				"Dia de Nascimento",
				"Mês de Nascimento",
				"Ano de Nascimento"
			);
	// dialog message
	var alertMsg = "Os seguintes campos são obrigatórios:\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = form.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		var email = document.form.email.value;
		var file = document.form.doc_id_1.value;

		if(checkEmail(email) && checkFileType(form,file)){
			form.submit();
		}
		else{
			return false;
		}
	}else{
		alert(alertMsg);
		return false;
	}
}

////////////////////////////////////////////

function formCheckContactosPedidoInfo(form){

	// name of mandatory fields
	var fieldRequired = Array(
				"name",
				"lastname",
				"email",
				"phone",
				"subject"
			);
	// field description to appear in the dialog box
	var fieldDescription = Array(
				"Nome",
				"Apelido",
				"E-Mail",
				"Telefone",
				"Assunto"
			);
	// dialog message
	var alertMsg = "Os seguintes campos são obrigatórios:\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = form.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" || obj.options[obj.selectedIndex].value == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		var email = document.form.email.value;
		
		if(checkEmail(email)){
			form.submit();
		}
		else{
			return false;
		}
	}else{
		alert(alertMsg);
		return false;
	}
}

///////////////////////////////////////////////

function formCheckRegisto(form){
	
	// name of mandatory fields
	var fieldRequired = Array(
				"name",
				"lastname",
				"email",
				"gender",
				"birthday_day",
				"birthday_month",
				"birthday_year"
			);
	// field description to appear in the dialog box
	var fieldDescription = Array(
				"Nome",
				"Apelido",
				"E-Mail",
				"Sexo",
				"Dia de Nascimento",
				"Mês de Nascimento",
				"Ano de Nascimento"
			);
	// dialog message
	var alertMsg = "Os seguintes campos são obrigatórios:\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = form.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		var email = document.form.email.value;
		
		if(checkEmail(email)){
			form.submit();
		}
		else{
			return false;
		}
	}else{
		alert(alertMsg);
		return false;
	}
}

////////////////////////////////////////////

function isNumberKey(evt)
{
   var charCode = (evt.which) ? evt.which : event.keyCode
   if (charCode > 31 && (charCode < 48 || charCode > 57))
      return false;

   return true;
}

function checkEmail(email) {

	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		alert('Introduza um email válido!');
		email.focus
		return false;
	}
	return true;
}

// check file type:
function checkFileType(form, file) {
	extArray = new Array(".doc", ".txt", ".docx", ".rtf", ".pdf"); 
	allowSubmit = false;
	if (!file) return true;
	while (file.indexOf("\\") != -1)
		file = file.slice(file.indexOf("\\") + 1);
	ext = file.slice(file.indexOf(".")).toLowerCase();
	for (var i = 0; i < extArray.length; i++) {
		if (extArray[i] == ext) { allowSubmit = true; break; }
	}
	if (allowSubmit) form.submit();
	else
		alert("Só são aceites ficheiros com extensão: "
				+ (extArray.join(" ")) + "\n"
				+ "Seleccione outro ficheiro e proceda ao reenvio do formulário");
}
////////////////////////////////////////////

// calc comissao:
function updatesum() {
	var tmp_pvp = document.formEditar.pvpPresente.options[document.formEditar.pvpPresente.selectedIndex].value;
	var tmp_iva = document.formEditar.iva.options[document.formEditar.iva.selectedIndex].value;
	var tmp_com = document.formEditar.avebComissao.options[document.formEditar.avebComissao.selectedIndex].value;
	var ext = document.formEditar.destaque40Dias.checked;
	pvp = tmp_pvp * 1;
	iva = tmp_iva * 1;
	com = tmp_com * 1;

	if ( pvp && iva && com ){
		//alert("CALCULA");
		if(iva == 0)  iva = '0.00';
		if(iva == 4)  iva = '1.04';
		if(iva == 5)  iva = '1.05';
		if(iva == 8)  iva = '1.08';
		if(iva == 12) iva = '1.12';
		if(iva == 15) iva = '1.15';
		if(iva == 20) iva = '1.20';
		
		if(ext) com = (com + 5);
		if(com == 25) com = '0.75';
		if(com == 30) com = '0.70';
		if(com == 35) com = '0.65';
		if(com == 40) com = '0.60';
		if(com == 45) com = '0.55';
		/*
		alert(pvp);
		alert(iva);
		alert(com);
		alert(ext);
		*/
		var val = ((pvp / iva) * com);
		var preco = roundNumber(val,2);
		document.formEditar.precoAveb.value = preco;
	}
}

// round
function roundNumber(number,decimal_points) {
	if(!decimal_points) return Math.round(number);
	if(number == 0) {
		var decimals = "";
		for(var i=0;i<decimal_points;i++) decimals += "0";
		return "0."+decimals;
	}

	var exponent = Math.pow(10,decimal_points);
	var num = Math.round((number * exponent)).toString();
	return num.slice(0,-1*decimal_points) + "." + num.slice(-1*decimal_points)
}