﻿// Ajax Common
var READY_STATE_UNINITIALIZED = 1;
var READY_STATE_LOADING = 1;
var READY_STATE_LOADED = 2;
var READY_STATE_INTERACTIVE = 3;
var READY_STATE_COMPLETE = 4;
function getXMLHttpRequest()
{
	var xRequest = null;
	if (window.XMLHttpRequest) {
		xRequest = new window.XMLHttpRequest();
	} else if (typeof ActiveXObject != "undefined") {
		xRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xRequest;
}

// Dialog layer Popup Functions

/*		기본 요구 사항
* <script language="JavaScript" src="prototype.js"></script>
* <div id="alphaDiv" style="display:none; position:absolute; z-index:999;"></div>
* <div id="다이얼로그 박스 아이디" style="display:none; position:absolute; z-index:1000;"></div>
*/

function getViewport(){
	var w=0;
	var h=0;

	if(window.innerWidth) w=window.innerWidth;
	if(document.documentElement.clientWidth){
		var w2 = document.documentElement.clientWidth;
		if(!w || w2 && w2 < w) w=w2;
	}else if(document.body){
		w=document.body.clientWidth;
	}

	if(window.innerHeight) h=window.innerHeight;
	if(document.documentElement.clientHeight) h=document.documentElement.clientHeight;
	else if(document.body) h=document.body.clientHeight;

	return [w,h];
}
function setOpacity(obj,value)
{
	if(typeof(obj)=='string') obj=document.getElementById(obj);
	obj.style.opacity = (value / 100);
	obj.style.MozOpacity = (value / 100);
	obj.style.KhtmlOpacity = (value / 100);
	obj.style.filter = "alpha(opacity=" + value + ")";
}


/*
*	openDialogBox
*
*	startupInfo = { ['center'|'absolute'|'relative'] , { [left], [top] }, { [width], [height] } }
*   closeType = ['clickout' | 'manual' | 숫자]  
*                   => default(없을경우) : clickout    cf. 숫자 - timer로 '숫자'이후 자동닫힘
*/
function openDialogBox ( dialogBoxId, startupInfo, closeType )
{
    var dlg = $(dialogBoxId);
    var alphaDiv = $('alphaDiv');
    if (!dlg||!alphaDiv) return;
    
    // drawing semitransparent layer(alphaDiv)
    var alphaDiv_opacity = 50;
    var alphaDiv_color = '#000';

    alphaDiv.style.background = alphaDiv_color;
    alphaDiv.style.cursor = '';
    setOpacity(alphaDiv,alphaDiv_opacity);
    Position.clone(document.body,alphaDiv);

    // calculating dialogBox position and size
        // preparing
    var dlgPos = new Array(2);
    var dlgSize = new Array(2);
    if ( !startupInfo || !startupInfo[0] ) startupInfo = new Array('center', new Array(2), new Array(2));
    if ( !startupInfo[1] ) startupInfo[1] = new Array(2);

        // set size
    if ( !startupInfo[2] || startupInfo[2][0] < 1 || startupInfo[2][1] < 1 || startupInfo[2][0] == 'undefined' || startupInfo[2][1] == 'undefined' || !startupInfo[2][0] || !startupInfo[2][1] )
    {
        dlg.style.display = 'block';
        var tmp=Element.getDimensions(dlg);
        dlgSize[0]=tmp.width;
        dlgSize[1]=tmp.height;

        if (dlg.childNodes[0]) {
	        dlgSize[0]=dlg.childNodes[0].offsetWidth;
	        dlgSize[1]=dlg.childNodes[0].offsetHeight;
        }
        dlg.style.display = 'none';
    }
    else
    {
        dlgSize=startupInfo[2];
    }

    Position.prepare();
        // set position
    if ( startupInfo[0]=='center' )
    {
        var scrPos=new Array(Position.deltaX,Position.deltaY);
		var scrSize=getViewport();

        startupInfo[1][0]=scrPos[0]+scrSize[0]/2-dlgSize[0]/2;
        startupInfo[1][1]=scrPos[1]+scrSize[1]/2-dlgSize[1]/2;

    }
    else if ( startupInfo[0]=='absolute' )
    {
    }
    else if ( startupInfo[0]=='relative' )
    {
        var scrPos=new Array(Position.deltaX,Position.deltaY);
        startupInfo[1][0]+=scrPos[0];
        startupInfo[1][1]+=scrPos[1];
    }

    // drawing dialogBox
    if ( startupInfo[1] && startupInfo[1][0] && startupInfo[1][1] )
    {
        dlgPos=startupInfo[1];

        dlg.style.left=dlgPos[0]+'px';
        dlg.style.top=dlgPos[1]+'px';

        if(dlgSize[0]>0 && dlgSize[1]>0)
        {
            dlg.style.width=dlgSize[0]+'px';
            dlg.style.height=dlgSize[1]+'px';
        }
    }

    // display divs
    alphaDiv.style.display='block';
    dlg.style.display='block';

    // connect close event
    if ( !closeType || closeType == 'clickout' )
    {
        Event.observe(alphaDiv,'click',function(evt){closeDialogBox(dialogBoxId)},false);
    }
    else if ( closeType == 'manual' )
    {
        alphaDiv.onclick = function(evt) {};
    }
    else if ( isFinite(closeType) )//closeType is numeric
    {
        Event.observe(alphaDiv,'click',function(evt){closeDialogBox(dialogBoxId)},false);
        setTimeout(function(){closeDialogBox(dialogBoxId)},closeType);
    }
}

/*
*       addCloseAlphaDivEvent
*
*/
function addCloseAlphaDivEvent ( obj, evt )
{
    Event.observe(obj,evt,function(evt){closeDialogBox('alphaDivOnly')},false);
}

/*
*       closeDialogBox
*
*/
function closeDialogBox ( dialogBoxId )
{
    var dlg=$(dialogBoxId);
    if(dialogBoxId!='alphaDivOnly'&&!dlg) return;
    var alphaDiv=$('alphaDiv');
    if(dlg){dlg.innerHTML="";dlg.style.display='none';}
    if(dialogBoxId!='sub-popup')if(alphaDiv)alphaDiv.style.display='none';
}

