/*----------- ȯ  Լ ----------------------------*/
//Zero  ڷ ȯ
function ZeroToStr(src, str)
{
	if(src == 0)
	{
		return str;
	}else{
		return src;
    }	
}

//Null  ڷ ȯ
function NullToStr(src, str)
{
	if(src == null || value == "null")
	{
		return str;
	}else{
		return src;
	}
}

//ڸ Ǽ ȯ
function StrToFloat(str)
{
	try{
		var fval = parseFloat(str);
		if(isNaN(fval))
		{
			return 0;
		}else{
			return fval;
		}
	}catch(e){
		return 0;
	}	
}

//ڸ Ǽ ȯ
function StrToMoney(str)
{
	try{
		return StrToFloat(str);
	}catch(e){
		return 0;
	}	
}

//ڸ 1000 ޸ ǥ  ȯ
function commaSplit(srcNumber, bEmp)
{  
	var value = '';
    var txtNumber = '' + srcNumber;
    txtNumber = EditFilterNum(txtNumber); 
    if (isNaN(txtNumber) || txtNumber == "") 
    {
		return "";
    }else {
         var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
         var arrNumber = txtNumber.split('.');
         arrNumber[0] += '.';
         do {
              arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');
         } while (rxSplit.test(arrNumber[0]));
        if (arrNumber.length > 1) 
        {
        	value = arrNumber.join('');
        }else {
            value = arrNumber[0].split('.')[0];
        }
   }
   if(bEmp)
   {
   		if(value == 0)
   		{
   			value = '';
   		}
   }
   return value;
}

//Է° ˻ 0̳ ڷ ȯ
function ValueChekEmpty(strSrc, bZero)
{
	if(isNaN(strSrc))
	{
		if(bZero)	
		{
			return 0;
		}else{
			return '';
		}
	}
	if(strSrc == '0')
	{
		if(bZero)	
		{
			return 0;
		}else{
			return '';
		}
	}	
}

// ڸ
function trim(str) 
{
      var count = str.length;
      var len = count;                
      var st = 0;
                
      while ((st < len) && (str.charAt(st) <= ' '))
      {
         st++;
      }
      while ((st < len) && (str.charAt(len - 1) <= ' '))
     {
         len--;
      }                
      return ((st > 0) || (len < count)) ? str.substring(st, len) : str ;   
}

//ڿ  ڿ    ڸ 
function SplitSubstring(strStartChar, strEndChar, strSource)
{
	if(strSource != null && strSource != "")
	{
		var nSpos = strSource.indexOf(strStartChar);
		var nEpos = strSource.indexOf(strEndChar);
		if(nSpos >= 0 && nEpos > 0)
		{
			var strValue = strSource.substring(nSpos + 1, nEpos);
			return strValue;
		}else{
			return '';
		}
	}else{
		return '';
	}				
}

//ڿ ȯ
function StringReplace(strValue, strOld, strNew, bReplaceAll)
{
	var look4 = strOld;
	var newLookFor = "";
	var origText = strValue
	var replaceWith = strNew;
	var delim = (bReplaceAll) ? "/g" : "/gi";
	var resultIs = "";

   	for (var i=0;i<look4.length;i++) 
   	{
    	 var speChar = specialChar(look4.charAt(i));
         var charToReplace = look4.charAt(i);
         
         if(speChar)
         {
         	newLookFor += "" + charToReplace;
         }else{
         	newLookFor += charToReplace;
         }
   	}

	var myString = new String(newLookFor);
	var mySeparator = ',';
	var arrayName = myString.split(mySeparator);

	for (var i=0;i<arrayName.length;i++) 
	{
   		if(i==0)
   		{
  			resultIs = origText.replace(eval("/"+arrayName[i]+delim), replaceWith);
  		}
  		if(i!==0)
  		{
  			resultIs = resultIs.replace(eval("/"+arrayName[i]+delim), replaceWith);
  		}
  	}
	return  resultIs;
}
/*-----------------------------------------------------------*/

/*-------------- ˻  Լ --------------------------*/
//Էõ  ڸ ȯ
function EditFilterNum(str)
{
       re = /^\$|,/g;
       return str.replace(re, "");
}

//NUMONLY ڸ, NUMBER   -, ALPHA ĺ, ALPHANUM ĺ  , CODE ڵ  
function ValidateCheck(strSrc, strCheckType) 
{
	var valid = '';
	
	if(strCheckType == 'NUMONLY')
	{
		valid = '1234567890';
	}else if(strCheckType == 'NUMBER'){		
		valid = '1234567890-.';
	}else if(strCheckType == 'ALPHA'){		
		valid = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';	
	}else if(strCheckType == 'ALPHANUM'){	
		valid = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';	
	}else if(strCheckType == 'CODE'){
		valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()-_+=|\\{}[]'?/.,";
	}else if(strCheckType = "NONCHAR")
	{
		valid = '~`!@#$%^&*()-_=+\|<>?,./;:"';
	}else if(strCheckType = "MAIL")
	{
		valid = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@.';
	}
	
	var ok = "yes";
	var temp;
	var nLen = 0;
	for (var i=0; i< strSrc.length; i++) 
	{
		temp = "" + strSrc.substring(i, i+1);
		if (valid.indexOf(temp) == -1)
		{ 
			ok = 'no';
			break;
		}
	}
	if(ok == 'no')
	{
		return false;
	}else{
		return true;
	}  	
}

// ̸ ּ üũ
function MailCheck(email_text)
{  
	var email = email_text;
	var email_1 = "";
	var email_2 = "";
	var check_point = 0;

	if(email.indexOf("@") < 0 ) 
	{	
		return false;
	}
	for (var j = 0 ; j < email.length; j++)
	{
	   if ( email.substring(j, j + 1) != "@"  && check_point == 0 ) {
			email_1 = email_1 + email.substring(j, j + 1)
	   } else if ( email.substring(j, j + 1) == "@" )  {
			check_point = check_point + 1;
	   } else {
	   		email_2 = email_2 + email.substring(j, j + 1);	
	   }
	}
	if (check_point > 1 ) 
	{
		return false;
	}	
	if (email.indexOf(".") < 0 ) 
	{	
		return false;
	}
	if (email.indexOf(" ") >= 0 ) 
	{	
		return false;
	}	
	return ValidateCheck(email, "MAIL");
}
/*------------------------------------------------------------*/

/*-------------   Լ -------------------------------*/
//ݿø ó
function Round(Num, Position)
{
	return parseFloat(Num.toFixed(Position));
}
/*------------------------------------------------------------*/

/*------------- ¥ Լ --------------------------------*/
function add_date(i) // ż尡  Լ 
{
	 var currentDate; //   
	 currentDate = this.getDate() + i*1;  //  ¥ () ¥ 
	 this.setDate(currentDate);  //  ¥ ٽ 
}

Date.prototype.addDate = add_date; // Date ü ޼ 	

//¥ 
function FormatDate(date, format) 
{
	
 	var today = date;
 	var yyyy, mm, dd;
 	var Today; 
 	format = format.toUpperCase();
 	
 	yyyy = today.getYear();
 	mm = make2Length(today.getMonth() + 1);	
 	dd = make2Length(today.getDate()); 
 
 	var nIdx =  format.indexOf("YYYY");
 	if(nIdx >= 0)
 	{
 		format = format.replace(/YYYY/g, yyyy.toString());
 	}
 	var nIdx =  format.indexOf("YY");
 	if(nIdx >= 0)
 	{
 		var strYear = yyyy.toString();
 		strYear = strYear.substring(2,5);
 		format = format.replace(/YY/g, strYear);
 	}
 	format = format.replace(/MM/g, mm.toString());
 	format = format.replace(/DD/g, dd.toString());

 	Today = format;  	
 	return Today;
}

function FormatDate(strDate, format, strSplit) 
{
	var aTemp = new Array();
	var aDate = new Array();
	
	aTemp = strDate.split(" ");
	format = format.toUpperCase();
	
	try{
		if(aTemp.length > 0)
		{
			aDate = aTemp[0].split(strSplit);
		}else{
			aDate = strDate.split(strSplit);
		}
		var yyyy = aDate[0];
		var mm = aDate[1];
		var dd = aDate[2];
		
		var nIdx =  format.indexOf("YYYY");
	 	if(nIdx >= 0)
	 	{
	 		format = format.replace(/YYYY/g, yyyy.toString());
	 	}
 		var nIdx =  format.indexOf("YY");
	 	if(nIdx >= 0)
	 	{
	 		var strYear = yyyy.toString();
	 		strYear = strYear.substring(2,5);
	 		format = format.replace(/YY/g, strYear);
	 	}
 		format = format.replace(/MM/g, mm.toString());
 		format = format.replace(/DD/g, dd.toString());
 		 	
 		return format;
	}catch(e){
		alert("[" + strDate + "] ¥ .");
		return strDate;
	}
}


	
// ڸ  - pansory
function make2Length(val) {
 if(val < 10) {
  val = "0" + val;
 }
 return val;
}

//ڸ ¥ ȯ
function StrToDate(strDate, strSplit)
{
	var aTemp = new Array();
	var aDate = new Array();
	
	aTemp = strDate.split(" ");
	try{
		if(aTemp.length > 0)
		{
			aDate = aTemp[0].split(strSplit);
		}else{
			aDate = strDate.split(strSplit);
		}
		var date = new Date(aDate[0], parseInt(aDate[1]) - 1, aDate[2]);
		return date;
	}catch(e){
		alert("[" + strDate + "] ¥ .");
		return null;
	}	
}

//ڸ ¥ð 
function StrToDateTime(strDate, strSplit)
{
	var aTemp = new Array();
	var aDate = new Array();
	var aTime = new Array();
	
	aTemp = strDate.split(" ");
	
	try{
		aDate = aTemp[0].split(strSplit);
		aTime = aTemp[1].split(":");
		var date = null;
		if(aTime.length > 1)
		{
			date = new Date(aDate[0], parseInt(aDate[1]) - 1, aDate[2], aTime[0], aTime[1]);
		}else if(aTime.length > 2)
		{
			date = new Date(aDate[0], parseInt(aDate[1]) - 1, aDate[2], aTime[0], aTime[1], aTime[2]);
		}
		return date;
	}catch(e){
		alert("[" + strDate + "] ¥ .");
		return null;
	}	
}


//ó¥ 
function TodayDate(split)
{
	toDate = new Date();
	year = toDate.getYear();
 	month = toDate.getMonth()+1;
 	day = toDate.getDate();
 	if(parseInt(month) < 10)
 	{
 		month = '0' + month;
 	}

 	if(parseInt(day) < 10)
 	{
 		day = '0' + day;
 	}
 	 	
 	if(split == null || split == "")
 	{
 		return year + "-" + month + "-" + day;
 	}else{
 		return year + split + month + split + day;
 	}
}

/*
// ϱ
function GetWeekday(x_nMonth, x_nDay, x_nYear) 
{

    if(x_nMonth >= 3)
    {        
    	x_nMonth -= 2;
    } else {
    	x_nMonth += 10;
    }

    if( (x_nMonth == 11) || (x_nMonth == 12) )
    {
    	x_nYear--;
    }

    var nCentNum = parseInt(x_nYear / 100);
    var nDYearNum = x_nYear % 100;

    var g = parseInt(2.6 * x_nMonth - .2);

    g +=  parseInt(x_nDay + nDYearNum);
    g += nDYearNum / 4;        
    g = parseInt(g);
    g += parseInt(nCentNum / 4);
    g -= parseInt(2 * nCentNum);
    g %= 7;
    
    if(x_nYear >= 1700 && x_nYear <= 1751) 
    {
    	g -= 3;
    }else {
    	if(x_nYear <= 1699) 
    	{
        	g -= 4;
        }
    }
    
    if(g < 0)
    {
    	g += 7;
    }    
    return g;
}

function leapYear(year) 
{
	if (year % 4 == 0) 
	{
		return true;
	}else{ 
		return false;
	}
}

// ¥ 
function MonthToLastDay(year, month)
{
  	var last  = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31 , 30 ,31);
  	if ((year%400==0 ) ||(year%4==0 && year%100!=0 ))
  	{
   		last[1]=29;
  	}
  	return last[month];
}

//¥ ´ ּ ϱ
function DateToWeekCount(dtDate)
{
	var arDay = new Array(12);
	arDay[0] = 31; 
	arDay[1] = (leapYear(dtDate.getYear())) ? 29 : 28; 
	arDay[2] = 31; 
	arDay[3] = 30;
	arDay[4] = 31;
	arDay[5] = 30; 
	arDay[6] = 31; 
	arDay[7] = 31;
	arDay[8] = 30;
	arDay[9] = 31;
	arDay[10] = 30;
	arDay[11] = 31;

	var firstDayInstance = new Date(dtDate.getYear(), dtDate.getMonth(), 1);
	var firstDay = firstDayInstance.getDay();
	firstDay = firstDay + 1;
	var lastDate = arDay[dtDate.getMonth()];
	var week = Math.ceil((lastDate + firstDay) / 7);
	
	return week;
}

//¥ ´  ϱ
function DateToWeekNum(dtDate)
{
	var arDay = new Array(12);
	arDay[0] = 31; 
	arDay[1] = (leapYear(dtDate.getYear())) ? 29 : 28; 
	arDay[2] = 31; 
	arDay[3] = 30;
	arDay[4] = 31;
	arDay[5] = 30; 
	arDay[6] = 31; 
	arDay[7] = 31;
	arDay[8] = 30;
	arDay[9] = 31;
	arDay[10] = 30;
	arDay[11] = 31;

	var firstDayInstance = new Date(dtDate.getYear(), dtDate.getMonth(), 1);
	var firstDay = firstDayInstance.getDay();
	firstDay = firstDay;
	var nDay = dtDate.getDate();
	return Math.ceil((nDay + firstDay) / 7);
}*/

function HanCharVal(src)
{
  var a = src;

  hanTable=new Array();
  hanTable[0]=''; // 19 ʼ
  hanTable[1]='¤äĤŤƤǤȤɤʤˤ̤ͤΤϤФѤҤ'; //21 ߼
  hanTable[2]=' '; //28 

  str="";

	for(i=0;i<a.length;i++)
	{
		b=a.charCodeAt(i);
		hcode=b-0xAC00;
		
		cho=new Array();
		cho[0]=parseInt(hcode / 588); //ʼ
		hcode2=hcode % 588;
		
		cho[1]=parseInt(hcode2 / 28); //߼
		cho[2]=hcode2 % 28; // ,,, 
		
		m=new Array();
	
		//ʼ 
		m[0]=Math.floor((b-0xAC00)/(21*28));       
		//߼
		m[1]=Math.floor(((b-0xAC00)%(21*28))/28); 
		//
		m[2]=(b-0xAC00)%28;
		
		
		mun=new Array();
		mun[0]=hanTable[0].charAt(cho[0]);
		mun[1]=hanTable[1].charAt(cho[1]); //
		mun[2]=hanTable[2].charAt(cho[2]); //0 
		
		hap=String.fromCharCode(0xAC00+(cho[0]*21*28)+(cho[1]*28)+cho[2]);   
		str	+=	mun	+	"\n";
	}
	return str;
}

function NameFirstChar(src)
{
	var tmp = "";
	tmp = src;
	for(var i = 0; i < tmp.length; i++)
	{
		var str = tmp.charAt(i);
		if(ValidateCheck(str, "NONCHAR"))
		{
			
		}else{
			if(ValidateCheck(str, "NUMONLY"))
			{
				return "123"
			}else if(ValidateCheck(str, "ALPHA"))
			{
				return "ABC"
			}else{
				return HanCharVal(str).substring(0,1);
			}
		}
	}
	return "123";
}


function FileExtName(fname)
{
    var s=fname.lastIndexOf("\\");
    var m=fname.lastIndexOf(".");
    var e=fname.length;
    var extname=fname.substring(m+1,e);
    return extname;
}

function FileName(fname)
{
    var s=fname.lastIndexOf("\\");
    var m=fname.lastIndexOf(".");
    var e=fname.length;
    var filename=fname.substring(s+1,m);
    return filename;
}

function decodeURL(str){

    var s0, i, j, s, ss, u, n, f;

    s0 = "";                // decoded str

    for (i = 0; i < str.length; i++){   // scan the source str

        s = str.charAt(i);

        if (s == "+"){s0 += " ";}       // "+" should be changed to SP

        else {

            if (s != "%"){s0 += s;}     // add an unescaped char

            else{               // escape sequence decoding

                u = 0;          // unicode of the character

                f = 1;          // escape flag, zero means end of this sequence

                while (true) {

                    ss = "";        // local str to parse as int

                        for (j = 0; j < 2; j++ ) {  // get two maximum hex characters for parse

                            sss = str.charAt(++i);

                            if (((sss >= "0") && (sss <= "9")) || ((sss >= "a") && (sss <= "f"))  || ((sss >= "A") && (sss <= "F"))) {

                                ss += sss;      // if hex, add the hex character

                            } else {--i; break;}    // not a hex char., exit the loop

                        }

                    n = parseInt(ss, 16);           // parse the hex str as byte

                    if (n <= 0x7f){u = n; f = 1;}   // single byte format

                    if ((n >= 0xc0) && (n <= 0xdf)){u = n & 0x1f; f = 2;}   // double byte format

                    if ((n >= 0xe0) && (n <= 0xef)){u = n & 0x0f; f = 3;}   // triple byte format

                    if ((n >= 0xf0) && (n <= 0xf7)){u = n & 0x07; f = 4;}   // quaternary byte format (extended)

                    if ((n >= 0x80) && (n <= 0xbf)){u = (u << 6) + (n & 0x3f); --f;}         // not a first, shift and add 6 lower bits

                    if (f <= 1){break;}         // end of the utf byte sequence

                    if (str.charAt(i + 1) == "%"){ i++ ;}                   // test for the next shift byte

                    else {break;}                   // abnormal, format error

                }

            s0 += String.fromCharCode(u);           // add the escaped character

            }

        }

    }

    return s0;

}

