﻿
Element.implement({
    show: function() {
        this.setStyle('display', 'block');
    },
    hide: function() {
        this.setStyle('display', 'none');
    },
    isVisible: function() {
        return !this.hasClass('Hide');
    },
    toggle: function(){
		return this[this.isVisible() ? 'hide' : 'show']();
	},
    collapsibleBox: function() {
        return this.retrieve('box');
	},
	swapClass: function(remove, add) {
		return this.removeClass(remove).addClass(add);
	},
	ShowSmallLoad: function() {
	    this.set('html', '<img class="Middle" style="padding:0px 5px 0 0;" src=\'/Images/load2.gif\' />');
    }
	
});

window.addEvent('domready', function() {

});

function Authenticate(element, link) {
    new Request.JSON({
        url: '/Controls/Ajax/Authenticate.ashx',
        method: 'post',
        noCache: true,
        onSuccess: function(jsonObj) {
            if (jsonObj.userCorrect)
                eval(link.href);
            else {
                $('LoginError').show();
            }
        }
    }).send('user=' + $$('#' + element + ' input[type=text]')[0].get('value').replace("+", "%2B") + '&pass=' + $$('#' + element + ' input[type=password]')[0].get('value').replace("+", "%2B"));
    return false;
}

function TableHovers(ClassName) {
    $$('.' + ClassName).each(function(item, index) {
        item.store('back', item.getStyle('background-color'));
        item.addEvents({
            'mouseover': function() {
                    item.setStyle('background-color', '#cccccc');
            },
            'mouseout': function() {
                    if (index % 2 == 1)
                        item.setStyle('background-color', item.retrieve('back'));
                    else
                        item.setStyle('background-color', item.retrieve('back'));
            }
        });
    });
}

function ToggleTabs(ulID, AjaxUrl, UpdateDiv, LoadingID, index, OnCompleteScript) {
    $$('#' + ulID + ' li').each(function(item, index2) {
        if (index == index2)
            item.addClass('Selected');
        else
            item.removeClass('Selected');
    });

    $(UpdateDiv).show();
    $(UpdateDiv + '_TabContent').hide();
    
    LoadTabPage(AjaxUrl, UpdateDiv, LoadingID, OnCompleteScript);
}

function ToggleTabsNoXHR(ulID, UpdateDiv, index, OnCompleteScript) {
    $$('#' + ulID + ' li').each(function(item, index2) {
        if (index == index2)
            item.addClass('Selected');
        else
            item.removeClass('Selected');
    });

    $(UpdateDiv).hide();
    $(UpdateDiv + '_TabContent').show();

    $(UpdateDiv + '_TabContent').getChildren().each(function(item, index2) {
        if (item.get('id') == (UpdateDiv + '_' + index))
            item.show();
        else
            item.hide();
    });

    eval(OnCompleteScript);
}

function LoadTabPage(AjaxUrl, UpdateDiv, LoadingID, OnCompleteScript) {

    if (!AjaxUrl.contains('PlaceHolder.ashx')) {

        new Request.HTML({
            noCache: true,
            onRequest: function() {
                $(LoadingID).addClass('Loading');
            },
            onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript) {
                $(UpdateDiv).set('html', responseHTML);
                $(LoadingID).removeClass('Loading');
                eval(OnCompleteScript);
            }
        }).get(AjaxUrl);
    }
    else {
        eval(OnCompleteScript);
    }
}

function LoadLightBoxWithXHR(innerWidth, url) {
    $('LightBoxContent').load(url);
    LoadLightBox(innerWidth, element);
}

function LoadLightBox(innerWidth, element) {
    //$('LightBox').set('opacity', .5);
    var scroll = window.getScrollSize();
    var scroll2 = window.getScroll();

    $('LightBox').setStyles({
        opacity: .7,
        background: '#000000',
        width: scroll.x,
        height: scroll.y,
        position: 'absolute',
        top: 0,
        left: 0,
        display: 'block'
    });

    $('LightBox').store('popper', element);

    $(element).setStyles({
        top: scroll2.y + 70,
        width: innerWidth,
        left: (scroll.x - innerWidth) / 2,
        display: 'block',
        zindex: 10, 
        position: 'absolute'
    });
}

function HideLightBox() {
    $('LightBox').setStyle('display', 'none');
    $($('LightBox').retrieve('popper')).setStyle('display', 'none');
}

function loadInnerContent(what, where) {
    
    //$(where).innerHTML ="<div style='height:500px;'><p style='text-align : center;'><img src='/Lib/Images/ajax-loader.gif'></p></div>";
	$("ajaxLoading").setStyle('visibility', 'visible');

    new Request.HTML({url: what, 

        onSuccess: function(html) {

                $(where).set('text', '');

                $(where).adopt(html);
                
                $("ajaxLoading").setStyle('visibility', 'hidden');
        },

        onFailure: function() {

                $(where).set('text', 'Page could not load.  Please try clicking on the link again.');
                
                $("ajaxLoading").setStyle('visibility', 'hidden');

        }

    }).send();
    }
