/**
* @namespace TOTECS.portal_util.login
* Allows user to log in
*/
TOTECS.portal_util.login = function() {
	//server vars
	var domainServletURL;
	var pkey;
	
	/**
	 * Initialises layout and websites tree
	 * @constructor
	 *
	 * @param domServletURL		url of the page
	 * @param projectKey		unique project key
	 */ 
	function constructor(domServletURL, projectKey) {
		domainServletURL=domServletURL;
		pkey = projectKey;
	}
	
	function login_user()
	{
		var url = domainServletURL + "command=util_login&util_login_action=login_user"
		+"&user="+encodeURIComponent(document.getElementById('userID').value)
		+"&pass="+encodeURIComponent(document.getElementById('pass').value)
		+"&pkey=utils";
		
		TOTECS.util.showLoadDialog('Authenticating User, Please wait...','Authentication');
	
		//make ajax request to save item
		var callback = {
			success : function(o) {
				var response;
				TOTECS.util.hideLoadDialog();
				
				try{
					response = YAHOO.lang.JSON.parse(o.responseText);
				}catch (e){
					response= {"result":"false", "message":"Unable to handle server response. Please try again later."};
				}
				
				if(response.result=='success')
				{
					TOTECS.util.showLoadDialog('You have been logged in. Loading interface, please wait...','Loading Interface');
					window.location = '/view;jsessionid='+response.jessionid+'?command=ui_home&ui_home_action=drawHomeUI';
				}
				else
					TOTECS.util.showErrorDialog(response.message,'Error');
			},
			failure : function(o) {
				TOTECS.util.hideLoadDialog();
				TOTECS.util.showErrorDialog('Unable to handle server response, please try again later.','Error');
			}
		}
		YAHOO.util.Connect.asyncRequest('GET', url+'&r=' + new Date().getTime(), callback);
	
	}
	
	/** public methods **/
	return {
		constructor:constructor,
		login_user:login_user
	}
}();
