/* $Id: listtable.js 14980 2008-10-22 05:01:19Z testyang $ */
if (typeof Utils != 'object')
{
  alert('Utils object doesn\'t exists.');
}

var listTable = new Object;
var _database = new Object;
var _databasetime;
listTable.query = "query";
listTable.filter = new Object;
listTable.url = location.href;
listTable.url += location.href.lastIndexOf("?") == -1 ? "?re=ajax" : "&re=ajax";
/**
 * 创建一个可编辑区
 */
listTable.edit = function(obj, act, id)
{
  var tag = obj.firstChild.tagName;

  if (typeof(tag) != "undefined" && tag.toLowerCase() == "input")
  {
    return;
  }

  /* 保存原始的内容 */
  var org = obj.innerHTML;
  var val = Browser.isIE ? obj.innerText : obj.textContent;

  /* 创建一个输入框 */
  var txt = document.createElement("INPUT");
  txt.value = (val == 'N/A') ? '' : val;
  txt.style.width = (obj.offsetWidth + 12) + "px" ;

  /* 隐藏对象中的内容，并将输入框加入到对象中 */
  obj.innerHTML = "";
  obj.appendChild(txt);
  txt.focus();

  /* 编辑区输入事件处理函数 */
  txt.onkeypress = function(e){
    var evt = Utils.fixEvent(e);
    var obj = Utils.srcElement(e);

    if (evt.keyCode == 13){
      obj.blur();
      return false;
    }
    if (evt.keyCode == 27){
      obj.parentNode.innerHTML = org;
    }
  }

  /* 编辑区失去焦点的处理函数 */
  txt.onblur = function(e){
    if (Utils.trim(txt.value).length > 0){
	  var res = ajax(listTable.url+'&do='+act+'&name='+encodeURIComponent(txt.value)+'&id='+id);
	  if(res.error==0){
		obj.innerHTML =  res.name;
	  }else{
		alert(res.error);
        obj.innerHTML = org;
	  }
    }else{
      obj.innerHTML = org;
    }
  }
  
}


listTable.ajaxform = function(title,obj){
  showhandle({
    html:'<div class="Message">确定要结算奖金吗</div>',
    width:320,
    height:138,
    id:'ajaxform',
    title:title
  },function(){
	$("#controlLoad").show();
	removetip("ajaxform");
	var queryString = $('#'+obj).formSerialize();
    $.ajax({url:location.href,
	  type:'POST',
      dataType:"json",
	  data:queryString,
      success:function(res){
		$("#controlLoad").hide();
	    if(res.error=='0'){
		  $(".ok").hide();
		  if(res.message==null){
			if(res._message){
			  $(".Message").html(res._message);
			  $("#controlLoad").show().html("执行中，不要刷新或关闭该窗口");
			  resizebox("ajaxform",320,$(".lotab").height()+140);
			  _database = res;
			  _databasetime = window.setInterval("ajaxdatabase(_database)",1000);
			}  
		  }else{
			hidebox('ajaxform',true);
			Right(res.message,{},function(){
			  if(res.url){
				location.href = res._url=='1' ? eval(res.url) : res.url;	
			  }else{
				Close();  
			  }
			});  
		  }
	    }else{
		  addtip("ajaxform",res.error);
	    }
      }
	});
  });
}

function ajaxdatabase(data){
  $.ajax({url:data.url,type:'GET',success:function(res){																
    _database = res;
    $(".Message").html(res._message);
	resizebox("ajaxform",320,$(".lotab").height()+140);
	if(res.url==null){
	  clearInterval(_databasetime);
	  hidebox('ajaxform',true);
	  Right(res.message,{},function(){
		location.href = res.gourl;						 
	  });
	}
  },dataType:"json"});
}
listTable.change = function(obj, act, id, typename)
{
  var tag = obj.firstChild.tagName;
  var url = '/index.php?mod=tools&act=getType&typename='+typename;
  
  if (typeof(tag) != "undefined" && tag.toLowerCase() == "select")
  {
    return;
  }

  /* 保存原始的内容 */
  var org = obj.innerHTML;
  var val = Browser.isIE ? obj.innerText : obj.textContent;

  /* 创建一个输入框 */
  var txt = document.createElement("select");
  txt.id = 'typeid';
  txt.style.width = (obj.offsetWidth + 32) + "px" ;
  obj.innerHTML = "";
  obj.appendChild(txt);  
  $.getJSON(url,function(data){
    $.each(data, function(i, item) {
	 if(item['typename']==org){
	  $("#typeid").append("<option value='"+item['id']+"' selected>"+item['typename']+"</option>");
	 }else{
      $("#typeid").append("<option value='"+item['id']+"'>"+item['typename']+"</option>"); 
	 }
	});
  });
  txt.focus();
  
  /* 编辑区输入事件处理函数 */
  txt.onkeypress = function(e){
    var evt = Utils.fixEvent(e);
    var obj = Utils.srcElement(e);

    if (evt.keyCode == 13){
      obj.blur();
      return false;
    }
    if (evt.keyCode == 27){
      obj.parentNode.innerHTML = org;
    }
  }

  /* 编辑区失去焦点的处理函数 */
  txt.onblur = function(e){
    if (Utils.trim($('#typeid option:selected').text()).length > 0){
	  var res = ajax(listTable.url+'&do='+act+'&typeid='+$('#typeid').val()+'&id='+id);
	  if(res.error==0){
		obj.innerHTML = $('#typeid option:selected').text();
	  }else{
		alert(res.error);
        obj.innerHTML = org;
	  }
    }else{
      obj.innerHTML = org;
    }
  }
  
}

/**
 * 切换状态
 */
listTable.toggle = function(obj, act, id)
{
  var val = (obj.src.match(/yes.gif/i)) ? 0 : 1;
  var res = ajax(listTable.url+'&do='+act+'&val='+val+'&id='+id);
  if(res.error == 0){
    obj.src = val ? obj.src.replace('no.gif',"")+'yes.gif' : obj.src.replace('yes.gif',"")+'no.gif';
  }else{
    alert(res.error); 
  }
}

/**
 * 删除列表中的一个记录
 */
listTable.remove = function(id, cfm, opt)
{
  if (opt == null){
    opt = "remove";
  }
  $.prompt(cfm,{buttons:{'确认':true,'取消':false},callback:function(e,v,m,f){
   if(v){
     var res = ajax(listTable.url+'&do='+opt+'&id='+id);
     if(res.error == 0){
       //$("#remove_"+id).hide('slow', function(){ $(this).remove(); });
	   $("#remove_"+id).remove();
     }else{
       alert(res.error); 
     }  
   }
  }});
}

listTable.removeCart = function(id,cfm){
  var opt = "remove";
  $.prompt(cfm,{buttons:{'确认':true,'取消':false},callback:function(e,v,m,f){
   if(v){
     var res = ajax(listTable.url+'&do='+opt+'&id='+id);
     if(res.error == 0){
       set_sum_price();
	   $("#remove_"+id).remove();
	   $('#goods_cart table tbody').each(function(i){
		 if($(this).html()==''){
		   	$(this).parent().remove(); 
		 }					   
	   });
     }else{
       alert(res.error); 
     }  
   }
  }});
}

listTable.selectAll = function(obj, chk)
{
  if (chk == null)
  {
    chk = 'checkboxes';
  }

  var elems = obj.form.getElementsByTagName("input");

  for (var i=0; i < elems.length; i++)
  {
    if (elems[i].name == chk || elems[i].name == chk + "[]")
    {
      elems[i].checked = obj.checked;
    }
  }
}

listTable.deltake = function(id,cfm,opt){
  if (opt == null) opt = "remove";
  if (cfm == null) cfm = "";
  showAsk({Msg:cfm},function(){
     $.getJSON(get_path_url("?mod=goods&act=shopcart&re=ajax&do=removetake&id="+id),function(res) {
       if(res.error == 0){
         $("#remove_"+id).remove();
		 if($("#take_id").val()==id) $("#take_id").val('');
         Close();
       }else{
         Wrong(res.error); 
       }											
     });																													 
  },function(){
     Close();  
  });
}