String.prototype.checkFormat = {
	email : {
		reg : /^[_\da-zA-Z-]+(:?\.[_\da-zA-Z-]+)*@[\da-zA-Z-]+(:?\.[\da-zA-Z-]+)*$/,
		min : 4,
		max : 80
	},
	password : {
		reg : /(?:^[_0-9a-zA-Z-]+$)/,
		min : 4,
		max : 12
	},
	name : {
		min : 4,
		max : 30
	},
	phone : {
		reg : /^0\d{1,2}-\d{3,4}-\d{4}$/
	},
	date : {
		fn : Date.parse
	},
	
	flag : {
		reg : /^0|1$/
	},
	
	two : {
		reg : /^1|2$/
	}
};

String.prototype.check = function(type)
{
	var fm = this.checkFormat[type];
	
	if(fm.reg)
	{
		var result = this.match(fm.reg);
		
		if(!result || result[0] != this)
			return false;
	}
	
	if(fm.fn && !fm.fn(this))
		return false;
	if(typeof fm.max != "undefined" && fm.max < this.getByte())
		return false;
	if(typeof fm.min != "undefined" && this.getByte() < fm.min)
		return false;
	
	return true;
};

String.prototype.getByte = function()
{
   var i;
   var strLen;
   var strByte;
   strLen = this.length;
      
  for(i=0, strByte = 0; i < strLen; i++)
  {  
     if(this.charAt(i) >= ' ' && this.charAt(i) <= '~' )
         strByte++;
     else
         strByte += 2;
  }
  
  return strByte;
};

String.prototype.ltrim = function() 
{
	var trimRe = /^\s*(.*)\s*$/;
	return this.replace(trimRe, "$1");
};
String.prototype.cachePixel = {};
String.prototype.substrPixel = function(length, px, line, isBold)
{
	var line = line || 1, height;
	
	if(this.length * px < length * line)
		return new String(this);
	
	if(!this.div)
	{
		var div = Ext.get(document.createElement("div"));
		div.setStyle("position", "absolute");
		div.setStyle("zIndex", -999);
		div.setStyle("visibility", "hidden");
		div.setStyle("wordBreak", "break-all");
		div.appendTo(document.body);
		String.prototype.div = div;
	}
	
	this.div.setStyle("width", length + "px");
	this.div.setStyle("fontSize", px + "px");
	
	if(isBold)
		this.div.setStyle("fontWeight", "bold");
	else
		this.div.setStyle("fontWeight", "normal");
	
	this.div.setStyle("width", length + "px");
	
	if(this.cachePixel[px])
		height = this.cachePixel[px];
	else
	{
		this.div.dom.innerHTML = "한<br/>한";
		
		height = this.cachePixel[px] = Math.ceil(this.div.getHeight() / 2);
	}
	height *= line;
	
	this.div.dom.innerHTML = this;
	
	if(this.div.getHeight() <= height)
		return new String(this);
	
	for(var i = this.length - 1, returnStr; i >= 0; i--)
	{
		returnStr = this.substr(0, i) + "...";
		
		this.div.dom.innerHTML = returnStr;
		
		if(this.div.getHeight() <= height)
			break;
	}
	
	return returnStr;
};

// trim 함수
String.prototype.trim = function()
{
 return this.replace(/(^\s*)|(\s*$)/gi, "");
};

String.prototype.ellipse = function(maxLength){
    if(this.length > maxLength){
        return this.substr(0, maxLength-3) + '...';
    }
    return this;
};

String.prototype.replaceLink = function ()   
{
    return this.replace(/(http\:\/\/)?((?:(?:\w+)[.])+(?:asia|biz|cc|cn|com|de|eu|in|info|jobs|jp|kr|mobi|mx|name|net|nz|org|travel|tv|tw|uk|us)(?:\/(?:\w*))*)/ig, "<a href='http://$2' target=new>$1$2</a>");
};


String.prototype.firstCharUpper = function()
{
	var ch = this.charAt(0);
	ch = ch.toUpperCase();
	var str = ch + this.substring(1);	
    return str;
};

//whcarrot
String.prototype.entitiesDecode = function()
{
	if(!String.prototype._TEXTAREA)
		String.prototype._TEXTAREA = document.createElement("TEXTAREA");
	
	this._TEXTAREA.innerHTML = this;
	
	return this._TEXTAREA.value;
};

if(Ext.isIE)
{
	String.prototype.entitiesEncode = function()
	{
		if(!String.prototype._TEXTAREA)
			String.prototype._TEXTAREA = document.createElement("TEXTAREA");
		
		this._TEXTAREA.value = this;
		
		return this._TEXTAREA.innerHTML;
	}
}
else
{
	String.prototype.entitiesEncode = function()
	{
		if(!String.prototype._TEXTAREA)
			String.prototype._TEXTAREA = document.createElement("TEXTAREA");
		
		this._TEXTAREA.defaultValue = this;
		
		return this._TEXTAREA.innerHTML;
	}
}

String.prototype.encode = function() {
	return encodeURIComponent(this);
};
String.prototype.decode = function() {
	try
	{
		return decodeURIComponent(this);
	}
	catch(e)
	{
		return "";
	}
};

//MySQL 4.1 이후 password()
String.prototype.password = function()
{
	var sha1_input = function(message_array)
	{
		var len, i;
		len = message_array.length & 0xfffffffc;
		for (i = 0; i < len; i += 4)
		{
			this.message_block.push(((message_array.charCodeAt(i) << 8 | message_array.charCodeAt(i+1)) << 8 | message_array.charCodeAt(i+2)) << 8 | message_array.charCodeAt(i+3));
			
			if (this.message_block.length == 16)
				this.sha1processmessageblock();
		}
		switch (message_array.length & 0x00000003)
		{
			case 0:
				this.message_block.push(0x00000080 << 24);
				break;
			case 1:
				this.message_block.push((message_array.charCodeAt(i) << 8 | 0x00000080) << 16);
				break;
			case 2:
				this.message_block.push(((message_array.charCodeAt(i) << 8 | message_array.charCodeAt(i+1)) << 8 | 0x00000080) << 8);
				break;
			case 3:
				this.message_block.push(((message_array.charCodeAt(i) << 8 | message_array.charCodeAt(i+1)) << 8 | message_array.charCodeAt(i+2)) << 8 | 0x00000080);
				break;
		}
		
		if (this.message_block.length > 14)
		{
			for (i = this.message_block.length; i < 16; i++)
				this.message_block.push(0);
			this.sha1processmessageblock();
		}
		
		for (i = this.message_block.length; i < 14; i++)
			this.message_block.push(0);
		
		this.message_block.push(message_array.length >>> 29);
		this.message_block.push(message_array.length << 3);
		this.sha1processmessageblock();
	};
	
	var sha1processmessageblock = function()
	{
		var t, temp, w, a, b, c, d, e;
		w = new Array(80);
		
		for (t = 0; t < 16; t++)
			w[t] = this.message_block[t];
		
		for (t = 16; t < 80; t++)
			w[t] = circularshift(w[t-3] ^ w[t-8] ^ w[t-14] ^ w[t-16], 1);
		
		a = this.intermediate_hash[0];
		b = this.intermediate_hash[1];
		c = this.intermediate_hash[2];
		d = this.intermediate_hash[3];
		e = this.intermediate_hash[4];
		
		for (t = 0; t < 20; t++)
		{
			temp = circularshift(a, 5) + ((b & c) | (~b & d)) + e + w[t] + 0x5A827999;
			e = d;
			d = c;
			c = circularshift(b, 30);
			b = a;
			a = temp;
		}
		
		for (t = 20; t < 40; t++)
		{
			temp = circularshift(a, 5) + (b ^ c ^ d) + e + w[t] + 0x6ED9EBA1;
			e = d;
			d = c;
			c = circularshift(b, 30);
			b = a;
			a = temp;
		}
		
		for (t = 40; t < 60; t++)
		{
			temp = circularshift(a, 5) + ((b & c) | (b & d) | (c & d)) + e + w[t] + 0x8F1BBCDC;
			e = d;
			d = c;
			c = circularshift(b, 30);
			b = a;
			a = temp;
		}
		
		for (t = 60; t < 80; t++)
		{
			temp = circularshift(a, 5) + (b ^ c ^ d) + e + w[t] + 0xCA62C1D6;
			e = d;
			d = c;
			c = circularshift(b, 30);
			b = a;
			a = temp;
		}
		
		this.intermediate_hash[0] += a;
		this.intermediate_hash[1] += b;
		this.intermediate_hash[2] += c;
		this.intermediate_hash[3] += d;
		this.intermediate_hash[4] += e;
		this.message_block = new Array();
	};
	
	var sha1_result = function()
	{
		var i, message_digest="";
		
		for (i = 0; i < 5; i++)
		{
			message_digest += String.fromCharCode(this.intermediate_hash[i] >>> 24 & 0x000000ff);
			message_digest += String.fromCharCode(this.intermediate_hash[i] >>> 16 & 0x000000ff);
			message_digest += String.fromCharCode(this.intermediate_hash[i] >>> 8 & 0x000000ff);
			message_digest += String.fromCharCode(this.intermediate_hash[i] & 0x000000ff);
		}
		
		return message_digest;
	};
	var circularshift = function(word, bits)
	{
		return (word << bits) | (word >>> (32 - bits));
	};
	var octet2hex = function(str)
	{
		var i, alpha, beta, to="";
		
		for (i = 0; i < str.length; i++)
		{
			alpha = str.charCodeAt(i);
			beta = alpha >>> 4;
			
			if (beta < 10)
				to += String.fromCharCode(48 + beta);
			else
				to += String.fromCharCode(55 + beta);
			
			beta = alpha & 0x0000000f;
			if (beta < 10)
				to += String.fromCharCode(48 + beta);
			else
				to += String.fromCharCode(55 + beta);
		}
		
		return to;
	};
	var sha1_reset = function()
	{
		this.message_block = new Array();
		this.intermediate_hash = new Array(5);
		this.intermediate_hash[0] = 0x67452301;
		this.intermediate_hash[1] = 0xEFCDAB89;
		this.intermediate_hash[2] = 0x98BADCFE;
		this.intermediate_hash[3] = 0x10325476;
		this.intermediate_hash[4] = 0xC3D2E1F0;
		this.sha1_input = sha1_input;
		this.sha1processmessageblock = sha1processmessageblock;
		this.sha1_result = sha1_result;
	};
	
	var sha1_context, hash_stage2, to;
	
	if(this.length == 0)
		return "";
	
	sha1_context = new sha1_reset();
	sha1_context.sha1_input(this);
	to = sha1_context.sha1_result();
	
	sha1_context = new sha1_reset();
	sha1_context.sha1_input(to);
	hash_stage2 = sha1_context.sha1_result();
	to = octet2hex(hash_stage2);
	to = "*" + to;
	
	return to;
};

String.prototype.comma = function()
{
	var split = [];
	for(var i = this.length; i > 0; i -= 3)
		split.push(this.substring(i - 3, i));
	
	return split.reverse().join(",");
};


String.prototype.byteEllipsis = function(maxLength){
	var strByte = 0;
	
	for(var i=0; i < this.length; i++)
	{
		if(this.charAt(i) >= ' ' && this.charAt(i) <= '~' )
			strByte++;
		else
			strByte += 2;
		
		if(strByte >= maxLength)
			return this.substr(0, i - 3) + "...";
	}
	
	return this;
};
