(function(){
GupShup.html.signupPage = GupShup.html.signupPage || {

emailControl : null,

pwdControl : null,

pwdVerifyControl : null,

phoneControl : null,

captchaControl : null,

countryCodeControl : null,

randControl : null,

grpNameControl : null,

register : function(newUserFlow) {
	GupShup.html.util.disableBtn('cancelBtnId');
	GupShup.html.util.disableBtn('signupId');

	if (!newUserFlow)
	{
		var grpName = this.grpNameControl.get('value');
		var status = GupShup.util.isValidGroupName(grpName);
		if (status.code != 'VALID')
		{
			this.grpNameControl.showNotification(GupShup.html.errorMessages.invalidGroupName,{failure:true,position:'right'});
			GupShup.html.util.enableBtn('cancelBtnId');
			GupShup.html.util.enableBtn('signupId');
			return;
		}
	}
	
	if (this.validateSignupForm())
	{
		var obj = this;
		var callback = {
			success: function(o){obj.signupSuccess(o,obj)},
			failure: function(o){obj.signupFailure(o,obj)},
			argument: {}
		};
		
		this.randControl.set(b64_md5(this.pwdControl.get('value') + GupShup.conf.systemSalt)); 
		GupShup.util.putDummyPassword(this.pwdControl.get('object'));
		GupShup.util.putDummyPassword(this.pwdVerifyControl.get('object'));
		GupShup.html.util.submitFormUsingAjax('POST', GupShup.conf.writeUrl + "&type=REGISTER_USER", callback, 'signupForm');
	}
	else
	{
		GupShup.html.util.enableBtn('cancelBtnId');
		GupShup.html.util.enableBtn('signupId');
	}
},
 
signupSuccess : function(o, obj) {
	var response  = GupShup.util.getJsonFromResponse(o.responseText);
    if(response.status == "SUCCESS") 
	{
		window.location = GupShup.conf.contextPath + "/" + response.nextServlet;
	}
	else
	{
		obj.reloadCapcha();
		obj.pwdControl.set("");
		obj.pwdVerifyControl.set("");
		obj.captchaControl.set("");
		GupShup.html.util.enableBtn('cancelBtnId');
		GupShup.html.util.enableBtn('signupId');
		errorCode = response.errorCode;
		var i = errorCode.lastIndexOf(".");
		errorCode = errorCode.substr(i+1);
		var message = response.message;
		if(errorCode == 'GROUPNAMEREJECTED')
			this.grpNameControl.showNotification(message,{failure:true,position:'right'});
		else if(errorCode == 'PHONEREJECTED' || errorCode == 'DUPLICATEPHONE' || errorCode == 'PHONEMISSMATCH' || errorCode == 'DND')
			this.phoneControl.showNotification(message,{failure:true,position:'right'});
		else if (errorCode == 'DUPLICATEEMAIL')	
			this.emailControl.showNotification(message,{failure:true,position:'right'});
		else if (errorCode == 'INVALIDCAPTCHA')
			this.captchaControl.showNotification(message,{failure:true,position:'right'});
		else
			GupShup.forms.showNotification(message,{failure:true});
		if (GupShup.conf.useTokenAuth) 
		{ 
			GupShup.conf.systemChallenge = response.challenge; 
			document.loginForm.challenge.value = response.challenge; 
		}
	}
},

signupFailure : function(o, obj) {
obj.reloadCapcha();
obj.pwdControl.set("");
obj.pwdVerifyControl.set("");
obj.captchaControl.set("");
GupShup.html.util.enableBtn('cancelBtnId');
GupShup.html.util.enableBtn('signupId');
GupShup.forms.showNotification(GupShup.html.errorMessages.internalError,{failure:true});
},

validateSignupForm: function() {
	var email = this.emailControl.get('value');
	var pass = this.pwdControl.get('value');
	var passV = this.pwdVerifyControl.get('value');
	var phn = this.phoneControl.get('value');
	var cap = this.captchaControl.get('value');
	var code = this.countryCodeControl.get('value');
	
	if (phn.length < 1)
	{
		this.phoneControl.showNotification(GupShup.html.errorMessages.blankPhone,{failure:true,position:'right'});
		return false;
	}
	
	var err = false;
	if (code != "91")
	{
		if (!GupShup.util.validatePhoneNumber(phn, true))
			err= true;
	}
	else
	{
		if (!GupShup.util.validatePhoneNumber(phn, false))
			err= true;
	}
	
	if (err)	
	{
		this.phoneControl.showNotification(GupShup.html.errorMessages.invalidPhone,{failure:true,position:'right'});
		return false;
	}

	if (!GupShup.util.isValidEmail(email))
	{
		GupShup.html.util.clearTextBox('emailId');
		this.emailControl.showNotification(GupShup.html.errorMessages.invalidEmail,{failure:true,position:'right'});
		return false;
	}
	
	if (pass.length < 5)
	{
		GupShup.html.util.clearTextBox('pwdId');
		this.pwdControl.showNotification(GupShup.html.errorMessages.currentPassLength,{failure:true,position:'right'});
		return false;
	}
	
	if(passV != pass)
	{
		//GupShup.html.util.clearTextBox('pwdId');
		//GupShup.html.util.clearTextBox('pwdVerifyId');
		this.pwdVerifyControl.showNotification(GupShup.html.errorMessages.verifyPassMismatch,{failure:true,position:'right'});
		return false;
	}
	
	if (cap == "" )
	{
		GupShup.html.util.clearTextBox('captchaId');
		this.captchaControl.showNotification(GupShup.html.errorMessages.blankCaptcha,{failure:true,position:'right'});
		return false;
	}
	
	return true;
},

enableSignup: function() {
	if (document.signupForm.terms.checked)
		GupShup.html.util.enableBtn("signupId");
	else
		GupShup.html.util.disableBtn("signupId");
},

checkAvalibility: function(newGroupNameId) {
	
	var newGroupName = document.getElementById(newGroupNameId).value;	
	var status = GupShup.util.isValidGroupName(newGroupName);
	if(status.code != 'VALID')
	{
			this.grpNameControl.showNotification(status.err,{failure:true,position:'right'});
			return false;
	}
	
	var obj = this;
	var callback = {
		success: function(o){obj.checkAvalibilitySuccess(o,obj)},
		failure: function(o){obj.checkAvalibilityError(o,obj)},
		argument: {}
	};
	
	GupShup.ajax.send('POST', GupShup.conf.contextPath + "/GroupNameStatus?groupName=" + newGroupName, callback ,'WRITE');
		
	
},

checkAvalibilitySuccess : function(o, obj) {
	var response  = GupShup.util.trim(o.responseText);
	if(response == "PHONEMISSMATCH" || response == "INVALID" || response == "DUPLICATENICK" || response == "ALREADYACTIVATED")
	{
		obj.grpNameControl.showNotification(GupShup.html.errorMessages.groupNameInUse,{failure:true,position:'right'});
	}
	else if(response == "BLOCKED" )
	{
		obj.grpNameControl.showNotification(GupShup.html.errorMessages.userBlocked,{failure:true,position:'right'});
	}
	else if(response == "AUTHDBERR" || response == "SMSDBERR" || response == "ERROR")
	{
		obj.grpNameControl.showNotification(GupShup.html.errorMessages.internalError,{failure:true,position:'right'});
	}
	else if(response == "GROUPNAMEREJECTED" )
	{
		obj.grpNameControl.showNotification(GupShup.html.errorMessages.blockedGroupName,{failure:true,position:'right'});
	}
	else if(response == "NEW" )
	{
		obj.grpNameControl.showNotification(GupShup.html.errorMessages.groupAvailible,{success:true,position:'right'});
	}	
	else
	{
		obj.grpNameControl.showNotification(GupShup.html.errorMessages.internalError,{failure:true,position:'right'});
	}
},

checkAvalibilityError : function(o, obj) {
	
	GupShup.forms.showNotification(GupShup.html.errorMessages.internalError,{failure:true});
},

initPage : function(isNewUser) {
	this.emailControl = new GupShup.forms.controls({name:'signupForm.email',toolTip:'', limit : 50});
	this.emailControl.init();
	
	this.pwdControl = new GupShup.forms.controls({name:'signupForm.pwd',toolTip:'', limit : 50});
	this.pwdControl.init();

	this.pwdVerifyControl = new GupShup.forms.controls({name:'signupForm.pwdVerify',toolTip:'', limit : 50});
	this.pwdVerifyControl.init();
	
	this.phoneControl = new GupShup.forms.controls({name:'signupForm.phone',toolTip:'', limit: 18});
	this.phoneControl.init();
	
	this.captchaControl = new GupShup.forms.controls({name:'signupForm.captcha', limit : 6});
	this.captchaControl.init();
	
	this.countryCodeControl = new GupShup.forms.controls({name:'signupForm.countryCode'});
	this.countryCodeControl.init();

	this.randControl = new GupShup.forms.controls({name:'signupForm.rand'});
	this.randControl.init();
	
	if (!isNewUser)
	{
		this.grpNameControl = new GupShup.forms.controls({name:'signupForm.grpName',toolTip:'', limit : 15});
		this.grpNameControl.init();
	}
},

reloadCapcha : function()
{
	var dt = new Date();
	var tableCell = document.getElementById('capchaImageId');
	var captchaImgElementsArr = tableCell.getElementsByTagName('img');
	captchaImgElementsArr[0].src = GupShup.conf.contextPath + "/captcha?capReqType=0&sId=" + dt.getMilliseconds();
}

}

})();


