
//Initially set up the jQuery dialog window (http://docs.jquery.com/UI/Dialog)
// divDialogId:     The div id that utilizies the jQuery Dialog
function initializeDialog(divDialogId) {

    $("#" + divDialogId).dialog({
        autoOpen: false,
        bgiframe: true,
        modal: true,
        position: "center",
        resizable: false
    });

}

//Show the jQuery Dialog (http://docs.jquery.com/UI/Dialog)
// divDialogId:     The div id that utilizies the jQuery Dialog
// viewPath:        The view that needs to be loaded in the divDialogId
// data:            Information that needs to be passed to the view being loaded
// callback:        Function to be called once the view has been loaded
// dialogTitle:     Title for the jQuery Dialog
// dialogHeight:    Height for the jQuery Dialog
// dialogWidth:     Width for the jQuery Dialog
function showDialog(divDialogId, viewPath, data, callback, dialogTitle, dialogHeight, dialogWidth) {

    $("#" + divDialogId).html("");

    $("#" + divDialogId).dialog("option", "title", dialogTitle);
    $("#" + divDialogId).dialog("option", "height", dialogHeight);
    $("#" + divDialogId).dialog("option", "width", dialogWidth);

    $("#" + divDialogId).dialog("open");

    $("#" + divDialogId).html("<img src='/Content/Images/ajax-loader.gif' alt='Loading' style=' margin: 0px auto;' /><br />Loading...");
   // $("#" + divDialogId).html("Loading...");
    
    $("#" + divDialogId).load(viewPath, data, callback);

}

//Close the jQuery Dialog (http://docs.jquery.com/UI/Dialog)
// divDialogId:     The div id that utilizies the jQuery Dialog
// divToUpdateID:   The div that needs to have its content updated after the jQuery Dialog is closed
// viewPath:        The view that needs to be loaded in the divToUpdateID
function closeDialog(divDialogId, divToUpdateID, viewPath) {

    $("#" + divDialogId).dialog("close");
    
    //Update a div information if needed
    if ((divToUpdateID != "") && (divToUpdateID != undefined) && (viewPath != "") && (viewPath != undefined)) {
        $("#" + divToUpdateID).load(viewPath);
    }
}
function doLoadingPage() {
    var PageContent = $("#PageContent");
    //PageContent.html("");
    showAnimation(PageContent);
}
//Load the partial view in the PageContent div
// viewPath:        The view that needs to be loaded in the divDialogId
// caller:          The element id that called this function (used for styling the selected item)
// callback:        Function to be called once the view has been loaded
function loadView(viewPath, caller, callback) {

    if (caller == "Search") {
        var searchTerm = document.getElementById('searchBox').value.replace(' ', '_');
        viewPath = viewPath + searchTerm;
    }

    //Change class
    if ($("#" + caller).length > 0) {
        $(".selected").removeClass("selected");
        $("#" + caller).addClass("selected");
    }
    doLoadingPage();
    var PageContent = $("#PageContent");
//    //PageContent.html("");
//    showAnimation(PageContent);

    //Load view
    PageContent.load(viewPath, "", function(responseText, textStatus, XMLHttpRequest) {
        hideAnimation(PageContent); eval(callback);
    });
}

//Textbox element Got Focus
// element:     The Search Field textbox
// originalValue:   The default value that is displayed in the textbox, which is erased on focus
function SearchFieldGotFocusActions(element, originalValue) {
    if (jQuery.trim(element.val()) == originalValue) {
        element.val('');
        element.addClass("searchFieldTextFocus");
    }
}

//Textbox element Lost Focus
// element:     The Search Field textbox
// originalValue:   The default value that is displayed in the textbox, which is shown when the textbox is empty
function SearchFieldLostFocusActions(element, originalValue) {
    if (jQuery.trim(element.val()) == '') {
        element.val(originalValue);
        element.removeClass("searchFieldTextFocus");
    }
}

//String Buffer to make building strings faster
//http://www.softwaresecretweapons.com/jspwiki/javascriptstringconcatenation
function StringBuffer() {
    this.buffer = [];
}

StringBuffer.prototype.append = function append(string) {
    this.buffer.push(string);
    return this;
};

StringBuffer.prototype.toString = function toString() {
    return this.buffer.join("");
};

//Convert a Military Time to Standard
function convertMilitaryToStandard(val) {

    var hours = val.toString().split(":")[0];
    var minutes = val.toString().split(":")[1];

    var dn = "AM";

    if (hours > 12) {
        dn = "PM";
        hours = hours - 12;
    }

    if (hours == 0) {
        hours = 12;
    }

    return hours + ":" + minutes + " " + dn;
}
