﻿// Javascript for Default.aspx
var bHasFocus = false;
function authenticate()
{
	document.getElementById("btnAuth").style.cursor = "wait";
	document.body.style.cursor = "wait";
	var aUser = new Array();
	aUser.push(BrowserDetect.browser+" "+BrowserDetect.version);
	aUser.push(BrowserDetect.OS);
	aUser.push(screen.height+"x"+screen.width);
	var sEmail = document.getElementById("ctl00_ContentPlaceHolder1_txtUserName").value;
	var sPassword = document.getElementById("txtPassword").value;
	var bRememberMe = document.getElementById("ctl00_ContentPlaceHolder1_cbxRememberMe").checked;
	progress(-1);
	ret = Default.authenticate(sEmail, sPassword, aUser, bRememberMe, OnComplete, OnTimeOut, OnError);
}
var timeout = null;
function progress(nTimer)
{
	if(nTimer == -1)
	{
		timeout = setTimeout("progress("+(nTimer+1)+");", 500);
	}
	else
	{
		document.body.className = "processing";
		var oProgress = document.getElementById("divProgress");
		oProgress.style.left = '50%';
		oProgress.style.display = 'block';
		var sOutput = "<h3>Processing";
		for(var nIndex = 0; nIndex < nTimer%4; nIndex++)
			sOutput += ".";
		sOutput += "</h3>";
		oProgress.innerHTML = sOutput;
		timeout = setTimeout("progress("+(nTimer+1)+");", 500);
	}
}
function endProgress()
{
	document.body.className = "";
	document.getElementById("divProgress").style.display = 'none';
	clearTimeout(timeout);
}
function forgotPass()
{
	var sEmail = document.getElementById("ctl00_ContentPlaceHolder1_txtUserName").value;
	if(sEmail == "")
		alert("Please enter your email address in the login form and click this link again.\nYour login information will be sent to the provided email address.");
	else
		ret = Default.forgotPassword(sEmail, OnCompleteForgot, OnTimeOut, OnError);
}
function resendActivation()
{
	var sEmail = document.getElementById("ctl00_ContentPlaceHolder1_txtUserName").value;
	if(sEmail == "")
		alert("Please enter your email address in the login form and click this link again.\nYour activation email will be sent to the provided email address.");
	else
		ret = Default.resendActivation(sEmail, OnCompleteActivation, OnTimeOut, OnError);
}
function OnCompleteActivation(arg)
{
	if(arg == "sent")
	{
		alert("Your activation email has been sent to the provided email address.");
	}
	else
	{
		alert("No user information exists for that email address.");
	}
}
function OnCompleteForgot(arg) {
	if(arg)
	{
		alert("You login information has been sent to your email address.");
	}
	else
	{
		alert("No user information exists for that email address.");
	}
}
function OnComplete(arg) {
	var sLocation = window.location.toString();
	sLocation = sLocation.substring(sLocation.indexOf('//')+2);
	sLocation = "http://"+sLocation.substring(0,sLocation.lastIndexOf('/'));
	if(arg.length != 0 && arg[0] == "error")
	{
		endProgress();
		document.getElementById("btnAuth").style.cursor = "pointer";
		document.body.style.cursor = "";
		alert(arg[1]);
	}
	else if(arg.length != 0 && arg[0] == "default")
	{
		if(window.location.toString().indexOf("search=") > 0)
		{
			var sSearchId = window.location.toString().substring(window.location.toString().indexOf('search=')+7);
			window.location = sLocation+"/display.aspx?Strategy="+sSearchId;
		}
		else
			window.location = sLocation+"/display.aspx?Strategy="+arg[1];
	}
	else if(arg.length != 0 && arg[0] == "new")
	{
		window.location = sLocation+"/searchstrategy.aspx";
	}
	else
	{
		alert('An error was encountered.');
	}
}
function OnTimeOut(arg) {
	endProgress();
	alert("TimeOut encountered.");
}
function OnError(arg) {
	endProgress();
	alert("Error encountered.");
}
function submitEnter(evt)
{
	var code;
	if (evt.keyCode) code = evt.keyCode;
	else if (evt.which) code = evt.which;
	if(code == 13 && bHasFocus)
	{
		authenticate();
		return false;
	}
}