﻿$(document).ready(function () {
    //    SendCommand("ClientReady", type, "Category", "", "");
    TDM30.ContentService.HelloWorld();
    AdjustLeftMenuBar();
});


function AdjustLeftMenuBar() {
    var LeftColumn = $("div.twoColumnLeft");
    var LeftHeight = LeftColumn.height();
    if (LeftHeight > 0) {
        var RightColumn = $("div.twoColumnRight");

        var RightHeight = RightColumn.height();
        var MaxHeight = LeftHeight;
        if (RightHeight > LeftHeight)
            MaxHeight = RightHeight;
        LeftColumn.height(MaxHeight);
        RightColumn.height(MaxHeight);
    }
};

function ShowSubmenu(pageid) {
    HideSubmenus();
    switch (pageid) {
        case "2": $('ul#PositionSubMenu').removeClass('hide'); break;
        case "3": $('ul#CandidateSubMenu').removeClass('hide'); break;
        default: break;
    }
}

function HideSubmenus() {
    $('ul#PositionSubMenu').addClass('hide');
    $('ul#CandidateSubMenu').addClass('hide');
}

$('p').focus(function () {
    $('ul#PositionSubMenu').removeClass('hide');
});
$('li[pageid=2] a').mouseout(function () {
    $('ul#PositionSubMenu').addClass('hide');
});

function DoCmd(cmd, category, item) {
    switch (cmd) {
        case "EditPositionItem": break;
        case "DeletePositionItem": break;
        default: break;
    }
}


function SendCommand(cmd, type, noun, category, itemid) {
    var callObj = getCallObj(type, noun, cmd, category, itemid, true);
    CommandSend(callObj);
}

function SendCommandNoForm(cmd, type, noun, category, itemid) {
    var callObj = getCallObj(type, noun, cmd, category, itemid, false);
    CommandSend(callObj);
}

//function CommandSend(callObj) {
//    $.ajax({
//        type: 'POST',
//        timeout: 30000,
//                url: "ContentService.aspx",
//        url: "ContentService.aspx",
//        data: callObj,
//        success: AjaxSucceeded,
//        error: AjaxFailed
//    });
//}

function CommandSend(callObj) {
    TDM30.ContentService.DoCommand(callObj, AjaxSucceeded, AjaxFailed);
}

function AjaxSucceeded(result) {
    var rlist = result.split("||");
    for (var r in rlist)
        ApplyResult(rlist[r]);
}

function AjaxFailed(error) {
    alert("Ajax Error : " + error._message + "  " + error._stackTrace);
}

//function DoCmd(cmd, type, category, item) {
//    switch (cmd) {
//        case "ConfirmDetail": ConfirmCommand(); break;
//        case "CancelDetail": CancelCommand(); break;

//        case "ShowRemovedItems": ShowRemovedItems(); break;
//        case "HideRemovedItems": HideRemovedItems(); break;
//        case "AttachItem": SendCommandNoForm("AttachItem", type, "Item", category, item); break;

//        case "HideInactiveCats": ToggleDeleted("HideDeletedCats", "Category"); break;
//        case "ShowInactiveCats": ToggleDeleted("ShowDeletedCats", "Category");  break;
//        case "HideInactiveItems": ToggleDeleted("HideDeletedItems", "Item");  break;
//        case "ShowInactiveItems": ToggleDeleted("ShowDeletedItems", "Item"); break;

//        case "ShowCategory": SendCommandNoForm("Show", type, "Category", category, ""); break;
//        case "EditCategory": SendCommandNoForm("Edit", type, "Category", category, ""); break;
//        case "DeleteCategory": SendCommandNoForm("Delete", type, "Category", category, ""); break;
//        case "ReactivateCategory": SendCommandNoForm("Reactivate", type, "Category", category, ""); break;

//        case "EditItem": SendCommandNoForm("Edit", type, "Item", category, item); break;
//        case "DeleteItem": SendCommandNoForm("Delete", type, "Item", category, item); break;
//        case "VoidItem": SendCommandNoForm("Void", type, "Item", category, item); break;
//        case "ReactivateItem": SendCommandNoForm("Reactivate", type, "Item", category, item); break;
//        case "RemoveItem": SendCommandNoForm("Remove", type, "Item", category, item); break;
//        case "ShowItem": SendCommandNoForm("Show", type, "Item", category, item); break;

//        case "AddCategory": SendCommandNoForm("Add", type, "Category", "", ""); break;
//        case "AddItem": AddItem(); break;
//        default:  break;
//    };
//}

function ShowRemovedItems() {
    type = $('#MainContent_CategoryType').attr('value');
    SendCommandNoForm("ShowRemoved", type, "Item", "", "");
}

function HideRemovedItems() {
    type = $('#MainContent_CategoryType').attr('value');
    SendCommandNoForm("HideRemoved", type, "Item", "", "");
}

function ToggleDeleted(command, subtype) {
    var type = $('#MainContent_CategoryType').attr('value');
    SendCommandNoForm(command, type, subtype, "", "");
}

function AddItem()
{
    var type = $('#MainContent_CategoryType').attr('value');
    var catid = $('#MainContent_CategoryId').attr('value');
    SendCommandNoForm("AddItem", type, "Category", catid, "");
}


//function SendCommand(cmd, type, noun, category, itemid) {
//    var callObj = getCallObj(type, noun, cmd, category, itemid);
//    TDMAssoc_Admin12.AdminContentService.DoCommand(callObj, null, AjaxSucceeded, AjaxFailed);
//}


function getCallObj(type, subtype, cmd, category, itemid, addForm) {
/*    var showcatstate = $('a#btnCatHideDeleted').attr('displaystate');
    var showitemstate = $('a#btnItemHideDeleted').attr('displaystate');*/
    var showcatstate = "";
    var showitemstate = "";
    var formStr="";
/*    if (addForm)
        formStr = getFormStr();
    else formStr = "";*/

    var callstr = "{'type':'" + type + "','subtype':'" + subtype + "','cmd':'" + cmd + "','category':'" + category + "','itemid':'" + itemid + "','showcatstate':'" + showcatstate + "','showitemstate':'" + showitemstate + "','formstr':'" + formStr + "'}";
    try {
        var callObj = eval('(' + callstr + ')');
    }
    catch (e) {
        alert(e.Description);
    };
    return callObj;
}


//function SendConfirmCommand(type, subtype, cmd, category, itemid) {
//    var callObj = getCallObj(type, subtype, cmd, category, itemid);
//    var formStr = getFormStr();
//    TDMAssoc_Admin12.AdminContentService.DoCommand(callObj, formStr, AjaxSucceeded, AjaxFailed);
//}

//var formvalues;

function getFormStr() {
    var formvalues = "";
    var textboxes = "input[group='ajaxfrmdetail']";
    var textareas = "textarea[group='ajaxfrmdetail']";
    var selects = "select[group='ajaxfrmdetail']";
    var hiddens = "input[type='hidden']";
    var radios = "input[type='radio']";

    $(textboxes).filter($("input[type='text']")).each(function () {
        formvalues= addFormPair(formvalues, $(this).attr("ajaxfldname"), $(this).attr("value"));
    });

    $(textareas).each(function () {
        formvalues = addFormPair(formvalues, $(this).attr("ajaxfldname"), $(this).attr("value"));
    });

    $(selects).each(function () {
        formvalues = addFormPair(formvalues, $(this).attr("ajaxfldname"), $(this).attr("value"));
    });

    $(hiddens).not($("#__VIEWSTATE")).not($("#__EVENTARGUMENT")).not($("#__EVENTTARGET")).each(function () {
        formvalues = addFormPair(formvalues, $(this).attr("id"), $(this).attr("value"));
    });

    $(radios).each(function () {
        formvalues = addFormPair(formvalues, $(this).attr("id"), $(this).attr("checked"));
    });

    return formvalues;
}

function addFormPair(fvstring, name, value) {
    var delim;
    if (fvstring.length == 0)
        delim = "";
    else delim = "||";
    fvstring += delim + name + "^^" + value;
    return fvstring;
}






function ApplyResult(rstr) {

    if (rstr.length > 0) {
        var results = rstr.split("^^");

        switch (results[0]) {
            case "HighlightSelection": HighlightSelection(results[1], results[2]); break;
            case "ClearSelection": ClearSelection(results[1]); break;
            case "ShowDeleted": ShowDeleted(results[1]); break;
            case "SetDirty": SetDirty(results[1]); break;
            default: ProcessAjaxCommand(results[0], results[1], results[2], results[3]); break;
        };
    }
}

function HighlightSelection(subtype, id) {
    var allrows = 'a[subtype = ' + subtype + ']';
    $(allrows).parent().parent().removeClass('selected');
    var selected = 'a[arg= ' + id + ']';
    $(allrows).filter(selected).parent().parent().addClass('selected');
}

function ClearSelection(subtype) {
    var allrows = 'a[subtype = ' + subtype + ']';
    $(allrows).parent().parent().removeClass('selected');
}

function SetDirty(state) {
    $('#MainContent_IsDirty').attr('value', state);
}

function ConfirmCommand() {
    var type = $('#MainContent_CategoryType').attr('value');
    var command = $('#MainContent_CommandType').attr('value');
    var catid = $('#MainContent_CategoryId').attr('value');
    var itemid = $('#MainContent_ItemId').attr('value');

    switch (command) {
        case "AddCategory": SendCommand("ConfirmAdd",type, "Category",  "", ""); break;
        case "EditCategory": SendCommand("ConfirmEdit",type, "Category",  catid, ""); break;
        case "EditItem": SendCommand("ConfirmEdit",type, "Item",  catid, itemid, ""); break;
        case "ReactivateCategory": SendCommand("ConfirmReactivate",type, "Category",  catid, "", ""); break;
        case "DeleteCategory": SendCommand("ConfirmDelete", type, "Category", catid, "", ""); break;
        case "AttachItem": SendCommand("ConfirmAttach", type, "Item", catid, itemid); break;
        case "AddItem": SendCommand("ConfirmAddItem", type, "Category", "", ""); break;
        case "DeleteItem": SendCommand("ConfirmDelete", type, "Item", catid, itemid); break;
        case "VoidItem": SendCommand("ConfirmVoid", type, "Item", catid, itemid); break;
        case "ReactivateItem": SendCommand("ConfirmReactivate", type, "Item", catid, itemid); break;
        case "RemoveItem": SendCommand("ConfirmRemove", type, "Item", catid, itemid); break;
        default: break;
    }
}


function CancelCommand() {
    if ($('#MainContent_IsDirty').attr('value') == "true")
        ShowDialog("You are about to discard your changes.  Are you sure?", "You Bet!", "No Way!");
    else
        ClearEdit();
}

function ClearEdit() {
    SendCommandNoForm("ClearEdit", "", "", "", "");
}

function ShowRemaining(elem, max) {
   var element = $("input#" + elem);
    var current = element.val().length;
    var left = max - current;
    element.next().text(left);
    ValidText(element);
}

function ValidText(element) {
    
    if (element.val().length == 0)
        element.addClass('errorinput');
    else
        element.removeClass('errorinput');
}




function MoveToTop(objName) {
    var posy = 0;
    element = document.getElementById(objName);

    while (element != null) {
        posy += element.offsetTop;
        element = element.offsetParent;
    }

//    if (typeof window.pageYOffset != 'undefined') {
//        window.pageYOffset = posy;
//    }

    if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0) {
        document.documentElement.scrollTop = posy;
    }

    else if (typeof document.body.scrollTop != 'undefined') {
        document.body.scrollTop = posy;
    }

}




function DialogClick(answer) {
    if (answer == 'true') {
        ClearEdit();
    }

    HideDialog();
}




function ShowDialog(prompt, btn1Label, btn2Label) {

    $('#theMask').removeClass('wayout');
    $('#theMask').addClass('masking').css('opacity', '0').animate({ 'opacity': '.83' }, 'slow');

    $('#dialogText').text(prompt);
    $('#btnDialog1').text(btn1Label);
    $('#btnDialog2').text(btn2Label);

    positionDialog();
    $('#theDialog').removeClass('wayout');
    $('#theDialog').addClass('showDialog').css('opacity', '0').animate({ 'opacity': '1' }, 'slow');

}

function HideDialog() {
    $('#theDialog').css('opacity', '1').animate({ 'opacity': '0' }, 'slow');
    $('#theMask').css('opacity', '1').animate({ 'opacity': '0' }, 'slow');
    setTimeout(function () {
        $('#theDialog').addClass('wayout');
        $('#theDialog').removeClass('showDialog');
        $('#theMask').addClass('wayout');
        $('#theMask').removeClass('masking');
    }, 500);

}

function positionDialog() {
    var top = ($(window).height() - $('#theDialog').height()) / 2;
    var left = ($(window).width() - $('#theDialog').width()) / 2;
    $('#theDialog')
.css({
    'top': top + $(document).scrollTop(),
    'left': left
});

    var docht = $(document).height();
    $('#theMask').css({ 'height': docht });
}
