// JavaScript Document

function js_checkClienteName(){
	js_progressIcon(true);
	var x = document.getElementById("f_username");
	if(x.value==''){
		x.style.backgroundColor='#FF9933';
		js_progressIcon(false);
		return false;
	}
	else{
		xajax_fn_checkClienteNameAvailability(x.value);
	}	
}

function js_updateClienteNameAvailability(j_availability){
	js_progressIcon(true);
	var x = document.getElementById("f_checkavailable");
	var y = document.getElementById("f_username");
	if(j_availability==0){
		y.style.backgroundColor='#FFFFFF';
		x.style.color='#00CC33';
		x.innerHTML="Sim";
	}
	if(j_availability==1){
		y.style.backgroundColor='#FF9933';
		x.style.color='#CC3333';
		x.innerHTML="N&atilde;o";
	}
}

function js_auxNewCliente(){
	js_progressIcon(true);
	//This only checks for the name availability (server side stuff). If it is ok, the server will continue with email creation
	var x = document.getElementById("f_username");
	if(x.value==''){
		x.style.backgroundColor='#FF9933';
		js_progressIcon(false);
		return false;
	}
	else{
		xajax_fn_auxCreateClienteAccount(x.value);
	}
}

function js_newCliente(j_availability){
	js_progressIcon(true);
	var j_AllInOrder = true;
	var p1 = document.getElementById("f_password");
	var p2 = document.getElementById("f_password2");
	var pc1 = document.getElementById("f_checkpassword");
	var pc2 = document.getElementById("f_samepassword");
	var contribuinte = document.getElementById("f_contribuinte");
	var email = document.getElementById("f_email");
	var telefone = document.getElementById("f_telefone");
	var nome = document.getElementById("f_nome");
	var morada = document.getElementById("f_morada");
	var termos = document.getElementById("f_aceitatermos");
	var contribuintec = document.getElementById("f_contribuinteinvalido");
	var emailc = document.getElementById("f_emailinvalido");
	var telefonec = document.getElementById("f_telefoneinvalido");
	var termosc = document.getElementById("f_termosporaceitar");
	var nomec = document.getElementById("f_nomevalido");
	var moradac = document.getElementById("f_moradavalida");
	pc1.innerHTML="";
	pc2.innerHTML="";
	contribuintec.innerHTML="";
	emailc.innerHTML="";
	telefonec.innerHTML="";
	termosc.innerHTML="";
	nomec.innerHTML="";
	moradac.innerHTML="";
	p1.style.backgroundColor='#FFFFFF';
	p2.style.backgroundColor='#FFFFFF';
	contribuinte.style.backgroundColor='#FFFFFF';
	telefone.style.backgroundColor='#FFFFFF';
	email.style.backgroundColor='#FFFFFF';
	nome.style.backgroundColor='#FFFFFF';
	morada.style.backgroundColor='#FFFFFF';
	if(nome.value==""){
		nome.style.backgroundColor='#FF9933';
		nomec.innerHTML = "Nome obrigat&oacute;rio!";
		j_AllInOrder = false;
	}
	if(morada.value==""){
		morada.style.backgroundColor='#FF9933';
		moradac.innerHTML = "Morada obrigat&oacute;ria!";
		j_AllInOrder = false;
	}
	if(p1.value==""){
		p1.style.backgroundColor='#FF9933';
		pc1.innerHTML = "Senha obrigat&oacute;ria!";
		j_AllInOrder = false;
	}
	if(p1.value!=p2.value){
		p1.style.backgroundColor='#FF9933';
		p2.style.backgroundColor='#FF9933';
		pc2.innerHTML = "Senhas diferentes!";
		j_AllInOrder = false;
	}
	
	if(js_numeroValido(contribuinte.value)==false){
		contribuinte.style.backgroundColor='#FF9933';
		contribuintec.innerHTML = "N&uacute;mero inv&aacute;lido!";
		j_AllInOrder = false;
	}
	
	if(js_numeroValido(telefone.value)==false){
		telefone.style.backgroundColor='#FF9933';
		telefonec.innerHTML = "N&uacute;mero inv&aacute;lido!";
		j_AllInOrder = false;
	}
	
	if(mailValido(email.value)==false){
		email.style.backgroundColor='#FF9933';
		emailc.innerHTML = "E-mail inv&aacute;lido!";
		j_AllInOrder = false;
	}
	
	if(termos.checked==false){
		termosc.innerHTML = "Deve aceitar os termos &amp; condi&ccedil;&otilde;es!";
		j_AllInOrder = false;
	}

	if(j_availability==1)
		j_AllInOrder=false;

	if(j_AllInOrder){
		xajax_fn_createClienteAccount(xajax.getFormValues("clientRegistration"));
		return false;
	}
	else{
		js_progressIcon(false);
		return false;
	}
}

function js_loginCliente(){
	js_progressIcon(true);
	var utilizador = document.getElementById("login_utilizador");
	var senha = document.getElementById("login_senha");
	var login = document.getElementById("login_submit");
	var j_AllInOrder=true;
	
	senha.style.backgroundColor='#FFFFFF';
	utilizador.style.backgroundColor='#FFFFFF';
	
	if(senha.value==""){
		senha.style.backgroundColor='#FF9933';
		j_AllInOrder=false;
	}
	
	if(utilizador.value==""){
		utilizador.style.backgroundColor='#FF9933';
		utilizador.value = "Utilizador";
		j_AllInOrder=false;
	}
	
	if(j_AllInOrder){
		xajax_fn_loginCliente(xajax.getFormValues("login_cliente"));
		return false;
	}
	else{
		js_progressIcon(false);
		return false;
	}
}

function js_logOut(){
	js_progressIcon(true);
	xajax_fn_logOutCliente();
}

function js_adquirirServicoCliente(){
	js_progressIcon(true);
	xajax_fn_formularioAdquirirServicoCliente();
}

function js_updatePrecosServico(){
	js_progressIcon(true);
	var elemento = document.getElementById("f_servicoEscolhido");
	var opcao = elemento.options[elemento.selectedIndex].value;
	xajax_fn_updatePrecoServico(opcao);
}

function js_adicionarServicoCliente(){
	js_progressIcon(true);
	var servico = document.getElementById("f_servicoEscolhido");
	var preco = document.getElementById("f_pagamentoEscolhido");
	var servicoc = document.getElementById("f_servicoNaoEscolhido");
	var precoc = document.getElementById("f_pagamentoNaoEscolhido");
	servico.style.backgroundColor='#FFFFFF';
	preco.style.backgroundColor='#FFFFFF';
	servicoc.innerHTML = "";
	precoc.innerHTML = "";
	var allOK=true;
	if(servico.selectedIndex<1){
		allOK=false;
		servicoc.innerHTML = "Escolha servi&ccedil;o";
		servico.style.backgroundColor='#FF9933';
	}
	if(preco.selectedIndex<0){
		allOK=false;
		precoc.innerHTML = "Escolha pagamento";
		preco.style.backgroundColor='#FF9933';
	}
	
	if(allOK)
		xajax_fn_adquirirServicoCliente(xajax.getFormValues("adicionar_servicos"));
	else
		js_progressIcon(true);
	return false;
}

function js_alterarDadosCliente(){
	js_progressIcon(true);
	xajax_fn_alteraDadosCliente();
}

function js_editCliente(j_availability){
	js_progressIcon(true);
	var j_AllInOrder = true;
	var contribuinte = document.getElementById("f_contribuinte");
	var email = document.getElementById("f_email");
	var telefone = document.getElementById("f_telefone");
	var contribuintec = document.getElementById("f_contribuinteinvalido");
	var emailc = document.getElementById("f_emailinvalido");
	var telefonec = document.getElementById("f_telefoneinvalido");
	var nome = document.getElementById("f_nome");
	var morada = document.getElementById("f_morada");
	var nomec = document.getElementById("f_nomevalido");
	var moradac = document.getElementById("f_moradavalida");
	contribuintec.innerHTML="";
	emailc.innerHTML="";
	telefonec.innerHTML="";
	nomec.innerHTML="";
	moradac.innerHTML="";
	contribuinte.style.backgroundColor='#FFFFFF';
	telefone.style.backgroundColor='#FFFFFF';
	email.style.backgroundColor='#FFFFFF';
	nome.style.backgroundColor='#FFFFFF';
	morada.style.backgroundColor='#FFFFFF';

	if(nome.value==""){
		nome.style.backgroundColor='#FF9933';
		nomec.innerHTML = "Nome obrigat&oacute;rio!";
		j_AllInOrder = false;
	}
	if(morada.value==""){
		morada.style.backgroundColor='#FF9933';
		moradac.innerHTML = "Morada obrigat&oacute;ria!";
		j_AllInOrder = false;
	}
	if(js_numeroValido(contribuinte.value)==false){
		contribuinte.style.backgroundColor='#FF9933';
		contribuintec.innerHTML = "N&uacute;mero inv&aacute;lido!";
		j_AllInOrder = false;
	}
	
	if(js_numeroValido(telefone.value)==false){
		telefone.style.backgroundColor='#FF9933';
		telefonec.innerHTML = "N&uacute;mero inv&aacute;lido!";
		j_AllInOrder = false;
	}
	
	if(mailValido(email.value)==false){
		email.style.backgroundColor='#FF9933';
		emailc.innerHTML = "E-mail inv&aacute;lido!";
		j_AllInOrder = false;
	}
	
	if(j_availability==1)
		j_AllInOrder=false;

	if(j_AllInOrder){
		xajax_fn_editClienteAccount(xajax.getFormValues("clientRegistration"));
		return false;
	}
	else{
		js_progressIcon(true);
		return false;
	}
}

function js_alterarSenhaCliente(){
	js_progressIcon(true);
	var j_AllInOrder = true;
	var p1 = document.getElementById("f_password");
	var p2 = document.getElementById("f_password2");
	var pc1 = document.getElementById("f_checkpassword");
	var pc2 = document.getElementById("f_samepassword");
	pc1.innerHTML="";
	pc2.innerHTML="";
	p1.style.backgroundColor='#FFFFFF';
	p2.style.backgroundColor='#FFFFFF';
	if(p1.value==""){
		p1.style.backgroundColor='#FF9933';
		pc1.innerHTML = "Senha obrigat&oacute;ria!";
		j_AllInOrder = false;
	}
	if(p1.value!=p2.value){
		p1.style.backgroundColor='#FF9933';
		p2.style.backgroundColor='#FF9933';
		pc2.innerHTML = "Senhas diferentes!";
		j_AllInOrder = false;
	}
	if(j_AllInOrder){
		xajax_fn_clienteAlterarSenha(xajax.getFormValues("clientRegistration"));
		return false;
	}
	else{
		js_progressIcon(true);
		return false;
	}
}

function js_obterDetalheServicoCliente(servico){
	js_progressIcon(true);
	xajax_fn_obterDetalheServicoCliente(servico);	
}