/*
	dynjax.js
	(c) DeRose Technologies, Inc. 2005
*/

function radiogroupValueGet( strIdRoot ) {
	var i = 1;
	
	var obj;
	
	while ( obj = document.getElementById( strIdRoot+i ) ) {	
		if (obj.checked == true)
			break;
		i++;
	}
	
	if (obj != undefined)
		return obj.value;
	
	return null;
}

function Dynjax (objJpServer) {
	objJpServer.timeout = 3000;
	objJpServer.clientErrorFunc 		= this.errorBannerOn;
	objJpServer.applicationErrorFunc = this.errorBannerOn;
	objJpServer.serverErrorFunc  		= this.errorBannerOn;
	objJpServer.errorBannerOff 		= this.errorBannerOff;

	this.objJpServer = objJpServer;
	this.aDynErrors = Array();
};

Dynjax.prototype = {
	serverTo: function ( sStepPath, strOp, aParams ) {
		aInOperations = this.objJpServer.dynjaxfromclient(sStepPath, strOp, aParams);

		if ( aInOperations && (typeof aInOperations == 'object') && (aInOperations.constructor == Array) ) {
			if (document.hasAlertedErr) {
				this.errorBannerOff();
			}
			return this.serverFrom (aInOperations);
		}
		
		return;
	},
	
	errorBannerOff: function () {
		objSpanErrBanner = document.getElementById('divErrNoticeBanner');
		
		objSpanErrBanner.style.visibility = 'hidden';
		objSpanErrBanner.style.display 	 = 'none';
	},
	
	errorBannerOn: function (e) {
		objSpanErrBanner = document.getElementById('divErrNoticeBanner');
	
		if (typeof e == 'string') {
			objSpanErrBanner.innerHTML = 'Error: '+e;
		}	
		else {
			try {
			   var errorMsg = e.name+': ';
			   if ( e.code ) {
			       errorMsg += '('+e.code+')';
			   }
			   errorMsg += ' '+e.message;
			} catch (ex) {
			   var errorMsg = '('+type+') ';
			   if ( e.code ) {
			       errorMsg += '('+e.code+')';
			   }
			   errorMsg += ' '+e.message;
			}
			
			if ( e.client && e.call ) {
			   errorMsg += "<BR>Method called: "
			       +e.client+ "."+e.call+"()";
			}
		
			objSpanErrBanner.innerHTML = errorMsg;
		}
		
		objSpanErrBanner.style.visibility = 'visible';
		objSpanErrBanner.style.display 	= 'block';
		
		document.hasAlertedErr = true;
		
		return;
	},

	serverFrom: function ( aInOperations ) {
		
		objDivErrBanner = document.getElementById('divErrNoticeBanner');
		
		// Check for prior error records and if present, remove..
		if (objDivErrBanner.style.visibility == 'visible') {		
			var strElementErr;
			while ( strElementErr = this.aDynErrors.pop() ) {
				document.getElementById(strElementErr).style.visibility = 'hidden';
			}
			
			;
			objDivErrBanner.style.visibility = 'hidden';
			objDivErrBanner.style.display 	= 'none';
			
		}
		
		for (var i=0; i < aInOperations.length; i++) {
			switch (aInOperations[i][0]) {
				// Error-control: //////////////////
				case 'error':
					this.aDynErrors.length = 0;

					objDivErrBanner.innerHTML			=  'Error: '+aInOperations[i][1];
					objDivErrBanner.style.visibility = 'visible';
					objDivErrBanner.style.display 	= 'block';
										
					for(j=0;j<aInOperations[i][2].length;j++) {
						document.getElementById(aInOperations[i][2][j]).style.visibility = 'visible';	
						this.aDynErrors.push(aInOperations[i][2][j]);
					}
					break;
					
				// Span/Div Ops : //////////////////
				case 'spanhide':
				case 'divhide':
					document.getElementById(aInOperations[i][1]).style.visibility = 'hidden';
					break;
				case 'spanshow':
				case 'divshow':
					document.getElementById(aInOperations[i][1]).style.visibility = 'visible';
					break;
				
				case 'spanupdate':
				case 'divupdate':
					document.getElementById(aInOperations[i][1]).innerHTML = aInOperations[i][2];
					break;

				// Input Ops : /////////////////////
				case 'inputupdate':
					var objSelect = document.getElementById(aInOperations[i][1]);
					
					objSelect.options.length =0;
					
					aOptions = aInOperations[i][3];
					for(j=0;j<aOptions.length;j++) {
						objSelect.options[j] = new Option(aOptions[j][1], aOptions[j][0]);
					}	
					objSelect.value = aInOperations[i][2];
					
					objSelect.disabled =false;
					break;
				case 'inputdisable':
					document.getElementById(aInOperations[i][1]).disabled =true;
					document.getElementById(aInOperations[i][1]).length = 1;
					break;
				case 'inputenable':
					document.getElementById(aInOperations[i][1]).disabled =false;
					
					break;
				// Prompt Ops : /////////////////////
				case 'continueornext':
					var strAlertPrompt = aInOperations[i][1];
					
					Dialog.confirm(
		            strAlertPrompt,
			         {
			            windowParameters: {
			                width:550,
			                closable: true,
			                className: "united_popups",
			                title: "",
			                showEffectOptions: {
			                    duration: 0.2
			                },
			                hideEffectOptions: {
			                    duration: 0.2
			                }
			                  
			            },
		               okLabel: "Yes",
	                  cancelLabel: "No",
	                  cancel:function(win) {
	                  	$("buttonNext").click();
	                  	return true;
	                  }
		            }
		        	);
					break;
			}
			
		}
	}

}