// 숫자만 입력 가능..
function checkNum(str) {
	var nIndex;
	var ascChrCurrent;
	var id_len=str.value.length;
	var id_tmp=str.value;
	
	for( nIndex = 0; nIndex < id_len; nIndex++ )
	{
		ascChrCurrent = id_tmp.charAt(nIndex);

		if( !('0' <= ascChrCurrent && ascChrCurrent <= '9') )
		{
			alert("숫자만 입력 가능합니다");
			str.value = "";
			str.focus();
			return false;
		}
	}
	return true;
}

// 영문, 숫자만 입력 가능..
function checkEng(str) {
	var nIndex;
	var ascChrCurrent;
	var id_len=str.value.length;
	var id_tmp=str.value;
	
	for( nIndex = 0; nIndex < id_len; nIndex++ )
	{
		ascChrCurrent = id_tmp.charCodeAt(nIndex);

		if( ascChrCurrent > 127){
			alert('영문, 숫자만 입력가능합니다');
			str.value = "";
			str.focus();
			return false;
		}
	}
} 


// 한글만 입력가능..
function checkHan(str) {
	var nIndex;
	var ascChrCurrent;
	var id_len=str.value.length;
	var id_tmp=str.value;
	
	for( nIndex = 0; nIndex < id_len; nIndex++ )
	{
		ascChrCurrent = id_tmp.charCodeAt(nIndex);

		if( ascChrCurrent < 127){
			alert('영문, 숫자만 입력가능합니다');
			str.value = "";
			str.focus();
			return false;
		}
	}
} 

// 팝업창 띄우기..
function MM_openBrWindow(theURL,winName,features)
{
	frm = document.form1;

	window.open("",winName,features);

	frm.target = winName;
	frm.action = theURL;
	frm.submit();
}

// 이메일 체크..
function emailCheck(email){
	var mailexp = /[a-z0-9_]{2,}@[a-z0-9-]{2,}\.[a-z0-9]{2,}/i; 

	if(!mailexp.test(email))
	{
		alert("이메일을 정확히 입력해주세요!!");
		return false;
	}

	return true;
}

// 이미지 사이즈 조정
function imgSize(which,max_width){
    var width = eval("document."+which+".width");
    var height = eval("document."+which+".height");
    var temp = 0; 
    
    if ( width > max_width ) {  // 이미지가 700보다 크다면 너비를 700으로 맞우고 비율에 맞춰 세로값을 변경한다.      
       height = height/(width / max_width);
       eval("document."+which+".width = max_width");     
       eval("document."+which+".height = height");
    }
}

function imgcheck(imgObj, bool)
{
	var imgWidth = 700; //** 설정 이미지 폭값
	var imgHeight = 265; //** 설정 이미지 높이값

	if(bool) //** 이미지가 로딩이 다 되었을경우
	{
		var O_Width = imgObj.width; //** 이미지의 실제 폭
		var O_Height = imgObj.height; //** 이미지의 실제 높이
		var ReWidth = O_Width; //** 변화된 폭 저장 변수
		var ReHeight = O_Height; //** 변화된 높이 저장 변수

		if(ReWidth > imgWidth)
		{
			ReWidth = imgWidth;
			ReHeight = (O_Height * ReWidth) / O_Width;
		}

		/*if(ReHeight > imgHeight)
		{
			ReWidth = (ReWidth * imgHeight) / ReHeight;
			ReHeight = imgHeight;
		}*/

		//** 처리
		imgObj.width = ReWidth;
		//imgObj.height = ReHeight;
		//imgObj.alt = ReWidth +','+ ReHeight;
	}
	else //** 이미지가 해당 경로에 없어 로딩 에러가 생겼을경우
	{
		//** 안보이게 스타일 시트로 처리
		imgObj.style.display = 'none';
	}
}

function resizeiframe(name) {
	if(name == null || name == "") {
		return;
	}

	try {       
		var obj = eval("document."+name);
		var oBody = obj.document.body;
		var oFrame = document.all(name);
		var i_height = oBody.scrollHeight + (oBody.offsetHeight-oBody.clientHeight);
		var i_width = oBody.scrollWidth + (oBody.offsetWidth-oBody.clientWidth);
		oFrame.style.height = i_height;
		oFrame.style.width = i_width;
	} catch(e) {
		window.status = 'Error: ' + e.number + '; ' + e.description;
	}
}

function check_jumin(frm){      // form의 이름과 주민등록번호를 입력하는 2개의 text 객체를 받아온다
	// 주민등록번호 체크을 위한 기본 변수 선언
	var jumin = frm.number1.value + frm.number2.value;      // 입력된 주민등록번호 가져오기
	var key = "234567892345";           // 주민번호 생성 key 값
	var days = 0;                // 입력된 월의 일수를 저장할 변수
	var sum = 0;                // 곱해서 더한 총합 
	var result = 0;                // 연산후 마지막 숫자가 들어갈 변수
	var year_next = jumin.substring(0, 2);       // 연도(뒤에 두자리)에 해당하는 두자리를 구함
	var month = jumin.substring(2, 4);        // 월에 해당하는 두자리를 구함
	var day = jumin.substring(4, 6);         // 일에 해당하는 두자리를 구함
	var sex = jumin.charAt(6);           // 성별에 해당하는 한자리를 구함
	var year_prev = (sex == "1" || sex == "2") ? "19" : "20"; // 연도(앞에 두자리)에 해당하는 두자리를 구함
	var year = year_prev + year_next;         // 연도에 해당하는 네자리를 구함

	// 길이가 13자인지 체크
	if (jumin.length != 13){
		alert("주민등록번호는 13자리이어야 합니다.\n\n다시 확인하시고 입력해 주세요");
		return false;
	}

	// 월에 해당하는 두자리의 적합성 검사
	if(month < 1 || month > 12){
		alert("주민등록번호중, 월에 해당하는 두자리가 잘못 입력되었습니다.\n\n다시 확인하시고 입력해 주세요.");
		frm.number1.value="";
		frm.number1.focus();
		return false;
	}

	// 월에 따른 일에 해당하는 두자리의 적합성 검사(윤년체크 포함)
	if(month=="01" || month=="03" || month=="05" || month=="07" || month=="08" || month=="10" || month=="12") {
		days = 31;
	}

	if(month=="04" || month=="06" || month=="09" || month=="11"){
		days = 30;
	}

	if(month=="02"){
		if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){     // 윤년일 경우의 2월의 일수를 구함
			days = 29;
		}else{
			days = 28;
		}
	}

	if(day > days){
		alert("주민등록번호중, 일에 해당하는 두자리가 범위보다 큽니다.\n\n다시 확인하시고 입력해 주세요.");
		return false;
	}

	// 성별 비트에 따라 성별 자동으로 설정하기
	if(sex == "1" || sex == "3"){}    //form.sex.value="m";
	else if(sex == "2" || sex == "4"){}  //form.sex.value="f";
	else{
		alert("주민등록번호중, 성별에 해당하는 한자리가 잘못 입력되었습니다.\n\n다시 확인하시고 입력해 주세요.");
		frm.number2.value="";
		frm.number2.focus();
		return false;
	}

	// 주민등록번호 생성 알고리즘에 의한 유효성 체크
	for(i=0; i<12; i++){
		sum += jumin.charAt(i) * key.charAt(i);
	}

	result = (11 - (sum % 11)) % 10;

	if (jumin.charAt(12) != result){
		alert("유효하지 않는 주민번호입니다.\n\n다시 확인하시고 입력해 주세요.");
		frm.number1.value="";
		frm.number2.value="";
		frm.number1.focus();
		return false;
	}

	return true;
}

function bookmark()
{
	window.external.AddFavorite('http://www.okfoto.or.kr', '사진인화 즐겨찾기, 오케이포토!');
}

function scrapAPI(type) {
	var title = "OKFOTO";
	
	var rurl = "http://www.ntck.or.kr/";
	
	var link; 
	switch(type) {
		case 'me2day': 
			var tag = 'OKFOTO';
			link = 'http://me2day.net/posts/new?new_post[body]="' + encodeURIComponent(title) + '" : ' + encodeURIComponent(rurl) + '&new_post[tags]=' + encodeURIComponent(tag) ;
			popwin = window.open(link,'popwin', 'menubar=yes,toolbar=yes,status=yes,resizable=yes,location=yes,scrollbars=yes');
			if(popwin)
				popwin.focus();
		break;

		case 'twitter': 
			link = 'http://twitter.com/home?status=' + encodeURIComponent(title) + ' : ' + encodeURIComponent(rurl);
			popwin = window.open(link,'popwin', 'menubar=yes,toolbar=yes,status=yes,resizable=yes,location=yes,scrollbars=yes');
			if(popwin)
				popwin.focus();
		break;
		
		case 'facebook': 
			link = 'http://www.facebook.com/share.php?t=' + encodeURIComponent(title) + '&u=' + encodeURIComponent(rurl);
			popwin = window.open(link,'popwin', 'menubar=yes,toolbar=yes,status=yes,resizable=yes,location=yes,scrollbars=yes');
			if(popwin)
				popwin.focus();
		break;

		default:
		break;
	}
}

function CheckID(uid){
	if(!/^[a-z0-9_]{5,12}$/.test(uid)){ 
		alert('아이디는 숫자와 영문소문자,특수기호(_)로 5~12자리를 사용해야 합니다.'); 
		return false;
	}

	var chk_num = uid.search(/[0-9]/g); 
	var chk_eng = uid.search(/[a-z]/ig); 

	/*if(chk_num < 0 || chk_eng < 0){ 
		alert('아이디는 숫자와 영문자를 혼용하여야 합니다.'); 
		return false;
	}*/

	if(/(\w)\1\1\1/.test(uid)){
		alert('아이디는 같은 문자를 4번 이상 사용하실 수 없습니다.'); 
		return false;
	}

	return true;
}

function CheckPassword(upw){
	if(!/^[a-zA-Z0-9]{6,8}$/.test(upw)){ 
		alert('비밀번호는 영문과 숫자의 조합으로 6~8자리를 사용해야 합니다.'); 
		return false;
	}

	var chk_num = upw.search(/[0-9]/g); 
	var chk_eng = upw.search(/[a-zA-Z]/ig); 

	if(chk_num < 0 || chk_eng < 0){ 
		alert('비밀번호는 숫자와 영문자를 혼용하여야 합니다.'); 
		return false;
	}

	if(/(\w)\1\1\1/.test(upw)){
		alert('비밀번호에 같은 문자를 4번 이상 사용하실 수 없습니다.'); 
		return false;
	}

	/*if(upw.search(uid)>-1){
		alert('ID가 포함된 비밀번호는 사용하실 수 없습니다.');
		return false;
	}*/

	return true;
}

function goNextFocusChk(obj, len, next_item) {
	if (obj.value.length == len){
		next_item.focus();
	}
}

function openStorymarket(url,id,value){
	window.open(url,id,value);
}
