﻿$(function () {
    $('div.icon')
		.mouseenter(function () {
		    $(this).addClass('ui-state-hover');
		})
		.mouseleave(function () {
		    $(this).removeClass("ui-state-hover");
		});
});

 jQuery.fn.sort = function () {
    return this.pushStack([].sort.apply(this, arguments), []);
};

TM = {};

TM.emptyGuid = function () {
    return '00000000-0000-0000-0000-000000000000';
}

TM.newGuid = function () {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

TM.alert = function (title, message, onOk) {

    var dialog = $("<div/>").attr('id', 'alert_' + TM.newGuid()).attr("title", title).appendTo($('body'));
    $("<span/>").text(message).appendTo(dialog);

    dialog.dialog({
        resizable: false,
        modal: true,
        buttons: {
            "OK": function () { $("#" + this.id).dialog("close"); $("#" + this.id).remove(); if (onOk != null) onOk(); }
        }
    });
}

TM.confirm = function (title, message, onOk, onCancel) {
    var dialog = $("<div/>").attr('id', 'confirm_' + TM.newGuid()).attr("title", title).appendTo($('body'));

    $("<span/>").text(message).appendTo(dialog);

    dialog.dialog({
        resizable: false,
        modal: true,
        buttons: {
            "OK": function () { $("#" + this.id).dialog("close"); $("#" + this.id).remove(); if (onOk != null) onOk(); },
            Cancel: function () { $("#" + this.id).dialog("close"); $("#" + this.id).remove(); if (onCancel != null) onCancel(); }
        }
    });
}

function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

$(function () {
    // set up the maximum characters for text input fields
    $('.char-mgmt input[type="text"]').each(function () {
        $(this).next().text('Maximum ' + $(this).attr('maxlength') + ' characters');
    });

    // update the remaining characters on keyup
    $('.char-mgmt input[type="text"]').keyup(function () {
        var el = $(this);
        var ml = el.attr('maxlength');
        var l = el.val().length;

        if (l == 0) {
            el.next().text('Maximum ' + ml + ' characters');
        }
        else {
            el.next().text((ml - l) + ' characters remaining');
        }
    });

    setClearRequiredFields();

    //$('button').button();
});

function setClearRequiredFields() {
    $('div.form-field textarea, input[type="text"], input[type="password"], input[type="file"], input[type="submit"], select, div').mousedown(function () {
        $(this).removeClass('required');
        clearMessages();
    });

    $('div.form-field textarea, input[type="text"], input[type="password"], input[type="file"], input[type="submit"], select').focus(function () {
        $(this).removeClass('required');
        clearMessages();
    });

    // for removing required class on the the checkboxlist and radiobuttonlist parent table
    $('div.form-field input[type="checkbox"]').focus(function () {
        $(this).parent().parent().parent().parent().removeClass('required');
        clearMessages();
    });
}

function updateRemainingChars() {
    $('.char-mgmt input[type="text"]').each(function () {
        var el = $(this);
        var ml = el.attr('maxlength');
        var l = el.val().length;

        if (l == 0) {
            el.next().text('Maximum ' + ml + ' characters');
        }
        else {
            el.next().text((ml - l) + ' characters remaining');
        }
    });
}

function showLoading() {
    $("#ajax-loader").css('display', 'inline-block');
}

function hideLoading() {
    $("#ajax-loader").css('display', 'none');
}

function clearMessages() {
    $('.error').html('');
    $('.message').html('');
}

function clearCheckboxLabelStatus() {
    $('div.status').each(function () {
        $(this).html('');
    });
}

//function setClearMessageEvents() {
//    $('.form-field').live('click', function () {
//        clearMessages();
//    });
//    $('.iframe-uploader').live('focus', function () {
//        clearMessages();
//    });
//}

function clearRequiredFields() {
    $('.form-field textarea, input[type="text"], input[type="password"], input[type="file"], input[type="submit"], select').each(function () {
        $(this).removeClass('required');
    });
}

function resetForm() {
    $('.form-field textarea, input[type="text"], input[type="password"], input[type="file"]').each(function () {
        $(this).val('');
    });

    $('.form-field select').each(function () {
        $(this).get(0).selectedIndex = 0;
    });

    $('.form-field input[type="checkbox"]').each(function () {
        $(this).attr('checked', false);
    });

    updateRemainingChars();
}

//// format the TM ranking
//function formatTMRank(tm) {
//    // convert to string
//    tm += '';

//    if (tm.indexOf('.00') > -1 || tm == '0') {
//        tm += '.00';
//    }

//    return tm;
//}

//// format the NTRP ranking
//function formatNtrp(ntrp) {
//    // convert to string
//    ntrp += '';

//    if (ntrp.indexOf('.0') > -1 || ntrp == '0') {
//        ntrp += '.0';
//    }

//    return ntrp;
//}

function getPaymentMethodText(paymentMethodID) {
    var paymentMethod;

    switch (paymentMethodID) {
        case 1:
            paymentMethod = 'PayPal';
            break;
        case 2:
            paymentMethod = 'Check';
            break;
        case 3:
            paymentMethod = 'Cash';
            break;
        case 4:
            paymentMethod = 'Credits';
            break;
    }

    return paymentMethod;
}

function createButton(cls, html) {
    var a = $('<a />').attr('href', 'javascript:void(0);').addClass(cls);
    var em = $('<em />').appendTo(a);
    var b = $('<b />').html(html).appendTo(em);

    return a;
}

function toggleContent() {
    // The item clicked is the title div... get this parent (the overall container) and toggle the content within it.
    $(".collapsible-content", $(this).parent()).slideToggle();

    if ($(this).hasClass('collapse')) {
        $(this).removeClass('collapse');
        $(this).addClass('expand');
    }
    else {
        $(this).addClass('collapse');
        $(this).removeClass('expand');
    }
}

function formatTelephoneNumber(telephoneNumber) {
    var formattedTelephoneNumber;

    if (telephoneNumber.length == 10) {
        formattedTelephoneNumber = '(' + telephoneNumber.substr(0, 3) + ') ' + telephoneNumber.substr(3, 3) + '-' + telephoneNumber.substr(6, 4);

        return formattedTelephoneNumber;
    }
    else {
        return telephoneNumber;
    }
}

function replaceLineBreaks(str) {
    if (str.indexOf('\r') == -1) {
        return str.replace(new RegExp('\\n', 'g'), '<br />')
    }
    else {
        return str.replace(new RegExp('\\r\\n', 'g'), '<br />')
    }
}

function blockUI() {
    $.blockUI({ message: '<h3><img src="/Images/busy.gif" /> Processing...</h3>' });
}

function unblockUI() {
    $.unblockUI();
}
