var commonUtil = (function(){
    return {

        doAction : function(obj){

            if (typeof obj != "object") {
                throw "Ajax Type Error";
            }

            $.ajax({
                type : "POST",
                url : obj.url,
                data : JSON.stringify(obj.data),
                dataType : "json",
                // contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
                contentType: 'application/json; charset=UTF-8',
                beforeSend : function(data, frm, opt){

                },
                success : function(data){
                    obj.callBack(data);
                },
                error : function(jqXHR, textStatus, errorThrown){
                    // console.log(jqXHR);
                    let errMsg = "[ " + jqXHR.status + " ] error가 발생하였습니다.";
                    if (jqXHR.responseText.length > 0) {
                        let resText = JSON.parse(jqXHR.responseText);
                        if (resText.message.length > 0 ) {
                            errMsg += "\nerror message: " + resText.message;
                        }
                    }
                    $.utils.error(errMsg);
                }
            });
        },

        doActionFile : function(obj){

            if (typeof obj != "object") {
                throw "Ajax Type Error";
            }

            $.ajax({
                type : "POST",
                url : obj.url,
                data : obj.data,
                dataType : "json",
                contentType : false,
                async : true,
                cache : false,
                processData : false,
                success : function(data){
                    obj.callBack(data);
                },
                error : function(jqXHR, textStatus, errorThrown){
                    alert("일시적인 장애가 발생하였습니다.");
                }
            });

        },

        doActionGet : function(obj){

            if (typeof obj != "object") {
                throw "Ajax Type Error";
            }

            $.ajax({
                type : "GET",
                url : obj.url,
                data : obj.data,
                dataType : "json",
                contentType : 'application/json; charset=UTF-8',
                beforeSend : function(data, frm, opt){

                },
                success : function(data){
                    obj.callBack(data);
                },
                error : function(jqXHR, textStatus, errorThrown){
                    // console.log(jqXHR);
                    let errMsg = "[ " + jqXHR.status + " ] error가 발생하였습니다.";
                    if (jqXHR.responseText.length > 0) {
                        // console.log(jqXHR.responseText);
                        let resText = JSON.parse(jqXHR.responseText);
                        if (resText.message.length > 0 ) {
                            errMsg += "\nerror message: " + resText.message;
                        }
                    }

                    $.utils.error(errMsg);
                }
            });
        },

        doActionText : function(obj){

            if (typeof obj != "object") {
                throw "Ajax Type Error";
            }

            $.ajax({
                type : "POST",
                url : obj.url,
                data : obj.data,
                dataType : "text",
                // contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
                beforeSend : function(data, frm, opt){

                },
                success : function(data){
                    obj.callBack(data);
                },
                error : function(jqXHR, textStatus, errorThrown){
                    // console.log(jqXHR);
                    let errMsg = "[ " + jqXHR.status + " ] error가 발생하였습니다.";
                    if (jqXHR.responseText.length > 0) {
                        let resText = JSON.parse(jqXHR.responseText);
                        if (resText.message.length > 0 ) {
                            errMsg += "\nerror message: " + resText.message;
                        }
                    }
                    $.utils.error(errMsg);
                }
            });
        },

        getContextPath : function(){
            return sessionStorage.contextPath;
        },

        dateFormat : function(data){
            var result = "";
            if(data != null){
                result = data.substr(0, 4)+"-"+data.substr(4, 2)+"-"+data.substr(6, 2)+" "+data.substr(8, 2)+":"+data.substr(10, 2);
            }
            return result;
        },

        numberWithCommas : function(x) {
            return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        },

        getURLParameter : function(name) {
            return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
        },

        // 쿠키 저장하기
        setCookie : function(cName, cValue, cDay){
            var expire = new Date();
            expire.setDate(expire.getDate() + cDay);
            cookies = cName + '=' + escape(cValue) + '; path=/ '; // 한글 깨짐을 막기위해 escape(cValue)를 합니다.
            if(typeof cDay != 'undefined') cookies += ';expires=' + expire.toGMTString() + ';';
            document.cookie = cookies;
        },

        // 쿠키 가져오기
        getCookie : function(cName){
            cName = cName + '=';
            var cookieData = document.cookie;
            var start = cookieData.indexOf(cName);
            var cValue = '';
            if(start != -1){
                start += cName.length;
                var end = cookieData.indexOf(';', start);
                if(end == -1)end = cookieData.length;
                cValue = cookieData.substring(start, end);
            }
            return unescape(cValue);
        },

        // 숫자값 자동 콤마 채우기 이벤트
        prKeydown : function(e){
            return $(e).number(true);
        },

        // 파일명 Encoding
        fileDownload : function(e){

            var uri = new URI($(e).parent().find("a[id=downloadBtn]").attr("href"));
            var file = uri.search(true);
            uri.search(file);
            $(e).parent().find("a[id=downloadBtn]").attr("href", uri.toString());

            return true;
        },

        checkPasswd: function(str){
            var pw = str;
            var num = pw.search(/[0-9]/g);
            var eng = pw.search(/[a-z]/ig);
            var spe = pw.search(/[`~!@@#$%^&*|₩₩₩'₩";:₩/?]/gi);
            if ( pw.search(/₩s/) != -1 ) {
                alert("비밀번호는 공백없이 입력해주세요.");
                return false;
            } else if ( num < 0 || eng < 0 || spe < 0 || pw.length < 8 ) {
                alert("영문,숫자, 특수문자를 혼합하여 8자리 이상 입력해주세요.");
                return false;
            }
            return true;
        },

        masking:{
            id: function(str){
                if ( str == 'NA' || str == 'N/A' || str == '없음' || str == "" || str == null) {
                    return "-";
                } else if( str.length == 1) {
                    return str;
                } else if ( str.length > 1 ) {
                    var arr = str.split('');
                    for (var i = 1; i < arr.length-1; i++) {
                        arr[(str.length)-i] = '*';
                    }
                    return arr.join('');
                }
            },

            name: function(str){
                if ( str == 'NA' || str == 'N/A' || str == '없음' || str == ''  ) {
                    return "-";
                } else if ( str.length == 1) {
                    return str;
                } else if ( str.length > 1 ) {
                    var arr = str.split('');
                    arr[str.length-2] = '*';
                    return arr.join('');
                }
            },
            names: function(str){
                if ( str == 'NA' || str == 'N/A' || str == '없음' || str == ''  ) {
                    return "-";
                } else if ( str.length == 1) {
                    return str;
                } else if ( str.length > 1 ) {
                    var arr = str.split(',');
                    for (var i = 0; i < arr.length; i++) {
                        var mask = arr[i].replace(arr[i][arr[i].length-2], '*');
                        arr[i] = mask;
                    }
                    return arr.join(',');
                }
            },
            email: function(str){
                if ( str == 'NA' || str == 'N/A' || str == '없음' || str == ''  ) {
                    return "-";
                } else if ( str.length == 1) {
                    return str;
                } else if ( str.length > 1 ) {
                    var arr = str.split('@');
                    var a = arr[0].substring(0,2);
                    for (var i = 1; i < arr[0].length; i++) {
                        a += arr[0][(str.length)-i] = '*';
                    }
                    arr[0] = a;
                    return arr.join('@');
                }
            },
            address: function(str){

                var strArr = str.split(' ');
                var maskStr = '';
                if ( str == '-' ) {
                    return str;
                } else {
                    var strArr = str.split(' ');
                    for ( var i=1; i<strArr.length-1; i++ ) {
                        maskStr += strArr[i] + ' '
                    }
                    maskStr += '***';
                    return maskStr;
                }
            },
            phone: function(str){
                if ( str == 'NA' || str == 'N/A' || str == '없음' || str == '' || str == null ) {
                    return "-";
                } else if ( str.length == 1) {
                    return str;
                } else if ( str.length > 1 ) {
                    var arr = str.split('-');
                    if (arr[1].length == 3) {
                        arr[1] = '***';
                    } else {
                        arr[1] = '****';
                    }
                    arr[2] = '****';
                    return arr.join('-');
                }
            },
        }

    }
})();
