/*
 * Validation Library
 *
 * @version 1.1
 * @author BAEK, CHANG YOL http://cybaek.com/
 * @date 2003.11.26
 */
var ERRMSG_DEFAULT_SCOPE = "°ªÀÇ ¹üÀ§°¡ Àû´çÇÏÁö ¾Ê½À´Ï´Ù. (ÃÖ¼Ò: $MinScope, ÃÖ´ë: $MaxScope)";
var ERRMSG_DEFAULT_LEN = "ÀÔ·ÂÇÑ °ªÀÇ ±æÀÌ°¡ Àû´çÇÏÁö ¾Ê½À´Ï´Ù. (ÃÖ¼Ò: $MinLen, ÃÖ´ë: $MaxLen)";
var ERRMSG_DEFAULT_CHECKFUNC = "ÀÔ·Â°ªÀÌ ¿Ã¹Ù¸£Áö ¾Ê½À´Ï´Ù.";
var ERRMSG_DEFAULT_FORBIDDEN_CHARS = "$ForbiddenChars´Â ±ÝÁö ¹®ÀÚÀÌ°í, $FoundChar°¡ ¹ß°ßµÇ¾ú½À´Ï´Ù."
var ERRMSG_DEFAULT_REGEXP = "ÀÔ·Â°ªÀÌ ¿Ã¹Ù¸£Áö ¾Ê½À´Ï´Ù."
var ERRMSG_NUMBER = "¼ýÀÚ¸¦ ÀÔ·ÂÇÏ½Ê½Ã¿À.";

function f_checkValid(f){

    var j = f.elements.length
    var i;
    var re;
    var curr;
    
    for (i=0; i<j; i++)
    {
		curr = f.elements[i];
        if (typeof(curr.V) == "undefined") continue;
		
		preprocess(curr);

		if (!checkLength(curr) ||
			!checkScope(curr) ||
			!checkWithFunc(curr) ||
			!checkWithRegExp(curr)){
			return false;
		}

		if (!checkForbiddenChars(curr)){
			return false;
		}
    }

    return true;
}

function preprocess(curr){
	removeExtremeSpace(curr);
}

function removeExtremeSpace(curr){
	if (typeof(curr.Trim) != "undefined"){
		if (curr.Trim.indexOf("L") != -1){
			curr.value = ltrim(curr.value);
		}
		if (curr.Trim.indexOf("R") != -1){
			curr.value = rtrim(curr.value);
		}
	}
}

// @author: brad@vermontsoftware.com
function ltrim(str){
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

// @author: brad@vermontsoftware.com
function rtrim(str){
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

// @author: brad@vermontsoftware.com
function trim(str){
   return rtrim(ltrim(str));
}


function checkLength(curr){
	var checkLen = true;
	var minLen, maxLen, length;
	minLen = parseInt(curr.MinLen);
	if (isNaN(minLen)){
		return true;
	}
	maxLen = parseInt(curr.MaxLen);
	if (isNaN(maxLen)){
		return true;
	}

	length = parseInt(b_length(curr.value));
	if ((length<minLen) || (length>maxLen)){
		curr.focus();
		if (typeof(curr.LenErrMsg) == "undefined"){
			curr.LenErrMsg = ERRMSG_DEFAULT_LEN;
		}
		var errMsg = curr.LenErrMsg;
		errMsg = errMsg.replace("$MinLen", curr.MinLen);
		errMsg = errMsg.replace("$MaxLen", curr.MaxLen);
		alert(errMsg);
		return false;
	}

	return true;
}

function checkScope(curr){
	var checkScope = true;
	var minScope, maxScope;

	minScope = parseInt(curr.MinScope);
	if (isNaN(minScope)){
		return true;
	}
	maxScope = parseInt(curr.MaxScope);
	if (isNaN(maxScope)){
		return true;
	}
	
	if (isNaN(curr.value)){
		curr.focus();
		alert(ERRMSG_NUMBER);
		return false;
	}
	
	if ((curr.value > maxScope) || (curr.value < minScope)){
		curr.focus();
		if (typeof(curr.ScopeErrMsg) == "undefined"){
			curr.ScopeErrMsg = ERRMSG_DEFAULT_SCOPE;
		}
		var errMsg = curr.ScopeErrMsg;
		errMsg = errMsg.replace("$MinScope", curr.MinScope);
		errMsg = errMsg.replace("$MaxScope", curr.MaxScope);
		alert(errMsg);
		return false;
	}

	return true;
}

function checkWithFunc(curr){
	var result;

	if (typeof(curr.CheckFunc) != "undefined"){
		try{
			result = eval(curr.CheckFunc+"(curr, curr.value);");
		}
		catch(e){
			alert("ÀÔ·Â°ª °Ë»ç ÇÔ¼ö " + curr.CheckFunc + "(src, value) ¿¡ ¿À·ù°¡ ÀÖ½À´Ï´Ù.");
			result = false;
		}
		if (!result){
			curr.focus();
			if (typeof(curr.CheckFuncErrMsg) == "undefined"){
				curr.CheckFuncErrMsg = ERRMSG_DEFAULT_CHECKFUNC;
			}
			alert(curr.CheckFuncErrMsg);
			return false;
		}
	}
	return true;
}

function checkForbiddenChars(curr){
	if (typeof(curr.ForbiddenChars) == "undefined"){
		return true;
	}

	var length, i, currChar, value;
	length = curr.value.length;
	value = curr.value;

	for(i=0; i<length; i++){
		currChar = value.charAt(i);
		if (curr.ForbiddenChars.indexOf(currChar) != -1){
			curr.focus();
			if (typeof(curr.ForbiddenCharsErrMsg) == "undefined"){
				curr.ForbiddenCharsErrMsg = ERRMSG_DEFAULT_FORBIDDEN_CHARS;
			}
			var errMsg = curr.ForbiddenCharsErrMsg;
			errMsg = errMsg.replace("$ForbiddenChars", curr.ForbiddenChars);
			errMsg = errMsg.replace("$FoundChar", currChar);
			alert(errMsg);
			return false;
		}
	}
	return true;
}

function checkWithRegExp(curr){
	if (typeof(curr.RegExp) == "undefined"){
		return true;
	}

	var re = new RegExp(curr.RegExp, "gi");
	if (!re.test(curr.value)){
		curr.focus();
		if (typeof(curr.RegExpErrMsg) == "undefined"){

				curr.RegExpErrMsg = ERRMSG_DEFAULT_REGEXP;
		}
		var errMsg = curr.RegExpErrMsg;
		errMsg = errMsg.replace("$RegExp", curr.RegExpErrMsg);
		alert(errMsg);
		return false;
	}

	return true;
}



function b_length(str){
	var iRet = 0;
	var iLen = str.length;
	
	for (i=0; i<iLen; i++){
		if ((str.charCodeAt(i)<0) || (str.charCodeAt(i)>127)){
			iRet = iRet + 1;
		}
	}
	
	return (iLen + iRet);
}

//ÇÔ¼ö:select¿¡¼­ ¼±ÅÃµÈ°ÍÀÌ ÀÖ³ª
function c_selectValid(src,value) {
	var i=src.selectedIndex;
	if (typeof(i)=="undefined" || i==0 ) {
		src.focus();
		return false
	} else {
      return true;
	}
}

