var finishUrl = ''
function getResult(success, message, url){
    stopTimer();
	if(success){
	    finishUrl = url;
	    /*
		if($('app').value == 'myaccess'){ logUsername(); }
		else{ redirectSuccess(); }
		*/
		redirectSuccess();
	}else{
		alert(message);
		finishUrl = '';
		stopLoading($('loginBox'));
	}
}

function redirectSuccess(){
    window.location = finishUrl;
}

function logUsername(){
	var thisAJAX = new ajax();
	var thisHTTP = thisAJAX.http;
	var thisCONTENT = this;
	thisHTTP.open("post", "/scripts/script.ajax.ipodRegister.php", true);
	thisHTTP.onreadystatechange = function(){
		if(thisHTTP.readyState == 4){
			redirectSuccess();
		}
	}
	thisHTTP.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	thisHTTP.send("username=" + $('username').value);
}

function makeRequest(){
	if(loading == false){
	    startLoading($('loginBox'));
		var loginUrl = $('loginUrl').value;
		var user = $('username').value;
		var pass = $('password').value;
		if(user == '' || user == "Username"){
			alert("You must enter a username to login!");
			stopLoading($('loginBox'));
		}else if(pass == '' || pass == 'Password'){
			alert("You must enter a password to login!");
			stopLoading($('loginBox'));
		}else{
			var request = eval(loginUrl);
			aObj = new JSONscriptRequest(request);
			aObj.buildScriptTag();
			aObj.addScriptTag();
		}
	}
}

function submitenter(e) {
	var keycode;
	if(window.event){ keycode = window.event.keyCode; }
	else if(e){ keycode = e.which; }
	else{ return true; }
	if(keycode == 13){
   		//makeRequest();
   		document.loginForm.submit();
   		return false;
	}else{ return true; }
}

var loading = false;
function startLoading(elem){
	if(loading == false){
	    loading = true;
	    startTimer();
		$('username').disabled = true;
		$('password').disabled = true;
		$('loginButton').className = $('loginButton').className.split(' ')[0] + ' buttonStandard100_disabled';
		$('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').disabled = false;
		$('password').disabled = false;
		$('loginButton').innerHTML = "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($('loginBox'));
		}
	}else{
		timerID = setTimeout("updateTimer()", 1000);
	}
}

function startTimer(){
   tStart = new Date();
   timerID = setTimeout("updateTimer()", 1000);
}

function stopTimer(){
   if(timerID) {
      clearTimeout(timerID);
      timerID = 0;
   }
   tStart = null;
}
