var userNameCheck = function(userNameInputId , requestPath)
{
	// setup params
	this.inputEL = document.getElementById(userNameInputId);
	this.requestPath = (requestPath)?requestPath:'/ajax/usernames?name=';
	
	// button label config
	this.txtCheck = 'Check for availability';
	this.txtChecking = 'Checking for availability...';
	this.txtTaken = 'taken';
	this.txtAvailable = 'available'; 
	
	// dataloader 
	this.dataLoader = new contentLoader(); // requires dataLoader.js
	this.dataLoader.onload = this.onDataLoad;
	this.dataLoader.onerror = this.onDataError;
	this.dataLoader.parent = this;	
	this.dataLoader.inRequest = false; // inrequest flag - packing additional value into dataloader
	// storage for previously checked names
	this.checkedNames = Array();
	
	// toggle autosuggest - under-developed -  not wise to use!
	this.checkServerOnKeyStroke = false;
}

userNameCheck.prototype =
{
	initalise:function()
	{
		if(this.dataLoader.isSupported()) // check we have http request functionality
		{
			this.inputEL.parentNode.appendChild(this.createButton());
			var objRef = this;
			this.buttonEL.onclick = function(){objRef.clickEvents(); return false;} 
			this.inputEL.onkeyup = function(){objRef.inputEvents();} 
		}
	},
	createButton:function()
	{
		// ensure button does not exist on page ie back click ect;
		var testElement = document.getElementById('availability_check');
		if(testElement)testElement.parentNode.removeChild(testElement);
		
		// create button
		this.buttonEL = document.createElement('input');
		this.buttonEL.setAttribute('id' , 'availability_check');
		this.buttonEL.setAttribute('class' , 'button');
		this.buttonEL.setAttribute('type' , 'submit');
		this.buttonEL.setAttribute('value' , this.txtCheck );
		return this.buttonEL;
	},
	inputEvents:function()
	{
		this.checkName(false != this.checkServerOnKeyStroke);
	},
	clickEvents:function()
	{
		if(this.buttonEL.value == this.txtCheck) // ensure the app is in the correct state
		{
			this.checkName(true);
		}
	},
	checkName:function(makeServerCall)
	{
		if(this.inputEL.value == ''){return false;} // just make sure the string isn't empty
		
		// reset the button	
		this.buttonEL.setAttribute('value' , this.txtCheck ); 
		this.buttonEL.setAttribute('class' , 'button');
		
		// check previously searched Array
		var nrChecked = this.checkedNames.length;
		for(var i = 0; i < nrChecked; i++)
		{
			if(this.checkedNames[i].username == this.inputEL.value &&  !this.checkedNames[i].available)
			{
				this.handleTaken(this.checkedNames[i].username);
				break;
				return false;
			}
			else if(this.checkedNames[i].username == this.inputEL.value &&  this.checkedNames[i].available)
			{
				this.handleAvailable(this.checkedNames[i].username);
				break;
				return false;
			}
		}
		
		// make server call
		var makeServerCall = (!makeServerCall && makeServerCall != null)?false:true; // should we check the server
		if(makeServerCall)
		{
			this.dataLoader.url = this.requestPath +  this.inputEL.value;
			this.buttonEL.value = this.txtChecking;
			this.buttonEL.setAttribute('class' , 'button');
			if(this.checkServerOnKeyStroke && this.dataLoader.inRequest)this.dataLoader.cancel(); // request cancel for autosuggest - hacky
			this.dataLoader.getData();
			this.dataLoader.inRequest = true;
		}
		
	},
	onDataLoad:function()
	{
		this.parent.dataLoader.inRequest = false; // reset in request flag
		this.parent.handleResponseData(this.loadedData); // pass data into parent object
	},
	onDataError:function()
	{
		this.parent.dataLoader.inRequest = false; // reset in request flag
		this.parent.handleError();
	},
	handleResponseData:function(data)
	{
		// reset button
		this.buttonEL.setAttribute('value' , this.txtCheck );
		this.buttonEL.setAttribute('class' , 'button');
		
		try // try to convert into js
		{
			eval(data);
		}
		catch(e) // otherwise drop out and call error handler
		{
			this.handleError(e);
			return false;
		}
		
		this.checkedNames.push(userNameCheck); // remember what the server told us
		
		if(userNameCheck.available === true || userNameCheck.available === false) // we have required data
		{
			this.inputEL.value = userNameCheck.username; // make use of any server string handling
			this.checkName(false); // check username without making call to server
		}
		else
		{
			this.handleError('unexpected data returned');
		}

	},
	handleAvailable:function()
	{
		this.buttonEL.value = this.txtAvailable;
		this.buttonEL.setAttribute('class' , 'button available');
	},
	handleTaken:function()
	{
		this.buttonEL.value = this.txtTaken;
		this.buttonEL.setAttribute('class' , 'button taken');
	},
	handleError:function(e)
	{
		this.buttonEL.parentNode.removeChild(this.buttonEL);
		if(window.console)window.console.log('An error occured loading username data:' + e);
	}
}