//필수처리
function val_Notnull(id, msg, focus) {
    var msg = msg_creater(id, msg, 'id');
        
    if(document.getElementById(id).value == "") {
        alert(msg);
        if(focus == true) {
            document.getElementById(id).focus();
        }
        return false;
    }
}

//한글처리
function val_Hangul(id, msg, focus) {
    var msg = msg_creater(id, msg, 'id');
    
    var str2 = document.getElementById(id).value;
    
    for(h=0; h<str2.length; h++){
        var g = str2.charCodeAt(h);
        if(!((0xAC00 <= g && g <= 0xD7A3) || (0x3131 <= g && g <= 0x318E))) {
            alert(msg);
            if(focus == true) {
                document.getElementById(id).focus();
            }
            return false;
        }
    }
}

//영문공백만 가능
function val_English(id, msg, focus) {
    var msg = msg_creater(id, msg, 'id');
    
    var txt = document.getElementById(id).value;
    
    var cnt = 0;
    for(i=0; i<txt.length; i++) {
        //65 ~ 90 : A ~ Z
        //97 ~ 122 : a ~ z
        //32 : 공백
        if((txt.charCodeAt(i) >= 65 && txt.charCodeAt(i) <= 90) || (txt.charCodeAt(i) >= 97 && txt.charCodeAt(i) <= 122) || txt.charCodeAt(i) == 32) {
            // ascii
        } else {
            // not ascii
            cnt++;
        }
    }
    
    if(cnt != 0) {
        alert(msg);
        if(focus == true) {
            document.getElementById(id).focus();
        }
        return false;
    }
}

//오로지 영문만 가능
function val_EnglishNotspace(id, msg, focus) {
    var msg = msg_creater(id, msg, 'id');
    
    var txt = document.getElementById(id).value;
    
    var cnt = 0;
    for(i=0; i<txt.length; i++) {
        //65 ~ 90 : A ~ Z
        //97 ~ 122 : a ~ z
        //32 : 공백
        if((txt.charCodeAt(i) >= 65 && txt.charCodeAt(i) <= 90) || (txt.charCodeAt(i) >= 97 && txt.charCodeAt(i) <= 122)) {
            // ascii
        } else {
            // not ascii
            cnt++;
        }
    }
    
    if(cnt != 0) {
        alert(msg);
        if(focus == true) {
            document.getElementById(id).focus();
        }
        return false;
    }
}

//영문(공백안됨)과숫자체크
//if(val_EnglishNotspaceNumber('ordersName', '을 영문과 숫자로만 입력하여 주십시오.,', true) == false) { return false; }
function val_EnglishNotspaceNumber(id, msg, focus) {
    var msg = msg_creater(id, msg, 'id');
    
    var txt = document.getElementById(id).value;
    
    var cnt = 0;
    for(i=0; i<txt.length; i++) {		
        //65 ~ 90 : A ~ Z
        //97 ~ 122 : a ~ z
        //32 : 공백
        if((txt.charCodeAt(i) >= 65 && txt.charCodeAt(i) <= 90) || (txt.charCodeAt(i) >= 97 && txt.charCodeAt(i) <= 122) || (txt.charCodeAt(i) >= 48 && txt.charCodeAt(i) <= 57) || txt.charCodeAt(i) == 95) {
            // ascii
        } else {
            // not ascii
            cnt++;
        }
    }
    
    if(cnt != 0) {
        alert(msg);
        if(focus == true) {
            document.getElementById(id).focus();
        }
        return false;
    }
}

//영문(공백안됨)과'-'와숫자체크
function val_EnglishNotspaceDashNumber(id, msg, focus) {
    var msg = msg_creater(id, msg, 'id');
    
    var txt = document.getElementById(id).value;
    
    var cnt = 0;
    for(i=0; i<txt.length; i++) {
        //65 ~ 90 : A ~ Z
        //97 ~ 122 : a ~ z
        //32 : 공백
        //95 : 언더바 _
        if((txt.charCodeAt(i) >= 65 && txt.charCodeAt(i) <= 90) || (txt.charCodeAt(i) >= 97 && txt.charCodeAt(i) <= 122) || (txt.charCodeAt(i) >= 48 && txt.charCodeAt(i) <= 57) || txt.charCodeAt(i) == 45 || txt.charCodeAt(i) == 95) {
            // ascii
        } else {
            // not ascii
            cnt++;
        }
    }
    
    if(cnt != 0) {
        alert(msg);
        if(focus == true) {
            document.getElementById(id).focus();
        }
        return false;
    }
}

//영문(공백안됨)과 숫자, '.' 체크
function val_EnglishNotspaceNumberDot(id, msg, focus) {
    var msg = msg_creater(id, msg, 'id');
    
    var txt = document.getElementById(id).value;
    
    var cnt = 0;
    for(i=0; i<txt.length; i++) {
        //65 ~ 90 : A ~ Z
        //97 ~ 122 : a ~ z
        //32 : 공백
        if((txt.charCodeAt(i) >= 65 && txt.charCodeAt(i) <= 90) || (txt.charCodeAt(i) >= 97 && txt.charCodeAt(i) <= 122) || (txt.charCodeAt(i) >= 48 && txt.charCodeAt(i) <= 57) || txt.charCodeAt(i) == 46) {
            // ascii
        } else {
            // not ascii
            cnt++;
        }
    }
    
    if(cnt != 0) {
        alert(msg);
        if(focus == true) {
            document.getElementById(id).focus();
        }
        return false;
    }
}

//영문(공백안됨)과 숫자, '.', '-' 체크
function val_EnglishNotspaceDashNumberDot(id, msg, focus) {
    var msg = msg_creater(id, msg, 'id');
    
    var txt = document.getElementById(id).value;
    
    var cnt = 0;
    for(i=0; i<txt.length; i++) {
        //65 ~ 90 : A ~ Z
        //97 ~ 122 : a ~ z
        //32 : 공백
        if((txt.charCodeAt(i) >= 65 && txt.charCodeAt(i) <= 90) || (txt.charCodeAt(i) >= 97 && txt.charCodeAt(i) <= 122) || (txt.charCodeAt(i) >= 48 && txt.charCodeAt(i) <= 57) || txt.charCodeAt(i) == 46 || txt.charCodeAt(i) == 45) {
            // ascii
        } else {
            // not ascii
            cnt++;
        }
    }
    
    if(cnt != 0) {
        alert(msg);
        if(focus == true) {
            document.getElementById(id).focus();
        }
        return false;
    }
}

//문자열길이체크 min : 최소, max : 최대
function val_TxtLength(id, msg, focus, min, max) {
    var msg = msg_creater(id, msg, 'id');
    
    var txt = document.getElementById(id).value;
    
    if(txt.length < min || txt.length > max) {
        alert(msg);
        if(focus == true) {
            document.getElementById(id).focus();
        }
        return false;
    }
}

//이메일체크
//다른건 똑같고 level 값으로 필수인지 선택인지 판단한다. 1이 필수 2가 선택
//2가 선택이지만 값이 입력이 되면 체크를 태운다.
function val_EmailChk(id, msg, focus, level) {
    if(level == 1 || document.getElementById(id+"1").value || document.getElementById(id+"2").value) {
        if(val_Notnull(id+'1', msg, focus) == false) { return false; }
        if(val_EnglishNotspaceDashNumberDot(id+'1', msg, focus) == false) { return false; }
        
        if(val_Notnull(id+'2', msg, focus) == false) { return false; }
        if(val_EnglishNotspaceDashNumberDot(id+'2', msg, focus) == false) { return false; }
    }
}

//숫자처리(정수)
function val_Number(id, msg, focus) {
    var msg = msg_creater(id, msg, 'id');
    
    var input = document.getElementById(id).value;
    
    var cnt = 0;
    for(var i=0;i<input.length;i++) {
        if(input.charAt(i) == " ") {
            cnt++;
        }
        
        if(isNaN(input.charAt(i))) {
            cnt++;
        }
    }
    
    if(cnt != 0) {
        alert(msg);
        if(focus == true) {
            document.getElementById(id).focus();
        }
        return false;
    }
}

//숫자처리(소숫점포함)
//pos : 소숫점 자릿수
function val_NumberDot(id, msg, focus, pos) {
    var msg = msg_creater(id, msg, 'id');
    
    var input = document.getElementById(id).value;
    
    var cnt = 0;
    var j = 0;
    var k = 0;
    for(var i=0;i<input.length;i++) {
        if(input.charAt(i) != ".") {
            if(input.charAt(i) == " ") {
                cnt++;
            }
            
            if(isNaN(input.charAt(i))) {
                cnt++;
            }
            
            if(j == 1) {
                k++;
            }
        } else {
            j++;
        }
    }
    
    if(cnt != 0 || j > 1 || (j == 1 && k != pos)) {
        alert(msg);
        if(focus == true) {
            document.getElementById(id).focus();
        }
        return false;
    }
}

//숫자처리('-' 포함)
function val_NumberDash(id, msg, focus) {
    var msg = msg_creater(id, msg, 'id');
    
    var input = document.getElementById(id).value;
    
    var cnt = 0;
    for(var i=0;i<input.length;i++) {
        if(input.charAt(i) != "-") {
            if(input.charAt(i) == " ") {
                cnt++;
            }
            
            if(isNaN(input.charAt(i))) {
                cnt++;
            }
        }
    }
    
    if(cnt != 0) {
        alert(msg);
        if(focus == true) {
            document.getElementById(id).focus();
        }
        return false;
    }
}

//라디오체크..(이건 태그 id가 아닌 태그 name 으로 확인한다.)
function val_RadioChk(name, msg) {
    var msg = msg_creater(name, msg, 'name');
    
    var input = document.getElementsByName(name);
    var j = 0;
    for(var i=0;i<input.length;i++) {
        if(input[i].checked == true) {
            j++;
        }
    }
    
    if(i == 0 || j == 0) {
        alert(msg);
        return false;
    }
}

//전화번호체크(3개의 오브젝트로 구성된 전화번호 전용)
//다른건 똑같고 level 값으로 필수인지 선택인지 판단한다. 1이 필수 2가 선택
//2가 선택이지만 값이 입력이 되면 체크를 태운다.
//if(val_PhoneNumberChk('ordersTel', '을(를) 올바르게 입력하여 주십시오.', true, 1) == false) { return false; }
function val_PhoneNumberChk(id, msg, focus, level) {
    if(level == 1 || (level == 2 && (document.getElementById(id+"1").value || document.getElementById(id+"2").value || document.getElementById(id+"3").value))) {
        if(val_Notnull(id+'1', msg, focus) == false) { return false; }
        if(val_Number(id+'1', msg, focus) == false) { return false; }
        if(val_TxtLength(id+'1', msg, focus, 2, 4) == false) { return false; }
        
        
        if(val_Notnull(id+'2', msg, focus) == false) { return false; }
        if(val_Number(id+'2', msg, focus) == false) { return false; }
        if(val_TxtLength(id+'2', msg, focus, 3, 4) == false) { return false; }
        
        if(val_Notnull(id+'3', msg, focus) == false) { return false; }
        if(val_Number(id+'3', msg, focus) == false) { return false; }
        if(val_TxtLength(id+'3', msg, focus, 4, 4) == false) { return false; }
    }
}


function msg_creater(id, msg, opt) {
    if(opt == "id") {
        //title 태그로 박스명을 정한다. title태그가 없으면 메세지만 쓴다
        if(document.getElementById(id).title == undefined) {
            msg = msg;
        } else {
            msg = document.getElementById(id).title + msg;
        }
    } else if(opt == "name") {
        //title 태그로 박스명을 정한다. title태그가 없으면 메세지만 쓴다
        if(document.getElementsByName(id)[0].title == undefined) {
            msg = msg;
        } else {
            msg = document.getElementsByName(id)[0].title + msg;
        }
    }
    
    return msg;
}