﻿; (function($) {
    /**
    * jqGrid extension for manipulating Grid Data
    * Tony Tomov tony@trirand.com
    * http://trirand.com/blog/ 
    * Dual licensed under the MIT and GPL licenses:
    * http://www.opensource.org/licenses/mit-license.php
    * http://www.gnu.org/licenses/gpl.html
    **/
    $.fn.extend({

        editRowCustom: function(rowid, keys, oneditfunc, succesfunc, url, extraparam, aftersavefunc, errorfunc) {
            return this.each(function() {
                var $t = this, nm, tmp, editable, cnt = 0, focus = null, svr = [];
                if (!$t.grid) { return; }
                var sz, ml, hc;
                if (!$t.p.multiselect) {
                    editable = $("#" + rowid, $t.grid.bDiv).attr("editable") || "0";
                    if (editable == "0") {
                        $('#' + rowid + ' td', $t.grid.bDiv).each(function(i) {
                            nm = $t.p.colModel[i].name;
                            hc = $t.p.colModel[i].hidden === true ? true : false;
                            try {
                                tmp = $.unformat(this, { colModel: $t.p.colModel[i] }, i);
                            } catch (_) {
                                tmp = $.htmlDecode($(this).html());
                            }
                            svr[nm] = tmp;
                            if (nm !== 'cb' && nm !== 'subgrid' && $t.p.colModel[i].editable === true && !hc) {
                                if (focus === null) { focus = i; }
                                $(this).html("");
                                var opt = $.extend($t.p.colModel[i].editoptions || {}, { id: rowid + "_" + nm, name: nm });
                                if (!$t.p.colModel[i].edittype) { $t.p.colModel[i].edittype = "text"; }
                                var elc = createEl($t.p.colModel[i].edittype, opt, tmp, $(this));
                                $(elc).addClass("editable");
                                $(this).append(elc);
                                //Agin IE
                                if ($t.p.colModel[i].edittype == "select" && $t.p.colModel[i].editoptions.multiple === true && $.browser.msie) {
                                    $(elc).width($(elc).width());
                                }
                                cnt++;
                            }
                        });
                        if (cnt > 0) {
                            svr['id'] = rowid; $t.p.savedRow.push(svr);
                            $('#' + rowid, $t.grid.bDiv).attr("editable", "1");
                            $('#' + rowid + " td:eq(" + focus + ") input", $t.grid.bDiv).focus();
                            if (keys === true) {
                                $('#' + rowid, $t.grid.bDiv).bind("keydown", function(e) {
                                    if (e.keyCode === 27) { $($t).restoreRow(rowid); }
                                    if (e.keyCode === 13) {
                                        $($t).saveRowCustom(rowid, succesfunc, url, extraparam, aftersavefunc, errorfunc);
                                    }
                                    e.stopPropagation();
                                });
                            }
                            if (typeof oneditfunc === "function") { oneditfunc(rowid); }
                        }
                    }
                }
            });
        },






        saveRowCustom: function(rowid, succesfunc, url, extraparam, aftersavefunc, errorfunc) {
            return this.each(function() {
                var $t = this, nm, tmp = {}, tmp2 = {}, editable, fr;
                if (!$t.grid) { return; }
                editable = $('#' + rowid, $t.grid.bDiv).attr("editable");
                if (editable === "1" && $t.p.savefunction) {
                    $('#' + rowid + " td", $t.grid.bDiv).each(function(i) {
                        nm = $t.p.colModel[i].name;
                        if (nm !== 'cb' && nm !== 'subgrid' && $t.p.colModel[i].editable === true) {
                            if ($t.p.colModel[i].hidden === true) { tmp[nm] = $.htmlDecode($(this).html()); }
                            else {
                                switch ($t.p.colModel[i].edittype) {
                                    case "checkbox":
                                        var cbv = $t.p.colModel[i].editoptions.value.split(":") || ["Yes", "No"];
                                        tmp[nm] = $("input", this).attr("checked") ? cbv[0] : cbv[1];
                                        break;
                                    case 'text':
                                    case 'password':
                                    case 'textarea':
                                        tmp[nm] = $("input, textarea", this).val();
                                        break;
                                    case 'select':
                                        if (!$t.p.colModel[i].editoptions.multiple) {
                                            tmp[nm] = $("select>option:selected", this).val();
                                            tmp2[nm] = $("select>option:selected", this).text();
                                        } else {
                                            var sel = $("select", this);
                                            tmp[nm] = $(sel).val();
                                            var selectedText = [];
                                            $("select > option:selected", this).each(
										function(i, selected) {
										    selectedText[i] = $(selected).text();
										}
									);
                                            tmp2[nm] = selectedText.join(",");
                                        }
                                        break;
                                }
                            }
                        }
                    });
                    if (tmp) { tmp["id"] = rowid; if (extraparam) { $.extend(tmp, extraparam); } }
                    if (!$t.grid.hDiv.loading) {
                        $t.grid.hDiv.loading = true;
                        $("div.loading", $t.grid.hDiv).fadeIn("fast");
                        $t.p.savefunction(tmp,

                        function(res) {

                            var ret;
                            if (typeof succesfunc === "function") { ret = succesfunc(res); }
                            else ret = true;
                            if (ret === true) {
                                tmp = $.extend(tmp, tmp2);
                                $($t).setRowData(rowid, tmp);
                                $('#' + rowid, $t.grid.bDiv).attr("editable", "0");
                                for (var k = 0; k < $t.p.savedRow.length; k++) {
                                    if ($t.p.savedRow[k].id === rowid) { fr = k; break; }
                                };
                                if (fr >= 0) { $t.p.savedRow.splice(fr, 1); }
                                if (typeof aftersavefunc === "function") { aftersavefunc(rowid, res.responseText); }
                            } else { $($t).restoreRow(rowid); }

                        },
                           function(err) {
                               if (typeof errorfunc == "function") {
                                   errorfunc(err)
                               } else {
                                   alert("Error: "+err.get_message());
                               }
                           }
                        );

                        $t.grid.hDiv.loading = false;
                        $("div.loading", $t.grid.hDiv).fadeOut("fast");
                        $("#" + rowid, $t.grid.bDiv).unbind("keydown");
                    }
                }
            });
        }






    });
})(jQuery);

var grdimgpath = 'themes/steel/images';

function GridDataReceived(data) {
    var thegrid = $("#" + data.controlid);
    thegrid[0].addJSONData(data, thegrid.bDiv, 9)
}

function WebServiceError(e) {
    alert("Error: "+ e.get_message());
}

function EditRowOnSelect(id) {
    
    if ((!this.lastsel) || (id && id != this.lastsel)) {
        if (this.lastsel) {
            jQuery('#' + this.id).restoreRow(this.lastsel);
        }
        jQuery('#'+this.id).editRowCustom(id, true);
    }
    this.lastsel = id;

}

