(function(){

GupShup.html.forgotPasswordPage = GupShup.html.forgotPasswordPage || {
	
countryCodeControl : new GupShup.forms.controls({name:'forgotPasswordForm.countryCode',toolTip:''}),
phoneControl : new GupShup.forms.controls({name:'forgotPasswordForm.phone',toolTip:''}),
emailControl : new GupShup.forms.controls({name:'forgotPasswordForm.email',toolTip:'', limit : 50}),

forgotPasswordSuccess : function(o, obj) {
	var response  = GupShup.util.getJsonFromResponse(o.responseText);
	
	if(response.status == "SUCCESS") 
	{
		GupShup.forms.showNotification(response.message,{success:true});
		GupShup.html.util.showHideElement('loginDiv', 'forgotPasswordDiv');
	}
	else
	{
		if(response.errorCode == "EMAIL_MISMATCH")
		{
			obj.emailControl.showNotification(response.message,{failure:true,position:'right'});
		}
		else
		{
			GupShup.forms.showNotification(response.message,{failure:true});
		}		
	}
	
	GupShup.html.util.enableBtn('forgotSubmitBtnId');
	GupShup.html.util.enableBtn('forgotCancelBtnId');
},

forgotPasswordFailure : function(o, obj) {
	GupShup.forms.showNotification(GupShup.html.errorMessages.internalError,{failure:true});
	GupShup.html.util.enableBtn('forgotSubmitBtnId');
	GupShup.html.util.enableBtn('forgotCancelBtnId');		
 },

validateForgotPasswordForm: function() {
	phone = this.phoneControl.get('value');
	email = this.emailControl.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))
	{
		this.phoneControl.showNotification(GupShup.html.errorMessages.invalidPhone,{failure:true,position:'right'});
		return false;	
	}
	
	if(email.length == 0)
	{
		this.emailControl.showNotification(GupShup.html.errorMessages.blankEmail,{failure:true,position:'right'});
		return false;
	}

	if(!GupShup.util.isValidEmail(email))
	{
		this.emailControl.showNotification(GupShup.html.errorMessages.invalidEmail,{failure:true,position:'right'});
		return false;	
	}
	
	return true;	
},

sendRequest : function() {
	GupShup.html.util.disableBtn('forgotSubmitBtnId');
	GupShup.html.util.disableBtn('forgotCancelBtnId');
	
	if(this.validateForgotPasswordForm())
	{
		var obj = this;
		var callback = {
			success: function(o){obj.forgotPasswordSuccess(o,obj)},
			failure: function(o){obj.forgotPasswordFailure(o,obj)},
			argument: {}
		};
		
		GupShup.html.util.submitFormUsingAjax('POST', GupShup.conf.writeUrl + "&type=RESET_PASSWORD", callback, 'forgotPasswordForm');
	}
	else
	{
		GupShup.html.util.enableBtn('forgotSubmitBtnId');
		GupShup.html.util.enableBtn('forgotCancelBtnId');
	}

	return false;
},

initPage : function() {
	var forgotPasswordControls = [GupShup.html.forgotPasswordPage.countryCodeControl, GupShup.html.forgotPasswordPage.phoneControl, 
									GupShup.html.forgotPasswordPage.emailControl];
	GupShup.forms.controls.initControls(forgotPasswordControls);
},

showForgotPwdDiv : function(){
	GupShup.html.forgotPasswordPage.toggleForgotPwdInput();
	GupShup.html.util.showElement('forgotPasswordDiv');
	GupShup.html.util.hideElement('loginDiv');
},

toggleForgotPwdInput : function(){
	var cCode = this.countryCodeControl.get('value');
	var tableRows = document.getElementById('forgotPasswordFormTable').getElementsByTagName("TR");
	var isIndian = true;
	if(cCode.length > 0 && cCode != "91")
		isIndian = false;
	if(isIndian)
		for(var i = 1; i < tableRows.length; i++)
			if( i != 1)
				tableRows[i].style.display =  "none";
			else
				tableRows[i].style.display =  GupShup.html.util.isIE() ? "block" : "table-row";
	else
		for(var i = 1; i < tableRows.length; i++)
			if( i == 1)
				tableRows[i].style.display =  "none";
			else
				tableRows[i].style.display =  GupShup.html.util.isIE() ? "block" : "table-row";
}

}

})();
