function js_saveForm(_form){

    $("form[name="+_form.name+"] input[type=text]").css("border", "2px solid #ccc");
    $("form[name="+_form.name+"] select").css("border", "2px solid #ccc");
    $("form[name="+_form.name+"] textarea").css("border", "2px solid #ccc");
    $("form[name="+_form.name+"] input[type=radio]").parent().parent().css("border", "0");
    $("form[name="+_form.name+"] input[type=checkbox]").parent().parent().css("border", "0");

    x = $("form[name="+_form.name+"]").serializeArray();
    x[x.length+1] = {"name":"_formName", "value": _form.name};
    
    $.post("ajax/formsave.php", x, function(res){
        if(res.errorCode) infoBox(res.errorMsg);
        else if(_form.name == "przepisy_skladniki2") czyscForm();
        infoBox(res.msg);
        refreshDG(res.name);
    },"json");
}

function refreshDG(n){
    id = gup("id");
    if(!id) id = IDREFRESH;
    
    $("#dg_"+n).load("ajax/dg_"+n+".php?id="+id);
}

function refreshDG2(n, id){
    $("#dg_"+n).load("ajax/dg_"+n+".php?id="+id);
}

function infoBox(msg){
    $.growl("Informacja", msg);
}

function gup( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function ajaxEdit(fName, id){
    $("<div id='ajaxForm_"+fName+"' class='popupDiv'></div>")
        .hide()
        .appendTo($("body"))
        .dialog({ position: 'center', width:"450px"})
        .load("ajax/showform.php?form="+fName+"&id="+id);
}

function ajaxEditSave(_form){
    x = $("form[name="+_form.name+"][class=inPopup]").serializeArray();
    x[x.length+1] = {"name":"_formName", "value": _form.name};
    $.post("ajax/formedit.php", x, function(res){
        if(res.errorCode) infoBox(res.errorMsg);
        infoBox(res.msg);
        refreshDG(res.name);
        if(_form.name=="przepisy_skladniki2"){
            czyscForm();
            $(".popupDiv form").remove();
        }
        $(".popupDiv").dialog('destroy');
    }, "json");
}

function ajaxDelete(fName, id){
    $.post("ajax/formdelete.php", {"fName": fName, "id":id}, function(res){
        if(res.errorCode) infoBox(res.errorMsg);
        infoBox(res.msg);
        refreshDG(fName);
    }, "json");
}
