(function(){
GupShup.html.actions = GupShup.html.actions || {
	
forwardMsg : function(msgId) { 
	GupShup.html.actions.pageReload(GupShup.conf.contextPath + "/p2pgupshupspace?msgId=" + msgId);
},

viewMsg : function(msgId) { 
	GupShup.html.actions.pageReload(GupShup.conf.contextPath + "/messages/" + msgId);
},

forwardToPaymentSystem : function(amount,phone,returnUrl) { 
	GupShup.html.actions.pageReload(GupShup.conf.paymentFundRU + "?phone=" + phone + "&amt=" + amount + "&ru=" + returnUrl);
},

pageReload : function(url){
	window.location = url;
},

postToMyGroup : function(msgId) { 
	GupShup.html.actions.pageReload(GupShup.conf.contextPath + "/gupshupspace?msgId=" + msgId);
},

sendMsg : function(sendMsgTo) { 
	GupShup.html.actions.pageReload(GupShup.conf.contextPath + "/p2pgupshupspace?sendMsgTo=" + sendMsgTo);
},

reportOffensive : function(msgId) { 
	
	var flagtype  = new GupShup.forms.controls({name:'flagMessage.flagType'});
	flagtype.init();
	
	var commentS  = new GupShup.forms.controls({name:'flagMessage.comment'});
	commentS.init();
	
	if(commentS.get('length') > 200)
	{
		commentS.showNotification(GupShup.html.errorMessages.bigComment,{failure:true,position : 'right'});
		return;
	}
	else if(!GupShup.util.isAscii(commentS.getValue()))
	{
		commentS.showNotification(GupShup.html.errorMessages.nonAsciiChar,{failure:true,position : 'right'});
		return;
	}
	else if(!GupShup.util.isValidContent(commentS.getValue(), stopWords))
	{
		commentS.showNotification(GupShup.html.errorMessages.stopWordsInComment,{failure:true,position : 'right'});
		return;
	}

	if(flagtype.getValue()  != 'NONE')
	{
		var obj = this;
			var callback = {
				success: function(o){obj.flagSuccess(o,obj)},
				failure: function(o){obj.flagError(o,obj)},
				argument: {}
			};
		GupShup.html.util.disableBtn("flagMsgBtn");
		GupShup.html.util.disableBtn("flagMsgCancel");
		GupShup.html.util.submitFormUsingAjax('POST', GupShup.conf.writeUrl , callback, "flagMessage", false);
	}
	else
	{
		flagtype.showNotification(GupShup.html.errorMessages.noFlagTypeSelected,{failure:true,position : 'right'});
	}
},

showOffensivePopUp : function(msgId, msgType, ref) {
GupShup.forms.hideNotification();
GupShup.html.util.showElement('flagMessageTemplate');
document.forms['flagMessage'].comment.value = '';
document.forms['flagMessage'].flagType.selectedIndex = 0;
if(msgType == "REPLY")
{
	document.forms['flagMessage'].replyId.value = msgId;
	document.forms['flagMessage'].msgId.value = -1;
}
else
{
	document.forms['flagMessage'].replyId.value = -1;
	document.forms['flagMessage'].msgId.value = msgId;
}
var ele = document.getElementById('flagMessageTemplate');
GupShup.html.util.overlay(ref,ele,"top left", 0 , -400);
GupShup.html.util.adjustPopupTop(ele,false,10);
},

showLoginToFlagErr : function(queryStr){
		var lbObj  = {msg:GupShup.html.errorMessages.loginToFlagMsg};
		GupShup.html.util.showLightBox(lbObj);
},

flagSuccess : function(o,obj)
{
	var response  = GupShup.util.getJsonFromResponse(o.responseText);
	if(response.status == "SUCCESS") 
	{
		GupShup.forms.showNotification(response.message,{success:true});
		GupShup.html.util.hideElement('flagMessageTemplate');
	}
	else
	{
		GupShup.forms.showNotification(response.message,{failure:true});
		GupShup.html.util.hideElement('flagMessageTemplate');
	}
	GupShup.html.util.enableBtn("flagMsgBtn");
	GupShup.html.util.enableBtn("flagMsgCancel");
	GupShup.html.actions.pageReload("#");
},

flagError : function(o, obj) {
	GupShup.forms.showNotification(GupShup.html.errorMessages.internalError,{failure:true});
},

shareGroup :function(formName){
var callBack = {
	success:GupShup.callback.shareResponseHandler,
	failure:GupShup.callback.shareResponseHandler
}
GupShup.html.util.submitFormUsingAjax('POST', GupShup.conf.writeUrl, callBack, formName, false);
},

processNoSpam :function(){
	var callBack = {
	success: GupShup.callback.processNoSpamSuccess,
	failure:GupShup.callback.processNoSpamErr
}
GupShup.html.util.submitFormUsingAjax('POST', GupShup.conf.writeUrl, callBack, "spamControl", false);
},

renewSubscription : function (formName) {		
	var callback = {
		success: GupShup.html.premiumPlan.methods.processSubscribeResp,
		failure: GupShup.html.premiumPlan.methods.processSubscribeResp
	};
	GupShup.html.util.submitFormUsingAjax('POST', GupShup.conf.writeUrl + '&type=' + GupShup.conf.actions.manageSubscribers , callback, formName, false);
},

requestSubscription : function (formName) {		
	var callback = {
		success: GupShup.callback.processSubscribeResp,
		failure: GupShup.callback.processSubscribeResp
	};
	GupShup.html.util.submitFormUsingAjax('POST', GupShup.conf.writeUrl + '&type=' + GupShup.conf.actions.manageSubscribers , callback, formName, false);
},

requestComSubscription : function (formName) {		
	var callback = {
		success: GupShup.callback.processComSubscribeResp,
		failure: GupShup.callback.processComSubscribeResp
	};
	GupShup.html.util.submitFormUsingAjax('POST', GupShup.conf.writeUrl + '&type=' + GupShup.conf.actions.manageSubscribers , callback, formName, false);
},

requestAccountBalance : function (callback) {
	GupShup.ajax.send('POST', GupShup.conf.readUrl + '&type=USER_INFO&infoType=ACCOUNT_BALANCE', callback, 'USER_INTIATED_READ');
},

removeSubscribers :function(formName){
	var callBack = {
		success: GupShup.callback.processMySubscribers,
		failure: GupShup.callback.processMySubscribersErr
	}
	GupShup.html.util.submitFormUsingAjax('POST', GupShup.conf.writeUrl + '&type=' + GupShup.conf.actions.manageSubscribers , callBack, formName, false);
},

deleteMsg : function(entityId, msgId, currPageStart, limit, callback) {
	var queryUrl = GupShup.conf.writeUrl + '&type=' + GupShup.conf.actions.deletePost
				   + '&msgId=' + msgId + '&entityId=' + entityId + '&start=' + currPageStart + '&limit=' + limit;
	GupShup.ajax.send('POST', queryUrl, callback, 'WRITE');
},

blockUnblockSender : function(name, srcEntityId, action, callback, currPageStart, limit) {
	var queryUrl = GupShup.conf.writeUrl + '&type=' + action
				   + '&userIdentifier=' + name + '&srcEntityId=' + srcEntityId + '&start=' + currPageStart + '&limit=' + limit;
	GupShup.ajax.send('POST', queryUrl, callback, 'WRITE');
},

showReplyBox: function(formName,status){
	if(!document.forms[formName])
		return; 
	if(status)
	{
		document.forms[formName].style.display = "block";
		if(typeof document.forms[formName].ctrl == "undefined")
		{
			document.forms[formName].ctrl = new GupShup.forms.controls({
								  name:formName + '.text',
								  limit:GupShup.conf.replyMessageLength,
								  toolTip:''});
			document.forms[formName].ctrl.init();
			var counter = GupShup.util.trim(document.forms[formName].counter.value);
			document.forms[formName].ctrl.addListner('counter', {holder : counter});
		}
		document.forms[formName].text.focus();
	}
	else
	{
		document.forms[formName].style.display = "none";
		document.forms[formName].text.value = document.forms[formName].to.value + " ";
	}
},

sendReply :function(formName){
	var formH = document.forms[formName];
	var reply = GupShup.util.trim(formH.text.value);
	var replyObj = GupShup.html.util.getDestinationsForReplies(formH.text.value);
	var validReply = replyObj.isTextPresent;
	var noOfGrps = replyObj.count;
	var overflow = (reply.length > GupShup.conf.replyMessageLength);
	if(validReply && !overflow && noOfGrps > 0)
	{
		if(!GupShup.util.isAscii(reply))
		{
			formH.ctrl.showNotification(GupShup.html.errorMessages.nonAsciiReplyErr,{failure:true,position:'right'});
			return;
		}
		var successH = GupShup.html.actions.sendReplySuccess;
		var failureH = GupShup.html.actions.ajaxFailure;
		
		var successCH = GupShup.util.getFunction(formH.onsuccess.value);
		var failureCH = GupShup.util.getFunction(formH.onfailure.value);
		
		if(successCH.status == true)
			successH = successCH.h;
		if(failureCH.status == true)
			failureH = failureCH.h;
		
		var callback = {
						success:successH,
						failure:failureH,
						argument:{form:formName}
					   }
		GupShup.html.util.submitFormUsingAjax('POST', GupShup.conf.writeUrl , callback, formName, false);
	}
	if(overflow)
		GupShup.forms.showNotification(GupShup.html.errorMessages.replyLengthOverFlow,{failure:true});
	
	if(!validReply)
		formH.ctrl.showNotification(GupShup.html.errorMessages.invalidReply,{failure:true,position:'right'});
		
	return;
},

sendReplySuccess : function (request) {
	var resp = GupShup.util.getJsonFromResponse(request.responseText);
	if(resp.status == 'SUCCESS')
	{
		GupShup.html.actions.showReplyBox(request.argument.form,false);
		GupShup.forms.showNotification(resp.message, {success:true});
	}
	else
		GupShup.forms.showNotification(resp.message, {failure:true});
},

ajaxFailure : function () {
GupShup.forms.showNotification(GupShup.html.errorMessages.internalError,{failure:true});
},

updateReplyStatus : function(entityId, msgId, currPageStart, limit, action, callback) {
	var queryUrl = GupShup.conf.writeUrl + '&type=' + GupShup.conf.actions.updateReply
				   + '&replyId=' + msgId + '&entityId=' + entityId + '&start=' + currPageStart + '&limit=' + limit + '&updateAction=' + action ;
	GupShup.ajax.send('POST', queryUrl, callback, 'WRITE');
},

redirectToPaymentFundRU: function (params, returnURL, promotionsURL) {
	var fundruURL = GupShup.conf.paymentFundRU + '?' + params;
	fundruURL += '&ru=' + escape(GupShup.conf.fromUrl + '/' + returnURL);
	if (GupShup.util.isInitialized(promotionsURL))
		fundruURL += '&pu=' + escape(GupShup.conf.fromUrl + '/' + promotionsURL);
	window.location = fundruURL;
}

}
})();

(function(){
GupShup.html.pendingAction = {};
GupShup.html.pendingAction.ajaxSendPendingActionsSuccess = function(o) {
	
	var response  = GupShup.util.getJsonFromResponse(o.responseText);
	if(response.status != "SUCCESS") 
	{
		GupShup.forms.showNotification(GupShup.html.errorMessages.internalError,{failure:true});
		return;
	}
	else
	{
		var message = "";
		
		switch(o.argument.type)
		{
			case 'FORWARD' : message = GupShup.html.errorMessages.loginToForwardMsg; break;
			case 'JOIN' : message = GupShup.html.errorMessages.loginToJoinMsg; break;
			case 'COMMUNITY' : message = GupShup.html.errorMessages.loginToJoinComMsg; break;
			case 'SEND_P2P' :
			case 'PIC':
			case 'REPLY' :
			default : message = GupShup.html.errorMessages.loginToGupShupMsg; break;
		}
		
		var lbObj  = {
						msg:message,
						callBack:GupShup.html.pendingAction.cancelPendingAction
					}
		GupShup.html.util.showLightBox(lbObj);
	}
}

GupShup.html.pendingAction.ajaxSendPendingActionsFailure = function(o, obj) {
	GupShup.forms.showNotification(GupShup.html.errorMessages.internalError,{failure:true});
}

GupShup.html.pendingAction.sendP2PMsgPendingAction = function(queryStr) {
	GupShup.html.pendingAction.sendPendingAction(queryStr, 'SEND_P2P');	
}

GupShup.html.pendingAction.sendSharePendingAction = function(queryStr) {
	GupShup.html.pendingAction.sendPendingAction(queryStr, 'SHARE');	
}

GupShup.html.pendingAction.sendFwdPendingAction = function(queryStr){
	GupShup.html.pendingAction.sendPendingAction(queryStr,'FORWARD');	
}

GupShup.html.pendingAction.sendJoinPendingAction = function(queryStr){
	GupShup.html.pendingAction.sendPendingAction(queryStr,'JOIN');	
}

GupShup.html.pendingAction.sendComJoinPendingAction = function(queryStr){
	GupShup.html.pendingAction.sendPendingAction(queryStr,'COMMUNITY');	
}

GupShup.html.pendingAction.sendPicPendingAction = function(queryStr){
	GupShup.html.pendingAction.sendPendingAction(queryStr,'PIC');	
}

GupShup.html.pendingAction.sendReplyPendingAction = function(queryStr){
	GupShup.html.pendingAction.sendPendingAction(queryStr,'REPLY');	
}

GupShup.html.pendingAction.sendPendingAction = function(queryStr,actionType)
{
	queryStr = GupShup.util.trim(queryStr);
	if(queryStr != "")
	{
		var callback = {
			success: function(o){GupShup.html.pendingAction.ajaxSendPendingActionsSuccess(o);},
			failure: function(o){GupShup.html.pendingAction.ajaxSendPendingActionsFailure(o);},
			argument: {type : actionType}
		};
		
		var url = GupShup.conf.writeUrl + "&type=ADD_PENDING_ACTION&action=" + queryStr;
		var cObj = GupShup.ajax.send('POST', url, callback , 'PENDINGACTION');
		
	}
}

GupShup.html.pendingAction.cancelPendingAction = function()
{
	var obj = this;
	var callback = {
		success: function(o){GupShup.html.util.closeLightBox();},
		failure: function(o){GupShup.html.util.closeLightBox();}
	};
	var url = GupShup.conf.writeUrl + "&type=ADD_PENDING_ACTION&action=cancel";
	var cObj = GupShup.ajax.send('POST', url, callback , 'PENDINGACTION');
}
})();
