﻿// PopUp Resize
nWndHeight = -1;
nWndWidth = 600;
nTop = -1;

// Top Setting
function setNTop() 
{
	if (window.XMLHttpRequest) {
		nTop = 63;
	} 
	else{
		nTop = 20;
	}
}

// Resize Window
function resize_wnd (calltype)
{
	setNTop();
	try 
	{
		if ('object' != typeof(document.all.blank_img))
			return;
		if (document.all.blank_img.style.display == 'none')
			document.all.blank_img.style.display = 'block';

		if (nWndWidth == 600) nWndWidth = document.body.offsetWidth + 8;
		
		var nHeight = 0;
		if (document.all.blank_img) nHeight = document.all.blank_img.offsetTop + nTop;
		document.all.blank_img.style.display = 'none';
		var maxHeight = screen.availHeight;

		if (nHeight > maxHeight) {
			nHeight = maxHeight;
			document.body.scroll= 'yes';
		}
		if (nWndHeight>0)
			nHeight = nWndHeight;
		window.resizeTo(nWndWidth + 10, nHeight);
	} 
	catch(e) 
	{
		if (calltype != 'recall')
		{
			if (typeof(addOnloadHandle) == 'function')
				addOnloadHandle("resize_wnd(\"recall\")");
			else
				setTimeout("resize_wnd(\"recall\")", 1500);
		}
	}
}

// Autifit Window
function auto_fit_wnd (nNewWidth, nNewHeight)
{
	if (nNewWidth) nWndWidth = nNewWidth;
	if (nNewHeight) nWndHeight = nNewHeight;
	document.writeln("<iframe width='100' height='100' frameborder=0 marginheight=0 marginwidth=0 scrolling=no id='blank_img' src='about:blank' onload='resize_wnd();'></iframe>\n");
}

// Cookie Util
// Cookie setting시, ts_setCookieField
// Cookie get ts_getCookie
var g_cookie1day = 86400;
var g_domainname = "tstudycorp.com";

// Delete Cookie
function ts_deleteCookie(name)
{
	var value = "";
	var exp = new Date();
	if((name==null)||(value==null)) return false;
	exp.setTime(exp.getTime());
	document.cookie = name+"="+escape(value)+"; "+"expires="+exp.toGMTString()+"; path=/; domain="+g_domainname;
} 

// "name"으로 쿠키를 생성(기존에 있던 필드 그대로 저장)
function ts_setCookieField(name, subname, value, sec) 
{
	var oldcookie = ts_getCookie(name, "");

	if(oldcookie == null)
	{
		ts_setCookie(name, subname+"="+value, sec); // "name" 쿠키가 없었으면 생성
	}
	else
	{
		var carray = oldcookie.split("&");
		var fieldfound = false;
		var newcookie = "";
		
		for(var i=0; i<carray.length; i++) // 현재 쿠키에 해당 필드가 있는지 check
		{
			if( carray[i].indexOf(subname+"=") == 0) // field value에 subname+"="이 올 수 있으므로 정확하게 하기위해	
			{
				fieldfound = true;
				carray[i] = subname + "=" + value;
				break;
			}
		}
		
		if(fieldfound) // 이미 쿠키에 해당 필드가 존재
		{
			for(var i=0; i<carray.length; i++)
			{
				if(newcookie == "")
					newcookie += carray[i];
				else
					newcookie += "&" + carray[i];
			}
		}
		else
		{
			newcookie = oldcookie + "&" + subname + "=" + value; // oldcookie는 항상 null이 아님(위의 If문)
		}
	}
}

// "name" 으로 쿠키를 생성
function ts_setCookie(name,value,sec) 
{
	var exp = new Date();
	if((name==null)||(value==null)) return false; 

	if((sec==null)||(sec=="")||(sec=="0")||(sec==0))
		document.cookie = name+"="+value+"; "+"; path=/; domain="+g_domainname;
	else
	{
		exp.setTime(exp.getTime()+(9*60*60*1000)+(sec*1000)); //9시간 차이가 난다. 
		document.cookie = escape(name)+"="+escape(value)+"; "+"expires="+exp.toGMTString()+"; path=/; domain="+g_domainname;
	}
}

// "name" 으로 쿠키를 생성 (not use escape)
function ts_setCookieNoEscape(name,value,sec) 
{
	var exp = new Date();
	if((name==null)||(value==null)) return false; 
	
	if((sec==null)||(sec=="")||(sec=="0")||(sec==0))
		document.cookie = name+"="+value+"; "+"; path=/; domain="+g_domainname;
	else
	{
		exp.setTime(exp.getTime()+sec);
		document.cookie = name+"="+value+"; "+"expires="+exp.toGMTString()+"; path=/; domain="+g_domainname;
	}
}

// subname == "" or null then return cookie row else return cookie field in cookie row
function ts_getCookie(name, subname) { 
	var prefix = name + "=";
	var cookieStartIndex = document.cookie.indexOf(prefix);
	if (cookieStartIndex == -1)
		return null;
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1)
		cookieEndIndex = document.cookie.length;
		
	if(subname == null || subname == "" || subname == 'undefined')
		return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex)); // cookie row
	else
		return ts_getSubCookie(subname, unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))); // cookie field in cookie row
}

 // called from ts_getCookie function
function ts_getSubCookie(name, ckstring){
	var prefix = name + "=";
	var cookieStartIndex = ckstring.indexOf(prefix);
	if (cookieStartIndex == -1)
		return null;
	var cookieEndIndex = ckstring.indexOf("&", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1)
		cookieEndIndex = ckstring.length;
		
	return ckstring.substring(cookieStartIndex + prefix.length, cookieEndIndex);
}

// Open Window
function openWin(url, name, w, h)
{
	var vleft = (screen.width - w)/2;
	var vtop = (screen.height - h)/2-150;
	var oWin = window.open(url, name, "scrollbars=no, resizable=yes, width="+w+", height="+h+", top="+vtop+", left="+vleft );
	try{ oWin.focus(); }catch(Exception){}
}

// Close Window
function closeWin()
{
	window.close();
}
