/**
* 콘트롤(모든 콘트롤 가능)에 값이 입력 또는 선택되었는지와 
* maxlength가 설정된 경우 한글을 2바이트 계산하여 길이를 체크한다.
* [파라메터]
* ctl_name : 체크할 콘트롤 이름
* err_message : 체크 오류시 출력할 메세지
*
*	ex) if(!emptyLengthCheckName("BD_C_TITLE","제목을 입력해주세요.")) return;
*/
function emptyLengthCheckName(ctl_name,err_message) { 
	var ctl = document.getElementsByName(ctl_name);
	
	if(ctl.length == null || ctl.length == 'undefined'){
		return validationCheck(ctl,err_message);
//	}else if(ctl[0].type == 'checkbox' || ctl[0].type == 'radio'){
//		return validationCheck(ctl,err_message);
	}else if(ctl.type != 'undefined' && ctl.type != null){
		return validationCheck(ctl,err_message);
	}else{
		for(var i = 0; i < ctl.length; i ++ ) {
			if(!ctl[i].disabled){
				if(!validationCheck(ctl[i],err_message)) return false;
			} 
		}
	}

	
	return true;
} 
/*==============================================================================================================
*  공백, 숫자 및 길이 체크
*=============================================================================================================*/	
/**
* 콘트롤에 값이 공백 또는 숫자인지와  
* maxlength가 설정된 경우 길이를 체크한다.
* [파라메터]
* ctl_name : 체크할 콘트롤 이름
* err_message : 체크 오류시 출력할 메세지
*/
function numericLengthCheckName(ctl_name,err_message) { 
	var ctl = document.getElementsByName(ctl_name);

	if(ctl.length == null || ctl.length == 'undefined'){
		if (! isNumber(ctl.value)){
		    alert(err_message); 
		    ctl.focus();
		    return false;
		} 
	}else{
		for(var i = 0; i < ctl.length; i ++ ) {
			if(!ctl[i].disabled){
				if (! isNumber(ctl[i].value)){
				    alert(err_message); 
				    ctl[i].focus();
				    return false;
				} 
			} 
		}
	}

	return emptyLengthCheckName(ctl_name,err_message);

} 
/*==============================================================================================================
*   공백 및 길이 체크 
*=============================================================================================================*/	
function validationCheck(ctl,err_message) { 

	if ( ctl.type == 'text' || ctl.type == 'textarea' || ctl.type == 'file' || ctl.type == 'password' || ctl.type == 'hidden'){
		if ( ctl.value == '' ){
		    alert(err_message); 
		    if(!(ctl.type == 'hidden'))ctl.focus();
		    return false;
		} 
		return checkByteLength(ctl, ctl.maxLength);
	}
	else if ( ctl.type == 'select-one' ){
		if (ctl.size < 2 && ctl.selectedIndex == 0 || ctl.size > 1 && ctl.selectedIndex == -1){
		    alert(err_message); 
		    ctl.focus();
		    return false;
		} 
	} 
	else{
		if (! isCheckRadioSelect(ctl) ){
		    alert(err_message); 
		    return false;
		} 
	}
	
	return true;
} 
/*==============================================================================================================
*  입력한 항목의 byte length 체크
*=============================================================================================================*/	
function checkByteLength(ctl, len)
{
	if(len > 0){
		if(getByteLengthIde(ctl.value) > len)
		{
	        //alert("value length is " + len + "  Check Length");
	        alert("입력한 항목의 길이가 " + len + "Bytes를 초과 하였습니다.");
	        ctl.focus();
	        return false;
		}
		return true;	
	}else{
		return true;
	}
}
/*==============================================================================================================
*  입력한 항목의 byte length 체크 (필수항목이 아닌 경우)
*=============================================================================================================*/	
function checkByteLengthIde(ctl_name, len)
{	
	var ctl = document.getElementById(ctl_name);
	
	if(getByteLengthIde(ctl.value) > len)
	{
        //alert("value length is " + len + "  Check Length");
        alert("입력한 항목의 길이가 " + len + "를 초과 하였습니다.");
        ctl.focus();
        return false;
	}
	return true;
}
/*==============================================================================================================
*   입력한 항목의 byte length를 계산 한다.
*=============================================================================================================*/	
function getByteLengthIde(val)
{
	var len = 0;
	//UTF-8 3 bytes로 계산
	//var lengthTable = new Array( new Array("\u0080", 1), new Array("\u07FF", 2), new Array("\uFFFF", 3) );
	var lengthTable = new Array( new Array("\u0080", 1), new Array("\u07FF", 2), new Array("\uFFFF", 2) );
	if ( val == null ) return 0

	for(var i = 0; i < val.length; i ++ ) {
		for (j = 0; j  <  lengthTable.length; j++) {
			if (val.charAt(i)  <  lengthTable[j][0]) {
				len += lengthTable[j][1];
				break;
			}
		}
	}
	return len;
}

/*==============================================================================================================
*   체크박스나 라디오버튼의 선택 여부를 체크한다.
*=============================================================================================================*/	
function isCheckRadioSelect(vObj) {
	if(vObj.length == null || vObj.length == 'undefined'){
		if (vObj.checked) {
			return true;
		}
		return false;
	}
	else{
	
		for (i=0; i  <  vObj.length; i++) {
			if (vObj[i].checked) {
				return true;
			}
		}
		return false;
	}
}

/*==============================================================================================================
*   ID 체크
*=============================================================================================================*/
function isValid_id( str )
{
     // check whether input value is included space or not
     if( str == ""){
     	alert("아이디를 입력해 주세요.");
     	return false;
     }

	// 아이디 가운데 빈 공간이 없도록 체크한다.
     var retVal = checkSpace( str );
     if( retVal ) {
         alert("아이디는 빈 공간 없이 연속된 영문 소문자와 숫자만 사용할 수 있습니다.");
         return false;
     }

     // 아이디는 '-' 로 시작할 수 없다.
	if( str.charAt(0) == '_') {
		alert("아이디의 첫문자는 '_'로 시작할수 없습니다.");
		return false;
	}

     // 길이와 허용 문자를 체크한다.
     var isID = /^[a-z0-9_]{4,15}$/;
     if( !isID.test(str) ) {
         alert("아이디는 4~15자의 영문 소문자와 숫자,특수기호(_)만 사용할 수 있습니다.");
         return false;
     }

	 var isNum = /\d/;
     var i;
     var cnt = 0;
     for( i=0; i < str.length; i++) {
     	if( isNum.test( str.substring( i, i+1 ) ) ) {
     		cnt++;
     	}
     	if( cnt > 7 ) {
     		alert("같은 문자가 7개 이상 사용되면 안됩니다.");
     		return false;
     	}
     }

     return true;
}

/*==============================================================================================================
*   비밀번호 체크
*=============================================================================================================*/
function isValid_passwd( str )
{
     var cnt = 0;
     if( str == ""){
     	alert("비밀번호를 입력하세요.");
     	return false;
     }

    /* check whether input value is included space or not  */
     var retVal = checkSpace( str );
     if( retVal ) {
         alert("비밀번호에는 공백이 있으면 안됩니다.");
         return false;
     }
			if( str.length < 4 ){
				alert("비밀번호는 4~12자의 영문 대소문자와 숫자 , - , _ 등을 사용할 수 있습니다.");
				return false;
			}
     for( var i=0; i < str.length; ++i)
     {
         if( str.charAt(0) == str.substring( i, i+1 ) ) ++cnt;
     }
     if( cnt == str.length ) {
         alert("보안상의 이유로 한 문자로 연속된 비밀번호는 허용하지 않습니다.");
         return false;
     }

     var isPW = /^[A-Za-z0-9-_]{4,12}$/;
     if( !isPW.test(str) ) {
         alert("비밀번호는 4~12자의 영문 대소문자와 숫자 , -, _ 등을 사용할 수 있습니다.");
         return false;
     }
     return true;
}

/*==============================================================================================================
*   이메일 유효성 체크
*=============================================================================================================*/
function isValid_email( str )
{
     /* check whether input value is included space or not  */
     if(str == ""){
     	alert("이메일 주소를 입력하세요.");
     	return false;
     }
     var retVal = checkSpace( str );
     if( retVal ) {
         alert("이메일 주소를 빈공간 없이 넣으세요.");
         return false;
     }

     if( -1 == str.indexOf('.') ) {
     	alert("이메일 형식이 잘못 되었습니다.");
        return false;
     }

     /* checkFormat */
     var isEmail = /[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+(\.[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+)*@[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+(\.[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+)*/;
     if( !isEmail.test(str) ) {
         alert("이메일 형식이 잘못 되었습니다.");
         return false;
     }
     if( str.length > 60 ) {
         alert("이메일 주소는 60자까지 유효합니다.");
         return false;
     }

     return true;
}


// space 가 있으면 true, 없으면 false
function checkSpace( str )
{
     if(str.search(/\s/) != -1){
     	return true;
     } else {
        return false;
     }
}

function isNumber(_value){
	// Only Positive Number.
	var myRe = /\D/g ;
	return !(myRe.test(_value)) ;
}

function newWindow(as_win_nm, as_url, ai_width, ai_height, ai_scrollbar)   {
    var li_top = screen.height / 2 - ai_height / 2 - 50;
    var li_left = screen.width / 2 - ai_width / 2 ;
    var win = open(as_url, as_win_nm, 'width='+ai_width+',height='+ai_height+',top='+li_top+',left='+li_left+',resizable=yes,status=yes,toolbar=no,menubar=no,scrollbars='+ai_scrollbar);
    win.focus();
}
