function hookupProfile() {

    var customizeForm = document.getElementById("customizeForm");

    if (customizeForm && customizeForm.className == "customize") {

        var radioButtons = customizeForm.getElementsByTagName("input");

        for (var i = 0; i < radioButtons.length; i++) {
            var radioButton = radioButtons.item(i);
            if (radioButton.type == "radio") {
                radioButton.onclick = previewTheme;
            }
        }

    }

}

function hotnessify(e, options) {

    var hotnessStyle = options && options.style ? options.style : "full";
    var color = options && options.color ? options.color : "transparent";

    var postfix;
    if( color == "white" ){
        postfix = "w";
    } else if( color == "blue"){
        postfix = "b";
    } else {
        postfix = "";
    }

    // CSS3, I beg for thee.

    var baseElements = document.getElementsByTagName("base");
    var imageBase;
    if (baseElements && baseElements.length == 1) {
        imageBase = baseElements.item(0).href + "images/";
    } else {
        imageBase = "images/";
    }

    var container = newElement("table", "container " + hotnessStyle);
    if (e.id) {
        container.id = e.id + "Wrapped";
    }

    // Using the returned value makes Opera happier for some reason.
    var replaced = e.parentNode.replaceChild(container, e);

    var body = container.appendChild(newElement("tbody"));

    if (hotnessStyle == "header" || hotnessStyle == "full") {

        var topRow = body.appendChild(newElement("tr", "topRow row"));

        var topLeft = topRow.appendChild(newElement("td", "boxTopLeft corner"));
        applyBackground(topLeft, imageBase + "C_cornerTL" + postfix + ".png");

        var top = topRow.appendChild(newElement("td", "boxTop edge"));
        applyBackground(top, imageBase + "C_T" + postfix + ".png");

        var topRight = topRow.appendChild(newElement("td", "boxTopRight corner"));
        applyBackground(topRight, imageBase + "C_cornerTR" + postfix + ".png");
    }

    var contentRow = body.appendChild(newElement("tr", "contentRow row"));

    var centerLeftImage, centerImage, legacyCenterImage, centerRightImage;
    if (hotnessStyle == "header") {
        centerLeftImage = "C_LH" + postfix + ".png";
        centerImage = "C_Center50x39" + postfix + ".png";
        legacyCenterImage = "C_Center50x39_IE6" + postfix + ".png";
        centerRightImage = "C_RH" + postfix + ".png";
    } else {
        centerLeftImage = "C_L" + postfix + ".png";
        centerImage = "C_Center50x50" + postfix + ".png";
        legacyCenterImage = "C_Center_IE6" + postfix + ".png";
        centerRightImage = "C_R" + postfix + ".png";
    }

    var centerLeft = contentRow.appendChild(newElement("td", "boxLeft edge"));
    applyBackground(centerLeft, imageBase + centerLeftImage);

    var center = contentRow.appendChild(newElement("td", "boxContent"));
    if (color == "white" && hotnessStyle != "header") {
        center.style.backgroundColor = "rgb(254, 254, 254)";
    } else {
        applyBackground(center, imageBase + centerImage, imageBase + legacyCenterImage);
    }
    center.appendChild(replaced);

    var centerRight = contentRow.appendChild(newElement("td", "boxRight edge"));
    applyBackground(centerRight, imageBase + centerRightImage);

    if (hotnessStyle == "body" || hotnessStyle == "full") {
        var bottomRow = body.appendChild(newElement("tr", "bottomRow row"));

        var bottomLeft = bottomRow.appendChild(newElement("td", "boxBottomLeft corner"));
        applyBackground(bottomLeft, imageBase + "C_cornerBL" + postfix + ".png");

        var bottom = bottomRow.appendChild(newElement("td", "boxBottom edge"));
        applyBackground(bottom, imageBase + "C_B" + postfix + ".png");

        var bottomRight = bottomRow.appendChild(newElement("td", "boxBottomRight corner"));
        applyBackground(bottomRight, imageBase + "C_cornerBR" + postfix + ".png");
    }
}

function applyBackground(element, imageUrl, legacyImageUrl) {
    // If IE 5.5 or 6.0 in non-scaled mode, use DX hack.
    if (document.all &&
        !window.opera &&
        screen.deviceXDPI == screen.logicalYDPI &&
        (navigator.userAgent.indexOf("MSIE 6") >= 0 ||
         navigator.userAgent.indexOf("MSIE 5.5") >= 0)) {
        element.runtimeStyle.filter =
            "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" +
            (legacyImageUrl ? legacyImageUrl : imageUrl) + "', sizingMethod='scale')";
    } else {
        element.style.backgroundImage = "url(" + imageUrl + ")";
    }
}

function newElement(elementName, className) {
    var element = document.createElement(elementName);
    if (className) {
        element.className = className;
    }
    return element;
}

function previewTheme(event) {

    var e = event ? event : window.event;
    var src = e.currentTarget ? e.currentTarget : e.srcElement;
    if (src.id.substring(0, 5) == "color") {
        var color = src.nextSibling.style.backgroundColor;
        document.body.style.backgroundColor = color;
        document.body.style.backgroundImage = "none";
    } else if (src.id == "theme.default") {
        document.body.style.backgroundColor = "white";
        document.body.style.backgroundImage = "none";
    } else {
        document.body.style.backgroundColor = "white";
        document.body.style.backgroundImage =
            "url(images/BG_" + src.id.substring(6) + "pattern.png)";
    }

}

function checkHeader(element) {
    //all links except Request an Account links should go to sign in
    //when logged out
    return !(element.href.indexOf("SignUp.aspx") >= 0); 
}
