/* utility definitinos */
var utility = new function() {

	this.getText = function(/*String*/template, /*Object or arguments*/args) {
		var map = {};
		if (!dojo.lang.isUndefined(args)) {
			map = (dojo.lang.isPureObject(args) ? args : dojo.lang.toArray(arguments, 1));
		}

		var text;
		try {
			text = template.replace(/\n/g, '<br />').replace(/\s+/g, ' '); // Normalize space characters
		} catch (e) { return ""; }

		var localization = dojo.i18n.getLocalization("res.i18n", "nsa");

		if (!dojo.lang.isUndefined(localization[text])) {
			text = localization[text];
		}
		else {
			dojo.debug("string not in locales: " + text);
		}

		text = text.replace(/\%\{(\w+)\}/g, function(match, key) {
			if (!dojo.lang.isUndefined(map[key])) {
				return map[key];
			}
			else if (!dojo.lang.isUndefined(localization[key])) {
				return localization[key];
			}
			else {
				return key;
			}
		});
		return text;
	};

	//
	// setLanguage traverses all supported widgets under rootWidget, then set the
	// locale strings, and resize if sizer given (in order to display properly)
	//
	// Support widget includes HTMLSpanElement, Button
	this.setLanguage = function(/*Widget*/rootWidget)
	{
		var spans = rootWidget.domNode.getElementsByTagName("span");

		for (var i = 0, len = spans.length; i < len; i++)
		{
			var c = dojo.html.getClass(spans[i]);
			if (c && c === "dispstr")
			{
				var t = dojo.dom.textContent(spans[i]);
				dojo.dom.replaceChildren(spans[i], document.createTextNode(utility.getText(t)));
			}
		}

		var widgets = rootWidget.getChildrenOfType("TabContainer");
		for (i = 0, len = widgets.length; i < len; i++) {
			widgets[i].onResized();
		}

		var widgets = dojo.widget.manager.getWidgetsByType("Button");
		for (i = 0, len = widgets.length; i < len; i++) {
			widgets[i]._sizeMyself();
		}

		widgets = rootWidget.getChildrenOfType("Form");
		for (i = 0, len = widgets.length; i < len; i++) {
			widgets[i].onResized();
			widgets[i].notifyChildrenOfResize();
		}
	};

	 /* makeUniqueNumber generates an unique number */
	this.makeUniqueNumber = function() {
		var d = new Date();

		return Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDay(),
				        d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(),
				        d.getUTCMilliseconds()) - parseInt(Math.random() * 1000, 10);
	};

	/* Internal use only */
	this._makeBlockingLayer = function(/*Widget*/container) {

		var bg = document.createElement("div");
		var widthChecker = (container.domNode.clientWidth > 0 ? container.domNode.clientWidth : "100%");
		var heightCheker = (container.domNode.clientHeight > 0 ? container.domNode.clientHeight : "100%");

		var attrs = ["width: " + widthChecker, "height: " + heightCheker, "position: absolute",
		"z-index: 1000", "background-color: white"].join(";");

		var progressBar = document.createElement("img");
		progressBar.setAttribute("src", "../user/images/ProgressBar.gif");

		dojo.html.setClass(bg, 'blockingLayer');
		dojo.html.setStyleAttributes(bg, attrs);

		var box = document.createElement("div");
		var box_attrs = ["border: 0px solid #999", "text-align: center", "color: #365A79", "background-color: white", "position: relative", "top: 40%"].join(";");
		dojo.html.setStyleAttributes(box, box_attrs);

		box.appendChild(progressBar);
		box.appendChild(document.createElement("br"));
		box.appendChild(document.createTextNode(utility.getText("Please wait") + "..."));

		dojo.dom.prependChild(box, bg);

		dojo.lfx.html.fade(box, {start: 0, end: 1}, 1618).play();

		return bg;
	};

	this._makeTransBlockingLayer= function(/*Widget*/container) {

		var bg = document.createElement("div");
		var widthChecker = (container.domNode.clientWidth > 0 ? container.domNode.clientWidth : "100%");
		var heightCheker = (container.domNode.clientHeight > 0 ? container.domNode.clientHeight : "100%");

		var attrs = ["width: " + widthChecker, "height: " + heightCheker, "position: absolute",
		"z-index: 1000", "background-color: gray", "opacity: 0.7"].join(";");

		dojo.html.setClass(bg, 'blockingLayer');
		dojo.html.setStyleAttributes(bg, attrs);

		return bg;
	};

	this.blockInput = function(/*Widget*/container) {

		// A blocking layer already existed?
		var list = container.domNode.getElementsByTagName("div");
		for (var i = 0, len = list.length; i < len; i++) {
			if (dojo.html.getClass(list[i]) == 'blockingLayer') {
				return;
			}
		}

		var layer = utility._makeBlockingLayer(container);
		dojo.dom.prependChild(layer, container.domNode);
	};

	this.blockInputTrans = function(/*Widget*/container) {

		// A blocking layer already existed?
		var list = container.domNode.getElementsByTagName("div");
		for (var i = 0, len = list.length; i < len; i++) {
			if (dojo.html.getClass(list[i]) == 'blockingLayer') {
				return;
			}
		}

		var layer = utility._makeTransBlockingLayer(container);
		dojo.dom.prependChild(layer, container.domNode);
	};

	this.unblockInput = function(/*Widget*/container) {

		// Find blocking layers and remove them
		var layers = container.domNode.getElementsByTagName("div");

		for (var i = 0, len = layers.length; i < len; i++) {
			if (dojo.html.getClass(layers[i]) == 'blockingLayer') {
				dojo.dom.removeNode(layers[i], true);
			}
		}
	};

	this.setupLoadingHint = function(/*Widgt*/container) {
		dojo.event.connect("before", container, "onDownloadEnd", function() { container.domNode.style.cursor= "wait"; dojo.html.setStyleAttributes(container.domNode, "display: none");});
		dojo.event.connect("before", container, "onLoad", function() {  dojo.html.setStyleAttributes(container.domNode, "display: block"); utility.blockInput(container);});
		//dojo.event.connect("after", container, "onLoad", function() { });
		dojo.event.connect("after", container, "onDownloadEnd", function() { container.domNode.style.cursor= "auto"; /*dojo.html.setStyleAttributes(container.domNode, "display: block");*/ });
	};

	this.containerLoadDone = function(/*Widget*/container) {
		if(container.domNode.style ){
			container.domNode.style.cursor = "auto";
		}

		utility.unblockInput(container);
	};

	this.fixSelect = function(/*Select*/_select, /*Boolean*/editable, /*String*/label, /*String*/value) {
		// Make text input not editable
		if (editable == false) {
			_select.textInputNode.style.cssText = "color: black; background: white";
			_select.textInputNode.readOnly = true;
			dojo.event.connect("after", _select.textInputNode, "onclick", _select, "handleArrowClick");
		}

		// Show initial value
		_select.setAllValues(label, value);
		var opt = document.createElement("div");
		opt.appendChild(document.createTextNode(label));
		opt.setAttribute("resultName", label);
		opt.setAttribute("resultValue", value);
		opt.className = "dojoComboBoxItem dojoComboBoxItemEven";
		_select.optionsListNode.appendChild(opt);
	};
	this.checkActiveSessions = function(/*obj*/activeSessionsList){
		var adminSessionIP = {};
		var adminSessionIPCount = 0;
		var otherConditionCount = 0;
		var isShowWarningDlg = false;
		for(var i = 0; i < activeSessionsList.length; i++){
			if(activeSessionsList[i].name == "admin" && activeSessionsList[i].type.toLowerCase() == "web"){
				if(adminSessionIP[activeSessionsList[i].from] == null || adminSessionIP[activeSessionsList[i].from] == undefined){
					adminSessionIP[activeSessionsList[i].from] = "1";
					adminSessionIPCount++;
				}
			}
			else
				otherConditionCount++;
		}
		if( adminSessionIPCount > 1 || otherConditionCount > 0)
			isShowWarningDlg = true;
		else
			isShowWarningDlg = false;
		return isShowWarningDlg;
	};
};

var Navigator = new function() {

	/* className should be 'success', 'warning', or 'error' */
	this.setMessage = function(/*String*/className, /*String*/message) {
		try {
			dojo.byId("statusBar").className = className;
			dojo.dom.replaceChildren(dojo.byId("statusBar"), document.createTextNode(message));
		} catch(e) { }
	};

	this.logout = function() {
		location.replace("/");
		/*
		dojo.io.bind({
			method: "POST",
			content: {perform: "logout"},
			url: "../cgi-bin/setuser.cgi",
			load: function() { location.replace("../loginwrap.html"); }
		});
		*/
	};

	this.setActiveStyleSheet = function(title) {
		var i, a, main;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
				if(a.getAttribute("title") == title)
					a.disabled = false;
				else
					a.disabled = true;
			}
		}
	};
};

