/**
 * @namespace com.thesis.control.MEAccountControl
 * @author 钟军锐 August.R@263.net
 */

/** @id MEAccountControl */
function MEAccountControl(sFrame){
	if(typeof sFrame == "string")
		this.Frame = $(sFrame);
	else
		this.Frame = sFrame;
	this.value = -1;
	
	this.Form = {}
	this.Form.sURL = "/Service/MEAccountService.asmx/getMEAccounts";
	
	this.BO = new BaseBO(MEAccountObject,this.Form);
	
	if(typeof MEAccountControl._initialized == "undefined"){
		MEAccountControl._initialized = true;
		
		MEAccountControl.prototype.clear = function(){
			while(this.Frame.firstChild)
				this.Frame.removeChild(this.Frame.firstChild);
		};
		
		MEAccountControl.prototype.refresh = function(){
			this.clear();
			this.BO.execute();
		};
		
		/** @id getAccountID */
		MEAccountControl.prototype.getAccountID = function(){
			for(var i=0;i<this.Frame.childNodes.length;i++){
				if(this.Frame.childNodes[i].checked){
					this.value = this.Frame.childNodes[i].value;
					return this.value;
					break;
				}
			}
		};
		
		/** @id setAccountID */
		MEAccountControl.prototype.setAccountID = function(sID){
			if (sID) {
				for (var i = 0; i < this.Frame.childNodes.length; i++) {
					if (this.Frame.childNodes[i].value == sID) {
						this.Frame.childNodes[i].checked = true;
						break;
					}
				}
			}
			else {
				this.Frame.childNodes[0].checked = true;
			}
		};
		
		/** @id registerAccount */
		MEAccountControl.prototype.registerAccount = function(rdo,span){
			uEvent.addBrowserHandler(span,"click",function(){
				rdo.checked = true;
			});
		};
		
		/** @id addAccount */
		MEAccountControl.prototype.addAccount = function(meac){
			var str = "Input";
			var rdo = null;
			if(Detect.isIE){
				str = "<Input type='radio' name='MEAccount'/>";
				rdo = C$(str);
			}
			else{
				rdo = C$(str);
				rdo.type = "Radio";
				rdo.name ="MEAccount";
			}
			rdo.value = meac.AccountID;
			var span = C$("Span");
			span.innerHTML = meac.Bank + " " + meac.AccountID + ";<br>";
			this.Frame.appendChild(rdo);
			this.Frame.appendChild(span);
			this.registerAccount(rdo,span);
		};
		
		/** @id handleData */
		MEAccountControl.prototype.handleData = function(lst){
			this.clear();
			for(var i in lst){
				this.addAccount(lst[i]);
			}
		};
		
		/** @id initialize */
		MEAccountControl.prototype.initialize = function(Root){
			uEvent.Listener.add(this.BO,"Executed",function(lst){
				Root.handleData(lst);
			});
			this.refresh();
		};
	}
	
	this.initialize(this);
}
