ASPxClientReportViewer = _aspxCreateClass(ASPxClientControl, {
 constructor: function(name) {
  this.constructor.prototype.constructor.call(this, name);
  this.searchWindow = null;
  this.textRange = null;
  this.pageCount = 0;
  this.exportWindow = null;
  this.pageByPage = true;
  this.printUsingAdobePlugIn = true;
  this.searchPopupControl = null;
  var me = this;
  _aspxAttachEventToElement(window, "beforeunload", function() { me.onunload(); });
  this.printHelper = new dx_PrintHelper();
  this.formHelper = new dx_FormHelper();
  this.PageLoad = new ASPxClientEvent();
  this.postBackStarted = false;
 },
 AfterInitialize: function() {
  this.constructor.prototype.AfterInitialize.call(this);
  this.RaisePageLoadEvent();
  var obj = this;
  this.addBeforeFormSubmit(theForm, function() { obj.postBackStarted = true; })
 },
 addBeforeFormSubmit: function(form, beforeSubmitFunction) {
  var original = form.submit;
  form.submit = function() {
   beforeSubmitFunction();
   var callee = arguments.callee;
   this.submit = original;
   var submitResult = this.submit();
   this.submit = callee;
   return submitResult;
  };
 },
 onunload: function() {
  this.closeWindow(this.searchWindow);
 },
 closeWindow: function(win) {
  if(win != null && !win.closed) {
   win.close();
  }
 },
 OnCallbackInternal: function(result, isError) {
  if(result == "") {
   this.SendPostBack("");
   return;
  }
  var callbackType = this.getResultParam(result);
  if(callbackType != null) {
   var cacheKeyAssignmentScript = this.getResultParam(callbackType.remainder);
   var resultRemainder = cacheKeyAssignmentScript.remainder;
   eval(cacheKeyAssignmentScript.param);
   switch(callbackType.param) {
    case "page":
     this.OnPageChanged(resultRemainder);
     break;
    case "print":
     this.OnPrint(resultRemainder);
     break;
    case "search":
     this.OnSearch(resultRemainder);
     break;
    case "searchControl":
     this.OnSearchControl(resultRemainder);
     break;
    case "saveToWindow":
     this.OnSaveToWindow(resultRemainder);
     break;
    case "saveToDisk":
     this.OnSaveToDisk(resultRemainder);
     break;
    case "bookmark":
     this.OnBookmark(resultRemainder);
     break;
   }
  }
 },
 gotoPageInternal: function(pageIndex) {
  this.execContentChangingCallback(pageIndex, "page");
 },
 getResultParam: function(result) {
  var pos = result.indexOf("|");
  if(pos > -1)
   return { param: result.substr(0, pos), remainder: result.substr(pos + 1) };
  return null;
 },
 execExport: function(exportKind, params, win) {
  this.execPostbackInWindow((win != null ? win : this.getFrameForExport()), exportKind, params);
 },
 execPrintPdf: function(pageIndex) {
  if(this.printHelper.pdfExists())
   this.execExport("saveToDisk", { format: "pdf", showPrintDialog: "true", idx: pageIndex });
  else
   this.execCallbackPrint(pageIndex);
 },
 execCallbackPrint: function(pageIndex) {
  this.execCallback("print", { idx: pageIndex, wholePage: !__aspxOpera });
 },
 execCallback: function(command, params) {
  if(!this.postBackStarted && !this.InCallback()) {
   this.createLoadingPanel();
   this.CreateCallback(this.createEventArgument(command, params));
  }
 },
 execPostbackInWindow: function(win, command, params) {
  _aspxRaisePostHandlerOnPost();
  this.formHelper.sendPostbackInWindow(win, this.uniqueID, this.createEventArgument(command, params));
 },
 createEventArgument: function(command, params) {
  var formatter = new dx_ParamFormatter();
  formatter.params = params;
  return command + "=" + formatter.getValue();
 },
 createLoadingPanel: function() {
  this.CreateLoadingPanelWithAbsolutePosition(this.getContentElement());
 },
 execContentChangingCallback: function(pageIndex, command, params) {
  this.setCurrentPageIndex(pageIndex);
  this.execCallback(command, params);
 },
 OnCallback: function(result) {
  this.OnCallbackInternal(result, false);
 },
 OnCallbackError: function(result, data) {
  this.OnCallbackInternal(result, true);
 },
 OnPageChanged: function(result) {
  var element = this.getContentElement();
  if(element != null) {
   _aspxSetInnerHtml(element, result);
  }
 },
 OnSearch: function(result) {
  this.OnPageChanged(result);
 },
 OnSearchControl: function(result) {
  var div = document.createElement("DIV");
  var element = this.getContentElement();
  element.parentNode.appendChild(div);
  div.outerHTML = result;
 },
 OnPrint: function(result) {
  this.printHelper.print(result, this.getContentElement().className);
 },
 OnSaveToWindow: function(result) {
  if(this.exportWindow != null && !this.exportWindow.closed) {
   this.setWindowLocation(this.exportWindow, result);
  }
 },
 OnSaveToDisk: function(result) {
  this.printHelper.getFrame().location = result;
 },
 OnBookmark: function(result) {
  this.OnPageChanged(result);
 },
 showSearchWindow: function() {
  if(this.searchPopupControl == null) {
   this.execCallback("searchControl", {});
   return;
  }
  this.closeTextRange();
  this.searchPopupControl.ShowAndFocus();
 },
 findText: function(s) {
  var f = new dx_ParamFormatter();
  f.parse(s);
  this.findTextCore(f.params);
 },
 findTextCore: function(params) {
  var up = params["up"];
  var range = this.getTextRange(up);
  if(!range.findText(params["txt"], params["word"], params["case"], up)) {
   this.execCallback("search", params);
  }
 },
 getTextRange: function(up) {
  if(!_aspxIsExists(this.textRange)) {
   var f = this.getContentElement();
   this.textRange = new dx_TextRange(f, up);
  }
  return this.textRange;
 },
 closeTextRange: function() {
  if(_aspxIsExists(this.textRange))
   this.textRange.empty();
  this.textRange = null;
 },
 getContentElement: function() {
  return _aspxGetElementById(this.name + "_Div");
 },
 setViewSize: function(width, height) {
  var el = this.getContentElement();
  if(el != null) {
   el.style.width = _aspxGetValueWithPxUnit(width);
   el.style.height = _aspxGetValueWithPxUnit(height);
  }
 },
 onPageLoad: function(pageCount) {
  this.textRange = null;
  if(_aspxIsExists(this.bookmarkHighlighter))
   this.bookmarkHighlighter.Reset();
  this.pageCount = pageCount;
  this.assignPageBackColor();
  if(this.isInitialized)
   this.RaisePageLoadEvent();
 },
 assignPageBackColor: function() {
  var contentElement = this.getContentElement();
  for(var child in contentElement.childNodes)
   if(contentElement.childNodes[child].tagName == 'TABLE') {
   contentElement.style.backgroundColor = contentElement.childNodes[child].style.backgroundColor;
   break;
  }
 },
 getCurrentPageIndexHidenField: function() {
  return _aspxGetElementById(this.name + "CurrentPageIndex");
 },
 getCurrentPageIndex: function() {
  return parseInt(this.getCurrentPageIndexHidenField().value);
 },
 setCurrentPageIndex: function(pageIndex) {
  return this.getCurrentPageIndexHidenField().value = pageIndex;
 },
 setHiddenFieldValue: function(fieldName, value) {
  var hiddenField = _aspxGetElementById(this.name + fieldName);
  hiddenField.value = value;
 },
 RaisePageLoadEvent: function() {
  var args = new ASPxClientReportViewerPageLoadEventArgs(this.getCurrentPageIndex(), this.pageCount);
  this.PageLoad.FireEvent(this, args);
 },
 GotoBookmark: function(pageIndex, bookmarkPath) {
  if(!this.pageByPage) return;
  if(pageIndex != this.getCurrentPageIndex()) {
   this.execContentChangingCallback(pageIndex, "bookmark", { path: bookmarkPath });
  } else {
   this.HighlightBookmark(bookmarkPath);
  }
 },
 HighlightBookmark: function(bookmarkPath) {
  if(!_aspxIsExists(this.bookmarkHighlighter))
   this.bookmarkHighlighter = new dx_BookmarkHighlighter(this.getContentElement(), this.name);
  this.bookmarkHighlighter.Highlight(this.getCurrentPageIndex(), bookmarkPath);
 },
 setWindowLocation: function(win, loc) {
  if(__aspxIE && __aspxBrowserVersion >= 7) { 
   win.document.open("text/html", "replace");
   win.document.write("<frameset><frame src='" + loc + "'></frame></frameset>");
   win.document.close();
  } else
   win.location = loc;
 },
 getFrameForExport: function() {
  return (__aspxIE || __aspxChrome || __aspxSafari) ? this.printHelper.getFrameRecreated() : this.printHelper.getFrame();
 }
});
_aspxGetValueWithPxUnit = function(value) { return value.toString() + 'px' };
function aspxRVSDLoaded(reportViewerID, popupControlID, textEditID, buttonFindID, checkWordID, checkCaseID, radioUpID, radioDownID) {
 var popupControl = aspxGetControlCollection().Get(popupControlID);
 popupControl.reportViewer = aspxGetControlCollection().Get(reportViewerID);
 popupControl.buttonFind = aspxGetControlCollection().Get(buttonFindID);
 popupControl.textEdit = aspxGetControlCollection().Get(textEditID);
 popupControl.checkWord = aspxGetControlCollection().Get(checkWordID);
 popupControl.checkCase = aspxGetControlCollection().Get(checkCaseID);
 popupControl.radioUp = aspxGetControlCollection().Get(radioUpID);
 popupControl.radioDown = aspxGetControlCollection().Get(radioDownID);
 var textEdit = popupControl.textEdit;
 var inputElement = textEdit.GetInputElement();
 inputElement.onpropertychange = function() {
  if(popupControl.buttonFind != null)
   popupControl.buttonFind.SetEnabled(inputElement.value != "" && __aspxIE);
 };
 if(popupControl.reportViewer != null)
  popupControl.reportViewer.searchPopupControl = popupControl;
 popupControl.ShowAndFocus = function() {
  this.Show();
  this.buttonFind.Focus();
  this.textEdit.SelectAll();
 };
 popupControl.ShowAndFocus(); 
}
function aspxRVSDFind(popupControlID) {
 var popupControl = aspxGetControlCollection().Get(popupControlID);
 if(popupControl != null) {
  var params = { "txt" : popupControl.textEdit.GetText(),
   "case" : popupControl.checkWord.GetChecked(),
   "word" : popupControl.checkCase.GetChecked(),
   "up" : popupControl.radioUp.GetChecked()};
  popupControl.reportViewer.findTextCore(params);
 }
}
function aspxRVSDClose(popupControlID) {
 var popupControl = aspxGetControlCollection().Get(popupControlID);
 if(popupControl != null)
  popupControl.Hide();
}
function dx_ParamFormatter() {
 this.params = [];
 this.addParam = function(name, val) {
  this.params[name] = val;
 }
 this.getValue = function() {
  var val = "";
  for(var i in this.params)
   val += this.format(i, this.params[i]);
  return val;
 }
 this.format = function(name, val) {
  return name + ":" + val + ";";
 }
 this.parse = function(s) {
  var ss = s.split(";");
  for(var i = 0; i < ss.length; i++) {
   var sss = ss[i].split(":");
   if(sss.length == 2)
    this.addParam(sss[0], sss[1]);
  }
 }
}
function dx_TextRange(element, searchUp) {
 this.isTrue = function(val) {
  return val == true || val == "true";
 }
 this.selText = "";
 this.element = element;
 this.range = element.document.body.createTextRange();
 this.range.moveToElementText(element);
 this.originalRange = this.range.duplicate();
 this.empty = function() { 
  this.element.document.selection.empty();
 }
 this.select = function(text) { 
  this.range.select();
  this.selText = text;
 }
 this.findText = function(text, mword, mcase, up) {
  if(text == null || text.length == 0)
   return true;
  var fl = this.getFlags(mword, mcase);
  var val = this.isTrue(up) ? this.findUp(text, fl) :
   this.findDown(text, fl);   
  if(val) this.select(text);
  return val;
 }
 this.getFlags = function(mword, mcase) {
  var fl = 0;
  if( this.isTrue(mword) ) fl += 2;
  if( this.isTrue(mcase) ) fl += 4;
  return fl;
 }
 this.findUp = function(text, fl) { 
  this.range.moveEnd("character", -this.selText.length);
  var val = this.range.findText(text, -1000, fl) && this.originalRange.inRange(this.range);      
  if(!val) this.range.moveEnd("character", this.selText.length);
  return val;
 }
 this.findDown = function(text, fl) { 
  this.range.moveStart("character", this.selText.length);
  var val = this.range.findText(text, 1000, fl) && this.originalRange.inRange(this.range);
  if(!val) this.range.moveStart("character", -this.selText.length);
  return val;
 }
}
function dx_BookmarkHighlighter(contentElement, ownerName) {
 this.contentElement = contentElement;
 this.bookmarkElement = null;
 this.selectionTemplate = _aspxGetElementById(ownerName + "_Bookmark");
 this.Highlight = function(pageIndex, bookmarkPath) {
  if(_aspxIsExists(this.bookmarkElement)) {
   try {
    this.contentElement.removeChild(this.bookmarkElement)
   } catch(e) {}
   this.bookmarkElement = null;
  }
  bookmarkPath = pageIndex.toString() + "_" + bookmarkPath;
  var bookmarkElements = this.getBookmarkElements(_aspxGetElementsByTagName(this.contentElement, "A"), bookmarkPath);
  if(bookmarkElements.length == 0)
   return; 
  var bounds = this.getBookmarkBounds(bookmarkElements);
  this.bookmarkElement = this.addBookmarkElement(bounds);
 }
 this.Reset = function() {
  this.bookmarkElement = null;
 }
 this.getBorderWidth = function() {
  return parseInt(this.selectionTemplate.style.borderWidth);
 }
 this.getBookmarkElements = function(elements, name) {
  var bookmarkElements = new Array();
  for(var i = 0; i < elements.length; i++) {
   if(elements[i].name == name)
    _aspxArrayPush(bookmarkElements, elements[i].parentNode);
  }
  return bookmarkElements; 
 }
 this.getBookmarkBounds = function(bookmarkElements) {
  var x = this.getLeft(bookmarkElements[0]);
  var y = this.getTop(bookmarkElements[0]);
  var right = this.getRight(bookmarkElements[0]);
  var bottom = this.getBottom(bookmarkElements[0]);
  for(var i = 1; i < bookmarkElements.length; i++) {
   x = Math.min(x, this.getLeft(bookmarkElements[i]));
   y = Math.min(y, this.getTop(bookmarkElements[i]));
   right = Math.max(right, this.getRight(bookmarkElements[i]));
   bottom = Math.max(bottom, this.getBottom(bookmarkElements[i]));
  }
  var width = right - x - 2 * this.getBorderWidth();
  var height = bottom - y - 2 * this.getBorderWidth();
  return {'left' : x, 'top' : y, 'width': width, 'height': height };
 }
 this.getLeft = function(el) { return _aspxGetAbsoluteX(el) - _aspxGetAbsoluteX(this.contentElement); };
 this.getTop = function(el) { return _aspxGetAbsoluteY(el) - _aspxGetAbsoluteY(this.contentElement); };
 this.getWidth = function(el) { return el.offsetWidth; };
 this.getHeight = function(el) { return el.offsetHeight; };
 this.getRight = function(el) { return this.getLeft(el) + this.getWidth(el); };
 this.getBottom = function(el) { return this.getTop(el) + this.getHeight(el); };
 this.addBookmarkElement = function(bounds) {
  var newEl = this.selectionTemplate.cloneNode(false);
  newEl.style.display = 'block';
  newEl.style.backgroundColor = '';
  contentElement.appendChild(newEl);
  bounds.left -= this.getLeft(newEl);
  bounds.top -= this.getTop(newEl);
  newEl.style.width = _aspxGetValueWithPxUnit(bounds.width);
  newEl.style.height = _aspxGetValueWithPxUnit(bounds.height);
  newEl.style.position = 'relative';
  newEl.style.left = _aspxGetValueWithPxUnit(bounds.left);
  newEl.style.top = _aspxGetValueWithPxUnit(bounds.top);
  return newEl;
 }
}
function aspxRVGotoBM(name, pageIndex, bookmarkIndices){
 var reportViewer = aspxGetControlCollection().Get(name);
 if(reportViewer != null) reportViewer.GotoBookmark(pageIndex, bookmarkIndices);
}
function dx_FormHelper() { 
 this.sendPostbackInWindow = function(win, eventTarget, eventArgument) {
  this.eventTarget = eventTarget;
  this.eventArgument = eventArgument;
  var formAttributes = { name : theForm.name, method : theForm.method, action : this.getCorectedAction(theForm.action), id : theForm.id };
  var content = "<html><body><div style='overflow:hidden;width:0px;height:0px'>" +
  this.buildTag("form", formAttributes, this.getInputs() + "<input type='submit' value='submit'/>") +
  '</div></body></html>';
  this.submitForm(win, content);
 }
 this.submitForm = function(win, content) {
  var me = this;
  var submitFormCore = function() {
   me.writeContent(win, content);
   var form = win.document.getElementById(theForm.id);
   if(__aspxChrome && form.submit == undefined) { 
    var fakeInput = win.document.createElement("input");
    fakeInput.setAttribute("type", "submit");
    win.document.forms[0].appendChild(fakeInput);
    fakeInput.click();
   } else 
    form.submit();
  };
  if(typeof(win.document) == 'object') {
   submitFormCore();
   return;
  }
  if(win.frameElement) {
   win.frameElement.onreadystatechange = function() {
    if(win.frameElement.readyState == "complete") { 
     win.frameElement.onreadystatechange = null;
     submitFormCore();
    }
   }
   win.location = "about:blank";
  }
 }
 this.getCorectedAction = function(action) {
  var formAction = action;
  if(__aspxChrome || __aspxSafari) {
   if(formAction.indexOf('?') < 0)
    formAction = formAction + "?dxrep_fake=";
   else
    formAction.replace('?', "?dxrep_fake=&");
  }
  return formAction;
 }
 this.writeContent = function(win, content) {
  var doc = win.document;
  doc.open("text/html", "replace");
  doc.write(content);
  doc.close();
 }
 this.getInputs = function() {
  var count = theForm.elements.length;
  var element;
  var result = "";
  for (var i = 0; i < count; i++) {
   element = theForm.elements[i];
   var tagName = element.tagName.toLowerCase();
   if (tagName == "input") {
    var type = element.type;
    if ((type == "checkbox" || type == "radio") && element.checked)
     result += this.buildInput(element.type, element.name, element.id, this.getElementValue(element), element.checked);
    else if(type == "text" || type == "hidden" || type == "password")
     result += this.buildInput(element.type, element.name, element.id, this.getElementValue(element));
   }
   else if (tagName == "select") {
    var selectCount = element.options.length;
    for (var j = 0; j < selectCount; j++) {
     var selectChild = element.options[j];
     if (selectChild.selected == true) {
      result += this.buildInput("hidden", element.name, element.id, selectChild.value);
     }
    }
   }
   else if (tagName == "textarea") {
    result += this.buildTextArea(element.name, element.id, element.value);
   }
  }
  return result;
 }
 this.getElementValue = function(element) {
  if(element.id == '__EVENTTARGET')
   return this.eventTarget;
  if(element.id == '__EVENTARGUMENT')
   return this.eventArgument;
  return element.value;
 }
 this.buildFakeInput = function(name, value) {
  return this.buildInput('hidden', name, name, value)
 }
 this.buildInput = function(type, name, id, value, checked) {
  var params = {type : type, name : name, id : id, value : value};
  if(checked)
   params.checked = checked;
  return this.buildTag("input", params);
 }
 this.buildTextArea = function(name, id, value) {
  return this.buildTag("textarea", {name : name, id : id}, value);
 }
 this.buildTag = function(tag, attributes, content) {
  var result = '<' + tag + ' ';
  for(var attrName in attributes)
   result += attrName + '="' + attributes[attrName].toString().replace(/"/g,'&quot;') + '" ';
  result += content != null ? '>' + content + '</' + tag + '>\n' : '/>\n';
  return result;    
 }
}
 ASPxClientReportViewer.prototype.Print = function(pageIndex) {
  if(pageIndex == null)
   pageIndex = "";
  if(this.printUsingAdobePlugIn)
   this.execPrintPdf(pageIndex);
  else
   this.execCallbackPrint(pageIndex);
 };
 ASPxClientReportViewer.prototype.GotoPage = function(pageIndex) {
  pageIndex = Math.max(0, Math.min(pageIndex, this.pageCount - 1));
  if(pageIndex != this.getCurrentPageIndex())
   this.gotoPageInternal(pageIndex);
 }; 
 ASPxClientReportViewer.prototype.Refresh = function() {
  this.gotoPageInternal(0);
 }; 
 ASPxClientReportViewer.prototype.Search = function() {
  if(this.IsSearchAllowed())
   this.showSearchWindow();
 };
 ASPxClientReportViewer.prototype.SaveToWindow = function(format) {
  this.exportWindow = window.open('', '_blank', 'toolbars=no, resizable=yes, scrollbars=yes');
  this.execExport("saveToWindow", {format: format}, this.exportWindow);
 };
 ASPxClientReportViewer.prototype.SaveToDisk = function(format) {
  this.execExport("saveToDisk", {format: format}); 
 };
 ASPxClientReportViewer.prototype.IsSearchAllowed = function() {
  try {
   if(document.body.createTextRange() != null)
   return true;
  } catch(e) {}
  return false;
 };
 ASPxClientReportViewerPageLoadEventArgs = _aspxCreateClass(ASPxClientEventArgs, {
  constructor: function(pageIndex, pageCount){
   this.constructor.prototype.constructor.call(this);
   this.PageIndex = pageIndex;
   this.PageCount = pageCount;
  },
  IsFirstPage: function() { return this.PageIndex == 0; },
  IsLastPage: function() { return this.PageIndex == this.PageCount - 1; }
 });

