﻿// JScript 파일
IncludeCSS("/popup/css/style.css")	//팝업 스타일시트 인클루드

var Popup = Class.create({
	initialize: function (idx, pTop, pLeft, pWidth, pHeight, pScrollbars, autosize, Body) {
		this.idx = idx;
		this.pTop = pTop;
		this.pLeft = pLeft;
		this.pWidth = pWidth;
		this.pHeight = pHeight;
		this.pScrollbars = pScrollbars;
		this.autosize = autosize;
		this.Body = Body;
		
		this.oPopup = document.createElement("div");
	},
	
	showPop: function () {
		this.oPopup.id = "LayerPopup"
		this.oPopup.style.zIndex = "10000" + this.idx;
		this.oPopup.style.position = "absolute";
		this.oPopup.style.top = this.pTop;
		this.oPopup.style.left = this.pLeft;
		this.oPopup.style.width = this.pWidth;
		this.oPopup.style.height = this.pHeight;
		this.oPopup.style.overflow = ((this.pScrollbars == "1") ? "auto" : "visible");
		this.oPopup.innerHTML = this.Body;
		
		var _Doc = (document.compatMode != "BackCompat" ? document.documentElement : document.body);
		
		_Doc.appendChild(this.oPopup);
	},
	
	hidePop: function () {
		this.oPopup.style.display = "none";
	}
});

function initPopup(){
	URL = "/popup/popupcheck.asp";
	params = "";
	
	new Ajax.Request(URL, {
		parameters : params,
		
		onSuccess: function (xmlHttp) {
			Rst = xmlHttp.responseText;
			
			if(Rst != ''){
				var tPop = eval(Rst)
				
				for(j = 0; j < tPop.length; j++){
					var pInfo = tPop[j];
					
					idx = pInfo[0];
					ptype = pInfo[1];
					pTop = pInfo[2];
					pLeft = pInfo[3];
					pWidth = pInfo[4];
					pHeight = pInfo[5];
					pToolbar = pInfo[6];
					pLocation = pInfo[7];
					pStatus = pInfo[8];
					pMenubar = pInfo[9];
					pScrollbars = pInfo[10];
					pResizable = pInfo[11];
					pClose = pInfo[12];
					autosize = pInfo[13];
					
					var pName = "Popup"+idx
					var eventCookie = getCookie(pName); 
					
					if(ptype == "1"){
						if (eventCookie != "no") 
							Wopen(idx, pTop, pLeft, pWidth, pHeight, pToolbar, pScrollbars, pStatus, pResizable, pLocation, pMenubar);
					}else if(ptype == "0"){
						if (eventCookie != "no") 
							Lopen(idx, pTop, pLeft, pWidth, pHeight, pScrollbars, autosize);
					}
				}
			}
		},
		
		onFailure: function (xmlHttp) {
			alert("Ajax연동 오류");
		}
	});
}

function getCookie(name) { 
    var Found = false 
    var start, end 
    var i = 0 

    while(i <= document.cookie.length) { 
      start = i 
      end = start + name.length 
      if(document.cookie.substring(start, end) == name) { 
          Found = true 
          break 
      }
      
      i++ 
    } 

    if(Found == true) { 
      start = end + 1 
      end = document.cookie.indexOf(";", start) 
      
      if(end < start) 
          end = document.cookie.length 
      
      return document.cookie.substring(start, end) 
    } 
    return "" 
}

function setCookie( name, value, expiredays ){ 
		var todayDate = new Date(); 
		todayDate.setDate( todayDate.getDate() + expiredays ); 
		document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";" 
}

function Wopen(idx, pTop, pLeft, pWidth, pHeight, pToolbar, pScrollbars, pStatus, pResizable, pLocation, pMenubar) {
	Url = "/popup/popup.asp?idx="+idx
	Name = "Popup"+idx
	Features = "top="+pTop+",left="+pLeft+",width="+pWidth+",height="+pHeight+",scrollbars="+pScrollbars+",toolbar="+pToolbar+",status="+pStatus+",resizable="+pResizable+",menubar="+pMenubar+",location="+pLocation;
	
  window.open(Url, Name, Features)
}

var popupBox = [];

function Lopen(idx, pTop, pLeft, pWidth, pHeight, pScrollbars, autosize){
	try{
		URL = "/popup/getPopup.asp";
		params = "idx=" + idx;
		
		new Ajax.Request(URL, {
			parameters : params,
			
			onSuccess: function (xmlHttp) {
				Rst = xmlHttp.responseText;
				
				if(Rst != ''){
					if(autosize == "1"){
						var _Doc = (document.compatMode != "BackCompat" ? document.documentElement : document.body);
						
						var divEl = document.createElement("div");
						divEl.style.position = "absolute";
						divEl.style.border = "1px solid #6f6f6f";
						divEl.style.left = "0px";
						divEl.style.top = "0px";
												
						_Doc.appendChild(divEl);
						
						divEl.innerHTML = Rst;
						
						pWidth = divEl.offsetWidth;
						pHeight = divEl.offsetHeight;
						
						_Doc.removeChild(divEl);
					}
					
					popupBox[idx] = new Popup(idx, pTop, pLeft, pWidth, pHeight, pScrollbars, autosize, Rst);
					popupBox[idx].showPop();
					
					HideSelectBox();
				}
			},
			
			onFailure: function (xmlHttp) {
				alert("Ajax연동 오류");
			}
		});
	}catch(e) {}
}

function HideSelectBox(){
	var box = document.getElementsByTagName("select")
	
	for(i = 0; i < box.length; i++){
		box[i].style.visibility = "hidden";
	}
}

function ShowSelectBox(){
	box = document.getElementsByTagName("select")
	
	for(i = 0; i < box.length; i++){
		box[i].style.visibility = "visible";
	}
}

function HidePopup(idx){
	popupBox[idx].hidePop();
	
	var Flag = true;
	var pop = document.getElementsByName("LayerPopup")
	for(i = 0; i < pop.length; i++){
		if(pop[i].style.display != "none")
			Flag = false;
	}
	
	if(Flag) ShowSelectBox();
}

function replaceLocation(idx, flag, url){
	if(flag == "2"){
		window.open(url);
	}else if(flag == "1"){
		document.location.replace(url);
	}
}

function CloseWin(idx, name, term){
	setCookie(name, "no" , 1); // 1일동안 쿠키를 보존합니다.
	
	HidePopup(idx);
}

document.observe("dom:loaded", function () {initPopup()});

