if (String.prototype.hasOwnProperty('trim') == false) {
	String.prototype.trim = function() {
		return this.replace(/^\s*|\s*$/, '');
	}
}

if (String.prototype.hasOwnProperty('bytelength') == false) {
	String.prototype.bytelength = function() {
		var length = 0;
		if (this.length <= 0)
			return 0;
		for (var i = 0; i < this.length; ++i) {
			var c = escape(this.charAt(i));
			if (c.indexOf("%u") != -1)
				length += 2;
			else
				++length;
		}

		return length;
	}
}

function resetFileForm(el, name, id) {
	var fileid = name + '_file' + id;
	var textid = name + '_txt' + id;
	var obj = document.getElementById(fileid);
	obj.value = ''; // for FireFox
	var obj2= obj.cloneNode(false);
	obj.parentNode.replaceChild(obj2, obj);

	if (id > 1) {
		var divid = name + '_div' + id;
		var div = document.getElementById(divid);
		div.style.display = 'none';
	}
	el.checked=false;
	document.getElementById(textid).value = '';
}

/**
 * toggle sublist for a patch list
 *
 * @author	Won-Kyu Park <wkpark@kldp.org>
 * @since	2009-05-27
 * @license	GPLv2
 */

function togglePatchInfo(el) {
	var info = el.parentNode.getElementsByTagName('dd')[0];
	if ( info.style.display == 'none') {
		info.style.display = 'block';
        	$(el).addClassName('minus');
	} else {
		info.style.display = 'none';
        	$(el).removeClassName('minus');
	}
}

/**
 * toggle diff section for a patch list
 *
 * @author	Won-Kyu Park <wkpark@kldp.org>
 * @since	2009-05-27
 * @license	GPLv2
 */

function togglePatchDiff(el) {
	var diff = el.parentNode.parentNode.parentNode.getElementsByTagName('pre')[0];
	if ( diff.style.display == 'none') {
		diff.style.display = 'block';
        	$(el).addClassName('minus');
		if (diff.parentNode.style.display == 'none')
			diff.parentNode.style.display = 'block';
	} else {
		diff.style.display = 'none';
        	$(el).removeClassName('minus');
		if (diff.parentNode.style.display == 'block')
			diff.parentNode.style.display = 'none';
	}
}


/**
 * hack for a label as a checkbox
 *
 * @author	Won-Kyu Park <wkpark@kldp.org>
 * @since	2009-04-25
 * @license	GPLv2
 */

function toggleCheckBox(el) {
	var id = el.getAttribute('for');
	if (!id) // for IE;;
		id = el.attributes['for'].value;
	var check = document.getElementById(id);
	var file = el.parentNode.parentNode.firstChild;
	if (check.checked == false) {
		el.className='checked';
		file.className = 'file checked';
	} else {
		el.className='';
		file.className = 'file';
	}
}

/* from bugzilla */
/**
 * Adds the reply text to the `comment' textarea
 */
function replyToComment(id,txtarea) {
	/* pre id="comment_name_N" */
	var text_elem = document.getElementById('comment-text-'+id);
	var text = _getText(text_elem);

	/**
	 * make sure we split on all newlines -- IE or Moz use \r and \n
	 * respectively
	 */
	text = text.split(/\r|\n/);

	var replytext = "";
	for (var i=0; i < text.length; i++) {
		replytext += "> " + text[i] + "\n"; 
		//replytext += " " + text[i] + "\n"; 
	}

	replytext = "(In reply to comment " + id + ")\n" + replytext + "\n";

	/* <textarea name="savetext"> XXX */
	var textarea;
	if (txtarea) {
		var textarea = document.getElementById(txtarea);
	} else {
		var textarea = document.getElementsByTagName('textarea')[0];
	}
	textarea.value += replytext;

	textarea.focus();
}
 
if (!Node) {
	/* MSIE doesn't define Node, so provide a compatibility array */
	var Node = {
		TEXT_NODE: 3,
		ENTITY_REFERENCE_NODE: 5
	};
}
 
/**
 * Concatenates all text from element's childNodes. This is used
 * instead of innerHTML because we want the actual text (and
 * innerText is non-standard)
 */
function _getText(element) {
	var child, text = "";
	for (var i=0; i < element.childNodes.length; i++) {
		child = element.childNodes[i];
		var type = child.nodeType;
		if (type == Node.TEXT_NODE || type == Node.ENTITY_REFERENCE_NODE) {
			text += child.nodeValue;
		} else {
			/* recurse into nodes of other types */
			text += _getText(child);
		}
	}
	return text;
}

/**
 * Set some msg for input form : Set msg first. Clear msg on focus out.
 *
 * @since	2009/09/02
 * @author	Wom-Kyu Park <wkpark@kldp.org>
 */

function set_input_msg(el, msg) {
	if (el != undefined && el.value == '') {
		el.value = msg;
		el.select();
		el.onblur = function() { this.value==msg ? this.value='' : false; };
		el.onclick = function() { this.value==msg ? this.value='' : false; };
	}
}

/**
 * 통합 검색에서 프로젝트 전체를 검색할 때 action을 변경한다.
 */
function change_search_action() {
	if ($('type_of_search').value == 'soft') {
		$('form_group_name').value = $('search_word').value;
		$('go').action = '/projects/trove_list.php';
	} else {
		$('go').action = '/search/';
	}
}

/**
 * set comment textarea height size auto resizeing
 * 
 * @since	2009/12/15
 * @author	Gyeong-Nam Kim <boptop@nhn.com>
 */
function setLine(txa){ 
	line = 5 
	new_line = txa.value.split( "\n" ).length + 1; 
	if( new_line < line ) new_line = line; 
	txa.rows = new_line; 
}

/**
 * get webkit version for safari and chrome
 *
 * @since	2010/06/18
 */
function webkit_version() {
    var version = 0;
    var reg = /AppleWebKit\/([\d.]+)/;
    var result = reg.exec(navigator.userAgent);

    if(result) {
        version = parseFloat(result[1]);
    }
    return version;
}

/**
 * fix button margins bug of the old webkit for safari and chrome
 *
 * @since	2010/06/18
 */
function fix_webkit() {
	var wkv = webkit_version();
	var agent = navigator.userAgent + "";
	if (wkv > 0 && wkv < 534.0 && !agent.match(/Chrome/)) {
		var sheet = document.createElement('style');
		sheet.innerHTML =
			"@media screen and (-webkit-min-device-pixel-ratio:0) { button span { margin-top:-1px;margin-right:-3px; }}";
		document.body.appendChild(sheet);
	}
}

