/*
 * 功能：新增修改操作，POST提交数据
 * 编写日期: 2014-03-05
 * 作者:     David.Dai@zkteco.com 
 */
var app_label = $("#app_label").val()
$("#id_edit_form").find("input[type!=hidden]:first").focus();
var data_model_url = $("#data_model_url").val()
var model_name = $("#model_name").val()
//确定按钮
$("#OK").click(function(){
	 ok_this = this;
     var pk =  $("#id_model_pk").val();
     $form= $("#id_edit_form");
     if(typeof(before_submit)!= "undefined"){
		if(!before_submit()){
			return;
	    };
     }
     if ($form.valid()){
	     var data=$form.serialize();
	     var url = app_label+data_model_url+"/_new_/"
	     if (pk!='None'){
	     	url = app_label+data_model_url+"/"+pk+"/"
	     }
	     $.ajax({
	        url:url,
	        data:data,
	        type:"POST",
	        dataType:'html',
	        success:function(callback){
	            if(callback.indexOf('{ Info:"OK" }') != -1){
	            	window.location.assign(app_label+"/page"+app_label+model_name+"Report/?pure");
	            } else{
	                var $error=$(callback).find("ul.errorlist");
	                if($error.length!=0){
	                    $("#id_info").html($error.eq(0)).hide().show(100); 
	                }else{
	                    $("#id_info").html(callback);
	                } 
	            }
	        },
	        error:function(callback){
	                alert("服务器出错!");
	            }
	       });
     }else{
        $form.find("input.error").eq(0).focus();
     }
});
$("#Cancel").click(function(){
	window.location.assign(app_label+"/page"+app_label+model_name+"Report/?pure");
});
//日期控件渲染 by dym 2014-03-03
$("body input.date").each(function(){
	var $this = $(this);
	var opts = {};
	if ($this.attr("format")) opts.pattern = $this.attr("format");
	if ($this.attr("yearstart")) opts.yearstart = $this.attr("yearstart");
	if ($this.attr("yearend")) opts.yearend = $this.attr("yearend");
	$this.datepicker(opts);
});
