/**
 * (1) JS Infomation
 *   - TLEJavaScript (http://kldp.net/projects/tle)
 *   - Online Manual : http://javacan.madvirus.net/main/content/read.tle?contentId=111
 *   - nona / (c)netville / 2007.12.10.
 *
 * (2) Exercise
 *   <script type="text/javascript">
 *     var formName = document.formName;
 *     var checker = new FormChecker(formName);
 *     checker.checkRequired('title', '제목을 입력해 주십시오.', true);
 *     checker.checkMaxLengthByte('title', 50, '제목은 50자 (한글 25자) 이하입니다.', true);
 *
 *   function goAction() {
 *     ...
 *     if (!checker.validate()) return;  // 실제적으로 Validation 체크하는 부분 (true/false)
 *     formName.submit();
 *   }
 *  </script>
 *
 * (3) Function List
 *   - checkRequired(fieldName, errorMessage, focus)
 *     : 값을 입력했는지의 여부를 검사한다.
 *   - checkMaxLength(fieldName, maxLength, errorMessage, focus)
 *     : 값의 길이가 maxLength보다 작거나 같은지 검사한다.
 *   - checkMaxLengthByte(fieldName, maxLength, errorMessage, focus)
 *     : 값의 바이트 길이가 maxLength보다 작거나 같은지 검사한다.
 *   - checkEqualsLength(fieldName, equalsLength, errorMessage, focus)
 *     : 값의 길이가 equalsLength와 같은지 검사한다.
 *   - checkEqualsLengthByte(fieldName, equalsLength, errorMessage, focus)
 *     : 값의 바이트 길이가 equalsLength와 같은지 검사한다.
 *   - checkMinLength(fieldName, minLength, errorMessage, focus)
 *     : 값의 길이가 minLength보다 크거나 같은지 검사한다.
 *   - checkMinLengthByte(fieldName, minLength, errorMessage, focus)
 *     : 값의 바이트 길이가 minLength보다 크거나 같은지 검사한다.
 *   - checkAlphaNum(fieldName, errorMessage, focus)
 *     : 값이 알파벳과 0~9사이의 숫자만 포함하는 지 검사한다.
 *   - checkBigAlphaNum(fieldName, errorMessage, focus)
 *     : 값이 알파벳대문자와 0~9사이의 숫자만 포함하는 지 검사한다.
 *   - checkOnlyNumber(fieldName, errorMessage, focus)
 *     : 값이 0~9 사이의 문자만 포함하는 지 검사한다.
 *   - checkDecimal(fieldName, errorMessage, focus)
  *     : 값이 0~9 사이의 문자와 - 포함하는 지 검사한다.
 *   - checkTelNum(fieldName, errorMessage, focus)
 *     : 값이 숫자인지 검사한다.
 *   - checkEmail(fieldName, errorMessage, focus)
 *     : 값이 올바른 이메일 주소인지 검사한다.
 *   - checkSelected(fieldName, firstIdx, errorMessage, focus)
 *     : <select>에서 선택한 옵션의 인덱스가 firstIdx보다 크거나 같은 지 검사한다.
 *   - checkAtLeastOneChecked(fieldName, errorMessage, focus)
 *     : checkbox나 radio 입력 요소가 최소한 1개 이상 선택됐는지 검사한다.
 *   - checkRegex(fieldName, regex, errorMessage, focus)
 *     : 값이 정규표현식에 해당하는 지 검사한다.
 *   - checkPassword(idFieldName, pwdFieldName, errorMessage, focus)
 *     : 값이 비밀번호 형식에 맞는지 검사한다.
 *
 * (4) History (자유롭게 수정하되 수정사항 발생시 기록 해 주세요.)
 *   - 2007.12.10. / nona / TLEJavaScript에 상단 주석 추가
 *   - 금지어 확인 추가. checkForbiddenWord 메서드 추가됨.
 *   - 에러메시지가 존재할때만 출력하도록 수정.
 *   - 2008.03.12. / nona / RequiredValidator.prototype.validate trim 처리 추가
 *   - 2008.04.03. / kkyaaa / 비밀번호 validation 처리 추가 
 */

// TLEJavaScript Original {s} =====================================================================
function FormChecker(checkForm) {
	this.checkForm = checkForm;
	this.validatorList = new Array();
}
FormChecker.prototype.checkRequired = function(fieldName, errorMessage, focus) {
	this.validatorList.push(new RequiredValidator(this.checkForm, fieldName, errorMessage, focus));
}
FormChecker.prototype.checkForbiddenWord = function(fieldName, focus) {
	this.validatorList.push(new ForbiddenwordValidator(this.checkForm, fieldName, focus));
}
FormChecker.prototype.checkMaxLength = function(fieldName, maxLength, errorMessage, focus) {
	this.validatorList.push(new MaxLengthValidator(this.checkForm, fieldName, maxLength, errorMessage, focus));
}

FormChecker.prototype.checkMaxLengthByte = function(fieldName, maxLength, errorMessage, focus) {
	this.validatorList.push(new MaxLengthByteValidator(this.checkForm, fieldName, maxLength, errorMessage, focus));
}

FormChecker.prototype.checkEqualsLength = function(fieldName, equalsLength, errorMessage, focus) {
    this.validatorList.push(new EqualsLengthValidator(this.checkForm, fieldName, equalsLength, errorMessage, focus));
}

FormChecker.prototype.checkEqualsLengthByte = function(fieldName, equalsLength, errorMessage, focus) {
    this.validatorList.push(new EqualsLengthByteValidator(this.checkForm, fieldName, equalsLength, errorMessage, focus));
}

FormChecker.prototype.checkMinLength = function(fieldName, minLength, errorMessage, focus) {
	this.validatorList.push(new MinLengthValidator(this.checkForm, fieldName, minLength, errorMessage, focus));
}
FormChecker.prototype.checkMinLengthByte = function(fieldName, minLength, errorMessage, focus) {
	this.validatorList.push(new MinLengthByteValidator(this.checkForm, fieldName, minLength, errorMessage, focus));
}

FormChecker.prototype.checkRegex = function(fieldName, regex, errorMessage, focus) {
	this.validatorList.push(
		new RegexValidator(this.checkForm, fieldName, regex, errorMessage, focus));
}

FormChecker.prototype.checkAlphaNum = function(fieldName, errorMessage, focus) {
	this.validatorList.push(
		new RegexValidator(this.checkForm, fieldName,
			/^[a-zA-Z0-9]+$/, errorMessage, focus));
}

FormChecker.prototype.checkBigAlphaNum = function(fieldName, errorMessage, focus) {
    this.validatorList.push(
        new RegexValidator(this.checkForm, fieldName,
            /^[A-Z0-9]+$/, errorMessage, focus));
}

FormChecker.prototype.checkOnlyNumber = function(fieldName, errorMessage, focus) {
	this.validatorList.push(
		new RegexValidator(this.checkForm, fieldName,
			/^[0-9]+$/, errorMessage, focus));
}

FormChecker.prototype.checkDecimal = function(fieldName, errorMessage, focus) {
	this.validatorList.push(
		new RegexValidator(this.checkForm, fieldName,
			/^(\-)?[0-9]*(\.[0-9]*)?$/, errorMessage, focus));
}

FormChecker.prototype.checkTelNum = function(fieldName, errorMessage, focus) {
	this.validatorList.push(
		new RegexValidator(this.checkForm, fieldName,
			/^[0-9\-]+$/, errorMessage, focus));
}

FormChecker.prototype.checkEmail = function(fieldName, errorMessage, focus) {
	this.validatorList.push(
		new RegexValidator(this.checkForm, fieldName,
			/^((\w|[\-\.])+)@((\w|[\-\.])+)\.([A-Za-z]+)$/, errorMessage, focus));
}

FormChecker.prototype.checkSelected = function(fieldName, firstIdx, errorMessage, focus) {
	this.validatorList.push(new SelectionValidator(this.checkForm, fieldName, firstIdx, errorMessage, focus));
}

FormChecker.prototype.checkAtLeastOneChecked = function(fieldName, errorMessage, focus) {
	this.validatorList.push(new AtLeastOneCheckValidator(this.checkForm, fieldName, errorMessage, focus));
}

FormChecker.prototype.checkPassword = function(idFieldName, fieldName, errorMessage, focus, isCheckSpecialChar) {
    this.validatorList.push(new PasswordValidator(this.checkForm, idFieldName, fieldName, errorMessage, focus, isCheckSpecialChar));
}

FormChecker.prototype.validate = function() {
	for (vali = 0 ; vali < this.validatorList.length ; vali ++ ) {
		validator = this.validatorList[vali];
		if (validator.validate() == false) {
			if(validator.getErrorMessage()) {
				alert(validator.getErrorMessage());
			}
			if (validator.isFocus() == true) {
				this.checkForm[validator.getFieldName()].focus();
			}
			return false;
		}
	}
	return true;
}

FormChecker.prototype.getForm = function() {
	return this.checkForm;
}


// Validator is base class of all validators
function Vaildator() {
}
Vaildator.prototype.getFieldName = function() {
	return this.fieldName;
}
Vaildator.prototype.getErrorMessage = function() {
	return this.errorMessage;
}
Vaildator.prototype.isFocus = function() {
	return this.focus;
}

// required validator
function RequiredValidator(form, fieldName, errorMessage, focus) {
	this.form = form;
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.focus = focus;
}
RequiredValidator.prototype = new Vaildator;
RequiredValidator.prototype.validate = function() {
	var result = false;
	var value = (this.form[this.fieldName].value).replace(/(^\s*)|(\s*$)/g, "");
	if(value != '') result = true;
	return result;
}

function ForbiddenwordValidator(form, fieldName, focus) {
	this.form = form;
	this.fieldName = fieldName;
	this.errorMessage = '';
	this.focus = focus;
}
ForbiddenwordValidator.prototype = new Vaildator;
ForbiddenwordValidator.prototype.validate = function() {
	return checkForbiddenWord(this.form[this.fieldName].value);
}

// max length validator
function MaxLengthValidator(form, fieldName, maxLength, errorMessage, focus) {
	this.form = form;
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.focus = focus;
	this.maxLength = maxLength;
}
MaxLengthValidator.prototype = new Vaildator;
MaxLengthValidator.prototype.validate = function() {
	return this.form[this.fieldName].value.length <= this.maxLength;
}

// max length(byte) validator
function MaxLengthByteValidator(form, fieldName, maxLength, errorMessage, focus) {
	this.form = form;
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.focus = focus;
	this.maxLength = maxLength;
}
MaxLengthByteValidator.prototype = new Vaildator;
MaxLengthByteValidator.prototype.validate = function() {
	str = this.form[this.fieldName].value;
	return(str.length+(escape(str)+"%u").match(/%u/g).length-1) <= this.maxLength;
}

// equals length validator
function EqualsLengthValidator(form, fieldName, equalsLength, errorMessage, focus) {
    this.form = form;
    this.fieldName = fieldName;
    this.errorMessage = errorMessage;
    this.focus = focus;
    this.equalsLength = equalsLength;
}
EqualsLengthValidator.prototype = new Vaildator;
EqualsLengthValidator.prototype.validate = function() {
    return this.form[this.fieldName].value.length == this.equalsLength;
}

// equals length(byte) validator
function EqualsLengthByteValidator(form, fieldName, equalsLength, errorMessage, focus) {
    this.form = form;
    this.fieldName = fieldName;
    this.errorMessage = errorMessage;
    this.focus = focus;
    this.equalsLength = equalsLength;
}
EqualsLengthByteValidator.prototype = new Vaildator;
EqualsLengthByteValidator.prototype.validate = function() {
	str = this.form[this.fieldName].value;
	return(str.length+(escape(str)+"%u").match(/%u/g).length-1) == this.equalsLength;
}

// min length validator
function MinLengthValidator(form, fieldName, minLength, errorMessage, focus) {
	this.form = form;
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.focus = focus;
	this.minLength = minLength;
}
MinLengthValidator.prototype = new Vaildator;
MinLengthValidator.prototype.validate = function() {
	return this.form[this.fieldName].value.length >= this.minLength;
}

// min length(byte) validator
function MinLengthByteValidator(form, fieldName, minLength, errorMessage, focus) {
	this.form = form;
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.focus = focus;
	this.minLength = minLength;
}
MinLengthByteValidator.prototype = new Vaildator;
MinLengthByteValidator.prototype.validate = function() {
	str = this.form[this.fieldName].value;
	return(str.length+(escape(str)+"%u").match(/%u/g).length-1) >= this.minLength;
}

// regex pattern validator
function RegexValidator(form, fieldName, regex, errorMessage, focus) {
	this.form = form;
	this.fieldName = fieldName;
	this.regex = regex;
	this.errorMessage = errorMessage;
	this.focus = focus;
}
RegexValidator.prototype = new Vaildator;
RegexValidator.prototype.validate = function() {
	var str = this.form[this.fieldName].value;
	if (str.length == 0) return true;
	return str.search(this.regex) != -1;
}

// check selected
function SelectionValidator(form, fieldName, firstIdx, errorMessage, focus) {
	this.form = form;
	this.fieldName = fieldName;
	this.firstIdx = firstIdx;
	this.errorMessage = errorMessage;
	this.focus = focus;
}
SelectionValidator.prototype = new Vaildator;
SelectionValidator.prototype.validate = function() {
	var idx = this.form[this.fieldName].selectedIndex;
	return idx >= this.firstIdx;
}

// check checkbox checked
function AtLeastOneCheckValidator(form, fieldName, errorMessage, focus) {
	this.form = form;
	this.fieldName = fieldName;
	this.errorMessage = errorMessage;
	this.focus = focus;
}
AtLeastOneCheckValidator.prototype = new Vaildator;
AtLeastOneCheckValidator.prototype.validate = function() {
	ele = this.form[this.fieldName];
	if (typeof(ele[0]) != "undefined") {
		// 2~
		for (var idxe = 0 ; idxe < ele.length ; idxe++) {
			if (ele[idxe].checked == true) {
				return true;
			}
		}
		return false;
	} else {
		// only 1
		return ele.checked == true;
	}
}

// password validator
function PasswordValidator(form, idFieldName, fieldName, errorMessage, focus, isCheckSpecialChar) {
    this.form = form;
    this.idFieldName = idFieldName;
    this.fieldName = fieldName;
    this.errorMessage = errorMessage;
    this.focus = focus;
    
    if(isCheckSpecialChar != false)
    	isCheckSpecialChar = true;
    this.isCheckSpecialChar = isCheckSpecialChar;
}
PasswordValidator.prototype = new Vaildator;
PasswordValidator.prototype.validate = function() {
    var id = this.form[this.idFieldName].value;
    var pwd = this.form[this.fieldName].value;

    // 6자 미만 체크
    if (pwd.length < 6) {
        this.errorMessage = "비밀번호를  6자 이상 입력하세요.   ";
        return false;
    }

    // 아이디와 같은 비밀번호 체크
    if (id.indexOf(pwd) != -1 || pwd.indexOf(id) != -1) {
        this.errorMessage = "비밀번호를  아이디와 다르게  입력하세요.   ";
        return false;
    }
    
    // 유추 가능한 비밀번호 체크
    var names = new Array("%", "#", "?", "&", "enclean","carlog","zone","usedcar","newcar","review","club", "admin", "master", "dealer");
    
    for (var i = 0; i < names.length; i++) {
        if (pwd.toLowerCase().indexOf(names[i]) != -1) {
            this.errorMessage = "비밀번호에 \'"+names[i]+"\'을 사용할 수 없습니다.   ";
            return false;
        }
    }
    
    // 연속된 숫자 체크
    strNum1 = "01234567890";
    strNum2 = "09876543210";
    
    if (strNum1.indexOf(pwd) != -1) {
        this.errorMessage = "비밀번호에 연속된 숫자를 사용할 수 없습니다.   ";
        return false;
    }

    if (strNum2.indexOf(pwd) != -1) {
        this.errorMessage = "비밀번호에 연속된 숫자를 사용할 수 없습니다.   ";
        return false;
    }

    for(var i=0; i<=strNum1.length-4; i++){
        tmpStr=strNum1.substring(i,i+4);
        if (pwd.indexOf(tmpStr)>=0){
            this.errorMessage = "연속된 4자리의 숫자가 포함된 비밀번호는 사용할 수 없습니다.   ";
            return false;
        }
    }

    for(var i=0; i<=strNum2.length-4; i++){
        tmpStr=strNum2.substring(i,i+4);
        if (pwd.indexOf(tmpStr)>=0){
            this.errorMessage = "연속된 4자리의 숫자가 포함된 비밀번호는 사용할 수 없습니다.   ";
            return false;
        }
    }
    
    // 연속된 문자 체크
    strString1 = "abcdefghijklmnopqrstuvwxyz";
    strString2 = "zyxwvutsrqponmlkjihgfedcba";
            
    if (strString1.indexOf(pwd.toLowerCase()) != -1) {
        this.errorMessage = "비밀번호에 연속된 문자를 사용할 수 없습니다.   ";
        return false;
    }
    
    if (strString2.indexOf(pwd.toLowerCase()) != -1) {
        this.errorMessage = "비밀번호에 연속된 문자를 사용할 수 없습니다.   ";
        return false;
    }        

    for(var i=0; i<=strString1.length-4; i++){
        tmpStr=strString1.substring(i,i+4);
        if (pwd.indexOf(tmpStr)>=0){
            this.errorMessage = "연속된 4자리의 문자가 포함된 비밀번호는 사용할 수 없습니다.   ";
            return false;
        }
    }

    for(var i=0; i<=strString2.length-4; i++){
        tmpStr=strString2.substring(i,i+4);
        if (pwd.indexOf(tmpStr)>=0){
            this.errorMessage = "연속된 4자리의 문자가 포함된 비밀번호는 사용할 수 없습니다.   ";
            return false;
        }
    }

    // 한문자로 이루어진 비밀번호 체크
    var ch = pwd.charAt(0);
    re = new RegExp("^[" + ch + "]+$");
    if (pwd.match(re)) {
        this.errorMessage = "비밀번호는 숫자,문자,특수문자 조합으로 입력하세요.   ";                
        return false;
    }       

    // 숫자 문자 특수문자 조합 체크
    var existNum = false;
    var existCh = false;
    var existSCh = false;
    var reNum = /^[0-9]+$/;
    var reCh  = /^[A-z]+$/;
    var reSCh = /[~!@\#$%^&*\()\-=+_]/gi;

    for (var i=0; i < pwd.length; i++) {
        var tmpCh = pwd.charAt(i);

        if (tmpCh.match(reNum)) { existNum = true; }
        if (tmpCh.match(reCh)) { existCh = true; }
        if (tmpCh.match(reSCh)) { existSCh = true; }
        
        if(!this.isCheckSpecialChar)
        	existSCh = true;

        if (existNum == true && existCh == true && existSCh == true) {
            break;
        }
    }

    if (existNum == false || existCh == false || existSCh == false) {
    	if(this.isCheckSpecialChar)
	        this.errorMessage = "비밀번호는 숫자,문자,특수문자 조합으로 입력하세요.";
	    else
	    	this.errorMessage = "비밀번호는 숫자,문자 조합으로 입력하세요.";
        return false;
    }
    
    return true;   
}
// TLEJavaScript Original {e} =====================================================================
