// window에서 받아드릴수 있는 문자열로 변경한다.
String.prototype.trim = function(str) {
	str = this != window ? this : str;
	return str.replace(/^\s+/g,'').replace(/\s+$/g,'');
}

// 아스키 코드를 비교하여 바이트 수를 계산한다.
String.prototype.bytes = function(str) {
	str = this != window ? this : str;
	var len = "";
	for(j=0; j<str.length; j++) {		
		var chr = str.charAt(j);
		var chr_code = (chr.charCodeAt() > 128) ? 2 : 1
		len = parseInt(len + chr_code);
	}
	return len;
}
function test2()
{
	alert ("test2");
}
function FormValueCheck(form)
{
	
	// 몇개의 input 이 존재하는지를 검사한다. submit 버튼도 포함된다. 갯수 계산시 주의 할것
	var FormValueCount = form.elements.length;

	for(i=0; i < FormValueCount; i++) {

		var f = form.elements[i];

		if(f.value) {
			f.value = f.value.trim();
		}

		var required = f.getAttribute("required");		// 필수 항목검사
		var minbyte = f.getAttribute("minbyte");		// 최소 바이트 검사
		var maxbyte = f.getAttribute("maxbyte");		// 최대 바이트 검사
		var itemname = f.getAttribute("itemname");		// 필드 종류
		var name = f.getAttribute("name");				// 필드 이름
		var match = f.getAttribute("match");			// MATCH 필드
		var check = f.getAttribute("check");			// 특별 check
		var notfocus = f.getAttribute("notfocus");		// 포커스가지않기
		var checktype = f.getAttribute("type");			// 필드 타입
		if(itemname != null || itemname) {	
			var msg = itemname;
		} else { 
			var msg = name;
		}

		// 필수 항목검사
		if(required != null) {
			
			// type이 checkbox를 제외한 모든것
			if(f.value == null || f.value == "") {
				var ReturnErrorMsg = msg + " 는 필수 입력항목입니다.";
				return ErrorMsg(f,ReturnErrorMsg,notfocus);
			}

			// type이 checkbox일 경우 체크 방식이 value를 체크하는것이 아니라 checked를 체크해야된다.  //checked갯수가 하나라도
			if(checktype == "checkbox") {
				var cnt=0;
				for(var j=0; j < FormValueCount; j++) {
					if(form.elements[j].checked){
						cnt++;
					}
				}

				if(cnt==0){
					var ReturnErrorMsg = msg + "는 필수 체크항목입니다.";
					return ErrorMsg(f,ReturnErrorMsg,notfocus);
				}
				/*if(!form.elements[i].checked) {  
					var ReturnErrorMsg = msg + "는 필수 체크항목입니다.";
					return ErrorMsg(f,ReturnErrorMsg,notfocus);       
				}*/
			}

		}

		// 최소 바이트 검사
		if(minbyte != null) {
			if(f.value.bytes() < parseInt(minbyte)) {
				var ReturnErrorMsg = msg+" 는 최소 "+ minbyte + " 바이트 이상 입력해야합니다.";
				return ErrorMsg(f,ReturnErrorMsg,notfocus);

			}
		}

		// 최대 바이트 검사
		if(maxbyte != null) {
			if(f.value.bytes() > parseInt(maxbyte)) {
				var ReturnErrorMsg = msg+" 는 최대 "+ maxbyte + " 바이트를 넘을수 없습니다.";
				return ErrorMsg(f,ReturnErrorMsg,notfocus);
			}
		}

		// MATCH 필드
		if(match != null) {
			if(f.value != form.elements[match].value) {
				var ReturnErrorMsg = msg+" 가 서로 일치 하지 않습니다.";
				return ErrorMsg(f,ReturnErrorMsg,notfocus);
			}
		}

		// 특별 check
		if(check != null) {
			switch(check) {				
				case 'number' :		// 숫자로 구성된 것을 체크한다.			
						if(!isNumberCheck(f.value)) {
							var ReturnErrorMsg = msg+" 는 숫자로만 구성되어야 합니다.";
							return ErrorMsg(f,ReturnErrorMsg,notfocus);
						} 
					break;				
				case 'email' :		// 이메일 체크
						if(!isEmailCheck(f.value)) {
							var ReturnErrorMsg = msg+"는 이메일 형태로 구성되어야 합니다.";
							return ErrorMsg(f,ReturnErrorMsg,notfocus);
						}						
					break;				
				case 'hangul' :		// 한글 체크
						if(!isHangulCheck(f.value)) {
							var ReturnErrorMsg = msg+"는 한글로만 구성되어야 합니다.";
							return ErrorMsg(f,ReturnErrorMsg,notfocus);
						}
					break;
				default :
					break;
			}
		}
	}

	return true;
}

// 숫자로만 구성되어있는지를 검사한다.
function isNumberCheck(value) 
{
	var pattern = /^[0-9]+$/;
	return (pattern.test(value)) ? true : false;
}

function isEmailCheck(value)
{
	var pattern = /^[_A-Za-z0-9-\.]+@[A-Za-z0-9-_\.]+\.[A-Za-z0-9]+$/;
	return (pattern.test(value)) ? true : false;
}

function isHangulCheck(value)
{
	var temp_check = "";
	var pattern = /^[가-힝]/;

	for(var i=0; i<value.length; i++)
	{
		temp_check = value.substring(i,i+1);
		
		if(pattern.test(temp_check) == false) {
			return false;
		}
	}

	return true;
}

// 에러메세지를 리턴한다.
function ErrorMsg(f,ReturnErrorMsg,NotFocus)
{
	alert(ReturnErrorMsg);
	if(NotFocus == null) {
		f.focus();
	}
	return false;
}