// ************************************************************************************ //
//
// 
// ************************************************************************************ //

/* ************************************************************************** */
// Global parameters
/* ************************************************************************** */
var _LOGIN_SUBMIT_ENDPOINT = "./scripts/CustomLogin.php";
var _SWF_ID = "whiterhinoswf";

/* ************************************************************************** */
function submitLogin( username, password )
{
	if(console && console.debug) console.debug("submitLogin: %s - %s", username,  password);
	if( checkParams(username, password) )
	{
		var _params = new Hash();
		_params['username'] = username;
		_params['password'] = password;
	
	
		new Ajax.Request(getEndpoint(), 
					{ 
						method: 'post', 
						parameters: _params,
						onComplete: function(t) 
						{
	        				loginResponse(t.responseText);
	    				}
						
					}
				)
	}
}

function submitCustomLogin( username, password, id )
{
	if(console && console.debug) console.debug ("submitCustomLogin: %s - %s", username,  password)
	if( checkParams(username, password) )
	{
		var _params = new Hash();
		_params['username'] = username;
		_params['password'] = password;
		if(id != null) { _params['id'] = id; }
		
	
		new Ajax.Request(getEndpoint(), 
					{ 
						method: 'post', 
						parameters: _params,
						onComplete: function(t) 
						{
	        				cutomLoginResponse(t.responseText);
	    				}
						
					}
				)
	}
}

function loginResponse( str )
{
	if(console && console.debug) console.debug ("loginResponse: %s", str)
	var _swf = document.getElementById(_SWF_ID);
	if(str.startsWith("ERROR")) { _swf.loginError(str.slice(6)); }
	else if(str.startsWith("SUCCESS")) 
	{
		var _location = str.slice(8);
		if(console && console.debug) console.debug ("String starts with 'SUCCESS' redirecting to: %s", _location)
		window.location.href = _location;
	}
	else {  _swf.loginError(str); }
}

function cutomLoginResponse( str )
{
	if(console && console.debug) console.debug ("cutomLoginResponse: %s", str)
	var _swf = document.getElementById(_SWF_ID);
	if(str.startsWith("ERROR")) {  _swf.customLoginError(str.slice(6)); }
	else if(str.startsWith("SUCCESS"))  
	{ 
		if(console && console.debug) console.debug ("String starts with 'SUCCESS' calling customLoginSuccess: %o", _swf)
		 _swf.customLoginSuccess(); 
	}
	else { _swf.customLoginError(str); }
}

function getEndpoint()
{
	var _endpoint = _LOGIN_SUBMIT_ENDPOINT;
	var _currLoc = window.document.location.toString();
	if(_currLoc.indexOf("/cp/") > 0)
	{
		_endpoint = "../." +_LOGIN_SUBMIT_ENDPOINT;
	}
	if(console && console.debug) console.debug ("getEndpoint: %s", _endpoint)
	return _endpoint;
}

function checkParams(username, password)
{
	var _swf = document.getElementById(_SWF_ID);
	if( username == null )
	{
		 _swf.customLoginError("Please enter a username");
	}
	else if( password == null )
	{
		 _swf.customLoginError("Please enter a password");
	}
	else { return true; }
	return false;
}

