
emmapp.ajax = {

	JSON_STATUS_VALUE_SUCCESS: "1",
	JSON_STATUS_VALUE_FAILED: "-1",
	JSON_STATUS_VALUE_REQUIRE_AUTH: "-2",
	JSON_STATUS_VALUE_SESSION_EXPIRED: "-3",

	doAjaxGet: function(url, callback) {	
		if ( callback == null || typeof callback == 'undefined' ) {
			callback = function(response, ioArgs) {
				console.log("xhrGet OK", response, ioArgs);				
				return response;
			}
		}
		
		emmapp.showWaitCursor(); 
		
		dojo.xhrGet({
			url: url,
			handleAs: "json",
			load: function(response, ioArgs) {emmapp.showDefaultCursor(); callback(response, ioArgs);},

			error: function(response, ioArgs) {
				console.log("xhrGet failed", response, ioArgs);
				return response;
			}
		});
		
		
	},


	doAjaxPost: function(url, callback) {	
		if ( callback == null || typeof callback == 'undefined' ) {
			callback = function(response, ioArgs) {
				console.log("xhrPost OK", response, ioArgs);
						
				return response;
			}
		}

		emmapp.showWaitCursor(); 

		/*
		 * formToJson failed if formdat contains &
		 * so changed to submit the form directly
		var formData = dojo.formToJson(emmapp.page.formId);
		*/
		dojo.rawXhrPost({
			url: url,
			handleAs: "json",
			/*postData: formData,*/
	       preventCache: true,               
	        form: emmapp.page.formId,
			load: function(response, ioArgs) {emmapp.showDefaultCursor(); callback(response, ioArgs);},

			error: function(response, ioArgs) {
				console.log("xhrPost failed", response, ioArgs);

				return response;
			}
		});
		
	},


//	__getIdKey: function(id) {
//		var i = id.lastIndexOf("$");
//		if ( i > 0 ) {
//			return id.substring(0, i+1);
//		}
//		
//		return "";
//	},

	// some browser does not trigger a reload,
	// change the url a little bit --- simple workaround
	changeCaptchaImg_count: 0,
	changeCaptchaImg: function(id) {
//			console.log("changeCaptchaImg for " + id);
		var e = document.getElementById(id);
		if ( e != null ) {
			
//				console.log("calling server for new image ... changeCaptchaImg for " + id);
			e.src = emmapp.page.captchaImgUrl + "?count=" + this.changeCaptchaImg_count++;
		}
		
//			console.log("done changeCaptchaImg for " + id);

	},

	/*check the status of the jason document, return true if the call can continue
	return false if the caller should stop futher action, typically this means 
	this method will take over the action, such as reset the url
	 */
	checkJsonState: function(jsonDoc) {

		if ( jsonDoc.status == this.JSON_STATUS_VALUE_REQUIRE_AUTH ) {
			location.href = emmapp.page.loginRequiredUrl;
			return false;
		} else if ( jsonDoc.status == this.JSON_STATUS_VALUE_SESSION_EXPIRED ) {
			window.location.reload(true);
			return false;

		} else if ( jsonDoc.status == this.JSON_STATUS_VALUE_FAILED ) {
			if (jsonDoc.errMsg) {
				alert(jsonDoc.errMsg);
			}
			return false;
		}		
		
		return true;
	},

	
	doAjaxLoadElementHtml: function(masterElement, elementName) {
		
		console.log("doAjaxLoadElementHtml: masterElement = " + masterElement + ", slaveElement = " + elementName);
		
		var callback = function(response, ioArgs) {
			//console.log("xhrGet OK", response, ioArgs);	
//				alert(response.content);
			var e = emmapp.page.byId(elementName);
			if ( e != null ) {	
				
				/*
				if (dojo.isIE) {
					//alert("I am the stupid IE");
					// the first option got lost in IE, this is a dirty workaround
		 			var html = "<option value=\"\">---</option>" + response.content;
		 			e.innerHTML = html;
					var re = new RegExp("(\)(.*?)(\)");
					//if(e.outerHTML) e.outerHTML = e.outerHTML.replace(re, "$1" + html + "$4");
					if(e.outerHTML) e.outerHTML = e.outerHTML.replace(re, "$1");
					
				} else {
					e.innerHTML = response.content;				
				}
				*/
				
				emmapp.selectionlist.setSelectOptionHtml(e, response.content);
				console.log("doAjaxLoadElementHtml: " + response.content);
				console.log(e.id + "=" + e.value);
				
				emmapp.page.setInvalid(e.id, (response.content == ""));
				
				/*
				var e_container = e.parentNode.parentNode;
				if ( response.content == "" ) {
					// hide the container
					e_container.style.visibility = 'hidden';
					e_container.style.display = 'none';
					
					emmapp.page.setInvalid(e.id, true);
				} else {
					e_container.style.visibility = 'visible';
					e_container.style.display = 'block';
					emmapp.page.setInvalid(e.id, false);
				}
				*/
				
			}
			
			
			return response;
		}
		
		var url = emmapp.url.addParameters(
				emmapp.page.ajaxElementBaseUrl,
				"masterElement", masterElement,
				"element", elementName); 
		
//			alert(url);
		this.doAjaxPost(url, callback);
	}
		
		
};
