/*global Prototype, navigator, $, $$, Option */
Prototype.Browser.IE6 = (Prototype.Browser.IE && (Prototype.Browser.version_major === 4) && (navigator.userAgent.indexOf("msie 6.") !== -1));
Prototype.Browser.IEbefore7 = (parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE") + 5), 10) < 7);

var lightbox = {

    yPos     : 0,
    xPos     : 0,
    popupWin : 'error_panel',

    show: function (name)
    {
        this.popupWin = name;

        if (Prototype.Browser.IEbefore7)
        {
            this.getScroll();
            this.prepareIE('100%', 'hidden');
            this.setScroll(0, 0);
            this.hideSelects('hidden');
        }
        $('overlay').show();
        $(name).show();
    },

    hide : function ()
    {
        $(this.popupWin).hide();
        if (Prototype.Browser.IEbefore7)
        {
            this.setScroll(0, this.yPos);
            this.prepareIE("auto", "auto");
            this.hideSelects("visible");
        }
        $('overlay').hide();
    },

    prepareIE: function (height, overflow)
    {
        var bod = document.getElementsByTagName('body')[0];
        bod.style.height = height;
        bod.style.overflow = overflow;

        var htm = document.getElementsByTagName('html')[0];
        htm.style.height = height;
        htm.style.overflow = overflow;
    },


    // In IE, select elements hover on top of the lightbox
    hideSelects: function (visibility)
    {
        var selects = $$('select:not(.donthide)');
        for (var i = 0; i < selects.length; i++)
        {
            selects[i].style.visibility = visibility;
        }
    },


    // Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
    getScroll: function ()
    {
        if (self.pageYOffset)
        {
            this.yPos = self.pageYOffset;
        }
        else if (document.documentElement && document.documentElement.scrollTop)
        {
            this.yPos = document.documentElement.scrollTop;
        }
        else if (document.body)
        {
            this.yPos = document.body.scrollTop;
        }
    },

    setScroll: function (x, y)
    {
        window.scrollTo(x, y);
    }
};


function close_popup(name)
{
    lightbox.hide();
}

function close_error_window()
{
    close_popup('error_panel');
}


function open_popup(name)
{
    lightbox.show(name);
}



function display_error_window(text, title)
{
    $('error_panel_title').innerHTML = title;
    $('error_panel_txt').innerHTML = text;
    open_popup('error_panel');
}


function test_cookie()
{
    var return_var = (navigator.cookieEnabled)? true : false;
    //if not IE4+ nor NS6+
    if (typeof navigator.cookieEnabled === "undefined" && !return_var)
    {
        create_cookie('testcookie', 'none', '', '/', '', '');
        if (read_cookie('testcookie'))
        {
            return_var = true;
            erase_cookie('testcookie');
        }
        else
        {
            return_var = false;
        }
    }
    return return_var;
}


var cookieEnabled = test_cookie();


function select_add_option(obj, text, value, selected)
{
    if ((obj !== null) && (obj.options !== null))
    {
        obj.options[obj.options.length] = new Option(text, value, false, selected);
    }
}


function select_has_options(obj)
{
    if ((obj !== null) && (obj.options !== null))
    {
        return true;
    }
    return false;
}


function select_move_selected_options(from, to)
{
    if (!select_has_options(from))
    {
        return;
    }
    var index = 0;
    var o = null;
    var i = 0;
    for (i = 0; i < from.options.length; i++)
    {
        o = from.options[i];
        if (o.selected)
        {
            if (!select_has_options(to))
            {
                index = 0;
            }
            else
            {
                index = to.options.length;
            }
            to.options[index] = new Option(o.text, o.value, false, false);
        }
    }
    // Delete them from original
    for (i = (from.options.length - 1); i >= 0; i--)
    {
        o = from.options[i];
        if (o.selected)
        {
            from.options[i] = null;
        }
    }
    if ((arguments.length < 3) || (arguments[2] === true))
    {
        select_sort(from);
        select_sort(to);
    }
    from.selectedIndex = -1;
    to.selectedIndex = -1;
}


function select_remove_all_options(from)
{
    if (!select_has_options(from))
    {
        return;
    }
    for (var i = (from.options.length - 1); i >= 0; i--)
    {
        from.options[i] = null;
    }
    from.selectedIndex = -1;
}


function select_serialise(obj, outname)
{
    if (!select_has_options(obj))
    {
        return '';
    }
    var return_txt = '';
    for (var i = (obj.options.length - 1); i >= 0; i--)
    {
        return_txt += '&' + outname + '=' + obj.options[i].value;
    }
    return return_txt;
}


function select_sort(obj)
{
    var o = [];
    var i = 0;
    if (!select_has_options(obj))
    {
        return;
    }
    for (i = 0; i < obj.options.length; i++)
    {
        o[o.length] = new Option(obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected);
    }
    if (o.length === 0)
    {
        return;
    }
    o = o.sort(
        function (a, b)
        {
            if ((a.text.toLowerCase() + "") < (b.text.toLowerCase() + ""))
            {
                return -1;
            }
            if ((a.text.toLowerCase() + "") > (b.text.toLowerCase() + ""))
            {
                return 1;
            }
            return 0;
        }
    );

    for (i = 0; i < o.length; i++)
    {
        obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
        if ((i % 2) === 0)
        {
            obj.options[i].className = 'contact_row1';
        }
        else
        {
            obj.options[i].className = 'contact_row2';
        }
    }
}


function external_links()
{
    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";
        }
    }
}

function create_cookie(name, value, days)
{
    var expires = "";
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function read_cookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    var c = '';
    for (var i = 0; i < ca.length; i++)
    {
        c = ca[i];
        while (c.charAt(0) === ' ')
        {
            c = c.substring(1, c.length);
        }
        if (c.indexOf(nameEQ) === 0)
        {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}


function erase_cookie(name)
{
    create_cookie(name, "", -1);
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj)
{
	if (!radioObj)
	{
		return "";
	}

	var radioLength = radioObj.length;

	if (radioLength === undefined)
	{
		if (radioObj.checked)
		{
			return radioObj.value;
		}
		else
		{
			return "";
		}
	}
	for (var i = 0; i < radioLength; i++)
	{
		if(radioObj[i].checked)
		{
			return radioObj[i].value;
		}
	}
	return "";
}