');
params['REQUEST_FROM_FACEBOX']=1;
$('.GeneralFormPopUp').load(params['action'],params['query_params'],function(){
var dlg=$(this);
form=dlg.find('form');
var showResponse = function(responseText, statusText, xhr) {
var result;
var errorfound=0;
/*try to find the input fields that have errors and mark them in the dialog*/
$(responseText).find('.errfield').each(function(){
$(this).find('input').each(function(){
var input=form.find("[name='"+$(this).attr('name')+"']");
var div_container=input.closest('div');
div_container.addClass('errfield');
div_container.append('Fältet måste fyllas i');
});
});
/*try to find an error message on the result page and print it in the dialog*/
$(responseText).find('.error').each(function(){
form.append('
'+$(this).html()+'
');
errorfound=1;
});
if(errorfound == 0){
/*close dialog and load the page that is in the forms action*/
dlg.dialog('destroy').remove();
location.href=params['reload_url'];
}
}
/*add ajax function to the form*/
var options = {
success: showResponse
}
form.submit(function(e){
if(e.target.onsubmit){
if(e.target.onsubmit()){
$(this).ajaxSubmit(options);
}
return false;
}
$(this).ajaxSubmit(options);
return false;
});
/*find all buttons in the form and add them as buttons in the dialog*/
$($(this).find('.button').get().reverse()).each(function(){
var action=$(this).attr('name');
$(this).hide();
if(action !='CANCEL'){
buttons[$(this).val()]=function() {
form.append('');
form.submit();
};
}
});
/*add a default cancel button*/
buttons['Avbryt']=function() {
$(this).dialog('destroy').remove();
};
$('.GeneralFormPopUp').dialog({
resizable: false,
modal: true,
width: parseInt(params['width']),
buttons: buttons,
close: function(){
$(this).dialog('destroy').remove();
}
});
});
}
function form_validate_general(obj,callback,params){
var result=true;
if(params===undefined){
params={};
}
var form;
if(obj.is( "form" )){
form = obj;
}else{
form = obj.closest('form');
}
form.find('input').each(function(){
if($(this).attr('data-required')){
$(this).click();
if($(this).val() == $(this).attr('data-default') || $(this).val()==''){
$(this).closest('div').addClass('errfield');
$(this).val($(this).attr('data-default'));
result=false;
}else{
$(this).closest('div').removeClass('errfield');
}
}
});
form.find('textarea').each(function(){
if($(this).attr('data-required')){
$(this).click();
if($(this).val() == $(this).attr('data-default') || $(this).val()==''){
$(this).closest('div').addClass('errfield');
$(this).val($(this).attr('data-default'));
result=false;
}else{
$(this).closest('div').removeClass('errfield');
}
}
});
if(result){
if(callback!==undefined){
callback(params['image'],params['form']);
}
var error=form.find('.error');
if(error.length>0){
error.remove();
}
}else{
var error=form.find('.error');
if(error.length<1){
form.append('');
error=form.find('.error');
}
error.html('Obligatoriskt fält saknar värde');
if(params['image']!==undefined){
$('#'+params['image']).hide();
}
}
return result;
}