/*---------------------------------------------------------------------------
 * ÇÑ±¹½Å¿ëÁ¤º¸ ¸¶ÀÌÅ©·¹µðÆ® ÇÁ·ÎÁ§Æ®
 *---------------------------------------------------------------------------
 * @(#)form.js
 * @author K.Y.W
 * @version 1.0, 2005/10/19
 *
 * 1.Description   : form ¶Ç´Â control ´ÜÀ§·Î Ã³¸®ÇÏ´Â ÇÔ¼öµé
 * 2.Page Type     : Java Script
 * 3.Update Source :
 *       <2005/10/19><±è¿µ¿ì>:<init version>
 *--------------------------------------------------------------------------*/

/**
 * ÀÔ·ÂÇÊµå À¯È¿ Ã¼Å©
 * - form À» ÀÎÀÚ·Î ³Ñ°Ü¹Þ¾Æ form ¾ÈÀÇ ¸ðµç ÄÁÆ®·Ñ¿¡ ´ëÇØ¼­ Ã¼Å©ÇÑ´Ù.
 * - Ã¼Å©¿¡ ÇÊ¿äÇÑ user define tag ¸¦ ¹Ì¸® Á¤ÀÇÇØ ³õ¾Æ¾ß ÇÑ´Ù. Á¤ÀÇÇÏÁö ¾ÊÀ¸¸é °Ç³Ê¶Ú´Ù.
 *
 * [user define tag]
 *  - essential : ÇÊ¼ö¿©ºÎ, °ªÀÌ true or 1 ÀÌ¸é ÇÊ¼ö¿©ºÎ Ã¼Å©, Á¤ÀÇÇÏÁö ¾Ê°Å³ª false or 0 ÀÌ¸é ÆÐ½º..
 *  - fieldname : ÇÊµå¸í
 *  - datatype  : µ¥ÀÌÅÍ Å¸ÀÔ("STR","NUM","DAT")
 *  - maxlength : ÀÔ·Â°ª ÃÖ´ë ±æÀÌ
 *
 * [example]
 *  function saveData()
 *  {
 *      if (!checkValidity(frmMain)) return ;
 *  }
 *
 * @param oForm form object
 * @return boolean
 */
function checkValidity( oForm )
{
	var oElement;
	var elementName = "";
	var elementTagName ;
	var isNewArrayElem;
	var elementArrCnt = 0;
	var arr_elementName = new Array();
	var idxLine;

	if ( oForm == null ) return false;

	if (arguments[1] != null && arguments[1] != "") idxLine = arguments[1] ;

	for( var idx=0; idx<oForm.elements.length; idx++ )
	{
		//alert("start name="+oForm.elements[idx].name + "\n" + "type="+oForm.elements[idx].type + "\n" + "tagName="+oForm.elements[idx].tagName + "\n" + "datatype="+oForm.elements[idx].datatype );

		oElement = oForm.elements[idx];
		elementTagName = oElement.tagName.toUpperCase();

		//hidden, radio, checkbox Á¦¿Ü
		if(elementTagName== "INPUT" && oElement.type.toUpperCase() != "TEXT") continue;

		elementName	= oElement.name;
		oElement = eval("oForm." + elementName);

		// ÀÏ¹ÝÀûÀÎ Ã³¸®(¹è¿­ÀÌ ¾Æ´Ñ °æ¿ì)
		if (oElement.length==null || (elementTagName=="SELECT" && oElement[0]!=null && oElement[0].length==null))
		{
			if (!checkValidateByATT(oElement)) return false;
		}
		// °´Ã¼°¡ ¹è¿­ÀÏ °æ¿ìÀÇ Ã³¸®
		else
		{
			isNewArrayElem = true;

			for(var k = 0; k < elementArrCnt; k++)
			{
				if ( arr_elementName[k] == elementName )
				{
					isNewArrayElem = false;
					break;
				}
			}

			if (isNewArrayElem)
			{
				if (oElement[0] != null)
				{
					arr_elementName[elementArrCnt] = elementName;
					elementArrCnt += 1;

					//Æ¯Á¤ ÇÑ°³¸¸ Ã³¸®ÇÒ °æ¿ì
					if (idxLine != null && idxLine >=0)
					{
						if (!checkValidateByATT(oElement[idxLine])) return false;
					}
					//¸ðµç ¹è¿­¿¡ ´ëÇØ Ã³¸®ÇÒ °æ¿ì
					else
					{
						for(var m=0; m<oElement.length; m++)
						{
							if (!checkValidateByATT(oElement[m])) return false;
						}
					}
				}
			}
		}

	}
	return true;
}

/**
 * ÀÔ·ÂÇÊµå À¯È¿ Ã¼Å© (with User defined Tag), checkValidity() ¿¡¼­ È£Ãâ
 *
 * @param oElement object
 * @return boolean
 */
function checkValidateByATT( oElement )
{
	if (oElement.disabled) return true;
	if (oElement.essential == null) oElement.essential = false;

	var elementFieldName = oElement.fieldname;
	if(elementFieldName==null || elementFieldName=="") elementFieldName = "ÇØ´ç";

	var elementDataType = oElement.datatype;
	if(elementDataType==null || elementDataType=="") elementDataType = "";

	var elementValue = trim(oElement.value);

	//±æÀÌ ÀÔ·Â Ã¼Å©
	if(oElement.datalength != "" && elementValue != "" && elementFieldName != "ÇØ´ç")
	{
		if (elementValue.length > oElement.maxlength )
			return alertError( oElement, getMessage("DATALENGTH", elementFieldName, oElement.maxlength ));
	}

	//ÇÊ¼ö ÀÔ·Â Ã¼Å©
	if (oElement.essential == true )
	{
		if (elementValue.length < 1)
			return alertError( oElement, getMessage("ESSENTIAL", elementFieldName) );
	}

	//¼ýÀÚ ÀÔ·Â Ã¼Å©
	if (elementDataType.toUpperCase() == "NUM" &&  elementValue.length > 0)
	{
		//elementValue = elementValue.replace( /\.|\,/g, "" )

		if (!isNumber(elementValue) )
			return alertError( oElement, getMessage("VALID_NUMBER", elementFieldName) );
	}

	//³¯Â¥(YYYYMMDD) ÀÔ·Â Ã¼Å©
	if (elementDataType.toUpperCase() == "DAT" &&  elementValue.length > 0)
	{
		if (!isDate(elementValue))
			return alertError( oElement, getMessage("VALID_DATE", elementFieldName) );
	}

	return true;
}

/**
 * FormÀÇ °¢ Object °ªÀ» clear
 *
 * @param oForm 		form object
 * @param objectType	Æ¯Á¤ Object Type(input,select,textarea µî), ÇØ´ç Å¸ÀÔ Object¿¡ ´ëÇØ¼­¸¸ clear
 * @return none
 */
function resetForm( oForm )
{
	if(oForm==null) return false;

	var objectType = "";

	if (arguments[1] != null && arguments[1] != "")
	{
		objectType = arguments[1] ;
		//hidden ÀÏ°æ¿ì INPUTÅÂ±×·Î ÀÎ½Ä
		objectType = (trim(objectType.toUpperCase())=="HIDDEN") ? "INPUT":objectType.toUpperCase() ;
	}

	for( var idx=0; idx<oForm.elements.length; idx++ )
	{
		var oElement = oForm.elements[idx];
		var elementTagName = oElement.tagName.toUpperCase();
		var elementType = oElement.type.toUpperCase();

		if(elementType=="RADIO" || elementType=="CHECKBOX") continue;

		if( objectType != "" )
		{
			if( objectType!=elementTagName ) continue;
		}

		if( elementTagName == "SELECT" )
			oElement.selectedIndex = 0;
		else
			oElement.value = "";
	}
}

/**
 * ¿¡·¯ ¸Þ¼¼Áö¸¦ º¸¿©ÁÖ°í ÇØ´ç object Æ÷Ä¿½º
 *
 * @param obj
 * @param msg 	error message
 * @return boolean
 */
function alertError( obj, msg )
{
	alert( msg );
	if (obj != null)
	{
		var type = obj.type.toUpperCase();
		if( ! obj.isDisabled && ! obj.readOnly && type!="HIDDEN")
		{
			obj.value = "";
			obj.focus();
		}
	}
	return false;
}

/**
 * À¯È¿ÇÑ ³¯Â¥ÀÎÁö Ã¼Å©ÇÑ ´ÙÀ½, ³¯Â¥ Æ÷¸Ë(YYYY-MM-DD)À» Àû¿ëÇÑ´Ù.
 *
 * @param obj 	date object
 * @param msg 	error message
 * @return boolean
 */
function checkDate ( obj )
{
	if (obj==null) return false;

	var objectValue = obj.value ;
	if (objectValue == "") return false;
	objectValue = objectValue.replace( /\/|\-/g, "" );
	if ( !isDate(objectValue) )
		return alertError( obj, getMessage("INVALID_DATE") )
	else
		obj.value = formatDate(objectValue);

}


/**
 * À¯È¿ÇÑ ¼ýÀÚÀÎÁö Ã¼Å©ÇÑ ´ÙÀ½, ¼ýÀÚ Æ÷¸ËÀ» Àû¿ëÇÑ´Ù.
 *
 * @param obj 			number object
 * @param iNumDecimals 	º¸¿©ÁÙ ¼Ò¼öÁ¡ÀÌÇÏ ÀÚ¸®¼ö(optional)
 * @return boolean
 */
function checkNumber( obj )
{
	if(obj==null) return false;

	var objectValue = obj.value;
	if( objectValue == "" ) return false;

	if( ! isNumber(objectValue) ) return alertError( obj, getMessage("INVALID_NUMBER") )

	var iNumDecimals = 0 ;
	if (arguments[1] != null && arguments[1] != "") iNumDecimals = arguments[1] ;

	objectValue = formatNumber(objectValue, iNumDecimals);
	obj.value = objectValue;
}



/**
 * ÀüÈ­¹øÈ£ Ã¼Å©
 *
 * @param obj
 */
function checkPhoneNumber(obj)
{
	var availStr="-0123456789 ";
	var compareStr=obj.value;
	var objLen=compareStr.length;

	for (var i=0; i < objLen; i++)
	{
		ch=compareStr.substr(i,1);
		if(availStr.indexOf(ch)<0) return alertError(getMessage("INVALID_PHONENUMBER"));
	}
}

/**
 * ÁÖ¹Îµî·Ï¹øÈ£ Ã¼Å©
 *
 * @param objResNo ÁÖ¹Îµî·Ï¹øÈ£ ÀÔ·Â¹Ú½º ÄÁÆ®·Ñ
 * @return boolean
 */
function checkJuminNo(objResNo)
{
	if(objResNo == null) return false;

	if( ! isValidPersonID(objResNo.value))
		return returnError( objResNo, getMessage("INVALID_JUMIN") );
}

/**
 * ÁÖ¹Îµî·Ï¹øÈ£ Ã¼Å©
 *
 * @param jumin_no ÁÖ¹Îµî·Ï¹øÈ£ ¹®ÀÚ¿­
 * @return boolean
 */
function isValidPersonID(jumin_no)
{
	jumin_no = jumin_no.replace( /\-/g, "" );
	if(jumin_no.length < 13) return false;

	var a = new Array(6)
	var b = new Array(7)
	var tot=0
	var c=0

	var sJumin0 = jumin_no.substring(0,6);
	var sJumin1 = jumin_no.substring(6,13);

	var genderType = sJumin1.substring(0,1);
	if(genderType!="1"&&genderType!="2"&&genderType!="3"&&genderType!="4" ) return false;

	for(var i=1;i<7;i++)
	{
		a[i]=sJumin0.substring(i-1,i);
		b[i]=sJumin1.substring(i-1,i);

		if(i<3)
			c=Number(b[i])*(i+7);
		else
			c=Number(b[i])*((i+9)%10);

		tot = tot + Number(a[i])*(i+1) + c;
	}

	b[7]=sJumin1.substring(6,7);

	if(Number(b[7]) != ((11-(tot%11))%10))
		return false;
	else
		return true;;
}

/**
 * »ç¾÷ÀÚ¹øÈ£ Ã¼Å©
 *
 * @param objCorpNo »ç¾÷ÀÚ¹øÈ£ ÀÔ·Â¹Ú½º ÄÁÆ®·Ñ
 * @return boolean
 */
function checkCorpNo(objCorpNo)
{
	if(objCorpNo == null) return false;

	if( ! isValidCorpNo(objCorpNo.value))
		return returnError( objCorpNo, getMessage("INVALID_CORPNO") );
	else
		return true;
}

/**
 * »ç¾÷ÀÚ¹øÈ£ Ã¼Å©
 *
 * @param sCorpNo »ç¾÷ÀÚµî·Ï¹øÈ£ ¹®ÀÚ¿­(10ÀÚ¸®)
 * @return boolean
 */
function isValidCorpNo(sCorpNo)
{
	var chkRule = "137137135";
	var step1, step2, step3, step4, step5, step6, step7;

	step1 = 0;

	for (var i=0; i<7; i++)
	{
		step1 = step1 + (sCorpNo.substring(i, i+1) * chkRule.substring(i, i+1));
	}

	step2 = step1 % 10;
	step3 = (sCorpNo.substring(7, 8) * chkRule.substring(7, 8)) % 10;
	step4 = sCorpNo.substring(8, 9) * chkRule.substring(8, 9);
	step5 = Math.round(step4 / 10 - 0.5);
	step6 = step4 - (step5 * 10);
	step7 = (10 - ((step2 + step3 + step5 + step6) % 10)) % 10;

	if (sCorpNo.substring(9, 10) != step7)
		return false;
	else
		return true;
}

/**
 * E-Mail À¯È¿¼ºÈ®ÀÎ
 *
 * @param objEmail E-Mail ¹®ÀÚ¿­(10ÀÚ¸®)
 * @return boolean
 */
 /*
function checkEmail( objEmail )
{
	var speExp3 = /[#=+\*\"\'\~\!\#\$\%\^\&\*\(\)\`\,\/\:\"\;\'\|\<\>\?\\]/;
	var email = Trim(objEmail.value);
	if (email == "") return true;

	var isInvalid = false;
	if (!isInvalid && email.indexOf("@") == -1) isInvalid=true;
	if (!isInvalid && email.indexOf(".") == -1) isInvalid=true;
	if (!isInvalid && email.search(speExp3) != -1) isInvalid=true;
	if (!isInvalid && email.indexOf("@") != email.lastIndexOf("@")) isInvalid=true;
	if (!isInvalid && email.indexOf("@") > email.indexOf(".")) isInvalid=true;
	if (!isInvalid && email.indexOf("@") < 2 ) isInvalid=true;
	if (!isInvalid && email.length - email.indexOf("@") < 5 ) isInvalid=true;

	var email_tail = email.substring(email.lastIndexOf(".")+1, email.length);
	if (!isInvalid && email_tail == '' ) isInvalid=true;
	if (!isInvalid && email_tail != 'info' && email_tail != 'biz'  && email_tail != 'com'  && email_tail != 'net'
	&& email_tail != 'org'  && email_tail != 'edu'  && email_tail != 'kr'  && email_tail != 'kr'
	&& email_tail != 'jp'  && email_tail != 'cn'  && email_tail != 'to'  ) isInvalid=true;

	if(isInvalid) return returnError(objEmail, getMessage("INVALID_EMAIL") );
	return true;
}*/

/**
 *  E-Mail À¯È¿¼ºÈ®ÀÎ
 *
 * @param obj
 */
function checkEmail(obj)
{
	if(obj.value != "") return false;

	var defStr="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_@.";
	var str = obj.value;

	for (var i=0; i<str.length; i++)
	{
		if(defStr.indexOf(str.substring(i,i+1))<0) return alertError(getMessage("INVALID_EMAIL"));
	}

	if (str.length < 10) return alertError(getMessage("INVALID_EMAIL"));

	var nPos=str.indexOf('@');
	if (nPos < 0) return alertError(getMessage("INVALID_EMAIL"));

	var subStr=str.split("@");
	a = subStr[0];
	b = subStr[1];

	//¸ÞÀÏ°èÁ¤ÀÌ 3ÀÚ¹Ì¸¸ÀÏ °æ¿ì
	if (subStr[0].length < 3) return alertError(getMessage("INVALID_EMAIL") + getMessage("INVALID_EMAIL3CHR"));

	// '@' µÚ ºÎºÐÀÇ '.'À» Æ÷ÇÔÇÑ ±ÛÀÚ¼ö°¡ 6ÀÚ¹Ì¸¸ÀÔ´Ï´Ù.
	if (subStr[1].length < 6) return alertError(getMessage("INVALID_EMAIL") + getMessage("INVALID_EMAIL6CHR"));

	//'.' ±¸ºÐÀÚ°¡ ¾ø½¿!!
	if (subStr[1].indexOf('.') < 0) return alertError(getMessage("INVALID_EMAIL") );
}

/**
 *  ÄÁÆ®·ÑÀÇ IME Mode¸¦ ¼ÂÆÃÇÑ´Ù.
 *
 * @param obj
 * @param isKorean	ÇÑ±Û:true, ¿µ¹®:false
 */
function setIMEMode(obj, isKorean)
{
	if(isKorean)
		obj.style.imeMode='active';
	else
		obj.style.imeMode='inactive';
}

/**
 * º¸ÀÌ±â ¼³Á¤À» Á¶Á¤ÇÑ´Ù.
 *
 * @param obj
 * @param flag	º¸ÀÌ±â:true, ¾Èº¸ÀÌ±â:false
 */
function setDisplay(obj,flag)
{
    if(flag)
        obj.style.display = "";
    else
        obj.style.display = "none";
}

/**
 * get Radio Object Value
 *
 * @param radioObj object
 * @return String
 */
function getRadioValue(radioObj)
{
	if (radioObj == null) return "";

	if (radioObj.length != null)
	{
		for(i=0; i<radioObj.length; i++)
		{
			if (radioObj[i].checked) return radioObj[i].value;
		}
	}
	else
	{
		if (radioObj.checked) return radioObj.value;
	}

	return "";
}

/**
 * set Radio Object Value
 *
 * @param radioObj object
 * @param value object value
 * @return String
 */
function setRadioValue(radioObj, value)
{
	if (radioObj == null) return;

	if (radioObj.length != null)
	{
		for(i=0; i<radioObj.length; i++)
		{
			if (radioObj[i].value == value)
				radioObj[i].checked = true;
		}
	}
	else
	{
		if (radioObj.value == value)
			radioObj.checked = true;
	}
}

/**
 * get combo object value
 *
 * @param obj
 */
function getComboValue(obj)
{
	if(obj) return obj.options[obj.selectedIndex].value;
}


/**
 * get combo object text
 *
 * @param obj
 */
function getComboText(obj)
{
	if(obj) return obj.options[obj.selectedIndex].text;
}

/**
 * create combo options
 *
 * @param obj 		combo object
 * @param optionValue  	option value array(0:value, 1:text)
 */
function createCombo(obj, optionValue)
{
	var optionElem;

	for(i=0; i<optionValue.length; i++)
	{
		optionElem = document.createElement("OPTION");
		optionElem.value = optionValue[i][0];
		optionElem.text	= optionValue[i][1];

		obj.add(optionElem);
	}
}

/**
 * add option
 *
 * @param obj 		combo object
 * @param value  	option value
 * @param text		option text
 */
function addOption(obj, value, text)
{
	var optionElem;

	optionElem = document.createElement("OPTION");
	optionElem.value = value;
	optionElem.text	= text;

	obj.add(optionElem);
}

/**
 * remove option
 *
 * @param obj 	combo object
 */
function clearCombo(obj)
{
	var cntOption = obj.length;

	for(i=0; i<cntOption; i++)
	{
		obj.options.remove(0);
	}
}

/**
 * checkbox ÀüÃ¼¼±ÅÃ/¼±ÅÃÇØÁ¦
 *
 * @param obj	checkbox object
 */
var _allcheck = false;
function selectAll(obj)
{
	_allcheck = !_allcheck;

	if(obj)
	{
		if(obj.length)
		{
			for(var i=0;i<obj.length;i++)
			{
				obj[i].checked = _allcheck;
			}
		}
		else
			obj.checked = _allcheck;
	}
	else
		alert(getMessage("NO_ITEM"));
}

/**
 * checkbox Ã¼Å©µÈ °ª °¡Á®¿À±â
 *
 * @param obj	checkbox object
 */
function getCheckItem( obj )
{
	var returnvalue;
	if(!obj) return false;
	var checked = false;

	if(obj.length)
	{
		for(i=0; i<obj.length; i++)
		{
			if(obj[i].checked == true)
			{
				returnvalue = returnvalue + "," + obj[i].value;
				checked = true;
			}
		}
	}
	else
	{
		if(obj.checked == true)
		{
			returnvalue = returnvalue + "," + obj.value;
			checked = true;
		}
	}

	if (checked) returnvalue = returnvalue.substring(1);
	return returnvalue;
}



/**
 * ±Ý¾× ÀÔ·Â Ã¼Å©
 * ÀÔ·Â°¡´É key °ª : [0..9][.][,][-]
 * ex) onKeyPress="keyCheckAmount(this)"
 *
 * @param thisObj
 */
function keyCheckAmount(thisObj)
{
	if(thisObj.readOnly) return false;
	allowOnly("0..9|.,-");
}

/**
 * ¼ýÀÚ ÀÔ·Â Ã¼Å©
 * ÀÔ·Â°¡´É key °ª : [0..9]
 * ex) onKeyPress="keyCheckNumeric(this)"
 *
 * @param thisObj
 */
function keyCheckNumeric(thisObj)
{
	if(thisObj.readOnly) return false;
	allowOnly("0..9");
}

/**
 * ¼ýÀÚ ÀÔ·Â Ã¼Å©
 * ÀÔ·Â°¡´É key °ª : [1..9][-:45]
 * ex) onKeyPress="keyCheckInteger(this)"
 *
 * @param thisObj
 */
function keyCheckInteger(thisObj)
{
	if(thisObj.readOnly) return false;
	allowOnly("0..9|-");
}

/**
 * ¼ýÀÚ ÀÔ·Â Ã¼Å©
 * ÀÔ·Â°¡´É key °ª : [1..9][-:45][.:46]
 * ex) onKeyPress="keyCheckFloat(this)"
 *
 * @param thisObj
 */
function keyCheckFloat(thisObj)
{
	if(thisObj.readOnly) return false;
	allowOnly("0..9|.-");
}

/**
 *  This function allow entering just the specified expression to a textbox or textarea control.
 *
 * @param expression 	Allowed characters
 *                   	a..z => ONLY LETTERS
 *                  	0..9 => ONLY NUMBERS
 *                  	other symbols...
 * @return none
 *
 * [example] use the onKeyPress event to make this function work:
 *           //Allows only from A to Z
 *           onKeyPress="allowOnly('a..z');"
 *
 *           //Allows only from 0 to 9
 *           onKeyPress="allowOnly('0..9');"
 *
 *           //Allows only A,B,C,1,2 and 3
 *           onKeyPress="allowOnly('abc123');"
 *
 *           //Allows only A TO Z,@,#,$ and %
 *           onKeyPress="allowOnly('a..z|@#$%');"
 *
 *           //Allows only A,B,C,0 TO 9,.,,,+ and -
 *           onKeyPress="allowOnly('ABC|0..9|.,+-');"
 *
 * [remarks] Use the pipe "|" symbol to separate a..z from 0..9 and symbols
 */
function allowOnly(expression)
{
	expression = expression.toLowerCase();
	expression = expression.replace( "a..z", "abcdefghijklmnopqrstuvwxyz");
	expression = expression.replace( "0..9", "0123456789");
	expression = expression.replace( "1..9", "123456789");
	expression = expression.replace( "|", "");

	var ch = String.fromCharCode(window.event.keyCode);
	ch = ch.toLowerCase();
	expression = expression.toLowerCase();
	var a = expression.indexOf(ch);
	if (a == -1) window.event.keyCode = 0;
}