﻿function getXMLHttpRequest() {
	if (window.ActiveXObject) {
		try {
			return new ActiveXObject('Msxml2.XMLHTTP');
		} catch(e) {
			try {
				return new ActiveXObject('Microsoft.XMLHTTP');
			} catch(e1) { return null; }
		}
	} else if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else {
		return null;
	}
}

function enter(func){
	if(event.keyCode == 13) eval(func);
}

String.prototype.isForeign = function() {
	var arg = arguments[0] ? arguments[0] : "";
	var jumin = eval("this.match(/[0-9]{2}[01]{1}[0-9]{1}[0123]{1}[0-9]{1}" + arg + "[5678]{1}[0-9]{1}[02468]{1}[0-9]{2}[6789]{1}[0-9]{1}$/)");
	if(jumin == null) {
		return false;
	}
	else {
		jumin = jumin.toString().num().toString();
	}
	var birthYY = (parseInt(jumin.charAt(6)) == (5 || 6)) ? "19" : "20";
	birthYY += jumin.substr(0, 2);
	var birthMM = jumin.substr(2, 2) - 1;
	var birthDD = jumin.substr(4, 2);
	var birthDay = new Date(birthYY, birthMM, birthDD);
	if(birthDay.getYear() % 100 != jumin.substr(0,2) || birthDay.getMonth() != birthMM || birthDay.getDate() != birthDD) {
		return false;
	}
	if((parseInt(jumin.charAt(7)) * 10 + parseInt(jumin.charAt(8))) % 2 != 0) {
		return false;
	}
	var sum = 0;
	var num = [2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5]
	var last = parseInt(jumin.charAt(12));
	for(var i = 0; i < 12; i++) {
		sum += parseInt(jumin.charAt(i)) * num[i];
	}
	return (((11 - sum % 11) % 10) + 2 == last) ? true : false;
}    

function isRegNo_fgnno(fgnno) { 
        var sum=0; 
        var odd=0; 
        buf = new Array(13); 
        for(i=0; i<13; i++) { buf[i]=parseInt(fgnno.charAt(i)); } 
        odd = buf[7]*10 + buf[8]; 
        if(odd%2 != 0) { return false; } 
        if( (buf[11]!=6) && (buf[11]!=7) && (buf[11]!=8) && (buf[11]!=9) ) { 
                return false; 
        } 
        multipliers = [2,3,4,5,6,7,8,9,2,3,4,5]; 
        for(i=0, sum=0; i<12; i++) { sum += (buf[i] *= multipliers[i]); } 
        sum = 11 - (sum%11); 
        if(sum >= 10) { sum -= 10; } 
        sum += 2; 
        if(sum >= 10) { sum -= 10; } 
        if(sum != buf[12]) { return false } 
        return true; 
} 

function passportNumber_chk(fgnno) { 

  nA = new Array(9);
  nA[1] = fgnno.substring(0,1);
  nA[2] = fgnno.substring(1,2);
  nA[3] = parseFloat(fgnno.substring(2,3));
  nA[4] = parseFloat(fgnno.substring(3,4));
  nA[5] = parseFloat(fgnno.substring(4,5));
  nA[6] = parseFloat(fgnno.substring(5,6));
  nA[7] = parseFloat(fgnno.substring(6,7));
  nA[8] = parseFloat(fgnno.substring(7,8));
  nA[9] = parseFloat(fgnno.substring(8,9));

    if ( fgnno.length != 9 ) {
        return false;
    }
    if ( nA[1] < 'A' || nA[1] > 'Z' ) {
        return false;
    }
    else if ( nA[2] < 'A' || nA[2] > 'Z' ) {
        return false;
    }

    nSum = nA[3] * 1000000 + nA[4] * 100000 + nA[5] * 10000 + nA[6] * 1000 + nA[7] * 100 + nA[8] * 10 + nA[9];
    if ( nSum < 0 || nSum > 9999999 ) {
        return false;
    }

    return true;
}

function numCheck(target)
{  
	var Digit = '1234567890';
   for (i=0 ;i<=target.length ;i++)
   {  if(Digit.indexOf(target.substring(i,i+1)) < 0) 
      {  
		 return false;
      }
   }
}

function commaNum(num) {  

        if (num < 0) { num *= -1; var minus = true} 
        else var minus = false 
         
        var dotPos = (num+"").split(".") 
        var dotU = dotPos[0] 
        var dotD = dotPos[1] 
        var commaFlag = dotU.length%3 

        if(commaFlag) { 
                var out = dotU.substring(0, commaFlag)  
                if (dotU.length > 3) out += "," 
        } 
        else var out = "" 

        for (var i=commaFlag; i < dotU.length; i+=3) { 
                out += dotU.substring(i, i+3)  
                if( i < dotU.length-3) out += "," 
        } 

        if(minus) out = "-" + out 
        if(dotD) return out + "." + dotD 
        else return out  
} 

function number_chk(obj,chk)
{
	if(numCheck(obj.value) ==false)
	{
		alert('number only');
		obj.value =chk ;		 
		obj.focus();	 
	}
}

function email_validchk(value)
{
    var t = value
	var ValidFlag = false
	var atCount = 0
	var SpecialFlag
	var atLoop
	var atChr
	var BadFlag
	var tAry1
	var UserName
	var DomainName

	if ( t.length > 0 && t.indexOf("@") > 0 && t.indexOf(".") > 0 ) {
		atCount = 0
		SpecialFlag = false

		for( atLoop=1; atLoop<=t.length; atLoop++ ) {
			atChr = t.substring( atLoop, atLoop+1 )
			if ( atChr == "@" ) atCount = atCount + 1

			if ( (atChr >= 32) && (atChr <= 44) ) SpecialFlag = true 
			if ( (atChr == 47) || (atChr == 96) || (atChr >= 123) ) SpecialFlag = true 
			if ( (atChr >= 58) && (atChr <= 63) ) SpecialFlag = true 
			if ( (atChr >= 91) && (atChr <= 94) ) SpecialFlag = true 
		}

		if ( ( atCount == 1 ) && (SpecialFlag == false ) ) {
			BadFlag = false
			tAry1 = t.split("@")
			UserName = tAry1[0]
			DomainName = tAry1[1]
			if ( (UserName.length <= 0 ) || (DomainName.length <= 0 ) ) BadFlag = true
			if ( DomainName.substring( 1, 2 ) == "." ) BadFlag = true
			if ( DomainName.substring( DomainName.length-1, DomainName.length) == "." ) BadFlag = true
			ValidFlag = true
		}
	}
	if ( BadFlag == true ) ValidFlag = false
	return ValidFlag
}
 
function Jumin_chk(it) {
	IDtot = 0;
	IDAdd = "234567892345";

	for(i=0; i<12; i++) IDtot = IDtot + parseInt(it.substring(i, i+1)) * parseInt(IDAdd.substring(i, i+1));
	IDtot = 11 - (IDtot%11);
	if (IDtot == 10) IDtot = 0;
	else if (IDtot == 11) IDtot = 1;
	if(parseInt(it.substring(12, 13)) != IDtot) return true;
	else return false
}

function nullchk(frm,txt)
{
	if(frm.value =="")
	{
		alert(txt);
		frm.focus();
		return false;
	}
	else
	{
		return true;
	}

}

String.prototype.trim = function() {
    return this.replace(/(^ *)|( *$)/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/(^ *)/g, "");
}
String.prototype.rtrim = function() {
    return this.replace(/( *$)/g, "");
}

function calculateBytes( szValue){
	var tcount = 0;

	var tmpStr = new String(szValue);
	var temp = tmpStr.length;

	var onechar;
	for ( k=0; k<temp; k++ ){
		onechar = tmpStr.charAt(k);
		if (escape(onechar).length > 4){
			tcount += 2;
		}
		else{
			tcount += 1;
		}
	}
	return tcount;
}

function isid(value) {
	if (value.charAt(0) == '-') return false;
	if (value.charAt(value.length-1) == '-') return false;
	if (value.search(/[^a-z0-9-]/) == -1) return true;
	else return false;
}

function setPng24(obj) {
	obj.width=obj.height=1;
    obj.className=obj.className.replace(/\bpng24\b/i,'');
    obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ obj.src +"',sizingMethod='image');";
	obj.src=''; 
	return '';
}

var popup_url;
var popup_width;
var popup_height;

function img_resize(url,width){
	var img = new Image();
	img.src = url;
	
	if(img.width!=0){
		popup_url = url;
		popup_width = img.width;
		popup_height = img.height;

		if(img.width > width){
			img.style.width = width;
			img.style.height = 'auto';
			document.getElementById('img_area').appendChild(img);		
		}
		else{
			document.getElementById('img_area').appendChild(img);
		}
	}
	else{
		idleID = setTimeout('img_resize("' + url + '")',50);
	}
}

function popup_img(){
	var left = Math.ceil((window.screen.width - popup_width)/2);
	var top = Math.ceil((window.screen.height - popup_height)/2);

	window.open("/admin/popup_img.php?url="+popup_url,"image","left="+left+",top="+top+",scrollbars=no,toolbar=no,width="+popup_width+",height="+popup_height+"");
}

function check_img(filename, txt){
	if(filename != ''){
		if(filename.substr(filename.length-4,4) != ".jpg" && filename.substr(filename.length-3,3) != ".gif" && filename.substr(filename.length-3,3) != ".bmp"){
			alert(txt);
			return false;
		}
	}

	return true;
}

function set_cookie(name, value, exp_y, exp_m, exp_d, path, domain, secure){  
	var cookie_string = name + "=" + escape (value);

	if(exp_y){
		var expires = new Date(exp_y, exp_m, exp_d);
		cookie_string += "; expires=" + expires.toGMTString();
	}

	if(path) cookie_string += "; path=" + escape ( path );
	if(domain) cookie_string += "; domain=" + escape ( domain );
	if(secure) cookie_string += "; secure";

	document.cookie = cookie_string;
}

function delete_cookie(cookie_name){
	var cookie_date = new Date( );  // current date & time
	cookie_date.setTime ( cookie_date.getTime() - 1 );
	document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function get_cookie(cookie_name){
	var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
	if(results) return (unescape(results[2]));
	else return null;
}

function date_add(target_date , plus_day){
	var new_date = new Date();
	var process_time = target_date.getTime()+(parseInt(plus_day) * 24 * 60 * 60 * 1000);
	new_date.setTime(process_time);

	var return_value = new_date.getYear();
	
	if((new_date.getMonth()+1) < 10) return_value = return_value + "-0" + (new_date.getMonth()+1);
	else return_value = return_value + "-" + (new_date.getMonth()+1);

	if(new_date.getDate() < 10) return_value = return_value + "-0" + new_date.getDate(); 
	else return_value = return_value + "-" + new_date.getDate(); 


	return return_value;
}

function view_photo(url , width , height){
	var winHandle = window.open("" ,"Image","toolbar=no,scrollbars=yes,status=no, location = no,resizable=no,width=" + (parseInt(width)+20) + ",height=" + height + ",left=225,top=165");
	if(winHandle != null){
		var htmlString = "<html><head><title>DETAIL IMAGE</title></head>"; 
		htmlString += "<body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>";
		htmlString += "<a href=javascript:window.close()><img src=" + url + " border=0 alt=닫기></a>";
		htmlString += "</body></html>";
		winHandle.document.open();
		winHandle.document.write(htmlString);
		winHandle.document.close();
	} 
	if(winHandle != null) winHandle.focus();
	return winHandle;
}

function set_photo(obj , width){
	obj.owidth = obj.width;
	obj.oheight = obj.height;
	if(obj.width > width) obj.width = width;
	obj.style.visibility = 'visible';
}

function popup_pos(width , height){
	click_pos = event_pos(event).split(',');

	var top = click_pos[1] - (height / 2);
	var left = click_pos[0] - (width / 2);

	if(top < 0) top = 0;
	if(left < 0) left = 0;

	return top + "," + left;
}

function event_pos(e){
	if (document.layers){
		xCoord = e.x;
		yCoord = e.y;
	}
	else if (document.all){
		xCoord = event.clientX;
		yCoord = event.clientY;
	}
	else if (document.getElementById){
		xCoord = e.clientX;
		yCoord = e.clientY;
	}

	return xCoord + ',' + yCoord;
}

function insertIMG(src){
	var sHTML = "<img src='" + src + "' style='cursor:hand;' border='0'>";
	oEditors.getById["comment"].exec("PASTE_HTML", [sHTML]);
}

function login(){
	pop_float('/popup/pop_login.php',408,236);
}

function alert_login(){
	alert('로그인 해주세요');
	pop_float('/popup/pop_login.php',408,236);
}

function logout(){
	var url = "/popup/logout.php";
	req = getXMLHttpRequest();
	req.onreadystatechange = LogoutResult;
	req.open("GET", url, true);
	req.send(null);
}

function LogoutResult(){
	if(req.readyState == 4) {
		if(req.status == 200){
			callExternalInterface('logout');
			alert("로그아웃 하였습니다.");
		} 
		else{
			alert("잠시후 다시 시도해주세요.");
		}
	}
}

var chk_login_s_func;
var chk_login_f_func;
function chk_login(s_func , f_func){
	chk_login_s_func = s_func;
	chk_login_f_func = f_func;

	var url = "/data/check_login.php";
	req = getXMLHttpRequest();
	req.onreadystatechange = ChkLoginResult;
	req.open("GET", url, true);
	req.send(null);
}

function ChkLoginResult(){
	if(req.readyState == 4) {
		if(req.status == 200){
            var resultValue = req.responseText;
			if(resultValue == 'Y'){
				eval(chk_login_s_func);
			}
			else{
				eval(chk_login_f_func);
			}
		} 
		else{
			alert("잠시후 다시 시도해주세요.");
		}
	}
}

function go_url(url){
	location.href = url;
}

function serialize(form){
	var return_value = '';
	for(i = 0;i < form.elements.length;i++){
		if(form.elements.item(i).type == 'checkbox' || form.elements.item(i).type == 'radio'){
			if(form.elements.item(i).checked){
				if(i > 0) return_value = return_value + '&';
				return_value = return_value + form.elements.item(i).name + '=' + encodeURIComponent(form.elements.item(i).value);
			}
		}
		else{
			if(i > 0) return_value = return_value + '&';
			return_value = return_value + form.elements.item(i).name + '=' + encodeURIComponent(form.elements.item(i).value);
		}
	}
	return return_value;
}

function callExternalInterface(flag) { // login status toggle
	if(getMovieName("main").loginstatus){
		getMovieName("main").loginstatus(flag); 
	}
	else{
		setTimeout("callExternalInterface('"+flag+"')", 100);
	}
}

function getMovieName(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	}
	else {
		return document[movieName];
	}
}
