


/**
 * This function will be called for each ajax enabled dynamic list feature.
 */
function j21p_doAjaxCall(url, successFunction, errorFunction, itemId) {
  // Implement AJAX calls via jQuery
  jQuery.ajax({
    url: url,
    success: function(data) {
        var codePos = data.indexOf("\n");
        if (codePos > -1) {
          var returnCode = data.substring(0, codePos);
          var htmlCode = data.substring(codePos + 1);
          var wasOk = returnCode.indexOf("ok") > -1;
          if (!wasOk) {
            errorFunction(returnCode, htmlCode, itemId);
          } else {
            successFunction(returnCode, htmlCode, itemId);
          }
        } else {
          errorFunction(returnCode, htmlCode, itemId);
        }
      },
      type: "POST",
      dataType: "text"
  });
/*
  // Implement AJAX calls via dojo
	dojo.io.bind({
    	url: url,
    	handler: function(type, data, evt) {
    		var codePos = data.indexOf("\n");
    		if (codePos > -1) {
    			var returnCode = data.substring(0, codePos);
    			var htmlCode = data.substring(codePos + 1);
				if (returnCode != "ok") {
					errorFunction(returnCode, htmlCode, itemId);
				} else {
					successFunction(returnCode, htmlCode, itemId);
				}
			} else {
				errorFunction(returnCode, htmlCode, itemId);
			}
    	},
    	mimetype: "text/html"
	});
*/
}

/**
 * This function will be called for each ajax enabled dynamic list "select all items" feature.
 */
function j21p_doAjaxCallWithForm(url, successFunction, errorFunction, itemId, fNode) {
  // Implement AJAX call via jQuery (use the jquery.forms plugin)
  jQuery(fNode).ajaxSubmit({
    url: url,
    success: function(data, statusText) {
      var codePos = data.indexOf("\n");
      if (codePos > -1) {
        var returnCode = data.substring(0, codePos);
        var htmlCode = data.substring(codePos + 1);
        if (returnCode != "ok") {
          errorFunction(returnCode, htmlCode, itemId);
        } else {
          successFunction(returnCode, htmlCode, itemId);
        }
      }
    }
  });
  

  // Implement AJAX calls via dojo
//	dojo.io.bind({
//    	url: url,
//   	handler: function(type, data, evt) {
//    		var codePos = data.indexOf("\n");
//    		if (codePos > -1) {
//    			var returnCode = data.substring(0, codePos);
//    			var htmlCode = data.substring(codePos + 1);
//				if (returnCode != "ok") {
//					errorFunction(returnCode, htmlCode, itemId);
//				} else {
//					successFunction(returnCode, htmlCode, itemId);
//				}
//			} else {
//				errorFunction(returnCode, htmlCode, itemId);
//			}
//    	},
//			/*
//			 * Use the method specified by the form
//			 */
//			method: fNode.method,
//			/*
//			 * The form node whose data will be sent
//			 */
//			formNode: fNode, 
//    	mimetype: "text/html"
//	});
}

/*
 * Asks the server to submit a snippet, identified by 'snippetId'
 * and if the calls succeeds, insert the received HTML fragment into a div with
 * the 'divId'.
 * If the snippet depends on a list, the 'itemId' will be transmitted too.
 */
function j21p_getSnippet(snippetId, divId, itemId) {
 return;
/*
	dojo.io.bind({
    	url: "/snippet/getSnippet.htm/" + snippetId + (itemId == "" ? "" : "?itemId=" + itemId),
    	load: function(type, data, evt){ 
    		// Insert into DIV
    		var div = document.getElementById(divId);
    		div.innerHTML = data;
    	 },
    	error: function(type, error){  },
    	mimetype: "text/html"
	});
*/
}

/*
 * Call an AJAX function using the given url, the function
 * that will be executed whenever the call succeeds, the function
 * for error events and the form node whose data will be submitted.
 */
function j21p_callAjaxWithForm(url, loadFunction, errorFunction, fNode, expResultFormat) {
  // Use jQuery to send the form
  jQuery(fNode).ajaxSubmit({
    url: url,
    dataType: "json",
    success: function(data, statusText) {
      loadFunction(data);
    }
  });

//	try {
//		// Call dojo.io.bind function to asynchronously send
//		// a form's data to the given url
//		dojo.io.bind({
//			url: url,
//			
//			encoding: "utf-8",
//			
//			/*
//			 * The function to be executed whenever an answer was received
//			 */
//			load: function(type, data, evt) {
//				/*
//				 * Call the given load function with 'data' as single parameter
//				 */
//				loadFunction(data);
//			},
//			/*
//			 * If a network-error or other errors occur, that results in not
//			 * receiving the response, call 'errorFunction'
//			 */
//			error: errorFunction,
//			/*
//			 * Define the expected result format
//			 */
//			mimetype: expResultFormat,
//			/*
//			 * Use the method specified by the form
//			 */
//			method: fNode.method,
//			/*
//			 * The form node whose data will be sent
//			 */
//			formNode: fNode
//		});
//	} catch (e) {
//		/*
//		 * Print out the error as a message box
//		 */
//		alert(e);
//	}
}

/*
 * Call an AJAX function using the given url, the function
 * that will be executed whenever the call succeeds, the function
 * for error events and the form node whose data will be submitted.
 * 
 * The response is expected to be in HTML format.
 */
function j21p_callAjaxWithFormHTMLResult(url, successFunction, errorFunction, fNode) {
	/*
	 * Make the AJAX call with result type 'text/html'.
	 * This is used to insert some HTML fragments which was received from the server.
	 */
	j21p_callAjaxWithForm(url, successFunction, errorFunction, fNode, "text/html");
}

/*
 * Call an AJAX function using the given url, the function
 * that will be executed whenever the call succeeds, the function
 * for error events and the form node whose data will be submitted.
 * 
 * The response is expected to be in JSON format.
 */
function j21p_callAjaxWithFormJsonResult(url, successFunction, errorFunction, fNode) {
	/*
	 * Make the AJAX call with result type 'text/json'.
	 * This is used to process somehow formatted objects.
	 */
	j21p_callAjaxWithForm(url, successFunction, errorFunction, fNode, "text/json");
}

function j21p_setMessages(htmlResult) {
	var parentDiv = document.getElementById("j21p_messageWrap");
	parentDiv.style.display = "block";
	parentDiv.innerHTML = htmlResult;
}


