function popUp(link, w, h) {
    window.open(link, '', 'width=' + w + ',height=' + h + ',left=' + ((screen.width - w) / 2) + ', top=' + ((screen.height - h) / 2) + 'resizable=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes');
}

function photo(photoLink, titulo) {
    newWindow = window.open('about:blank', '', 'scrollbars=no, width=1, height=1, left=' + ((screen.width - 160) / 2) + ', top=' + ((screen.height - 136) / 2));

    with (newWindow.document){
        writeln('<html>');
        writeln('<head>');
        writeln('<title>A carregar</title>');
        writeln('<script>');
        writeln('var MT = 0;');
        writeln('var ML = 0;');
        writeln('if (navigator.appName == "Opera") {');
        writeln('MT = 13;');
        writeln('} else if (navigator.appName == "Netscape") {');
        writeln('MT = 18;');
        writeln('ML = -4;');
        writeln('}');
        writeln('</script>');
        writeln('</head>');
        writeln('<body bottommargin="0" leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0" background="../gifs/load.gif" scroll="no" onload="window.resizeTo(document.images[0].width + 10 + ML, document.images[0].height + 36 + MT);">');
        writeln('<img name="Foto" id="Foto" src=' + photoLink + '>');
        writeln('<script>');
        writeln('window.moveTo((screen.width - document.images["Foto"].width) / 2, (screen.height - document.images["Foto"].height) / 2);');
        writeln('document.title="' + titulo + '";');
        writeln('document.body.background = "";');
        writeln('</script>');
        writeln('</body>');
        writeln('</html>');
        close();
    }
}

function tamanhoLetra(tagId, tam) {
    document.getElementById(tagId).style.fontSize = tam;
}

function limitTextArea(field, limit) {
    if (limit - field.value.length < 0) field.value = field.value.slice(0, limit);
}

function validaLoginCliente(form) {
    if ("" == form.email.value || "" == form.password.value) {
        alert("Preencha correctamente o E-mail e a Password");
        return false;
    }

    return true;
}

function validaLoginAdmin(form) {
    if ("" == form.username.value || "" == form.password.value) {
        alert("Preencha correctamente Username e a Password");
        return false;
    }

    return true;
}

function validaRecuperarPassword(form) {
    if ("" == form.email.value) {
        alert("Preencha correctamente o E-mail e a Password");
        return false;
    }

    return true;
}

function validaRegistoCliente(form) {
	return true;
    if ("" == form.nome.value) {
        alert("Preencha correctamente o Nome");
        return false;
    }
    if ("" == form.morada.value) {
        alert("Preencha correctamente a Morada");
        return false;
    }
    if (isNaN(form.codigo_postal_1.value) || 4 != form.codigo_postal_1.value.length || isNaN(form.codigo_postal_2.value) || 3 != form.codigo_postal_2.value.length) {
        alert("Preencha correctamente o Código Postal");
        return false;
    }
    if ("" == form.localidade.value) {
        alert("Preencha correctamente a Localidade");
        return false;
    }
	if ("" != form.nif.value && isNaN(form.nif.value)) {
        alert("Preencha correctamente o Telefone");
        return false;
    }
    if ("" == form.telefone.value && "" == form.telemovel.value) {
        alert("Preencha correctamente o Telefone ou Telemóvel");
        return false;
    }
    if ("" != form.telefone.value && isNaN(form.telefone.value)) {
        alert("Preencha correctamente o Telefone");
        return false;
    }
    if ("" != form.telemovel.value && isNaN(form.telemovel.value)) {
        alert("Preencha correctamente o Telemóvel");
        return false;
    }
    if ("" == form.email.value) {
        alert("Preencha correctamente o E-mail");
        return false;
    }
    if ("" == form.password.value || form.password.value != form.password_2.value) {
        alert("Preencha correctamente a Password");
        return false;
    }

    return true;
}

function validaDadosPessoaisCliente(form) {
    if ("" == form.nome.value) {
        alert("Preencha correctamente o Nome");
        return false;
    }
    if ("" == form.morada.value) {
        alert("Preencha correctamente a Morada");
        return false;
    }
    if (isNaN(form.codigo_postal_1.value) || 4 != form.codigo_postal_1.value.length || isNaN(form.codigo_postal_2.value) || 3 != form.codigo_postal_2.value.length) {
        alert("Preencha correctamente o Código Postal");
        return false;
    }
    if ("" == form.localidade.value) {
        alert("Preencha correctamente a Localidade");
        return false;
    }
	if ("" != form.nif.value && isNaN(form.nif.value)) {
        alert("Preencha correctamente o Telefone");
        return false;
    }
    if ("" == form.telefone.value && "" == form.telemovel.value) {
        alert("Preencha correctamente o Telefone ou Telemóvel");
        return false;
    }
    if ("" != form.telefone.value && isNaN(form.telefone.value)) {
        alert("Preencha correctamente o Telefone");
        return false;
    }
    if ("" != form.telemovel.value && isNaN(form.telemovel.value)) {
        alert("Preencha correctamente o Telemóvel");
        return false;
    }
    if ("" == form.email.value) {
        alert("Preencha correctamente o E-mail");
        return false;
    }

    return true;
}

function changeQtCarrinho(idp, qt) {
    newQt = parseInt(eval("document.formCarrinhoComprasProdutos.idp_" + idp + ".value")) + qt;
    eval("document.formCarrinhoComprasProdutos.idp_" + idp + ".value = " + newQt);
    document.formCarrinhoComprasProdutos.idp.value = idp;
    document.formCarrinhoComprasProdutos.formCarrinhoComprasProdutos.value = "update";
    document.formCarrinhoComprasProdutos.submit();
}

function deleteCarrinhoProduto(idp) {
    document.formCarrinhoComprasProdutos.idp.value = idp;
    document.formCarrinhoComprasProdutos.formCarrinhoComprasProdutos.value = "delete";
    document.formCarrinhoComprasProdutos.submit();
}

function calculaDesconto(Preco, Desconto) {
	return (parseInt(Preco) - parseInt(Preco) * (parseInt(Desconto) / 100)).toFixed(2);
}
