
function manager() {

    this.request = null;
    this.page = 1;

    this.init = function() {

        $(document).ready(function() {
            FormHighlight();
            InitWatermarks();
            LoginEnterKeyHandler();
        });

    };
}

var manager = new manager();
manager.init();

/**
*   Catch Console Errors
*/
if (typeof (console) === 'undefined') {
    console = new function() { };
    console.log = function() { };
}

/**
*   Initialize Watermarks On Text Input
*/
function InitWatermarks() {
    $('.watermarked')
            .each(function() {
                if ($(this).val() === $(this).attr('title')) $(this).addClass('watermark');
            })
            .focus(function() {
                if ($(this).val() == $(this).attr('title')) {
                    $(this).val('').removeClass('watermark');
                }
            })
            .blur(function() {
                if ($(this).val() == '') {
                    $(this).addClass('watermark').val($(this).attr('title'));
                }
            }).each(function() {
                if ($(this).val() == '') {
                    $(this).addClass('watermark').val($(this).attr('title'));
                }

            });
}

/**
*   Initialize Form Input Highlights
*/
function FormHighlight() {
    $('input[type="text"], input[type="password"], select, textarea, input[type="checkbox"]').focus(function() {
        if ($(this).attr('type') != 'checkbox') {
            $(this).addClass('focus').prevAll('label:first').addClass('focus');
        }
        else {
            $(this).next('label').addClass('focus');
        }
    });

    $('input[type="text"], input[type="password"], select, textarea, input[type="checkbox"]').blur(function() {
        $('input[type="text"], input[type="password"], select, label, textarea, input[type="checkbox"]').removeClass('focus');
    });
}

/**
*   Add Hash To Url
*/
function AddHash(hash) {
    //pageTracker._trackPageview('/market/' + hash);
    if ('#' + hash == window.location.hash) {
        window.location.hash = new String((new Date().getMilliseconds()));
    }
    window.location.hash = hash;
}

/**
*   Global Validation Summary Display
*/
function ValidateResult(result, success, fail) {

    // Test For Authentication
    if (result.Authenticate && !result.IsAuthenticated)
        window.location = '/Logout?ReturnUrl=' + window.location.href;

    // Hide Success Message
    $('.success').hide();

    // Get Validation Container
    $('.validationSummary').hide();
    var validationSummary = $('.' + result.ValidationGroup + ':last').hide();

    // Clear Validation Container
    validationSummary.empty();

    var length = result.ModelState != null ? result.ModelState.length : 0;

    if (length == 0) {

        if (result.Success) {
            $('.success').html(result.Result).show();
        }

        if (typeof (success) === 'function') {
            success();
        }
    }
    else {


        for (var i = 0; i < length; i++) {
            validationSummary.append('<li>' + result.ModelState[i].Value.Errors[0].ErrorMessage + '</li>');
        }

        validationSummary.parent().show().find('*').show();

        if (typeof (fail) === 'function') {
            fail();
        }
    }
}

function LoginEnterKeyHandler() {
    $('.EnterForm').unbind('keyup').bind('keyup', function(e) {
        if (e.keyCode === 13) {
            $(this).find('a').click();
        }
    });
}

function SetDatePicker() {
    // Fixes Date Picker Bug
    $(".datePicker").datepicker({
        onSelect: function(dateText) {
            document.all ?
                      $(this).get(0).fireEvent("onchange")
                : $(this).change();
        }
    });
}