﻿/*
Author : Pranay Kumar Sharma
Date : 4/15/2009
*/
Ext.onReady(function(){

	var forgotPwdBtn = new Ext.Button({
		id : 'forgotPwdBtn',
		renderTo : 'forgotPwdDiv',
		text : "Forgot Your Password"
	});


	Ext.get("forgotPwdBtn").on("click",function(){
		
		var userName = Ext.getDom('userName').value;

		if( userName!==null && userName!==''){
			Ext.MessageBox.confirm('Please Confirm', 'Are you sure you want a new password?', processResult);
		}
		else{
			Ext.MessageBox.alert('ERROR', 'Please input username');
		}
	});


	var actionURL = Ext.getDom('contextPath').value+'/login/login.do';

	checkEmail = function(user) {
		emailCheck = new Ext.data.Connection();
		emailCheck.on('beforerequest', function() {
			Ext.MessageBox.wait('Please wait .....', 'Waiting Server Response');
		});
		emailCheck.on('requestcomplete', function() {
			Ext.MessageBox.hide();
		});
		emailCheck.request({
			url: actionURL,
			params: {
						'action':'doCheckLogIn',			
						'userName':user
					 },
			method: 'POST',
			success : function(response,options) {
				if (response.responseText == 'SUCCESS') {
					Ext.MessageBox.alert('SUCCESS', 'Your new password is sent to your EmailId');
				}else{
					if (response.responseText == 'FAILURE') {
						Ext.MessageBox.alert('ERROR', 'Your login has not been found in our system. '+
														'Please provide correct log in.');
					}else{
						showCommunicationError();
						return false;
					}
				}
			},
			failure: function(response,options){							
					showCommunicationError();
					return false;
			}
		});
		return true;
	}

	function showCommunicationError(){
		Ext.MessageBox.alert('Server communication error', 'There is a problem in communicating with the server. Please try again');
	}

	function processResult(btn){
		var userName = Ext.getDom('userName').value;
		if(btn=='yes'){	
			checkEmail(userName);
		}
	}
});
