/*
SUBJ： x object

cPBar   ... ProgressBar | cPAjax  ... PageAjax
cBCast  ... BroadCast   | cFld    ... FormField  | cFldChk ... FormCheck

C/U-Date: 2007-09-28
*/

/*-- obj definition --*/
function cPBar() {
	this._container = '';
	this._msg = _get_PBarStatusText();
	this._obj = null;
	this._started = false;
	this._inited = false;
}

cPBar.prototype = {
	setext : function(s) {
		this._msg = s;
	},
	start : function() {
		this._initobj();
		if(this._inited) this._act(1);
	},
	stop : function() {
		if(this._started) this._act(2);
	},
	_act : function(a) {
		this._obj.innerHTML = (a==1)?this._msg:'';
		this._started = (a==1)?true:false;
	}
}
cPBar.prototype.setcontainer = setcontainer;
cPBar.prototype._initobj = _initobj;

function cPAjax(c) {
	this.code = '';
	this.subject = '';
	this.responseText = '';
	this.responseXML = '';
	this.responseType = 0; //0 txt; 1 xml
	this.started = false;
	this.pb = new cPBar();

	this._container = c;
	this._obj = null;
	this._inited = false;
}

cPAjax.prototype = {
	setcode : function(tv,ti,c) {
		this.code = _get_actAjaxFormHead(tv + this.subject) + c + _get_actAjaxFormCommand(ti) + _get_actAjaxFormBottom();
	},
	show : function() {
		this._initobj();
		if(this._inited) {
			this._obj.innerHTML = this.code;
			this.started = true;
		}
	},
	hide : function() {
		if(this.started) {
			this._obj.innerHTML = '';
			this.started = false;
		}
	},
	focus : function(o,s) {
		timeoutobj('focus_obj(\'' + o + '\')',s);
	},
	getresponse : function() {
		var ao = ajaxpack.ajaxobj,ft = ajaxpack.filetype;
		var rt = 0,rs = 1,r = '';
		if (ao.readyState==4) {
			if (ao.status==200||window.location.href.indexOf("http")==-1) {
				rt = (ft=='txt')?0:1;
				if(rt==0) {
					r = ao.responseText;
					if (r=='OK'||r.substr(0,2)=='OK') {
						r = r.substr(((r=='OK')?2:6),r.length);
						rs = 0;
					}
					else {
						alert('ERR:\n\n' + r);
					}
					this.responseText = r;
				}
				else
				this.responseXML = ao.responseXML;
				this.responseType = rt;
			}
		}
		return (rs);
	}
}
cPAjax.prototype.setcontainer = setcontainer;
cPAjax.prototype._initobj = _initobj;

function cBCast(i,s,t,c) {
	this.started = false;
	this.purecode = false;

	this._id = i;
	this._msg = s;
	this._livetime = t;
	this._container = c;
	this._obj = null;
	this._timer = null;
	this._cdtimer = null;
	this._inited = false;
}

cBCast.prototype = {
	setcontent : function(s,t) {
		this._msg = (!this.purecode)?_get_BCastText(s):s;
		if(t>0) this._livetime = t;
	},
	start : function() {
		this._initobj();
		if(this._inited&&this._livetime>0) {
			this._obj.innerHTML = this._msg;
			this._obj.style.display = '';
			this._timer = timeoutobj(this._id + '.stop()',this._livetime*1000);
			if(!this.purecode) this._docdown();
			this.started = true;
		}
	},
	stop : function() {
		clearTimeout(this._timer);
		if(this._cdtimer) clearTimeout(this._cdtimer);
		this._obj.style.display = 'none';
	},
	_docdown : function() {
		if(this._livetime==0) {
			clearTimeout(this._cdtimer);
			return;
		}
		getobj('_bc_cdown').innerHTML = (this._livetime --).toString();
		this._cdtimer = timeoutobj(this._id + '._docdown()',1000);
	}
}
cBCast.prototype.setcontainer = setcontainer;
cBCast.prototype._initobj = _initobj;

function cFld(i,d,v,t,z) {
	this.id = i;
	this.desc = d;
	this.type = t; //0 text,1 radio,2 option,3 checkbox
	this.isEmpty = !((/\S/).test(v));
	this.isZero = (typeof(z)!='undefined'&&z==1&&parseInt(v)<=0);
}

function cFldChk() {
	this.firstid = '';

	this._items = 0;
	this._item = new Array();
}

cFldChk.prototype = {
	add : function(fld) {
		this._item[this._items] = fld;
		this._items ++;
	},
	check : function() {
		var fld = null,nv = true,msg = '',id = '',tp = 0,i = 0,j = 0,k = 1;
		j = this._items;
		if(j==0) return true;
		for(i=0; i<j; i++) {
			fld = this._item[i];
			tp = fld.type;
			id = fld.id;
			nv = ((tp==0)?(fld.isEmpty||fld.isZero):((tp==1)?getRadioValue(id)==-999:((tp==2)?getOptionValue(id)==-999:getCheckboxValue(id)=='')));
			if(nv) {
				if(k==1) this.firstid = id;
				msg += k.toString() + '. ' + fld.desc + (fld.isZero?_get_NotZeroDesc():'') + '\n';
				k ++;
			}
		}
		this._initobj();

		nv = (k>1)?true:false;
		if(nv) {
			alert(_get_NotNullDesc(msg));
			fld = getobj(this.firstid);
			if(fld) fld.focus();
		}
		return (!nv);
	},
	_initobj : function() {
		this._item = new Array();
		this._items = 0;
	}
}

/*-- object prototype f --*/
function setcontainer(c) {
	this._container = c;
	this._inited = false;
}

function _initobj() {
	if(!this._inited) {
		this._obj = getobj(this._container);
		this._inited = (this._obj)?true:false;
	}
}

/*-- obj instance --*/
var p = new cPAjax('lbsp01');
var b = new cBCast('b','',10,'lbc');
var v = new cFldChk();

/*-- obj f --*/

function _chk_FormField(vf,t) {
	var _vr = null,_fc = vf.length;
	var _vi = 0,_vz = 0,_vs = 0,_vo = '',_vd = '',_vt = '',_vv = '',_tv = '';

	for(_vi=0; _vi<_fc; _vi++) {
		_vr = vf[_vi].split('.'); //tp.id.desc.ischk.isnotzero.isnum
		_vt = parseInt(_vr[0]);
		_vo = _vr[1];
		_vd = _vr[2];

		_vz = (_vr.length>=5?parseInt(_vr[4]):0);
		_vs = (_vr.length>=6?parseInt(_vr[5]):0);

		switch(_vt) {
			case 0:
				_vv = getobjvalueS(_vo);
				_vv = (_vs==0?_vv:_vv.replace(/\D/g,''));
				break;
			case 1:
				_vv = getRadioValue(_vo);
				break;
			case 2:
				_vv = getOptionValue(_vo);
				break;
			case 3:
				_vv = getCheckboxValue(_vo);
				break;
		}

		if(parseInt(_vr[3])==1) v.add(new cFld(_vo,_vd,_vv,_vt,_vz));
		if(t==1) _tv += '$|' + getchar(_vi+66) + '|$' + escape(_vv);
	}

	if(!v.check()) return (((t==1)?'':false));
	return (((t==1)?_tv:true));
}

function _get_Url() {
	return ((!gHasNC)?gfN:(gfN + '?t=' + _navcat.toString()));
}

function _get_ok() {
	return ('操作成功！');
}

function _get_fail() {
	return ('操作失败，出错信息：\n\n');
}

function _get_cfont(c,s) {
	return ('<font color="' + c + '">' + s + '</font>');
}

function _get_ConfirmText(o,s) {
	return (((o=='')?s:'操作对象：' + o + '\n\n\n' + s));
}

function _get_NotNullDesc(s) {
	return ('以下内容不能为空：\n\n' + s + '\n请填写完整，谢谢！');
}

function _get_NotZeroDesc() {
	return ('，该项数值须大于0');
}

function _get_PBarStatusText() {
	return ('<br /><img src="images/loading.gif" /> 操作进行中，请稍候...');
}

function _get_BCastText(s) {
	var c = '<div class="infob infob_T">&nbsp;提示信息 <span id="_bc_cdown"></span></div><div class="infob infob_C">' + s + '</div>';
	return (c);
}

function _get_actLockDesc() {
	return (_get_actDesc('锁定'));
}

function _get_actLockResultT(s) {
	return (_get_actResultT(s,'锁定'));
}

function _get_actLockResultS() {
	return (_get_cfont('red','LOCKED'));
}

function _get_actUnlockDesc() {
	return ('是否要【解除锁定】对该' + p.subject + '的锁定？');
}

function _get_actDelDesc() {
	return (_get_actDesc('删除'));
}

function _get_actDelResultT(s) {
	return (_get_actResultT(s,'删除'));
}

function _get_actDelResultS() {
	return (_get_cfont('red','DELETED'));
}

function _get_actActResultS(s) {
	return (_get_cfont('red',s));
}

function _get_actResultT(s,t) {
	var c = _get_cfont('black','<strike><b>' + s + '</b></strike>') + '&nbsp;(' + _get_cfont('red','已' + t) + ')';
	return (c);
}

function _get_AjaxCommonLead() {
	return ('ajax=1&a=' + gCID + '&t=' + _navcatS + '&gp=' + _navcpS);
}

function _get_actNewEditResultBC(a,v) {
	var c = '操作 ' + ((a==5)?'新建':'编辑') + p.subject + _get_actResultBC(v);
	return (c);
}

function _get_actResultBC(s) {
	var c = '【<span id="#mf_bold" class="cred">' + s + '</span>】完成！<br /><br /><br />刷新后即可看到最新的数据～<br /><br />［<a href="javascript:location.reload(1)">点击这里刷新当前页面</a>］';
	return (c);
}

function _get_actAjaxFormCommand(a) {
	var c = '<tr class="hs"><td class="bd br br bt" colspan="2">　　　<input type="button" value=" 确定 " onclick="javascript:act(' + a.toString() + ')" id="se_o_btnok" class="buttnc" accesskey="S" />&nbsp;<input type="button" value=" 取消 " class="buttnc" accesskey="R" onclick="javascript:act(21)" /><span id="lbi"></span></td></tr>';
	return (c);
}

function _get_actAjaxFormHead(t) {
	var c = '<table width="600px" border="0" cellpadding="5" cellspacing="0" class="tcinfo ajaxtb" style="background-color:white"><tr><td><table width="100%" border="0" cellpadding="0" cellspacing="0" class="list"><tr><td colspan="2" class="titles">&nbsp;' + t +'</td></tr>';
	return (c);
}

function _get_actAjaxFormBottom() {
	return ('</table></td></tr></table>');
}

function _get_actDesc(t) {
	return ('是否要【' + t + '】该' + p.subject + '？');
}

/*-- normal f --*/

function se_form(t) {
	var obj = null,aa = '$|A|$',i = 0,cc = '',kk = '',ss = '',vv = '';
	var oID = new Array('c','t','o');

	for(i=66; i<69; i++) {
		if(i==66&&(!gHasNC)) {
			i = 67;
			ss = 'a0$$';
		}
		cc = getchar(i);
		obj = getobj('s' + oID[i-66] + 'p');
		vv = obj.options[obj.options.selectedIndex].value;
		aa += ((i==68)?vv:escape(vv)) + '$|' + cc + '|$';
		ss += obj.options[obj.options.selectedIndex].id.toString() + '$$';
	}

	kk = getobj('skp').value.toString();
	aa += kk;
	ss += escape(kk);

	if (trim(kk)=='')
		this.location.href = _get_Url();
	else
		se_sf(3,aa,ss);
}

function se_sf(a,cs,seo) {
	var f = document.form1;

	f.a.value = a;
	f.aid.value = cs;
	f.seobj.value = seo;
	f.submit();
}

function se_keyp_return(t,e) {
	if(!e) e = window.event;
	var k = getkeycode(e);
	if(k==13) {
		if(window.event) {
			e.cancelBubble = false;
			e.returnValue = false;
		}
		else {
			e.stopPropagation();
			e.preventDefault();
		}
		se_form(t);
		return true;
	}
}
