﻿
var Common = 
{
    loadingImg : "<img src=/images/common/loading.gif>",
    loadinghtmlVisible : true, 
    
    //tag 키 등록
    //@obj : 등록할 오브젝트
    //@toID : 다음 포커스를 이동할 오브젝트 ID 
    tabReg : function (obj, toID) {
        if (toID == null) return;
        var maxLen = obj.getAttribute("maxlength"); 
        if (obj.value.length >= maxLen) {
            this.focusThis(toID);
        }
    },
    
    //해당 id 의 오브젝트에 focus
    //@id : 포커스를 맞출 오브젝트 ID 
    focusThis : function (id) {
        document.getElementById(id).focus();
    },
   
    //XML Element 에서 하위노드의 첫번째 값을 반환하는 함수
    //@obj : node object
    //@nodename : 가져올 자식 노드명  
    getXmlchildvalue : function (obj,nodename)
    {  
        
        var tobj =  obj.getElementsByTagName(nodename)[0];
        if(tobj.firstChild)
       {
            return tobj.firstChild.nodeValue;
       }else
       {
            return "";
       }
       
    },

    //UTF8 인코딩
    //@str : 인코딩할 문자열 
    utfEncoding : function (str)
    {
        return encodeURIComponent(str);
    },
   
    //Unicode 인코딩
    //@str ; 인코딩할 문자열 
    unicodeEncoding : function (str)
    {
        return escape(str);
    },
    
    // 해당 연도, 월의 마지막날을 얻는다.
    //@nYear :  년도
    //@nMonth : 월
    GetEndDay : function(nYear, nMonth)
    {
        try
        {
	        if(nMonth < 1 || nMonth > 12 || nYear <= 0)
		        return 0;
        	
	        if(nMonth == 1 || nMonth == 3 || nMonth == 5 || nMonth == 7 || nMonth == 8 || nMonth == 10 || nMonth == 12)
		        return 31;
        	
	        if(nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11)
		        return 30;
        		
        	
	        // 윤년인지 검사	
	        if((nYear % 4 == 0 && nYear % 100 != 0) || (nYear % 400 == 0))
		        return 29;
	        else
		        return 28;
		    }
		catch(e)
		{
		    return 100;
		}
    },

  
    //Window Open
    //@targetUrl : 오픈할 Page URL
    //@windowName : 오픈할 Window Handle Name
    //@properties : window.open properties (예: width=400,height=300)  
    popupWindow : function (targetUrl, windowName, properties) 
    {
        var popObj = window.open(targetUrl, windowName, properties);
        popObj.focus();
    },  
   
    
    //화면 중간으로 Window Open
    //@targetUrl : 오픈할 Page URL
    //@windowName : 오픈할 Window Handle Name
    //@properties : window.open properties (예: width=400,height=300)  
    popupWindowInCenter : function (targetUrl, windowName, properties) 
    {
        var arr = properties.replace(/=/ig,',').split(',');
        var childWidth, childHeight;
        for(var i=0; i<arr.length; i++)
        {
            if(arr[i].toLowerCase() == "width")
                childWidth = eval(arr[i+1]);
            if(arr[i].toLowerCase() == "height")
                childHeight = eval(arr[i+1]);
        }
        //var childTop = event.clientY + 8;
        //var childLeft = event.clientX + 8;
        var childTop = (screen.height-childHeight)/2 - 50;     //사이즈가 아래가 가리는 경향이 있어서 50을 줄임. 
        var childLeft = (screen.width-childWidth)/2;
        properties += ',top=' + childTop + ',left='+childLeft;
        var popObj = window.open(targetUrl, windowName, properties);
        popObj.focus();
    },
   
    closeLayer : function(layerstep)
   {
   
        var divid = "popup_window";
	    if(typeof(layerstep)!="undefined")
	    {
	        divid += "" + layerstep;
	    }
	    
        var div = $(divid);
        if(div)document.body.removeChild(div);
        $("divScreenblank").style.display="none";
   },  
   
    popupLayerMousePosition : function(event,width,height,url,exec,layerstep)
   {
        var position_left = 0;
        var position_top = 0;
        

        
        var mouseX, mouseY;
        mouseX = window.event.clientX + document.documentElement.scrollLeft + 3;
        mouseY = window.event.clientY + document.documentElement.scrollTop + 6;
        
        this.popupLayer(mouseY,mouseX,width,height,url,exec,layerstep);
   }, 
   
    //레이어 화면중간 보이기    
    //@width : 레이어 넓이
    //@height : 레이어 높이
    //@url : 레이어URL
    //@exec : 레이어드로잉후 실행되어야 할 함수   
    popupLayerInCenter : function (width,height,url,exec,layerstep)
    {
        //화면의 중간위치 계산하기
        var scleft = document.documentElement.scrollLeft;
	    var sctop = document.documentElement.scrollTop;
    	
	    var screenwidth = document.documentElement.clientWidth ;
	    var screenheight = document.documentElement.clientHeight;
	    var left = parseInt((screenwidth - width)/2) + scleft;
	    var top = parseInt((screenheight - height)/2) + sctop;
	 
	    this.popupLayer(top,left,width,height,url,exec);
    },
    
    //레이어 보이기
    //@top : 상단위치 
    //@width : 레이어 넓이
    //@height : 레이어 높이
    //@url : 레이어URL
    //@exec : 레이어드로잉후 실행되어야 할 함수      
    popupLayer : function (top, left, width, height, url, exec,layerstep) 
    {		
   
        /*
        var scleft = document.documentElement.scrollLeft;
        var sctop = document.documentElement.scrollTop;
        var screenwidth = document.documentElement.clientWidth ;
        var screenheight = document.documentElement.clientHeight;
        var getdiv = $("divScreenblank");
        getdiv.style.top = "0px";
        getdiv.style.left = "0px";
        getdiv.style.width = (screenwidth + scleft) + "px";
        getdiv.style.height = document.documentElement.scrollHeight + "px";//(screenheight + sctop) + "px";
        getdiv.style.backgroundColor = "#ffffff"; 
        getdiv.style.display = "block";
        */
         
         
         
	    
	    var divid = "popup_window";
	    if(typeof(layerstep)!="undefined")
	    {
	        divid += "" + layerstep;
	    }
	    
	    var div = $(divid);
	    if(div)document.body.removeChild(div);
	    div = $(divid);
	    
	    
	    
	    if (!div) {
		    div = document.createElement("div"); 
		    div.id = divid;//"popup_window";
		    div.className = "layer";
		    div.style.top = top + "px";
		    div.style.width = width + "px";
		    div.style.height = height + "px";
		    //div.style.overflow = "auto";
		    //div.style.left = (document.body.clientWidth - width)/2 + "px";
		    div.style.left = left + "px";
		    div.style.zIndex = 9990;
		    div.style.position = "absolute";
		    document.body.appendChild(div);
	    } else {
		    div.style.top = top + "px";
		    div.style.left = left + "px";
		    div.style.width = width + "px";
		    div.style.height = height + "px";
		    div.style.overflow = "auto";
		    div.style.left = (document.body.clientWidth - width)/2 + "px";				    
		    Element.show(div);
	    }
	    this.drawRemotehtml(url, div, exec,width,height);	
	    
    },
   
    // 특정 DIV 위치의 내용을 변경
    //@url : layer page url 
    //@dest : layer 드로잉할 object
    //@exec : onsuccess 후 실행되어야 할 함수
    //@width (optional) : 레이어넓이
    //@height (optional) : 레이어높이     
    drawRemotehtml : function (url, dest, exec,width,height){
        var loadinghtml = this.loadingImg;
    	if(typeof(width)!="undefined"&&typeof(height)!="undefined")
    	    loadinghtml = "<table width='" + width + "' cellspacing=1 cellpadding=0 border=0 bgcolor='#cccccc'><tr><td bgcolor='#ffffff' height='" + height + "' align='center' style='font-size:10px;font-family:tahoma;'>" + this.loadingImg + " Loading...</td></tr></table>";
    	 
	    new Ajax.Updater(
					     dest, 
					     url, 
					     {
					      asynchronous: true, 
					      method: 'get', 
					      onLoading: function()
					      {
					            /*
					            if(Common.loadinghtmlVisible)
					                Element.update(dest, loadinghtml)
					            else
					            {					                
					                Common.loadinghtmlVisible = true;
					                //Element.update(dest, "");
					             }
					             */
					      }, 
					      onComplete: function(result){					                    
					  				    eval( '(' + exec + ')' ); 
					  			      },
					      evalScripts: true 
					     }
					    );
		
    },
   
   
    // 이벤트추가
    //@instance : 이벤트를 추가할 객체
    //@eventName : 추가할 이벤트명 (예:click)
    //@callbackfunc : 이벤트를 실행할 함수명   
    zAttachEvent : function (instance,eventName,callbackfunc)
    {    
        if(document.addEventListener)
        {
            //support - FireFox 
            instance.addEventListener(eventName,callbackfunc,false);
        }else
        {
            //support - IE 
            instance.attachEvent("on" + eventName,callbackfunc);
        }
    },

    // Early Binding for Window OnLoad Event
    //@init_func : 온로드함수 
    _earlyOnload : function (init_func) { 
        if(document.addEventListener) { // for Mozilla
            document.addEventListener("DOMContentLoaded", function(){eval(init_func)}, false);
        } else { // for other browsers
            window.onload = function(){eval(init_func)};
        }
    },
   
   
   //CheckBox 를 모두 선택하거나 해제합니다.
   //@cbxname : 체크박스명
   //@docheck : 체크여부 결정(true/false)
    SelectAllCheckbox : function(cbxname,docheck)
   {
        var objs = $n(cbxname);
        for(i=0;i<objs.length;i++)
        {
            if(typeof(docheck)=="undefined")
                objs[i].checked = !objs[i].checked;
            else
                objs[i].checked = docheck;                
        }
   },
   
   //이벤트를 추가합니다.(Attach)
   //@obj : 이벤트를 추가할 DOM Object
   //@eventName : 이벤트명 (on 은 제외하고 예:load)
   //@functionNotify : 이벤트 콜백함수
   AddEventHandler : function(obj, eventName, functionNotify) {
        if (obj.attachEvent) {
            obj.attachEvent('on' + eventName, functionNotify);
        }
        else if (obj.addEventListener) {
            obj.addEventListener(eventName, functionNotify, true);
        }
        else {
            obj['on' + eventName] = functionNotify;
        }
    },
   
   //이벤트를 제거합니다.(Detach)
   //@obj : 이벤트를 추가할 DOM Object
   //@eventName : 이벤트명 (on 은 제외하고 예:load)
   //@functionNotify : 이벤트 콜백함수
    RemoveEventHandler : function(obj, eventName, functionNotify) {
        if (obj.detachEvent) {
            obj.detachEvent('on' + eventName, functionNotify);
        }
        else if (obj.removeEventListener) {
            obj.removeEventListener(eventName, functionNotify, true);
        }
        else {
            obj['on' + eventName] = null;
        }
    },
   
   
    //SWF Object 를 생성합니다. (SWFObject 객체 참조)
    //@swfurl : flash url 
    //@swffilename : only filename (abc.swf)
    //@width : flash width
    //@height : flash height    
    CreateSWF : function(swffilename,swfname,width,height)
    {
        window[swfname] = new Object();
        var so = new SWFObject("/images/flash/" + swffilename, swfname, width, height, "8.0.0.0", "#ffffff");
        so.addParam("quality", "high");
        so.addParam("wmode", "transparent");
        so.addParam("allowScriptAccess", "always");
        return so;
    } 
        
}



