﻿/* ****************************************************************************
//  FILENAME    : formhelper.js 
//  VERSION     : 1.1.1
//  LAST MODIFIED : 2003.10.10.
//  COMMENT     : checkRE(r, v) 추가 
//  REFERENCE   : http://www.xbrain.co.kr/js/formhelper.js 
//                http://www.xbrain.co.kr/js/formhelper.html (예제 및 설명)
**************************************************************************** */

// checkFormValue(폼이름)
// compare(e1, e2) : 두 엔터티의 값을 비교 
// checkRRN(n) : 주민등록번호 검증. 00-00, 0000 형식 모두 검증 가능 
// checkRRN2(n1, n2)  
// checkRRN2E(e1, e2) : 값을 가지고 있는 엔터티를 인자로 사용 
// checkReg(r, v) : 앞의 정구식으로 뒤의 값을 평가 
// .trim : 앞뒷 공백문자 제거 


function checkFormValue(f)
{
	var i; 	var re; 
	var args;
	var result;
	for (i = 0; i < f.elements.length ; i++) 
	{
		if (typeof(f.elements[i].tag) == "undefined") continue;

		args = f.elements[i].tag.split("||", 3);
		if (args[0] == "MF" || (args[0] == "OF" && f.elements[i].value.length > 0)) 
		{
			result = eval(args[1] + "(f.elements[i].value);");
		}
		else if (args[0] == "MC" || (args[0] == "OC" && f.elements[i].value.length > 0)) 
		{	
			result = eval(args[1]);
		} 
		else if (args[0] == "MR" || (args[0] == "OR" && f.elements[i].value.length > 0))
		{ 
			re = new RegExp(args[1], "gi");
			result = re.test(f.elements[i].value);
		} 
		
		if (result == false)
		{
		    if (f.elements[i].type != 'hidden')
			    f.elements[i].focus();
			alert(args[2]);
		    if (f.elements[i].type != 'hidden')
    			f.elements[i].select();
			return false;
		}
	}
	return true;
}

function compare(a, b) {
	if (a.value == b.value) return true;
	return false;	
}

		
function checkRRN(no) 
{
	sum = 0;
	seed = "234567892345";
	no = no.trim();
	if (no.length == 14) no = no.substring(0, 6) + no.substring(7, 14);
	for (i = 0; i < 12; i++) 
	{
		sum = sum+ parseInt(no.substring(i, i+1)) * parseInt(seed.substring(i, i+1));
	}
	chksum = (11 - (sum % 11)) % 10;
	if (parseInt(no.substring(12, 13)) != chksum) return false;
	else return true;
}

function checkRRN2(no1, no2)
{
	regno = no1.trim() + no2.trim();
	return checkRRN(regno);
}

function checkRRN2E(e1, e2)
{
	regno = e1.value.trim() + e2.value.trim();
	return checkRRN(regno);
}

String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function checkReg(r, v) 
{
	re = new RegExp(r, "gi");
	return re.test(v);
}