/**
 * name에 해당하는 Element들을 반환한다.
 * @param	name	Element에 선언된 name
 * @return 	해당 Element 배열
 */
function _getObjects(name) {
	return document.getElementsByName(name);
}

/**
 * name에 해당하는 Element들 중 해당 index에 Element을 반환한다.
 * 만약, index를 선언하지 않았다면 첫번째 index의 Element를 반환한다.
 *
 * @param	name	Element에 선언된 name
 * @param	index	배열 첨자
 * @return	해당 Element 집합 중 index에 해당하는 Element
 */
function _getObject(name, index) {
	index = index || 0;
	return _getObjects(name)[index];
}

/**
 * id에 해당하는 Element를 반환한다.
 *
 * @param	id		Element에 선언된 id
 * @return 	해당 Element
 */
function _getLayer(id) {
	return document.getElementById(id);
}

/**
 * Element에서 tag 명칭에 해당하는 집합을 반환한다.
 *
 * @param	tag		Element 명칭
 * @return 	해당 Element 집합
 */
function _getTags(tag) {
	return document.getElementsByTagName(tag);
}

/**
 * Element에서 tag 명칭에 해당하는 집합중 index에 해당하는 Element를 반환한다.
 * 만약, index를 선언하지 않았다면 첫번째 index의 Element를 반환한다.
 * 
 * @param	tag		Element 명칭
 * @param	index	배열 첨자
 * @return 	해당 Element 집합
 */
function _getTag(name, index) {
	index = index || 0;
	return _getTags(name)[index];
}

function _setFocus(name) {
	var obj = _getObject(name);
	if ( typeof(obj) == "object" ) {
		obj.focus();
	}
}

function _removeCombo(name, idx) {
	var obj = _getObject(name, idx);
	var len = obj.length - 1;
	var min = ( obj.size > 1 ? 0 : 1 );
	for ( var i = len ; i >= min ; i-- ) {
		if ( obj[i].value != "" ) {
			obj.remove(i);
		}
	}
}

function _removeAllCombo(name, idx) {
	var obj = _getObject(name, idx);
	var len = obj.length - 1;
	for ( var i = len ; i >= 0 ; i-- ) {
		obj.remove(i);
	}
}
function _addCombo(name, text, value, idx) {
	idx = idx || 0;
	if ( typeof(name) != "object" ) {
		obj = _getObject(name, idx);
	}
	else {
		obj = name;
	}

	var opt = document.createElement("OPTION");
	opt.text = text;
	opt.value = value;

	obj.options.add(opt);

	return opt;
}

function _selectCombo(name, value) {
	obj = _getObject(name);
	for ( var i = 0 ; i < obj.options.length ; i++ ) {
		if ( obj[i].value == value ) {
			obj[i].selected = true;
			break;
		}
	}
}

function _formatCurrency(price) {
	var currency = "";
	var word = new Array();
	if ( typeof(price) != "string" ) {
		price = new String(price);
	}
	for ( var i = price.length ; i >= 0 ; i-=3 ) {
		word[word.length] = price.substring(i-3 < 0 ? 0 : i-3, i);
	}
	
	
	for ( var i = word.length - 1 ; i >= 0 ; i-- ) {
		if ( i < word.length - 1 ) {
			currency += ",";
		}
		currency += word[i];
	}
	
	return currency;
}
/**
function _innerHTMLInTable(id, contents, ) {
	var table = document.getElementById(id);
    var tb = document.createElement("TBODY");
	var tr = document.createElement("TR");
	
	for ( var i = 0 ; i < contents.length ; i++ ) {
		var td = document.createElement("TD");
		td.innerHTML = contents[i];
		tr.appendChild(td);
	}
	tb.appendChild(tr);
	
	table.appendChild(tb);
}
*/

function _removeAllRows(id) {
	var table = _getLayer(id);
	if ( table.childNodes.length > 0 ) {
		for ( var i = table.childNodes.length - 1 ; i >= 0 ; i-- ) {
			table.removeChild(table.childNodes[i]);
		}
	}
}

function _appendRow(id, tr) {
	var table;
	if ( typeof(id) != "object" ) {
		table = _getLayer(id);
	}
	else {
		table = id;
	}
    var tb = document.createElement("TBODY");

	tb.appendChild(tr);	
	table.appendChild(tb);	

	return table;
}

function _createRow(attributes) {
	var attributes = attributes || null;
	var tr = document.createElement("TR");

	if ( null != attributes ) {
		for ( var i = 0 ; i < attributes.entries.length ; i++ ) {
			eval("tr." + attributes.entries[i].key + " = '" + attributes.entries[i].value + "'");
		}
	}

	return tr;
}

function _appendColumn(tr, td) {
	tr.appendChild(td);
	return tr;
}

function _createColumn(value, attributes) {
	var value = value || null;
	var attributes = attributes || null;

	var td = document.createElement("TD");
	
	if ( null != value ) {
		td.innerHTML = value;
	}
	
	if ( null != attributes ) {
		for ( var i = 0 ; i < attributes.entries.length ; i++ ) {
			eval("td." + attributes.entries[i].key + " = '" + attributes.entries[i].value + "'");
		}
	}

	return td;
}

function _Entry(key, value) {
	this.key = key;
	this.value = value;
}

function _Map() {
	this.entries = new Array();
}

_Map.prototype.put = function(key, value) {
	this.entries[this.entries.length] = new _Entry(key, value);
}

_Map.prototype.replace = function(key, value) {
	var index = -1;
	for ( var i = 0 ; i < this.entries.length ; i++ ) {
		if ( key == this.entries[i].key ) {
			index = i;
			this.entries[i].value = value;
			break;
		}
	}
	if ( -1 == index ) {
		this.put(key, value);
	}
}

_Map.prototype.remove = function(key) {
	var newEntries = new Array();
	for ( var i = 0 ; i < this.entries.length ; i++ ) {
		if ( key != this.entries[i].key ) {
			newEntries[newEntries.length] = new _Entry(this.entries[i].key, this.entries[i].value);
		}
	}
	this.entries = newEntries;
}

_Map.prototype.get = function(key) {
	for ( var i = 0 ; i < this.entries.length ; i++ ) {
		if ( key == this.entries[i].key ) {
			return this.entries[i].value;
		}
	}
	return null;
}
_Map.prototype.contain = function(key) {
	if ( this.get(key) == null ) {
		return false;
	}
	else {
		return true;
	}
}
