var pathToRoot = "";
var sbPageLoaded = false;
var soImageArray = new Array();

function navMouseOver(e) {
    if (window.event) e = window.event;
    var oElement = e.srcElement ? e.srcElement : e.target;
    if (oElement.className == "clsNavArrow" || oElement.className == "clsNavSpacer") {
        oElement.parentNode.childNodes[0].src = pathToRoot + "images/nav_arrow_magenta.gif";
    }
    else {
        var oChildNodes = oElement.childNodes;
        if (oChildNodes.length > 0 && oChildNodes[0].className == "clsNavArrow") {
            oChildNodes[0].src = pathToRoot + "images/nav_arrow_magenta.gif";
        }
    }
}

function navMouseOut(e) {
    if (window.event) e = window.event;
    var oElement = e.srcElement ? e.srcElement : e.target;
    var oTargetElement = e.toElement ? e.toElement : e.relatedTarget;
    if ((oElement.className == "clsNavArrow" || oElement.className == "clsNavSpacer") && oTargetElement != oElement.parentNode) {
        oElement.parentNode.childNodes[0].src = pathToRoot + "images/nav_arrow_grey.gif";
    }
    else {
        var oChildNodes = oElement.childNodes;
        if (oChildNodes.length > 0 && oChildNodes[0].className == "clsNavArrow") {
            var bFound = false;
            for (var i = 0; i < oChildNodes.length; ++i) {
                if (oChildNodes[i] == oTargetElement) {
                    bFound = true;
                    break;
                }
            }
            if (!bFound) {
                oChildNodes[0].src = pathToRoot + "images/nav_arrow_grey.gif";
            }
        }
    }
}

function textSizerMouseOver(e) {
    if (window.event) e = window.event;
    var oElement = e.srcElement ? e.srcElement : e.target;
    if (oElement.id == "idTextPlusSize") {
        oElement.src = pathToRoot + "images/icon_text_plus_over.gif";
    }
    else if (oElement.id == "idTextMinusSize") {
        oElement.src = pathToRoot + "images/icon_text_minus_over.gif";
    }
}

function textSizerMouseOut(e) {
    if (window.event) e = window.event;
    var oElement = e.srcElement ? e.srcElement : e.target;
    if (oElement.id == "idTextPlusSize") {
        oElement.src = pathToRoot + "images/icon_text_plus.gif";
    }
    else if (oElement.id == "idTextMinusSize") {
        oElement.src = pathToRoot + "images/icon_text_minus.gif";
    }
}

function setupImageMouseOvers() {
    var oTemp = document.getElementById("idNav");
    if (oTemp != null) {
        oTemp.onmouseover = navMouseOver;
        oTemp.onmouseout = navMouseOut;

        soImageArray[0] = new Image();
        soImageArray[0].src = pathToRoot + "images/nav_arrow_grey.gif";
        soImageArray[1] = new Image();
        soImageArray[1].src = pathToRoot + "images/nav_arrow_magenta.gif";
    }

    oTemp = document.getElementById("idTextPlusSize");
    if (oTemp != null) {
        oTemp.onmouseover = textSizerMouseOver;
        oTemp.onmouseout = textSizerMouseOut;
    }

    oTemp = document.getElementById("idTextMinusSize");
    if (oTemp != null) {
        oTemp.onmouseover = textSizerMouseOver;
        oTemp.onmouseout = textSizerMouseOut;
    }
}

function setupLinkClicks() {
    var oTemp = document.getElementById("idButton10");
    if (oTemp != null) {
        oTemp.onclick = function () { printThisPage(); return false; };
    }
}

function highlightCurrentHeaderFooter() {
    var sURL = window.location.href;
    if (sURL.indexOf("important_safety_information.html") >= 0) {
        document.getElementById("idImportantSafetyInformation").className = "clsFooterSelected";
    }
    else if (sURL.indexOf("sitemap.html") >= 0) {
        document.getElementById("idSitemap").className = "clsFooterSelected";
        document.getElementById("idSitemap2").className = "clsHeaderSelected";
    }
    else if (sURL.indexOf("contact_us.html") >= 0) {
        document.getElementById("idContactUs").className = "clsFooterSelected";
        document.getElementById("idContactUs2").className = "clsHeaderSelected";
    }
    else if (sURL.indexOf("terms_of_use.html") >= 0) {
        document.getElementById("idTermsOfUse").className = "clsFooterSelected";
    }
    else if (sURL.indexOf("medicare_part_d.html") >= 0) {
        document.getElementById("idMedicarePartD").className = "clsFooterSelected";
    }
}

var ssIndexCurrent = "1";
var sbAnimating = false;

function selectTab(sIndexNew) {
    if (sbPageLoaded) {
        if (!sbAnimating) {
            sbAnimating = true;
            var $oTemp = jQuery('#idTab' + ssIndexCurrent)
            $oTemp.removeClass("clsTabCurrent");
            $oTemp.addClass("clsTab");
            $oTemp = jQuery('#idTab' + sIndexNew)
            $oTemp.removeClass("clsTab");
            $oTemp.addClass("clsTabCurrent");
            jQuery('#idTabSection' + ssIndexCurrent).eq(0).fadeOut('slow', function () {
                jQuery('#idTabSection' + sIndexNew).eq(0).fadeIn('slow', function () { sbAnimating = false; });
            });
            ssIndexCurrent = sIndexNew;
        }
    }

    return false;
}

function adjustForMacOSX() {
    if (navigator.appVersion.indexOf("Mac") != -1) {
        var oHead = document.getElementsByTagName("head")[0];
        var oCssNode = document.createElement("link");
        oCssNode.type = "text/css";
        oCssNode.rel = "stylesheet";
        oCssNode.href = "css/mac.css";
        oCssNode.media = "screen";
        oHead.appendChild(oCssNode);
    }
}

/*
** Sitewide Functions
*/

/*
** Configurable Directives
*/
var scripts = new Array('layerMaker', 'swfobject', 'expander', 'didYouKnow', 'textSizer', 'utilities');

/*
** Script Auto-loader, takes array of filenames
*/
function dynLoadScript(scriptArr) {
    // Find the relative path to our javascript includes
    handler = document.getElementsByTagName("head")[0].getElementsByTagName("script");
    for (var x = 0; x < handler.length; x++) {
        if (handler[x].getAttribute("src").match(/functions.js/)) {
            scriptPrefix = handler[x].getAttribute("src").replace(/functions.js/, "");
            pathToRoot = scriptPrefix.replace(/javascript/, "").replace(/\/\//, "/");
            if (pathToRoot == "/") {
                pathToRoot = "";
            }
        }
    }
    var a = new Array();
    for (var x = 0; x < scriptArr.length; x++) {
        // safari fails on dynamic loading, brute force
        document.write('<script type="text/javascript" src="' + scriptPrefix + scriptArr[x] + '.js"></script>');
    }
    return false;
}
dynLoadScript(scripts);

/*
** Basic site-wide and site-specific functionality
*/
function initSite() {
    setupImageMouseOvers();
    setupLinkClicks();
    highlightCurrentHeaderFooter();
    adjustForMacOSX();

    // Loading external file functions next
    initUtilitiesArea();
    validateForm.prep();
    layerMaker.init();
    didYouKnow.init();
    initExpander();
    initRedirLink();
    setTimeout("", 2000);
    if (getCookie('sentThisPage') == "yes") {
        layerMaker.activate('Email This Page Confirmation', '');
        document.cookie = "sentThisPage=no";
    }
    if (getCookie('isHCP') == "yes") {
        layerSets['HCP Disclaimer']['isActive'] == false;
    }

    sbPageLoaded = true;
}

function initRedirLink() {
    if (document.getElementById('dykGoBack')) {
        url = getCookie('dykOrigUrl');
        document.getElementById('dykGoBack').href = url;
        document.getElementById('dykGoBack').onclick = "";
    }
}


function hcpDisclaimerConfirmed() {
    document.cookie = "isHCP=yes";
}

function initHCPDisclaimer() {
    sandbox = document.getElementsByTagName("a");
    for (var x = 0; x < sandbox.length; x++) {
        if (sandbox[x].href.match(/hcp/) && !window.location.href.match(/hcp/)) {
            sandbox[x].onclick = function () { layerMaker.activate('HCP Disclaimer', this); return false; };
        }
    }
    return false;
}

var currBounding1 = 0;
var currBounding2 = 0;
var currBounding3 = 0;
var currBounding4 = 0;
var currBounding5 = 0;

function textSizerCalls(myOper) {
    currBounding1 = textSizer(myOper, '1', 'px', '1', 'idText', currBounding1);
    currBounding2 = textSizer(myOper, '1', 'px', '1', 'idTouts', currBounding2);
    currBounding3 = textSizer(myOper, '1', 'px', '1', 'idTopOfPage', currBounding3);
    currBounding4 = textSizer(myOper, '1', 'px', '1', 'idTopNav', currBounding4);
    currBounding5 = textSizer(myOper, '1', 'px', '1', 'idFooterText', currBounding5);
    for (var nIndex = 1; nIndex <= 10; ++nIndex) {
        var oElement = document.getElementById("idButton" + nIndex);
        if (oElement != null) {
            oElement.style.fontSize = "11px";
            oElement.style.lineHeight = "31px";
        }
    }
    document.getElementById("idLogo").style.fontSize = "12px";
}

function initUtilitiesArea() {
    if (document.getElementById('siteUtilitiesArea')) {
        parentObj = document.getElementById('siteUtilitiesArea');
        parentObj.getElementsByTagName("a")[0].onclick = function () { _taq.gaTrackEvent('Site Action', 'Email This Page', window.location.pathname); layerMaker.activate('Email This Page', this); return false; };
        parentObj.getElementsByTagName("a")[1].onclick = function () { printThisPage(); return false; };
        parentObj.getElementsByTagName("a")[2].onclick = function () { textSizerCalls('add'); return false; };
        parentObj.getElementsByTagName("a")[3].onclick = function () { textSizerCalls('subtract'); return false; };
    }
    return false;
}

function submitEmailThisPage() {
    document.getElementById('emailThisPageSuccess').value = window.location.href;
    if (validateForm.initEmail(document.forms[document.forms.length - 1])) {
        _taq.gaTrackGoal("Email This Page", "Completed");
        document.forms[document.forms.length - 1].submit();
    } else {
        return false;
    }
}

function addFriendInput() {
    // Find relative path depth to determine location of images folder
    pathDepth = 0;
    hcpSide = false;
    var strHref = window.location.href;
    var handler = strHref.split('/');
    for (var i = 0; i < handler.length; i++) {
        if (handler[i] == "info") {
            pathDepth = 1;
        }
        if (handler[i] == "about_azor" || handler[i] == "managing_hypertension" || handler[i] == "patient_resources" || handler[i] == "utilities" || handler[i] == "professional_resources") {
            pathDepth = 2;
        }
        if (handler[i] == "hcp") {
            hcpSide = true;
        }
    }
    if (hcpSide == true) {
        pathDepth++;
    }

    // Determine how many friend inputs currently exist
    counter = 1;
    for (var x = 0; x < counter; x++) {
        if (document.getElementById('recipient' + counter)) {
            counter++;
        }
    }

    // If friend inputs number less than our limit AND first friend email is not blank, construct new input field
    if (counter <= 5) {
        if (counter == 5) {
            document.getElementById('idAddMoreFriendLink').style.display = "none";
        }
        document.getElementById('recipient1').innerHTML = "* Recipient's email address 1";
        myDiv = document.getElementById('extraEmailInputs');

        var objFormListArea = document.createElement("div");
        // objFormListArea.setAttribute('class','formlistArea4');
        objFormListArea.style.clear = "both";
        objFormListArea.style.margin = "0px 0px 10px 18px";
        objFormListArea.style.height = "20px";
        objFormListArea.style.width = "430px";
        myDiv.appendChild(objFormListArea);

        var objInputLabel = document.createElement("span");
        objInputLabel.setAttribute('id', 'recipient' + counter);
        objInputLabel.innerHTML = "&nbsp;&nbsp;Recipient's email address " + counter;
        objInputLabel.style.float = "left";
        if (!Prototype.Browser.WebKit) {
            objInputLabel.style.margin = "0px 10px 0px 0px";
        }
        objInputLabel.style.width = "170px";
        objInputLabel.style.display = "inline";
        objInputLabel.style.fontFamily = "Arial,Sans-serif";
        objInputLabel.style.color = "#FFFFFF";
        objInputLabel.style.fontSize = "12px";
        objInputLabel.style.lineHeight = "18px";
        objInputLabel.style.height = "18px";
        objFormListArea.appendChild(objInputLabel);

        var objInputArea = document.createElement("input");
        objInputArea.setAttribute('type', 'test');
        objInputArea.setAttribute('id', 'toAddress' + counter);
        //objInputArea.setAttribute('name', 'toAddress'+counter);
        objInputArea.setAttribute('name', 'toAddress');
        // objInputArea.setAttribute('class', 'formTextInputArea2');
        objInputArea.style.float = "left";
        objInputArea.style.display = "inline";
        objInputArea.style.margin = "0px 3px 0px 10px";
        objInputArea.style.padding = "2px 2px 1px 10px";
        objInputArea.style.height = "17px";
        objInputArea.style.width = "170px";
        if (pathDepth == 0) {
            objInputArea.style.background = "url(images/bg_input_form.gif) 0px 0px no-repeat";
        } else if (pathDepth == 1) {
            objInputArea.style.background = "url(../images/bg_input_form.gif) 0px 0px no-repeat";
        } else if (pathDepth == 2) {
            objInputArea.style.background = "url(../../images/bg_input_form.gif) 0px 0px no-repeat";
        }
        objInputArea.style.backgroundColor = "#FFFFFF";
        objInputArea.style.border = "1px solid #C3C3C3";
        objInputArea.style.fontFamily = "Arial,Sans-serif";
        objInputArea.style.color = "#000000";
        objInputArea.style.fontSize = "11px";
        objInputArea.style.lineHeight = "11px";
        objFormListArea.appendChild(objInputArea);

        document.getElementById("extraEmailInputs").style.display = "block";
        var clearBothDiv = document.createElement("div");
        // clearBothDiv.setAttribute('class','clearBoth');
        clearBothDiv.style.clear = "both";
        myDiv.appendChild(clearBothDiv);
    }
}

function initYearHelper() {
    if (document.getElementById('yearHelperInfo')) {
        workingObj = document.getElementById('yearHelperInfo');
        workingObj.href = "javascript:void(0);";
        workingObj.onclick = function () { layerMaker.activate('Why year of birth tooltip', this) };
    }
    return false;
}

function initLeaveSite() {
    sandbox = document.getElementsByTagName("a");
    for (var x = 0; x < sandbox.length; x++) {
        currentLinkIsExt = 0;
        currentHref = sandbox[x].href;
        if (currentHref.match(/http:\/\//) || currentHref.match(/https:\/\//)) {
            if (leavingSiteExcludeSites) {
                for (var i = 0; i < leavingSiteExcludeSites.length; i++) {
                    if (currentHref.search(leavingSiteExcludeSites[i]) > -1) {
                        currentLinkIsExt += 1;
                    }
                }
            }
            if (currentHref.match("\\b" + currentUrl.domainName() + "\\b")) {
                currentLinkIsExt += 1;
            }
            if (currentLinkIsExt == 0) {
                sandbox[x].onclick = function () { layerMaker.activate('Leaving the Site', this); return false; };
            }
        }
    }
    return false;
}

function initLeaveToDSI() {
    sandbox = document.getElementsByTagName("a");
    for (var x = 0; x < sandbox.length; x++) {
        if (sandbox[x].href.indexOf("dsi.com/prescribing_information-portlet/getDocument?product=TR&inline=true") >= 0) {
            sandbox[x].onclick = function () { return true; };
        }
        else if (sandbox[x].href.match(/sankyopharma.com/) || sandbox[x].href.match(/www.dsus.com/) || sandbox[x].href.match(/www.dsi.com/)) {
            sandbox[x].onclick = function () { layerMaker.activate('External DSI Disclaimer', this); return false; };
        }
    }
    return false;
}

// Mozilla and Opera
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", initSite, false);
} else {
    if (window.onload) {
        oldOnloader = window.onload;
        window.onload = function () { oldOnloader(); window.onload; return false; };
    } else {
        window.onload = function () { initSite(); return false; };
    }
}

