var nLoginCallbackTimeout = 0;

var COOKIE_SERVER_NAME = 'server_name';

/**
 * Attempt a login
 * 
 * Called via onsubmit on the login form.
 * 
 * 1. Validate form using fValidate 
 * 2. Attempt to login to a dashboard 
 *    set form action and sumbit on success, don't submit on error
 */
function attemptLogin(form) {
	// give user feedback for incomplete form
	if (!validateForm( form,false,false,false,false,5 )) {
		return false;
	}	
	// hide messages and fade out form a bit
	$("div#2").hide("medium");
	$("div#3").hide("medium");
	$("div#4").hide("medium");
	$("div#5").hide("medium");
	$("div#6").hide("medium");
	$("form.gfimaxform").fadeTo("medium", 0.33);
	// set a 5 second timeout after which to give some feedback
	nLoginCallbackTimeout = setTimeout('endTimeout()', 10000);
	// compose query URL
	queryPath = "/siteforms/login_redirector.php";	
	queryData = "username="+escape(form.username.value);
	queryData+= "&password="+escape(form.password.value);
	queryData+= "&server_name="+escape(getCookie(COOKIE_SERVER_NAME));
	if (document.location.hostname == 'hounddogiseasy') { 
		// testing
		queryProtocolHost = "http://hdog";
	} else { 
		// production 
		// todo: verify we can use SSL here
		queryProtocolHost = "https://wwweurope1.systemmonitor.eu.com"; 
	}
	loginRedirectorURL = queryProtocolHost+queryPath+"?"+queryData+"&jsoncallback=?"; 	
	// execute query 
	$.getJSON(loginRedirectorURL, function(json) {
		evaluateRedirectorReturn(form, json); 
	});
	
	$("div#6").show("medium");
	
	// return false in any case so that the form is not submitted unless the JSON-P call succeeded
	return false;	
}

/**
 * This function is called by through the asynchronous JSON-P call in attemptLogin()
 * It will cancel the timeout, obviously. It will then evaluate the content of
 * the data returned and take appropriate action.
 * On success set the form target and submit it to the dashboard login page
 * @param the form object
 * @param redirectorResult the json data returned
 * @return
 */
function evaluateRedirectorReturn(form, redirectorResult) {
	$("div#6").hide("medium");
	// first of all we received a response, we can kill the timeout
	clearTimeout( nLoginCallbackTimeout);
	sResult	= redirectorResult['result'];
	sURL = redirectorResult['url'];
	switch (sResult) {
		case 'redirect':
			$("div#5").show("fast");
			setCookie(COOKIE_SERVER_NAME, sURL);
			form.action = sURL;
			form.submit();
			break;	
		case 'fail':
			$("div#4").show("medium");
			break;
		case 'error':
		default:
			$("div#3").show("medium");
			break;
	}
	$("form.gfimaxform").fadeTo("medium", 1);
	return false;
}

/**
 * This function is called by a timeout event, created in attemptLogin()
 * It will show a timeout message and fade back in the login form
 * @return
 */
function endTimeout() {
	$("div#6").hide("medium");
	$("div#2").show("medium");
/* document.getElementById( 2 ).style.display="inline"; */
	$("form.gfimaxform").fadeTo("medium", 1);
}

function setCookie(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie  =c_name + "=" + escape(value) + ((expiredays==null)?"":";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";", c_start);
			if (c_end==-1) { 
				c_end=document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}