(function(){

GupShup.html.supportForm = {
formDoc : null,
countryCodeControl : null,
phoneControl : null,
emailControl : null,
groupControl : null,
msgControl : null,
subControl : null,
capControl : null,
nameControl : null,
profControl : null,
locControl : null,
initAll : false,

init : function(doc){
if(this.initAll)
	return;
this.formDoc = doc;
this.countryCodeControl = new GupShup.forms.controls({name:'paidSupport.countryCode',toolTip:'', doc : doc});
this.phoneControl = new GupShup.forms.controls({name:'paidSupport.phone',toolTip:'', doc : doc});
this.emailControl = new GupShup.forms.controls({name:'paidSupport.from',toolTip:'', doc : doc});
this.groupControl = new GupShup.forms.controls({name:'paidSupport.group',toolTip:'', doc : doc});
this.msgControl = new GupShup.forms.controls({name:'paidSupport.content',toolTip:'', doc : doc});
this.subControl = new GupShup.forms.controls({name:'paidSupport.subject',toolTip:'', doc : doc});
this.capControl = new GupShup.forms.controls({name:'paidSupport.captcha',toolTip:'', doc : doc});
this.nameControl = new GupShup.forms.controls({name:'paidSupport.Name',toolTip:'', doc : doc});
this.profControl = new GupShup.forms.controls({name:'paidSupport.Profession',toolTip:'', doc : doc});
this.locControl = new GupShup.forms.controls({name:'paidSupport.Location',toolTip:'', doc : doc});
this.initAll = true;
},	

clearInputs : function(){
	this.phoneControl.set("");
	this.emailControl.set("");
	if(this.groupControl.isValid())
		this.groupControl.set("");
	if(this.msgControl.isValid())	
		this.msgControl.set("");
	this.capControl.set("");
	if(this.nameControl.isValid())
                 this.nameControl.set("");
	if(this.profControl.isValid())
                  this.profControl.set("");
	if(this.locControl.isValid())
                  this.locControl.set("");
	if(this.formDoc.forms['paidSupport'].subject)
		this.formDoc.forms['paidSupport'].subject.selectedIndex = 0;
},

reloadCapcha : function()
{
	var dt = new Date();
	var tableCell = this.formDoc.getElementById('capchaImageId');
	var captchaImgElementsArr = tableCell.getElementsByTagName('img');
	captchaImgElementsArr[0].src = GupShup.conf.contextPath + "/captcha?capReqType=0&sId=" + dt.getMilliseconds();
},

validateLoginForm: function() {
	phone = this.phoneControl.get('value');
	cCode = this.countryCodeControl.get('value');
	email = this.emailControl.get('value');
	cap = this.capControl.get('value');
	if(phone.length == 0)
	{
		alert(GupShup.html.errorMessages.blankPhone);
		return false;
	}
	if (!GupShup.util.isValidEmail(email))
	{
		alert(GupShup.html.errorMessages.invalidEmailId);
		return false;
	}
	if(!GupShup.util.isValidPhone(cCode,phone))
	{
		alert(GupShup.html.errorMessages.invalidPhone);
		return false;
	}
	if(this.subControl.isValid() && this.subControl.getValue()  == 'NONE')
	{
		alert(GupShup.html.errorMessages.noSubSelected);
		return false;
	}
	if(this.msgControl.isValid())
	{
		if(this.msgControl.get('length') < 1)
		{
			alert(GupShup.html.errorMessages.noComment);
			return false;
		}
		if(this.msgControl.get('length') > 200)
		{
			alert(GupShup.html.errorMessages.bigComment);
			return false;
		}
		if(!GupShup.util.isAscii(this.msgControl.getValue()))
		{
			alert(GupShup.html.errorMessages.nonAsciiChar);
			return false;
		}
		if(!GupShup.util.isValidContent(this.msgControl.getValue(), stopWords))
		{
			alert(GupShup.html.errorMessages.stopWordsInComment);
			return false;
		}
	}
	if(this.capControl.get('length') < 1)
	{
		alert(GupShup.html.errorMessages.blankCaptchaAlert);
		return false;
	}
	
	return true;
	
},

submitForm : function(pDoc) {
	this.init(pDoc);
	if(this.validateLoginForm())
	{
		var callback = {
			success: function(o){GupShup.html.supportForm.supportSuccess(o)},
			failure: function(o){GupShup.html.supportForm.supportFailure(o)},
			scope : GupShup.html.supportForm,
			argument: {}
		};

		GupShup.html.util.submitFormUsingAjax('POST', GupShup.conf.writeUrl + "&type=SEND_EMAIL", callback, this.formDoc.forms['paidSupport']);
		GupShup.html.util.disableBtn('sendBtnId',this.formDoc);
	}
	else
	{
		GupShup.html.util.enableBtn('sendBtnId',this.formDoc);		
	}
	
	return false;
},

supportSuccess : function(o) {
	var response  = GupShup.util.getJsonFromResponse(o.responseText);
    if(response.status == "SUCCESS") 
	{
		alert(response.message);
		this.clearInputs();
	}
	else
	{
		alert(response.message);
	}
	this.reloadCapcha();
	GupShup.html.util.enableBtn('sendBtnId',this.formDoc);		
	return;
},

supportFailure : function(o){
	GupShup.html.util.enableBtn('sendBtnId',this.formDoc);		
}

}
})();
