﻿var ozDocumentReady = false;
var ozGoogleReady = false;
var ozGoogleRetryDelay = 300;

function isParamDef(param) {
    if (param = undefined)
        return false;
    if (param = null)
        return false;
    return true;
}
function statusDialogShowByTitle(popupTitle) {
    modalDialogShow($j('#dialogStatus'), popupTitle);
}
function statusDialogShowByTitleAndText(popupTitle, text, awidth, aheight) {
    $j('#dialogStatus').html(text);
    modalDialogShow($j('#dialogStatus'), popupTitle, awidth, aheight);
}
function modalDialogShowSuccess() {
    statusDialogShowByTitleAndText('Success', 'Operation Successfully completed!', 300, 300);
}
function modalDialogShowFailed() {
    statusDialogShowByTitleAndText('Failed', 'Operation Failed!', 300, 300);
}
function modalDialogShow(container, popupTitle, awidth, aheight, closeFn) {
    awidth = awidth || 800;
    aheight = aheight || 600;
    container.dialog('destroy');
    container.dialog({
        modal: true,
        width: awidth,
        height: aheight,
        title: popupTitle,
        buttons:
                {
                    'Close this Window': function() {
                        $j(this).dialog('close');
                    }
                },
        close: function() {
            if (typeof closeFn == 'function') {
                closeFn();
            }
        }
    });
    container.dialog('open');
}
function modalDialogWithRedirect(container, popupTitle, redirectUrl) {

    container.dialog('destroy');
    container.dialog({
        close: function(event, ui) {
            window.location = redirectUrl;
        },
        modal: true,
        width: 400,
        height: 200,
        title: popupTitle,
        buttons:
			    {
			        ' Ok ': function() {
			            $j(this).dialog('close');
			            window.location = redirectUrl;
			        }
			    }
    });


    container.dialog('open');
}
function modalDialogConfirmation(container, popupTitle, OkAction, CancelAction, width, height) {
    width = width || 400;
    height = height || 200;
    container.dialog('destroy');
    container.dialog({
        modal: true,
        width: width,
        height: height,
        title: popupTitle,
        buttons:
			    {
			        ' Ok ': function() {
			            $j(this).dialog('close');
			            if (isParamDef(OkAction))
			                OkAction(); ;
			        },
			        ' Cancel ': function() {
			            $j(this).dialog('close');
			            if (isParamDef(CancelAction))
			                CancelAction();
			        }
			    }
    });
    container.dialog('open');
}

function validatorValid(name) {
    var validator = document.getElementById(name);
    if (validator != null) {
        ValidatorValidate(validator);
        if (validator.isvalid)
            return true;
        else
            return false;
    };
}

function ManualGUnload() {
    if (typeof GUnload != 'undefined')
        GUnload();
}
function fireGoogleAPILoaded() {
    ozGoogleReady = true;
}
function fireDocumentLoaded() {
    ozDocumentReady = true;
}
function queueGoogleTask(task) {
    if (ozGoogleReady == false) {
        setTimeout(function() { queueGoogleTask(task) }, ozGoogleRetryDelay);
    }
    else {
        eval(task);
    }
}
function insertAtCursor(myField, myValue) {
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
    else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)
        + myValue
        + myField.value.substring(endPos, myField.value.length);
    } else {
        myField.value += myValue;
    }
}
function keywordInsert(keyword) {
    var currentValue = $j('textarea').val();
    $j('textarea').val(currentValue + keyword);
}

function postFeedback(message) {
    while (message.indexOf('\n') != -1)
        message = message.replace('\n', ' ');
    $j.ajax({
        type: "POST",
        url: "/feedback.ashx",
        data: { 'message': message },
        dataType: "html",
        success: function(layout) {
            statusDialogShowByTitleAndText('Thank you!', 'Thank you for your feedback!', 300, 300);
        }
    });
}

function validateEmpty(tbName, errorLabel, errorMsg) {
    if (tbName.val().trim().length == 0) {
        errorLabel.html(errorMsg);
        return false;
    }
    else {
        errorLabel.html('');
        return true;
    }
}

function validateCompare(tbName1, tbName2, errorLabel, errorMsg) {
    if (tbName1.val() != tbName2.val()) {
        errorLabel.html(errorMsg);
        return false;
    }
    else {
        errorLabel.html('');
        return true;
    }
}

function validateEmail(tbEmail, errorLabel, errorMsg) {
    var pattr = /\w+([-+.\u0027]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/gi;
    if (tbEmail.val().trim().match(pattr) == null) {
        errorLabel.html(errorMsg);
        return false;
    }
    else {
        errorLabel.html('');
        return true;
    }
}

function validateNumeric(tbName, errorLabel, errorMsg) {
    var pattr = /^-{0,1}\d*\.{0,1}\d+$/gi;
    alert(tbName.val().trim().match(pattr));
    if (tbName.val().trim().match(pattr) == null) {
        errorLabel.html(errorMsg);
        return false;
    }
    else {
        errorLabel.html('');
        return true;
    }
}

(function($) {
    $.fn.tabSlideOut = function(callerSettings) {
        var settings = $.extend({
            tabHandle: '.handle',
            speed: 300,
            action: 'click',
            tabLocation: 'left',
            topPos: '200px',
            leftPos: '20px',
            fixedPosition: false,
            positioning: 'absolute',
            pathToTabImage: null,
            imageHeight: null,
            imageWidth: null,
            onLoadSlideOut: false
        }, callerSettings || {});

        settings.tabHandle = $(settings.tabHandle);
        var obj = this;
        if (settings.fixedPosition === true) {
            settings.positioning = 'fixed';
        } else {
            settings.positioning = 'absolute';
        }

        //ie6 doesn't do well with the fixed option
        if (document.all && !window.opera && !window.XMLHttpRequest) {
            settings.positioning = 'absolute';
        }



        //set initial tabHandle css

        if (settings.pathToTabImage != null) {
            settings.tabHandle.css({
                'background': 'url(' + settings.pathToTabImage + ') no-repeat',
                'width': settings.imageWidth,
                'height': settings.imageHeight
            });
        }

        settings.tabHandle.css({
            'display': 'block',
            'textIndent': '-99999px',
            'outline': 'none',
            'position': 'absolute'
        });

        obj.css({
            'line-height': '1',
            'position': settings.positioning
        });


        var properties = {
            containerWidth: parseInt(obj.outerWidth() + 150, 10) + 'px',
            containerHeight: parseInt(obj.outerHeight(), 10) + 'px',
            tabWidth: parseInt(settings.tabHandle.outerWidth(), 10) + 'px',
            tabHeight: parseInt(settings.tabHandle.outerHeight(), 10) + 'px'
        };

        //set calculated css
        if (settings.tabLocation === 'top' || settings.tabLocation === 'bottom') {
            obj.css({ 'left': settings.leftPos });
            settings.tabHandle.css({ 'right': 0 });
        }

        if (settings.tabLocation === 'top') {
            obj.css({ 'top': '-' + properties.containerHeight });
            settings.tabHandle.css({ 'bottom': '-' + properties.tabHeight });
        }

        if (settings.tabLocation === 'bottom') {
            obj.css({ 'bottom': '-' + properties.containerHeight, 'position': 'fixed' });
            settings.tabHandle.css({ 'top': '-' + properties.tabHeight });

        }

        if (settings.tabLocation === 'left' || settings.tabLocation === 'right') {
            obj.css({
                'height': properties.containerHeight,
                'top': settings.topPos
            });

            settings.tabHandle.css({ 'top': 0 });
        }

        if (settings.tabLocation === 'left') {
            obj.css({ 'left': '-' + properties.containerWidth });
            settings.tabHandle.css({ 'right': '-' + properties.tabWidth });
        }

        if (settings.tabLocation === 'right') {
            obj.css({ 'right': '-' + properties.containerWidth });
            settings.tabHandle.css({ 'left': '-' + properties.tabWidth });

            $('html').css('overflow-x', 'hidden');
        }

        //functions for animation events

        settings.tabHandle.click(function(event) {
            event.preventDefault();
        });

        var slideIn = function() {
            if (settings.tabLocation === 'top') {
                obj.animate({ top: '-' + properties.containerHeight }, settings.speed).removeClass('open');
            } else if (settings.tabLocation === 'left') {
                obj.animate({ left: '-' + properties.containerWidth }, settings.speed).removeClass('open');
            } else if (settings.tabLocation === 'right') {
                obj.animate({ right: '-' + properties.containerWidth }, settings.speed).removeClass('open');
            } else if (settings.tabLocation === 'bottom') {
                obj.animate({ bottom: '-' + properties.containerHeight }, settings.speed).removeClass('open');
            }

        };

        var slideOut = function() {
            if (settings.tabLocation == 'top') {
                obj.animate({ top: '-3px' }, settings.speed).addClass('open');
            } else if (settings.tabLocation == 'left') {
                obj.animate({ left: '-3px' }, settings.speed).addClass('open');
            } else if (settings.tabLocation == 'right') {
                obj.animate({ right: '-3px' }, settings.speed).addClass('open');
            } else if (settings.tabLocation == 'bottom') {
                obj.animate({ bottom: '-3px' }, settings.speed).addClass('open');
            }
        };

        var clickScreenToClose = function() {
            return;
            obj.click(function(event) {
                event.stopPropagation();
            });

            $(document).click(function() {
                slideIn();
            });
        };

        var clickAction = function() {
            settings.tabHandle.click(function(event) {
                if (obj.hasClass('open')) {
                    slideIn();
                } else {
                    slideOut();
                }
            });

            clickScreenToClose();
        };

        var hoverAction = function() {
            obj.hover(
                function() {
                    slideOut();
                },

                function() {
                    slideIn();
                });

            settings.tabHandle.click(function(event) {
                if (obj.hasClass('open')) {
                    slideIn();
                }
            });
            clickScreenToClose();

        };

        var slideOutOnLoad = function() {
            slideIn();
            setTimeout(slideOut, 500);
        };

        //choose which type of action to bind
        if (settings.action === 'click') {
            clickAction();
        }

        if (settings.action === 'hover') {
            hoverAction();
        }

        if (settings.onLoadSlideOut) {
            slideOutOnLoad();
        };

    };
})(jQuery);