var CommonCodeQuery = Class.create();

CommonCodeQuery.prototype = {
	initialize : function(containerId, iName, iValue, dValue) {
		// ?e?????l??
		this.iName = iName;
		this.dName = "disp_"+iName;
		this.container = $(containerId);
		this.iHtml = "<input id='"+iName+"' name='"+iName+"' value='"+iValue+"' class='sym3_text2'/>";
		this.dHtml = 
			"<span class='sym3_text7' style='width:100%' id='"+iName+"_name'>"
			+ ((dValue!=null)?dValue:"&nbsp;")
			+ "</span>";
		this.bHtml = 
			"<input id='cmd"+iName+"Query' class='button_gray' value='...' "
			+ "type='button' style='padding-left:2px;padding-right:2px;'>";
		this.messageCode = "S007";

		this.show();
		return this;
	},
	setInputAttrs : function(attrs) {
		// ?]?w???J?????S?O????
		if (this.inputField==null) return;
		for (n in attrs) {
			try {
				this.inputField[n] = attrs[n];
			}
			catch(E) { }
		}
		return this;
	},
	show : function() {
		// ?????@???N?X?????J???H?????s
		this.container.innerHTML = 
			"<table border='0' cellpadding='0' cellspacing='0' width='210'>" 
			+ "<tbody><tr>"
			+ "<td>"+this.iHtml+"</td>"
			+ "<td width='30'>"+this.bHtml+"</td>"
			+ "<td width='5'>&nbsp;</td>"
			+ "<td width='95' nowrap>"+this.dHtml+"</td>"
			+ "</tr></tbody>"
			+ "</table>";
		this.inputField = this.container.firstChild.rows[0].cells[0].firstChild;
		this.button = this.container.firstChild.rows[0].cells[1].firstChild;
		this.displayField = this.container.firstChild.rows[0].cells[3].firstChild;
		
		// input field onchange .. if value blank, clear display
		this.inputField.queryObj = this;
		//this.assignInputFieldChangeEvent();
		this.inputField.onchange = this.inputFieldChangedEvent.bindAsEventListener(this);
		this.inputField.onkeypress = function() { return handleEnter(this, event); }
		
		// Remember assign parameter;
		this.commomCodeInfo = arguments;
		// Assign Click Event;
		//this.button.queryObj = this;
		this.button.onclick = this.btnClick.bindAsEventListener(this);
		return this;
	},
	inputFieldChangedEvent : function(evt) {
		// input field onchange .. 
		this.displayField.innerHTML = "&nbsp;";
	},
	assignMessageCode : function(code) {
		// assing [...] message code when no data return
		this.messageCode = code;
		return this;
	},
	assignQuery : function(args) {
		// assign [...] Query mvc url
		this.args = args;
		return this;
	},
	changeQueryArgus : function(name, value) {
		try {
			this.args[name] = value;
		}
		catch(E) {}
	},
	assignReturnProc : function(proc) {
		// assign [...] click, after retrun value from common query, call back function
		this.returnProc = proc;
		return this;
	},
	btnClick : function() {
		if (this.args==null) {
			alert("pls set query info first!!!");
			return false;
		}
		commonCodePopupUrl = 
			((this.args.mvcName==null)?"Common.CommonCodeList.mvc":this.args.mvcName);
		commonCodePopupUrl += (commonCodePopupUrl.indexOf("?")<0)?"?":"&";
		commonCodePopupUrl = commonCodePopupUrl
			+ "c_" + this.args.P1.replace(/[.]/g, "_")+ "=" + this.inputField.value 
			+ "&queryClass=" + this.args.P0
			+ "&popupProperty=" + escape(this.args.props)
			+ "&popupTitle=" + escape(this.args.titles);
		
		var returnValue = DialogCall(commonCodePopupUrl);
		if (returnValue==null) {
			//???e?p?G?^?????O null, ???B?z
			this.inputField.value = "";
			this.displayField.innerHTML = "&nbsp;";
			return false;
		}
		else if (!returnValue) {
			// ???? messageCode ?]?w?????~?T??
			CommonMessage.showNotification(this.messageCode);
			// ?n???]?w???e?????? Focus?A?~?i?H?]?w???e???????????? focus
			window.focus();
			
			this.inputField.value = "";
			this.displayField.innerHTML = "&nbsp;";
			Field.activate(this.inputField);
			return false;
		}
		else {
			try {
				this.inputField.value = eval("returnValue."+this.args.P1);
			}
			catch(E) {}
			// 20060512 Ricky Chiang Add clear "undefined" value
			if (this.inputField.value=="undefined") {
				this.inputField.value = "";
			}
			try {
				this.displayField.innerHTML = eval("returnValue."+this.args.P2) + "&nbsp;";
			}
			catch(E) {}
			// 20060512 Ricky Chiang Add clear "undefined" value
			if (this.displayField.innerText=="undefined") {
				this.displayField.innerHTML = "&nbsp;";
			}
		}
		
		if (this.returnProc && this.inputField.value != "") {
			this.returnProc.call(
				this, 
				new CommonCodeQuery.ReturnObj(this, returnValue)
			);
		}
	}
};

CommonCodeQuery.ReturnObj = Class.create();

CommonCodeQuery.ReturnObj.prototype = {
	initialize : function(queryObj, dialogResult) {
		this.queryObj = queryObj;
		this.dialogResult = dialogResult;
	},
	getId : function() {
		try {
			return eval("this.dialogResult."+this.queryObj.args.P1);
		}
		catch(E) { return null; }
	},
	getName : function() {
		try {
			return eval("this.dialogResult."+this.queryObj.args.P2);
		}
		catch(E) { return null; }
	},
	getRelationData : function(aMvcUrl) {
		if (aMvcUrl.trim() == "") {
			alert("getRelationData need MVC URL!!! ");
			return null;
		}
		else if (aMvcUrl.indexOf(".ajax")<0) {
			alert("getRelationData MVC URL need end with '.ajax' !!!");
			return null;
		}
		// Query Data use aMvcUrl !!!
		var relationResult = new Ajax.Request(
			aMvcUrl, {asynchronous: false, method: 'post'}
		);
		// Parse Return Data
		var xmlObj = relationResult.transport.responseXML;
		if (xmlObj.selectSingleNode("//html")!=null) {
			var win = window.open("about:blank");
			win.document.write(relationResult.transport.responseText);
		}
		else if (xmlObj.selectSingleNode("//exception")!=null) {
			alert(xmlObj.selectSingleNode("//exception").text);
			return null;
		}
		else {
			eval("var ajaxResult = "+xmlObj.selectSingleNode("/ajax-response/result").text+";");
			return ajaxResult;
		}
	}
};
