
emmapp.querystore = {
		STORE_BUZLIST: "store_buzlist",
		STORE_MEMLIST: "store_memlist",
		STORE_DEPARTMENT: "store_department",
		STORE_TITLE: "store_title",
		 
	inputChanged: function(/*FilteringSelect*/w, /*String*/store, /*id*/assocElement, /*boolean*/normalize, /*boolean*/maketitle) {
		
		var hasAssoc = assocElement != null &&  assocElement != "" && typeof assocElement != 'undefined';
		
		var value = w.getValue();
		var text = w.getDisplayedValue();
		
		// new user input, notify server
		var text2 = text;
		
		console.log("assocElement=" + assocElement); 
		// special handling for now, need to fix later TODO
		var ignorevalidation = (store == emmapp.querystore.STORE_BUZLIST || hasAssoc && assocElement.indexOf("mem_orgid") >= 0);
		
		console.log("emmapp.querystore.inputChanged: store=" + store);
		
		// normalize the user input
		if ( (typeof normalize == 'undefined' || normalize == true) && !ignorevalidation ) {
			text2 = emmapp.string.normalize(text2, ' ');
			console.log("normalized: " + text2);
		}
		
		// make title
		if ( (typeof maketitle == 'undefined' || maketitle == true) && !ignorevalidation ) {
			text2 = emmapp.string.cnvrt2title(text2);
			console.log("titled: " + text2);
		}
		
		if ( text2 != text ) {
			w.setDisplayedValue(text2);
		}
		
		// if the assoc element is given, set the value
		if ( hasAssoc ) {
			var e = dojo.byId(assocElement);
			if ( e != null ) {
				e.value = text2;
				console.log("assoc element set: " + e.id + ","  + text2);
			}
		}
		
		var needCheck = ( value != text );  // for one column store, the value and text are the same
		
//		// if it's ready only store, don't change the display text if value is there
//		// this is not right since user can select a value and then change the text --- the hidden value is still there
//		if ( store == this.STORE_BUZLIST || store == this.STORE_MEMLIST ) {
//			needCheck = (value == "" );
//		}
		
		if ( needCheck ) {
		
			// if the store is a readonly store, ignore
			if ( store == null || store == "" || typeof store == 'undefined' || store == this.STORE_BUZLIST || store == this.STORE_MEMLIST ) {
				return true;
			}

			var url = emmapp.url.addParameters(emmapp.page.ajaxStoreUpdateBaseUrl, 
					"store", store, 
					"text", emmapp.url.encode(text2));
			
			console.log("emmapp.querystore.inputChanged: executing " + url);
			
//			alert(url);
			
			emmapp.ajax.doAjaxGet(url, function(response, ioArgs) {
				console.log("store updated: " + store + ", " + text2, response, ioArgs);				
				return response;
			});
			
		}
		
		return true;
	}
};
