/*********************************************************** Bitrix AJAX library ver 6.5 alpha ***********************************************************/ /* private CAjaxThread class - description of current AJAX request thread. */ function CAjaxThread(TID) { this.TID = TID; this.httpRequest = this._CreateHttpObject(); this.arAction = []; } CAjaxThread.prototype._CreateHttpObject = function() { var obj = null; if (window.XMLHttpRequest) { try {obj = new XMLHttpRequest();} catch(e){} } else if (window.ActiveXObject) { try {obj = new ActiveXObject("Microsoft.XMLHTTP");} catch(e){} if (!obj) try {obj = new ActiveXObject("Msxml2.XMLHTTP");} catch (e){} } return obj; } CAjaxThread.prototype.addAction = function(obHandler) { this.arAction.push(obHandler); } CAjaxThread.prototype.clearActions = function() { this.arAction = []; } CAjaxThread.prototype.nextAction = function() { return this.arAction.shift(); } CAjaxThread.prototype.Clear = function() { this.arAction = null; this.httpRequest = null; } /* public CAjax main class */ function CAjax() { this.arThreads = {}; this.obTemporary = null; } CAjax.prototype._PrepareData = function(arData, prefix) { var data = ''; if (null != arData) { for(var i in arData) { if (data.length > 0) data += '&'; var name = jsAjaxUtil.urlencode(i); if(prefix) name = prefix + '[' + name + ']'; if(typeof arData[i] == 'object') data += this._PrepareData(arData[i], name) else data += name + '=' + jsAjaxUtil.urlencode(arData[i]) } } return data; } CAjax.prototype.GetThread = function(TID) { return this.arThreads[TID]; } CAjax.prototype.InitThread = function() { while (true) { var TID = 'TID' + Math.floor(Math.random() * 1000000); if (!this.arThreads[TID]) break; } this.arThreads[TID] = new CAjaxThread(TID); return TID; } CAjax.prototype.AddAction = function(TID, obHandler) { if (this.arThreads[TID]) { this.arThreads[TID].addAction(obHandler); } } CAjax.prototype._OnDataReady = function(TID, result) { if (!this.arThreads[TID]) return; while (obHandler = this.arThreads[TID].nextAction()) { obHandler(result); } } CAjax.prototype._Close = function(TID) { if (!this.arThreads[TID]) return; this.arThreads[TID].Clear(); this.arThreads[TID] = null; } CAjax.prototype._SetHandler = function(TID) { var oAjax = this; function __cancelQuery(e) { if (!e) e = window.event if (!e) return; if (e.keyCode == 27) { oAjax._Close(TID); jsEvent.removeEvent(document, 'keypress', this); } } function __handlerReadyStateChange() { if (oAjax.bCancelled) return; if (!oAjax.arThreads[TID]) return; if (!oAjax.arThreads[TID].httpRequest) return; if (oAjax.arThreads[TID].httpRequest.readyState == 4) { var status = oAjax.arThreads[TID].httpRequest.getResponseHeader('X-Bitrix-Ajax-Status'); var bRedirect = (status == 'Redirect'); var s = oAjax.arThreads[TID].httpRequest.responseText; jsAjaxParser.mode = 'implode'; s = jsAjaxParser.process(s); if (!bRedirect) oAjax._OnDataReady(TID, s); oAjax.__prepareOnload(); if (jsAjaxParser.code.length > 0) jsAjaxUtil.EvalPack(jsAjaxParser.code); oAjax.__runOnload(); //setTimeout(function() {alert(1); oAjax.__runOnload(); alert(2)}, 30); oAjax._Close(TID); } } this.arThreads[TID].httpRequest.onreadystatechange = __handlerReadyStateChange; jsEvent.addEvent(document, "keypress", __cancelQuery); } CAjax.prototype.__prepareOnload = function() { this.obTemporary = window.onload; window.onload = null; } CAjax.prototype.__runOnload = function() { if (window.onload) window.onload(); window.onload = this.obTemporary; this.obTemporary = null; } CAjax.prototype.Send = function(TID, url, arData) { if (!this.arThreads[TID]) return; if (null != arData) var data = this._PrepareData(arData); else var data = ''; if (data.length > 0) { if (url.indexOf('?') == -1) url += '?' + data; else url += '&' + data; } if(this.arThreads[TID].httpRequest) { this.arThreads[TID].httpRequest.open("GET", url, true); this._SetHandler(TID); return this.arThreads[TID].httpRequest.send(""); } } CAjax.prototype.Post = function(TID, url, arData) { var data = ''; if (null != arData) data = this._PrepareData(arData); if(this.arThreads[TID].httpRequest) { this.arThreads[TID].httpRequest.open("POST", url, true); this._SetHandler(TID); this.arThreads[TID].httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); return this.arThreads[TID].httpRequest.send(data); } } /* public CAjaxForm - class to send forms via iframe */ function CAjaxForm(obForm, obHandler, bFirst) { this.obForm = obForm; this.obHandler = obHandler; this.obFrame = null; this.isFormProcessed = false; if (null == bFirst) this.bFirst = false; else this.bFirst = bFirst; this.__tmpFormTarget = ''; this.obAJAXIndicator = null; this.currentBrowserDetected = ""; if (window.opera) this.currentBrowserDetected = "Opera"; else if (navigator.userAgent) { if (navigator.userAgent.indexOf("MSIE") != -1) this.currentBrowserDetected = "IE"; else if (navigator.userAgent.indexOf("Firefox") != -1) this.currentBrowserDetected = "Firefox"; } this.IsIE9 = !!document.documentMode && document.documentMode >= 9; } CAjaxForm.prototype.setProcessedFlag = function(value) { if (null == value) value = true; else value = value ? true : false; this.obForm.bxAjaxProcessed = value; this.isFormProcessed = value; } CAjaxForm.isFormProcessed = function(obForm) { if (obForm.bxAjaxProcessed) return obForm.bxAjaxProcessed; else return false; } CAjaxForm.prototype.process = function() { var _this = this; function __formResultHandler() { if (!_this.obFrame.contentWindow.document || _this.obFrame.contentWindow.document.body.innerHTML.length == 0) return; if (null != _this.obHandler) { _this.obHandler(_this.obFrame.contentWindow.document.body.innerHTML); } if (_this.obFrame.contentWindow.AJAX_runExternal) _this.obFrame.contentWindow.AJAX_runExternal(); if (_this.obFrame.contentWindow.AJAX_runGlobal) _this.obFrame.contentWindow.AJAX_runGlobal(); if (_this.bFirst) { try { _this.obForm.target = _this.__tmpFormTarget; _this.obAJAXIndicator.parentNode.removeChild(_this.obAJAXIndicator); _this.obForm.bxAjaxProcessed = false; } catch (e) { _this.obForm = null; } _this.obAJAXIndicator = null; if (this.currentBrowserDetected != 'IE') jsEvent.removeAllEvents(_this.obFrame); // fixing another strange bug. Now for FF var TimerID = setTimeout("document.body.removeChild(document.getElementById('" + _this.obFrame.id + "'));", 100); _this.obFrame = null; if (window.onFormLoaded) { window.onFormLoaded(); window.onFormLoaded = null; } } } if (this.obForm.target && this.obForm.target.substring(0, 5) == 'AJAX_') return; if (this.currentBrowserDetected == 'IE') { if (this.IsIE9) { this.obAJAXIndicator = document.createElement('input'); this.obAJAXIndicator.setAttribute('name', 'AJAX_CALL'); this.obAJAXIndicator.setAttribute('type', 'hidden'); } else { this.obAJAXIndicator = document.createElement(''); } } else { this.obAJAXIndicator = document.createElement('INPUT'); this.obAJAXIndicator.type = 'hidden'; this.obAJAXIndicator.name = 'AJAX_CALL'; } this.obAJAXIndicator.value = 'Y'; this.obForm.appendChild(this.obAJAXIndicator); var frameName = 'AJAX_' + Math.round(Math.random() * 100000); if (this.currentBrowserDetected == 'IE') if (this.IsIE9) { this.obFrame = document.createElement('iframe'); this.obFrame.setAttribute('name', frameName); } else { this.obFrame = document.createElement(''); } else this.obFrame = document.createElement('IFRAME'); this.obFrame.style.display = 'none'; this.obFrame.src = 'javascript:\'\''; this.obFrame.id = frameName; this.obFrame.name = frameName; document.body.appendChild(this.obFrame); this.__tmpFormTarget = this.obForm.target; this.obForm.target = frameName; // one more strange bug in IE.. if (this.currentBrowserDetected == 'IE') this.obFrame.attachEvent("onload", __formResultHandler); else jsEvent.addEvent(this.obFrame, 'load', __formResultHandler); this.setProcessedFlag(); } var jsAjaxParser = { code: [], mode: 'implode', regexp: null, regexp_src: null, process: function(s) { this.code = []; if (null == this.regexp) this.regexp = /(]*)>)([\S\s]*?)(<\/script>)/i; do { var arMatch = s.match(this.regexp); if (null == arMatch) break; var pos = arMatch.index; var len = arMatch[0].length; if (pos > 0) this.code.push({TYPE: 'STRING', DATA: s.substring(0, pos)}); if (typeof arMatch[1] == 'undefined' || arMatch[1].indexOf('src=') == -1) { var script = arMatch[3]; script = script.replace('