/*================================================================================*/
/*                                                                                */
/*             BW2Technologies.Web.JScript.Ajax (Alpha V1.0.0.0)                  */
/*                                                                                */
/*              Copyright © 2006-2010 by BW2Technologies (HP. Lassnig)            */
/*                           mail to : hansi@lassnig.ch                           */
/*                                                                                */
/*================================================================================*/

var _oAjaxRequests = new Array();
var _oAjaxShowStatusObj = null;
var _oResponse = null;

var _fAjaxAsync = false; 
var _strAjaxURL; 

/*--- Ajax-Jobs ---*/
var _fAjaxJobAsync = false;
var _strAjaxJobObjID = null;
var _strAjaxJobKey = null;

/*--- Ajax-Polling ---*/
var _fAjaxPolling = false;              /* Polling Enabled/Disabled */ 
var _nAjaxPollingInterval = 0;          /* Polling-Interval in ms */ 
var _nAjaxPollingInitTimeout = 10;      /* Wartezeit in ms bis Polling zum ersten mal ausgeführt wird */

/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/


/*================================================================================*/
/* <Function> Ajax Error </Function>
/*================================================================================*/
function __bw2AjaxError(strName, strDescription, lNumber) {
         this.Name = strName;
	     this.Description = strDescription;
	     this.Number = lNumber;

	     return this; 
}

/*--------------------------------------------------------------------------------*/

__bw2AjaxError.prototype.toString = function() { return this.Name + " " + this.Description; }

/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/


/*================================================================================*/
/* <Function> Create XMLHttpRequest-Object [Cross-Browser] </Function>
/*================================================================================*/
function __bw2AjaxGetXMLHttpRequest() {

         /*--- For IE7, Firefox, Mozilla, Netscape, Opera, Safari, .. ---*/
	     if (window.XMLHttpRequest) {
		    var oXMLR = new XMLHttpRequest();
		    if (oXMLR.overrideMimeType) oXMLR.overrideMimeType('text/html');
		    return oXMLR;

         /*--- For Microsoft-IE < 7.x ---*/
	     } else {
		    if (window.ajaxXMLHttpRequestProgID) {
		       var oXMLR = new ActiveXObject(window.ajaxXMLHttpRequestProgID);
               return oXMLR;

		    } else {
		       var strProgIDs = ['Msxml2.XMLHTTP', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'Microsoft.XMLHTTP'];
			         
		       for (var nI = 0; nI < strProgIDs.length; ++nI) {
		           var strProgID = strProgIDs[nI];

		           try {
         	           var oXMLR = new ActiveXObject(strProgID);
		               window.ajaxXMLHttpRequestProgID = strProgID;
			           return oXMLR;
			       } catch (e) {
                       __bw2Debug('__bw2AjaxGetXMLHttpRequest.Error : ' + e);
			       }
			   }
		    }
	     }

	     return null;
}

/*================================================================================*/
/* <Function> __bw2AjaxURL </Function>
/*================================================================================*/
function __bw2AjaxURL() {
         if (_strAjaxURL == null) _strAjaxURL = this.location.href.replace(this.location.search, '');
         return _strAjaxURL;
}

/*================================================================================*/
/* <Function> __bw2AjaxCreateRequest </Function>
/*================================================================================*/
function __bw2AjaxCreateRequest(strContext, strTagName, strURL, strSendData) {
         for (var nI = 0; nI < _oAjaxRequests.length; nI++)	{
             if (_oAjaxRequests[nI].Obj.readyState == 4) {
                _oAjaxRequests[nI].Obj.abort();
                _oAjaxRequests[nI].Context = strContext;
		        _oAjaxRequests[nI].TagName = strTagName;
		        _oAjaxRequests[nI].URL = strURL;
		        _oAjaxRequests[nI].SendData = strSendData;

		        return _oAjaxRequests[nI];
 	         }
         }

         var nPos = _oAjaxRequests.length;

         _oAjaxRequests[nPos] = Object();
         _oAjaxRequests[nPos].Obj = new __bw2AjaxGetXMLHttpRequest();
         _oAjaxRequests[nPos].Index = nPos;
         _oAjaxRequests[nPos].Context = strContext;
         _oAjaxRequests[nPos].TagName = strTagName;
         _oAjaxRequests[nPos].URL = strURL;
         _oAjaxRequests[nPos].SendData = strSendData;

         return _oAjaxRequests[nPos];
}

/*================================================================================*/
/* <Function> __bw2AjaxGetData </Function>
/*================================================================================*/
function __bw2AjaxGetData(oRequest) {
	     if (oRequest.Obj.responseXML != null && oRequest.Obj.responseXML.xml != null && oRequest.Obj.responseXML.xml != '') {
            return oRequest.Obj.responseXML;
         } else if (oRequest.Obj.responseText != null && oRequest.Obj.responseText != '') {
            return oRequest.Obj.responseText;
         }
}

/*================================================================================*/
/* <Function> __bw2AjaxResponse </Function>
/*================================================================================*/
function __bw2AjaxResponse(oRequest) {
	     this.Request = oRequest.Obj;
	     this.Error = null;
	     this.Execute = null;
	     this.Value = null;
	     this.Context = oRequest.Context;
	     this.TagName = oRequest.TagName;

	     if (oRequest.Obj.status < 202)	{
	        try {
          	    /*--- Decode Request ---*/
          	    if (oRequest.Obj.status == 200)	{
	               this.Value = decodeURI(__bw2AjaxGetData(oRequest));

                /*--- Not decode Request (meisst Executes) ---*/
         	    } else if (oRequest.Obj.status == 201) {
	 	           this.Execute = __bw2AjaxGetData(oRequest);

                /*--- Execute Data ---*/
                if ((this.Execute) && (this.Execute.length > 0)) {
	               var nSP = this.Execute.indexOf('[{');

                   if (nSP > -1) {
                      var nSP2 = this.Execute.indexOf('}]') + 2;
                      this.Value = this.Execute.substr(nSP, nSP2 - nSP)
                      this.Execute = this.Execute.replace(this.Value, '');
			       }

			       try {
			           eval(this.Execute); 
			       } catch (ee) {
           		       this.Error = new __bw2AjaxError(ee.name, ee.description, ee.number);
     	               __bw2Debug('__bw2AjaxResponse.Execute-Error : ' + this.Error);
     	               alert('__bw2AjaxResponse.Execute-Error : ' + this.Error);
			       }
                   }
   	            }
			
			    if (this.Value && this.Value.error) {
				   this.Error = this.Value.error;
				   this.Value = null;
			    }
		    } catch(e) {
			    this.Error = new __bw2AjaxError(e.name, e.description, e.number);
     	        __bw2Debug('__bw2AjaxResponse.Error 1 : ' + this.Error);
     	        alert('__bw2AjaxResponse.Error 1 : ' + this.Error);
		    }
	     } else {
		    this.Error = new __bw2AjaxError('HTTP request failed with status: ' + oRequest.Obj.status, oRequest.Obj.status);
   	        __bw2Debug('__bw2AjaxResponse.Error 2 : ' + this.Error);
   	        alert('__bw2AjaxResponse.Error 2 : ' + this.Error);
	     }
	
	     return this;
}

/*================================================================================*/
/* <Function> __bw2AjaxPost </Function>
/*================================================================================*/
function __bw2AjaxPost(strContext, strData) {
         if (__bw2AjaxURL() == null) return;

         try {
             var oObj = new __bw2AjaxGetXMLHttpRequest();
             var strP = 'bw2ajaxpost|context=' + strContext + '|post=' + encodeURI(strData);

             oObj.open('POST', __bw2AjaxURL(), false);
 	         oObj.send(strP);
 	     
 	     } catch (e) {
	         alert('__bw2AjaxPost.Error : ' + e);
	         __bw2Debug('__bw2AjaxPost.Error : ' + e);
 	     }
}

/*================================================================================*/
/* <Function> __bw2AjaxRequest </Function>
/*================================================================================*/
function __bw2AjaxRequest(strContext, strTagName, oQuery, strOnEvent, oCallback, strOptions, fReturnValue) {
         var strURL = __bw2AjaxURL(); 

         if (strURL == null) return;
         if (oQuery == null) oQuery = '';

         if ((typeof (oQuery) == 'string') && (oQuery != '') && (oQuery.indexOf('|') > -1)) {
             oQuery = oQuery.replace(/\|/, '@@@');
         }

         /*--- Create Send-Data ---*/
         var strS = 'bw2ajaxrequest|context=' + strContext + '|query=' + encodeURI(oQuery);
         if (strOnEvent != null) strS += '|onevent=' + strOnEvent; 
         if (strOptions != null) strS += '|options=' + encodeURI(strOptions); 
         strS += '|';

         /*--- Create Request-Object ---*/
         var oRequest = __bw2AjaxCreateRequest(strContext, strTagName, strURL, strS);
         var fAsync = typeof(oCallback) == 'function';
 
         if (fAsync) {
            try {
                oRequest.Obj.onreadystatechange = function() { if (oRequest.Obj.readyState == 4) oCallback(new __bw2AjaxResponse(oRequest)); }
                oRequest.Obj.open('POST', strURL, fAsync);
                oRequest.Obj.send(strS);
            } catch (e) {
  	            alert('__bw2AjaxRequest.Async.Error : ' + e);
	        }   
	     } else if (fReturnValue) {
            try {
                oRequest.Obj.open('POST', strURL, fAsync);
                oRequest.Obj.send(strS);
    	        return new __bw2AjaxResponse(oRequest);  
            } catch (e) {
  	            alert('__bw2AjaxRequest.Error : ' + e + ', ' + strS);
	        }   
	     } else {
	        setTimeout("__bw2AjaxRequestInt(" + oRequest.Index + ")", 1);
	     }
}

/*--------------------------------------------------------------------------------*/

  function __bw2AjaxRequestInt(nIndex) {
         try {
             _oResponse = null;
         
             with (_oAjaxRequests[nIndex]) {
                  Obj.open('POST', URL, false);
                  Obj.send(SendData);
	         }

	         _oResponse = new __bw2AjaxResponse(_oAjaxRequests[nIndex]);  
         } catch (e) {}   
}

/*================================================================================*/
/* <Function> Ajax Async-CallBack </Function>
/*================================================================================*/
function __bw2AjaxAsyncCallBack(strContext, strQuery, nDelay) {
         if ((nDelay != null) && (nDelay > 0)) {
            setTimeout("__bw2AjaxAsyncCallBack('" + strContext + "', '" + strQuery + "')", nDelay);
         } else {
            __bw2AjaxRequest(strContext, null, strQuery, 'onasynccallback', __bw2AjaxAsyncCallBackResponse);
         }
}

/*--------------------------------------------------------------------------------*/

function __bw2AjaxAsyncCallBackResponse(oRequest) {}

/*================================================================================*/
/* <Function> __bw2AjaxStop </Function>
/*================================================================================*/
function __bw2AjaxStop() {
	     for (var nI = 0; nI < _oAjaxRequests.length; nI++) {
	         if(_oAjaxRequests[nI] != null)	_oAjaxRequests[nI].Obj.abort();
	     }
}

/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/


/*================================================================================*/
/* <Function> __bw2AjaxJobOnExecute </Function>
/*================================================================================*/
function __bw2AjaxJobOnExecute(strObjID, strJobKey, nWait, fScreenLock, fAsync) {
         if ((strObjID == null) || (strJobKey == null)) return;
         if (nWait == null) nWait = 10;
         if (fAsync != null) _fAjaxJobAsync = fAsync; else fAsync = false;

         _strAjaxJobObjID = strObjID;
         _strAjaxJobKey = strJobKey;

         if (fScreenLock) __bw2ScreenLock();

         if (nWait > 0) {
            setTimeout('__bw2AjaxJobRequest()', nWait);
         } else {
            __bw2AjaxJobRequest();
         }
}

/*================================================================================*/
/* <Function> __bw2AjaxJobRequest </Function>
/*================================================================================*/
function __bw2AjaxJobRequest() {
         if (_fAjaxJobAsync) {
            __bw2AjaxRequest(_strAjaxJobObjID, null, _strAjaxJobKey, 'onjobexecute', __bw2AjaxJobResponse, null);
         } else {
            __bw2AjaxJobResponse(__bw2AjaxRequest(_strAjaxJobObjID, null, _strAjaxJobKey, 'onjobexecute', null, null, true));
         }            
}

/*================================================================================*/
/* <Function> __bw2AjaxJobResponse </Function>
/*================================================================================*/
function __bw2AjaxJobResponse(oRequest) {
         __bw2Loading(false);
}

/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/


/*================================================================================*/
/* <Function> __bw2AjaxObjOnEvent </Function>
/*================================================================================*/
function __bw2AjaxObjOnEvent(oEvent, oObj, strOnEvent, strValue, fAsync, fLoading) {
         if (!__bw2AjaxURL()) return;
         if (strOnEvent == null) return;
         if (fAsync == null) fAsync = _fAjaxAsync;

         oObj = __bw2GetElementById(oObj);
         if ((oObj == null) || (oObj.disabled)) return;

         var oRequest = null; 
         var strQuery = null;
         var strOption = null;

         if (fLoading) __bw2Loading(true);
         if (oObj.value) strQuery = oObj.value;
         if (strValue != null) strQuery = strValue;

         switch (strOnEvent) {
                case 'onchange' :
                     try {
                         if (oEvent.keyCode != null) strOption = 'keycode:' + oEvent.keyCode;
                         __bw2Debug(strOnEvent + ', ' + oObj.id + ', ' + oEvent.keyCode + ', ' + strOption);
                     } catch (e) {
                     }

                     if (fAsync) {
                        __bw2AjaxRequest(oObj.id, oObj.tagName, strQuery, strOnEvent, __bw2AjaxObjResponseOnChange, strOption);
                     } else {    
                        __bw2AjaxObjResponseOnChange(__bw2AjaxRequest(oObj.id, oObj.tagName, strQuery, strOnEvent, null, strOption, true));
                     }
                       
                     break;             

                case 'oncheck' :
                     if (fAsync) {
                        __bw2AjaxRequest(oObj.id, oObj.tagName, strQuery, strOnEvent, __bw2AjaxObjResponseOnCheck);
                     } else {    
                        __bw2AjaxObjResponseOnCheck(__bw2AjaxRequest(oObj.id, oObj.tagName, strQuery, strOnEvent, null, null, true));
                     }
                       
                     break;             

                case 'ondblclick':
                     __bw2Loading(true);

                case 'onclick' : 
                     if (fAsync) {
                        __bw2AjaxRequest(oObj.id, oObj.tagName, strQuery, strOnEvent, __bw2AjaxObjResponseOnClick);
                     } else {    
                        //__bw2AjaxObjResponseOnClick(__bw2AjaxRequest(oObj.id, oObj.tagName, strQuery, strOnEvent, null, null, true));
                        __bw2AjaxRequest(oObj.id, oObj.tagName, strQuery, strOnEvent);
                     }
                       
                     break;

                case 'onkeypressesc' :  
                     if (oEvent.keyCode != 27) return;

                     if (fAsync) {
                        __bw2AjaxRequest(oObj.id, oObj.tagName, strQuery, strOnEvent, __bw2AjaxObjResponseOnClick);
                     } else {    
                        __bw2AjaxObjResponseOnClick(__bw2AjaxRequest(oObj.id, oObj.tagName, strQuery, strOnEvent, null, null, true));
                     }
                       
                     break;             
                     
                case 'onkeypressreturn' :  
                     if (oEvent.keyCode != 13) return;

                     if (fAsync) {
                        __bw2AjaxRequest(oObj.id, oObj.tagName, strQuery, strOnEvent, __bw2AjaxObjResponseOnClick);
                     } else {    
                        __bw2AjaxObjResponseOnClick(__bw2AjaxRequest(oObj.id, oObj.tagName, strQuery, strOnEvent, null, null, true));
                     }
                       
                     break;             

                default:
                     if (fAsync) {
                        __bw2AjaxRequest(oObj.id, oObj.tagName, strQuery, strOnEvent, __bw2AjaxObjResponseOn);
                     } else {    
                        __bw2AjaxObjResponseOn(__bw2AjaxRequest(oObj.id, oObj.tagName, strQuery, strOnEvent, null));
                     }
                       
                     break;             
         }
}

/*================================================================================*/
/* <Function> __bw2AjaxObjResponseOn.. </Function>
/*================================================================================*/
function __bw2AjaxObjResponseOn(oRequest) {
         __bw2Loading(false);
         //__bw2Debug('__bw2AjaxObjResponseOn : ' + oRequest.Context + ' -> ' + oRequest.Value);
}

/*================================================================================*/
/* <Function> __bw2AjaxObjResponseOnChange </Function>
/*================================================================================*/
function __bw2AjaxObjResponseOnChange(oRequest) {
         __bw2Loading(false);

         if ((!oRequest) || (!oRequest.Value)) return;

         try {
             eval(oRequest.Value);
             if (_oResponse) __bw2GetElementById(_oResponse.InputID).value = _oResponse.Text;
             
         } catch (e) {
             alert('__bw2AjaxObjResponseOnChange : ' + e);
         }   
}

/*================================================================================*/
/* <Function> __bw2AjaxObjResponseOnCheck </Function>
/*================================================================================*/
function __bw2AjaxObjResponseOnCheck(oRequest) {
         __bw2Loading(false);

         if ((!oRequest) || (!oRequest.Value)) return;

         try {
             eval(oRequest.Value);
             if (_oResponse) __bw2GetElementById(_oResponse.InputID).src = _oResponse.Image;

         } catch (e) {
             alert('__bw2AjaxObjResponseOnCheck : ' + e);
         }   
}

/*================================================================================*/
/* <Function> __bw2AjaxObjResponseOnClick </Function>
/*================================================================================*/
function __bw2AjaxObjResponseOnClick(oRequest) {
         //__bw2Loading(false);

         //var oObj = __bw2GetElementById(oRequest.Context);
         //__bw2Debug('__bw2AjaxObjResponseOnClick : ' + oRequest.Context + ' -> ' + oRequest.Value);
}

/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/


/*================================================================================*/
/* <Function> Initialisierung des Pollings über Ajax </Function>
/*================================================================================*/
function __bw2AjaxPollingInit(nInterval, nInitTimeout) {
         if (nInterval) _nAjaxPollingInterval = nInterval;
         if (nInitTimeout) _nAjaxPollingInitTimeout = nInitTimeout;

         _fAjaxPolling = true;
    
         if (_nAjaxPollingInterval > 0) {
            if (_nAjaxPollingInitTimeout > 0) {
               setTimeout('__bw2AjaxPolling()', _nAjaxPollingInitTimeout);
            } else {
               setTimeout('__bw2AjaxPolling()', _nAjaxPollingInterval);
            }
         }
}

/*================================================================================*/
/* <Function> Polling einschalten </Function>
/*================================================================================*/
function __bw2AjaxPollingON() {
         _fAjaxPolling = true;
         if (_nAjaxPollingInterval > 0) setTimeout('__bw2AjaxPolling()', _nAjaxPollingInterval);
}

/*================================================================================*/
/* <Function> Polling ausschalten </Function>
/*================================================================================*/
function __bw2AjaxPollingOFF() {
         _fAjaxPolling = false;
}

/*================================================================================*/
/* <Function> Polling </Function>
/*================================================================================*/
function __bw2AjaxPolling() {
         if (!_fAjaxPolling) return;

         try {
             __bw2AjaxRequest('__POLLING', null, 'POLLING', null, null, null);

             if (_nAjaxPollingInterval > 0) setTimeout('__bw2AjaxPolling()', _nAjaxPollingInterval);
         } catch (e) {
             if (_strFailoverURL != null) window.location = _strFailoverURL;
         }
}

/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/


/*================================================================================*/
/* <Function> __bw2AjaxRedirect </Function>
/*================================================================================*/
function __bw2AjaxRedirect(strRedirect) {
         if (strRedirect) __bw2AjaxRequest('__REDIRECT', null, strRedirect, 'onredirect', null, null);
}

/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/


/*================================================================================*/
/* <Function> Form Inits </Function>
/*================================================================================*/
function __bw2AjaxOnFormInits() {
         try {
 	           var oForm = __bw2GetElementById('oForm');

             if (oForm) {
                var strA = oForm.action;
                var nP = strA.indexOf('?');
                   
                if (nP > -1) oForm.action = strA.substring(0, nP);

                oForm.action += '?IgnorePost';
                oForm.onsubmit = __bw2AjaxOnFormSubmit;
             }
         } catch (e) {
         }
}

/*================================================================================*/
/* <Function> Form submit </Function>
/*================================================================================*/
function __bw2AjaxOnFormSubmit() {

}

/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/
