/*
 * contains logic to process intermediate process steps like displaying layers and error messages mostly in an AJAX-like way
 */
 
/*
* jQuery needs a specific format for the ID selector so prefix the ID with a hash
*/
function getIDSelector(divID){
	return  '#' + divID;
}

/*
 * return the inner text of a div element
 */
function getTrimmedDivText(divID){
	
	return jQuery.trim($(getIDSelector(divID)).text());
}
 
/*
  *	displayLayer displays the given div in a modal window in the center of the main page
  *	the main page is darkened
 */
function displayLayer(layerDivID){
	
	var layerDivIDSelector  = getIDSelector(layerDivID);
	
	$(layerDivIDSelector).modal({onOpen: function (dialog) {
		dialog.overlay.fadeIn('slow', function () {
			dialog.data.hide();
			dialog.container.fadeIn('slow', function () {
				dialog.data.slideDown('slow');
			});
		});
	}});
}
/*
 * close the modal window
 */
function closeLayer(){
	
	$.modal.close();
}

/*
 * returns the script name provided by the AJAX request
 * invoked finally after AJAX response processing
 */
function getTargetScript(){
	return getTrimmedDivText('targetScript');
}

/*
 * invokes the provided target URL/script
 * 
 * used as jump target if the intermediate step was successful
 */
function invokeTargetScript(){
	eval(getTrimmedDivText('targetScript'));
}

/*
 * returns the statusCode as result of the Ajax request processing
 * used to determine if a layer needs to be displayed (because statusCode indicates an error)
 * or if the targetURL/targetScript has to be invoked (if statusCode indicates success)
 */
function getAjaxResultStatusCode(){
	return getTrimmedDivText('statusCode');
}

/*
 * status code is set in the called AJAX pipeline
 * status code = 0 means success
 * status code = -1 means error
 * 
 * the AJAX pipeline can define new return codes and handle it appropriately by itself
 */
function isAjaxResultSuccessful(){
	if (getTrimmedDivText('statusCode') == '0'){
		return true;
	}
	return false;
}


/*
 * submits the form of a given formID
 */
function submitForm(formID){
	var formIDSelector = getIDSelector(formID);
	$(formIDSelector).submit();
}

/*
 * submits the form of a given formID and a given URL
 */
function submitFormByURL(url, formID){
	var formIDSelector = getIDSelector(formID);
	$(formIDSelector).attr("action", url);
	$(formIDSelector).submit();
}

/*
 * calls the URL as it would be a link
 */
function triggerByURL(url){
	window.location.href = url;

}

/*
  *	display the error message provided by the intermediate-Layer in the errorSourceDiv in the
  *	errorTargetDiv of the main page
 */
function displayError(errorTargetDivID, errorSourceDivID){
	
	var errorTargetDivSelector  = getIDSelector(errorTargetDivID);
	var errorSourceDivSelector  = getIDSelector(errorSourceDivID);
	
	$(errorTargetDivSelector).html($(errorSourceDivSelector).text());
}


/*
  *	mark a given id with the provided CSS class
  *
  * used to mark input fields as error
 */
function markError(divID, classToAdd ){
	
	var layerDivIDSelector  = getIDSelector(divID);
	$(layerDivIDSelector).addClass(classToAdd);


}


/*
 * fires an AJAX request with the following parameter and loads it in a given div
 *    - intermediateURL: mandatory, the target ajax pipeline URL (which can be rendered in a layer)
 *    - targetDivID: mandatory, the id of the div were the response will be rendered in --> MUST exists in source page
 *    - formID: optional, if given a form with all parameters is serialized and send with the request
 *    - targetScript: optional, if given the targetScript mostly a JS-function call is passed and invoked afterwards
 * 
 */
function triggerAjaxRequest(intermediateURL, targetDivID, formID, targetScript){

	$(document).ready(function(){
		
		var targetDivIDSelector  = getIDSelector(targetDivID);
		var formData = null;
		
		if (null != formID){
			var formIDSelector  = getIDSelector(formID);
			var formData = $(formIDSelector).serialize() ;
		}	
		if (null != targetScript){
	
			var params = { TargetScript:targetScript };
			var additionalParameter = jQuery.param(params);
			
			if (null != formData){
				formData = formData + '&' + additionalParameter;
			}
			else {
				formData = additionalParameter;
			}
		}

		$(targetDivIDSelector).load(intermediateURL, formData, function(response, status, xhr) {});
	});
}

/*
 * fires an AJAX request with the following parameter and loads it in a given div
 *    - intermediateURL: mandatory, the target ajax pipeline URL (which can be rendered in a layer)
 *    - targetDivID: mandatory, the id of the div were the response will be rendered in --> MUST exists in source page
 *    - divID: optional, if given all parameters inside this div will be serialized and send with the request
 *    - targetScript: optional, if given the targetScript mostly a JS-function call is passed and invoked afterwards
 * 
 */
function triggerAjaxRequest2(intermediateURL, targetDivID, divID, targetScript){

	$(document).ready(function(){
		
		var targetDivIDSelector  = getIDSelector(targetDivID);
		var divData = null;
		
		if (null != divID){
			var divIDSelector  = getIDSelector(divID);
			var divData = $(divIDSelector + " *").serialize() ;
		}	
		if (null != targetScript){
	
			var params = { TargetScript:targetScript };
			var additionalParameter = jQuery.param(params);
			
			if (null != divData){
				divData = divData + '&' + additionalParameter;
			}
			else {
				divData = additionalParameter;
			}
		}

		$(targetDivIDSelector).load(intermediateURL, divData, function(response, status, xhr) {});
	});
}

