function setKookie(name,val,exp) {
	var Kval = val + "|";
	Kval = escape(Kval);

	var kookie = name+"="+Kval+
		"; domain=.discovery.com; path=/;"
	if (exp) kookie += " expires="+exp
	document.cookie = kookie
}

function getKookie(name) {
	var nameStr = name + "=";
	var maxLen = document.cookie.length
	var i = 0
	while (i < maxLen) {
		var j = i + nameStr.length
		if (document.cookie.substring(i,j) == nameStr) {
			var cookieEnd = document.cookie.indexOf(";",j);
			if (cookieEnd == -1) {
				cookieEnd = document.cookie.length;
			}
		return unescape(document.cookie.substring(j,cookieEnd));
		}
	i++
	}
	return false;
}

var over18 			= false;
var one_yr 				= (1000*60*60*24*365);	//one year
var nuf_yrs 		= (one_yr*18); 					//made it!

function ofAge(yr,mon,day) {

	var now 				= new Date();
	var time_now 		= now.getTime();
	var date_eligible = new Date(time_now - nuf_yrs);
	var time_eligible	= date_eligible.getTime();
	var user_bdate		= new Date(yr,mon,day);
	var user_time 		= user_bdate.getTime();
	var user_age 		= (time_now - user_time);

	over18 = (user_age >= time_eligible);

	//debug
	//alert("user_bdata="+user_bdate+"\ndate_elig="+date_eligible+"\nover18 = " + over18);
	return over18;
	
}

var userName;

function chkFrm(frm) {
	var ufld = frm.user_name;
	var uval = ufld.value;
	if (uval == "") {
		alert("Please enter a User Name");
		ufld.focus(); return false;
	} else {
		userName = uval;
	}

	var user_day = frm.day[frm.day.selectedIndex].value;
	var user_month = (frm.month[frm.month.selectedIndex].value);
	var user_year  = frm.year.value;

	if (user_month == "--") {
		alert("Please select your birth month."); frm.month.focus(); return false;
	}

	if (!user_day || user_day == "" || user_day == "--") {
		alert("Please select your birth day."); frm.day.focus(); return false;
	}

//	checkDate (frm.year, frm.month, frm.day);

	if (!isYear(user_year)) {
		strMsg = "Please enter a valid year.";
		alert(strMsg);
		frm.year.focus();
		return false;
	}

	if (!isDate (user_year, user_month, user_day) ){
		strMsg  = "The date entered does not form a valid date.\n";
		strMsg += "Please enter a valid Date of Birth.";
		alert(strMsg);
		frm.month.focus();
		return false;
	}

	if (!ofAge(user_year,user_month,user_day)) userName = "Guest";

	setKookie('lounge',userName);

	return userName;

}

function chatInit() { 						//this function is currently defunct - the logic is being performed in chat.jsp
	var cookie = getKookie('lounge');
	var cookieVals = new Array();
	if (cookie) {
		cookieVals = cookie.split ("|");
		over18 = cookieVals[0];
	}
	document.getname.user_name.focus(); 
}


function popup(url,w,h,s,r) {

	var detect = navigator.userAgent.toLowerCase();
	if (detect.indexOf("safari") != -1) { w -= 40; h -= 40; }

	if (!s) { var s = 0; }
	if (!r) { var r = 0; }
	var winleft = 5;
	var wintop = 5;
	//var wintop = (screen.height - h) - 50;
	//var winleft = (screen.width - w) / 2;
	//var wintop = (screen.height - h) / 2;
	var agent = navigator.userAgent;
	var windowName = 'Chat_Lounge';
	var params = "";
		 params += "scrollbars="+s;
		 params += ",resizable="+r;
		 params += ",left="+winleft+",top="+wintop;
		 params += ",width="+w;
		 params += ",height="+h;

	var pup = window.open(url,windowName,params);

	if (agent.indexOf("Mozilla/2") != -1 && agent.indexOf("Win") == -1) pup = window.open(url, windowName , params)
	if (!pup.opener) pup.opener = window.opener
	pup.focus();
	return false;
}

function loungeInit(url,startDate,endDate) {

	//debug
	//alert("now="+now+"\nstartDate="+startDate+"\nendDate="+endDate);

	if (startDate)  {
		var now = new Date();
		if ( now.getTime() >= startDate.getTime() && now.getTime() <= endDate.getTime() ) {
			return popup(url,468,540,0,1);
		} else { return false; }
	} else {
		return popup(url,468,540,0,1);
	}
}

