(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") 
	{
		window.location = GupShup.conf.contextPath + "/" + response.nextServlet;
	}
	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)
	{
		GupShup.forms.showNotification(GupShup.html.enterpriseMessages.invalidUserId,{failure:true});
		return false;
	}

	if(GupShup.util.isValidPhone(cCode,phone) || GupShup.util.isValidLoginId(phone))
	{
		if(pass.length > 0)
			return true;
		else
		{
			GupShup.forms.showNotification(GupShup.html.errorMessages.blankPassword,{failure:true});
			return false;
		}
	}
	else
	{
		GupShup.forms.showNotification(GupShup.html.enterpriseMessages.invalidUserId,{failure:true});
		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);
	GupShup.html.loginPage.showBrowserSupportPopup();
},
showForgetPasswdPopup : function () {
	GupShup.html.util.showLightBox({element:'sendForgetEmailDiv'});
},

cancelForgetPasswdPopup : function () {
	GupShup.html.util.hideElement('sendForgetEmailDiv');
	GupShup.html.util.closeLightBox();	
},
sendForgetPasswd : function () {
// All well, hide the popup.
	GupShup.html.util.hideElement('sendForgetEmailDiv');
	GupShup.html.util.closeLightBox();
},

showBrowserSupportPopup : function(){
	var userAgent = navigator.userAgent;
	var version = parseInt(navigator.appVersion);
	var browserName  = navigator.appName;
	var browserCode = navigator.appCodeName;
	var verOffset;

	if ((verOffset=userAgent.indexOf("MSIE"))!=-1) 
	{
	 version =parseInt(userAgent.substring(verOffset+5));
	
		if ((browserName == "Microsoft Internet Explorer") && (version < 7))
		{
			GupShup.html.util.showLightBox({element:'browserSupportPopupDiv'});
		}
	}
	
	else if ((verOffset=userAgent.indexOf("Firefox"))!=-1) 
	{
	 version =parseInt(userAgent.substring(verOffset+8));
	
		if ((browserName == "Netscape") && (browserCode == "Mozilla") && (version < 3))
		{
			GupShup.html.util.showLightBox({element:'browserSupportPopupDiv'});
		}
	}
	
	else if (!(browserName == "Microsoft Internet Explorer") || !(browserName == "Netscape") && !(browserCode == "Mozilla")){
		GupShup.html.util.showLightBox({element:'browserSupportPopupDiv'});
	}
},
closeBrowserSupportPopup : function(){
	GupShup.html.util.hideElement('browserSupportPopupDiv');
	GupShup.html.util.closeLightBox();	
}
}
})();

