var gncLogin = function() {
	return {
		init : function() {
		},
		checkLogin : function() {
			var fOK = true;
			var strMsg = "You have not filled in all the required fields:\n";
			if (document.getElementById('username').value.length == 0) {
				fOK = false;
				strMsg = strMsg + "\n" + "- Enter username.";
			}
			if (document.getElementById('password').value.length == 0) {
				fOK = false;
				strMsg = strMsg + "\n" + "- Enter password.";
			}
			if(!fOK) {
				alert(strMsg);
			} else {
				gncLogin.login();	
			}
		},
		login : function() {	
		
			Ext.Ajax.request({
				params:{JsonType:'custom',username:Ext.getDom('username').value,password:Ext.getDom('password').value,logintype:Ext.getDom('logintype').value},
				url: '/challenge/rpc/json/?method=login.checkLogin',
				scope:this,
				callback: function(opts,suss,resp){
					result = Ext.util.JSON.decode(resp.responseText);
					if (result.results) {
						document.location.href='/challenge/diaries/';	
					} else {		
						document.getElementById('logindisplay').innerHTML = result.output;		
					}
					if (Ext.isIE6) {
						fnLoadPngs();
					}
				}
			});
		},
		loginAgain : function() {	
			Ext.Ajax.request({
				params:{JsonType:'custom',logintype:Ext.getDom('logintype').value},
				url: '/challenge/rpc/json/?method=login.getLoginBox',
				scope:this,
				callback: function(opts,suss,resp){
					Ext.getDom('logindisplay').innerHTML = resp.responseText;
					if (Ext.isIE6) {
						fnLoadPngs();
					}
					checkLoginBox();
				}
			});
		},
		forgotPass : function() {
			Ext.Ajax.request({
				params:{JsonType:'custom',logintype:Ext.getDom('logintype').value},
				url: '/challenge/rpc/json/?method=login.getForgotPassBox',
				scope:this,
				callback: function(opts,suss,resp){
					Ext.getDom('logindisplay').innerHTML = resp.responseText;
					if (Ext.isIE6) {
						fnLoadPngs();
					}
				}
			});
		},
		checkEmail : function(theForm) {	
			function isValidEmail(str) {
				var filter=/^.+@.+\..{2,3}$/
				return (filter.test(str))
			}
			var fOK = true;
			var strMsg = "You have not filled in all the required fields:\n";
			if (theForm.email.value.length == 0 || (theForm.email.value.length != 0 && !isValidEmail(theForm.email.value))) {
				fOK = false;
				strMsg = strMsg + "\n" + "- Enter a valid email address.";
			}
			if(!fOK) {
				alert(strMsg);
			} else {
				gncLogin.checkCFEmail();	
			}
		},
		checkCFEmail : function() {
			Ext.Ajax.request({
				params:{JsonType:'custom',email:Ext.getDom('email').value,logintype:Ext.getDom('logintype').value},
				url: '/challenge/rpc/json/?method=login.getDetails',
				scope:this,
				callback: function(opts,suss,resp){
					result = Ext.util.JSON.decode(resp.responseText);
					Ext.getDom('logindisplay').innerHTML = result.output;
					if (Ext.isIE6) {
						fnLoadPngs();
					}
				}
			});
		}		
	}
}();