﻿// Json CommandResult ToString
function JsonCommandResultToHTMLString(data) {
    var html = '<div class="' + SuccessToCSSClass(data) + '"><h4>' + data.Title + '</h4><p>' + data.Message + '</p></div>';
    console.log("JsonCommandResultToHTMLString: html: " + html);
    return html;
}

// Takes a Json CommandResult and returns a CSSClass
function SuccessToCSSClass(data) {
    var cssClass = "";
    if (data.Success) {
        cssClass += "ui-success";
    }
    else {
        cssClass += "ui-error";
    }
    if (data.Warning) {
        cssClass += "-warning";
    }
    return cssClass;
}

// Removes all success/fail based ui classes
function RemoveUISuccessStatusClasses(elementId) {
    $(elementId).removeClass('ui-error');
    $(elementId).removeClass('ui-error-warning');
    $(elementId).removeClass('ui-success');
    $(elementId).removeClass('ui-success-warning');
}









///* Create_OnSuccess
//----------------------------------------------------------*/
//// Handles events after the success of a create
//function OnCreate_Success(data, messageArea, controller, listElement) {
//    AjaxHelper(controller + "/_List", '', listElement, listElement, 'EmbedActionResult');
//}


///* DELETE
//----------------------------------------------------------*/
//// Currenly set to work for rows in tables
//// bug prevents row animation, so animates td then removes tr
//function Delete(controller, id) {
//    if (fitsystems_debug)
//        console.log("Delete: controller: " + controller + "id: " + id);
//    jQuery.ajax({
//        type: 'POST',
//        url: controller+"/Delete",
//        data: { id: id },
//        beforeSend: function () {
//            // this is where we append a loading image
//            if (fitsystems_debug)
//                console.log("beforeSend");
//        },
//        success: function (data) {
//            // successful request; do something with the data
//            if (fitsystems_debug)
//                console.log("Success");
//            if (data.Success) {
//                if (fitsystems_debug)
//                    console.log("The operation was a success, controller: "+controller+" id: "+id);
//                $('#' + controller + "_" + id).find('td').effect("highlight", { color: "#e33535" }, 500).fadeOut(500, function () { $(this).parent().remove(); });
//            }
//            else {
//                if (fitsystems_debug)
//                    console.log("The operation was a failure, controller: " + controller + " id: " + id);
//                $('#' + controller+ "_" + id).find('td').effect("highlight", { color: "#e33535" }, 500);
//                alert(data.Title + ": " + data.Message);
//            }
//        },
//        error: function () {
//            // failed request; give feedback to user
//            if (fitsystems_debug)
//                console.log("Error");
//            alert("Failed to delete reccord");
//        }
//    });
//}

///* AjaxHelper
//----------------------------------------------------------*/
//// for DropDownLists which can't send in selected id themselves
//function DDLAjaxHelper(url, ddlId, returnElementId, returnDisplayType) {
//    console.log("url: " + url + ", ddlId: " + ddlId);
//    var data = { id: $(ddlId).val() };
//    //console.log("id: " + id);
//    AjaxHelper(url, data, returnElementId, returnDisplayType);
//    //console.log("result: " + result);
//}
//// For buttons, and second part of select lists
//function AjaxHelper(url, data, inputElementId, returnElementId, returnDisplayType) {
//    console.log("AjaxHelper: url: " + url + ", returnElementId: " + returnElementId);
//    var idurl = '';
//    var id = data.id;
//    if (id != '') {
//        idurl = "/" + id;
//    }
//    console.log("id: " + id);
//    jQuery.ajax({
//        type: 'POST',
//        url: url + idurl,
//        data: data,
//        beforeSend: function () {
//            if (fitsystems_debug)
//                console.log("AjaxHelper: Before:");
//            // this is where we append a loading image
//            $(returnElementId + "Loading").show();
//        },
//        success: function (data) {
//            if (fitsystems_debug)
//                console.log("AjaxHelper: Success:");
//            if (returnDisplayType == 'EmbedActionResult')
//                EmbedActionResult(inputElementId, returnElementId, data);
//            else if (returnDisplayType == 'EmbedJsonResult') {
//                EmbedJsonResult(inputElementId, returnElementId, data);
//                //SaveIndicatorSuccess(returnElementId, data);
//            }


//            //           else if (returnDisplayType == 'Dialog')
//            //               Dialog(returnElementId, url, data);
//            //           else if (returnDisplayType == 'Prepend')
//            //               Prepend(returnElementId, data);
//            //           else if (returnDisplayType == 'SaveIndicator')
//            // this is wrong, successful ajax doesn't mean entity was successfully saved

//            $(returnElementId + "Loading").hide();
//            AutoInstantEdit();
//        },
//        error: function () {
//            // failed request; give feedback to user
//            if (fitsystems_debug)
//                console.log("AjaxHelper: Error:");
//            if (returnDisplayType == 'Dialog' || returnDisplayType == 'Embed' || returnDisplayType == 'Prepend') {
//                HandleError(url, id, returnElementId);
//                $(returnElementId + "Loading").hide();
//            }
//            else if (returnDisplayType == 'SaveIndicator')
//                SaveIndicatorError(returnElementId, data);
//        }
//    });
//}
//// SaveIndicator
//function SaveIndicatorSuccess(returnElementId, data) {
//    //console.log('returnElementId: ' + returnElementId);
//    $(returnElementId)
//        .animate({ backgroundColor: "#00FF00" }, 500)
//        .slideUp();
//    var inputId = returnElementId.toString().substr(0, (returnElementId.length - 13));
//    //console.log('inputId: ' + inputId);
//    $(inputId).removeClass('InstantEditInput-ActionFailed');
//}
//function SaveIndicatorError(returnElementId, data) {
//    //console.log('returnElementId: ' + returnElementId);
//    $(returnElementId)
//        .animate({ backgroundColor: "#FF0000" }, 500);
//    var inputId = returnElementId.toString().substr(0, (returnElementId.length - 13));
//    //console.log('inputId: ' + inputId);
//    $(inputId).addClass('InstantEditInput-ActionFailed');
//}

////// Prepend
////function Prepend(returnElementId, data) {
////    $(returnElementId).prepend(data);
////}

//// EmbedActionResult
//function EmbedActionResult(inputElementId, returnElementId, data) {
//    if (fitsystems_debug)
//        console.log("In EmbedActionResult Method");
//    if (data.Success) {
//        if (fitsystems_debug)
//            console.log("EmbedActionResult: Request was successful, returnElementId: " + returnElementId);
//        $(returnElementId).html(data);
//    }
//    else {
//        $(returnElementId).html(data);
//        
//        if (fitsystems_debug) {
//            console.log("EmbedActionResult: Request was failure, returnElementId: " + returnElementId);
//        }
//        
//    }
//}

//// EmbedJsonResult
//function EmbedJsonResult(inputElementId, returnElementId, data) {
//    if (fitsystems_debug)
//        console.log("In EmbedJsonResult Method");
//    var html = JsonCommandResultToHTMLString(data);

//    RefreshSlide(returnElementId, html);

//    RemoveUISuccessStatusClasses(inputElementId);
//    $(inputElementId).addClass(SuccessToCSSClass(data));

//    if (data.Success) {
//        if (fitsystems_debug)
//            console.log("EmbedJsonResult: Request was successful, returnElementId: " + returnElementId);
//        $(inputElementId).data('default', $(inputElementId).val());
//        $(inputElementId).animate({ backgroundColor: "#FFFFFF" }, 2000, function () {
//            $(this).css('background-color', '');
//            slideUpAndClear(returnElementId);
//            RemoveUISuccessStatusClasses(inputElementId);
//            $(inputElementId).removeClass('ui-edited');
//        });
//    }
//    else {
//        if (fitsystems_debug)
//            console.log("EmbedJsonResult: Request was failure, returnElementId: " + returnElementId);
//        //$(returnElementId).val(html);
//    }
//}

//// RefreshSlide
//function RefreshSlide(returnElementId, html) {
//    $(returnElementId).slideUp('slow', function () {
//        $(returnElementId).empty().html(html).slideDown('slow');
//    });
//}

//// returnElementId slideUp and clear val
//function slideUpAndClear(returnElementId) {
//    console.log("slideUp");
//    $(returnElementId).slideUp('slow', function () {
//        $(this).empty();
//    });
//}

//// returnElementId slideDown
//function slideDownAddSetVal(returnElementId, html) {
//    console.log("slideDown");
//    $(returnElementId).empty().html(html).slideDown('slow');
//}







//// Dialog
//function Dialog(returnElementId, title, data) {
//    $(returnElementId).empty();
//    $(returnElementId).html(data);
//    $(returnElementId).dialog({
//        title: title,
//        minWidth: 400,
//        minHeight: 300,
//        closeOnEscape: false,
//        open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); },
//        autoOpen: false,
//        modal: true
//    });
//    $(returnElementId).dialog('open');
//}

///* ERROR HANDELING
//----------------------------------------------------------*/
//function HandleError(url, id, returnElementId) {
//    if (fitsystems_debug)
//        console.log("Handle Error: url: " + url + " id: " + id + " returnElementId: " + returnElementId);
//    jQuery(returnElementId).html('<p class="ui-error"><strong>Sorry</strong>, the request to the server failed (' + url + '/' + id + ').</p>');
//}


///* InputInstantEdit
//----------------------------------------------------------*/
//function AutoInstantEdit() {
//    //console.log('AutoInstantEdit Called');
//    $(".InstantEditInput").each(function () {
//        var id = '#' + $(this).attr("id");
//        InputInstantEdit(id);
//        $(this).removeClass('InstantEditInput');
//        $(this).addClass('InstantEditInput-Set');
//    });
//}

//function AllInputInstantEdit(containingId) {
//    TextInputInstantEdit(containingId);
//    SelectInputInstantEdit(containingId);
//}
//function TextInputInstantEdit(containingId) {
//    $(containingId + ' input[type=text]').each(function () {
//        var id = '#' + $(this).attr("id");
//        //console.log('TextInputInstantEdit id: ' + containingId);
//        InputInstantEdit(id);
//    });
//}
//function SelectInputInstantEdit(containingId) {
//    $(containingId + ' select').each(function () {
//        var id = '#' + $(this).attr("id");
//        //console.log('SelectInputInstantEdit id: ' + containingId);
//        InputInstantEdit(id);
//    });
//}
//function InputInstantEdit(inputId) {
//    // debug
//    // $(inputId).css({ backgroundColor: '#aaffaa' });
//    // setup vars

//    var saveIndicatorId = inputId + "SaveIndicator";

//    var substr = inputId.toString().substr(1, inputId.length - 1).split('_');
//    var controller = substr[0];
//    var fieldName = substr[3];

//    var entityIdHiddenInputId = substr[0] + "_" + substr[1] + "_" + substr[2] + "_Id";
//    var entityId = $('#' + entityIdHiddenInputId).val();

//    //console.log("fieldName: " + fieldName + " entityId: " + entityId);
//    // setup input[type=text]

//    $(inputId).focus(function () {
//        //console.log('click id: ' + inputId + ' saveIndicatorId: ' + saveIndicatorId);
//        $(saveIndicatorId)
//                    .animate({ backgroundColor: "#FF0000" }, 1)
//                    .slideDown('fast');
//    });
//    $(inputId).blur(function () {
//        $(saveIndicatorId)
//                    .animate({ backgroundColor: "#f8b003" }, 500);
//        //console.log("fieldName: " + fieldName + " entityId: " + entityId);
//        EditField(controller, fieldName, entityId, $(inputId).val(), '');
//    });

//    EditField(fieldName, entityId, value);

//    $(inputId).after("<div id='" + saveIndicatorId.toString().substr(1, saveIndicatorId.length - 1) + "' class='SaveIndicator'></div>");
//    $(saveIndicatorId).hide();
//}

////function EditField(controller, fieldName, entityId, value) {
////    AjaxHelper(controller + "/EditField", { fieldName: fieldName, id: entityId, value: value }, saveIndicatorId, 'SaveIndicator');
////}

////function EditField(controller, fieldName, entityId, value) {
////    AjaxHelper(controller + "/EditField", { fieldName: fieldName, id: entityId, value: value }, '', 'SaveIndicator');
////}

//function EditField(controller, fieldName, entityId, value, inputElementId, returnElementId, returnDisplayType) {
//    AjaxHelper(controller + "/EditField", { fieldName: fieldName, id: entityId, value: value }, inputElementId, returnElementId, returnDisplayType);
//}

//function EditFieldOf(inputElementId) {
//    var split = inputElementId.split('_');
//    var controller = split[0].toString().substring(1, split[0].toString().length); //removes # from beigining
//    var entityId = split[1];
//    var fieldName = split[3];
//    var value = $(inputElementId).val();
//    console.log("EditFieldOf controller: " + controller + " fieldName: " + fieldName + " entityId: " + entityId + " value: " + value + " inputElementId: " + inputElementId);
//    EditField(controller, fieldName, entityId, value, inputElementId, inputElementId + "_ReturnElement", 'EmbedJsonResult');
//}

