function _win(obj) {

    var url = (typeof obj.url == 'undefined') ? null : obj.url;
    var name = (typeof obj.name == 'undefined') ? null : obj.name;
	
    var param = [
    "width=" + ((typeof obj.width == 'undefined') ? 600 : obj.width),
    "height=" + ((typeof obj.height == 'undefined') ? 400 : obj.height),
    "scrollbars=" + ((typeof obj.scrollbars == 'undefined') ? 'yes' : ((obj.scrollbars) ? 'yes' : 'no')),
    "toolbars=" + ((typeof obj.toolbars == 'undefined') ? 'no' : ((obj.toolbars) ? 'yes' : 'no')),
    "location=" + ((typeof obj.location == 'undefined') ? 'no' : ((obj.location) ? 'yes' : 'no')),
    "resizable=" + ((typeof obj.resizable == 'undefined') ? 'yes' : ((obj.resizable) ? 'yes' : 'no')),
    "status=" + ((typeof obj.status == 'undefined') ? 'yes' : ((obj.status) ? 'yes' : 'no')),
    "menubar=" + ((typeof obj.menubar == 'undefined') ? 'no' : ((obj.menubar) ? 'yes' : 'no'))
    ];
	
    var win = window.open(url, name, param.join(', ')); 
    return win;
}

function _c(obj) {
    return (typeof obj == 'undefined' || obj == null || obj == false) ? false : true;
}

function _go(url) {
    document.location = url;   
}

function _form(id) {
    return (document.getElementById('form_' + id)) ? document.getElementById('form_' + id) : false;
}

function _cancelSearch(id) {
    if (!_form(id)) return false;
    _length = _form(id).elements.length;
    _buf = '';
    for (i = 0 ; i < _length ; i++) {
        _buf += _form(id).elements[i].type;
        if (_form(id).elements[i].type == 'text')
            _form(id).elements[i].value = '';
        if (_form(id).elements[i].type == 'select-one')
            _form(id).elements[i].selectedIndex = 0;
    }
    _form(id).page.value = '1';
    _form(id).submit();
}

function _validForm(obj, title) {
    if (!obj || obj.value.length == 0 || obj.value == '') {
        window.alert("'" + title + "' is empty");
        obj.focus();
        return false;
    } else return true;
}

function mPostalcode() {
    return _win({
        url: '/postalcode',
        name: 'wPostalCodeSearch',
        width: 300,
        height: 400
    }); 
}

(function($){
    
    var patterns = new Array();
    patterns["phone"] = /^0(2|31|32|33|41|42|43|51|52|53|54|55|61|62|63|64)+\d{7,9}$/;
    patterns["fax"] = /^0(2|31|32|33|41|42|43|51|52|53|54|55|61|62|63|64|303|505|70)+\d{7,9}$/;
    patterns["mobile"] = /^(01[016789]|02|0505|0506|0502)\d{6,8}$/;
    
    $.fn.number = function(string) {
        if (typeof string != 'undefined') 
            return _number(string);
        
        return this.each(function () {
            $(this).val(_number($(this).val()));
        });
    };

    $.fn.phone = function(string) {
        if (string) 
            return _phone(string);
        
        return this.each(function () {
            $(this).val(_phone($(this).val()));
        });
    };
    
    $.fn.isValidNumber = function(type) {
        return _number($(this).val()).match(patterns[type]);
    };
    
    $.fn.isPhone = function() {
        return $(this).isValidNumber('phone');
    };
    
    $.fn.isFax = function() {
        return $(this).isValidNumber('fax');
    };
    
    $.fn.isMobile = function() {
        return $(this).isValidNumber('mobile');
    };

    $.fn.defaultVal = function(string) {
        return this.each(function () {
            if (!$(this).val()) {
                $(this).val(string);
                $(this).data('defaultVal', string);
                $(this).addClass('defaultVal');
            }
            
            $(this).unbind('focus');
            $(this).focus(function() {
                if ($(this).val() == string) {
                    $(this).val('');
                    $(this).removeClass('defaultVal');
                }
            });
            
            $(this).unbind('blur');
            $(this).blur(function() {
                if (!$(this).val()) {
                    $(this).val(string);
                    $(this).addClass('defaultVal');
                }
            });
        });
    };
    
    $.fn.backToDefault = function() {
        return this.each(function () {
            $(this).val($(this).data('defaultVal'));
            $(this).addClass('defaultVal');
        });
    };
    
    function _phone(string) {
        var secondLength;
        string = _number(string);
        if (string.match(/^02/)) {
            secondLength = (string.length > 9) ? 4 : 3;
            string = string.substring(0, 2) 
            + "-" + string.substring(2, 2 + secondLength) 
            + "-" + string.substring(2 + secondLength);
        } else {
            secondLength = (string.length > 10) ? 4 : 3;
            string = string.substring(0, 3) 
            + "-" + string.substring(3, 3 + secondLength) 
            + "-" + string.substring(3 + secondLength);
        }
        return string;
    }
    
    function _number(string) {
        return string.replace(/([^0-9])/g, "");
    }
    
})(jQuery);


function _ext(string) {
    if (string.length == 0 || !string || string == '') return null;
    var _pos = string.lastIndexOf(".");
    if (_pos < 0) return null;
    return string.substring(_pos+1).toLowerCase();
}

function _pageinfo(options) {
    var ret = {};
    ret.total = (_c(options.total)) ? options.total : 0;
    ret.page = (_c(options.page)) ? options.page : 1;
    ret.itemPerPage = (_c(options.itemPerPage)) ? options.itemPerPage : 9;
    ret.pagePerScreen = (_c(options.pagePerScreen)) ? options.pagePerScreen : 9;
    ret.totalPage = (_c(ret.total)) ? Math.ceil(ret.total / ret.itemPerPage) : 0;
    ret.skip = (_c(ret.total)) ? (ret.page - 1) * ret.itemPerPage : 0;
    return ret;
}

function _pagelist(options) {
    var page = {};
    if (options.total) {
        page.first = (options.page > 2) ? 1 : null;
        page.prev = (options.page != 1) ? options.page - 1 : null;
        page.next = (options.page != options.totalPage) ? options.page + 1 : null;
        page.last = (options.page < options.totalPage - 1) ? options.totalPage : null;
        
        var i, _p;
        page.pages = new Array;
        for (i = 0 ; i < options.pagePerScreen ; i++) {
            _p = i - Math.floor(options.pagePerScreen / 2);
            if (_p > 0 && _p < (options.totalPage + 1))
                page.pages.push(_p);
        }
    }
    return page;
}

function mb_strlen(str) {
 
    var i = 0, j, result = 0;
 
    for (j = str.length ; i < j ; ++i) {
        if (str.charCodeAt(i) < 256) {
            result += 1;
        } else {
            result += 2;
        }
    }

    return result;
}

function mb_substr(str, start, len) {
 
    var i = 0, j, _start = 0, cutStart, cutLen;
 
    //길이가 주어지지 않으면 최대로 처리
    if( len === 0 ) {
        len = mb_strlen(str) - 1;
    }
 
    //시작점을 0에서 끝까지 사이에 정리함.
    if( start < 0 ){
 
        start = mb_strlen( str ) - start;
 
    }else if( start > mb_strlen( str ) - 1 ){
 
        start = mb_strlen( str ) - 1;
 
    }
 
    //시작점을 정리함
    if( start ){
        for( j = str.length ; i < j ; ++i ){
 
            if( str.charCodeAt(i) < 256 ){
                _start += 1;
            }else{
                _start += 2;
            }
            if( _start >= start ){
                cutStart = i - 1;
                break;
            }
        }
    }
 
    //길이를 정리함
    for( i = cutStart , j = str.length ; i < j ; ++i ){
        if( str.charCodeAt(i) < 256 ){
            len += 1;
        }else{
            len += 2;
        }
        cutLen = i;
        if( len >= len ){
            break;
        }
    }
 
    //출력
    return str.substr( cutStart, cutLen );
}

