/*--------------------------------------------------------------------------------*\		
*  JavaScript framework, version 2.0		
*		
*  Date : 2006. 08. 15.		
*  Copyright 1998-2007 by Vricks Studio All right reserved.		
*  @author Jeff Yang routine@vricks.com		
*  자주 쓰이는 스트링 관련 prototype관련 정리		
\*--------------------------------------------------------------------------------*/		
		
/*--------------------------------------------------------------------------------*\		
*  String prototype		
\*--------------------------------------------------------------------------------*/		
//-----------------------------------------------------------------------------		
// 문자의 좌, 우 공백 제거		
// @return : String		
//-----------------------------------------------------------------------------		
String.prototype.trim = function() {		
    return this.replace(/(^\s*)|(\s*$)/g, "");		
};

//-----------------------------------------------------------------------------		
// 문자의 좌 공백 제거		
// @return : String		
//-----------------------------------------------------------------------------		
String.prototype.ltrim = function() {		
    return this.replace(/(^\s*)/, "");		
};

//-----------------------------------------------------------------------------		
// 문자의 우 공백 제거		
// @return : String		
//-----------------------------------------------------------------------------		
String.prototype.rtrim = function() {		
    return this.replace(/(\s*$)/, "");    		
};

//-----------------------------------------------------------------------------		
//문자 바꾸기, 사용법 var str = 문자열.replaceAll("a", "1");  
// @return : String		
//-----------------------------------------------------------------------------		
String.prototype.replaceAll = function(str1, str2) {
	var temp_str = "";
	if (this.trim() != "" && str1 != str2) {
		temp_str = this.trim();
		while (temp_str.indexOf(str1) > -1){
			temp_str = temp_str.replace(str1, str2);
		}
	}
	return temp_str;
};

//-----------------------------------------------------------------------------		
// 문자열의 byte 길이 반환		
// @return : int		
//-----------------------------------------------------------------------------		
String.prototype.byte=function() {		
    var cnt = 0;		
    for (var i = 0; i < this.length; i++) {		
	   if (this.charCodeAt(i) > 127)	
		  cnt += 2;
	   else	
		  cnt++;
    }		
    return cnt;		
};

//-----------------------------------------------------------------------------		
// 정수형으로 변환		
// @return : String		
//-----------------------------------------------------------------------------		
String.prototype.int = function() {		
    if(!isNaN(this)) {		
	   return parseInt(this);	
    }		
    else {		
	   return null;    	
    }		
};

//-----------------------------------------------------------------------------		
// 숫자만 가져 오기		
// @return : String		
//-----------------------------------------------------------------------------		
String.prototype.num = function() {		
    return (this.trim().replace(/[^0-9]/g, ""));		
};

//-----------------------------------------------------------------------------		
// 숫자에 3자리마다 , 를 찍어서 반환		
// @return : String		
//-----------------------------------------------------------------------------		
String.prototype.money = function() {		
    var num = this.trim();		
    while((/(-?[0-9]+)([0-9]{3})/).test(num)) {		
	   num = num.replace((/(-?[0-9]+)([0-9]{3})/), "$1,$2");	
    }		
    return num;		
};

//-----------------------------------------------------------------------------		
// 숫자의 자리수(cnt)에 맞도록 반환		
// @return : String		
//-----------------------------------------------------------------------------		
String.prototype.digits = function(cnt) {		
    var digit = "";		
    if (this.length < cnt) {		
	   for(var i = 0; i < cnt - this.length; i++) {	
		  digit += "0";
	   }	
    }		
    return digit + this;		
};

//-----------------------------------------------------------------------------		
// " -> &#34; ' -> &#39;로 바꾸어서 반환		
// @return : String		
//-----------------------------------------------------------------------------		
String.prototype.quota = function() {		
    return this.replace(/"/g, "&#34;").replace(/'/g, "&#39;");		
};

//-----------------------------------------------------------------------------		
// 파일 확장자만 가져오기		
// @return : String		
//-----------------------------------------------------------------------------		
String.prototype.ext = function() {		
    return (this.indexOf(".") < 0) ? "" : this.substring(this.lastIndexOf(".") + 1, this.length);    		
};

//-----------------------------------------------------------------------------		
// 파일 확장자 제외하고 가져오기		
// @return : String		
//-----------------------------------------------------------------------------		
String.prototype.filename = function() {		
    return (this.indexOf(".") < 0) ? "" : this.substring(0, this.lastIndexOf("."));    		
};

//-----------------------------------------------------------------------------		
// URL에서 파라메터 제거한 순수한 url 얻기		
// @return : String		
//-----------------------------------------------------------------------------    		
String.prototype.uri = function() {		
    var arr = this.split("?");		
    arr = arr[0].split("#");		
    return arr[0];    		
};	
		
/*---------------------------------------------------------------------------------*\		
*  각종 체크 함수들		
\*---------------------------------------------------------------------------------*/		
//-----------------------------------------------------------------------------		
// 정규식에 쓰이는 특수문자를 찾아서 이스케이프 한다.		
// @return : String		
//-----------------------------------------------------------------------------		
String.prototype.meta = function() {		
    var str = this;		
    var result = ""		
    for(var i = 0; i < str.length; i++) {		
	   if((/([\$\(\)\*\+\.\[\]\?\\\^\{\}\|]{1})/).test(str.charAt(i))) {	
		  result += str.charAt(i).replace((/([\$\(\)\*\+\.\[\]\?\\\^\{\}\|]{1})/), "\\$1");
	   }	
	   else {	
		  result += str.charAt(i);
	   }	
    }		
    return result;		
};

//-----------------------------------------------------------------------------		
// 정규식에 쓰이는 특수문자를 찾아서 이스케이프 한다.		
// @return : String		
//-----------------------------------------------------------------------------		
String.prototype.remove = function(pattern) {		
    return (pattern == null) ? this : eval("this.replace(/[" + pattern.meta() + "]/g, \"\")");		
};

//-----------------------------------------------------------------------------		
// 최소 최대 길이인지 검증		
// str.isLength(min [,max])		
// @return : boolean		
//-----------------------------------------------------------------------------		
String.prototype.isLength = function() {		
    var min = arguments[0];		
    var max = arguments[1] ? arguments[1] : null;		
    var success = true;		
    if(this.length < min) {		
	   success = false;	
    }		
    if(max && this.length > max) {		
	   success = false;	
    }		
    return success;		
};

//-----------------------------------------------------------------------------		
// 최소 최대 바이트인지 검증		
// str.isByteLength(min [,max])		
// @return : boolean		
//-----------------------------------------------------------------------------		
String.prototype.isByteLength = function() {		
    var min = arguments[0];		
    var max = arguments[1] ? arguments[1] : null;		
    var success = true;		
    if(this.byte() < min) {		
	   success = false;	
    }		
    if(max && this.byte() > max) {		
	   success = false;	
    }		
    return success;		
};

//-----------------------------------------------------------------------------		
// 공백이나 널인지 확인		
// @return : boolean		
//-----------------------------------------------------------------------------		
String.prototype.isBlank = function() {		
    var str = this.trim();		
    for(var i = 0; i < str.length; i++) {		
	   if ((str.charAt(i) != "\t") && (str.charAt(i) != "\n") && (str.charAt(i)!="\r")) {	
		  return false;
	   }	
    }		
    return true;		
};


//-----------------------------------------------------------------------------		
// 문자열 자르기		
// str.cut(str, cnt, [,char])		
// @return : String		
//-----------------------------------------------------------------------------		
String.prototype.cut = function() {		
    var message = this;		
    var maximum = arguments[0];		
    var defaultstr = arguments[1]?arguments[1]:"";		
	var inc = 0;
	var nbytes = 0;
	var msg = "";
	var msglen = message.length;
	var exceed = false;
	var i=0;
	if (message == null)
		message = "";
	
	for (i=0; i<msglen; i++) {
		var ch = message.charAt(i);
		if (escape(ch).length > 4) {
			inc = 2;
		} else if (ch == '\n') {
			if (message.charAt(i-1) != '\r') {
				inc = 1;
			}
		} else if (ch == '<' || ch == '>') {
			inc = 4;
		} else {
			inc = 1;
		}
		if ((nbytes + inc) > maximum) {
			exceed = true;
			break;
		}
		
		nbytes += inc;
		msg += ch;
	}

	if (defaultstr != null && defaultstr.length > 0 && exceed)
		return msg + defaultstr;
	else
		return msg;
};

//-----------------------------------------------------------------------------		
// 숫자로 구성되어 있는지 학인		
// arguments[0] : 허용할 문자셋		
// @return : boolean		
//-----------------------------------------------------------------------------		
String.prototype.isNum = function() {		
    return (/^[0-9]+$/).test(this.remove(arguments[0])) ? true : false;		
};

//-----------------------------------------------------------------------------		
// 영어만 허용 - arguments[0] : 추가 허용할 문자들		
// @return : boolean		
//-----------------------------------------------------------------------------		
String.prototype.isEng = function() {		
    return (/^[a-zA-Z]+$/).test(this.remove(arguments[0])) ? true : false;		
};

//-----------------------------------------------------------------------------		
// 숫자와 영어만 허용 - arguments[0] : 추가 허용할 문자들		
// @return : boolean		
//-----------------------------------------------------------------------------		
String.prototype.isEngNum = function() {		
    return (/^[0-9a-zA-Z]+$/).test(this.remove(arguments[0])) ? true : false;		
};

//-----------------------------------------------------------------------------		
// 숫자와 영어만 허용 - arguments[0] : 추가 허용할 문자들		
// @return : boolean		
//-----------------------------------------------------------------------------		
String.prototype.isNumEng = function() {		
    return this.isEngNum(arguments[0]);		
};

//-----------------------------------------------------------------------------		
// 이미지 파일 체크		
// @return : String		
//-----------------------------------------------------------------------------		
String.prototype.isPhoto = function() {		
	var strExt = this.trim().ext();
	var arrExtList = ["gif", "jpg", "jpeg", "png", "bmp"];
	var nLen = arrExtList.length;
	var nLoopCnt = 0;
	
	for (nLoopCnt=0; nLoopCnt<nLen; nLoopCnt++)
	{
		if (arrExtList[nLoopCnt].toLowerCase() == strExt.toLowerCase())
			return true;
	}
    return false;    		
};

//-----------------------------------------------------------------------------		
// 아이디 체크 영어와 숫자만 체크 첫글자는 영어로 시작 - arguments[0] : 추가 허용할 문자들		
// @return : boolean		
//-----------------------------------------------------------------------------		
String.prototype.isUserid = function() {		
    return (/^[a-zA-z]{1}[0-9a-zA-Z]+$/).test(this.remove(arguments[0])) ? true : false;		
};

//-----------------------------------------------------------------------------		
// 한글 체크 - arguments[0] : 추가 허용할 문자들		
// @return : boolean		
//-----------------------------------------------------------------------------		
String.prototype.isKor = function() {		
    return (/^[가-힣]+$/).test(this.remove(arguments[0])) ? true : false;		
};

//-----------------------------------------------------------------------------		
// 숫자와 영어와 한글만 - arguments[0] : 추가 허용할 문자들		
// @return : boolean		
//-----------------------------------------------------------------------------		
String.prototype.isValidText = function() {		
    return (/^[0-9a-zA-Z가-힣ㄱ-ㅎ]+$/).test(this.remove(arguments[0])) ? true : false;		
};

//-----------------------------------------------------------------------------		
// 이메일의 유효성을 체크		
// @return : boolean		
//-----------------------------------------------------------------------------		
String.prototype.isEmail = function() {		
    return (/\w+([-+.]\w+)*@\w+([-.]\w+)*\.[a-zA-Z]{2,4}$/).test(this.trim());		
};

//-----------------------------------------------------------------------------		
// 숫자와 콤마를 찍을자리수를 매개변수로 받음
// @return : ,추가 문자열		
//-----------------------------------------------------------------------------		
String.prototype.toComma = function() {		
	var str=new Array(); //콤마스트링을 조합할 배열

	arguments[0] = arguments[0].replace(",","");
	arguments[0]=String(arguments[0]); //숫자를 스트링으로 변환
	for(var i=1;i<=arguments[0].length;i++){ //숫자의 길이만큼 반복
		if(i%arguments[1]) 
			str[arguments[0].length-i]=arguments[0].charAt(arguments[0].length-i); //자리수가 아니면 숫자만삽입
		else  
			str[arguments[0].length-i]=','+arguments[0].charAt(arguments[0].length-i); //자리수 이면 콤마까지 삽입
	}
	return str.join('').replace(/^,/,''); //스트링을 조합하여 반환
};

//-----------------------------------------------------------------------------		
// 콤마 없애기
// @return : 콤마 제거 문자열		
//-----------------------------------------------------------------------------		
String.prototype.removeComma = function() {		
	return arguments[0].value.replace(/,/gi,"");
};

//-----------------------------------------------------------------------------		
// 10진수를 16진수로
// @return : 변환된 16진수 값
//-----------------------------------------------------------------------------		
String.prototype.dec2Hex = function() {		
	var x_Hex = new Array();
	var x_serial = 0;
	var x_over16 = arguments[0];
	var x_tempNum = 0;
	while(arguments[0] > 15) {
		var x_h = arguments[0] % 16; //나머지
		arguments[0] = parseInt(arguments[0]/16); //몫
		x_Hex[x_serial++] = (x_h > 9 ? String.fromCharCode(x_h + 55) : x_h); //16진수코드변환
	}
	//마지막은 몫의 값을 가짐
	x_Hex[x_serial++] = (arguments[0] > 9 ? String.fromCharCode(arguments[0] + 55) : arguments[0]); //16진수코드변환
	//몫,나머지,나머지,.....
	var retValue = "";
	for(var i=x_Hex.length ; i>0 ;i--) {
		retValue += x_Hex[i-1];
	}
	return retValue;
};

function getRandomNum(m1, m2){
   return m1+parseInt(Math.random()*(m2+1-m1));
};