/* ---------------------------------
  @ CingoKit.Form.Username

  Registers a form element to utilize built-in ajax username checking
----------------------------------- */

// dialog constructor
CingoKit.Form.Username = function ( properties )
{
	// register baseclass
	this._class = CingoKit.Form.Username;

	// initialize
	this.init(properties);
}


// main functions
CingoKit.Form.Username.prototype = {

	init : function( properties )
	{
		// store properties for this dialog
		this.applyObject(properties);

		// set defaults
		this._valid = false;
		this._checked = "";

		// convert container
		this.container = $(this.container);

		// only proceed if all paramenters are defined
		if ( !isUndefinedOrNull(this.xfa, this.container) ) {

			// set onblur handler
			var self = this;
			connect(this.mgr.elem, 'onblur', self, 'prepCheck');

		};

	},

	prepCheck : function()
	{
		var username = this.mgr.getElementValue();

		if (!this.mgr.isTextElementValueEmpty() && username != this._checked) {
			if ( this.mgr.validateElement() )
			{
				// clear messages
				this.clearMessages();
				
				// re-enable the form for submission
				this.mgr.mgr.markAsAvailable();
	
				// store the checked so we don't do duplicate checking
				this._checked = username;

				// show the username check
				this.createMessage('processing', 'Checking username...');
				this.showContainer();

				// clear the interval before proceeding
				if (!isUndefinedOrNull(this._interval)) {
					clearInterval(this._interval);
					this._interval = null;
				};

				// check
				var self = this;
				this._interval = setInterval( function() { self.checkUsername(username); }, 500);
			} else {
				this.clearMessages();
			};
		};
	},

	checkUsername : function(username)
	{
		clearInterval(this._interval);
		this._interval = null;

		// -- begin ajax call ----------------
		var url = this.xfa;
		var qs = { username : username };

		var self = this;
		var d = loadXMLDoc( url, qs );
		d.addCallback(
			function(req) { self.captureResponse(req); }
		);
		d.addErrback( function( err ) {
			 if (err instanceof CancelledError) { return; }
			logError(err);
		});
		// -- end ajax call ---------------
	},

	captureResponse  : function( req )
	{
		var json = this.extractResponseJSON(req);

		this._valid = !json.error;

		// show appropriate message
		if ( isUndefinedOrNull(this._interval) ) {
			var mode = json.error ? "error" : "success"
			this.createMessage( mode, json.message );
			this.showContainer();
		};
	},

	cancelUsernameCheck : function()
	{
		this.usernameIsValid = false;

		if (!isUndefinedOrNull(this._interval)) {
			clearInterval(this._interval);
			this._interval = null;
		};

		this.hideUsernameMessage();
	},


	showContainer : function()
	{
		showElement(this.container);
	},

	hideContainer : function()
	{
		hideElement(this.container);
	},

	createMessage : function(type, msg)
	{
		// first, clear the messages
		this.clearMessages();
		
		// create the message element
		var element = H1({ 'class' : type, 'style' : 'float:left' });
		element.innerHTML = msg;
		
		this.container.appendChild( element );
	},

	clearMessages : function()
	{
		this.container.innerHTML = "";
	}

};

// Inherit from CingoKit.Base
setdefault(CingoKit.Form.Username.prototype, CingoKit);
