/*
	structure of function refers to lecture of
		http://user.chollian.net/~spacekan/
		(found first on empas.com with the words "ÀÚ¹Ù½ºÅ©¸³Æ® ÄíÅ°")

	created by kz <Keizi@mail.co.kr> on http://kz.mpecc.net/.
	all right reserved. 2001/04/16 about am 01 - am 03
	usage, modification, distribution is all free with this notification.
	
	updated 2004/05/12 by jonathan clark
	now sets expiration
*/

function setCookie( name, value )
{
	// Add expiration to cookie
	var expireDate = new Date();
	expireDate.setTime( expireDate.getTime() + (90 * 24 * 60 * 60 * 1000) ); // 90 days in milliseconds
	var expiration = "; expires=" + expireDate.toGMTString();
	
	// '=' is identifier between name and value for real cookie.
	document.cookie = name + '=' + value + expiration;
}
function getCookie( name, value )
{
	var flag = document.cookie.indexOf( name+'=' )
	if( flag != -1 ) {
		flag += name.length + 1
		end = document.cookie.indexOf( "; ", flag )
		if( end == -1 ) end = document.cookie.length
		return document.cookie.substring( flag, end )
	}
	return ""
}

function setSubCookie( uName, name, value )
{
	uValue = getCookie( uName )
	if( uValue ) {
		// ':' is breaker for sub cookie each
		uCookies = uValue.split( ':' )
		var found = -1 // whether sub-cookie for the name is present
		for( c = 0; c < uCookies.length; c++ )
		{
			// '-' is identifier between name and value for sub cookie.
			t = uCookies[c].split( '#' )
			tName = t[0]
			tValue = t[1]
			if( tName == name ) {
				found = 1
				tValue = value
				t[1] = tValue
				uCookies[c] = t.join( '#' )
			}
		}
		if( found == -1 )
			uCookies[uCookies.length] = name + '#' + value
		uValue = uCookies.join( ':' )
	} else {
		uValue = name + '#' + value
	}
	// update real cookie
	setCookie( uName, uValue )
}
function getSubCookie( uName, name )
{
	uValue = getCookie( uName )

	var flag = uValue.indexOf( name+'#' )
	if( flag != -1 ) {
		flag += name.length + 1
		end = uValue.indexOf( ':', flag )
		if( end == -1 ) end = uValue.length
		return uValue.substring( flag, end )
	}
}

