// 전역 변수
var errmsg = "";
var errfld;

/**
 * console.log() 대용(콘솔 사용불가시 자바스크립트 오류발생 방지)
 **/
function debugPrint(msg) {
    if (typeof console == 'object' && typeof console.log == 'function') {
        console.log(msg);
    }
}

/**
 * 필드 검사
 **/
function check_field(fld, msg) {
    if((fld.value = trim(fld.value)) == '') {
        error_field(fld, msg);
    } else {
        clear_field(fld);
    }

    return;
}

/**
 * 필드 오류 표시
 **/
function error_field(fld, msg) {
    if(msg != "") errmsg += msg + "\n";
    if(!errfld) errfld = fld;

    fld.style.background = "#BDDEF7";
}

/**
 * 필드를 깨끗하게
 **/
function clear_field(fld) {
    fld.style.background = "#FFFFFF";
}

/**
 * @TODO 함수 설명 필요
 **/
function trim(s) {
    var t = '';
    var from_pos = to_pos = 0;

    for(i = 0; i < s.length; i++) {
        if(s.charAt(i) == ' ') {
            continue;
        } else {
            from_pos = i;
            break;
        }
    }

    for(i = s.length; i >= 0; i--) {
        if(s.charAt(i - 1) == ' ') {
            continue;
        } else {
            to_pos = i;
            break;
        }
    }

    t = s.substring(from_pos, to_pos);

    return t;
}

/**
 * 자바스크립트로 PHP의 number_format 흉내를 냄
 * 숫자에 , 를 출력
 **/
function number_format(data) {
    var tmp = '';
    var number = '';
    var cutlen = 3;
    var comma = ',';
    var i;

    len = data.length;
    mod = (len % cutlen);
    k = cutlen - mod;

    for(i = 0; i < data.length; i++) {
        number = number + data.charAt(i);

        if(i < data.length - 1) {
            k++;
            if((k % cutlen) == 0) {
                number = number + comma;
                k = 0;
            }
        }
    }

    return number;
}

/**
 * 새 창
 **/
function popup_window(url, winname, opt) {
    window.open(url, winname, opt);
}

/**
 * a 태그에서 onclick 이벤트를 사용하지 않기 위해
 * @TODO 설명문구 수정
 **/
function win_open(url, name, option) {
    var popup = window.open(url, name, option);
    popup.focus();
}

/**
 * 폼메일 창
 **/
function popup_formmail(url) {
    opt = 'scrollbars=yes, width=417, height=385, top=10, left=20';
    popup_window(url, 'wformmail', opt);
}

/**
 * , 를 없앤다.
 * @TODO 문자열 치환하는데 왜 loop를 돌지?
 **/
function no_comma(data) {
    var tmp = '';
    var comma = ',';
    var i;

    for(i = 0; i < data.length; i++) {
        if(data.charAt(i) != comma) tmp += data.charAt(i);
    }

    return tmp;
}

/**
 * 삭제 검사 확인
 **/
function del(href) {
    if(confirm("한번 삭제한 자료는 복구할 방법이 없습니다.\n\n정말 삭제하시겠습니까?")) {
        document.location.href = href;
    }
}

/**
 * 쿠키 입력
 * @TODO 쿠키 플러그인으로 대체
 **/
function set_cookie(name, value, expirehours, domain) {
    var today = new Date();
    today.setTime(today.getTime() + (60 * 60 * 1000 * expirehours));
    document.cookie = name + '=' + escape( value ) + '; path=/; expires=' + today.toGMTString() + ';';

    if(domain) {
        document.cookie += 'domain=' + domain + ';';
    }
}

/**
 * 쿠키 얻음
 * @TODO 쿠키 플러그인으로 대체
 **/
function get_cookie(name) {
    var find_sw = false;
    var start, end;
    var i = 0;

    for(i = 0; i <= document.cookie.length; i++) {
        start = i;
        end = start + name.length;

        if(document.cookie.substring(start, end) == name) {
            find_sw = true;
            break;
        }
    }

    if(find_sw == true) {
        start = end + 1;
        end = document.cookie.indexOf(';', start);

        if(end < start) end = document.cookie.length;

        return document.cookie.substring(start, end);
    }
    return '';
}

/**
 * 쿠키 지움
 * @TODO 쿠키 플러그인으로 대체
 **/
function delete_cookie(name) {
    var today = new Date();

    today.setTime(today.getTime() - 1);
    var value = get_cookie(name);
    if(value != '') {
        document.cookie = name + '=' + value + '; path=/; expires=' + today.toGMTString();
    }
}



var last_id = null;

/**
 * @TODO 함수설명 필요
 **/
function menu(id) {
    if(id != last_id) {
        if(last_id != null) {
            jQuery('#' + last_id).hide();
        }
        jQuery('#' + id).show();
        last_id = id;
    } else {
        jQuery('#' + id).hide();
        last_id = null;
    }
}

/**
 * @TODO 함수설명 필요
 **/
function textarea_decrease(id, row) {
    if(document.getElementById(id).rows - row > 0) {
        document.getElementById(id).rows -= row;
    }
}

/**
 * @TODO 함수설명 필요
 **/
function textarea_original(id, row) {
    document.getElementById(id).rows = row;
}

/**
 * @TODO 함수설명 필요
 **/
function textarea_increase(id, row) {
    document.getElementById(id).rows += row;
}

/**
 * 글숫자 검사
 * @TODO 함수설명 보완
 **/
function check_byte(content, target) {
    var i = 0;
    var cnt = 0;
    var ch = '';
    var cont = document.getElementById(content).value;

    for(i = 0; i < cont.length; i++) {
        ch = cont.charAt(i);

        if(escape(ch).length > 4) {
            cnt += 2;
        } else {
            cnt += 1;
        }
    }

    // 숫자를 출력
    document.getElementById(target).innerHTML = cnt;

    return cnt;
}

/**
 * 브라우저에서 오브젝트의 왼쪽 좌표
 * @TODO jQuery 함수로 대체
 **/
function get_left_pos(obj) {
    var parentObj = null;
    var clientObj = obj;
    var left = obj.offsetLeft;

    while((parentObj = clientObj.offsetParent) != null) {
        left = left + parentObj.offsetLeft;
        clientObj = parentObj;
    }

    return left;
}

/**
 * 브라우저에서 오브젝트의 상단 좌표
 * @TODO jQuery 함수로 대체
 **/
function get_top_pos(obj) {
    var parentObj = null;
    var clientObj = obj;
    var top = obj.offsetTop;

    while((parentObj=clientObj.offsetParent) != null) {
        top = top + parentObj.offsetTop;
        clientObj = parentObj;
    }

    return top;
}

/**
 * @TODO 함수설명 필요
 **/
function flash_movie(src, ids, width, height, wmode) {
    var wh = '';
    if(parseInt(width) && parseInt(height)) {
        wh = " width='"+width+"' height='"+height+"' ";
    }

    return "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' "+wh+" id="+ids+"><param name=wmode value="+wmode+"><param name=movie value="+src+"><param name=quality value=high><embed src="+src+" quality=high wmode="+wmode+" type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?p1_prod_version=shockwaveflash' "+wh+"></embed></object>";
}

/**
 * @TODO 함수설명 필요
 **/
function obj_movie(src, ids, width, height, autostart) {
    var wh = "";
    if (parseInt(width) && parseInt(height))
        wh = " width='"+width+"' height='"+height+"' ";
    if (!autostart) autostart = false;
    return "<embed src='"+src+"' "+wh+" autostart='"+autostart+"'></embed>";
}

/**
 * @TODO 함수설명 필요
 **/
function doc_write(cont) {
    document.write(cont);
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, '');
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, '');
}



/**
 * 한글, 영문, 숫자 검사
 **/
function chk_hanalnum(s) {
    var pattern = /([^가-힣ㄱ-ㅎㅏ-ㅣ^a-z^0-9])/i;

    return !pattern.test(s);
}


/**
 * 이메일주소 검사
 **/
function chk_email(s) {
    var pattern = /([0-9a-zA-Z_-]+)@([0-9a-zA-Z_-]+)\.([0-9a-zA-Z_-]+)/;

    return pattern.test(s);
}

/**
 * 우편번호 창
 **/
var win_zip = function(href) {
    var new_win = window.open(href, 'win_zip', 'width=400, height=440, scrollbars=1');
    new_win.focus();
}

/**
 * 새로운 패스워드 분실 창 : 101123
 **/
win_password_lost = function(href)
{
    var new_win = window.open(href, 'win_password_lost', 'width=617, height=330, scrollbars=1');
    new_win.focus();
}

jQuery(function($) {
    // @FIXME 자동완성 기능을 왜 죄다 꺼버리는가?
    $('form').each(function(i) {
        $(this).attr('autocomplete', 'off');
    });
});

var cPrint = function ( id ) {
	$("#" + id).printElement({printMode:'popup'});
}

var getMenuOnOver = function ( id, o, itype ) {
	$("#" + id + " a").each(function(){
		var image = $(this).children("img");
		var imgsrc = $(image).attr("src");     //attr 속성의 값을 가져온다.
		var imgon = $(image).attr("class");

		if( imgon == 'on' ) {
			var on = imgsrc.replace("."+itype, "_" + o + "." + itype);   //앞에 문자열을 뒤에 문자열로 변환 
			$(image).attr("src",on);                 //attr 속성의 값을 변경
		}

		//add mouseOver
		$(this).mouseover(function(){
			if( imgon != 'on' ) {
				var on = imgsrc.replace("."+itype,   "_" + o + "." + itype);   //앞에 문자열을 뒤에 문자열로 변환 
				$(image).attr("src",on);                 //attr 속성의 값을 변경
			}
		});

		//add mouseOut
		$(this).mouseout(function(){
			if( imgon != 'on' ) {
				var off = imgsrc.replace("_" + o + "."+itype, "." + itype);
				$(image).attr("src",off);
			}
		});

		//add mouseDown
		$(this).mousedown(function(){
			if( imgon != 'on' ) {
				var dn = imgsrc.replace("."+itype,   "_" + o + "." + itype);
				$(image).attr("src",dn);
			}
		});
	});
}
