(function(){

GupShup.html.loginPage = {

countryCodeControl : new GupShup.forms.controls({name:'loginForm.countryCode',toolTip:''}),
phoneControl : new GupShup.forms.controls({name:'loginForm.phone',toolTip:''}),
passwordControl : new GupShup.forms.controls({name:'loginForm.password',toolTip:'', limit : 50}),

loginInSuccess : function(o,obj) {
	var response  = GupShup.util.getJsonFromResponse(o.responseText);
	
	if(response.status == "SUCCESS") 
	{
		if (typeof GupShup.html.loginPage.redirectUrl == 'undefined')
			window.location = GupShup.conf.contextPath + "/" + response.nextServlet;
		else
		{
			var ccPhone = response.countryCode + ((response.countryCode == '91') ? '' : '-') + response.phone;
			window.location = GupShup.html.loginPage.redirectUrl + "token=" + response.token + "&phone=" + ccPhone;
		}
	}
	else
	{
		GupShup.forms.showNotification(response.message,{failure:true});
		document.loginForm.password.value = "";
		GupShup.html.util.enableBtn('loginBtnId');
		if (GupShup.conf.useTokenAuth) 
		{ 
			GupShup.conf.systemChallenge = response.challenge; 
			document.loginForm.challenge.value = response.challenge; 
		}
	}
},

loginInFailure : function(o,obj) {
	GupShup.html.util.enableBtn('loginBtnId');
	GupShup.forms.showNotification(GupShup.html.errorMessages.internalError,{failure:true});
	document.loginForm.password.value = "";
},

validateLoginForm: function() {
	phone = this.phoneControl.get('value');
	pass = this.passwordControl.get('value');
	cCode = this.countryCodeControl.get('value');
	
	if(phone.length == 0)
	{
		this.phoneControl.showNotification(GupShup.html.errorMessages.blankPhone,{failure:true,position:'right'});
		return false;
	}

	if(GupShup.util.isValidPhone(cCode,phone))
	{
		if(pass.length > 0)
			return true;
		else
		{
			this.passwordControl.showNotification(GupShup.html.errorMessages.blankPassword,{failure:true,position:'right'});
			return false;
		}
	}
	else
	{
		this.phoneControl.showNotification(GupShup.html.errorMessages.invalidPhone,{failure:true,position:'right'});
		return false;
	}
},

loginUser : function() {
	GupShup.html.util.disableBtn('loginBtnId');
	
	if(this.validateLoginForm())
	{
		var password=this.passwordControl.get('value');
		var rand = b64_md5(password + GupShup.conf.systemSalt);
		rand = b64_md5(GupShup.util.trim(rand) + document.loginForm.challenge.value); 
		document.loginForm.rand.value = rand;
		GupShup.util.putDummyPassword(document.loginForm.password);
		var obj = this;
		var callback = {
			success: function(o){obj.loginInSuccess(o,obj)},
			failure: function(o){obj.loginInFailure(o,obj)},
			argument: {}
		};
		
		GupShup.html.util.submitFormUsingAjax('POST', GupShup.conf.writeUrl + "&type=LOGIN_USER", callback, 'loginForm');
	}
	else
	{
		GupShup.html.util.enableBtn('loginBtnId');		
	}
	
	return false;
},

initPage : function() {
	var loginControls = [GupShup.html.loginPage.countryCodeControl, GupShup.html.loginPage.phoneControl, GupShup.html.loginPage.passwordControl];
	GupShup.forms.controls.initControls(loginControls);
}

}
})();
