
function byid(id) {
	return document.getElementById(id);
}
////////////////////////////////////////
// debug
function addDebugPane() {
	var body = document.getElementsByTagName('body')[0];
	body.appendChild(document.createTextNode("\n\n["));
	var span = document.createElement('span');
	span.id = 'debug';
	span.onclick = function() { this.innerHTML = ''; }
	body.appendChild(span);
	body.appendChild(document.createTextNode("]\n"));
}
function debug(s) {
	var debugPane = byid('debug');
	if (undefined == debugPane) {
		addDebugPane();
		debugPane = byid('debug');
	}
	debugPane.innerHTML += s + '<br>\n';
}
function println(s) {
	var body = document.getElementsByTagName('body')[0];
	body.appendChild(document.createTextNode(s));
	body.appendChild(document.createElement('br'));
	body.appendChild(document.createTextNode('\n'));
}
function println_span(s) {
	var body = document.getElementsByTagName('body')[0];
	var span = document.createElement('span');
	body.appendChild(span);
	span.innerHTML = s;
	body.appendChild(document.createElement('br'));
	body.appendChild(document.createTextNode('\n'));
}
////////////////////////////////////////
// trim
function ltrim(s) {
	return s.replace(/^\s+/, "");
}
function rtrim(s) {
	return s.replace(/\s+$/, "");
}
function trim(s) {
	return rtrim(ltrim(s));
}
////////////////////////////////////////
// String
String.prototype.lengthB = function() {
	var len = this.length;
	var lenB = 0;
	for (var i = 0; i < len; i++) {
		var chr = this.charAt(i);
		if (escape(chr).length > 4) {
			lenB += 2;
		} else {
			lenB++;
		}
	}
	return lenB;
};
String.prototype.substrB = function(offsetB, lengthB) {
	var len = this.length;
	var off = 0;
	var lenB = 0;
	for (var i = 0; i < len; i++) {
		var chr = this.charAt(i);
		if (escape(chr).length > 4) {
			lenB += 2;
		} else {
			lenB++;
		}
		if (0 == off && offsetB == lenB) off = i + 1;
		if (lenB-offsetB > lengthB) return this.substring(off, i);
	}
	return this.substring(off);
};
////////////////////////////////////////
// user type check
var is_empty = function(s) {
	s = trim(s);
	return (null == s.match(/^.+$/));
}
var is_not_empty = function(s) {
	s = trim(s);
	//debug(s.match(/^.+$/));
	return (null != s.match(/^.+$/));
}
var is_multi_line = function(s) {
	s = trim(s);
	return (null != s.match(/^.+$/m));
}
var is_number = function(s) {
	return (null != s.match(/^\d+$/));
}
var is_date = function(s) {
	return (null != s.match(/^\d{4}-\d{2}-\d{2}$/));
}
var is_date_range = function(s1, s2) {
	if (is_date(s1) && is_date(s2)) {
		return (s1 <= s2);
	}
	return true;
}
var is_phone_number = function(s) {
	return (null != s.match(/^\d{3}-?\d{3,4}-?\d{4}$/));
}
var is_email = function(s) {
	return (null != s.toLowerCase().match(/^[a-z0-9_\.]+@[a-z0-9\-\.]*[a-z0-9\-]+\.(com|net|org|edu|kr)$/));
}
// TODO is_zip_code
// TODO is_hangul
////////////////////////////////////////
// form field hint
function toggleHelp(topic) {
	if (byid('msg_'+topic).style.display == 'none') {
		showHelp(topic);
	} else {
		hideHelp(topic);
	}
}
function showHelp(topic) {
	var elem = byid('msg_'+topic);
	//elem.onclick = function() { hideHelp(topic); /* this.innerHTML = ''; */ };
	//elem.style.display = 'block';
	alert(elem.innerHTML);
}
function hideHelp(topic) {
	var elem = byid('msg_'+topic);
	elem.style.display = 'none';
}
////////////////////////////////////////
// form field verification -- glbal variable params required
function verify(s, method) {
	//debug(typeof method);
	if (method instanceof RegExp) {
		return (null != s.match(method));
	}
	if (typeof method != 'undefined' && method instanceof Function) {
		return method(s);
	}
	return false;
}
function check_params() {
	for (var param_name in params) {
		hideHelp(param_name);	
	}
	for (var param_name in params) {
		var elem = byid('input_'+param_name);
		elem.value = trim(elem.value);
		if (false == verify(elem.value, params[param_name])) {
			//debug(param_name + ' not passed');
			showHelp(param_name);
			if ('contents' == param_name) {
				if (undefined != byid('input_editor_type') && 'Editor' == byid('input_editor_type').value) {
					return false;
				}
			}
			elem.focus();
			return false;
		}
	}
	return true;
}
////////////////////////////////////////
// navigation

////////////////////////////////////////
// field_cd, organ_cd selection
