/*******************************************************************************
 * (c) 2008 ABC Solutions Inc.
 ******************************************************************************/

function $(id) {
	var o = document.getElementById(id);
	if (o)
		return o;

	o = document.getElementsByName(id);
	
	return o ? o[0] : null;
}

function $$(tagName) {
	return document.getElementsByTagName(tagName);
}

/**
 * set states of checkboxes/radios whose name starts with string n inside of form object f.
 * if state is not given, it toggles the states
 */
function invertCheckElements(f, n, state) {
	var o = f.elements;
	var invert = typeof (state) == 'undefined';

	for (var i = 0; i < o.length; i++) {
		if (o[i].name.indexOf(n) == 0
				&& (o[i].type == 'checkbox' || o[i].type == 'radio')
				&& !o[i].disabled) {
			o[i].checked = invert ? !o[i].checked : (state ? true : false);
		}
	}
}

/**
 * get checked checkboxes/radios whose name starts with string n inside of form object f
 */
function getCheckedElements(f, n) {
	var checked = new Array();
	var o = f.elements;

	for (var i = 0; i < o.length; i++)
		if (o[i].name.indexOf(n) == 0
				&& (o[i].type == 'checkbox' || o[i].type == 'radio')
				&& o[i].checked)
			checked[checked.length] = o[i];

	return checked;
}

function _String_Trim() {
	return this.toString().replace(/(^[\s　]+)|([\s　]+$)/g, "");
}

String.prototype.trim = _String_Trim;

/**
 * trims all values of <input type=text> of a form
 */
function trimAllInputTexts(f) {
	for (var i = 0; i < f.elements.length; i++)
		if (f.elements[i].type == 'text')
			f.elements[i].value = f.elements[i].value.trim();
}

/**
 * check if a string is of 7-bit-only characters
 */
function asciiOnly(s) {
	for (var i = 0; i < s.length; i++)
		if (s.charCodeAt(i) > 127)
			return false;

	return true;
}

function getAjax() {
	try {
		return new XMLHttpRequest();
	} catch (e) { // IE 6 이하
		try {
			return new ActiveXObject("MSXML2.XMLHTTP.3.0");
		} catch (e) {
			alert("최신 브라우저를 사용하세요~");
			return;
		}
	}
}

/**
 * 컨텐트에 맞춰 iframe 크기 조정
 * http://www.faqts.com/knowledge_base/view.phtml/aid/1076/fid/127 에서 차용
 */
function adjustIFrameSize(iframeWindow) {
	if (iframeWindow.document.height) {
		var iframeElement = document.getElementById(iframeWindow.name);
		iframeElement.style.height = iframeWindow.document.height + 'px';
		iframeElement.style.width = iframeWindow.document.width + 'px';

	} else if (document.all) {
		var iframeElement = document.all[iframeWindow.name];
		if (iframeWindow.document.compatMode &&
				iframeWindow.document.compatMode != 'BackCompat') {
			iframeElement.style.height = iframeWindow.document.documentElement.scrollHeight + 5 + 'px';
			iframeElement.style.width = iframeWindow.document.documentElement.scrollWidth + 5 + 'px';

		} else {
			iframeElement.style.height = iframeWindow.document.body.scrollHeight + 5 + 'px';
			iframeElement.style.width = iframeWindow.document.body.scrollWidth + 5 + 'px';
	    }
	}
}

