/*
COPYRIGHT 1995-2005 ESRI

TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
Unpublished material - all rights reserved under the 
Copyright Laws of the United States.

For additional information, contact:
Environmental Systems Research Institute, Inc.
Attn: Contracts Dept
380 New York Street
Redlands, California, USA 92373

email: contracts@esri.com
*/


function addErrorPostBackHandler () {
	EsriControls.addPostBackTagHandler("jitk-errors", errorHandler);
}

function errorHandler(xml, eventSources) {	
	// create the error array
	var errors = new Array();
	var messageType = null;
	
	var errorNL = xml.getElementsByTagName("jitk-error");
	
	if(errorNL && errorNL.length > 0) {
	
		for(i=0; i < errorNL.length; i++) {
			errorNode = errorNL.item(i);
		
			if(messageType == null) {
				messageType = errorNode.getAttribute("code");
			}
		
			if(errorNode.firstChild.nodeValue.length > 0) {
				errors[errors.length] = errorNode.firstChild.nodeValue;
			}
		}
	}
	
	// hide the loading indicator on the map
	for(i=0; i < EsriControls.maps.length; i++) {
		EsriControls.maps[i].hideLoading();
	}
	
	// find the corresponding taskwindow and populate message element
	var hasTaskErrorMsg = false;
	if(taskWindowManager) {
		var win = taskWindowManager.windows["win_EsriTaskCell_" + eventSources];
		
		if(win && win.element_message) {
			win.showMessage(messageType, errors);
			hasTaskErrorMsg = true;
			win.hasError = true;
		}
	}

	// fall-back for the case that the error could not be shown in the taskwindow
	if(!hasTaskErrorMsg && errors.length > 0) {
		var errorStr = "";
		for(i=0; i < errors.length; i++) {
			errorStr += "- " + errors[i] + "\n";
		}
		alert(errorStr);
	}
}
