var tID = 0;
var tCount = 0;
var regAction = 0;
var requestID = 0;


function readCookie(name)
{
	if (document.cookie == '') return false;
	else
	{
		var c0, c1;
		var cookie = document.cookie;
		c0 = cookie.indexOf(name);
		if(c0 != -1)
		{
			c0 += name.length + 1;
			c1 = cookie.indexOf(';', c0);
			if(c1 == -1) c1 = cookie.length;
			return unescape(cookie.substring(c0,c1).replace('+',' '));
		}
		else return false;
    }
}

if (applicationURL == null) applicationURL = "";
if (folderList == null) folderList = "";
//document.write("<img src='/images/spacer.gif' width=15 height=15 id='regPixel' name='regPixel'>");
document.write("<iframe id='comm' name='comm' border=0 width=1 height=1 style='width:1px;height:1px;border:0px;'></iframe>");
document.write("<table width=100% cellpadding=0 cellspacing=0 border=0><form id='Login' action='"+(applicationURL+folderList)+"/index.io' onsubmit='return sendReg();' method='post'>");
document.writeln("<input type='hidden' name='Reg.Login.Action' value='verifyUser'>");
document.writeln("<input type='hidden' name='Reg.Login.Return' value='2'>");
document.writeln("<tr><td><div name='dashboardDiv' id='dashboardDiv' class='Dashboard'>"+buildDashboard()+"</div></td></tr>");
document.writeln("</form></table>");



//document.writeln("<div id='infoBlock' class='Dashboard'></div>");


function loadDashboard()
{
	d = document.getElementById("dashboardDiv");
	html = buildDashboard();
	d.innerHTML = html;
}

function buildDashboard()
{
	html = '';

	userID = readCookie(applicationName+'.USER.ID');
	userName = readCookie(applicationName+'.USER.USERNAME');if (!userName) userName="";

	if (userID==0)
	{
		html = html + "<table width=100% cellpadding=0 cellspacing=2 border=0>";
		html = html + "<tr>";
		html = html + "<td align=right class='FanZoneForm'><small>User</small></td>";
		html = html + "<td><input class='InputSmall' name='Display.Form.Login.UserName' size=10 maxlength=50 value="+userName+"></td>";
		html = html + "</tr>";
		html = html + "<tr>";
		html = html + "<td align=right class='FanZoneForm'><small>Pass</small></td>";
		html = html + "<td><input class='InputSmall' name='Display.Form.Login.Password' type='password' size=10 maxlength=50></td>";
		html = html + "</tr>";
		html = html + "<tr>";
		html = html + "<td align=center colspan=2 class='FanZoneForm'>Remember Me <input type='checkbox' value=1 name='KeepCookie' checked class='Radio'> <input type='submit' class='FanZoneButton' value='LOGIN' onclick='return sendReg();'></td>";
		html = html + "</tr>";
		html = html + "<tr>";
		html = html + "<td align=left class='FanZone'><a href='"+accountURL+"';' >register</a></td>";
		html = html + "<td align=right class='FanZone'><a href='"+passwordURL+"';' >forgot password?</a></td>";
		html = html + "</tr>";
		html = html + "</table>";
	}
	else
	{

		html = html + "<table width=100% cellpadding=0 cellspacing=2 border=0>";
		html = html + "<tr>";
		html = html + "<td colspan=2 align=center class='FanZone'>welcome <b><a href='"+accountURL+"' style='font-size:10px;'>"+userName+"</a></b></td>";
		html = html + "</tr>";
		html = html + "<tr>";
		html = html + "<td align=left class='FanZone'><a href='"+logoutURL+"';' onclick='return sendLogout();'>logout</a></td>";
		html = html + "<td align=right class='FanZone'><a href='"+accountURL+"';' >view account</a></td>";
		html = html + "</tr>";
		html = html + "</table>";
	}
	return html;
}

function readCommWindow(w,name)
{
	if (eval("w."+name))
		return eval("w."+name);
	else
		return '';
}

function getResponse()
{
	var r= '';
//	r = readCookie('R_'+requestID);
	r = readCommWindow(window.comm,'r_'+requestID);
//alert('r_'+requestID+" :: "+r);
	if (r == 1)
		loadDashboard()
	else if (r > '')
		alert(r.replace(new RegExp("\\+","g") , " "));
	else if (tCount++<150)
		tID = setTimeout("getResponse()",100);
	else
		alert('Sorry, but there was no response from the server.  Please try again');
}


function sendReg(f)
{
	f=document.forms["Login"];
	d = new Date();
	requestID = d.getTime();
	cgi = 't='+requestID;
	cgi = cgi + '&userName=' + f.elements["Display.Form.Login.UserName"].value;
	cgi = cgi + '&password=' + f.elements["Display.Form.Login.Password"].value;
	cgi = cgi + '&KeepCookie=' + f.elements["KeepCookie"].checked;
	src = applicationURL+'/click/reg/login/stub.io?'+cgi;
//info = document.getElementById("infoBlock");info.innerHTML="<a href="+src+" target=test>"+src+"</a>";
//	document.images["regPixel"].src = src;
	window.comm.location.href = src;
	if (tID > 0) clearTimeout(tID);
	tID = setTimeout("getResponse()",100);
	regAction = 1;
	tCount=0;
	return false;
}


function sendLogout()
{
	d = new Date();requestID = d.getTime();
	src = applicationURL+'/click/reg/logout/stub.io?t='+requestID;
//info = document.getElementById("infoBlock");info.innerHTML="<a href="+src+" target=test>"+src+"</a>";
//	document.images["regPixel"].src = src;
	window.comm.location.href = src;
	if (tID > 0) clearTimeout(tID);
	tID = setTimeout("getResponse()",100);
	regAction = 2;
	tCount=0;
	return false;
}

//loadDashboard();