/***********************************************
	ÀÛ¼ºÀÚ : À±½ÂÈ¯ (2003.9.17)
	ÆÄÀÏ : valueCheck.js
	¼³¸í : »ç¿ëÀÚ Æû ÀÔ·Â°ª °Ë»ç °ü·Ã ÇÔ¼ö
***********************************************/

/*
	»ç¿ë¹ý :
	
	ÀÔ·ÂÅÂ±×¿¡ »ç¿ëÇÒ¼ö ÀÖ´Â ¼Ó¼º
	
	notnull : ÀÔ·Â°ªÀÌ ¾ø´Â °ÍÀ» Çã¿ëÇÏÁö ¾ÊÀ½	
	onlynum : ¼ýÀÚ¸¸ Çã¿ë
	onlyemail : ¿Ã¹Ù¸¥ ÀÌ¸ÞÀÏÀÎÁö °Ë»ç

	onlyhan : ÇÑ±Û¸¸ ÀÔ·Â °¡´É
	denyhan : ÇÑ±Û ÀÔ·Â ºÒ°¡
	errname : ¿¡·¯³µÀ»¶§ ÇØ´ç ÆûÀ» ¼³¸íÇØÁÖ´Â ´Ü¾î
	radioNotNull : ¶óµð¿À ¹öÆ° Å¬¸¯ °Ë»ç
	checkNotNull=n : Ã¼Å©¹Ú½º¿¡ n°³ÀÇ °ªÀÌ ¼±ÅÃ µÇ¾ú´ÂÁö °Ë»ç
		if (n == 0) °¹¼ö Á¦ÇÑ ¾øÀ½
	juminCheck : ÁÖ¹Îµî·Ï¹øÈ£ Ã¼Å©, ÇÊµå¸íÀ» ¹Ýµå½Ã jumin1, jumin2 ·Î ÁöÁ¤ÇØ Áà¾ß ÇÑ´Ù.
	minlength=n : ÃÖ¼Ò ±ÛÀÚ¼ö °Ë»ç

	** function **
	boolean valueCheck(form) : ¸ðµç °Ë»ç ¼öÇà
	boolean isEmpty(string) : ºó¹®ÀÚ¿­ÀÎÁö °Ë»ç, ºó¹®ÀÚ¿­ÀÌ¸é true ¾Æ´Ï¸é false
	boolean notnull_check(object element) : element.value °¡ nullÀÌ ¾Æ´Ï¸é true, nullÀÌ¸é false
	boolean number_check(object element) : element.value °¡ ¸ðµÎ ¼ýÀÚ¸é true, ¾Æ´Ï¸é false
	boolean onlyemail_check(object element) : element.value °¡ ÀÌ¸ÞÀÏ Çü½Ä¿¡ ¸ÂÀ¸¸é true, ¾Æ´Ï¸é false
	boolean onlyhan_check (object element) : element.value °¡ ¸ðµÎ ÇÑ±ÛÀÌ¸é true, ¾Æ´Ï¸é false
	boolean denyhan_check (object element) : element.value ¿¡ ÇÑ±ÛÀÌ Æ÷ÇÔµÇ¾î ÀÖÀ¸¸é false ¾Æ´Ï¸é true
	boolean chkJuminNum(object form) : ÁÖ¹Îµî·Ï¹øÈ£ °Ë»ç ¸ÂÀ¸¸é true ¾Æ´Ï¸é false
	boolean checkMinLength(object element) : element.value ÀÇ ±ÛÀÚ¼ö°¡ minlength º¸´Ù ÀÛÀ¸¸é false
*/

// Ä¿¼­ ÀÚµ¿ ÀÌµ¿
function f_move_next(o_current, o_next)
{
	if(o_current.value.length == o_current.maxLength)
	{
		o_next.select()
		o_next.focus()
	}
}


// ºó ¹®ÀÚ¿­ÀÎÁö °Ë»ç
function isEmpty(str)
{
	for (var i = 0; i < str.length; i++)
	{
		if (str.substring(i, i+1) != " ")
			return false;
	}
	return true;
}

// ¹®ÀÚ¿­ ±æÀÌ (ÇÑ±ÛÀ» 2ÀÇ ±æÀÌ¸¦ °®´Â °ÍÀ¸·Î Àû¿ë)
function h_stringlength(str)
{
	char_cnt = 0;

	for(var i = 0; i < str.length; i++)
	{
		var chr = str.substr(i,1);
		chr = escape(chr);
		key_eg = chr.charAt(1);

		switch(key_eg)
		{
			case "u":
				key_num = chr.substr(2,(chr.length - 1));
				/*
					if((key_num < "AC00") || (key_num > "D7A3"))
					    return -1;
					else
				*/
				char_cnt += 2;
				break;
			case "B":
				char_cnt += 2;
				break;
			default:
				char_cnt += 1;
		}
	}

	return char_cnt;
}

// °´Ã¼¿¡ Æ÷Ä¿½º ÁÖ±â
// text, password, textarea, file Çü½Ä ÀÔ·Â»óÀÚ¿¡ notnull ¼Ó¼ºÀÌ ÀÖ´Â °æ¿ì
// °ªÀÌ ºñ¾îÀÖ´ÂÁö °Ë»ç
// ºó°ªÀÌ ¾øÀ» °æ¿ì true, ºó°ªÀÌ ÀÖÀ» °æ¿ì false ¸¦ ¸®ÅÏ
// notnull
function notnull_check(what)
{
	if (isEmpty(what.value))
	{
		alert(what.errname + "À»(¸¦) ÀÔ·ÂÇØ ÁÖ¼¼¿ä");
		what.focus();
		return false;
	}

	return true;
}

// ÃÖ¼Ò ±ÛÀÚ¼ö °Ë»ç
function checkMinLength(what)
{
	if ((what.value).length < what.minlength) {
		alert(what.minlength + " ÀÌ»ó ÀÔ·ÂÇÏ¼Å¾ß ÇÕ´Ï´Ù.");
		what.focus();
		return false;
	}
	return true;
}

// ¼ýÀÚÀÎÁö °Ë»ç
function number_check(what)
{
	if (what.onlynum != null)
	{
		if (isNaN(what.value))
		{
			alert (what.errname + "Àº(´Â) " + "¼ýÀÚ¸¸ ÀÔ·ÂÇÒ ¼ö ÀÖ½À´Ï´Ù");
			what.focus();
			return false;
		}
	}

	return true;
}

// text Çü½Ä ÀÔ·Â»óÀÚ¿¡ onlyemail ¼Ó¼ºÀÌ ÀÖ´Â °æ¿ì
// ¿Ã¹Ù¸¥ ÀÌ¸ÞÀÏÀÎÁö °Ë»ç
// onlyemail
function onlyemail_check (what) 
{
	var emailexp ;

	emailexp = new RegExp ("^[A-Za-z0-9-_\\.]{2,}@[A-Za-z0-9-_\\.]{2,}\\.[A-Za-z0-9-_]{2,}$") ;

	if ( what.onlyemail != null ) 
	{
		if ( what.type == "text" ) 
		{
			if ( what.value == "" )
				return true ;

			// ÇÑ±ÛÀÌ Æ÷ÇÔµÅ ÀÖ°Å³ª ÀÌ¸ÞÀÏ Çü½ÄÀÌ ¾Æ´Ñ °æ¿ì
			if ( (h_stringlength(what.value) != what.value.length)
				|| !emailexp.test (what.value) ) {
				alert (what.errname + "ÀÌ ¿Ã¹Ù¸£Áö ¾Ê½À´Ï´Ù") ;
				what.focus();
				return false ;
			}
		}
	}

	return true ;
}

// text, password, textarea Çü½ÄÀÇ °ª¿¡
// ÇÑ±Û¸¸ ÀÖ´ÂÁö °Ë»ç
// onlyhan
function onlyhan_check (what)
{
	if (what.onlyhan != null)
	{
		if (h_stringlength(what.value) != what.value.length*2) // ÇÑ±Û¸¸ Æ÷ÇÔµÇ¾î ÀÖ´ÂÁö °Ë»ç
		{
			alert (what.errname + "Àº(´Â) " +
				  "ÇÑ±Û¸¸ ÀÔ·ÂÇÒ ¼ö ÀÖ½À´Ï´Ù");
			what.focus();
			return false;
		}
	}

	return true;
}

// text, password, textarea Çü½ÄÀÇ °ª¿¡
// ¿µ¾îÀÌ¿ÜÀÇ ¹®ÀÚ°¡ ÀÖ´ÂÁö °Ë»ç
// denyhan
function denyhan_check (what) 
{
	if (what.denyhan != null)
	{
		if (h_stringlength(what.value) != what.value.length) // ÇÑ±ÛÀÌ Æ÷ÇÔµÇ¾î ÀÖ´ÂÁö °Ë»ç
		{
			alert (what.errname + "Àº(´Â) " +
				  "ÇÑ±ÛÀº ÀÔ·ÂÇÒ ¼ö ¾ø½À´Ï´Ù");
			what.focus();
			return false;
		}
	}

	return true;
}

// ÁÖ¹Îµî·Ï¹øÈ£ °Ë»ç
function chkJuminNum(f) 
{
	var jumin1 = f.jumin1.value;
	var jumin2 = f.jumin2.value;

	var sum;
	sum = 0;

	sum += jumin1.charAt(0)*2;
	sum += jumin1.charAt(1)*3;
	sum += jumin1.charAt(2)*4;
	sum += jumin1.charAt(3)*5;
	sum += jumin1.charAt(4)*6;
	sum += jumin1.charAt(5)*7;

	sum += jumin2.charAt(0)*8;
	sum += jumin2.charAt(1)*9;
	sum += jumin2.charAt(2)*2;
	sum += jumin2.charAt(3)*3;
	sum += jumin2.charAt(4)*4;
	sum += jumin2.charAt(5)*5;

	check = (11 - sum%11)%10;

	if (jumin2.charAt(6) != check || (jumin1.length != 6 && jumin2.length != 7))
	{
		alert("Àß¸øµÈ ÁÖ¹Îµî·Ï ¹øÈ£ ÀÔ´Ï´Ù.");
		f.jumin1.focus();
		return false;
	}

	if(!(jumin2.charAt(0) == '1' || jumin2.charAt(0)  == '2' || jumin2.charAt(0) == '3' || jumin2.charAt(0) == '4')) 
	{
		alert("Àß¸øµÈ ÁÖ¹Îµî·Ï ¹øÈ£ ÀÔ´Ï´Ù.");
		f.jumin1.focus();
		return false;
	}

	return true;
}

// ¸ðµç °Ë»ç ¼öÇà
function valueCheck(f)
{
	var what, i;
	var rtValue = true;

	for (i = 0; i < f.length; i++)
	{
		what = f.elements[i];

//		alert(what.type);
		if (what.checkNotNull != null && rtValue)	// Ã¼Å©¹Ú½º ÀÌ¸é
		{
			var errname = what.errname;
			var checkName = what.name;
			var initCheckedCnt = what.checkNotNull;
			var defaultCheck = what.defaultCheck;
			var checkedCnt = 0;
			var checkValue = false;

			while (what.name == checkName)
			{
				if (what.checked)
				{
					checkedCnt = checkedCnt + 1;
					checkValue = true;

					// n °³¸¦ ¼±ÅÃÇØ¾ß ÇÏ´Âµ¥ '¾øÀ½' ¼±ÅÃÇÒ °æ¿ì
					if (what.value == defaultCheck)
					{
						checkedCnt = initCheckedCnt;
					}
				}

				what = f.elements[++i];
			}

			if (!checkValue)
			{
				alert (errname + "À»(¸¦) " + "¼±ÅÃÇØ ÁÖ¼¼¿ä.");
				return false;
			}

			if (initCheckedCnt > 0 && initCheckedCnt != checkedCnt)
			{
				alert (errname + "À»(¸¦) " + initCheckedCnt + "°³ ¼±ÅÃÇØ ÁÖ¼¼¿ä.");
				return false;
			}
			
			i--;
			continue;
		}


		if (what.radioNotNull != null && rtValue)	// ¶óµð¿À ¹öÆ°ÀÌ¸é
		{
			var errname = what.errname;
			var radioName = what.name;
			var radioValue = false;

			while (what.name == radioName)
			{
				if (what.checked)
				{
					radioValue = true;
				}

				what = f.elements[++i];
			}

			if (!radioValue)
			{
				alert (errname + "À»(¸¦) " + "¼±ÅÃÇØ ÁÖ¼¼¿ä.");
				return false;
			}
			
			i--;
			continue;
		}

		if (what.notnull != null && rtValue)	// °ø¹é ¹®ÀÚ Ã¼Å©
			rtValue = notnull_check(what);
			
		if (what.minlength != null && rtValue)	// ÃÖ¼Ò ±ÛÀÚ¼ö Ã¼Å©
			rtValue = checkMinLength(what);

		if (what.onlyemail != null && rtValue)	// ÀÌ¸ÞÀÏ Ã¼Å©
			rtValue = onlyemail_check (what);

		if (what.onlyhan != null && rtValue)	// ÇÑ±Û Ã¼Å© (Çã¿ë)
			rtValue = onlyhan_check (what);

		if (what.denyhan != null && rtValue)	// ÇÑ±Û Ã¼Å© (ºÒÇã¿ë)
			rtValue = denyhan_check (what);

		if (what.onlynum != null && rtValue)	// Á¤¼ö Ã¼Å©
			rtValue = number_check(what);

		if (what.juminCheck != null && rtValue)	 // ÁÖ¹Îµî·Ï ¹øÈ£ °Ë»ç
			rtValue = chkJuminNum(f);

		if (!rtValue)
			return false;

	
	}

	return true;
}

//	¿øÇÏ´Â ±ÛÀÚ¼ö°¡ Ã¡À» °æ¿ì ÀÌµ¿ÇÏ´Â ÇÔ¼ö
function moveForm(str,nextOb,len){
	if(str.length == len ) nextOb.focus();
}

function Trim(strValue)
{
	var reg1 = /^\s*|\s*$/g;
	return strValue.replace(reg1, "");
}

