/* 예제
 * - hone 사용시
 * <input id="test" name="test" hone:validator="checkValidator" tkLang="en/ko" tkTitle="title명" tkMaxLen="입력최대값" tkOnlyKr tkNoKr tkIsEmail tkOnlyNum />
 * 
 * - formValidator(form) 사용시
 * <input id="test" name="test" tkLang="en/ko" tkTitle="title명" tkMaxLen="입력최대값" tkOnlyKr tkNoKr tkIsEmail tkOnlyNum tkNotNull />
 * tkLang : 메세지 선택 en : 영문, ko : 국문
 * tkTitle : 입력란의 Title
 * tkOnlyKr : 국문만 입력 가능
 * tkNoKr : 국문 입력 불가
 * tkIsEmail : 이메일 주소 체크
 * tkOnlyNum : 숫자인지 체크
 * tkOnlyImg : 이미지 파일만 업로드 가능
 * 
 * null Check은 hone:required를 사용
 */

//UTF-8용으로 한글자에 3바이트 처리
function getLength(string){
	var ch;
	var length = 0;
	var isKorean = "N";
	var isEnglish = "N";
	var isSpecial = "N";
	var isNumber = "N";
	var isEtc = "N";

	for(var k=0; k<string.length; k++){
		ch = string.charAt(k);

		if( ch >= 'ㄱ' && ch <= '힣' ){
			length += 3;
			isKorean = "Y";
		}
		else if ( ch > 128 ){
			length += 3;
			isSpecial = "Y";
		}
		else if(ch >= 'A' && ch <= 'z'){
			length++;
			isEnglish = "Y";
		}
		else if(ch >= '0' && ch <= '9'){
			length++;
			isNumber = "Y";
		}
		else{
			length++;
			isEtc = "Y";
		}
	}
	return length + "|" + isKorean + "|" + isEnglish + "|" + isSpecial + "|" + isNumber + "|" + isEtc;
}

function checkSpecialChar(value, lang) {
	var proccess = true;
	
	for (var i=0; i<value.length; i++) {
		ch_char = value.charAt(i);
		ch = ch_char.charCodeAt();

		if ( (ch >= 33 && ch <=47) || (ch >= 58 && ch <= 64) || (ch >= 91 && ch <= 96) || (ch >= 123 && ch <= 126) ) {
			proccess = false;
		}
	}
	
	if(!proccess) {
		if(lang == 'ko')
			alert("특수문자를 사용할 수 없습니다.");
		else
			alert("특수문자를 사용할 수 없습니다.");
	}
	
	return proccess;
}

function checkEmailAddress(value){
	var atPos = value.indexOf('@');            //"@"의 처음 위치
	var atLastPos = value.lastIndexOf('@');    //"@"의 마지막 위치
	var dotPos = value.indexOf('.');           //.의 위치
	var dotLastPos = value.lastIndexOf('.');   //.의 위치
	var spacePos = value.indexOf(' ');         //공백의 위치
	var commaPos = value.indexOf(',');         //,의 위치
	var eMailSize = value.length;              //메일주소의 길이 

	/*
	atPos == -1 : '@'기호가 없음
	atPos < 2: @기호 앞에 2문자 이상이 있어야 합니다. ww@w.com
	atPos != atLastPos: @기호는 하나만 존재해야 합니다.
	dotPos == -1 : '.'기호가 없음 
	dotPos < 4 : .의 위치로 . 앞에는 최소한 4문자 이상이 있어야합니다.
	spacePos > 0 : 공백이 없어야 함.
	commaPos > 0 : ','가 없어야 함.
	atPos + 1 > dotPos: ww@w.com   '@', '.'사이에 2문자이상 있어야 합니다.
	dotLastPos + 1 == eMailSize: ww@w.c, '.'이 마지막에 있으면 안됩니다.
	*/
	if (atPos == -1 ||
		atPos < 2 ||
		atPos != atLastPos ||
		dotPos == -1 ||
		dotPos < 4 ||
		spacePos > 0 ||
		commaPos > 0 || 
		atPos + 1 > dotPos ||
		dotLastPos + 1 == eMailSize){
		return false;
	}
	
	return true;
}

function getGetExtensionName(value){
	if(value == null || value.length == 0)
		return "";

	var dotLastPos = value.indexOf('.');
	var extension = "";

	if(dotLastPos == -1)
		return "";
	else
		extension = value.substring(dotLastPos+1, value.length);

	return extension;
}

function checkValidator(value, target){
	return tkValidator(target);
}

function tkValidator(target){
	var tkTitle = $(target).attr("tkTitle");
	var tkLang = $(target).attr("tkLang");
	var tkMaxLen = $(target).attr("tkMaxLen");
	var tkMinLen = $(target).attr("tkMinLen");
	var tkOnlyKr = $(target).attr("tkOnlyKr");
	var tkNoKr = $(target).attr("tkNoKr");
	var tkIsEmail = $(target).attr("tkIsEmail");
	var tkOnlyNum = $(target).attr("tkOnlyNum");
	var tkOnlyImg = $(target).attr("tkOnlyImg");
	var tkNotNull = $(target).attr("tkNotNull");
	var tkNotSpec = $(target).attr("tkNotSpec");

	var value = $(target).val().trim();

	var sReturn = getLength(value);
	var val = sReturn.split("|");

	//글자수 제한(MIN)
	if( tkMinLen != null && tkMinLen > 0 ){
		if(Number(val[0]) < Number(tkMinLen)){
			if(tkLang == 'ko')
				alert("["+tkTitle+"]란에 최소 글자수 이상 입력해 주세요.\n\n최소 입력 글자수 : " + tkMinLen + " Byte");
			else
				alert("["+tkTitle+"] It exceeds the limited characters.\n\nMinimum Input Characters : " + tkMinLen + " Byte");

			target.focus();
			return false;
		}
	}
	
	//글자수 제한(MAX)
	if( tkMaxLen != null && tkMaxLen > 0 ){
		if(Number(val[0]) > Number(tkMaxLen)){
			if(tkLang == 'ko')
				alert("["+tkTitle+"]란에 입력한 글자수가 제한 글자수를 넘었습니다.\n\n최대 입력 글자수 : " + tkMaxLen + " Byte");
			else
				alert("["+tkTitle+"] It exceeds the limited characters.\n\nMax Input Characters : " + tkMaxLen + " Byte");

			target.focus();
			return false;
		}
	}

	//한글사용 금지the Korean alphabet
	if( tkNoKr != null){
		if(val[1] == "Y"){
			if(tkLang == 'ko')
				alert("["+tkTitle+"]란은 한글은 입력할 수 없습니다.");
			else
				alert("["+tkTitle+"] column can not enter Hangul.");

			target.focus();
			return false;
		}
	}

	//한글/숫자만 사용
	if( tkOnlyKr != null){
		if(val[2] == "Y" || val[3] == "Y"){
			if(tkLang == 'ko')
				alert("["+tkTitle+"]란은 한글/숫자만 입력할 수 있습니다.");
			else
				alert("column ["+tkTitle+"] Hangul / numbers can be entered.");

			target.focus();
			return false;
		}
	}

	//이메일 주소 형식 확인
	if(tkIsEmail != null){
		if(!checkEmailAddress(value)){
			if(tkLang == 'ko')
				alert("["+tkTitle+"]란의 E-mail주소가 형식이 잘못되었습니다.");
			else
				alert("Please input an valid e-mail address.");

			target.focus();
			return false;
		}
	}

	//숫자만 입력 가능
	if(tkOnlyNum != null){
		if(val[1] == "Y" || val[2] == "Y" || val[3] == "Y" || val[5] == "Y"){
			if(tkLang == 'ko')
				alert("["+tkTitle+"]란은 숫자만 입력 가능합니다.");
			else
				alert("Enter the ["+tkTitle+"] column numbers are available.");

			target.focus();
			return false;
		}
	}

	if(tkOnlyImg != null){
		var ext = getGetExtensionName(value);
		var checkExt = "gif/jpg/jpeg/png/GIF/JPG/JPEG/PNG";
		if(ext == "" || checkExt.indexOf(ext) == -1){
			if(tkLang == 'ko')
				alert("["+tkTitle+"]란은 이미지만 업로드 가능합니다.");
			else
				alert("You can upload images to ["+tkTitle+"] column.");

			target.focus();
			return false;
		}
	}

	//필수입력값 체크
	if(tkNotNull != null){
		if(value == null || value.length == 0){
			if(tkLang == 'ko')
				alert("["+tkTitle+"]란은 필수 입력란입니다.");
			else
				alert("["+tkTitle+"] column is a required field.");

			target.focus();
			return false;
		}
	}
	
	return true;
}
