﻿Type.registerNamespace("NoviSurvey");

// ----------- color picker -----------

NoviSurvey.ColorPicker = function(swatchId, colorCodeId, targetId) {
  this._swatch = $get(swatchId);
  var swatchClickDel = Function.createDelegate(this, this.clickSwatch);
  $addHandler(this._swatch, "click", swatchClickDel);

  this._colorCode = $get(colorCodeId);
  var colorCodeChangedDel = Function.createDelegate(this, this.colorCodeChanged);
  $addHandler(this._colorCode, "blur", colorCodeChangedDel);

  if (targetId) {
    this._target = $get(targetId);
  }

  this.createDiv();

  this._documentClickDel = Function.createDelegate(this, this.clickDocument);
}

NoviSurvey.ColorPicker.prototype = {

  createDiv: function() {
    this._colorPickerDiv = $get('colorPickerDiv');

    if (this._colorPickerDiv.childNodes.length > 1) {
      return;
    }

    // initializes colors and behavior of the popup
    var colors = new Array(
                "#000000", "#000033", "#000066", "#000099", "#0000CC", "#0000FF", "#006600", "#006633", "#006666", "#006699", "#0066CC", "#0066FF", "#00CC00", "#00CC33", "#00CC66", "#00CC99", "#00CCCC", "#00CCFF",
                "#003300", "#003333", "#003366", "#003399", "#0033CC", "#0033FF", "#009900", "#009933", "#009966", "#009999", "#0099CC", "#0099FF", "#00FF00", "#00FF33", "#00FF66", "#00FF99", "#00FFCC", "#00FFFF",
                "#330000", "#330033", "#330066", "#330099", "#3300CC", "#3300FF", "#336600", "#336633", "#336666", "#336699", "#3366CC", "#3366FF", "#33CC00", "#33CC33", "#33CC66", "#33CC99", "#33CCCC", "#33CCFF",
                "#333300", "#333333", "#333366", "#333399", "#3333CC", "#3333FF", "#339900", "#339933", "#339966", "#339999", "#3399CC", "#3399FF", "#33FF00", "#33FF33", "#33FF66", "#33FF99", "#33FFCC", "#33FFFF",
                "#660000", "#660033", "#660066", "#660099", "#6600CC", "#6600FF", "#666600", "#666633", "#666666", "#666699", "#6666CC", "#6666FF", "#66CC00", "#66CC33", "#66CC66", "#66CC99", "#66CCCC", "#66CCFF",
                "#663300", "#663333", "#663366", "#663399", "#6633CC", "#6633FF", "#669900", "#669933", "#669966", "#669999", "#6699CC", "#6699FF", "#66FF00", "#66FF33", "#66FF66", "#66FF99", "#66FFCC", "#66FFFF",
                "#990000", "#990033", "#990066", "#990099", "#9900CC", "#9900FF", "#996600", "#996633", "#996666", "#996699", "#9966CC", "#9966FF", "#99CC00", "#99CC33", "#99CC66", "#99CC99", "#99CCCC", "#99CCFF",
                "#993300", "#993333", "#993366", "#993399", "#9933CC", "#9933FF", "#999900", "#999933", "#999966", "#999999", "#9999CC", "#9999FF", "#99FF00", "#99FF33", "#99FF66", "#99FF99", "#99FFCC", "#99FFFF",
                "#CC0000", "#CC0033", "#CC0066", "#CC0099", "#CC00CC", "#CC00FF", "#CC6600", "#CC6633", "#CC6666", "#CC6699", "#CC66CC", "#CC66FF", "#CCCC00", "#CCCC33", "#CCCC66", "#CCCC99", "#CCCCCC", "#CCCCFF",
                "#CC3300", "#CC3333", "#CC3366", "#CC3399", "#CC33CC", "#CC33FF", "#CC9900", "#CC9933", "#CC9966", "#CC9999", "#CC99CC", "#CC99FF", "#CCFF00", "#CCFF33", "#CCFF66", "#CCFF99", "#CCFFCC", "#CCFFFF",
                "#FF0000", "#FF0033", "#FF0066", "#FF0099", "#FF00CC", "#FF00FF", "#FF6600", "#FF6633", "#FF6666", "#FF6699", "#FF66CC", "#FF66FF", "#FFCC00", "#FFCC33", "#FFCC66", "#FFCC99", "#FFCCCC", "#FFCCFF",
                "#FF3300", "#FF3333", "#FF3366", "#FF3399", "#FF33CC", "#FF33FF", "#FF9900", "#FF9933", "#FF9966", "#FF9999", "#FF99CC", "#FF99FF", "#FFFF00", "#FFFF33", "#FFFF66", "#FFFF99", "#FFFFCC", "#FFFFFF",
                "#000000", "#1C1C1C", "#2A2A2A", "#383838", "#464646", "#545454", "#626262", "#707070", "#7E7E7E", "#8C8C8C", "#9A9A9A", "#A8A8A8", "#B6B6B6", "#C4C4C4", "#D2D2D2", "#E0E0E0", "#EEEEEE", "#FCFCFC");

    var borderColor = '#333333';

    var cpTable = document.createElement('table');
    cpTable.style.borderCollapse = 'collapse';
    cpTable.style.background = borderColor;
    this._colorPickerDiv.appendChild(cpTable);

    var cpTableBody = document.createElement('tbody');
    cpTable.appendChild(cpTableBody);

    var cpTableCurRow;
    var rowCellCount = 18;
    var cellHeight = 14;
    var cellWidth = 12;
    NoviSurvey.ColorPicker.prototype.colorCells = new Array();

    var q = NoviSurvey.ColorPicker.prototype.colorCells;

    for (var i = 0; i < colors.length; i++) {

      if (i % rowCellCount == 0) {
        cpTableCurRow = document.createElement("tr");
        cpTableBody.appendChild(cpTableCurRow);
      }

      var currentCell = document.createElement("td");
      NoviSurvey.ColorPicker.prototype.colorCells[i] = currentCell;
      currentCell.style.backgroundColor = colors[i];
      currentCell.style.width = cellWidth + "px";
      currentCell.style.height = cellHeight + "px";
      currentCell.style.padding = "0px";
      currentCell.style.border = "1px";
      currentCell.style.borderColor = borderColor;
      currentCell.style.borderStyle = "solid";
      cpTableCurRow.appendChild(currentCell);

      var mouseOverColorDel = Function.createDelegate(this, this.mouseOverColor);
      $addHandler(currentCell, "mouseover", mouseOverColorDel);
    }

    // layout for current color
    var curColorTable = document.createElement("table");
    curColorTable.style.borderCollapse = 'collapse';
    curColorTable.style.borderWidth = '0px 1px 1px 1px';
    curColorTable.style.borderColor = borderColor;
    curColorTable.style.borderStyle = 'solid';
    curColorTable.style.background = '#FFFFFF';
    this._colorPickerDiv.appendChild(curColorTable);

    var colorTableBody = document.createElement("tbody");
    curColorTable.appendChild(colorTableBody);

    var colorTableRow = document.createElement("tr");
    colorTableBody.appendChild(colorTableRow);

    this._selectedColorCell = document.createElement("td");
    this._selectedColorCell.style.background = '#FFFFFF';
    this._selectedColorCell.style.padding = '0px';
    this._selectedColorCell.style.height = '20px';
    this._selectedColorCell.style.width = (cellWidth * Math.floor(rowCellCount / 2)) + "px"
    colorTableRow.appendChild(this._selectedColorCell);

    this._selectedColorValueCell = document.createElement("td");
    this._selectedColorValueCell.style.background = '#FFFFFF';
    this._selectedColorCell.style.padding = '0px';
    this._selectedColorValueCell.style.textAlign = 'center';
    this._selectedColorValueCell.style.height = '20px';
    this._selectedColorValueCell.style.width = (cellWidth * (rowCellCount - Math.floor(rowCellCount / 2)) - 4 + rowCellCount + 1) + "px";

    colorTableRow.appendChild(this._selectedColorValueCell);
  },

  setCellsClickAction: function() {
    var i;
    for (i = 0; i < NoviSurvey.ColorPicker.prototype.colorCells.length; i++) {
      if (NoviSurvey.ColorPicker.prototype.mouseClickDel) {
        $removeHandler(NoviSurvey.ColorPicker.prototype.colorCells[i], "click", NoviSurvey.ColorPicker.prototype.mouseClickDel);
      }
    }

    NoviSurvey.ColorPicker.prototype.mouseClickDel = Function.createDelegate(this, this.clickColor);

    for (i = 0; i < NoviSurvey.ColorPicker.prototype.colorCells.length; i++) {
      $addHandler(NoviSurvey.ColorPicker.prototype.colorCells[i], "click", NoviSurvey.ColorPicker.prototype.mouseClickDel);
    }
  },

  mouseOverColor: function(evt) {
    evt.stopPropagation();
    this._selectedColorCell.style.backgroundColor = evt.target.style.backgroundColor;
    this._selectedColorValueCell.innerHTML = this.convertColor(evt.target.style.backgroundColor);
  },

  clickColor: function(evt) {
    evt.stopPropagation();
    var color = this.convertColor(evt.target.style.backgroundColor);
    this.setColorSwatch(color);
    this._colorCode.value = color;
    this._colorPickerDiv.style.visibility = 'hidden';
    this._colorPickerDiv.style.display = 'none';

    $removeHandler(document, 'click', this._documentClickDel);
  },

  clickSwatch: function(evt) {
    evt.stopPropagation();
    var swatchPos = findItemPos(this._swatch);
    this._colorPickerDiv.style.visibility = 'visible';
    this._colorPickerDiv.style.display = 'inline';
    this._colorPickerDiv.style.left = swatchPos[0] + 'px';
    this._colorPickerDiv.style.top = (swatchPos[1] + this._swatch.offsetHeight + 2) + 'px';

    this.setCellsClickAction();

    $addHandler(document, 'click', this._documentClickDel);
  },

  clickDocument: function(evt) {
    evt.stopPropagation();
    this._colorPickerDiv.style.visibility = 'hidden';
    this._colorPickerDiv.style.display = 'none';

    $removeHandler(document, 'click', this._documentClickDel);
  },

  colorCodeChanged: function(evt) {
    evt.stopPropagation();
    this.setColorSwatch(this._colorCode.value);
  },

  setColorSwatch: function(color) {
    // color code in the form #RGB where R, G, B are hex integers in the range of 0-FF  
    var swatchColor = color.match(/^#([0-9A-F]){6}$/i) ? color : '#FFFFFF';

    if (this._swatch.style.backgroundColor != swatchColor) {
      this._swatch.style.backgroundColor = swatchColor;
    }

    if (this._target) {
      this._target.style.color = color;
    }
  },

  // converts rgb(r, g, b) to #rgb
  convertColor: function(color) {
    color = color.toUpperCase().replace(/\s+/g, "");
    var res;
    if (color.indexOf('#') >= 0) {
      res = color;
    } else {
      var channels = color.replace(/RGB\(/, "").replace(/\)/, "").split(',');

      var redChannel = parseInt(channels[0]).toString(16);
      redChannel = "00".substring(0, 2 - redChannel.length) + redChannel;

      var greenChannel = parseInt(channels[1]).toString(16);
      greenChannel = "00".substring(0, 2 - greenChannel.length) + greenChannel;

      var blueChannel = parseInt(channels[2]).toString(16);
      blueChannel = "00".substring(0, 2 - blueChannel.length) + blueChannel;

      res = '#' + (redChannel + greenChannel + blueChannel).toUpperCase();
    }

    return res;
  }
}

NoviSurvey.ColorPicker.registerClass('NoviSurvey.ColorPicker', null, Sys.IDisposable);

// ----------- documentation -----------

NoviSurvey.Documentation = function(docLink, baseUrl, helpKey, tabHelpKey, culture) {
  this._docLink = $get(docLink);
  this._baseUrl = baseUrl;
  this._helpKey = helpKey;
  this._tabHelpKey = tabHelpKey;
  this._culture = culture;
}

NoviSurvey.Documentation.prototype = {
  setTabHelpKey: function(tabHelpKey) {
    this._tabHelpKey = tabHelpKey;
  },

  showDocumentation: function() {
    var page = "index.html" + (("" + this._helpKey).length > 0 ? "?" + this._helpKey : "");
    page += ("" + this._tabHelpKey).length > 0 && page !== "index.html" ? "-" + this._tabHelpKey : "";
    this._docLink.href = this._baseUrl + 'Help/' + this._culture + "/" + page;

    popupWindow(this._docLink.href, 1000, 700, "helpWindow");
  }
}

NoviSurvey.Documentation.registerClass('NoviSurvey.Documentation', null, Sys.IDisposable);

// ----------- tool -----------

NoviSurvey.Tool = function(panelId, requireDomainObject, activeTooltip, disabledTooltip, clickAction) {
  this._panel = $get(panelId);

  // the tool may actually not be in the page if some parent component was set as not visible
  if (!this._panel) {
    return;
    }

  this._requireDomainObject = requireDomainObject;
  this._activeTooltip = activeTooltip;
  this._disabledTooltip = disabledTooltip;
  this._clickAction = clickAction;
  
  this._link = firstChild(this._panel, 'A');
  this._label = firstChild(this._link, 'SPAN');

  this._stateForDomainObject = new Array();

  NoviSurvey.Tool.prototype.instances[panelId] = this;
}

NoviSurvey.Tool.prototype = {
  instances: new Array(),

  setEnabled: function(enabled) {
    if (!this._requireDomainObject) {
    return;
  }

    this._panel.className = enabled ? "toolOn" : "toolOff";
    this._panel.title = enabled ? this._activeTooltip : this._disabledTooltip;

    this._label.className = enabled ? "" : "toolLabelOff";

    this._link.className = enabled ? "toolLink" : "toolLinkOff";
    this._link.onclick = function() { return enabled ? eval(this._clickAction) : false; };
  },

  registerDomainObject: function(doid, enabled) {
    this._stateForDomainObject["" + doid] = enabled;
  },

  setDomainObject: function(doid) {
    var enabled = doid !== null ? this._stateForDomainObject["" + doid] : false;
    this.setEnabled(enabled);
    }
}

NoviSurvey.Tool.registerClass('NoviSurvey.Tool', null, Sys.IDisposable);

// ----------- selectable table -----------

NoviSurvey.Table = function(selectedRowFieldID, tableId, postbackOnRowSelection, toolIds, toolsState) {
  this._table = $get(tableId);

  if (!this._table) {
    return;
  }

  this._postbackOnRowSelection = postbackOnRowSelection;

  this._defaultStyle = "gridTableDefault";
  this._highlightedStyle = "gridTableHighlighted";
  this._selectedStyle = "gridTableSelected";

  // track the selection box for each row

  // selection in the table is tracked through a hidden field
  this._selected = $get(selectedRowFieldID);
  this._currentlySelectedRow = null;

  // add handlers on rows other than the header
  var clickRowDel = Function.createDelegate(this, this.clickRow);
  for (var i = 0; i < this._table.rows.length; i++) {
    var curRow = this._table.rows[i];
    if (curRow.childNodes.length > 1 && curRow.childNodes[1].tagName.toUpperCase() !== "TH" && curRow.getAttribute("hc")) {
      $addHandler(curRow, "click", clickRowDel);
    }

    // set initial selection state   
    if (this._selected.value === curRow.getAttribute("hc")) {
      curRow.className = this._selectedStyle;
      this._currentlySelectedRow = curRow;
      firstChild(firstChild(curRow, "TD"), "INPUT").checked = true;
    }
  }

  // tool state
  // expecting a 3D array [ [[objectId, state], [objectId, state], ...], [toolId, [[objectId, state], [objectId, state], ...]]
  //                        <-- corresponds to 1 tool Id           -->
  for (var i = 0; i < toolIds.length; i++) {
    var tool = NoviSurvey.Tool.prototype.instances[toolIds[i]];
    for (var j = 0; j < toolsState[i].length; j++) {
      var objId = toolsState[i][j][0];
      var enable = toolsState[i][j][1];
      tool.registerDomainObject(objId, enable);
    }
  }
}

NoviSurvey.Table.prototype = {

  firstParentRow: function(x) {
    var row = x;
    while ((row = firstParent(row, 'TR')).getAttribute("hc") === null) {
      row = row.parentNode;
    }
    return row;
  },

  isFirstColumn: function(x) {
    var cell = firstParent(x, 'TD');
    return !cell.previousSibling || !cell.previousSibling.tagName || cell.previousSibling.tagName.toUpperCase() !== 'TD';
  },

  clickRow: function(evt) {
    var row = this.firstParentRow(evt.target);
    if (row.getAttribute("hc") === this._selected.value) {
      this._selected.value = "";
      this._currentlySelectedRow = null;
      row.className = this._highlightedStyle;
      firstChild(firstChild(row, "TD"), "INPUT").checked = false;

      for (var toolId in NoviSurvey.Tool.prototype.instances) {
        NoviSurvey.Tool.prototype.instances[toolId].setDomainObject(null);
      }      
    } else {
      if (this._selected.value !== "" && this._currentlySelectedRow !== null) {
        this._currentlySelectedRow.className = this._defaultStyle;
        firstChild(firstChild(this._currentlySelectedRow, "TD"), "INPUT").checked = false;
      }
      row.className = this._selectedStyle;
      firstChild(firstChild(row, "TD"), "INPUT").checked = true;
      this._selected.value = row.getAttribute("hc");
      this._currentlySelectedRow = row;

      for (var toolId in NoviSurvey.Tool.prototype.instances) {
        var tool = NoviSurvey.Tool.prototype.instances[toolId];
        tool.setDomainObject(this._selected.value);
    }
    }

    // postback; no validation
    if (this._postbackOnRowSelection) {
    Sys.WebForms.PageRequestManager.getInstance()._doPostBack(this._table.id, '');
  }
  }
}

NoviSurvey.Table.registerClass('NoviSurvey.Table', null, Sys.IDisposable);

// ----------- Ping -----------

NoviSurvey.Ping = function(sessionId, timeout, errorPage) {
  NoviSurvey.Ping.prototype.sessionId = sessionId;
  NoviSurvey.Ping.prototype.timeout = timeout;
  NoviSurvey.Ping.prototype.errorPage = errorPage;
  this.doPing();
}

NoviSurvey.Ping.prototype = {
  doPing: function() {
    NoviSurvey.Web.WebServices.ClientUtilsWebService.Ping(NoviSurvey.Ping.prototype.pingSucess, OnFailedDoNothing);
    setTimeout('NoviSurvey.Ping.prototype.doPing();', NoviSurvey.Ping.prototype.timeout);
  },

  pingSucess: function(result, userContext, methodName) {
    if (NoviSurvey.Ping.prototype.errorPage.length > 0 && result !== NoviSurvey.Ping.prototype.sessionId) {
      window.location = NoviSurvey.Ping.prototype.errorPage;
    }
  }
}

NoviSurvey.Ping.registerClass('NoviSurvey.Ping', null, Sys.IDisposable);

// ----------- Page event handlers -----------

function onBeforeSubmit(durationHdnId) {
  // support for UI tests
  var ssf = $get('submitStartedFlag');
  if (ssf !== null) {
    ssf.value = '1';
  }

  // support for tracking time
  var dd = $get(durationHdnId);
  dd.value = (new Date()).getTime(); // time of form submission
}

function onPageLoaded(submitTimeHdn, handlerDurationHdnId, sessionId, reportingThreshold) {
  // support for UI tests
  $get('pageLoadedFlag').value = '1';

  // support for tracking time
  var submitTime = parseInt($get(submitTimeHdn).value); // ms
  var handlerTime = parseInt($get(handlerDurationHdnId).value); // ms
  var totalTime = (new Date()).getTime() - submitTime; // ms
  
  // threshold is in seconds
  if (!isNaN(handlerTime) && !isNaN(totalTime) && totalTime >= reportingThreshold * 1000) {
    NoviSurvey.Web.WebServices.ClientUtilsWebService.SetPageProcessingTime(sessionId, handlerTime, totalTime, location.href, OnSucceededDoNothing, OnFailedDoNothing);
  }  
}

// ----------- default services callback methods -----------

function OnSucceededDoNothing(result, userContext, methodName) { }
function OnFailedDoNothing(error, userContext, methodName) { }

// ----------- Internal frame handling -----------

function frameOkClicked(frameId, postbackId) {
  eval('$get("' + frameId + '").contentWindow.initiatePostBack()');
}

function frameCallback(callbackId, behaviorId) {
  $find(behaviorId).hide();
  Sys.WebForms.PageRequestManager.getInstance()._doPostBack(callbackId, '');
}

// ----------- generic functions -----------

function isBlank() {
  return this === undefined || this === null || this.replace(/\s+/gi, "").length === 0
}

String.prototype.isBlank = isBlank;

function checkPopupBlocked(poppedWindow, handler) {
  setTimeout(function () { doCheckPopupBlocked(poppedWindow, handler); }, 3500);
}

function doCheckPopupBlocked(poppedWindow, handler) {
  Sys.Debug.trace('Checking if popup was blocked');

  var blocked = false;

  if (poppedWindow) {
    poppedWindow.test = function () { };
  }

  try {
    if (typeof poppedWindow == 'undefined') {
      blocked = true;
      Sys.Debug.trace('safari: blocked');
    } else if (poppedWindow && poppedWindow.closed) {
      // window closed explicitely by the user
      blocked = false;
      Sys.Debug.trace('closed by user');
    } else if (poppedWindow && poppedWindow.outerWidth == 0) {
      // chrome      
      blocked = true;
      Sys.Debug.trace('chrome: blocked');
    } else if (poppedWindow && poppedWindow.test) {
      // standard case
      blocked = false;
      Sys.Debug.trace('standard case: not blocked');
    } else {
      // assume it was blocked
      Sys.Debug.trace('assume blocked');
      blocked = true;
    }
  } catch (err) {
    // no-op
  }

  if (blocked) {
    handler();
  }
}

function popupWindow(url, width, height, name, controlId) {
  //var params = 'height=' + height + ',width=' + width + ',scrollbars=yes, toolbar=yes, location=yes, status=yes, menubar=no, resizable=yes, dependent=no';
  var params = 'height=' + height + ',width=' + width + ',scrollbars=yes, toolbar=no, location=yes, status=yes, menubar=no, resizable=yes, dependent=no';

  var newWindow = window.open(
    url,
    name,
    params,
    true
  );

  var showBlockPoupWarning = function() {
    if (controlId) {
      $get(controlId).style.visibility = 'visible';
      $get(controlId).style.display = 'inline';
    }
  };

  if (newWindow) {
    newWindow.location = url;
    newWindow.focus();
    checkPopupBlocked(newWindow, showBlockPoupWarning);
  } else {
    if (controlId) {
      $get(controlId).style.visibility = 'visible';
      $get(controlId).style.display = 'inline';
    }
  }
}

function firstParent(node, tag) {
  while (node.tagName.toUpperCase() !== tag.toUpperCase() && node.parentNode) {
    node = node.parentNode;
  }
  return node.tagName.toUpperCase() === tag.toUpperCase() ? node : null;
}


function isWhiteSpace(node) {
  // Use ECMA-262 Edition 3 String and RegExp features
  return !(/[^\t\n\r ]/.test(node.data));
}

function isIgnorable(node) {
  // this function is used to ignore whitespace in the dom tree
  return (node.nodeType == 8) || // A comment node
         ((node.nodeType == 3) && isWhiteSpace(node)); // a text node, all ws
}

function firstChild(node, tag) {
  var res = node.firstChild;
  while (res) {
    if (!isIgnorable(res) && res.tagName.toUpperCase() == tag.toUpperCase()) break;
    res = res.nextSibling;
  }
  return res;
}

// Cross browser function to stop the propagation of an event
function stopEvent(evt) {
  if (!evt) evt = window.event;
  evt.cancelBubble = true;
  if (evt.stopPropagation) evt.stopPropagation();
}

function findItemPos(elem) {
  var curLeft = 0;
  var curTop = 0;
  if (!elem) {
    elem = window.event.srcElement;
  }
  if (elem.offsetParent != null) {
    curLeft = elem.offsetLeft;
    curTop = elem.offsetTop;
    var parent = elem.offsetParent;
    while (parent) {
      curLeft += parent.offsetLeft;
      curTop += parent.offsetTop;
      parent = parent.offsetParent;
    }
  }
  return [curLeft, curTop];
}

function superImposeDiv(divIdBottom, divIdTop) {
  var divBottom = $get(divIdBottom);
  var posBottom = findItemPos(divBottom);
  var divTop = $get(divIdTop);
  divTop.style.zIndex = parseInt(divBottom.style.zIndex) + 1;
  divTop.style.left = posBottom[0] + 'px';
  divTop.style.top = posBottom[1] + 'px';
  divTop.style.width = divBottom.offsetWidth + 'px';
  divTop.style.visibility = 'visible';
  // note: layer will not be transparent in IE7 if the div height is greater than 4094 px
  divTop.style.height = (divBottom.offsetHeight <= 4094 ? divBottom.offsetHeight : 4094) + 'px';
}

function copyToClipboard(inText) {
  if (window.clipboardData) {
    window.clipboardData.setData("Text", inText);
  }
  return false;
}

function findEventPosition(e) {
  if (!e) var e = window.event;

  var posx = e.clientX + document.documentElement.scrollLeft;
  var posy = e.clientY + document.documentElement.scrollTop;

  return [posx, posy];
}

var mousePosition;
document.onmousemove = function(e) { mousePosition = findEventPosition(e); };

function scrollToTop() {
  window.document.body.scrollTop = 0;
  window.document.documentElement.scrollTop = 0;
}

// ----------- Caret position in text field -----------

function getCaretPosition(control) {
  var caretPos = 0;
  // IE
  if (document.selection) {
    control.focus();
    var sel = document.selection.createRange();
    var sel2 = sel.duplicate();
    sel2.moveToElementText(control);
    caretPos = -1;
    while (sel2.inRange(sel)) {
      sel2.moveStart('character');
      caretPos++;
    }
  }
  // Firefox
  else if (control.selectionStart || control.selectionStart == '0') {
    caretPos = control.selectionStart;
  }
  return (caretPos);
}

function setCaretPosition(control, position) {
  // IE
  if (control.setSelectionRange) {
    control.focus();
    control.setSelectionRange(position, position);
  }
  // Firefox
  else if (control.createTextRange) {
    var range = control.createTextRange();
    range.collapse(true);
    range.moveEnd('character', position);
    range.moveStart('character', position);
    range.select();
  }
}

// ----------- wait tooltip -----------

function showWaitTooltip(e) {
  var pos = findEventPosition(e);
  var dt = $get('clickDiv');
  dt.style.visibility = 'visible';
  dt.style.display = 'block';

  var x = pos[0] - dt.offsetWidth / 2 - 1;
  var y = pos[1] - dt.offsetHeight - 5;

  dt.style.left = x + 'px';
  dt.style.top = y + 'px';
  setTimeout('hideWaitTooltip(' + x + ',' + y + ')', 1000);
}

function hideWaitTooltip(x, y) {
  var dt = $get('clickDiv');
  if (dt.offsetLeft === x && dt.offsetTop === y || dt.offsetLeft === 0 || dt.offsetTop === 0) {
    dt.style.visibility = 'hidden';
    dt.style.display = 'none';
  }
}

// --------------------------------------
//  RibbonIconCtl 

function VertIconMouseOver(path, leftImageId, rightImageId) {
  $get(leftImageId).src = path + 'Images/ribbon_vert_icon_bg_hover_l.png';
  $get(rightImageId).src = path + 'Images/ribbon_vert_icon_bg_hover_r.png';
}

function VertIconMouseOut(path, leftImageId, rightImageId) {
  $get(leftImageId).src = path + 'Images/ribbon_vert_icon_bg_l.png';
  $get(rightImageId).src = path + 'Images/ribbon_vert_icon_bg_r.png';
}

function VertIconDblClick() {
  if (document.selection) {
    document.selection.empty();
  }
}

// --------------------------------------

function VertIconClick(btnID) {
  $get(btnID).click();
}

function HidePanel(panelId, fieldId) {
  var panel = $get(panelId);
  panel.style.visibility = 'hidden';
  panel.style.display = 'none';
  var field = $get(fieldId);
  field.value = "Collapsed";
}

function centerDiv(df) {
  var dfs = df.style;
  var offsetW = -154 / 2;
  var offsetH = -52 / 2;
  dfs.position = 'absolute';
  dfs.zIndex = 999999999;
  if (window.innerWidth) {
    var hCenter = window.innerWidth / 2;
    var vCenter = window.innerHeight / 2;
    dfs.left = hCenter + offsetW + "px";
    dfs.top = vCenter + offsetH + "px";
  }
  else {
    var hCenter = document.documentElement.clientWidth / 2;
    var vCenter = document.documentElement.clientHeight / 2;
    dfs.left = hCenter + offsetW + "px";
    dfs.top = vCenter + offsetH + "px";
  }
}

function centerUpdateProgress(updateProgressId) {
  var windowHandler = function() { centerDiv($get(updateProgressId)); };

  window.onload = windowHandler;
  window.onresize = windowHandler;
  window.onscroll = windowHandler;
}

function RegisterUpdateProgress(panelId) {
  var panel = $get(panelId);
  function ShowUpdateProgress() {
    panel.style.visibility = 'visible';
    panel.style.display = 'block';
  }
  function HideUpdateProgress() {
    panel.style.visibility = 'hidden';
    panel.style.display = 'none';
  }
  Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(ShowUpdateProgress);
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(HideUpdateProgress);
}

//-------------------------------------------------------------------------------------------
// TextEditorControl functions

function ShowEditor(titleID, titleTxt, modalID, editorID, textBoxID, panelHiddenFieldID, supplementalPanelID, supplementalPanelList) {
  var title = $get(titleID);
  var modal = $find(modalID);
  var textBox = $get(textBoxID);
  var panelHiddenField = $get(panelHiddenFieldID);
  title.innerHTML = titleTxt;

  var editor = $find(editorID);
  editor.set_content("");
  editor.set_content(textBox.value);
  

  panelHiddenField.value = textBoxID;
  if (supplementalPanelList != "") {
    var panelList = supplementalPanelList.split(",");
    for (var i = 0; i < panelList.length; i++) {
      $get(panelList[i]).style.visibility = 'hidden';
      $get(panelList[i]).style.display = 'none';
    }
  }
  if (supplementalPanelID != "") {
    var supplementalPanel = $get(supplementalPanelID);
    supplementalPanel.style.visibility = 'visible';
    supplementalPanel.style.display = 'block';
  }

  
  modal.show();

  //set focus
  editor.get_editPanel().get_modePanels()[Sys.Extended.UI.HTMLEditor.ActiveModeType.Design]._doc.body.focus();
}

function SaveEditor(editorID, panelHiddenFieldID) {
  var panelHiddenField = $get(panelHiddenFieldID);
  var textBox = $get(panelHiddenField.value);
  textBox.value = $find(editorID).get_content();
}

function ShowCallOut(divID, calloutContentID, textBoxID) {
  var calloutContent = $get(calloutContentID)
  var textBox = $get(textBoxID);
  var div = $get(divID);
  if (textBox.value != null && textBox.value.match('<') == '<') {
    calloutContent.innerHTML = textBox.value;
    var pos = findItemPos(textBox);
    div.style.left = pos[0] + "px";
    div.style.top = pos[1] + 22 + "px";
    div.style.visibility = 'visible';
    div.style.display = 'inline';
  }
}

function HideCallOut(divID) {
  var div = $get(divID);
  div.style.visibility = 'hidden';
  div.style.display = 'none';
}

//-------------------------------------------------------------------------------------------
// Event handling with FIFO semantics

var addSeqHandler = function(element, eventName, handler) {
  var e = Function._validateParams(arguments, [
        { name: "element", domElement: true },
        { name: "eventName", type: String },
        { name: "handler", type: Function }
    ]);
  if (e) throw e;

  if (!element._seqEvents) {
    element._seqEvents = {};
  }
  var eventCache = element._seqEvents[eventName];
  if (!eventCache) {
    element._seqEvents[eventName] = eventCache = [];
  }
  if (eventCache.length == 0)
    $addHandler(element, eventName, _invokeSeqHandlers);

  eventCache[eventCache.length] = {
    handler: handler,
    browserHandler: function(e) { return handler.call(element, new Sys.UI.DomEvent(e)); }
  };
}

var _invokeSeqHandlers = function(eve) {
  var cache = this._seqEvents[eve.type];
  for (var i = 0; i < cache.length; i++) {
    cache[i].browserHandler.call(this, eve);
  }
}


