(function(f){f(window).on("load",function(u){u=f("form#noTransition");(0<u.length?u:f("form:first")).formMan()});f.fn.sort=function(){return this.pushStack(f.makeArray([].sort.apply(this,arguments)))};f.fn.formMan=function(u){var a={sFMsgCls:"errmsg",sMsgSaved:"\u767b\u9332\u3057\u307e\u3057\u305f\u3002",sKeepCls:"unreplaceable",onRowPlace:null};f.extend(a,f.hybs.defaults,u||{});return this.each(function(){var C=this,D=function(b,c,e){var k=b.find("."+a.sKeepCls).attr("src");var h=b.find("."+a.sKeepCls).attr("class");
b=f(c).replaceAll(b);if(a.onRowPlace)a.onRowPlace(sHybsCmd,b);else k&&b.find("."+a.sKeepCls).attr("src",k),h&&b.find("."+a.sKeepCls).removeClass().addClass(h);e&&!b.hasClass("row_hilight")&&b.removeClass("row_0 row_1").addClass("row_hilight");return b},J=function(b,c,e,k){var h=b.find("input[name="+a.sHybsSel+"]:checked"),x=0<h.length;c=f.hybs.getHybsTr(c);var y=c.is("tr"),g=f.hybs.getNoColId(b),v=[],q;switch(e){case a.sCmdInsert:case a.sCmdInsertOne:case a.sCmdCopy:case a.sCmdModify:case a.sCmdDelete:case a.sCmdUndo:case a.sCmdEntry:var z=
y?x?h.parents("tr"):c:x?h.parents("tbody"):c}z.sort(function(d,l){return d.getAttribute(a.sHybsRid,2)-l.getAttribute(a.sHybsRid,2)});var G=!1,n,E,w;z.each(function(d){if(!(e==a.sCmdInsertOne&&d<z.length-1))if(d=f(this),e==a.sCmdUndo&&"A"==d.attr(a.sHybsKh))q||=d.prev(),v.push(parseInt(d.attr(a.sHybsRid),10)),d.remove();else if(G||(y?(n=/(<tbody[^>]*>[\S\s]*<\/tbody>)/m.exec(k),E=/(<tr[^>]*?>[\S\s]*?<\/tr>)/m):(n=/(<tbody[^>]*>[\S\s]*<\/tbody>)/m.exec(k),E=/(<tbody[^>]*?>[\S\s]*?<\/tbody>)/m),n&&(w=
n[1]),G=!0),w&&(n=E.exec(w))){var l=n[1].replace(/<a href="#top"[^>]*><\/a>/m,""),m=d.find("input[name="+a.sHybsSel+"]:first").val();m||=0;e==a.sCmdCopy&&x&&m++;l=l.replace(new RegExp('(<input type="checkbox" name="'+a.sHybsSel+'"[^>]*>)',"m"),'$1<input type="hidden" name="'+a.sSelRow+'" value="'+m+'" />');switch(e){case a.sCmdInsert:case a.sCmdInsertOne:case a.sCmdCopy:l=x?f(l).insertAfter(d):f(l).insertBefore(d);l.removeClass("row_0 row_1").addClass("row_hilight");0<=g&&l.find("td:eq("+g+")").empty();
d.attr(a.sHybsKh)||d.find("input[name="+a.sHybsSel+"]:first").removeAttr("checked");bindEvents();break;case a.sCmdModify:case a.sCmdDelete:d.attr(a.sHybsKh)||(m=0<=g?d.find("td:nth("+g+")").text():"",d=D(d,l,!0),0<=g&&!m&&d.find("td:nth("+g+")").empty());bindEvents(d);break;case a.sCmdEntry:var F=d.attr(a.sHybsKh)||"";m=0<=g?d.find("td:nth("+g+")").text():"";var I=d.find("input[name="+a.sHybsSel+"]:first").attr("checked");d=D(d,l,!0);if(I)switch(F){case "D":q||=d.prev();v.push(parseInt(d.attr(a.sHybsRid),
10));d.remove();break;case "A":case "C":d.find("td:eq(0)").empty(),d.removeAttr(a.sHybsKh),d.find("input[name="+a.sHybsSel+"]:first").removeAttr("checked"),0<=g&&!m&&d.find("td:nth("+g+")").empty()}f("."+a.sFMsgCls).html(a.sMsgSaved);bindEvents(d);break;case a.sCmdUndo:F=d.attr(a.sHybsKh)||"";m=0<=g?d.find("td:nth("+g+")").text():"";d=D(d,l,!0);switch(F){case "C":case "D":d.find("td:eq(0)").empty(),d.removeAttr(a.sHybsKh),d.find("input[name="+a.sHybsSel+"]:first").removeAttr("checked"),0<=g&&!m&&
d.find("td:nth("+g+")").empty()}bindEvents(d)}w=w.substr(n.index+n[0].length)}});if(q){q[0]&&(y?q.parent().is("tbody"):q.children(":first").is("tr"))?h=q.next():(c=b.find("tbody tr:first"),h=y?c:c.parent());v.sort();for(c=v.length;h[0];){for(var r=0,t=parseInt(h.attr(a.sHybsRid),10),A=c-1;0<=A;A--)if(t>v[A]){r=A+1;break}0<r&&(t-=r,h.attr(a.sHybsRid,t).find("input[name="+a.sHybsSel+"]:first").attr({id:"cb"+t,value:t}),0<=g&&(r=h.find("td:nth("+g+")"),r.text()&&r.text(t+1)));h=h.next()}f("tbody:first:not(:has(tr:first-child))",
b).remove()}0<z.length&&(b.resizeView(),B())},K=function(b,c,e){if(c==a.sCmdInsert||c==a.sCmdInsertOne||c==a.sCmdCopy)if(c=/(?:<tbody>)?\r?\n?(<tbody>[\S\s]*<\/tbody>)\r?\n?(?:<\/tbody>)?/m.exec(e))if(c=c[1].replace(/<a href="#top"[^>]*><\/a>/m,"").trim())b.append(c),b.resizeView(),B(),bindEvents(b)},L=function(b,c,e){if(c==a.sCmdInsert||c==a.sCmdInsertOne||c==a.sCmdCopy)if(c=/(<div id="divHlr)|(\[\d+ - \d+\]\/\[\d+\])|(<div id="divPos)|(<table id="viewTable)/m.exec(e))if(c=/([\S\s]*<table[^>]*?>[\S\s]*<\/table>[\S\s]*)/m.exec(e.substr(c.index)))e=
f("input:hidden[name=pageSize]",b),e.prev("div[onclick]:empty").remove(),e.nextAll().andSelf().remove(),b.append(c[1]),b=b.find("table#viewTable"),b.resizeView(!0),B(),bindEvents(b)},B=function(b){var c=f(C),e=c.find("input:submit."+a.sEntryCls);b?e.attr("disabled","disabled"):(b=f(c.data(f.hybs.defaults.sHybsCmd).obj),b.hasClass(a.sEditCls)&&e.removeAttr("disabled"),b.hasClass(a.sEntryCls)&&e.attr("disabled","disabled"))},H={};H[a.sNoTsKey]="true";var p={};p.data=H;p.beforeSubmit=function(b,c,e){return a.beforeSubmit?
a.beforeSubmit(b,c,e):!0};p.success=function(b,c,e){c=f("form").data(f.hybs.defaults.sHybsCmd).cmd;if(0<=b.indexOf("row_error"))e=/(?:<\/table>)*[\S\s]*(\[\d+ - \d+\]\/\[\d+\])[\S\s]*(<table[^>]*?>[\S\s]+?row_error[\S\s]*<\/table>)/m.exec(b),f.hybs.ogAlert(e?e[1]+e[2]:b);else if(0<=f.inArray(c,[a.sCmdInsert,a.sCmdInsertOne,a.sCmdCopy,a.sCmdModify,a.sCmdDelete,a.sCmdEntry,a.sCmdUndo])){var k=!1;c==a.sCmdEntry&&(e=/<div id="noTransitionUrl">(.*)<\/div><div id="noTransitionTarget">(.*)<\/div>/m.exec(b))&&
(k=e[1],e=e[2],("null"!=e?eval("top."+e):window).location.href=k,k=!0);k||(e=f("form").find("table#viewTable"),k=e.find("tbody tr:first"),k[0]?J(e,k,c,b):e[0]?K(e,c,b):L(f("form"),c,b))}};p.error=function(b,c,e){f.hybs.ogLog(c+"\n"+b.responseText);f.hybs.ogAlert(c+"<br/>"+b.responseText)};p.complete=function(b,c){f(C).removeData(f.hybs.defaults.sHybsCmd)};p.cmds=p.cmds||[a.sCmdInsert,a.sCmdInsertOne,a.sCmdCopy,a.sCmdModify,a.sCmdDelete,a.sCmdEntry,a.sCmdUndo];B(!0);f(C).ajaxHybsForm(p)})}})(jQuery);
