
var V;

(function() { 
	if (V == null)
        V = new Object();
	V.TRIM_FLAG = true;
	V.OBJ = null;
	V.VALUE = null;
	V.MESSAGE = null;
	V.NULL_CHECK = 1 << 0;
	V.NULL_ERROR_MSG = "を入力してください。";
	
	/*
	* 전처리 함수. 에러 메세지 값 세팅,체크할 값 세팅,Null 체크,Trim 처리
	*/
	V.preProcess = function(obj,title,defualtMsg,option,msg){
		V.OBJ = obj;
		V.MESSAGE = V.makeMessage(title,defualtMsg,msg);
		// Value Check
		V.VALUE = V.getValue(V.OBJ);
		if( V.TRIM_FLAG ){
			V.VALUE = V.trim(V.VALUE);
			V.OBJ.value = V.VALUE;
		}
		// Null Check
		if( (option & V.NULL_CHECK) == V.NULL_CHECK ){
			if( V.VALUE == null || V.VALUE == "" )
				return V.postProcess(false);
		}
		return true;
	}
	/*
	* 후처리 함수. false일경우 에러 메세지 호출,객제 focus할당
	*/
	V.postProcess = function(flag){
		if( flag )
		{
			return true;
		}else{
			alert(V.MESSAGE);
			if( V.OBJ )
			{
				V.OBJ.select();
				V.OBJ.focus();
			}
			return false;
		}
	};
	/*
		return to removed character : half-word , full-word space  
		trim(" asb   ") -> "asb"
		trim("?asb ") -> "asb" 
	*/
	V.trim = function(val){
		if( val == null || val == "" ) return "";
		for( var i=0 ; i<val.length && ( val.charAt(i) ==' ' || val.charAt(i) == '　' ) ; i++ ) ;
		for( var j=val.length ; j>0 && ( val.charAt(j-1) ==' ' || val.charAt(j-1) == '　' ) ; j-- ) ;
		return i>=j ? "" : val.substring(i,j);
	};
	/**
	 * HTML 안에 있는 객체(INPUT, SELECT 등)의 value 를 얻는다.
	 *
	 * <예>
	 *	일반적인 객체들 :
	 *		var strValue = getValue(document.form1.input1);
	 *		alert(strValue);
	 *
	 *	SELECT 객체 :
	 *		var strValue = getValue(document.form1.select1);
	 *		alert(strValue);
	 *
	 *	MULTIPLE 속성이 있는 SELECT 객체 :
	 *		var aValue = getValue(docment.form1.select2);
	 *		for (var i = 0; i < aValue.length; i++) {
	 *			alert(aValue[i]);
	 *		}
	 *
	 * @param obj HTML 객체
	 * @return 객체의 value 속성 값
	 * 
	 */
	V.getValue = function(obj) {
	   var rValue = null;
	   switch(obj.tagName.toUpperCase()) {
	      case "SELECT" :
	         if(obj.multiple) {
	            var aValues = new Array();
	            var x = 0;
	            for(var i = 0; i < obj.length; i++) {
	               if(obj.options[i].selected) {
	                  aValues[x++] = obj.options[i].value;
	               }
	            }
	            return aValues;
	         }
	         else {
	            if(obj.selectedIndex >= 0) {
	               rValue = obj.options[obj.selectedIndex].value;
	            }
		        if(rValue == "none") {
	    	    	rValue = null;
	        	}
	         }
	         break;
	      default : 
	        rValue = obj.value;
	         break;
	   }
	   if(rValue == null || rValue == "undefined") {
	      rValue = "";
	   }
	   return rValue;
	};
	
	V.makeMessage = function(title,defaultMsg,msg){
		return (msg == null || msg == "") ? title+defaultMsg : msg;
	};
	V.isConsistChar = function(val,isNum,isAlpha,specialChar){
		alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		num = "0123456789";
		if( val == null ) return false;
		var checkString="";
		
		if( isNum )
			checkString += num;
		if( isAlpha )
			checkString += alpha;
		if( specialChar != null )
			checkString += specialChar;
		for( i=0 ; i < val.length ; i++ )
			if( checkString.indexOf( val.charAt( i ) ) == -1 )
				return false;
		
		return true;
	}
	
	/*******************************************************************************/
	/*
		Validation Check 함수.
		함수 만드는 방법.
		1. V.preProcess 호출
		2. Check 로직
		3. V.postProcess 호출
	*/
	/*******************************************************************************/
	V.checkNull = function(obj,title,option,msg){
		if( (option & V.NULL_CHECK) != V.NULL_CHECK )
			option |= V.NULL_CHECK;
		return V.preProcess(obj,title,"を入力してください。",option,msg);
	}
	/*
		check if is number
		0 -> true	23.43 -> true -23.23 -> true 23 -> true
		asdd -> false #$% -> false 134s -> false
	*/	
	V.checkNumber = function(obj,title,option,msg){
		if( !V.preProcess(obj,title,"は数字で入力してください。",option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		
		return V.postProcess(!isNaN(V.VALUE));
	};
	/*
	 check if is positive number include in float number
	 0 -> true
	 -12 -> false
	 34 -> true
	 34.23 -> true
	*/
	V.checkPositiveNumber = function(obj,title,option,msg){
		if( !V.preProcess(obj,title,"に0以上の値を入力してください。",option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		if( isNaN(V.VALUE) )
			return V.postProcess(false);
		return V.postProcess(V.VALUE >= 0);
	};
	/*
	 check if is negative number include in float number
	 0 -> true
	 -12 -> true
	 34 -> false
	 34.23 -> false
	*/
	V.checkNagativeNumber = function(obj,title,option,msg){
		if( !V.preProcess(obj,title,"に0以下の値を入力してください。",option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		
		if( isNaN(V.VALUE) )
			return V.postProcess(false);
		return V.postProcess(V.VALUE <= 0);
	}
	/*
	 check if is integer number
	 0 -> true
	 -12 -> true
	 34 -> true
	 34.23 -> false
	*/
	V.checkInteger = function(obj,title,option,msg){
		if( !V.preProcess(obj,title,"に正数を入力してください。",option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		
		if( isNaN(V.VALUE) || (V.VALUE.length > 1 && V.VALUE.charAt(0) == '0') )
			return V.postProcess(false);
		if( V.VALUE.length > 2 && V.VALUE.substring(0,2) == "-0" )
			return V.postProcess(false);
					
		return V.postProcess( V.VALUE.indexOf('.') == -1);
	}
	/* 
	   0~9 and a-Z : Not include '.' '-' 
	   2343ASBfe -> true
	   23.34 -> false
	   -234 -> false
 	*/
	V.checkNumberAndAlpah = function(obj,title,option,msg){
		if( !V.preProcess(obj,title,"に数字または英文字を入力してください。",option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		
		for( i=0 ; i<V.VALUE.length ; i++ )
		{
			ch = V.VALUE.charAt(i);
			if( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <='Z') || (ch >='0' && ch <='9') )
				continue;
			else
				return V.postProcess(false);
		} 
		return true; 
	}
	/*
		check scale
		checkScaleNumber("123.45",4) -> true
		checkScaleNumber("123.45",3) -> true
		checkScaleNumber("123.45",2) -> false
		checkScaleNumber("123",4) -> true
		checkScaleNumber("123",3) -> true
		checkScaleNumber("123",2) -> false
	*/
	V.checkScaleNumber = function(obj,size,title,option,msg){
		if( !V.preProcess(obj,title,"に"+size+"桁以上を入力してください。 ",option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		
		if( isNaN(V.VALUE) )
			return V.postProcess(false);
		pos = V.VALUE.indexOf('.');
		if( pos == -1 )
			return V.postProcess(V.VALUE.length <= size);
		else
			return V.postProcess(pos <= size);
	}
	/*
		checkLength("abcde",6,null) -> false
		checkLength("abcde",5,null) -> true
		checkLength("abcde",4,null) -> true
		checkLength("abcde",6,3) -> false
		checkLength("abcde",5,3) -> true
		checkLength("ab",4,3) -> false
		checkLength("ab",4,2) -> true
		checkLength("ab",null,3) -> false
		checkLength("ab",null,2) -> true
	*/
	V.checkLength = function(obj,minLength,maxLength,title,option,msg){
		var defaultMsg = "は ";
		if( minLength != null )
			defaultMsg += minLength+"以上";
		if( maxLength != null )
			defaultMsg += maxLength+"以下";
		defaultMsg += "の文字を入力してください。";
			
		if( !V.preProcess(obj,title,defaultMsg,option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		
		var result = true;
		if( maxLength != null )
			if( V.VALUE.length > maxLength )
				result = false;
		if( minLength != null )
			if( V.VALUE.length < minLength )
				result = false;
		return V.postProcess(result);
	}
	/*
		checkSize("999",999,null) -> false
		checkSize("999",1000,null) -> false
		checkSize("999",998,null) -> true
		checkSize("0",998,0) -> false
		checkSize("1",998,0) -> true
		checkSize("1",null,0) -> true
	*/
	V.checkSize = function(obj,minSize,maxSize,title,option,msg){
		var defaultMsg = "は";
		if( maxSize != null )
			defaultMsg += maxSize+"以下";
		if( minSize != null )
			defaultMsg += minSize+"以上";
		defaultMsg += "の数字を入力してください。";
		
		if( !V.preProcess(obj,title,defaultMsg,option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		
		var result = true;
		if( maxSize != null )
			if( V.VALUE > maxSize )
				result = false;
		if( minSize != null )
			if( V.VALUE < minSize )
				result = false;
		return V.postProcess(result);
	}
	// do not check for full-word character
	V.checkHasBlank = function(obj,title,option,msg){
		if( !V.preProcess(obj,title,"に空欄を入れないでください。",option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;

		return V.postProcess(V.VALUE.indexOf(" ") == -1);
	}
	/*
		check if first character is alphabet
		checkFirstAlphabet("a234") -> true
		checkFirstAlphabet("234adf") -> false
		checkFirstAlphabet("#asd") -> false
	*/
	V.checkFirstAlphabet =function(obj,title,option,msg){
		if( !V.preProcess(obj,title,"の最初の文字は英文字で入力してください。",option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		
		// if val is null , invalid data.
		if( V.VALUE == null ) return V.postProcess(false);
		var temp = V.VALUE.charAt(0);
	
		return V.postProcess(V.isConsistChar(temp,null,true,null));
	}
	/*
	* 영문자와 숫자만으로 구성되어 있는지 체크
	*  checkNumberAndAlphabet("1234한글") => false 
    *  checkNumberAndAlphabet("1234DFSAS") => true 
	*/
	V.checkNumberAndAlphabet = function(obj,title,option,msg){
		if( !V.preProcess(obj,title,"には英文字または数字だけ入力してください。",option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		
		return V.postProcess(V.isConsistChar(V.VALUE,true,true));
	}
	/*
		checkNumberAndHyphen("asdf-ssdf") -> false
		checkNumberAndHyphen("234-11") -> true
		checkNumberAndHyphen("23411") -> true
	*/
	V.checkNumberAndHyphen = function(obj,title,option,msg){
		if( !V.preProcess(obj,title,"は数字または'-'で入力してください。",option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		
//		return V.postProcess(V.isConsistChar(V.VALUE,true,false,"-") && V.VALUE.indexOf('-') != -1 );
		return V.postProcess(V.isConsistChar(V.VALUE,true,false,"-"));
	}
	/*
		checkNumberString("2342") -> true
		checkNumberString("2342.234") -> false
		checkNumberString("-2342") -> false
		checkNumberString("afd") -> false
	*/
	V.checkNumberString = function(obj,title,option,msg){
		if( !V.preProcess(obj,title,"は数字だけ入力してください。",option) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		
		return V.postProcess(V.isConsistChar(V.VALUE,true,false,null));
	}
	/** usage : isHyphenPatten("345-2343","xxx-xxxx") ***/
	V.checkNumberHyphenPatten = function(obj,patten,title,option,msg){
		if( !V.preProcess(obj,title,"は数字で"+patten+"のパターンで入力してください。",option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		
		if( patten == null ) return true;
		if( V.VALUE.length != patten.length ) return V.postProcess(false);
		
		for( i=0 ; i<patten.length ; i++ )
		{
			ch = patten.charAt(i);
			if( ch == '-' )
				if( V.VALUE.charAt(i) != '-' )
					return V.postProcess(false);
				else
					continue;
			else if( ch == 'x' )
				if( V.VALUE.charAt(i) >='0' && V.VALUE.charAt(i) <='9' )		
					continue;
				else
					return V.postProcess(false);
			else
				return V.postProcess(false);
		}
		return true;
	}
	/*
	* URL 문자인지 검사
	*/
	V.checkUrlStr =	function(obj,title,option,msg){
		if( !V.preProcess(obj,title,"はURL形式で入力してください。",option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		
		return V.postProcess(
				( V.VALUE.length > 7 && V.VALUE.toLowerCase().substring(0,7) == "http://" ) ||
			   	( V.VALUE.length > 8 && V.VALUE.toLowerCase().substring(0,8) == "https://" )
			   	);
	}
	/*
	* Email 형식에 맞는지 검사
	*/
	V.checkEmail = function(obj,title,option,msg){
		if( !V.preProcess(obj,title,"はEmail形式で入力してください。",option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(V.VALUE))
			return true;
		
		return V.postProcess(false);
	}
	V.checkHasChecked = function(obj,msg){
		if( typeof(obj.length) == "undefined" )
		{
			if( obj.checked )
				return true;
			else
			{
				alert(msg);
				return false;
			}
		}else{
			for( var i=0 ; i<obj.length ; i++ )
				if( obj[i].checked )
					return true;
			alert(msg);
			return false;
		}
	}
	/*
	* 사용해서는 안되는 문자를 사용했는지 체크 하는 함수
	* invalidCharList에는 사용해서는 안되는 문자 List를 넣는다.
	*  checkInvalidChar("1234%^&","%") => false 
    *  checkInvalidChar("1234%^&","!@") => true 
	*/
	V.checkInvalidChar = function(obj,invalidCharList,title,option,msg){
		if( !V.preProcess(obj,title,"には"+invalidCharList+"は使えないです。",option,msg) )
			return false;
		if( V.VALUE == null || V.VALUE == "" ) return true;
		
		for( i=0 ; i<invalidCharList.length ; i++ )
		{
			if( V.VALUE.indexOf(invalidCharList.charAt(i)) != -1 )
				return V.postProcess(false);
		}
		return true;
	}

	
}) ();


