/* Generic Aró site scripts */

// Used to trigger events when the page loads
// see http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}



// Attaches a javascript action to an object event
// eg: addEvent(window,"load",function(){ alert('hello!'); });
function addEvent(obj, type, fn) {
    if (obj.attachEvent) {
        obj['e' + type + fn] = fn;
        obj[type + fn] = function() { obj['e' + type + fn](window.event); }
        obj.attachEvent('on' + type, obj[type + fn]);
    } else
        obj.addEventListener(type, fn, false);
}
function removeEvent(obj, type, fn) {
    if (obj.detachEvent) {
        obj.detachEvent('on' + type, obj[type + fn]);
        obj[type + fn] = null;
    } else
        obj.removeEventListener(type, fn, false);
}



//Opens links with class="newWindow" in... a new window!
function newWindowSetup() {
    if (!document.getElementsByTagName) return false;
    var links = document.getElementsByTagName("a");
    for (var i = 0; i < links.length; i++) {
        if (links[i].className.match("newWindow")) {
            links[i].onclick = function() {
                window.open(this.href);
                return false;
            }
        }
    }
}
addLoadEvent(newWindowSetup);



// Parses email addresses to deter spambots, returns a mailto link
function linkEmailSetup() {
    if (!document.getElementsByTagName) return;
    var allElts = document.getElementsByTagName("*");
    if (allElts.length == 0 && document.all)
        allElts = document.all; //hack for IE5
    for (var i = 0; i < allElts.length; i++) {
        var elt = allElts[i];
        var className = elt.className || elt.getAttribute("class")
      || elt.getAttribute("className");
        if (className && className.match(/\belectronic-mail\b/)
        && elt.firstChild.nodeType == 3) {
            var addr = elt.firstChild.nodeValue;
            addr = addr.replace(/[ \[\{\(\|\/\\]at[ \]\}\)\|\/\\]/i, "@")
        .replace(/[ \[\{\(\|\/\\](dot|period)[ \]\}\)\|\/\\]/gi, ".");
            var lnk = document.createElement("a");
            lnk.setAttribute("href", "mailto:" + addr);
            lnk.appendChild(document.createTextNode(addr));
            elt.replaceChild(lnk, elt.firstChild);
        }
    }
}
addLoadEvent(linkEmailSetup);



// Popup window
function soopaPopSetup() {
    var a;
    for (var i = 0; (a = document.links[i]); i++) {
        if (a.target && a.target.indexOf("_soopaPop") == 0) {
            a.onclick = soopaPop;
        }
    }
}
function soopaPop() {
    var a = this.target.split(":");
    var sFeatures = a[1];
    window.open(this.href, a.length > 2 ? a[2] : String((new Date()).getTime()), sFeatures);
    return false;
    alert("hi")
}
addLoadEvent(soopaPopSetup);



// returns control of document element
function g(o) { return document.getElementById(o); }



// shows a div
function openDiv(obj) { g(obj).style.display = ''; }



// hides a div
function closeDiv(obj) { g(obj).style.display = 'none'; }



// toggles a div
function toggleDiv(obj) {
    var el = document.getElementById(obj);
    if (el.style.display != 'none') {
        el.style.display = 'none';
    }
    else {
        el.style.display = '';
    }

}

// accessible popup
function popUp(strURL, strType, strWidth, strHeight) {
    var strOptions = "";
    if (strType == "console") strOptions = "resizable,height=" + strHeight + ",width=" + strWidth;
    if (strType == "fixed") strOptions = "status,height=" + strHeight + ",width=" + strWidth;
    if (strType == "elastic") strOptions = "toolbar,menubar,scrollbars,resizable,location,height=" + strHeight + ",width=" + strWidth;
    window.open(strURL, 'newWin', strOptions);
}

// externalLinks - for "target=_blank like" action
function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
            anchor.target = "_blank";
        }
        if (anchor.getAttribute("class") == "extLink") {
            anchor.target = "_blank";
        }
    }
}


function bookmark(url, title) {
    if (window.sidebar) window.sidebar.addPanel(title, url, "");
    else if (window.opera && window.print) {
        var mbm = document.createElement('a');
        mbm.setAttribute('rel', 'sidebar');
        mbm.setAttribute('href', url);
        mbm.setAttribute('title', title);
        mbm.click();
    }
    else if (document.all) window.external.AddFavorite(url, title);
}


/*
* Clear Default Text: functions for clearing and replacing default text in
* <input> elements.
*/
addEvent(window, 'load', init, false);

function init() {
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];

        if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {
            /* Add event handlers */
            addEvent(theInput, 'focus', clearDefaultText, false);
            addEvent(theInput, 'blur', replaceDefaultText, false);

            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;

    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;

    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}

/* NU POPUPS MAR /APR 2008 */

function openCustomSignup() {
    OpenDialog('Newsletter sign-up', '/modules/dialogs/SignUp.aspx?url=' + window.location.href, 238, 340);
}

function openTellAFriend() {
    OpenDialog('Tell A Friend', '/modules/dialogs/TellAFriend.aspx?url=' + window.location.href, 250, 340);
}

function getcurrentTime() {
    Stamp = new Date(); var Hours; var tomenow; var Mins; var Time; Hours = Stamp.getHours();
    if (Hours >= 12) { Time = " PM"; } else { Time = " AM"; }
    if (Hours > 12) { Hours -= 12; }
    if (Hours == 0) { Hours = 12; }
    Mins = Stamp.getMinutes();
    if (Mins < 10) { Mins = "0" + Mins; }
    timenow = Hours + ":" + Mins + Time;
    return timenow
}

/* highslide alerts and highslide+ */
function openHislideWindow(link) { hs.htmlExpand(link, { objectType: 'iframe' }); }
function showValid(message) { var toppos = parseInt(g("tablePosId").style.top) + 35; var leftpos = parseInt(g("tablePosId").style.left) + 274; g('formValid').style.cssText = 'display:block;left:' + (leftpos) + 'px;top:' + (toppos) + 'px;'; g('formValid').innerHTML = message; }
function closeAlert() { try { g('formValid').style.cssText = 'display:none;' } catch (e) { } }


function setActiveStyleSheet(title) {
    //turn off flash
    document.cookie = "dorchester_dorchester_flash=false";

    var currLoc = document.location.toString();
    if (currLoc.indexOf("?") < 0) {
        window.location = document.location.toString() + "?print=true";
    }
    else {
        window.location = document.location.toString() + "&print=true";
    }
}