onload=init;

function dump_props(objName)
{
	var obj = eval(objName)
	var result = ""
	for (var i in obj) {
		result += objName + "." + i + " = " + obj[i] + "\n"
	}

	var a = window.open('', a);
	a.document.writeln('<pre>');
	a.document.writeln(result);
	a.document.writeln('</pre>');

	return result
}

function init()
{		
	//Form.enable(f); // 表單解鎖
	//buttons.push(Form.getInputs(f, 'submit'));
	
	// view state 處理	
	if(viewState.length > 0) // viewState 有被設定
	{
		//alert(viewState);
		var f = $("input_form");
		var elements = Form.getElements(f);
		var obj = eval('(' + viewState + ')');
		
		//dump_props(obj);
		// 2.1 POST 資料回復
		var posts = obj.posts;

		for (var i in posts)
		{
			//alert(i);
			if(typeof(posts[i]) == 'object')
			{
				var buf = posts[i];
				var inputData = new Array();

				for(var j in buf) {
					inputData.push(buf[j]);
				}

				setInputByValue(f[i + '[]'], buf);
			}
			else
			{
				setInputByValue(f[i], posts[i]);
			}
		}

		// 2.2 錯誤輸入項標記
		var errorInputPrompts = obj.errorInputPrompts;

		for (var i in errorInputPrompts)
		{
			var td = $('td_' + i);
			//td.className = 'tb_error_col';
			
			var promptSpan = document.createElement('div');
			promptSpan.className = 'highlight_notice';			
			promptSpan.innerHTML = errorInputPrompts[i];

			td.appendChild(promptSpan);

		}
	}
}

/*
 * 重設 Form 表單之值
 */

function setInputByValue(o, s)
{
	if(typeof(o) == "undefined") {
		return false;
	}
	if(typeof(o.length) == "undefined")
	{
		if((o.type == 'radio' || o.type == 'checkbox') && o.value == s) {
			o.checked = true;
		}
		if((o.type == 'text' || o.type == 'textarea' || o.type == 'hidden' || o.type == 'password')) {
			o.value = s;
			//var string_buffer=document.createElement("DIV");
			//string_buffer.innerHTML = s;
			//o.value = string_buffer.innerHTML;

			//o.fireEvent("onclick");
		}
	}
	else
	{
		for(i=0 ; i < o.length ; i++)
		{
			// 當 s 為一個陣列
			if(typeof(s) == 'object' && typeof(s.length) != 'undefined')
			{
				// 先取消現有的選取
				for(j=0,jmax=s.length ; j < jmax ; j++) {
					if(o.type == 'select-multiple') {
						o.options[i].selected = false;
						//o.fireEvent("onchange");
					}
					if(o[i].type == 'checkbox') {
						o[i].checked = false;
					}
				}
				// 再將需要的選取起來
				for(j=0,jmax=s.length ; j < jmax ; j++) {
					if(o.type == 'select-multiple') {
						if(o.options[i].value == s[j]) {
							o.options[i].selected = true;
							//o.fireEvent("onchange");
						}
					}
					if(o[i].type == 'checkbox') {
						if(o[i].value == s[j]) {
							o[i].checked = true;
							//o[i].fireEvent("onclick");
						}
					}
				}
			}
			else
			{
				if(o.type == 'select-one') {
					if(o.options[i].value == s) {
						//o.selectedIndex = i;
						o.options[i].selected = true;
						//o.fireEvent("onchange");
					}
				}
				if((o[i].type == 'radio' || o[i].type == 'checkbox') && o[i].value == s) {
					o[i].checked = true;
					//o[i].fireEvent("onclick");
				}
				if((o[i].type == 'text' || o[i].type == 'textarea' || o[i].type == 'hidden' || o[i].type == 'password' )) {
					o[i].value = s;
				}
			}
		}
	}
	return true;
}