/**
 * String.trim()
 */
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/gi, "");
}
/**
 * String.ltrim()
 */
String.prototype.ltrim = function() {
	return this.replace(/(^\s*)/g, "");
}
/**
 * String.rtrim()
 */
String.prototype.rtrim = function() {
	return this.replace(/(\s*$)/g, "");
}
/**
 * String.lpad()
 */
String.prototype.lpad = function(padLength, padChar) {
    padChar = padChar == null ? " " : padChar;
    var result = "";
    for (var i = 0, n = padLength - this.byteLength(); i < n; i++) {
        result += padChar;
    }

    return result + this;
}
/**
 * String.rpad()
 */
String.prototype.rpad = function(padLength, padChar) {
    padChar = padChar == null ? " " : padChar;
    var result = this;
    for (var i = 0, n = padLength - this.byteLength(); i < n; i++) {
        result += padChar;
    }

    return result;
}
/**
 * String.replaceAll(from, to)
 */
String.prototype.replaceAll = function(from, to) {
	return this.replace(new RegExp(from, "g"), to);
}
/**
 * String.removeAll(ch)
 */
String.prototype.removeAll = function(ch) {
	return this.replaceAll(ch, "");
}
/**
 * String.reverse()
 */
String.prototype.reverse = function() {
	var result = "";
	var i = this.length;

	while (i > 0) {
		result += this.charAt(--i);
	}

	return result;
}
/**
 * String.byteLength()
 */
String.prototype.byteLength = function() {
    var result = 0;
    for (var i = 0; i < this.length; i++) {
        var chr = escape(this.charAt(i));
        if (chr.length == 1 ) {
            result++;
        } else if (chr.indexOf("%u") != -1) {
            result += 2;
        } else if (chr.indexOf("%") != -1) {
            result += chr.length / 3;
        }
    }
    return result;
}
/**
 * String.byteIndexOf()
 */
String.prototype.byteIndexOf = function(chr) {
    var idx = this.indexOf(chr);
    if (idx == -1) {
        return idx;
    } else {
        return this.substring(0, idx).byteLength();
    }
}
/**
 * String.substringByte()
 */
String.prototype.substringByte = function (start, end) {
    var result = "";

    var preByte = 0;
    var curByte = 0;

    for (var i = 0; i < this.length; i++) {
        var chr = this.charAt(i);

        preByte = curByte;
        curByte += chr.byteLength();

        if (preByte >= start && curByte <= end) {
            result += chr;
        } else if (curByte > end) {
            break;
        }
    }

    return result;
}
/**
 * String.substrByte()
 */
String.prototype.substrByte = function(start, length) {
    return this.substringByte(start, start + length);
}

/**
 * String.defaultString()
 */
String.prototype.defaultString = function(def) {
	return this.trim() == "" ? def : this;
}


/**
 * String.toInt()
 */
String.prototype.toInt = function() {
    return this.trim() == "" ? 0 : parseInt(this.trim(), 10);
}
/**
 * String.toHex()
 */
String.prototype.toHex = function() {
    return this.toRadix(16);
}
/**
 * String.toRadix()
 */
String.prototype.toRadix = function(radix) {
    return Number(this).toString(radix).toUpperCase();
}
/**
 * String.isEmpty()
 */
String.prototype.isEmpty = function() {
    return this.trim().length == 0;
}
/**
 * String Number
 */
String.prototype.toNumber = function() {
	return Number(this.unmask("real"));
}
/**
 */
String.prototype.toDate = function() {
	var sDate = this.unmask("date");
	var iYear = sDate.substr(0, 4).toNumber();
	var iMonth = sDate.substr(4, 6).defaultString("0").toNumber() - 1;
	var iDate = sDate.substr(6, 4).defaultString("1").toNumber();

	return new Date(iYear, iMonth, iDate);
}
/**
 * String.isDigit()
 */
String.prototype.isDigit = function() {
    if (this.isEmpty()) {
        return true;
    }

    return this.search(/^\d+$/) != -1;
}
/**
 * String.isInt()
 */
String.prototype.isInt = function() {
    if (this.isEmpty()) {
        return true;
    }

    return this.search(/^(?:\-?|\+?)\d+$/) != -1;
}
/**
 * String.isDouble()
 */
String.prototype.isDouble = function(dec,frac) {
    if (this.isEmpty()) {
        return true;
    }
    oriStr  = this.removeAll(",");
    if (oriStr.indexOf(".") >= 0) {
        decStr  = oriStr.substring(0,oriStr.indexOf("."));
        fracStr = oriStr.substring(oriStr.indexOf(".")+1);
    } else {
        decStr  = oriStr;
        fracStr = "";
    }
    if (dec < decStr.length || frac < fracStr.length) return false;

    return !isNaN(this);
}
/**
 */
String.prototype.isNumber = function() {
    if (this.isEmpty()) {
        return true;
    }

    return !isNaN(this);
}
/**
 */
String.prototype.isZipNo = function() {
    if (this.isEmpty()) {
        return true;
    }

    return this.search(/^\d{3}\-?\d{3}$/) != -1;
}
/**
 */
String.prototype.isPhoneNo = function() {
    if (this.isEmpty()) {
        return true;
    }

    return this.search(/^(?:0\d{1,2}\-?\d{2,4}|\d{2,4})\-?\d{4}$/) != -1;
}

/**
 */
String.prototype.isYMD = function() {
    if (this.isEmpty()) {
        return true;
    }

    if (this.search(/^(\d{4})[\/|\-|.]?(\d{2})[\/|\-|.]?(\d{2})$/) == -1) {
        return false;
    }

    var lastDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if (RegExp.$2 > 12 || RegExp.$2 < 01) {
        return false;
    }

    if (RegExp.$3 > lastDays[RegExp.$2 - 1] || RegExp.$3 < 01) {
        return false;
    }

    if (RegExp.$2 == 2
            && RegExp.$3 > 28
            && (RegExp.$1 % 4 != 0 || (RegExp.$1 % 100 == 0 && RegExp.$1 % 400 != 0))) {
        return false;
    }

    return true;
}
/*for "xxxx-xx-xx"*/
String.prototype.isYMD1 = function() {
    if (this.isEmpty()) {
        return true;
    }

    if (this.search(/^(\d{4})-{1}?(\d{2})-{1}?(\d{2})$/) == -1) {
        return false;
    }
    var lastDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if (RegExp.$2 > 12 || RegExp.$2 < 01) {
        return false;
    }

    if (RegExp.$3 > lastDays[RegExp.$2 - 1] || RegExp.$3 < 01) {
        return false;
    }

    if (RegExp.$2 == 2
            && RegExp.$3 > 28
            && (RegExp.$1 % 4 != 0 || (RegExp.$1 % 100 == 0 && RegExp.$1 % 400 != 0))) {
        return false;
    }

    return true;
}
/**
 */
String.prototype.isYM = function() {
    if (this.isEmpty()) {
        return true;
    }

    if (this.search(/^(\d{4})[\/|\-|.]?(\d{2})$/) == -1) {
        return false;
    }

    if (RegExp.$2 > 12 || RegExp.$2 < 01) {
        return false;
    }

    return true;
}
/**
 * String.isMD() method \uFFFD\uFFFD\uFFFD\uFFFD - \uFFFD\uFFFD\uFFFD\uFFFD
 * \uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD => / or - or .
 */
String.prototype.isMD = function() {
    if (this.isEmpty()) {
        return true;
    }
    if (this.search(/^(\d{2})[\/|\-|.]?(\d{2})$/) == -1) {
        return false;
    }

    var lastDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if (RegExp.$1 > 12 || RegExp.$1 < 01) {
        return false;
    }

    if (RegExp.$2 > lastDays[RegExp.$1 - 1] || RegExp.$2 < 01) {
        return false;
    }
    return true;
}
/**
 * String.isHMS() method \uFFFD\uFFFD\uFFFD\uFFFD - \uFFFD\uFFFD\uFFFD\uFFFD
 */
String.prototype.isHMS = function() {
    if (this.isEmpty()) {
        return true;
    }

    if (this.search(/^([0|1|2]\d):?(\d{2})?:?(\d{2})?$/) == -1) {
        return false;
    }

    if (RegExp.$1 > 23 || RegExp.$2 > 59 || RegExp.$3 > 59) {
        return false;
    }

    return true;
}
/**
 * String.isMS() method \uFFFD\uFFFD\uFFFD\uFFFD - \uFFFD\uFFFD\uFFFD\uFFFD
 */
String.prototype.isMS = function() {
    if (this.isEmpty()) {
        return true;
    }

    if (this.search(/^(\d{2}):?(\d{2})$/) == -1) {
        return false;
    }

    if (RegExp.$1 > 59 || RegExp.$2 > 59) {
        return false;
    }

    return true;
}
/**
 * String.isEmail() method \uFFFD\uFFFD\uFFFD\uFFFD - email
 */
String.prototype.isEmail = function() {
    if (this.isEmpty()) {
        return true;
    }

	var checkTLD = 1;
	var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray = this.match(emailPat);

	if (matchArray == null) {
		return false;
	}

	var user = matchArray[1];
	var domain = matchArray[2];

	for (i = 0; i < user.length; i++) {
		if (user.charCodeAt(i) > 127) {
			return false;
	   }
	}

	for (i = 0; i < domain.length; i++) {
		if (domain.charCodeAt(i) > 127) {
			return false;
	    }
	}

	if (user.match(userPat) == null) {
		return false;
	}

	var ipArray = domain.match(ipDomainPat);

	if (ipArray != null) {
		for (var i = 1;i <= 4; i++) {
			if (ipArray[i] > 255) {
				return false;
 		    }
		}
		return true;
	}

	var atomPat = new RegExp("^" + atom + "$");
	var domArr = domain.split(".");
	var len = domArr.length;

	for (i = 0; i < len; i++) {
		if (domArr[i].search(atomPat) == -1) {
			return false;
	   }
	}

	if (checkTLD && domArr[domArr.length - 1].length != 2
		&& domArr[domArr.length - 1].search(knownDomsPat) == -1) {
		return false;
	}

    return len >= 2;
}
/**
 * String.isFormat() method \uFFFD\uFFFD\uFFFD\uFFFD - \uFFFD\uFFFD\uFFFD\uFFFD \uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD \uFFFD\uFFFD\uFFFD\uFFFD \uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD
 */
String.prototype.isFormat = function() {

    if (this.isEmpty()) {
        return true;
    }

    var formatStr = arguments[0].split("-");
    var searchStr = "this.search(/^";
    for (var _i=0; _i < formatStr.length; _i++) {
        if (_i==0) {
            searchStr += "\\d{"+formatStr[_i]+"}";
        } else {
            searchStr += "\\-?\\d{"+formatStr[_i]+"}";
        }
    }
    searchStr += "$/) != -1;";

    return eval(searchStr);
}
/**
 * URLBuiler Object
 */
function URLBuilder(url) {
    this.url      = url;
    this.paramMap = new Map();

    this.add = function(name, value) {
        this.paramMap.put(name, value);
    }


    this.get = function(name) {
        return this.paramMap.get(name);
    }


    this.remove = function(name) {
        return this.paramMap.remove(name);
    }


    this.clear = function() {
        return this.paramMap.clear();
    }


    this.queryString = function() {
        return this.paramMap.toString();
    }


    this.build = function() {
        return this.url + "?" + this.queryString();
    }
}
/*  Netscape/Mozilla insertAdjacentHTML emulation
 *  http://forums.mozilla.or.kr/viewtopic.php?t=678, http://www.faqts.com/knowledge_base/view.phtml/aid/5756
**/
if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){
	HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode){
		switch (where){
			case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this)
			break;
			case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild);
			break;
			case 'beforeEnd':
			this.appendChild(parsedNode);
			break;
			case 'afterEnd':
			if (this.nextSibling) this.parentNode.insertBefore(parsedNode,this.nextSibling);
			else this.parentNode.appendChild(parsedNode);
			break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr) {
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	}


	HTMLElement.prototype.insertAdjacentText = function(where,txtStr){
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	}
}

//////////////////////////////////////////////////
function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
//  theSel.options[selLength].selected = 1;
}

function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}

function moveOptions(theSelFrom, theSelTo)
{
  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
   addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
}
function moveAllOptions(theSelFrom, theSelTo)
{
  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      deleteOption(theSelFrom, i);
      selectedCount++;
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
}

function bookmarksite(title,url){
 if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
 else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
 }else if(document.all)// ie
	window.external.AddFavorite(url, title);
}