/*
 *  (c) 2005 Zhijie Chen <zigzag.chen@gmail.com>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Form 标签帮助类
 * @class
 * @scope public
 */
Swato.Form = Class.create();
Swato.Form.prototype = {
	
	/**
	 * 构建函数
	 * @scope public
	 * @param form： form 表单元素对象
	 */
	initialize : function(form) { 
		this.form = form;
	},

	/*
	 * 获取 form 表单 的 json 对象
	 * @scope public
	 */
	toJSON : function() { 
		var formObject=new Object();
//		var elements = Form.getElements($(this.form));  为了支持fireFox用下面那句
		var elements = document.getElementById(this.form).elements;
		for (var i=0;i < elements.length;i++){
			var isSerializable = Form.Element.serialize(elements[i]);
			if (isSerializable){
				formObject[elements[i].name]=$F(elements[i]);
			}	
		}
		return formObject;
	},
	
	/*
	 * 根据 json 对象设置 form 表单
	 * @scope public
	 * @param result: form 表单中所有 input 标签的“name” 对应的 json 对象
	 */
	gotResult : function(result /*:json 对象*/) { 
		if(result == undefined || result == null ) return;
		
		var elements = Form.getElements($(this.form));
		for (var i=0;i < elements.length;i++){
			if (result[elements[i].name]!=null) {
				FormUtil.setValue(elements[i],result[elements[i].name]);
			}
		}
	}
};

/*
 * 根据 json 对象设置 form 表单
 * @class
 * @scope public
 */
var FormUtil = { // Based on code from http://getahead.ltd.uk/dwr
	isArray : function(data) {
		return (data && data.join) ? true : false;
	},
	isHTMLElement : function(ele, nodeName) {
		if (nodeName == null) {
			return ele != null &&
				typeof ele == "object" &&
				ele.nodeName != null;
		} else {
			return ele != null &&
				typeof ele == "object" &&
				ele.nodeName != null &&
				ele.nodeName.toLowerCase() == nodeName.toLowerCase();
		}
	},
	isHTMLInputElement : function(ele) {
		return FormUtil.isHTMLElement(ele, "input");
	},
	isHTMLTextAreaElement : function(ele) {
		return FormUtil.isHTMLElement(ele, "textarea");
	},
	isHTMLSelectElement : function(ele) {
		return FormUtil.isHTMLElement(ele, "select");
	},
	/*
	 * 给 HTML 标签设置
	 * @scope public
	 * @param ele: 标签
	 * @param val: 标签的值
	 */
	setValue : function(ele /*:html 标签元素对象*/,val /*: String*/) { 
		// Select 标签
		if (FormUtil.isHTMLSelectElement(ele))
		{
			var found  = false;
			var i;
			for (i = 0; i < ele.options.length; i++) {
				if (ele.options[i].value == val){
					ele.options[i].selected = true;
					found = true;
				} else {
					ele.options[i].selected = false;
				}
			}
			if (found) {
				return;
			}
			for (i = 0; i < ele.options.length; i++) {
				if (ele.options[i].text == val) {
					ele.options[i].selected = true;
					break;
				}
			}
			return;
		}
		// Input 标签
		if (FormUtil.isHTMLInputElement(ele)) {
			switch (ele.type) {
			case "checkbox":
			case "check-box":
			case "radio":
				ele.checked = (val == true);
				return;
			case "hidden":
			case "text":
				ele.value = val;
				return;
			default:
				alert("Not sure how to setValue on a input element of type " + ele.type);
				ele.value = val;
				return;
			}
		}
		// TextArea 标签
		if (FormUtil.isHTMLTextAreaElement(ele)) {
			ele.value = val;
			return;
		}
	},
	
	/*
	 * 给 HTML 标签设置
	 * @scope public
	 * @param ele: 标签
	 * @param val: 标签的值
	 */
	setValues : function(result /*:json 对象*/){
		if(result == undefined || result == null ) return;
	
		for(var name in result){
			//alert(result[name]);
			if($(name)){
				FormUtil.setValue($(name),result[name]);
			}
		}
	},
	
	/*
	 * 获取 Select 标签的结构
	 * @scope public
	 * @param result: 一个 json 数组对象(List<LabelValueBean>)， 
	 * 
	 * 	Option 标签的文本为 result[i].label || result[i]
	 *  Option 标签的值为 result[i].value || result[i]
	 */
 	buildSelectOptions :function(slt /*:Selcet 标签元素对象*/,options /* json 数组对象 */) {
		if (options && options.join) { 
			// 清空 select 标签的所有选项
			slt.options.length = 0;
			
			// 添加一个默认的“请选择”选项
			var defaultOpt = new Option('Please select...', '');
			slt.options[0] = defaultOpt;
			
			// 给 Select 标签设置值
			for (var i = 0; i < options.length; i++) {
				var opt = new Option(options[i].label || options[i], options[i].value || options[i]);
				slt.options[i + 1] = opt;
			}
		}
	}
};
