﻿//<![CDATA[
//]]>

function getStyle(oElm, strCssRule){
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if(oElm.currentStyle){
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}

function largura() {
	var largura = 0;
	
	if( typeof( window.innerWidth ) == 'number' ) {
    	largura = window.innerWidth;
  	} else if( document.documentElement && document.documentElement.clientWidth ) {
    	largura = document.documentElement.clientWidth;
  	} else if( document.body && document.body.clientWidth ) {
    	largura = document.body.clientWidth;
  	}
	
	return largura;
}

function altura() {
	var altura = 0;
  	if( typeof( window.innerHeight ) == 'number' ) {
    	altura = window.innerHeight;
  	} else if( document.documentElement && document.documentElement.clientHeight ) {
    	altura = document.documentElement.clientHeight;
  	} else if( document.body && document.body.clientHeight ) {
    	altura = document.body.clientHeight;
  	}
	
	return altura;
}

function muda_altura(id, altura_ocupada) {  
  	document.getElementById(id).style.height = (altura() - altura_ocupada) + "px";
}

function muda_largura(id, largura_ocupada) {  
  	document.getElementById(id).style.width = (largura() - largura_ocupada) + "px";
}

function copia_dimensoes(id_origem, id_destino) {
	var altura_origem = getStyle(document.getElementById(id_origem), "height");
	var largura_origem = getStyle(document.getElementById(id_origem), "width");
	
	document.getElementById(id_destino).style.height = altura_origem;
	document.getElementById(id_destino).style.width = largura_origem;
}

function comprimento(id, propriedade) {
	var valor_str = getStyle(document.getElementById(id), propriedade);
	var valor = parseInt(valor_str.substring(0, valor_str.length - 2));
	
	return valor;
}

function centrarConteudo() {
	var largura_janela = largura();
	var altura_janela = altura();
	var largura_site = 960;
	var altura_site = 900;
	
	//document.getElementById('pagina').style.top = ((altura_janela - altura_site) / 2) + "px";
	document.getElementById('pagina').style.left = ((largura_janela - largura_site) / 2) + "px";
}

function popup(url, width, height) {
	var largura_janela = largura();
	var altura_janela = altura();
	var newwindow;
	newwindow=window.open(url,'name','height=' + height + ',width=' + width + ',left=' + ((largura_janela - width) / 2) + ', top=' + ((altura_janela - height - 50) / 2));
	if (window.focus) {newwindow.focus()}
}

function validarRegisto() {
	var form = document.reg_user;
	var erros = '';
	
	if(form.reg_nome.value == '') {
		erros +="O nome não pode estar vazio.\n";
	} 
	
	if(form.reg_sobrenome.value == '') {
		erros += "O apelido não pode estar vazio.\n";
	}
	
	if(form.reg_cod_postal_A.value == '') {
		erros += "O código postal não pode estar vazio.\n";
	} else if(isNaN(parseInt(form.reg_cod_postal_A.value)) || (form.reg_cod_postal_B.value != '' && isNaN(parseInt(form.reg_cod_postal_B.value)))) {
		erros += "O código postal não pode conter letras.\n";
	}
	
	if(form.reg_cod_postal_A.value.length < 4) {
		erros += "A parte principal do código postal tem que ter 4 dígitos.\n";
	}
	
	if(form.reg_cod_postal_B.value != '' && form.reg_cod_postal_B.value.length < 3) {
		erros += "A parte complementar do código postal tem que ter 3 dígitos.\n";
	}
	
	if(form.reg_email.value == '') {
		erros += "O email não pode estar vazio.\n";
	}
	
	if(form.reg_tel.value == '') {
		erros += "O telefone não pode estar vazio.\n";
	}
	
	if(isNaN(parseInt(form.reg_tel.value))) {
		erros += "O telefone não pode conter letras.\n";
	}
	
	if(form.reg_username.value == '') {
		erros += "O username não pode estar vazio.\n";
	}
	
	if(form.reg_password.value == '') {
		erros += "A password não pode estar vazia.\n";
	}
	
	if(erros == '') {
		return true;
	} else {
		alert(erros);
		return false;
	}
}
