// #################################################################
// ## window.onload calling functions that initialize page
// #################################################################

window.onload = function() {
    setup_frameEscape();
    if (typeof prepmenus != 'undefined') { prepmenus(); } // init sun.com flyout menus
    setup_links();
    setup_autoField("searchfield","Search");
}



// #################################################################
// ## functions named setup_* get called from window.onload
// #################################################################

function setup_links() {
    // scan all links on the page
    for (var a=0; a<document.links.length; a++) {
        if (a>10000) { break; } // don't hang on really long pages

        // make Omniture capture patch download links as custom links
        if (document.links[a].href.indexOf("/pdownload.pl?")!=-1
        && typeof s_co!='undefined') {
            document.links[a].onclick = function() {
                s_linkType='d';
                s_linkName=this.href;
                s_lnk=s_co(this);
                s_gs(s_account);
            }
        }

        // make specially tagged links become popups
        else if (hasClassName(document.links[a],"popup")) {
            document.links[a].onclick = function(e) {
                var matchArr = this.className.match(/\b(\d+)x(\d+)\b/i);
                var href = this.href;
                var targ = ((this.target) ? this.target : "popup");
                var atts = "width=600,height=400,left=0,top=0,resizable,scrollbars";
                var matchArr = this.className.match(/\b(\d+)x(\d+)\b/i);
                if (matchArr) {
                    var x = parseInt(matchArr[1]);
                    var y = parseInt(matchArr[2]);
                    if (document.body.mergeAttributes) {
                        x+=16;y+=4;
                    }
                    var atts = "width="+x+",height="+y+",left=0,top=0,resizable,scrollbars";
                }
                var n = window.open(href,targ,atts);
                try{n.focus();}catch(err){}
                return false;
            }
        }
    } // end loop
}

function setup_autoField(id, text) {
    // create form field text that dissapears on focus
    if (typeof document.getElementById == 'undefined') { return; }
    var field = document.getElementById(id);
    if (!field) { return; }
    field.onfocus = function() {
        if (this.value == text) { this.value = ''; }
    }
    field.onblur = function() {
        if (!this.value) {
            this.value = text;
        }
    }
}

function setup_frameEscape() {
    if (top.frames.length > 1) { top.location.href = location.href; }
}

function openClusterwindow()
{
        window.open("/html/cluster_bundle.html", "cluster","menubar=1,resizable=1,scrollbars=1,width=550,height=650");
}


// #################################################################
// ## utility functions
// #################################################################

function removeClassName(element, className) {
    if (!element.className) { return false; }
    if (!hasClassName(element,className)) { return false; }
    var classNames = element.className.split(' ');
    var newClassNames = new Array();
    for (var a=0; a<classNames.length; a++) {
        if (classNames[a]!=className) {
            newClassNames[newClassNames.length] = classNames[a];
        }
    }
    element.className=newClassNames.join(' ');
    return true;
}

function addClassName(element, className) {
    if (hasClassName(element, className)) { return false; }
    if (!element.className) { element.className = className; }
    else { element.className += ' '+className; }
    return true;
}

function hasClassName(element, className) {
    var exp = new RegExp("\\b"+className+"\\b");
    return (element.className && exp.exec(element.className))?true:false;
}

function toggleClassName(element, className) {
    if (hasClassName(element, className)) { removeClassName(element, className); }
    else { addClassName(element, className); }
}

if (typeof encodeURIComponent == 'undefined') {
    // alias these functions if they don't exist
    encodeURIComponent = escape;
    decodeURIComponent = unescape;
}

function popPage(url, w, h) {
    mywin = window.open(url, "myNewWin", "width="+w+",height="+h+",menubar=no,toolbar=no,scrollbars=yes,resizable=yes");
    mywin.focus();
}

function doClick(text) {
    alert('You will be notified when document '+text+' is updated.');
}

function subscribe2Coll(coll) {
    alert('You will be notified when new documents are added to the '+coll+' collection.');
}

function checkPasswdHiding() {
    if ((document.editContractUserForm.password2.value != "") &&
    (document.editContractUserForm.password2.value ==
    document.editContractUserForm.password1.value)) {
        var question = "Do you want the changed password to be\nincluded in your change confirmation?\n";
        var instructions = "Click OK for yes, Cancel for no.\n";
        if (confirm( question + instructions )) {
            document.editContractUserForm.showpass.value = "yes";
        } else {
            document.editContractUserForm.showpass.value = "no";
        }
        var line1 = "Your password is changing. After you click 'Submit Account Info',\n";
        var line2 = "your browser may require you to re-authenticate yourself with\n"
        var line3 = "your new password.";
        alert( line1 + line2 + line3 );
    }
}

function openSearchWindow() {
    var summary=document.srform.SUMMARY.value;
    summary=summary.replace(/\?/g,"");
    summary=summary.replace(/\!/g,"");
    var z32="";
    var z32Delim="\%20";
    var searchWords=summary.split(" ");
    for (var i=0; i<searchWords.length; i++) {
        var word=searchWords[i];
        word=word.toLowerCase();
        if ((word=="i") || (word=="me") || (word=="my") ||
        (word=="our") || (word=="us") || (word=="we") ||
        (word=="you") || (word=="your")) {
            continue;
        }
        if (z32.length > 0) {
            z32+=z32Delim;
        }
        z32+=word;
    }
    var searchURL="search.pl?mode=results&so=coll";
    searchURL+="&suncourier=yes&zone_32=";
    searchURL+=z32;
    var searchWin=window.open(searchURL, "searchWindow");
}

