var finishUrl = ''
function getResult(success, message, url){
    stopTimer();
	if(success){
	    finishUrl = url;
		redirectSuccess();
	}else{
		alert(message);
		finishUrl = '';
		stopLoading($('loginform'));
	}
}

function redirectSuccess(){
    window.location = finishUrl;
}


function makeRequest(){
	if(loading == false){
	    startLoading($('loginBox'));
		var loginUrl = $('login_url_input').getProperty('value');
		var user = $('username_input').getProperty('value');
		var pass = $('password_input').getProperty('value');
		if(user == '' || user == "Username"){
			alert("You must enter a username to login!");
			stopLoading($('loginform'));
		}else if(pass == '' || pass == 'Password'){
			alert("You must enter a password to login!");
			stopLoading($('loginform'));
		}else{
			var request = eval(loginUrl).replace('&amp;', '&');
			aObj = new JSONscriptRequest(request);
			aObj.buildScriptTag();
			aObj.addScriptTag();
		}
	}
}


var loading = false;
function startLoading(elem){
	if(loading == false){
	    loading = true;
	    startTimer();
		$('username_input').disabled = true;
		$('password_input').disabled = true;
		$('submit_input').disabled = true;
		$('submit_input').value = 'Loading..';
		//$('loginButton').innerHTML = 'Loading <img style="margin-top:-1px;" src="images/icon.ajax.circle.gif" alt=\"\" />';
	}
}
function stopLoading(elem){
	if(loading == true){
		loading = false;
		stopTimer();
		$('username_input').disabled = false;
		$('password_input').disabled = false;
		$('submit_input').disabled = false;
		$('submit_input').value = "Login";
		//$('loginButton').className = $('loginButton').className.split(' ')[0] + ' buttonStandard100';
	}
}


var timerID = 0;
var tStart  = null;
function updateTimer(){
   if(timerID){
      clearTimeout(timerID);
      timerID = 0;
   }
   if(!tStart){ tStart = new Date(); }
   var tDate = new Date();
   var tDiff = tDate.getTime() - tStart.getTime();
   tDate.setTime(tDiff);
	if(tDiff > 10000){
		if(finishUrl != ''){ redirectSuccess(); }
		else{
			alert("Application is currently unavailable.");
			stopLoading($('loginform'));
		}
	}else{
		timerID = setTimeout("updateTimer()", 1000);
	}
}

function startTimer(){
   tStart = new Date();
   timerID = setTimeout("updateTimer()", 1000);
}

function stopTimer(){
   if(timerID) {
      clearTimeout(timerID);
      timerID = 0;
   }
   tStart = null;
}
