﻿// JScript File

/*************** GLOBAL VARIABLES ****************/

top._isResourceModified = false;
var _timerKeyPress = null;

/*************************************************/


/*************** GRID EVENTS *********************/
var _globalRowSelected = null;

function OnRowSelected(rowObject)
{
  _globalRowSelected = rowObject;
  if (window.CustomRowSelected)
    CustomRowSelected(rowObject);
//  else if (window.EditRowSelected)
//    EditRowSelected(rowObject);
}

function OnRowDeselected(rowObject)
{
  if (window.CustomRowDeselected)
    CustomRowDeselected(rowObject);
}

function OnRowDblClick(rowIndex)
{
  if (window.CustomRowDblClick)
    CustomRowDblClick(_globalRowSelected);
}

function GridOpenEditWindow(url, isNew, grid, keyName, modalWidth, modalHeight)
{
  if (!isNew)
    if (!GridCheckSingleSelection(grid))
      return;
      
  OpenModal(url
          , 'CurrentID='+ (isNew ? '-1' : grid.MasterTableView.SelectedRows[0].KeyValues[keyName])
          , modalWidth
          , modalHeight
           );
}

function GridOpenEditInline(grid, openEditFn)
{
  if (!GridCheckSingleSelection(grid))
    return;
    
  openEditFn();
}

function GridCheckSingleSelection(grid)
{
  if ( !grid || grid.MasterTableView.SelectedRows.length == 0 )
  {
    alert('Selezionare l\'elemento da modificare.');
    return false;
  }
  else if (grid.MasterTableView.SelectedRows.length > 1)
  {
    alert('Selezionare un singolo elemento da modificare.');
    return false;
  }
  return true;
}

function GridSelectedRows(grid)
{
  if (!grid)
    return null;
  else
    return grid.MasterTableView.SelectedRows;
}

/*************************************************/


/**************** TOOLBAR EVENTS *****************/

function _toolBar_click_handler(sender, e)
{
  // If exists toolbarbutton function CustomOnClick, it has called
  var _handlerCustomOnClik = eval('window.'+ sender.ToolbarID +'_'+ sender.CommandName);
  if (_handlerCustomOnClik)
    return _handlerCustomOnClik();
    
  return true;
}

/*************************************************/


/**************** MODAL WINDOW *******************/

function OpenModal(url, urlParams, width, height, ClientCallBackFunction)
{
  //Getting rad window manager
  var oManager = GetRadWindowManager();
  //Success. Getting existing window DialogWindow using GetWindowByName
  var oWnd = oManager.GetWindowByName("modalWindow");
  //Success. Setting a size and a Url to the window using its client API before showing
  oWnd.SetSize(width, height);
  // Add params to url and set it
  url += (url.indexOf("?") != -1 ? "&" : "?") + urlParams;
  oWnd.SetUrl(url);
  //Success. Opening window
  oWnd.Show();
  //Success. Calling window RadWindowManager.GetActiveWindow (should return reference to current window
  var oActive = oManager.GetActiveWindow();
}

function OnModalWindowClose(radWindow)
{
  if (window.CustomModalWindowClose)
    CustomModalWindowClose(radWindow);
}

function OnModalWindowClosing()
{
  ChangeControlFocus();
  
  var _doClose = true;
  if (IsResourceModified())
    _doClose = confirm("If you close modal window some data changes will be lost.\nContinue?");
  
  if (_doClose)
  {
    ClearResourceModified();
    GetRadWindow().Close();
  }
}

var _radwindowCloseFunction = null;

function GetRadWindow()
{
  var oManager = top.GetRadWindowManager();
  //Success. Getting existing window DialogWindow using GetWindowByName
  var oWnd = oManager.GetWindowByName("modalWindow");

  if ( _radwindowCloseFunction == null )
    _radwindowCloseFunction = oWnd.Close;
  oWnd.Close = _radwindowCloseFunction;

  // Check if save or delete have raised any errors...
  if (window._raisedError && _raisedError == 1)
    // ... substitutes Close() function with empty function to not permit to close window
    oWnd.Close = function (){ };

  return oWnd;
}

/*************************************************/


/*********** STATUS MESSAGE BAR ******************/
			
function GetStatusBarObj()
{
  return document.getElementById("statusBar");
}

function GetStatusBarContainerObj()
{
  return document.getElementById("statusBarContainer");
}

function DisplayStatusMessage(text, duration)
{
  var _statusBar = GetStatusBarObj();
  var _statusBarContainer = GetStatusBarContainerObj();
  if (_statusBar && _statusBarContainer)
  {
    _statusBar.innerHTML = text.toUpperCase();
    _statusBarContainer.style.display = 'block';
    if (duration)
      setTimeout('HideStatusMessage()', duration * 1000);
  }
}

function HideStatusMessage()
{
  var _statusBarContainer = GetStatusBarContainerObj();
  if (_statusBarContainer)
    _statusBarContainer.style.display = 'none';
}

/*************************************************/


/******* DATE FORMAT VALIDATION ***********/
function OnDateSelected(sender, args)  
{
  if (!(args.NewDate == null && eval(GetDateErrorFlagEval(sender.ID))))
    eval(GetDateErrorFlagEval(sender.ClientID) +' = false;');
}

function OnDateError(sender, args)
{
  eval(GetDateErrorFlagEval(sender.ClientID) +' = true;');
}

function OnDateBlur(sender, args)  
{  
  if (sender.GetTextBoxValue() == "")
    eval(GetDateErrorFlagEval(sender.ClientID) +' = false;');
}

function GetDateErrorFlagEval(dateId)
{
  return 'window.dateErrorFlag_'+dateId+'_dateInput';
}
/*************************************************/


/******* FUNCTION TO SEARCH WITH KEYPRESS ********/

function OnKeyPressToSearch(eventObj, ajaxId, targetId)
{
  if (eventObj.keyCode != 13) return;

  if (_timerKeyPress != null)
    clearTimeout(_timerKeyPress);
    
  _timerKeyPress = setTimeout(ajaxId +".AjaxRequestWithTarget('"+ targetId +"', 'onclick#search');", 500);
}

/*************************************************/


/****** FUNCTION TO SET MODIFIED RESOURCE ********/
function SetResourceModified()
{
  top._isResourceModified = true;
}

function ClearResourceModified()
{
  top._isResourceModified = false;
}

function IsResourceModified()
{
  return top._isResourceModified
}

// Necessary for firefox, it change focus to intercept onchange of input controls
function ChangeControlFocus()
{
  var _txt = document.getElementById("_tmpInput");
  if (!_txt)
  {
    _txt = document.createElement("input");
    _txt.type = "text";
    _txt.id = "_tmpInput";
    document.forms[0].appendChild(_txt);
  }
  _txt.style.display = "block";
  _txt.focus();
  _txt.style.display = "none";
}

/*************************************************/


/*********** AJAXMANAGER CLIENT EVENTS ***********/

  function OnAjaxManagerRequestStarted(ajaxManager, eventArgs)
  {
    // Disable ajax behavior to export grid
    if (eventArgs.EventArgument.indexOf("excelexport") != -1 ||
        eventArgs.EventArgument.indexOf("wordexport") != -1 ||
        eventArgs.EventArgument.indexOf("pdfreport") != -1 ||
        eventArgs.EventArgument.indexOf("pdfexport") != -1)
       eventArgs.EnableAjax = false;
  }

/*************************************************/

