﻿// *******************************************************************
// Description  Returns the first element with the passed name.
// Parameters   id      The element id
// *******************************************************************
function getById(id) {
  var element = _aspxGetElementById(id);
  if (element == null) {
    element = window[id];
  }
  return element;
}

// *******************************************************************
// Description  Updates the value text of the passed element.
// Parameters   id      The element id
//              value   The element value
// *******************************************************************
function updateElementValue(id, value) {
  var element = getById(id);
  if (element != null) {
    element.value = value;
  }
}

// *******************************************************************
// Description  Updates the fore color of the passed element.
// Parameters   id      The element id
//              value   The element value
// *******************************************************************
function updateElementForeColor(id, value) {
  var element = getById(id);
  if (element != null) {
    if (typeof (element.GetMainElement) != 'undefined') {
      var obj = element.GetMainElement();
      if (obj != null) {
        obj.style.color = value;
      }
    }
    else {
      element.style.color = value;
    }
  }
}

// *******************************************************************
// Description  Updates the value and inner text of the passed element.
// Parameters   id      The element id.
//              value   The element inner html value.
// *******************************************************************
function updateElementInnerHtml(id, value) {
  var element = getById(id);
  if (element != null) {
    element.innerHTML = value;
  }
}

// *******************************************************************
// Description  Updates the validation settings of a DevExpress text box.
// Parameters   clientId           The textbox client id.
//              errorRegExPattern  The validation regular expression.
//              errorRegExMessage  The validation error message.
// *******************************************************************
function updateDXTextBoxValidationSettings(clientId, errorRegExPattern, errorRegExMessage) {
  var element = getById(clientId);
  if (element != null) {
    // Reinitialize coordinate validation.
    var dxo = new ASPxClientTextBox(clientId);
    // The events must be reassigned.
    dxo.GotFocus.AddHandler(function(s, e) { gotFocusNumeric(s, e, 0); });
    dxo.LostFocus.AddHandler(function(s, e) { lostFocusNumeric(s, e, 0, true); });
    dxo.KeyPress.AddHandler(function(s, e) { return keyPressNumeric(s, e, 0); });
    // Sets the new validation.
    dxo.customValidationEnabled = true;
    dxo.isValid = true;
    dxo.errorDisplayMode = "i";
    dxo.validationPatterns = [new ASPxRegularExpressionValidationPattern(errorRegExMessage, errorRegExPattern)];
    dxo.SetFocus();
    //Reinitialize and set control to window.
    dxo.InlineInitialize();
    window[clientId] = dxo;
  }
}



// *******************************************************************
// Description  Updates the validation settings of a DevExpress text box.
// Parameters   clientId        The textbox client id.
//              direction       The sort direction.
// *******************************************************************
function updateDXListBoxSortOrder(clientId, direction) {
  var element = window[clientId];
  if (element != null) {
    var itemToMoveDown;
    var itemToMoveUp;
    var selectedPos = element.GetSelectedIndex();
    var count = element.GetItemCount();
    if (direction == 'up') {
      if (selectedPos == 0) {
        itemToMoveDown = element.GetItem(selectedPos);
        element.RemoveItem(selectedPos);
        element.InsertItem(count - 1, itemToMoveDown.text, itemToMoveDown.value);
        element.SetSelectedIndex(count - 1);
      }
      if (selectedPos > 0) {
        itemToMoveDown = element.GetItem(selectedPos - 1);
        element.RemoveItem(selectedPos - 1);
        element.InsertItem(selectedPos, itemToMoveDown.text, itemToMoveDown.value);
      }
    }
    else if (direction == 'down') {
      if (selectedPos == count - 1) {
        itemToMoveUp = element.GetItem(selectedPos);
        element.RemoveItem(selectedPos);
        element.InsertItem(0, itemToMoveUp.text, itemToMoveUp.value);
        element.SetSelectedIndex(0);
      }
      if (selectedPos >= 0 && selectedPos < count - 1) {
        itemToMoveUp = element.GetItem(selectedPos + 1);
        element.RemoveItem(selectedPos + 1);
        element.InsertItem(selectedPos, itemToMoveUp.text, itemToMoveUp.value);
      }
    }
  }
}

// *******************************************************************
// Description  Executes client side validation against the passed validation group.
// Parameters   validationGroup The validation group against which client side validation is to be executed.
// *******************************************************************
function ExecuteValidation(validationGroup) {
  ASPxClientEdit.ValidateGroup(validationGroup, true);
}

// *******************************************************************
// Description  Clears the validation group.
// Parameters   validationGroup The validation group to be cleared.
// *******************************************************************
function ClearValidation(validationGroup) {
  ASPxClientEdit.ClearGroup(validationGroup);
}

// *******************************************************************
// Description  Set max length at the textAreaElement according to
//              length.
// Parameters   textAreaElement The text area element.
//              length          The max length.
// Remark:      http://www.devexpress.com/Support/Center/p/S90681.aspx
// *******************************************************************
function setMaxLength(textAreaElement, length) {
  textAreaElement.maxlength = length;
  ASPxClientUtils.AttachEventToElement(textAreaElement, "keyup", createEventHandler("onKeyUpOrChange"));
  ASPxClientUtils.AttachEventToElement(textAreaElement, "change", createEventHandler("onKeyUpOrChange"));
}

// *******************************************************************
// Description  Handle the on key up or change.
// Parameters   evt The event.
// *******************************************************************
function onKeyUpOrChange(evt) {
  processTextAreaText(ASPxClientUtils.GetEventSource(evt));
}

// *******************************************************************
// Description  Adjust the text value according the max length.
// Parameters   textAreaElement The text area element.
// *******************************************************************
function processTextAreaText(textAreaElement) {
  var maxLength = textAreaElement.maxlength;
  var text = textAreaElement.value;
  var isAcceptable = (maxLength == 0) || (text.length <= maxLength);
  if (maxLength != 0 && text.length > maxLength)
    textAreaElement.value = text.substr(0, maxLength);
}

// *******************************************************************
// Description  Create event handler according to the funcName.
// Parameters   funcName The name of the function.
// *******************************************************************
function createEventHandler(funcName) {
  return new Function("event", funcName + "(event);");
}