var secureWin = null;
var authTimer = null;
var waitTimer = null;

function selectProvider(name, pattern, link)
{
	var REGEXP = /{/;

	//display appropriate provider panel
	if(pattern == "local")
	{
		displayLocalLoginPanel();
	}
	else if(pattern == "remote")
	{
		displayRemoteLoginWindow('', link);
	}
	else if(pattern.search(REGEXP) == -1)
	{
		displayRemoteLoginWindow(pattern, link);
	}
	else //assume openid pattern needing input
	{
		displayOpenIDPanel(name, pattern, link);
	}
	
	return false;
}

function displayLocalLoginPanel(link)
{
	document.getElementById('id_providerOptions').style.display = 'none';
	document.getElementById('id_localLogin').style.display = 'block';
	
	return false;
}

function displayRemoteLoginWindow(identifier, link)
{
	document.getElementById('id_providerOptions').style.display = 'none';
	document.getElementById('id_windowLaunch').style.display = 'block';

	//launch secure window
	var childWidth = 800;
	var childHeight = 600;
	var left = Math.floor((screen.width - childWidth) /2);
	var top = Math.floor((screen.height - childHeight) /2);
	
	//getlink = link + '&openid=' + escape(identifier);
	
	secureWin = window.open('', 'secureWin', 'screenX=' + left + ', screenY=' + top + ', left=' + left + ',top=' + top + ',width=' + childWidth + ',height=' + childHeight + ',scrollbars=no,resizeable=no,toolbar=no,status=no,location=no,menubar=no,copyhistory=no,directories=no');
	secureWin.document.write('<html><head></head><body onload="document.rpxForm.submit();" >');
	secureWin.document.write('<div class="loading"></div>');
	secureWin.document.write('<div style="display:none;" >');
	secureWin.document.write('<form method="POST" id="rpxForm" name="rpxForm" action="' + link + '" >');
	secureWin.document.write('	<input type="text" name="openid_identifier" value="' + identifier + '" />');
	secureWin.document.write('</form>');
	secureWin.document.write('</div></body></html>');
	secureWin.document.close();
	
	secureWin.focus();
	
	authTimer = setInterval("checkSecureWin()", 1000);
	
	return false;
}

function displayOpenIDPanel(name, pattern, link)
{
	document.getElementById('id_providerOptions').style.display = 'none';
	
	//ROD HERE - display pattern correctly - prefix {input:size} postfix
	
	document.getElementById('id_openidLogin').style.display = 'block';
	
	return false;
}

function submitOpenID(name, openid, link)
{
	document.getElementById('id_openidLogin').style.display = 'none';
	displayRemoteLoginWindow(openid, link);
	
	return false;
}

function authenticateResponse(response)
{
	clearInterval(authTimer);
	secureWin.close();
	
	//Coming from a trusted source, so eval should be sufficient
	//ROD HERE - at least use a REGEX validation
	var responseJSON = eval('(' + response + ')');
	
	if(responseJSON.status == 'authenticated')
	{
		//Show success panel
		document.getElementById('id_windowLaunch').style.display = 'none';
		document.getElementById('id_welcomeUser').style.display = 'block';
		
		waitTimer = setTimeout('showNextSteps()', 2000);
	}
	else
	{
		//Show Failed Box
		document.getElementById('id_windowLaunch').style.display = 'none';
		document.getElementById('id_failedLogin').style.display = 'block';
	}
}

function checkSecureWin()
{
	if (secureWin == null || secureWin.closed)
	{
		clearInterval(authTimer);
		
		//Show Failed Box
		document.getElementById('id_windowLaunch').style.display = 'none';
		document.getElementById('id_failedLogin').style.display = 'block';
		
		waitTimer = setTimeout('cancelAuthentication()', 1500);
	}
	
	return false;
}

function showNextSteps()
{
	document.getElementById('id_welcomeUser').style.display = 'none';
	document.getElementById('id_nextSteps').style.display = 'block';
	
	return false;
}

function cancelAuthentication()
{
	parent.cancelAuthentication();
	
	return false;
}
