﻿var ajaxObjects = new Array();
var ajaxObjectsDelayProtect = new Array();

function trim(str) {
    return str.replace(/^\s*|\s*$/g, "");
}

var is = new function() {
    this.VER = navigator.appVersion
    this.AGENT = navigator.userAgent
    this.DOM = document.getElementById ? 1 : 0
    this.IE = (document.all && !this.DOM) ? 1 : 0;
    this.MAC = this.AGENT.indexOf("Mac") > -1
    this.NS6 = document.getElementById && !document.all ? 1 : 0
    this.NS4 = (document.layers && !this.DOM) ? 1 : 0;
    this.OPERA = this.AGENT.indexOf('Opera') > -1
    return this
}

function getElement(id) {
    if (is.DOM) return document.getElementById(id)
    else
        if (is.IE) return eval("document.all." + id)
    else
        if (is.NS) return eval("document." + id)
}

function bookmarksite(title, url) {
    if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    } else if (window.opera && window.print) { // opera
        var elem = document.createElement('a');
        elem.setAttribute('href', url);
        elem.setAttribute('title', title);
        elem.setAttribute('rel', 'sidebar');
        elem.click();
    } else if (document.all) {
        window.external.AddFavorite(url, title);
    }
}

function add_bookmark(url, imovel) {
    var browsName = navigator.appName;
    if (browsName == "Microsoft Internet Explorer") {
        window.external.AddFavorite(url, imovel);
    } else if (browsName == "Netscape") {
        alert("\nPara adicionar esta página aos seus Favoritos pressione CTRL+D");
    }
}

function CheckIsIE() {
    if (navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER') { return true; }
    else { return false; }
}

function email_valido(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (reg.test(email) == false) {
        return false;
    } else {
        return true;
    }
}

function regista_newsletter() {
    var msg = "";
    if (!email_valido(document.getElementById('email2').value))
        msg += 'Campo \'E-mail\' é obrigatorio. Preencha-o correctamente.\n';

    //mostra msg de erro caso existam
    if (msg.length != 0) {
        alert("Erro(s)\n" + msg);
        return (false);
    } else {
        //senao envia o formulário
        return (true);
    }
}

function valida_form_amigo() {
    var msg = "";

    if (document.getElementById('recomenda_nome').value.length < 4)
        msg += 'Campo \'O seu Nome\' é obrigatorio. (minimo 4 caracteres.)\n';

    if (document.getElementById('recomenda_amigo_nome').value.length < 4)
        msg += 'Campo \'Nome do Amigo\' é obrigatorio. (minimo 4 caracteres.)\n';


    if (!email_valido(document.getElementById('recomenda_amigo_email').value))
        msg += 'Campo \'E-mail do Amigo\' é obrigatorio. Preencha-o correctamente.\n';

    //mostra msg de erro caso existam
    if (msg.length != 0) {
        alert("Erro(s)\n" + msg);
        return (false);
    } else {
        //senao envia o formulário
        return (true);
    }
}

function valida_form_info() {
    var msg = "";

    if (document.getElementById('info_nome').value.length < 4)
        msg += 'Campo \'O seu Nome\' é obrigatorio. (minimo 4 caracteres.)\n';

    if (document.getElementById('info_telef').value.length < 9)
        msg += 'Campo \'Nº Telefone\' é obrigatorio. (minimo 9 caracteres.)\n';

    //mostra msg de erro caso existam
    if (msg.length != 0) {
        alert("Erro(s)\n" + msg);
        return (false);
    } else {
        //senao envia o formulário
        return (true);
    }
}
// ************ ini end *****************
function activa_tab(obj) {
    try {
        if (document.getElementById) {
            if (document.getElementById('tab1').className == "on") document.getElementById('tab1').className = "off";
            if (document.getElementById('tab2').className == "on") document.getElementById('tab2').className = "off";
            if (document.getElementById('tab3').className == "on") document.getElementById('tab3').className = "off";
            if (document.getElementById('tab4').className == "on") document.getElementById('tab4').className = "off";
            if (document.getElementById('tab5').className == "on") document.getElementById('tab5').className = "off";
            if (document.getElementById('tab6').className == "on") document.getElementById('tab6').className = "off";
            document.getElementById('showtab1').className = "hide_on";
            document.getElementById('showtab2').className = "hide_on";
            document.getElementById('showtab3').className = "hide_on";
            document.getElementById('showtab4').className = "hide_on";
            document.getElementById('showtab5').className = "hide_on";
            document.getElementById('showtab6').className = "hide_on";
            document.getElementById(obj).className = "on";
            document.getElementById('show' + obj).className = "hide_off";
        }
    }
    catch (e) {
        //nada
    }
}

function activa_tab_noticias(obj) {
    try {
        if (document.getElementById) {

            document.getElementById('default_block_noticias_tab1').className = "default_block_noticias_tab";
            document.getElementById('default_block_noticias_tab2').className = "default_block_noticias_tab";

            document.getElementById('show_default_block_noticias_tab1').className = "hide_on";
            document.getElementById('show_default_block_noticias_tab2').className = "hide_on";
            
            document.getElementById(obj).className = "default_block_noticias_tab_sel";
            document.getElementById('show_' + obj).className = "hide_off";
        }
    }
    catch (e) {
        //nada
    }
}
// ************ tabs end *****************


// ************ mostra_esconde end *****************
function mostra_esconde(idx) {
    var obj = document.getElementById(idx)
    //alert(obj.className);
    if (obj.className == 'hide_off') {
        obj.className = 'hide_on';
    } else {
        obj.className = 'hide_off';
    }
}

function set_style(idx, stl) {
    var obj = document.getElementById(idx)
    //alert(obj.className);
    obj.className = stl;
}

function showhide2(id) {
    obj = getElement(id);
    viz = obj.style.display;
    if (viz == 'block') {
        obj.style.display = "none";
    } else {
        obj.style.display = "block";
    }
}

function ShowHide(id, visibility) {
    obj = document.getElementsByTagName("div");
    //visibility =  obj[id].style.visibility;
    if (visibility == 'visible') {
        obj[id].style.display = 'block';
    } else {
        obj[id].style.display = 'none';
    }
}

// ************ mostra_esconde end *****************

function Imprimir() {
    var a = window.open('', '', 'scrollbars=yes,width=720,height=500');
    a.document.open("text/html");
    a.document.write('<html>\n<head>\n<link rel="stylesheet" href="/css/main.css" media="all" type="text/css" />\n<style>\n.noprint{display:none}\n<\/style>\n</head>\n<body style="background-image:none;background-color:#FFFFFF;">\n');
    a.document.write('<table align="center" width="700" border="0" cellpadding="10" cellspacing="0">\n<tr>\n<td align="left">\n<img align="absmiddle" src="/images/banner_impressao.jpg" border="0" alt="" /><br/><br/>');
    a.document.write('<tr>\n<td>\n');
    a.document.write(document.getElementById('print').innerHTML);
    a.document.write('\n<\/td>\n<\/tr>\n');
    a.document.write('<\/table>\n');
    a.document.write('<\/body>\n<\/html>');
    a.document.close();
    a.print();
}


//***************** AJAX **********************


function get_url(url,divs){
	var divx = getElement(divs);
	getContent(url,divx,'x=1','');
}

function getContent(url,divobj,fnc_completo,params){
	try {
	pageTracker._trackPageview(url);
	} catch(err) {}
	var ajaxIndex = ajaxObjects.length;
	ajaxObjectsDelayProtect[url] = ajaxIndex;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url;
	ajaxObjects[ajaxIndex].encodeURIString = false;
	ajaxObjects[ajaxIndex].onCompletion = function() {showContent(divobj,ajaxIndex,url,fnc_completo);};
	ajaxObjects[ajaxIndex].runAJAX(params);
	showWaitMessage(divobj);
} 

function showWaitMessage(divobj){
	divobj.innerHTML = '<br /><img align="top" src="/images/ajax-loader.gif" alt="A enviar dados..." />';
}

function showContent(divobj,index,url,fnc_completo){
	if (ajaxObjectsDelayProtect[url] == index){divobj.innerHTML = ajaxObjects[index].response;	}
	if (fnc_completo)eval(fnc_completo);
}

function send_contactos() {
    if (valida_rform1()) {
        var nome = Xencode(getElement('nome').value);
        var email = Xencode(getElement('email').value);
        var assunto = Xencode(getElement('assunto').value);
        var msg = Xencode(getElement('msg').value);
        var key = Xencode(getElement('secure').value);
        getContent('/conteudos/contactos_post.htm', getElement('formulario_contactos'), 'x=1', '&nome=' + nome + '&email=' + email + '&assunto=' + assunto + '&key=' + key + '&msg=' + msg);
    }
}


function mostra_submenu(objx) {
    var divx = getElement("submenu_" + objx.id);
    divx.className = "submnu_topo_active";
    posini = findPos(objx);
    divx.style.left = (findPosX(objx)) + "px";
    divx.style.top = (findPosY(objx) + objx.offsetHeight + 3) + "px";

}

function esconde_submenu(divx) {
    divx.className = "submnu_topo";
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft, curtop];
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}
