function invokeDestinationPage(link,container) {

	if( link.indexOf("?") != -1 ){
		link = link + "&isAsync=true";
	}
	else{
		link = link + "?isAsync=true";
	}

	jQuery.ajax( {
		async: true,
		cache: false,
		type: "GET",
		url: link,
		success: function(data) {
			jQuery("#"+container).replaceWith(data);
		},
        complete: function() {
            jQuery("#"+container).parent().scrollTop(0);
        }        
	} );
}

function openFormDialogWithURL(dialogId, url) {

	if( url.indexOf("?") != -1 ){
		url = url + "&isAsync=true";
	}
	else{
		url = url + "?isAsync=true";
	}

    var dialog = jQuery("#"+dialogId);
	dialog.empty();
	dialog.dialog("open");
	dialog.load(url, 	
		function(responseText, textStatus, XMLHttpRequest){
			if(handleResponse(responseText) ){
				applyDialogLayout(dialogId);
			}
		});	
}

function applyDialogLayout(dialogId){
	var minHeight = $("#"+dialogId).height()-(6+20+12);
	$("#"+dialogId+" .dialogContentWithButtons").attr('style','min-height:'+minHeight+'px; height:auto !important; height:'+minHeight+'px;');
	$("#"+dialogId+" .dialogButtons").css({'text-align':'right','margin':'12px 0px 6px 0px'});
}

function openDialogWithURLAndTitle(dialogId, title, url) {
	$('#'+dialogId).dialog("option","title",title);
	openFormDialogWithURL(dialogId,url);
}

function openFormDialog(dialogId, csrfToken) {
	var httpArgs=csrfToken;
	for (var i=2; i<arguments.length; i+=2) {
		httpArgs+= "&" + arguments[i]+"="+arguments[i+1];
	}

	openFormDialogWithURL(dialogId,dialogId+".htm?"+httpArgs);
}

function openFormDialogWithQuery(dialogId, query) {
	openFormDialogWithURL(dialogId,dialogId+".htm?"+query);
}

function ajaxFormSubmit(formId,options) {
	
	var defaultBeforeSend=function() {
		jQuery("#"+formId+" input").attr("disabled","disabled");
		jQuery("#"+formId+" textarea").attr("disabled","disabled");
		jQuery("#"+formId+" select").attr("disabled","disabled");
		jQuery("#"+formId+" button").attr("disabled","disabled");
	};
	
	var defaultAfterSuccess=function() {
		jQuery("#"+formId+" input").removeAttr("disabled");
		jQuery("#"+formId+" textarea").removeAttr("disabled");
		jQuery("#"+formId+" select").removeAttr("disabled");
		jQuery("#"+formId+" button").removeAttr("disabled");
	}
	
	extendForm(formId);
	
	var defaults={
				async: true, 
	            cache: false,
	            contentType: "application/x-www-form-urlencoded; charset=UTF-8", 
	            type: "POST",
           		data: jQuery("#"+formId).serialize(),
  				url: jQuery("#"+formId).attr("action"),
	            isFragment: false, 
	            myAfterSuccess: function() {}, 
	            myBeforeSend: function() {},
	            replaceBeforeSend: false,
	            replaceAfterSuccess: false,
	            beforeSend: function() {
	            	if(!this.replaceBeforeSend) {
	            		defaultBeforeSend();
	            	}
	            	this.myBeforeSend();
	            },
	            success: function(data) {
	            	handleResponse(data);	            
	              	this.myAfterSuccess(data);
	              	if(!this.replaceAfterSuccess) {
	              		defaultAfterSuccess();
	              	}
	            },
	            error: function() {
	            	redirectAsyncError();
	            }
	         };

	var settings={};
	
	jQuery.extend(settings, defaults, options);
	
	jQuery.ajax( settings );
}

function asyncSubmitForm(formId,isFragment,action) {
    if (action!=null) {
        jQuery("#"+formId).attr("action", action);
    }
	if (isFragment==true) {
		var f=function(data) {
			jQuery("#"+formId+"-container").html(data);
		};
	} else {
		var f=function(data) {
			jQuery("#"+formId+"-container").replaceWith(data);
		};
	}
	
	extendForm(formId);
	
	jQuery.ajax( {
		async: true,
		cache: false,
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		beforeSend: function() { 
			jQuery("#"+formId+"-container input").attr("disabled","disabled");
			jQuery("#"+formId+"-container textarea").attr("disabled","disabled");
			jQuery("#"+formId+"-container select").attr("disabled","disabled");
			jQuery("#"+formId+"-container button").attr("disabled","disabled");
		},
        complete: function() {
            jQuery("#"+formId+"-container").parent().scrollTop(0);    
        },
		data: jQuery("#"+formId).serialize(),
		type: "POST",
		url: jQuery("#"+formId).attr("action"),
		success: function(data) {
	           	handleResponse(data);	            
	           	f(data);
	    },
	    error: function() {
	    	redirectAsyncError();
	    }
	} );

}

function extendForm(formId){
	jQuery("<input type='hidden' name='isAsync' value='true'>").prependTo("#"+formId);
}

function disableForm(formId, disableButtons) {
	jQuery("#"+formId+" input").attr("disabled","disabled");
	jQuery("#"+formId+" textarea").attr("disabled","disabled");
	jQuery("#"+formId+" select").attr("disabled","disabled");
	if (disableButtons) {
		jQuery("#"+formId+" button").attr("disabled","disabled");
	}
}

function refreshContentTable(pageName,sectionName,token) {
	jQuery("#"+sectionName+"Content").load(pageName+"_"+sectionName+".htm?"+token,
		function(responseText, textStatus, XMLHttpRequest){
			handleResponse(responseText);
		});
}

function handleResponse(responseText) {
	if( responseText.indexOf("eq-render:exclusive") != -1 ){
		// response has error comment, it's a error page 
		// => display it in window
		//jQuery("html").replaceWith(responseText); // does not work while js is executing
		//jQuery("body").replaceWith(responseText); // does not work cuz of layout issues
		redirectAsyncError();
		return false;
	}
	return true;
}

function redirectAsyncError() {
	window.location.href = "sessionExpired.jsp?eq-message-key=ps.message.info.SessionExpired";
}
