var amina_mb_id_check = function() {
    $.ajax({
        type: "POST",
        url: amina_url+"/ajax.register.php",
        data: {
            "reg_mb_id": encodeURIComponent($("#reg_mb_id").val()),
            "opt": "id"
		},
        cache: false,
        async: false,
        success: function(data) {
			if(!data) data = '사용가능한 아이디입니다.';
            $('#chk_mb_id').html(data);
        }
    });
}


// 추천인 검사
var amina_mb_recommend_check = function() {
    $.ajax({
        type: "POST",
        url: amina_url+"/ajax.register.php",
        data: {
            "reg_mb_recommend": encodeURIComponent($("#reg_mb_recommend").val()),
            "opt": "recommend"
        },
        cache: false,
        async: false,
        success: function(data) {
			if(!data) data = '존재하는 아이디입니다.';
            $('#chk_mb_recommend').html(data);
        }
    });
}


var amina_mb_nick_check = function() {
    $.ajax({
        type: "POST",
        url: amina_url+"/ajax.register.php",
        data: {
            "reg_mb_nick": ($("#reg_mb_nick").val()),
            "reg_mb_id": encodeURIComponent($("#reg_mb_id").val()),
            "opt": "nick"
        },
        cache: false,
        async: false,
        success: function(data) {
			if(!data) data = '사용가능한 닉네임입니다.';
            $('#chk_mb_nick').html(data);
        }
    });
}


var amina_mb_email_check = function() {
    $.ajax({
        type: "POST",
        url: amina_url+"/ajax.register.php",
        data: {
            "reg_mb_email": $("#reg_mb_email").val(),
            "reg_mb_id": encodeURIComponent($("#reg_mb_id").val()),
            "opt": "email"
        },
        cache: false,
        async: false,
        success: function(data) {
			if(!data) data = '사용가능한 e-mail입니다.';
            $('#chk_mb_email').html(data);
        }
    });
}


var amina_mb_hp_check = function() {
    $.ajax({
        type: "POST",
        url: amina_url+"/ajax.register.php",
        data: {
            "reg_mb_hp": $("#reg_mb_hp").val(),
            "reg_mb_id": encodeURIComponent($("#reg_mb_id").val()),
            "opt": "hp"
        },
        cache: false,
        async: false,
        success: function(data) {
			if(!data) data = '등록가능한 번호입니다.';
            $('#chk_mb_hp').html(data);
        }
    });
}

function amina_zip(frm_name, frm_zip1, frm_zip2, frm_addr1, frm_addr2, frm_addr3, frm_jibeon) {
	url = gnu_url + "/bbs/zip.php?frm_name="+frm_name+"&frm_zip1="+frm_zip1+"&frm_zip2="+frm_zip2+"&frm_addr1="+frm_addr1+"&frm_addr2="+frm_addr2+"&frm_addr3="+frm_addr3+"&frm_jibeon="+frm_jibeon;
	window.open(url, "winZip", "left=50,top=50,width=616,height=460,scrollbars=1");
}

// 본인확인 인증창 호출
function amina_certify_win(type, url) {
    if(type == 'kcb-ipin') {
        var popupWindow = window.open( url, "kcbPop", "left=200, top=100, status=0, width=450, height=550" );
        popupWindow.focus();
    } else if(type == 'kcb-hp'){
        var popupWindow = window.open( url, "auth_popup", "left=200, top=100, width=430, height=590, scrollbar=yes" );
        popupWindow.focus();
    } else if(type == 'kcp-hp') {
        var return_gubun;
        var width  = 410;
        var height = 500;

        var leftpos = screen.width  / 2 - ( width  / 2 );
        var toppos  = screen.height / 2 - ( height / 2 );

        var winopts  = "width=" + width   + ", height=" + height + ", toolbar=no,status=no,statusbar=no,menubar=no,scrollbars=no,resizable=no";
        var position = ",left=" + leftpos + ", top="    + toppos;
        var AUTH_POP = window.open(url,'auth_popup', winopts + position);
    }
}

// 인증체크
function amina_cert_confirm() {
    var type;
    var val = document.fregisterform.cert_type.value

    switch(val) {
        case "ipin":
            type = "아이핀";
            break;
        case "hp":
            type = "휴대폰";
            break;
        default:
            return true;
    }

    if(confirm("이미 "+type+"으로 본인확인을 완료하셨습니다.\n\n이전 인증을 취소하고 다시 인증하시겠습니까?"))
        return true;
    else
        return false;
}

//회원탈퇴
function amina_leave() {
    if (confirm("정말 회원에서 탈퇴 하시겠습니까?"))
        location.href = gnu_url + "/bbs/member_confirm.php?url=member_leave.php";
}

function dateSelect(docForm,selectIndex) {
	watch = new Date(docForm.year.options[docForm.year.selectedIndex].text, docForm.month.options[docForm.month.selectedIndex].value,1);
	hourDiffer = watch - 86400000;
	calendar = new Date(hourDiffer);

	var daysInMonth = calendar.getDate();
		for (var i = 0; i < docForm.day.length; i++) {
			docForm.day.options[0] = null;
		}
		for (var i = 0; i < daysInMonth; i++) {
			docForm.day.options[i] = new Option(i+1);
	}
	document.form1.day.options[0].selected = true;
}

function dateView(year,mon,day){
     if(year == "null" && mon == "null" && day == "null"){       
     today = new Date();
     this_year=today.getFullYear();
     this_month=today.getMonth();
     this_month+=1;
     if(this_month <10) this_month="0" + this_month;
 
     this_day=today.getDate();
     if(this_day<10) this_day="0" + this_day;     
 }
 else{  
     var this_year = eval(year);
     var this_month = eval(mon); 
     var this_day = eval(day);
     }
 
  montharray=new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
  maxdays = montharray[this_month-1]; 
  //아래는 윤달을 구하는 것
  if (this_month==2) { 
      if ((this_year/4)!=parseInt(this_year/4)) maxdays=28; 
      else maxdays=29; 
  } 
 
 document.writeln("<select name='year' size=1 onChange='dateSelect(this.form,this.form.month.selectedIndex);'>");
     for(i=this_year;i>this_year-100;i--){//현재 년도에서 과거로 100년까지 까지를 표시함
         if(i==this_year) document.writeln("<OPTION VALUE="+i+ " selected >" +i); 
         else document.writeln("<OPTION VALUE="+i+ ">" +i); 
     }    
 document.writeln("</select>년");      
 
 document.writeln("<select name='month' size=1 onChange='dateSelect(this.form,this.selectedIndex);'>");
     for(i=1;i<=12;i++){ 
         if(i<10){
             if(i==this_month) document.writeln("<OPTION VALUE=0" +i+ " selected >0"+i); 
             else document.writeln("<OPTION VALUE=0" +i+ ">0"+i);
         }         
         else{
             if(i==this_month) document.writeln("<OPTION VALUE=" +i+ " selected >" +i);  
             else document.writeln("<OPTION VALUE=" +i+ ">" +i);  
         }                     
    }         
 document.writeln("</select>월");
 
 document.writeln("<select name='day' size=1>");
     for(i=1;i<=maxdays;i++){ 
         if(i<10){
             if(i==this_day) document.writeln("<OPTION VALUE=0" +i+ " selected >0"+i); 
             else document.writeln("<OPTION VALUE=0" +i+ ">0"+i); 
         }
         
         else{
             if(i==this_day) document.writeln("<OPTION VALUE=" +i+ " selected } >"+i); 
             else document.writeln("<OPTION VALUE=" +i+ ">" +i);  
         }                     
    }         
 document.writeln("</select>일"); 
   
}

$(document).ready(function() {

	var strPassword;
	var charPassword;
	var complexity = $("#complexity");
	var minPasswordLength = 3;
	var baseScore = 0, score = 0;
	
	var num = {};
	num.Excess = 0;
	num.Upper = 0;
	num.Numbers = 0;
	num.Symbols = 0;

	var bonus = {};
	bonus.Excess = 3;
	bonus.Upper = 4;
	bonus.Numbers = 5;
	bonus.Symbols = 5;
	bonus.Combo = 0; 
	bonus.FlatLower = 0;
	bonus.FlatNumber = 0;
	
	outputResult();
	$("#reg_mb_password").bind("keyup", checkVal);

function checkVal()
{
	init();
	
	if (charPassword.length >= minPasswordLength)
	{
		baseScore = 50;	
		analyzeString();	
		calcComplexity();		
	}
	else
	{
		baseScore = 0;
	}
	
	outputResult();
}

function init()
{
	strPassword= $("#reg_mb_password").val();
	charPassword = strPassword.split("");
		
	num.Excess = 0;
	num.Upper = 0;
	num.Numbers = 0;
	num.Symbols = 0;
	bonus.Combo = 0; 
	bonus.FlatLower = 0;
	bonus.FlatNumber = 0;
	baseScore = 0;
	score =0;
}

function analyzeString ()
{	
	for (i=0; i<charPassword.length;i++)
	{
		if (charPassword[i].match(/[A-Z]/g)) {num.Upper++;}
		if (charPassword[i].match(/[0-9]/g)) {num.Numbers++;}
		if (charPassword[i].match(/(.*[!,@,#,$,%,^,&,*,?,_,~])/)) {num.Symbols++;} 
	}
	
	num.Excess = charPassword.length - minPasswordLength;
	
	if (num.Upper && num.Numbers && num.Symbols)
	{
		bonus.Combo = 25; 
	}

	else if ((num.Upper && num.Numbers) || (num.Upper && num.Symbols) || (num.Numbers && num.Symbols))
	{
		bonus.Combo = 15; 
	}
	
	if (strPassword.match(/^[\sa-z]+$/))
	{ 
		bonus.FlatLower = -15;
	}
	
	if (strPassword.match(/^[\s0-9]+$/))
	{ 
		bonus.FlatNumber = -35;
	}
}
	
function calcComplexity()
{
	score = baseScore + (num.Excess*bonus.Excess) + (num.Upper*bonus.Upper) + (num.Numbers*bonus.Numbers) + (num.Symbols*bonus.Symbols) + bonus.Combo + bonus.FlatLower + bonus.FlatNumber;
	
}	
	
function outputResult()
{
	if ($("#reg_mb_password").val()== "")
	{ 
		complexity.html("영문자, 숫자만 입력 가능. 최소 3자이상 입력").removeClass("weak strong stronger strongest").addClass("default");
	}
	else if (charPassword.length < minPasswordLength)
	{
		complexity.html("<font color='#ed1c24'>"+ minPasswordLength+ "자 이상 입력하세요!</font>").removeClass("strong stronger strongest").addClass("weak");
	}
	else if (score<50)
	{
		complexity.html("보안성 : <font color='#ed1c24'>부적합!</font>").removeClass("strong stronger strongest").addClass("weak");
	}
	else if (score>=50 && score<75)
	{
		complexity.html("보안성 : <font color='#0000ff'>평균!</font>").removeClass("stronger strongest").addClass("strong");
	}
	else if (score>=75 && score<100)
	{
		complexity.html("보안성 : <font color='#0000ff'>좋음!</font>").removeClass("strongest").addClass("stronger");
	}
	else if (score>=100)
	{
		complexity.html("보안성 : <font color='#39b54a'>뛰어남!</font>").addClass("strongest");
	}
	
}

}); 
