/**
 * Creator: khastie
 * Date: Apr 8, 2009
 * Time: 10:33:19 AM
 * Contents are property of Cappex.com LLC, and strictly confidential.
 */

/**
 * CLASS: InviteFriend
 * DESCR:
 * USAGE: Requires var inviteStickyWin to be declared on the page
 * EXMPL:
 */
var InviteFriend = new Class({

	ajaxIndicator : Class.empty(),

	initialize : function (baseUrl) {

		this.baseUrl = baseUrl;

		this.ajaxValidator = new AjaxCpx('/ajax/account/invite/validateInviteFriend.jsp', {

			onComplete: function () {

				var errorText = Json.evaluate(this.ajaxValidator.transport.responseText).errorText;

				if (errorText == '') {
					this.submitInvite();
					$('inviteFriendErrorMsg').addClass('jsHide'); // remove error if shown
				} else {

					$('inviteFriendButton').removeAttribute('disabled');
					$('inviteFriendButton').removeClass('disabledBySubmit');

					$('inviteFriendErrorMsg').setHTML(errorText);
					$('inviteFriendErrorMsg').removeClass('jsHide');

				}
			}.bind(this)
		});


	},

	validateAndSubmit : function () {

		global.trace('InviteFriend.validateAndSubmit()');

		if (!$('inviteFriendButtonIndicator')) {
			this.ajaxValidator.buildIndicator('inviteFriendButton', 'after');
		}
		this.ajaxValidator.indicator.setStyle('display', '');
		
		this.ajaxValidator.request($('inviteStudentForm').toQueryString());
	},

	submitInvite : function () {

		global.trace('InviteFriend.submitInvite()');

		new AjaxCpx(this.baseUrl, {

			update: 'inviteStudentTarget',
			evalScripts: true,
			data: $('inviteStudentForm').toQueryString()
				+ '&invite=friend'
				+ '&submitButton=Submit',
			onSuccess: global.initButtonRollovers

		}).request();

	},

	test : function () {

	}
});

InviteFriend.implement(new Options);