
emmapp.ajax = {

	JSON_STATUS_VALUE_SUCCESS: "1",
	JSON_STATUS_VALUE_FAILED: "-1",
	JSON_STATUS_VALUE_REQUIRE_AUTH: "-2",
	JSON_STATUS_VALUE_SESSION_EXPIRED: "-3",

	/*
	 * ajax handleAs values:  Acceptable values are 
	 * "text", 
	 * "json" - default here, 
	 * "json-comment-optional", 
	 * "json-comment-filtered", 
	 * "javascript", 
	 * "xml"
	 */
	doAjaxGet: function(url, callback, handleAs) {	
	
		// ignore for robot session
		if ( emmapp.isRobotSession ) {
			console.debug("robot session request ignored for doAjaxGet: " + url);
			return;
		}
		
		if ( handleAs == null || typeof handleAs == 'undefined' ) {
			handleAs = "json";
		}
		
		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: handleAs,
			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, handleAs, formId) {	
		
		// ignore for robot session
		if ( emmapp.isRobotSession ) {
			console.debug("robot session request ignored for doAjaxPost: " + url);
			return;
		}
		
		if ( handleAs == null || handleAs =="" || typeof handleAs == 'undefined' ) {
			handleAs = "json";
		}
		
		if ( callback == null || callback == "" || typeof callback == 'undefined' ) {
			callback = function(response, ioArgs) {
				console.log("xhrPost OK", response, ioArgs);
						
				return response;
			}
		}

		if ( formId == null || formId =="" || typeof formId == 'undefined' ) {
			formId = emmapp.page.formId;
		}
		
		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: handleAs,
			/*postData: formData,*/
	       preventCache: true,               
	        form: 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(/*String*/masterElement, /*String*/elementName, /*String*/ defaulSelect, /*function*/ callFuncion) {
		
		console.log("doAjaxLoadElementHtml: masterElement = " + masterElement + ", slaveElement = " + elementName);
		
		var callback = function(response, ioArgs) {
			//console.log("xhrGet OK", response, ioArgs);	
//				alert(response.content);
			var e = emmapp.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;				
				}
				*/
				var innerStr = response.content;
				if ( e.id == emmapp.advsearch.state_selectid_edit && typeof(innerStr) !="undefined" && innerStr.length > 0){
					innerStr = "<option >Select One</option>"+innerStr;
				}
				emmapp.selectionlist.setSelectOptionHtml(e, innerStr);
				console.log("doAjaxLoadElementHtml: " + innerStr);
				console.log(e.id + "=" + dojo.byId(e.id).value);
				if ( typeof defaulSelect != 'undefined'){
					//IE BUG ,NEEN TO RESET option obj
					emmapp.selectionlist.setOptionsByValue(dojo.byId(e.id), defaulSelect);
				}

				if ( typeof callFuncion == 'function'){
					callFuncion.apply();
					//
				}
				
				emmapp.validator.setInvalid(e.id, (response.content == ""));
				
			}
			
			
			return response;
		}
		
		var url = emmapp.url.addParameters(
				emmapp.page.ajaxElementBaseUrl,
				"masterElement", masterElement,
				"element", elementName); 
		

		this.doAjaxPost(url, callback);
	}
		
};

