﻿/// <reference path="jquery-1.4.2.min-vsdoc.js" />
/*
Common Script
Dependency : jquery-1.4.2.min.js, AjaxObj.js
*/

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

/* Checker */
// is null or Empty
String.prototype.IsNullOrEmpty = function() {
    var strText = this.trim();
    for (var i = 0; i < strText.length; i++) {
        if ((strText.charAt(i) != "\t") && (strText.charAt(i) != "\n") && (strText.charAt(i) != "\r")) { return false; }
    }
    return true;
};

// Replace All
String.prototype.replaceAll = function(str1, str2) {
    return this.replace(new RegExp(str1, "gi"). str2);
}

// remove
String.prototype.remove = function(regix) { return (regix == null) ? this : eval("this.replace(/[" + regix.meta() + "]/g, \"\")"); }

// is Identity
String.prototype.IsUserId = function() { return (/^[0-9a-zA-Z_-]{4,12}$/).test(this.remove(arguments[0])); }

// is Password
String.prototype.IsPassWord = function () { return (/^(?=([a-zA-Z]+[0-9]+[a-zA-Z0-9!,@,#,$,%,^,&,*,?,_,~]*|[a-zA-Z]+[!,@,#,$,%,^,&,*,?,_,~]+[a-zA-Z0-9!,@,#,$,%,^,&,*,?,_,~]*|[0-9]+[a-zA-Z]+[a-zA-Z0-9!,@,#,$,%,^,&,*,?,_,~]*|[0-9]+[!,@,#,$,%,^,&,*,?,_,~]+[a-zA-Z0-9!,@,#,$,%,^,&,*,?,_,~]*|[!,@,#,$,%,^,&,*,?,_,~]+[0-9]+[a-zA-Z0-9!,@,#,$,%,^,&,*,?,_,~]*|[!,@,#,$,%,^,&,*,?,_,~]+[a-zA-Z]+[a-zA-Z0-9!,@,#,$,%,^,&,*,?,_,~])$).{6,20}/).test(this.remove(arguments[0])); }

// is Number  arguments[0] is permit String
String.prototype.IsNumber = function() { return (/^[0-9]+$/).test(this.remove(arguments[0])); }

// is Email
String.prototype.IsEmail = function() {
    return (/[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+(\.[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+)*@[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+(\.[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+)*/).test(this.remove(arguments[0]));
};

// is Phone
String.prototype.IsPhone = function() 
{
    var delimiter = arguments[0] ? arguments[0] : "";
    return eval("(/(0[1-9]{1}[0-9]{0,1})" + delimiter + "[0-9]{1}[0-9]{2,3}" + delimiter + "[0-9]{4}$/).test(this)");        
}

// is Main Phone (대표전화)
String.prototype.IsMainPhone = function() 
{
    var delimiter = arguments[0] ? arguments[0] : "";
    return eval("(/([1-9]{1}[0-9]{3})" + delimiter + "[0-9]{4}$/).test(this)");        
}

// String Byte Length
String.prototype.GetByteLength = function() { 
    var len = 0;
    var str = this.substring(0);
    if (str == null) return 0;
    for (var i = 0; i < str.length; i++) {
        var ch = escape(str.charAt(i));
        if (ch.length == 1) len++;
        else if (ch.indexOf("%u") != -1) len += 2;
        else if (ch.indexOf("%") != -1) len += ch.length / 3;
    }
    return len;
}

// 지정된 바이트로 글자를 잘라 줍니다.
String.prototype.ByteSubstring = function(limitCount) {
    var str = this.substring(0);
    if (str == null) return "";
    var byteCount = 0;
    var retString ="";
    for (var i = 0; i < str.length; i++) {    
        var ch = escape(str.charAt(i));        
        if (ch.length == 1) byteCount++;
        else if (ch.indexOf("%u") != -1) byteCount += 2;
        else if (ch.indexOf("%") != -1) byteCount += ch.length / 3;        
                
        if(byteCount == (limitCount - 1)) {        
            ch = escape(str.charAt(i+1));        
            if (ch.length == 1) {retString = str.substring(0, i + 2);}
            else if (ch.indexOf("%u") != -1) { retString = str.substring(0, i + 1);}
            else if (ch.indexOf("%") != -1) { retString = str.substring(0, i + 1);}
            break;
        } else if(byteCount == limitCount) {
            retString = str.substring(0, i + 1);
            break;
        }
    }
    return retString;   
}

// String Row Count (newline 및 한 줄에 들어갈 수 있는 Byte 체크)
String.prototype.GetRowCount = function(maxColumnByte) {
    var cnt = 0;
    var str = this.substring(0);
    if (str == null) return 0;        
    var cutByte = 0;
    for (var i = 0; i < str.length; i++) {                 
        if(str.charAt(i) == '\n' || cutByte > maxColumnByte) {
            cnt++;
            cutByte = 0;
        } else {
            var ch = escape(str.charAt(i));
            if (ch.length == 1) cutByte++;
            else if (ch.indexOf("%u") != -1) cutByte += 2;
            else if (ch.indexOf("%") != -1) cutByte += ch.length / 3;
        }
    }    
    return cnt;
}


// 정해진 줄수 밑으로 글자를 잘라 줍니다. 
String.prototype.RowSubstring = function(maxRowCount, maxColumnByte) {
    var len = 0;
    var str = this.substring(0);
    if (str == null) return "";
    var retString ="";
    var cutByte =0;
    for (var i = 0; i < str.length; i++) {        
        if(str.charAt(i) == '\n' || cutByte > maxColumnByte)
        {
            len++;
            cutByte =0;
            if( maxRowCount == len){ retString = str.substring(0, i);break;}
        } else {
            var ch = escape(str.charAt(i));
            if (ch.length == 1) cutByte++;
            else if (ch.indexOf("%u") != -1) cutByte += 2;
            else if (ch.indexOf("%") != -1) cutByte += ch.length / 3;
        }
    }
    return retString;
}

// comma 추가
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

// open popup window
// OpenSubWindow('../open/a.aspx', 'id', 100, 100, 'no', 'no');
function OpenSubWindow(url, id, width, height, scroll, resize) {
    var top = (document.documentElement.clientHeight - height) / 2;
    var left = (document.documentElement.clientWidth - width) / 2;
    var size = 'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left;
    var etc = size + ',resizable=' + resize + ',scrollbars=' + scroll + ',status=no';
    reval = window.top.open(url, id, etc);
    if (reval == null) {
        alert('팝업 차단을 허용해 주세요');
    } else {
        reval.focus();
    }
};

// option 1: view
// option any : hidden
function ShowBackLayer(option) {
    var divbacklayer = document.getElementById('divback');

    if (divbacklayer == null) {
        var backdiv = document.createElement('DIV');
        backdiv.className = 'layerback';
        backdiv.id = 'divback';

        try { backdiv.style.opacity = 0.5; } catch (e) { }
        try { backdiv.style.MozOpacity = 0.5; } catch (e) { }
        try { backdiv.style.filter = 'alpha(opacity=80)'; } catch (e) { }
        try { backdiv.style.KhtmlOpacity = 0.5; } catch (e) { }
        document.body.appendChild(backdiv);
        divbacklayer = document.getElementById('divback');
    }

    if (option == 1) {
        var height = document.body.scrollHeight;
        var width = document.body.scrollWidth;
        height = height > document.body.offsetHeight ? height : document.body.offsetHeight;
        width = width > document.body.offsetWidth ? width : document.body.offsetWidth;

        divbacklayer.style.height = height + 'px';
        divbacklayer.style.width = width + 'px';
        divbacklayer.style.display = 'block';
    } else {
        divbacklayer.style.display = 'none';
    }
}

// 브라우져 즐겨찾기 추가
function BookMark(title, url) {
    if (window.opera && window.print) { // opera

        var elem = document.createElement('a');
        elem.setAttribute('href', url);
        elem.setAttribute('title', title);
        elem.setAttribute('rel', 'sidebar');
        elem.click();
    }
    else if (document.all) // ie
    {                        
        var w = window.external;
        if (w != null) {
            w.AddFavorite(url, title);
        } else {
            window.open(url, '', '');
        }
    }
    else if (window.sidebar) // firefox
    {
        window.sidebar.addPanel(title, url, "");                        
    } else {
        window.open(url, '', '');
    }
}


// 유효성 체크
var CommonValidator = {
    // 해당 엘리먼트 값이 Null or Empty인지 여부
    CheckIsNullOrEmpty: function(element, msgId) {
        if ($('#' + element).val().IsNullOrEmpty()) {
            AJAXObj.AlertErrorMsg(msgId);
            $('#' + element).focus();
            return false;
        }
        return true;
    },

    // input check box 체크 여부
    CheckInputCheck: function(element, msgId) {
        if ($('#' + element).is(':checked') == false) {
            AJAXObj.AlertErrorMsg(msgId);
            $('#' + element).focus();
            return false;
        }
        return true;
    },

    // 해당 엘리먼트 값의 길이 체크
    CheckLength: function(element, len, msgId) {
        if ($('#' + element).val().length != len) {
            AJAXObj.AlertErrorMsg(msgId);
            $('#' + element).focus();
            return false;
        }
        return true;
    },

    // 검색 키워드 체크
    CheckSearchKeyword: function(keyword) {
        return this.CheckIsNullOrEmpty(keyword, 'in_Search_Keyword');
    },
    
    // 해당 엘리먼트 값의 길이 범위 체크 - Overloading
    CheckLengthPeriod: function(element, startLen, endLen, msgId) {
        if ($('#' + element).val().GetByteLength() < startLen || $('#' + element).val().GetByteLength() > endLen) {
            // 인자가 4개이면 에러일 경우 alert 메시지
            if (arguments.length == 4) {
                AJAXObj.AlertErrorMsg(msgId);
            }
            $('#' + element).focus();
            return false;
        }
        return true;
    } 
}

var CookieManager = {
    SetCookie: function(domain, name, value, addDate) {
        var expireDate = new Date();
        expireDate.setDate(expireDate.getDate() + addDate);
        document.cookie = name + "=" + escape(value) + "; path=/; domain=" + domain + " ; expires=" + expireDate.toGMTString() + ";";
    },
    
    GetCookie: function(name) {
        var nameOfCookie = name + "=";
        var x = 0;
        while (x <= document.cookie.length) {
            var y = (x + nameOfCookie.length);
            if (document.cookie.substring(x, y) == nameOfCookie) {
                if ((endOfCookie = document.cookie.indexOf(";", y)) == -1)
                    endOfCookie = document.cookie.length;
                return unescape(document.cookie.substring(y, endOfCookie));
            }
            x = document.cookie.indexOf(" ", x) + 1;
            if (x == 0)
                break;
        }
        return "";
    }
};

function image_auto_resize(this_s,width){ 
 var ta_image = new Image(); 
 ta_image.src = this_s.src; 
  if(!width){this_s.removeAttribute('width'); 
  this_s.style.width='auto';} 
  else if(width < ta_image.width){ 
  this_s.width = width; 
  }else{ 
  this_s.width = ta_image.width; 
  } 
  

  
} 
