﻿/* Checkbox
************************************************************************************/
//<div class="checkbox-container">
//    <asp:Label ID="labEmail" CssClass="label" runat="server">Email</asp:Label>
//    <asp:CheckBox ID="cboEmail" CssClass="checkbox" runat="server" />
//    <div class="checkbox"></div>
//</div>

var _checkboxes = new Array();
function checkbox(element)
{
    var _this = this;
    _this.element = element;
    _this.label = element.childNodes[0];
    _this.checkbox = element.childNodes[1].childNodes[0];
    _this.graphicCheckbox = element.childNodes[2];
    _this.checked = _this.value(false);
    _this.click = null;
    attachEventHandler(window, "load", function () { _this.initialise(); });
}
checkbox.prototype.initialise = function ()
{
    var _this = this;
    _this.graphicCheckbox.style.display = 'block';

    if (_this.checkbox.checked || _this.graphicCheckbox.className == "checkbox checkbox-checked")
    {
        _this.graphicCheckbox.className = "checkbox checkbox-checked";
        _this.checked = true;
    }

    _this.graphicCheckbox.onclick = function () { _this.toggle(); if (_this.click) _this.click(); };
    _this.graphicCheckbox.onmouseover = function () { _this.highlight(); };
    _this.graphicCheckbox.onmouseout = function () { _this.unhighlight(); };

    _this.label.onclick = function () { _this.toggle(); if (_this.click) _this.click(); };
    _this.label.onmouseover = function () { _this.highlight(); };
    _this.label.onmouseout = function () { _this.unhighlight(); };
}
checkbox.prototype.toggle = function ()
{
    var _this = this;
    if (_this.checked)
        _this.uncheck();
    else
        _this.check();
}
checkbox.prototype.check = function ()
{
    var _this = this;
    _this.checked = true;
    if (_this.graphicCheckbox.className.indexOf("checkbox-checked") == -1)
        _this.graphicCheckbox.className += " checkbox-checked";
    _this.checkbox.checked = true;
}
checkbox.prototype.uncheck = function ()
{
    var _this = this;
    _this.checked = false;
    if (_this.graphicCheckbox.className.indexOf("checkbox-checked") != -1)
        _this.graphicCheckbox.className = _this.graphicCheckbox.className.replace("checkbox-checked", "");
    _this.checkbox.checked = false;
}
checkbox.prototype.highlight = function ()
{
    var _this = this;
    if (_this.graphicCheckbox.className.indexOf("checkbox-hover") == -1)
        _this.graphicCheckbox.className += " checkbox-hover";
}
checkbox.prototype.unhighlight = function ()
{
    var _this = this;
    if (_this.graphicCheckbox.className.indexOf("checkbox-hover") != -1)
        _this.graphicCheckbox.className = _this.graphicCheckbox.className.replace("checkbox-hover", "");
}
checkbox.prototype.value = function (checked)
{
    var _this = this;
    if (checked != null)
    {
        if (checked == true)
            _this.checkbox = true;
        else
            _this.checked = false;
    }
    return _this.checked;
}

/* Drop down
************************************************************************************/
//<div class="drop-down-list">
//    <div class="drop-down-header">
//        <div class="text">
//            <p>Select...</p>
//        </div>
//        <div class="button"></div>
//    </div>
//    <div class="drop-down-container">
//        <div class="drop-down-content">
//            <ul>
//                <li><a href="#value">text</a></li>
//                ...
//            </ul>
//            <div class="scroll-bar">
//                <div class="scroll-bar-up"></div>
//                <div class="track">
//                    <div class="slider"></div>
//                </div>
//                <div class="scroll-bar-down"></div>
//            </div>
//        </div>
//    </div>
//</div>

var _dropDowns = new Array();
function dropDown(element, select)
{
    var _this = this;
    _this.element = element;
    _this.select = select;
    _this.header = element.childNodes[0];
    _this.header.hasMouseOver = false;
    _this.headerButton = _this.header.childNodes[1];
    _this.dropDown = element.childNodes[1];
    _this.items = _this.dropDown.childNodes[0].childNodes[0].childNodes;
    _this.scrollBar = new scrollBar(_this.dropDown.childNodes[1], _this.dropDown.childNodes[0]);
    _this.collapseEvent = null;
    _this.expanded = false;
    _this.selectedValue = "";
    _this.maxHeight = 167;
    _this.initialise();
}
dropDown.prototype.initialise = function ()
{
    var _this = this;
    _this.element.style.display = "block";      // Main element needs displaying.
    _this.select.style.display = "none";        // Non-JS drop down needs hiding.
    preventSelect(_this.header);

    // Fix the height of the drop down content.
    _this.dropDown.style.width = _this.header.offsetWidth + 'px';

    // Move drop down to bottom of the DOM. Position it accordingly.
    document.body.appendChild(_this.dropDown);
    _this.setDropDownPosition();

    // Put this into a property so we can remove it when we need to.
    _this.collapseEvent = function () { if (!_this.scrollBar.hasMouseOver && !_this.header.hasMouseOver) _this.collapse(); };

    // Header events.
    _this.header.onmouseover = function () { _this.header.className = "drop-down-header drop-down-header-hover"; _this.header.hasMouseOver = true; };
    _this.header.onmouseout = function () { _this.header.className = "drop-down-header"; _this.header.hasMouseOver = false; };
    _this.header.onclick = function () { _this.toggle(); };
    _this.header.onmousedown = function () { _this.detachCollapseEvent(); };

    // Item events
    for (var z = 0; z < _this.items.length; z++)
    {
        _this.items[z].childNodes[0].onclick = function (evt)
        {
            if (evt)
            {
                evt.preventDefault();
                _this.selectItem(evt.target.innerHTML, evt.target.getAttribute("href").replace("#", ""));
            }
            else
            {
                window.event.returnValue = false;
                _this.selectItem(window.event.srcElement.innerHTML, window.event.srcElement.getAttribute("href").substring(window.event.srcElement.getAttribute("href").indexOf("#") + 1));
            }
        };
    }

    // Scroll bar
    _this.expand();
    if (_this.dropDown.offsetHeight > _this.maxHeight)
    {
        _this.dropDown.childNodes[0].style.height = _this.maxHeight + "px";
        _this.dropDown.childNodes[0].style.overflow = "auto";

        _this.scrollBar.height(_this.maxHeight);
        _this.scrollBar.show();     // show the fancy scroll bar.
    }
    _this.collapse();

    // Set value on load.
    var index = _this.select.selectedIndex;
    _this.text(_this.items[index].childNodes[0].innerHTML);
    _this.value(_this.items[index].childNodes[0].getAttribute("href").substring(_this.items[index].childNodes[0].getAttribute("href").indexOf("#") + 1));
}
dropDown.prototype.text = function (text)
{
    var _this = this;
    if (text)
        _this.header.childNodes[0].childNodes[0].innerHTML = text;

    return _this.header.childNodes[0].childNodes[0].innerHTML;
}
dropDown.prototype.value = function (value)
{
    var _this = this;
    if (value)
    {
        _this.selectedValue = value;
        _this.select.value = value.replace("null", "");
    }
    else
    {
        _this.selectedValue = "";
        _this.select.value = "";
    }

    return _this.selectedValue;
}
dropDown.prototype.selectItem = function (text, value)
{
    var _this = this;
    _this.text(text);
    _this.value(value);
    if (_this.select.onchange)
        _this.select.onchange();
}
dropDown.prototype.toggle = function ()
{
    var _this = this;
    if (_this.expanded)
        _this.collapse();
    else
        _this.expand();
}
dropDown.prototype.expand = function ()
{
    var _this = this;
    _this.expanded = true;
    _this.dropDown.style.display = "block";
    _this.attachCollapseEvent();
}
dropDown.prototype.collapse = function ()
{
    var _this = this;
    _this.expanded = false;
    _this.dropDown.style.display = "";
    _this.detachCollapseEvent();
}
dropDown.prototype.attachCollapseEvent = function ()
{
    var _this = this;
    if (window.attachEvent)
        document.body.attachEvent("onmouseup", function () { _this.collapseEvent(); });
    else
        document.body.addEventListener("mouseup", function () { _this.collapseEvent(); }, false);
}
dropDown.prototype.detachCollapseEvent = function ()
{
    var _this = this;
    if (window.attachEvent)
        document.body.detachEvent("onmouseup", function () { _this.collapseEvent(); });
    else
        document.body.removeEventListener("mouseup", function () { _this.collapseEvent(); }, false);
}
dropDown.prototype.setDropDownPosition = function ()
{
    var _this = this,
        top = 0,
        left = 0;
    if (_this.header.offsetParent)
    {
        var obj = _this.header;
        while (obj)
        {
            top += obj.offsetTop;
            left += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }

    _this.dropDown.style.top = (top + _this.header.offsetHeight) + "px";
    _this.dropDown.style.left = left + "px";
}

/* Scroll bar
************************************************************************************/
//    <div class="scroll-bar">
//        <div class="scroll-bar-up"></div>
//        <div class="track">
//            <div class="slider"></div>
//        </div>
//        <div class="scroll-bar-down"></div>
//    </div>

function scrollBar(element, scrollElement)
{
    var _this = this;
    _this.element = element;
    _this.scrollElement = scrollElement;
    _this.upButton = element.childNodes[0];
    _this.downButton = element.childNodes[2];
    _this.track = element.childNodes[1];
    _this.slider = _this.track.childNodes[0];
    _this.hasMouseOver = false;
    _this.sliderHasMouseOver = false;
    _this.sliderHasMouseDown = false;
    _this.mouseMoveEvent = null;
    _this.mouseDownTimeout = null;
    _this.scrollElementOriginalTop = 0;
    _this.sliderMouseDownPosition = 0;  // We only need the Y position.
    _this.initialise();
}
scrollBar.prototype.initialise = function ()
{
    var _this = this;

    _this.element.onmouseover = function () { _this.hasMouseOver = true; };
    _this.element.onmouseout = function () { _this.hasMouseOver = false; };

    // Button events.
    _this.upButton.onmouseover = function () { _this.upButton.className = "scroll-bar-up scroll-bar-up-hover"; };
    _this.upButton.onmouseout = function () { _this.upButton.className = "scroll-bar-up"; clearTimeout(_this.mouseDownTimeout); };
    _this.upButton.onmousedown = function () { _this.moveUp(); _this.mouseDownTimeout = setTimeout(function () { _this.upButton.onmousedown(); }, 50); };

    _this.downButton.onmouseover = function () { _this.downButton.className = "scroll-bar-down scroll-bar-down-hover"; };
    _this.downButton.onmouseout = function () { _this.downButton.className = "scroll-bar-down"; clearTimeout(_this.mouseDownTimeout); };
    _this.downButton.onmousedown = function () { _this.moveDown(); _this.mouseDownTimeout = setTimeout(function () { _this.downButton.onmousedown(); }, 50); };

    // Scroll element events.
    _this.scrollElement.onscroll = function ()
    {
        _this.slider.style.top = Math.ceil((_this.scrollElement.scrollTop / _this.scrollElement.scrollHeight) * _this.track.offsetHeight) + "px";
    };

    // Track events.
    _this.track.onmousedown = function (evt)
    {
        var ev = typeof event != "undefined" ? event : evt;
        if (!_this.sliderHasMouseOver)
        {
            var sliderTop = 0;
            if (_this.slider.offsetParent)
            {
                var obj = _this.slider;
                do
                {
                    sliderTop += obj.offsetTop;
                } while (obj = obj.offsetParent);
            }

            if (ev)
            {
                // If the click occurs above the slider move up, otherwise assume that the click occurred below the slider.
                if (ev.clientY < sliderTop)
                    _this.moveUp();
                else
                    _this.moveDown();
            }
        }

        _this.mouseDownTimeout = setTimeout(function () { _this.track.onmousedown(ev); }, 50);
    };

    // Slider events.
    _this.slider.onmouseover = function () { _this.slider.className = "slider slider-hover"; _this.sliderHasMouseOver = true; };
    _this.slider.onmouseout = function () { _this.slider.className = "slider"; _this.sliderHasMouseOver = false; clearTimeout(_this.mouseDownTimeout); };
    _this.slider.onmousedown = function (evt)
    {
        _this.sliderHasMouseDown = true;

        var ev = typeof event != "undefined" ? event : evt;
        _this.scrollElementOriginalTop = isNaN(parseInt(_this.scrollElement.scrollTop)) ? 0 : parseInt(_this.scrollElement.scrollTop);
        _this.sliderMouseDownPosition = ev.clientY;     // Capture position.
    };

    // Mouse move events.
    _this.mouseMoveEvent = function (evt)
    {
        if (_this.sliderHasMouseDown)
        {
            var pos = _this.scrollElementOriginalTop + ((evt.clientY - _this.sliderMouseDownPosition) / _this.track.offsetHeight * _this.scrollElement.scrollHeight);
            _this.scrollElement.scrollTop = pos;
        }
    }

    if (window.attachEvent)
        document.attachEvent("onmousemove", _this.mouseMoveEvent);
    else
        document.addEventListener("mousemove", _this.mouseMoveEvent, false);

    // Stop mouse moving on mouse up.
    if (window.attachEvent)
        document.attachEvent("onmouseup", function () { clearTimeout(_this.mouseDownTimeout); if (_this.sliderHasMouseDown) _this.sliderHasMouseDown = false; });
    else
        document.addEventListener("mouseup", function () { clearTimeout(_this.mouseDownTimeout); if (_this.sliderHasMouseDown) _this.sliderHasMouseDown = false; }, false);

    // Stop element selection frustration.
    preventSelect(_this.upButton);
    preventSelect(_this.slider);
    preventSelect(_this.downButton);
}
scrollBar.prototype.show = function ()
{
    var _this = this;
    _this.element.style.display = "block";
}
scrollBar.prototype.hide = function ()
{
    var _this = this;
    _this.element.style.display = "none";
}
scrollBar.prototype.height = function (value)
{
    var _this = this;
    _this.show();

    _this.element.style.height = value + "px";

    var trackHeight = value - _this.upButton.offsetHeight - _this.downButton.offsetHeight;
    _this.track.style.height = trackHeight.toString() + "px";

    var sliderHeight = (value / _this.scrollElement.scrollHeight) * trackHeight;
    if (sliderHeight < 10)  // Yep, 10 is hard coded... This is the minimum height.
        sliderHeight = 10;
    _this.slider.style.height = sliderHeight + "px";

    _this.hide();
}
scrollBar.prototype.attachMouseMoveEvent = function ()
{
    var _this = this;
    if (window.attachEvent)
        document.attachEvent("onmousemove", _this.mouseMoveEvent);
    else
        document.addEventListener("mousemove", _this.mouseMoveEvent, false);
}
scrollBar.prototype.detachMouseMoveEvent = function ()
{
    var _this = this;
    if (window.attachEvent)
        document.detachEvent("onmousemove", _this.mouseMoveEvent);
    else
        document.removeEventListener("mousemove", _this.mouseMoveEvent, false);
}
scrollBar.prototype.moveUp = function ()
{
    var _this = this;
    _this.scrollElement.scrollTop -= 25;
}
scrollBar.prototype.moveDown = function ()
{
    var _this = this;
    _this.scrollElement.scrollTop += 25;
}

/* Main Navigation
************************************************************************************/
function mainNavigation(element)
{
    var _this = this;
    _this.element = element;
    _this.menuItems = [];

    _this.initialise();
}
mainNavigation.prototype.initialise = function ()
{
    var _this = this;

    for (var i = 0; i < _this.element.childNodes.length; i++)
    {
        if (_this.element.childNodes[i].tagName == "UL")
        {
            _this.ul = _this.element.childNodes[i];
            break;
        }
    }

    for (var j = 0; j < _this.ul.childNodes.length; j++)
    {
        var dd = document.getElementById('drop-down-' + (j + 1));

        // Move drop downs to bottom of the DOM.
        if (dd)
            document.body.appendChild(dd);

        _this.menuItems.push(new mainNavigationMenuItem(_this.ul.childNodes[j], dd, _this));
    }




    //    for (var i = 0; i < _this.element.childNodes.length; i++)
    //    {
    //        if (_this.element.childNodes[i].nodeName == "UL")
    //            _this.topUL = _this.element.childNodes[i];
    //        else if (_this.element.childNodes[i].nodeType == 1 && _this.element.childNodes[i].className.substr(0, 15) == "Sub-Navigation ")
    //            _this.subNavDivs.push(_this.element.childNodes[i]);
    //    }

    //    for (var i = 0; i < _this.topUL.childNodes.length; i++)
    //    {
    //        if (_this.topUL.childNodes[i].nodeName == "LI")
    //        {
    //            if (window.attachEvent)
    //                _this.topUL.childNodes[i].attachEvent("onmousemove", function (e) { _this._liMouseMove(e); });
    //            else if (window.addEventListener)
    //                _this.topUL.childNodes[i].addEventListener("mousemove", function (e) { _this._liMouseMove(e); }, false);
    //        }
    //    }

    //    if (window.attachEvent)
    //        _this.element.attachEvent("onmousemove", function (e) { cancelBubble(typeof event != "undefined" ? event : e); });
    //    else if (window.addEventListener)
    //        _this.element.addEventListener("mousemove", function (e) { cancelBubble(e); }, false);

    //    if (window.attachEvent)
    //        document.attachEvent("onmousemove", function (e) { _this.closeAll(); });
    //    else if (window.addEventListener)
    //        document.addEventListener("mousemove", function (e) { _this.closeAll(); }, false);
}
mainNavigation.prototype.collapseAll = function ()
{
    var _this = this;
    for (var i = 0; i < _this.menuItems.length; i++)
    {
        _this.menuItems[i].collapseDropDown();
    }
}
/*
// Hmm.
mainNavigation.prototype.closeAll = function ()
{
    for (var i = 0; i < this.subNavDivs.length; i++)
        this.subNavDivs[i].style.display = "none";
};
mainNavigation.prototype.liMouseMove = function (e)
{
    var 
            _this = this,
            evt = typeof event != "undefined" ? event : e,
            _el = evt.srcElement ? evt.srcElement : evt.target,
            _pn = _el.parentNode;

    while (_pn)
    {
        if (_pn.nodeName == "LI")
        {
            var iIndex = 0;
            for (var i = 0; i < _this.topUL.childNodes.length; i++)
            {
                if (_this.topUL.childNodes[i].nodeType == 1)
                {
                    if (_this.topUL.childNodes[i] == _pn && _this.subNavDivs[iIndex].style.display != "block")
                    {
                        _this._closeAll();
                        _this.subNavDivs[iIndex].style.display = "block";
                        break;
                    }
                    iIndex++;
                }
            }
            break;
        }
        _pn = _pn.parentNode;
    }
};
*/

/* Menu item */
function mainNavigationMenuItem(element, dropDown, menu)
{
    var _this = this;
    _this.element = element;
    _this.dropDown = dropDown;
    _this.dropDownInner = null;
    _this.menu = menu;
    _this.timeout = null;
    _this.collapseDelay = 500;

    // Shadow elements
    _this.tab = null;
    _this.shadowCorner = null;
    _this.shadowTabTop = null;
    _this.shadowTabTopRight = null;
    _this.shadowTop = null;
    _this.shadowTopRight = null;
    _this.shadowRight = null;
    _this.shadowBottomRight = null;
    _this.shadowLeft = null;

    _this.initialise();
}
mainNavigationMenuItem.prototype.initialise = function ()
{
    var _this = this;
    if (_this.dropDown)
    {
        _this.dropDownInner = _this.dropDown.childNodes[0];
        if (_this.dropDownInner)
        {
            // Set the width of the drop down.
            _this.expandDropDown();
            var width = 0,
                itemWidth = 0,
                count = 0;
            for (var i = 0; i < _this.dropDownInner.childNodes.length; i++)
            {
                if (_this.dropDownInner.childNodes[i].className.indexOf("navigation-secondary") != -1)
                {
                    count++;
                    if (itemWidth == 0)
                        itemWidth = _this.dropDownInner.childNodes[i].offsetWidth;
                }

                if (i % 3 == 0)
                    _this.setEqualHeight(_this.dropDownInner.childNodes[i], _this.dropDownInner.childNodes[i + 1], _this.dropDownInner.childNodes[i + 2]);
            }

            if (count >= 3)
                width = 3 * itemWidth;
            else
                width = count * itemWidth;

            for (var i = 0; i < _this.dropDown.childNodes.length; i++)
            {
                if (_this.dropDown.childNodes[i].className.indexOf("navigation-link") != -1)
                    _this.tab = _this.dropDown.childNodes[i];
                else if (_this.dropDown.childNodes[i].className == "shadow-tab-bottom-right")
                    _this.shadowCorner = _this.dropDown.childNodes[i];
                else if (_this.dropDown.childNodes[i].className == "shadow-tab-top")
                    _this.shadowTabTop = _this.dropDown.childNodes[i];
                else if (_this.dropDown.childNodes[i].className == "shadow-tab-top-right")
                    _this.shadowTopRight = _this.dropDown.childNodes[i];
                else if (_this.dropDown.childNodes[i].className == "shadow-top")
                    _this.shadowTop = _this.dropDown.childNodes[i];
                else if (_this.dropDown.childNodes[i].className == "shadow-right")
                    _this.shadowRight = _this.dropDown.childNodes[i];
                else if (_this.dropDown.childNodes[i].className == "shadow-bottom-right")
                    _this.shadowBottomRight = _this.dropDown.childNodes[i];
                else if (_this.dropDown.childNodes[i].className == "shadow-left")
                    _this.shadowLeft = _this.dropDown.childNodes[i];
            }
            _this.dropDown.style.width = width + "px";
            _this.dropDownInner.style.width = width + "px";
            _this.setupShadow();
            _this.collapseDropDown();

            // Drop down events.
            _this.dropDown.onmousemove = function () { _this.dropDownMouseOver(); };
            _this.dropDown.onmouseout = function () { _this.dropDownMouseOut(); };
        }
    }

    // Menu item events.
    _this.element.onmouseover = function () { _this.mouseOver(); };
}
mainNavigationMenuItem.prototype.setupShadow = function ()
{
    var _this = this;

    try
    {
        _this.shadowCorner.style.left = _this.tab.offsetWidth + "px";

        _this.shadowTabTop.style.width = (_this.tab.offsetWidth - 10) + "px";
        _this.shadowTabTop.style.backgroundPosition = -(1005 - _this.tab.offsetWidth) + "px top";

        _this.shadowTopRight.style.left = _this.tab.offsetWidth + "px";

        _this.shadowTop.style.width = (_this.dropDown.offsetWidth - _this.tab.offsetWidth - 5) + "px";
        _this.shadowTop.style.backgroundPosition = -(1000 + _this.tab.offsetWidth - _this.dropDown.offsetWidth) + "px top";

        _this.shadowRight.style.height = (_this.dropDown.offsetHeight - 10) + "px";
        _this.shadowRight.style.backgroundPosition = "right " + -(1005 - _this.dropDown.offsetHeight) + "px";

        _this.shadowBottomRight.style.width = _this.dropDown.offsetWidth + "px";

        _this.shadowLeft.style.height = _this.dropDown.offsetHeight + "px";
        _this.shadowLeft.style.backgroundPosition = "left " + -(995 - _this.dropDown.offsetHeight) + "px";
    }
    catch (e) { }
}
mainNavigationMenuItem.prototype.expandDropDown = function ()
{
    var _this = this;
    if (_this.dropDown)
        _this.dropDown.style.display = "block";
}
mainNavigationMenuItem.prototype.collapseDropDown = function ()
{
    var _this = this;
    if (_this.dropDown)
        _this.dropDown.style.display = "none";
}
mainNavigationMenuItem.prototype.setDropDownPosition = function ()
{
    var _this = this,
        top = 25,
        left = 0;
    if (_this.element.offsetParent)
    {
        var obj = _this.element;
        do
        {
            top += obj.offsetTop;
            left += obj.offsetLeft;
        } while (obj = obj.offsetParent);
    }

    if (_this.dropDown)
    {
        _this.dropDown.style.top = top + "px";
        _this.dropDown.style.left = left + "px";
    }
}
mainNavigationMenuItem.prototype.mouseOver = function ()
{
    var _this = this;
    _this.menu.collapseAll();
    if (_this.dropDown)
    {
        _this.setDropDownPosition();
        _this.dropDown.style.display = "block";
    }
}
mainNavigationMenuItem.prototype.dropDownMouseOver = function ()
{
    var _this = this;
    if (_this.dropDown)
        clearTimeout(_this.timeout);
}
mainNavigationMenuItem.prototype.dropDownMouseOut = function ()
{
    var _this = this;
    if (_this.dropDown)
        _this.timeout = setTimeout(function () { _this.collapseDropDown(); }, _this.collapseDelay);
}
mainNavigationMenuItem.prototype.setEqualHeight = function (columnOne, columnTwo, columnThree)
{
    // This function is so pretty...
    if (columnOne && columnOne.className.indexOf('navigation-secondary') == -1)
        columnOne = null;
    if (columnTwo && columnTwo.className.indexOf('navigation-secondary') == -1)
        columnTwo = null;
    if (columnThree && columnThree.className.indexOf('navigation-secondary') == -1)
        columnThree = null;

    var height = 0;
    if (columnOne && columnTwo && columnThree)
    {
        if (columnOne.offsetHeight > columnTwo.offsetHeight && columnOne.offsetHeight > columnThree.offsetHeight)
            height = columnOne.offsetHeight - 16;
        else if (columnTwo.offsetHeight > columnOne.offsetHeight && columnTwo.offsetHeight > columnThree.offsetHeight)
            height = columnTwo.offsetHeight - 16;
        else if (columnThree.offsetHeight > columnOne.offsetHeight && columnThree.offsetHeight > columnTwo.offsetHeight)
            height = columnThree.offsetHeight - 16;

        if (height > 0)
        {
            columnOne.style.height = height + 'px';
            columnTwo.style.height = height + 'px';
            columnThree.style.height = height + 'px';
        }
    }
    else if (columnOne && columnTwo && !columnThree)
    {
        if (columnOne.offsetHeight > columnTwo.offsetHeight)
            height = columnOne.offsetHeight - 16;
        else if (columnTwo.offsetHeight > columnOne.offsetHeight)
            height = columnTwo.offsetHeight - 16;

        if (height > 0)
        {
            columnOne.style.height = height + 'px';
            columnTwo.style.height = height + 'px';
        }
    }
}

/* Form Validation
************************************************************************************/
// Call me back form
function formValidator(button, validators)
{
    var _this = this;
    _this.button = button;
    _this.validators = validators
    _this.isValid = false;

    _this.initialise();
}
formValidator.prototype.initialise = function ()
{
    var _this = this;

    if (_this.button)
        _this.button.onclick = function (evt) { if (!_this.validate()) (evt) ? evt.preventDefault() : window.event.returnValue = false; };
}
formValidator.prototype.validate = function ()
{
    var _this = this;

    _this.isValid = true;
    for (var i = 0; i < _this.validators.length; i++)
    {
        _this.isValid = _this.validators[i].validate() && _this.isValid;
    }

    return _this.isValid;
}

/* Validation
************************************************************************************/
var validationType = { "mandatory": "mandatory", "email": "email", "postcode": "postcode", "telephone": "telephone" };

function validator(element, label, icon, validationTypes, bespokeValidators, elementInitiators)
{
    var _this = this;
    _this.element = element;
    _this.label = label;
    _this.icon = icon;
    _this.validationTypes = validationTypes;
    _this.validationFunctions = new Array();
    _this.bespokeValidators = bespokeValidators;
    _this.elementInitiators = elementInitiators;
    _this.isValid = false;
    _this.emailRegex = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name|mobi|asia|jobs|travel))$/;
    _this.postcodeRegex = /(((^[BEGLMNS][1-9]\d?)|(^W[2-9])|(^(A[BL]|B[ABDHLNRST]|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]|M[EKL]|N[EGNPRW]|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\d\d?)|(^W1[A-HJKSTUW0-9])|(((^WC[1-2])|(^EC[1-4])|(^SW1))[ABEHMNPRVWXY]))(\s*)?([0-9][ABD-HJLNP-UW-Z]{2}))|(^GIR\s?0AA)/
    _this.accountNumberRegex = /^[0-9]{8}\/[0-9]{1}$/;

    attachEventHandler(window, "load", function () { _this.initialise(); });
}
validator.prototype.initialise = function ()
{
    var _this = this;
    // Events
    if (_this.element)
    {
        switch (_this.element.tagName.toLowerCase())
        {
            case "input":
            case "textarea":
                _this.element.onblur = function () { _this.validate(); };
                break;
            case "select":    // Drop down.
                _this.element.onchange = function () { _this.validate(); };
                break;
        }
    }

    if (_this.elementInitiators && _this.elementInitiators.length > 0)
    {
        for (var i = 0; i < _this.elementInitiators.length; i++)
            _this.elementInitiators[i].onclick = function () { _this.validate(); };
    }

    // Run through the validation types and select the corresponding validation functions.
    if (_this.validationTypes)
    {
        for (var i = 0; i < _this.validationTypes.length; i++)
        {
            switch (_this.validationTypes[i])
            {
                case "mandatory":
                    _this.validationFunctions.push(function () { _this.mandatory(); });
                    break;
                case "email":
                    _this.validationFunctions.push(function () { _this.email(); });
                    break;
                case "postcode":
                    _this.validationFunctions.push(function () { _this.postcode(); });
                    break;
                case "telephone":
                    _this.validationFunctions.push(function () { _this.telephone(); });
                    break;
            }
        }
    }
}
validator.prototype.validate = function ()
{
    var _this = this;
    _this.isValid = true;   // Initialised as false so that calls to it prior to validation correctly return false. Set it to true here so the validation works.
    for (var i = 0; i < _this.validationFunctions.length; i++)
    {
        if (_this.validationFunctions[i])
            _this.validationFunctions[i]();
    }

    if (_this.bespokeValidators)
    {
        // Handle the bespoke validator functions
        if (typeof _this.bespokeValidators == "function")
            _this.isValid = (_this.isValid && _this.bespokeValidators());
        else
        {
            for (var j = 0; j < _this.bespokeValidators.length; j++)
            {
                if (typeof _this.bespokeValidators[j] == "function")
                    _this.isValid = (_this.isValid && _this.bespokeValidators[j]());
            }
        }
    }

    // Set the valid status of this control.
    if (_this.isValid)
        _this.setValid();
    else
        _this.setInvalid();

    return _this.isValid;
}
validator.prototype.setValid = function ()
{
    var _this = this;
    if (_this.label)
        _this.label.className = _this.label.className.replace("label-error", "");
    if (_this.icon)
    {
        _this.icon.className = _this.icon.className.replace("icon-error", "");

        if (_this.icon.className.indexOf("icon-valid") == -1)
            _this.icon.className = _this.icon.className + " icon-valid";            
    }
}
validator.prototype.setInvalid = function()
{
    var _this = this;
    if (_this.label && _this.label.className.indexOf("label-error") == -1)
        _this.label.className = _this.label.className + " label-error";
    if (_this.icon)
    {
        _this.icon.className = _this.icon.className.replace("icon-valid", "");

        if (_this.icon.className.indexOf("icon-error") == -1)
            _this.icon.className = _this.icon.className + " icon-error";
    }
}
validator.prototype.mandatory = function ()
{
    var _this = this;
    var inputID = "";
    if (_this.element.tagName.toLowerCase() == "select")
        inputID = "select";
    else if (_this.element.tagName.toLowerCase() == "textarea")
        inputID = "textarea";
    else
        inputID = (_this.element.tagName + "+" + _this.element.getAttribute("type")).toLowerCase()

    switch (inputID)
    {
        case "input+text":
        case "input+password":
        case "textarea":
            _this.element.value = _this.element.value.trim();
            _this.isValid = (_this.element.value.length > 0 && _this.element.value != "") ? _this.isValid && true : false;
            break;
        case "select":    // Drop down.
            _this.isValid = (_this.element.value && _this.element.value != "null" && _this.element.value != "") ? _this.isValid && true : false;
            break;
    }
}
validator.prototype.email = function ()
{
    var _this = this;
    switch (_this.element.tagName.toLowerCase() + "+" + _this.element.getAttribute("type").toLowerCase())
    {
        case "input+text":
            _this.element.value = _this.element.value.trim();
            _this.isValid = (_this.emailRegex.test(_this.element.value)) ? _this.isValid && true : false;
            break;
    }
}
validator.prototype.postcode = function ()
{
    var _this = this;
    switch (_this.element.tagName.toLowerCase() + "+" + _this.element.getAttribute("type").toLowerCase())
    {
        case "input+text":
            _this.element.value = _this.element.value.toUpperCase();
            if (_this.element.value.indexOf(' ') == -1)
                _this.element.value = _this.element.value.substring(0, _this.element.value.length - 3) + " " + _this.element.value.substring(_this.element.value.length - 3, _this.element.value.length);

            _this.isValid = (_this.postcodeRegex.test(_this.element.value)) ? _this.isValid && true : false;
            break;
    }
}
validator.prototype.telephone = function ()
{
    var _this = this;
    switch (_this.element.tagName.toLowerCase() + "+" + _this.element.getAttribute("type").toLowerCase())
    {
        case "input+text":
            _this.isValid = (_this.element.value.length >= 11 && new RegExp("^[0-9 ]+$", "gi").test(_this.element.value));
            break;
    }
}

function validateAtLeastOneChecked(checkBoxOne, checkBoxTwo)
{
    return !(!checkBoxOne.checked && !checkBoxTwo.checked);
}

function validateExactlyOneChecked(checkBoxOne, checkBoxTwo) {
    if (checkBoxOne.checked && checkBoxTwo.checked)
        return false;
    else if (checkBoxOne.checked || checkBoxTwo.checked)
        return true;

    return false;
}

function validatePostcodeOrAccountNumber(textbox)
{
    var accountNumberRegex = /^[0-9]{8}\/[0-9]{1}$/,
        postcodeRegex = /(((^[BEGLMNS][1-9]\d?)|(^W[2-9])|(^(A[BL]|B[ABDHLNRST]|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]|M[EKL]|N[EGNPRW]|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\d\d?)|(^W1[A-HJKSTUW0-9])|(((^WC[1-2])|(^EC[1-4])|(^SW1))[ABEHMNPRVWXY]))(\s*)?([0-9][ABD-HJLNP-UW-Z]{2}))|(^GIR\s?0AA)/;

    textbox.value = textbox.value.trim().toUpperCase();
    return (textbox.value != "" && (accountNumberRegex.test(textbox.value) || postcodeRegex.test(textbox.value)));
}

/* Misc
************************************************************************************/
function preventSelect (element)
{
    if (element.style.MozUserSelect !== undefined)
        element.style.MozUserSelect = "none";
    else if (element.style.webkitUserSelect !== undefined)
        element.style.webkitUserSelect = "none";

    element.setAttribute("unselectable", "on");
}

function cancelBubble(e)
{
    if (e.preventDefault)
    {
        e.preventDefault();
        e.stopPropagation();
    }
    else
    {
        e.returnValue = false;
        e.cancelBubble = true;
    }
}

String.prototype.trim = function ()
{
    var str = this.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
    while (ws.test(str.charAt(--i)));
    return str.slice(0, i + 1);
}

function queryStringManager(qs) { // optionally pass a querystring to parse
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&');

    for (var i = 0; i < args.length; i++) {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]);

        var value = (pair.length == 2)
		        ? decodeURIComponent(pair[1])
		        : name;

        this.params[name] = value;
    }
}

queryStringManager.prototype.get = function (key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_;
};

queryStringManager.prototype.contains = function (key) {
    var value = this.params[key];
    return (value != null);
};


/* Tabbed Content
************************************************************************************/


function tabbedContent(baseID, noTabs) {
    var tabbedContent = this;
    tabbedContent.baseID = baseID;
    tabbedContent.noTabs = noTabs;
    tabbedContent.tabCount = 0;
    tabbedContent.tabs = new Object();
    tabbedContent.panelCount = 0;
    tabbedContent.panels = new Object();
    tabbedContent.panelClass = new Object();
    tabbedContent.selectedTab = 0;
    tabbedContent.initialise();
    tabbedContent.onBeforeTabChanged = null;
    tabbedContent.onTabChanged = null;
}

tabbedContent.prototype =
    {
        initialise: function () {
            var tabbedContent = this, div, a, qs = new queryStringManager(), inpSelected = document.getElementById(tabbedContent.baseID + "_SelectedTab");
            var tab = 1;

            if (inpSelected && inpSelected.value != "") {
                tab = inpSelected.value;
                if (tab < 1) tab = 1;
                if (tab > tabbedContent.noTabs) tab = 1;
                inpSelected.value = tab;
            }

            tabbedContent.selectedTab = tab - 1;

            for (var i = 0; i < tabbedContent.noTabs; i++) {
                div = document.getElementById(tabbedContent.baseID + "_Tab" + (i + 1));
                if (div) {
                    tabbedContent.tabs[tabbedContent.tabCount] = div;
                    tabbedContent.tabCount++;

                    if (i == (tab - 1)) div.parentNode.className = "TabIn";
                    else div.parentNode.className = "Tab";

                    if (div.childNodes.length > 0) {
                        a = div.childNodes[0];
                        a.href = "javascript:tabbedcontentmanager.showTab(\"" + tabbedContent.baseID + "\", " + i + ");";
                    }
                }
                div = document.getElementById(tabbedContent.baseID + "_Content" + (i + 1));
                if (div) {
                    tabbedContent.panels[tabbedContent.panelCount] = div;
                    tabbedContent.panelClass[tabbedContent.panelCount] = div.parentNode.className;
                    tabbedContent.panelCount++;
                    if (i != (tab - 1))
                        div.parentNode.className = tabbedContent.panelClass[tabbedContent.panelCount] + " TabPanelOff";
                }
            }

            if (tabbedContent.onTabChanged != null)
                tabbedContent.onTabChanged(tabbedContent.selectedTab);
        },

        showTab: function (tabNo) {
            var tabbedContent = this, inpSelected = document.getElementById(tabbedContent.baseID + "_SelectedTab");

            if (!tabbedContent.onBeforeTabChanged || (tabbedContent.onBeforeTabChanged && tabbedContent.onBeforeTabChanged(tabNo))) {

                for (var i = 0; i < tabbedContent.tabCount; i++) {
                    if (i == tabNo)
                        tabbedContent.tabs[i].parentNode.className = "TabIn";
                    else
                        tabbedContent.tabs[i].parentNode.className = "Tab";
                }
                for (var i = 0; i < tabbedContent.panelCount; i++) {
                    if (i == tabNo) {
                        tabbedContent.panels[i].parentNode.className = tabbedContent.panelClass[i];
                        tabbedContent.panels[i].parentNode.style.display = "";
                    }
                    else
                        tabbedContent.panels[i].parentNode.className = tabbedContent.panelClass[i] + " TabPanelOff";
                }

                if (inpSelected) inpSelected.value = tabNo + 1;
                tabbedContent.selectedTab = tabNo;

                if (tabbedContent.onTabChanged != null)
                    tabbedContent.onTabChanged(tabbedContent.selectedTab);

            }
        }
    };

function tabbedContentManager() {
    var tabbedContentManager = this;
    tabbedContentManager.tabbedContentCount = 0;
    tabbedContentManager.tabbedContent = new Object();
}

tabbedContentManager.prototype =
    {
        create: function (baseID, noTabs) {
            var tabbedContentManager = this, tc = new tabbedContent(baseID, noTabs);
            tabbedContentManager.tabbedContent[tabbedContentManager.tabbedContentCount] = tc;
            tabbedContentManager.tabbedContentCount++;
            return tc;
        },

        getByBaseID: function (baseID) {
            var tabbedContentManager = this;
            for (var i = 0; i < tabbedContentManager.tabbedContentCount; i++) {
                if (tabbedContentManager.tabbedContent[i].baseID == baseID)
                    return tabbedContentManager.tabbedContent[i];
            }
        },

        showTab: function (baseID, tabNo) {
            try {
                var tabbedContentManager = this, tc = tabbedContentManager.getByBaseID(baseID);
                if (tc) tc.showTab(tabNo);
            }
            catch (e) { };
        }
    };

var tabbedcontentmanager = new tabbedContentManager();
