/*
Script: new_account_check.js
	Contains <NewAccountCheck>

Author:
	Alan Roemen

Class: NewAccountCheck

Options:
	baseURL: Base directory for script. Default: false
	cssFile: Name of css file. Set to location of CSS file or true to use default and false to use none. Default: true,
	scriptName: Name of javascript file. Default: 'new_account_check'
	className: CSS class name for script. Set to false for no CSS. Default: 'NewAccountCheck'
*/

var NewAccountCheck = new Class({
	options: {
		baseURL: false,
		cssFile: true,
		scriptName: 'new_account_check',
		className: 'NewAccountCheck'
	},	

	initialize: function(name, options) {
		this.error_msg = false;
		this.setOptions(options);
		this.input = document.getElement('input[id='+name+']');
		
		// Get script base path
		if(!this.options.baseURL) {
			var elements = document.getElementsByTagName('script');
			for (var i=0; i<elements.length; i++) {
				if (elements[i].src && (elements[i].src.indexOf(this.options.scriptName+'.js') != -1)) {
					var src = elements[i].src;
					this.options.baseURL = src.substring(0, src.lastIndexOf('/'));
					break;
				}
			}
			// Get document base path
			this.documentBasePath = document.location.href;
			if (this.documentBasePath.indexOf('?') != -1)
				this.documentBasePath = this.documentBasePath.substring(0, this.documentBasePath.indexOf('?'));
			this.documentBasePath = this.documentBasePath.substring(0, this.documentBasePath.lastIndexOf('/'));
			if (this.options.baseURL.indexOf('://') == -1 && this.options.baseURL.charAt(0) != '/')
				this.options.baseURL = this.documentBasePath + "/" + this.options.baseURL;
		}
		
		// Adds Stylesheet
		if(this.options.cssFile === true) new Asset.css(this.options.baseURL + '/' + this.options.scriptName + '.css');
		else if(this.options.cssFile !== false) new Asset.css(this.options.cssFile);
		
		// Setup Input
		this.input.addEvent('blur', function(e){ this.fieldCheck(name); }.bindWithEvent(this));
		this.pos = this.input.getCoordinates();
	},

	fieldCheck: function(field){
		var data = 'new_account&field='+field+'&value='+this.input.value;
		var ajaxURL = this.options.baseURL+'/'+this.options.scriptName+'.php';
		if(this.error_msg !== false) { this.error_msg.remove(); this.error_msg=false; }
		
		this.error_msg = new Element('div',{
			'class': this.options.className+'_load',
			'styles': {
				'top': this.pos.top.toInt()+3,
				'left': this.pos.right.toInt()+4
			}
		}).injectAfter(this.input);
		new Ajax(ajaxURL, {
			method: 'post',
			data: data,
			onComplete: function(response){
				if(response!='') this.error(response);
				else {
					this.error_msg.remove();
					this.error_msg = false;
				}
			}.bind(this)
		}).request();
	},

	error: function(msg){
		if(this.error_msg !== false) { this.error_msg.remove(); this.error_msg=false; }
		this.error_msg = new Element('div',{
			'class': this.options.className,
			'styles': {
				'top': this.pos.top.toInt()-25,
				'left': this.pos.left.toInt()-8
			}
		}).setHTML(msg).injectAfter(this.input);
		this.input.focus();
		this.input.select();
	}
});

NewAccountCheck.implement(new Options, new Events);
window.addEvent('domready', function(){
	new NewAccountCheck('user_name');
	new NewAccountCheck('email');
});
