// === string °´Ã¼ °ü·Ã ½ÃÀÛ === //

/**
 * ¹®ÀÚ¿­ÀÇ ¹ÙÀÌÆ® ±æÀÌ¸¦ ¹ÝÈ¯
 * 
 * param	: ¾øÀ½
 *
 * return	: int
 */
String.prototype.getByteLength = function()
{
	var result	= 0;     
	var len		= this.length;
	var c;
	for(var i=0; i<len; i++)
	{
		c = escape(this.charAt(i));
		if(c.length == 1)				result ++;
		else if(c.indexOf("%u") != -1)	result += 2;
		else if (c.indexOf("%") != -1)	result += c.length / 3;
	}            
	return result;
}


/**
 * ¹®ÀÚ¿­ÀÇ ¿ÞÂÊ °ø¹éÀ» Á¦°ÅÇÑ ¹®ÀÚ¿­À» ¹ÝÈ¯
 *
 * param	: ¾øÀ½
 *
 * return	: string
 */
String.prototype.ltrim = function()
{
	var result	= this;
	var len		= this.length;
	var index	= 0;
	for(var i=0; i<len; i++)
	{
		if(this.charAt(i) != " ")
		{
			index = i;
			break;
		}
	}

	return this.substring(index, len);
}


/**
 * ¹®ÀÚ¿­ÀÇ ¿À¸¥ÂÊ °ø¹éÀ» Á¦°ÅÇÑ ¹®ÀÚ¿­À» ¹ÝÈ¯
 *
 * param	: ¾øÀ½
 *
 * return	: string
 */
String.prototype.rtrim = function()
{
	var result;
	var len		= this.length;
	var index	= 0;
	for(var i=len-1; i>=0; i--)
	{
		if(this.charAt(i) != " ")
		{
			index = i;
			break;
		}
	}

	result = this.substring(0, index+1);
	if(result == " ") result = "";

	return result;
}


/**
 * ¹®ÀÚ¿­ÀÇ ¾çÂÊ °ø¹éÀ» Á¦°ÅÇÑ ¹®ÀÚ¿­À» ¹ÝÈ¯
 *
 * param	: ¾øÀ½
 *
 * return	: string
 */
String.prototype.trim = function()
{
	return this.ltrim().rtrim();
}


/**
 * ¹®ÀÚ¿­ÀÇ ¿ÞÂÊ¿¡¼­ count ¸¸Å­ÀÇ ¹®ÀÚ¿­À» ¹ÝÈ¯
 *
 * param	: int		count
 *
 * return	: string
 */
String.prototype.left = function(count)
{
	return this.substr(0, count);
}


/**
 * ¹®ÀÚ¿­ÀÇ Áß°£(start)¿¡¼­ count ¸¸Å­ÀÇ ¹®ÀÚ¿­À» ¹ÝÈ¯
 *
 * param	: int		start
			  int		count
 *
 * return	: string
 */
String.prototype.mid = function(start, count)
{
	return this.substr(start, count);
}


/**
 * ¹®ÀÚ¿­ÀÇ ¿À¸¥ÂÊ¿¡¼­ count ¸¸Å­ÀÇ ¹®ÀÚ¿­À» ¹ÝÈ¯
 *
 * param	: int		count
 *
 * return	: string
 */
String.prototype.right = function(count)
{
	return this.substr(this.length-count, count);
}

// === string °´Ã¼ °ü·Ã ³¡ === //






// === array °´Ã¼ °ü·Ã ½ÃÀÛ === //

/**
 * ÇØ´ç ¹è¿­ÀÇ °ªÀ» º¯°æ ÇÑ´Ù.
 *
 * param	: int		index
			  variant	value	
 * return	: boolean	¼º°ø ¿©ºÎ
 */
Array.prototype.set = function(index, value)
{
    if(isNaN(index) || index > this.length || index < 0)
	{
		self.status="[Array.set] Ãß°¡ÇÏ°íÀÚ ÇÏ´Â index´Â ¹üÀ§¸¦ ¹þ¾î³³´Ï´Ù. -" + index;
		return false;
	}
	else
		this[index] = value;

	return true;
}

/**
 * ¹è¿­ÀÇ ¸¶Áö¸·¿¡ ¹è¿­ Ãß°¡
 *
 * param	: variant	value	
 * return	: ¾øÀ½
 */
Array.prototype.add = function(value)
{
	this[this.length] = value;
}

/**
 * ÇØ´ç index´ÙÀ½¿¡ ÇØ´ç°ªÀ» Ãß°¡ÇÑ´Ù.
 *
 * param	: int		index
			  variant	value	
 * return	: boolean	¼º°ø ¿©ºÎ
 */
Array.prototype.insert = function(index, value)
{
    if(isNaN(index) || index > this.length || index < 0)
	{
		self.status="[Array.insert] Ãß°¡ÇÏ°íÀÚ ÇÏ´Â index´Â ¹üÀ§¸¦ ¹þ¾î³³´Ï´Ù. -" + index;
		return false;
	}
	else if(index == this.length)
		this.add(value);
	else
	{

		for(var i=this.length; i>index; i--)
			this[i] = this[i-1];

		this[index] = value;
	}

	return true;
}

/**
 * ÇØ´ç index ¹è¿­À» »èÁ¦ÇÑ´Ù.
 *
 * param	: int		index
 * return	: boolean	¼º°ø¿©ºÎ
 */
Array.prototype.remove = function(index)
{ 
    if(isNaN(index) || index > this.length || index < 0)
	{
		self.status="[Array.remove] Á¦°ÅÇÏ°íÀÚ ÇÏ´Â index´Â ¹üÀ§¸¦ ¹þ¾î³³´Ï´Ù. -" + index;
		return false;
	}
	else if(this.length == 0)
	{
		self.status = "[Array.remove] ´õ ÀÌ»ó Á¦°ÅÇÒ°ÍÀÌ ¾ø½À´Ï´Ù.";
		return false;
	}

    for(var i=0, n=0; i<this.length; i++)
    {  
        if(this[i] != this[index])
            this[n++] = this[i];
    }
    this.length -= 1;

	return true;
}

/**
 * ¹è¿­ÀÇ ¸ðµç ³»¿ëÀ» »èÁ¦ÇÑ´Ù.
 *
 * param	: ¾øÀ½
 * return	: ¾øÀ½
 */
Array.prototype.removeAll = function()
{
	this.length = 0;
}

/**
 * ¹è¿­¾È¿¡ value¿Í Ã³À½À¸·Î ÀÏÄ¡ÇÏ´Â index¸¦ ¹ÝÈ¯ÇÑ´Ù.
 *
 * param	: variant	value
 *			  int		start (½ÃÀÛ index)
 *			  int		end	  (Á¾·á index)
 *
 * return	: int (ÀÏÄ¡ÇÏ´Â °ªÀÌ ¾øÀ»°æ¿ì -1)
 */
Array.prototype.indexOf = function(value, start, end)
{
	if(start == null)	start = 0;
	if(end == null)		end = this.length;
	for(var i=start; i<end; i++)
	{
		if(value == this[i])
		{
			return i;
		}
	}

	return -1;
}

/**
 * ¹è¿­ ³»¿ë ºñ±³
 * - ºñ±³°ª°ú ¹è¿­°ªÀÌ °°À¸¸é 0 ¹ÝÈ¯
 * - ¹è¿­°ªÀÌ Å©¸é 1 ¹ÝÈ¯
 * - ºñ±³°ªÀÌ Å©¸é -1 ¹ÝÈ¯
 *
 * param	: int		index (ºñ±³ÇÒ ¹è¿­ ÀÎµ¦½º)
 *			  variant	value (ºñ±³ÇÒ°ª)
 *
 * return	: int
 */
Array.prototype.compare = function(index, value)
{
	var temp = this[index];
	if(temp == value)
		return 0;
	else if(temp > value)
		return 1;
	else if(temp < value)
		return -1;
	else
		return -1;
}

/**
 * ¹è¿­ º¹Á¦
 *
 * return	: array
 */
Array.prototype.clone = function()
{
	var nLen = this.length;
	var arrTemp = new Array(nLen);
	for(var i=0; i<nLen; i++)
	{
		arrTemp[i] = this[i];
	}

	return arrTemp;
}

// === array °´Ã¼ °ü·Ã ³¡ === //













function errorMsg(msg, classtype, name)
{
	var message = "" + msg + " (" + classtype;
	if(name != null)
		message += " - " + name + ")     ";
	else
		message += ")";
	alert( message );
}

function Point(x, y)
{
	if(typeof x != "number" || typeof y != "number")
	{
		errorMsg("x, y´Â ¼ýÀÚ¸¸ Çã¿ëÇÕ´Ï´Ù.", "Point");
		return;
	}

	this.x = x;
	this.y = y;
}

function Dimension(width, height)
{
	if(typeof width != "number" || typeof height != "number")
	{
		errorMsg("width, height´Â ¼ýÀÚ¸¸ Çã¿ëÇÕ´Ï´Ù.", "Dimension");
		return;
	}

	this.width = width;
	this.height = height;
}

function Rectangle(x, y, width, height)
{
	if(typeof x != "number" || typeof y != "number" ||
		typeof width != "number" || typeof height != "number")
	{
		errorMsg("x, y, width, height´Â ¼ýÀÚ¸¸ Çã¿ëÇÕ´Ï´Ù.", "Rectangle");
		return;
	}
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
}