function ClientWebServiceCaller( returnUrl, returnScript, submitMethodName, keys, webServiceCalls, autoPostBack, prgBarClientID, areProgressBarsVisible, areSubProgressBarsVisible, progressChangedCallback ) { var self = this; this._tracingEnabled = true; this._traceHistory = new Array(0); // For Progress Bars this._isCalculatingProgress = false; this._progress = new Array(0); this._areSubProgressBarsVisible = areSubProgressBarsVisible; this._areProgressBarsVisible = areProgressBarsVisible; this._autoPostBack = autoPostBack; this._prgBarClientID = prgBarClientID; this._progressChangedCallback = progressChangedCallback; // For web service calls this._webServiceCalls = webServiceCalls; this._keys = keys; this._returnUrl = returnUrl; this._returnScript = returnScript; this._submitMethodName = submitMethodName; this._isFinished = new Array( this._webServiceCalls.length ); // Trace Method this.trace = function( message ) { if( this._tracingEnabled ) { $get('debug').innerText += '\r\n' + message; this._traceHistory.push( message ); } } // Web Service Process Methods this.doAllFinished = function() { this._isAllFinished = true; try { if( this._returnScript != '' ) { eval( this._returnScript ); } if( this._returnUrl != '' ) { window.location = this._returnUrl; } //alert( 'Finished' ); } catch (error) { alert( error.toJSONString() ); $get( 'progressBarDisplay' ).style.display = "none"; } } this.checkForAllFinished = function() { var isAllFinished = true; for( var i = 0; i < this._isFinished.length; i++ ) { if( !this._isFinished[i] ) { isAllFinished = false; } } if( isAllFinished ) { // All Finished // self required to retain reference to this setTimeout( function() { self.reportProgress(); }, 100 ); setTimeout( "$get( '" + this._prgBarClientID + "' ).setTitle( 'Finished' )", 150 ); if( this._autoPostBack ) { setTimeout( function() { self.doAllFinished(); }, 200 ); } else { $get( 'btnAllFinished' ).style.display = 'inline'; } } } this.markFinished = function( index ) { this._isFinished[ index ] = true; this.checkForAllFinished(); } this.OnSucceeded = function( result, index, methodName ) { // This is required to get back a reference to this self.OnSucceededThis( result, index, methodName ); } this.OnSucceededThis = function( result, index, methodName ) { this.trace( 'Succeeded: ' + result ); this.trace( 'OnSucceeded index=' + index ); this.updateProgress( index, 1.0 ); this.markFinished( index ); } this.OnFailed = function( error, index ) { // This is required to get back a reference to this self.OnFailedThis( error, index ); } this.OnFailedThis = function( error, index ) { // alert( 'Failed: ' + error.toJSONString() ); // alert( 'userContext=' + index ); //this.UpdateProgress( index, 0 ); $get( this._prgBarClientID + index ).setTitle( "Failed" ); this.markFinished( index ); this.displayFailure(); } // Use ajax to submit contents this.submitContents = function( index, key, jsonData ) { this.updateProgress( index, 0.7 ); var resultText = formatContents( jsonData ); var userContext = index; this.trace( 'submitContents key=' + key + 'resultText=' + resultText ); var submitMethod = eval( this._submitMethodName ); submitMethod( key, resultText, self.OnSucceeded, self.OnFailed, index ); this.trace( 'submittedContents key=' + key ); } this.callBack = function( index, key, jsonData ) { this.updateProgress( index, 0.5 ); this.trace( 'callBack' ); this.submitContents( index, key, jsonData ); } this.callWebService = function( webServiceCall, index ) { this.updateProgress( index, 0.1 ); this.trace( 'Begin callWebService: webServiceCall[' + index + ']=' + webServiceCall ); // Create a new script object jsonObj = new JSONscriptRequest( webServiceCall ); // Build the script tag jsonObj.buildScriptTag(); this.trace( 'Calling Web Service from Client' ); // Execute (add) the script tag jsonObj.addScriptTag(); } // Progress Bar Methods this.reportProgress = function() { if( this._areProgressBarsVisible || this._progressChangedCallback != null ) { // Wrap for multi threading if( !this._isCalculatingProgress ) { try { this._isCalculatingProgress = true; // Calculate Progress var total = 0; // Calculate total and report progress for each individual bar for( var i = 0; i < this._progress.length; i++ ) { total += this._progress[i] / this._progress.length; try { if( this._areSubProgressBarsVisible && $get( this._prgBarClientID + i ).reportProgress != undefined ) { $get( this._prgBarClientID + i ).reportProgress( this._progress[i] ); } } catch(err){ this.trace( 'reportProgress failed: Cannot report sub progress: ' + err.description ); } } // Report Progress if( this._areProgressBarsVisible ) { $get(this._prgBarClientID).reportProgress( total ); } if( this._progressChangedCallback != null ) { this._progressChangedCallback( total ); } this.trace( 'Calculated Progress: total=' + total ); } catch(err) { this.trace( 'reportProgress failed: ' + err.description ); } finally { this._isCalculatingProgress = false; } } } } this.updateProgress = function( index, amount ) { this.trace( 'update Progress index=' + index + ' amount=' + amount ); // Make sure the index exists while( index >= this._progress.length ) { this._progress.push( 0 ); } // Update progress for index this._progress[ index ] = amount; this.reportProgress(); // Do sliding timeout //this.prepareTimeout(); } // Main Method this.makeQueries = function() { for( var i = 0; i < this._webServiceCalls.length; i++ ) { this.updateProgress( i, 0.01 ); } for( var i = 0; i < this._webServiceCalls.length; i++ ) { this.callWebService( this._webServiceCalls[i], i ); } } this.displayFailure = function() { // Don't let any resizing stop the process if( this._areProgressBarsVisible ) { try { var progressBarDisplay = $get( 'progressBarDisplay' ); progressBarDisplay.style.display = "none"; // Callback failure if( this._progressChangedCallback != null ) { this._progressChangedCallback( -1 ); } } catch (error) { alert( error.toJSONString() ); } } else { //alert( 'Running in Background: ' + this._webServiceCalls.length + ' Calls'); } } this.displayProgressBars = function() { // Don't let any resizing stop the process if( this._areProgressBarsVisible ) { try { var progressBarDisplay = $get( 'progressBarDisplay' ); var progressBar = $get( 'progressBar' ); var progressBarBackground = $get( 'progressBarBackground' ); progressBarDisplay.style.display = "block"; // Resize and center the area //progressBar.style.position = "absolute"; //progressBar.style.left = ( document.documentElement.offsetWidth - progressBar.offsetWidth ) / 2 + 'px'; // Center //progressBar.style.top = ( document.documentElement.offsetHeight - progressBar.offsetHeight ) / 4 + 'px'; // 1/4 to top // Adjust the background if Modal mode progressBarBackground.style.width = screen.availWidth + 'px'; progressBarBackground.style.height = screen.availHeight + 'px'; } catch (error) { alert( error.toJSONString() ); } } else { //alert( 'Running in Background: ' + this._webServiceCalls.length + ' Calls'); } } this.timeout = function( timeoutID ) { this.trace( 'timeout: timeoutID=' + timeoutID + 'this._lastTimeoutID=' + this._lastTimeoutID ); // Only respond to the last timeout call if not finished if( this._lastTimeoutID == timeoutID && !this._isAllFinished ) { this.trace( 'Timed Out - timeout: timeoutID=' + timeoutID + 'this._lastTimeoutID=' + this._lastTimeoutID ); this.displayFailure(); this._isAllFinished = true; } } this.prepareTimeout = function() { this._lastTimeoutID++; setTimeout( function( lastTimeoutID ) { self.timeout( lastTimeoutID ); }, 30000, this._lastTimeoutID ); } this.executeClientSideCalls = function(){ this._isAllFinished = false; this._progress = new Array(0); this._updateProgressLastAttempts = new Array(0); this.displayProgressBars(); //this.makeQueries(); setTimeout( function() { self.makeQueries(); }, 500 ); this._lastTimeoutID = 0; this.prepareTimeout(); } }