insertAtCursor = function (container, myValue)
{
    var sel, tagstart, str_len, tagend, startPos, endPos;
    myField = document.getElementById(container);
    if (document.selection)
    {
        myField.focus();
        sel = document.selection.createRange();
        switch (myValue)
        {
            case "[url]":
            case "[img]":
            case "[b]":
            case "[u]":
            case "[i]":
            case "[s]":
                tagstart = myValue; 
                str_len = myValue.length;
                tagend = myValue.substring(0,1) + '/' + myValue.substring(1,str_len);
                sel.text = tagstart + sel.text + tagend;
            break;
            default:
                sel.text = myValue;
            break;
        }
    }
    else if (myField.selectionStart || myField.selectionStart == '0')
    {
        startPos = myField.selectionStart;
        endPos = myField.selectionEnd;
        switch (myValue)
        {
            case "[url]":
            case "[img]":
            case "[b]":
            case "[u]":
            case "[i]":
            case "[s]":
                tagstart = myValue;
                str_len = myValue.length;
                tagend = myValue.substring(0,1) + '/' + myValue.substring(1,str_len);
                myField.value = myField.value.substring(0, startPos) + tagstart + myField.value.substring(startPos, endPos) + tagend + myField.value.substring(endPos, myField.value.length);
            break;
            default:
                myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
            break;
        }
    }
    else
    {
        myField.value += myValue;
    }
}

popupObject = function(width, height)
{
    this.delay = 5;
    this.speed = 10;
    this.popupWidth = 0;
    this.popupHeight = 0;
    this.popupCurrentWidth = 0;
    this.popupCurrentHeight = 0;
    this.myWidth = 0;
    this.myHeight = 0;
    this.cdiv = 'blank';
    this.expand = 'blank';
    this.innerHTML = '';
    this.popup = '';
    this.popupBackground = '';
    this.visible;

    this.closePopup = function()
    {
        clearInterval(this.cdiv);
        this.cdiv = 'blank';

        this.popup = '';
        var div_loop;
        while (div_loop = document.getElementById('popup'))
        {
            div_loop.parentNode.removeChild(div_loop);
        }
        while (div_loop = document.getElementById('popupBackground'))
        {
            div_loop.parentNode.removeChild(div_loop);
        }
        document.getElementsByTagName('html')[0].style.overflow = 'auto';
    }

    this.getWindowSize = function()
    {
        document.getElementsByTagName('html')[0].style.overflow = 'hidden';
        if (typeof( window.innerWidth ) == 'number')
        {
            this.myWidth = window.innerWidth;
            this.myHeight = window.innerHeight;
        }
        else if (document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ))
        {
            this.myWidth = document.documentElement.clientWidth;
            this.myHeight = document.documentElement.clientHeight;
        }
        else if (document.body && ( document.body.clientWidth || document.body.clientHeight ))
        {
            this.myWidth = document.body.clientWidth;
            this.myHeight = document.body.clientHeight;
        }
    }

    this.positionCenter = function()
    {
        this.getWindowSize();
        if (this.cdiv == 'blank') {this.cdiv = window.setInterval("popup.positionCenter()", 25);}
        this.popupBackground.style.top = Math.floor(Math.round( document.documentElement.scrollTop)) + 'px';
        this.popup.style.top = document.documentElement.scrollTop + Math.floor((this.myHeight - this.popupCurrentHeight)/2) + 'px';
        this.popup.style.left = document.documentElement.scrollLeft + Math.floor((this.myWidth - this.popupCurrentWidth)/2) + 'px';
        this.popupBackground.style.height = this.myHeight + 'px';
        this.popupBackground.style.width = this.myWidth + 'px';
    }

    this.expand = function(auto)
    {
        if (auto == true)
        {

        }
        if (this.popupCurrentWidth < this.popupWidth || this.popupCurrentHeight < this.popupHeight)
        {
            if (this.popupCurrentWidth < this.popupWidth)
            {
                if (this.popupCurrentWidth + Math.floor(this.popupWidth/this.speed) > this.popupWidth)
                {
                    this.popupCurrentWidth = this.popupWidth;
                }
                else
                {
                    this.popupCurrentWidth += Math.floor(this.popupWidth/this.speed);
                }
            }
            if (this.popupCurrentHeight < this.popupHeight)
            {
                if (this.popupCurrentHeight + Math.floor(this.popupHeight/this.speed) > this.popupHeight)
                {
                    this.popupCurrentHeight = this.popupHeight;
                }
                else
                {
                    this.popupCurrentHeight += Math.floor(this.popupHeight/this.speed);
                }
            }
            this.popup.style.width = this.popupCurrentWidth + 'px';
            this.popup.style.height = this.popupCurrentHeight + 'px';
            this.positionCenter();
            var expand = setTimeout('popup.expand()', this.delay);
        }
        else
        {
            this.popupWidth = this.popupCurrentWidth;
            this.popupHeight = this.popupCurrentHeight;
            this.innerText(this.visible);
        }
    }

    this.innerTextShow = function()
    {
        this.popup.style.width = this.popupWidth + 'px';
        this.popup.style.height = this.popupHeight + 'px';
        this.centerText();
        var popupcontainer = document.getElementById('popupcontainer');
        popupcontainer.style.visibility = 'visible';
        this.positionCenter();
    }

    this.centerText = function()
    {
        var popupcontainer = document.getElementById('popupcontainer');
        popupcontainer.style.visibility = 'hidden';
        popupcontainer.style.height = this.popupHeight + 'px';
        popupcontainer.style.width = this.popupWidth + 'px';
        this.popupCurrentWidth = this.popupWidth;
        this.popupCurrentHeight = this.popupHeight;
        var temppadding = Math.floor((this.popupHeight / 2) - (popupcontainer.offsetHeight / 2));
        if (temppadding < 0)
        {
            temppadding = 0;
        }
        popupcontainer.style.paddingTop = temppadding + 'px';
    }

    this.changeText = function(text)
    {
        this.innerHTML = text;
        var popupcontainer = document.getElementById('popupcontainer');
        popupcontainer.style.visiblility = 'hidden';
        popupcontainer.innerHTML = this.innerHTML;
    }

    this.innerText = function(visible)
    {
        this.popup.style.width = this.popupWidth + 'px';
        this.popup.style.height = this.popupHeight + 'px';
        var newimg = document.createElement('img');
        newimg.style.position = 'absolute';
        newimg.style.zIndex = '350';
        newimg.style.width = '43px';
        newimg.style.height = '42px';
        newimg.setAttribute("src", "http://images.apegaming.net/image_warehouse/mNGsImit.png");
        newimg.setAttribute("alt", "X");
        try
        {
            newimg.addEventListener('click', function(event) { popup.closePopup(); }, false);
        }
        catch(err)
        {
            newimg.attachEvent('onclick', function() { popup.closePopup() });
        }
        this.popup.appendChild(newimg);
        var popcont = document.createElement('div');
        popcont.setAttribute('id', 'popupcontainer');
        popcont.style.textAlign = 'center';
        popcont.style.position = 'relative';
        popcont.style.overflow = 'auto';
        popcont.style.fontWeight = 'bold';
        popcont.style.visibility = 'visible';
        popcont.innerHTML = this.innerHTML;
        this.popup.appendChild(popcont);
        this.centerText();
        if (visible == true)
        {
            var popupcontainer = document.getElementById('popupcontainer');
            popupcontainer.style.visibility = 'visible';
        }
    }

    this.dimensions = function(width, height)
    {
        this.popupCurrentWidth = 0;
        this.popupCurrentHeight = 0;
        this.getWindowSize();
        if (width == 0 || width > (this.myWidth * 0.98))
        {
            this.popupWidth = Math.floor(this.myWidth * 0.98);
        }
        else
        {
            this.popupWidth = width;
        }
        if (height == 0 || height > (this.myHeight * 0.98))
        {
            this.popupHeight = Math.floor(this.myHeight * 0.98);
        }
        else
        {
            this.popupHeight = height;
        }
    }

    this.open = function(width, height, text, visible)
    {
        if (visible != false) {this.visible = true} else {this.visible = false};
        this.dimensions(width, height);
        this.innerHTML = text;
        var ni = document.getElementsByTagName('body')[0];
        // container div
        var newdiv = document.createElement('div');
        newdiv.setAttribute('id', 'popupBackground');
        newdiv.style.position = "absolute";
        newdiv.style.zIndex = '300';
        newdiv.style.padding = '0';
        newdiv.style.width = Math.floor(this.myWidth * 1) + 'px';
        newdiv.style.height = Math.floor(this.myHeight * 1)+ 'px';
        newdiv.style.backgroundColor = 'black';
        newdiv.style.opacity = '0.5';
        newdiv.style.filter = 'alpha(opacity = 50)';
        ni.appendChild(newdiv);
            // html div
            var htmlTop = Math.floor((this.myHeight/2) - (this.popupHeight/2));
            var htmlLeft = Math.floor((this.myWidth/2) - (this.popupWidth/2));
            var newhtml = document.createElement('div');
            newhtml.setAttribute('id', 'popup');
            newhtml.style.position = "absolute";
            newhtml.style.zIndex = '301';
            newhtml.style.width = this.popupCurrentWidth + "px";
            newhtml.style.height = this.popupCurrentHeight + "px";
            newhtml.style.backgroundColor = 'white';
            newhtml.style.top = htmlTop  + 'px';
            newhtml.style.left = htmlLeft + 'px';
            newhtml.style.opacity = '1';
            newhtml.style.filter = 'alpha(opacity = 100)';
            newhtml.padding = '0px';
            ni.appendChild(newhtml);
        this.popup = document.getElementById('popup');
        this.popupBackground = document.getElementById('popupBackground');
        this.expand();
        this.positionCenter();
    }
}

galleryObject = function()
{
    this.imageList = new Array();
    this.element = 0;
    this.imageLocation = '';
    this.thumbLocation = '';
    this.width = 0;
    this.height = 0;
    this.timeout = 0;
    this.playTimeout = 5000;

    this.setDims = function(width, height)
    {
        this.width = width;
        this.height = height;
    }

    this.addImage = function (image)
    {
        var f = this;
        this.imageList.push(image);
        try
        {
            document.getElementById(image).addEventListener('click', function() { f.activatePopup(image); }, false);
        }
        catch(e)
        {
            document.getElementById(image).attachEvent('onclick', function() { f.activatePopup(image); });
        }
    }

    this.string = function(image)
    {
        var string = "<div style=\"width:100%;float:left;text-align:center;position:absolute;padding-top:5px;z-index:349;color:#FFFFFF;\">"+
                "<img onclick=\"gallery.previous();\" style=\"display:inline\" src=\"images/BTN_Last.png\" alt=\"\" /> "+
                "<img onclick=\"gallery.play();\" style=\"display:inline\" src=\"images/BTN_Play.png\" alt=\"\" /> "+
                "<img onclick=\"gallery.next();\" style=\"display:inline\" src=\"images/BTN_Next.png\" alt=\"\" />"+
            "</div>"+
            "<div id=\"galLoad\" style=\"padding-top:130px;float:right;position:absolute;z-index:348;color:#000000;width:100%;\">"+
            "<img style=\"margin-left:auto;margin-right:auto;\" src=\"images/loading.gif\" alt=\"Loading...\" />"+
            "</div>"+
            "<img id=\"largePic\" onload=\"gallery.showImage();\" style=\"display:none;vertical-align:middle;margin-right:auto;margin-left:auto;\" src=\"" + image +"\" alt=\"\" />";
        return string;
    }

    this.activatePopup = function(image)
    {
        for (var i in this.imageList)
        {
            if (this.imageList[i] == image )
            {
                this.element = i;
            }
        }
        popup.open(this.width, this.height, this.string(this.imageLocation + image));
    }

    this.previous = function()
    {
        this.element--;
        if (this.element < 0) this.element = this.imageList.length - 1;
        popup.changeText(this.string(this.imageLocation + this.imageList[this.element]));
        clearTimeout (this.timeout);
    }

    this.next = function()
    {
        this.element++;
        if (this.element > this.imageList.length - 1) this.element = 0;
        popup.changeText(this.string(this.imageLocation + this.imageList[this.element]));
        clearTimeout (this.timeout);
    }

    this.play = function()
    {
        if (document.getElementById('popup'))
        {
            this.next();
            this.timeout = setTimeout('gallery.play()', this.playTimeout);
        }
    }

    this.showImage = function()
    {
        document.getElementById('galLoad').style.display = 'none';
        document.getElementById('largePic').style.display = 'block';
        popup.dimensions(document.getElementById('largePic').width, document.getElementById('largePic').height);
        popup.innerTextShow();
    }
}

expandObject = function(type, delay, step, direction)
{
    this.divList = new Array();
    this.openList = new Array();
    this.reexpand = new Array();
    this.toHeight = 0;
    this.currentHeight = 0;
    this.toWidth = 0;
    this.maxWidth = 0;
    this.maxHeight = 0;
    this.currentWidth = 0;
    this.activeDivName;
    this.openImage = '';
    this.closeImage = '';
    this.images = false;
    this.type = type;
    this.delay = (delay == null) ? 5 : delay;
    this.step = (step == null) ? 5 : step;
    this.direction = (direction == null) ? 'vertical' : direction;

    this.activateTimed = function ()
    {
        this.getDimensions(document.getElementById(this.activeDivName));
        this.expander();
    }

    this.addImages = function(openImage, closeImage)
    {
        this.openImage = openImage;
        this.closeImage = closeImage;
        this.images = true;
    }

    this.addObject = function(div, trigger)
    {
        this.divList.push(div);
        this.openList.push(false);
        if (this.type == 'timed')
        {
                this.activeDivName = div;
        }
        else
        {
            var thisEventDiv = document.getElementById(div + "Trigger");
            var f = this;
            if (thisEventDiv.addEventListener)
            {
                if (this.type == 'click')
                {
                    thisEventDiv.addEventListener('click',function (e) {f.mouseEvent(e, div);}, false);
                }
                else
                {
                    thisEventDiv.addEventListener('mouseover',function (e) {f.mouseEvent(e, div);}, false);
                    thisEventDiv.addEventListener('mouseout',function (e) {f.mouseEvent(e, div);}, false);
                }
            }
            else if (thisEventDiv.attachEvent)
            {
                if (this.type == 'click')
                {
                    thisEventDiv.attachEvent('onclick',function (e) {f.mouseEvent(e, div);});
                }
                else
                {
                    thisEventDiv.attachEvent('onmouseover',function (e) {f.mouseEvent(e, div);});
                    thisEventDiv.attachEvent('onmouseout',function (e) {f.mouseEvent(e, div);});
                }
            }
        }
    }

    this.closeOthers = function()
    {
        this.unexpander();
        if (this.images == true)
        {
            for (var i=0; i<this.divList.length; i++)
            {
                if (this.divList[i] != this.activeDivName)
                {
                    document.getElementById(this.divList[i]+'Image').src = this.closeImage;
                }
            }
        }
    }

    this.unexpander = function ()
    {
        var continueTest = false;
        for (var i=0; i < this.divList.length; i++)
        {
            if (this.divList[i] != this.activeDivName && this.openList[i] == true)
            {
                var unexpandDiv = document.getElementById(this.divList[i]);
                if (unexpandDiv.offsetHeight > 0)
                {
                    continueTest = true;
                    var newHeight = unexpandDiv.offsetHeight - this.step;
                    if (newHeight <= 0)
                    {
                        newHeight = 0;
                        unexpandDiv.style.display = 'none';
                        this.openList[i] = false;
                    }
                    unexpandDiv.style.height = newHeight + 'px';
                }
            }
        }
        if (continueTest == true)
        {
            var f = this;
            var unexpand = setTimeout(function () { f.unexpander();},this.delay);
        }
    }

    this.expander = function() {
        if (this.activeDivName != '') {
            var expandDiv = document.getElementById(this.activeDivName);
            if (this.direction == 'vertical') {
                if (this.currentHeight < this.toHeight) {
                    this.currentHeight += this.step;
                    expandDiv.style.height = this.currentHeight + 'px';
                    expandDiv.style.width = this.toWidth + 'px';
                    var f = this;
                    clearTimeout(this.reexpand[expandDiv]);
                    this.reexpand[expandDiv] = setTimeout(function() { f.expander(); }, this.delay);
                }
            }
            else {
                if (this.currentWidth < this.toWidth) {
                    this.currentWidth += this.step;
                    expandDiv.style.width = this.currentWidth + 'px';
                    expandDiv.style.height = this.toHeight + 'px';
                    var f = this;
                    clearTimeout(this.reexpand[expandDiv]);
                    this.reexpand[expandDiv] = setTimeout(function() { f.expander(); }, this.delay);
                }
            }
        }
    }

    this.isMouseLeaveOrEnter = function (e, handler) {
        if (e.type != 'mouseout' && e.type != 'mouseover') return false;
        var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement;
        while (reltg && reltg != handler) reltg = reltg.parentNode;
        return (reltg != handler);
    }

    this.getDimensions = function (expandDiv) {
        this.toHeight = 0;
        this.toWidth = 0;
        this.currentHeight = 0;
        this.currentWidth = 0;
        expandDiv.style.display = 'block';
        expandDiv.style.overflow = 'auto';
        if (this.direction == 'vertical')
        {
            expandDiv.style.height = 'auto';
        }
        else
        {
            expandDiv.style.width = 'auto';
        }
        if (this.maxHeight > 0 && expandDiv.offsetHeight > this.maxHeight)
        {
            this.toHeight = this.maxHeight;
        }
        else
        {
            this.toHeight = expandDiv.offsetHeight;
        }
        if (this.maxWidth > 0 && expandDiv.offsetWidth > this.maxWidth)
        {
            this.toWidth = this.maxWidth;
        }
        else
        {
            this.toWidth = expandDiv.offsetWidth;
        }
        expandDiv.style.visibility = 'hidden';
        expandDiv.style.display = 'none';
        if (this.direction == 'vertical')
        {
                expandDiv.style.height = '0px';
        }
        else
        {
                expandDiv.style.width = '0px';
        }
        expandDiv.style.visibility = 'visible';
        expandDiv.style.display = 'block';
        expandDiv.style.overflow = 'hidden';
    }

    this.mouseEvent = function (e, div)
    {
        var objectDiv = false;
        for (var i=0; i<this.divList.length; i++)
        {
            if (this.divList[i] == div)
            {
                objectDiv = true;
            }
        }
        if (objectDiv == true)
        {
            var expandDiv = document.getElementById(div);
            if (e.type == 'mouseout')
            {
                if (this.isMouseLeaveOrEnter(e, document.getElementById(div)))
                {
                    this.activeDivName = '';
                    this.closeOthers();
                }
            }
            else
            {
                if (expandDiv.style.display == 'block' && this.type == 'click')
                {
                    this.activeDivName = '';
                    this.closeOthers();
                }
                else
                {
                    if (this.activeDivName != div)
                    {
                        this.activeDivName = div;
                        for (var i=0; i < this.divList.length; i++)
                        {
                            if (this.divList[i] == div)
                            {
                                this.openList[i] = true;
                            }
                        }
                        this.closeOthers();
                        this.getDimensions(expandDiv);

                        if (this.images == true)
                        {
                            document.getElementById(div+'Image').src = this.openImage;
                        }
                        this.expander();
                    }
                }
            }
        }
    }
}

function newsTicker(delay, step, direction, feed) {
    this.newTicker = function(first, back) {
        try {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        }
        catch (e) {
            try {
                xmlDoc = document.implementation.createDocument("", "", null);
            }
            catch (e) {
                alert(e.message);
                return;
            }
        }
        xmlDoc.async = false;
        xmlDoc.load(feed);
        var x = xmlDoc.getElementsByTagName("Article");
        if (this.totalNews == 0) this.totalNews = x.length;
        if (back == true) {
            this.currentNews--;
        }
        else {
            this.currentNews++;
        }
        if (this.currentNews == this.totalNews) this.currentNews = 0;
        if (this.currentNews < 0) this.currentNews = this.totalNews - 1;
        document.getElementById("tickerText").innerHTML = x[this.currentNews].getElementsByTagName("Text")[0].firstChild.nodeValue;
        if (document.getElementById("tickerDate") && x[this.currentNews].getElementsByTagName("Date"))
        {
            document.getElementById("tickerDate").innerHTML = x[this.currentNews].getElementsByTagName("Date")[0].firstChild.nodeValue;
        }
        if (first != true) this.newsTicker.activateTimed();
    }

    this.previous = function() {
        clearInterval(this.tickerTimer);
        var f = this;
        this.tickerTimer = setInterval(function() { f.newTicker(); }, 10000);
        this.newTicker(false, true);
    }

    this.next = function() {
        clearInterval(this.tickerTimer);
        var f = this;
        this.tickerTimer = setInterval(function() { f.newTicker(); }, 10000);
        this.newTicker();
    }
    this.totalNews = 0;
    this.currentNews = -1;
    this.newTicker(true);
    this.newsTicker = new expandObject('timed', delay, step, direction);
    this.newsTicker.addObject('ticker');
    this.newsTicker.maxWidth = document.getElementById('ticker').offsetWidth;
    var f = this;
    this.tickerTimer = setInterval(function() { f.newTicker(); }, 10000);
}

function perform_registration() {
    $("#message").html("&#160;");
    $("#alert_1").html("&#160;");
    $("#alert_2").html("&#160;");
    $("#alert_3").html("&#160;");
    var data = "username="+encodeURIComponent($("input#username").val())+
        "\&password="+ encodeURIComponent($("#password").val())+
        "\&conf_password="+encodeURIComponent($("input#password_conf").val())+
        "\&email="+encodeURIComponent($("input#email").val())+
        "\&conf_email="+encodeURIComponent($("input#email_conf").val());
    $.ajax({
        type: "POST",
        url: url_root + "/index.php?page=register_ajax",
        data: data,
        success: function (msg) {
            if ($("hide", msg).text() == "true") {
                $("#reg_form").hide();
            }
            if ($("message", msg).text()) {
                alert("Setting message");
                $("#register_message").html($("message", msg).text());
            }
            if ($("username", msg).text()) {
                $("#alert_1").html("*"+ $("username", msg).text());
            }
            if ($("password", msg).text()) {
                $("#alert_2").html("*"+ $("password", msg).text());
            }
            if ($("email", msg).text()) {
                $("#alert_3").html("*"+ $("email", msg).text());
            }
        }
    });
}