﻿/*
    Telerik RadWindow Client Side Dialog Functions
*/
var radGrid; // Filled by the OnGridCreated() client side event embedded in the Dining.aspx template
var activeCommandButton;
var activeTableView;
var activeRowObject;
var isItemInserted;
var editIndexesLength;
var activeDataItem;



//  ***  Confirm  *******************************************************************/

// This function is the callback put in place by the RadWindowManager's radconfirm call that is called using the
// "confirmAndFireItemCommand()" function below
function confirmCallBackFn(arg) {
    if (arg == true) {      
        var isInserting = radGrid.get_masterTableView().get_isItemInserted();
        var editIndexCount = radGrid._editIndexes.length;   
        var detailTablesArray = radGrid.get_detailTables();
        var detailTable = detailTablesArray[3];
        
        // We are either in Update or Insert mode
        if (editIndexCount > 0)
        {
            detailTable.fireCommand("UpdateFoodMenuItem");          
        }
        else
        {
            detailTable.fireCommand("InsertFoodMenuItem");   
        }
       
    }
}


function confirmFn() {
    if (confirm("Are you sure?")) {
        alert("confirm returned the following result: true");
    }
    else {
        alert("confirm returned the following result: false");
    }
}



function confirmAndFireItemCommand(customTitle, button) {
    activeCommandButton = button;
    return radconfirm('<h3><span style="color: #333399;">' + customTitle + '?</span></h3>', confirmCallBackFn, 330, 100, null, 'Confirmation');
}




 //  ***  Prompt  *******************************************************************/
function promptCallBackFn(arg) {
    radalert("radprompt returned the following result: <em><span style='color: #ff0000;'><h3>" + arg + "</h3></span></em>", null, null, "Result");
}


function promptFn() {
    var browserprompt = prompt('What is the answer of life, Universe and everything?', 42);
    alert("Prompt returned the following result: " + browserprompt);

}


//  ***  Open  *******************************************************************/
function OpenAlert() {
    radalert('<em>Welcome to <strong>RadWindow</strong>!</em>', 330, 100, "RadAlert custom title");
    return false;
}

function OpenConfirm(customTitle) {
    return radconfirm('<h3><span style="color: #333399;">' + customTitle + '?</span></h3>', confirmCallBackFn, 330, 100, customTitle);
    return false;
}

function OpenPrompt() {
    radprompt('<em><span style="color: #000099;">Please enter a number in the textbox below:</span></em>', promptCallBackFn, 330, 100, null, "RadPrompt custom title", "42");
    return false;
}


function OpenWindow() {
    var wnd = window.radopen("http://www.google.com", null);
    wnd.setSize(400, 400);
    return false;
}

function controlWindowButtons() {
    var oManager = GetRadWindowManager();
    var selitem = null;
    var e = document.forms[0].elements["RadioButtonList1"];
    for (var i = 0; i < e.length; i++) {
        if (e[i].checked) {
            selitem = e[i].value;
        }
    }
    eval("oManager." + selitem);
}


//function GridCreated(sender, args) {
//    radGrid = sender;
//    isItemInserted = radGrid.get_masterTableView().get_isItemInserted();
//    editIndexesLength = radGrid._editIndexes.length;  
//    //alert("Inserting:" + radGrid.get_masterTableView().get_isItemInserted());
//    //alert("editItems:" + radGrid._editIndexes.length);  
//    //activeTableView = args.get_tableView();
//}
//  
//function HierarchyExpanded(rowObj) {
//    activeRowObject = rowObj;

//    activeDataItem = radGrid.get_masterTableView().get_gridDataItem;
//    //activeTableView = document.getElementById('ctl00_ContentPlaceHolder1_FoodMenuItems');
//    activeTableView = activeRowObject.get_parentView; //activeRowObject.get_parent;
//    
//}


//function GridRowSelected(rowObj) {
//    activeRowObject = rowObj;
//    //activeTableView = document.getElementById('ctl00_ContentPlaceHolder1_FoodMenuItems');
//    activeTableView = activeRowObject.get_parentView; //activeRowObject.get_parent;
//}



//function confirmCallBackFn(arg) {
//    if (radGrid != null) {
//        var grid2 = $find("ctl00_ContentPlaceHolder1_grdFoodMenuListing");
//        var clientState = radGrid.get_clientState();
//        //  var txtTypeValue = document.getElementById('ctl00_ContentPlaceHolder1_grdFoodMenuListing_text');

//        var ddlType = document.getElementById('<%= grdFoodMenuListing.ClientID %>_ClientState');
//        var saveButton2 = document.getElementById('<%= btnSave.ClientID %>');

//        var saveButton = $find('ctl00_ContentPlaceHolder1_btnSave');

//        if (clientState != null && clientState != "") {
//            hasInitialFocus = (clientState == "Focused");
//            // this.set_ClientState(null);
//        }


//        var foodMenuTableView = $find('FoodMenuItems');


//        if (arg == true) {
//            // var tableName = radGrid.get_name();
//            var buttonValue = activeCommandButton.Value;
//            if (buttonValue == 'Update') {
//                var activeTableView = radGrid.get_detailTables()[2];
//                activeTableView.fireCommand("Update", 2);
//                //  var detailTablesArray = grid.get_detailTables();
//                //if (detailTablesArray != null && detailTablesArray != "") {
//                //     detailTablesArray[0][0].fireCommand("Update", 0);
//            }
//        }
//        //   var stateStr = radGrid.get_clientState();
//        //radGrid.get_masterTableView().fireCommand("Update", 0);
//    }
//        return arg;
//}



//function confirmCallBackFn(arg, button) {
//    //radGrid = $find("<%=grdFoodMenuListing.ClientID %>");
//    if (radGrid != null) {
//        if (arg == true) {

//            if (activeCommandButton.value == 'Update') {
//                for (i = 0; i < radGrid.get_detailTables().length; i++) {
//                    var detailTable = radGrid.get_detailTables()[i];
//                    var tableName = detailTable.get_name();
//                    if (tableName == 'FoodMenuItems') {
//                        detailTable.fireCommand("Update", 1);
//                        break;
//                    }
//                }
//            }
//        }
//    }
//    return false;

//    // var detailTablesArray = radGrid.get_detailTables();
//    //   if (detailTablesArray != null && detailTablesArray != "") {
//    //     detailTablesArray[2].fireCommand("Update", 0);

//    //                       
//    //                if (activeTableView != null && activeTableView != "") {
//    //                    activeTableView.fireCommand("Update", 0);
//    //                }
//}




//function confirmCallBackFn(arg) {
//    if (arg == true)
//        radGrid.get_masterTableView().fireCommand("PerformInsert", "");
//}   


//function confirmCallBackFn(arg) {
//  //  radalert("radconfirm returned the following result: <em><span style='color: #ff0000;'><h3>" + arg + "</h3></span></em>", null, null, "Result");
//    // return arg;
//    if (arg == true) {
//      //  var masterTable = $find("<%= grdFoodMenuListing.id %>").get_masterTableView();

//        var gridObject = $find('<%=grdFoodMenuListing.ClientID %>');
//        var masterTable = gridObject.get_masterTableView();  

//        masterTable.fireCommand("PerformInsert", "");
//    }

//}



//function confirmCallBackFn(arg) {
//    if (arg == true)
//        radGrid.get_masterTableView().fireCommand("PerformInsert", "");
//}   
