
var SimpleRegistration = Class.create();
SimpleRegistration.prototype = {
	defaultText: 'Your e-mail address',
	actionUrl: 'http://www.24sevenoffice.com/scriptaspx/registration/?resellerId=405147731304168&resellerPassword=mnsa&Language=EN&pkPackageId=1&clientCountry=US',
	
	initialize: function( element, source) {
		this.element = $(element);
		var opts = Object.extend( { label: '', className: 'big' }, arguments[2] || {} );
		this.source = source;
		this.label = opts.label;
		this.className = opts.className;
		this.paint();
	},
	
	paint: function() {
		this.regButton = Builder.node( 'SPAN', { className: 'simplereg-button' }, [ Builder.node('SPAN', this.label), Builder.node( 'IMG', { src: ( this.className == 'big' ? '/webpage/int/images/crm_free_trial.png' : '/webpage/int/images/crm_free_trial.png' )}) ]  );
		this.regInput = Builder.node('INPUT', { type: 'text', id: 'simplereg-input', value: 'Your e-mail address' } );
	
		this.regForm = Builder.node( 'DIV', { style: 'display: none', className: 'simplereg-form ' + this.className },
			[ this.regInput, this.regButton, Builder.node('DIV', { className: 'simplereg-warning' } )] );
		
		this.element.appendChild( this.regForm );
		setTimeout(this.display.bind(this), 10);
		Event.observe(this.regButton, 'click', this.submit.bind(this) );
		Event.observe(this.regInput, 'click', this.inputClick.bind(this) );
		Event.observe(this.regInput, 'keydown', this.inputKeyDown.bind(this) );
		Event.observe(this.regInput, 'blur', this.inputBlur.bind(this) );
	},
	
	display: function() {
		this.setWidth();
		new Effect.Appear(this.regForm);
	},
	
	setWidth: function() {
		try {
			var availableWidth = this.regForm.parentNode.offsetWidth;
			this.regInput.style.width = Math.min( availableWidth - this.regButton.offsetWidth - 10, 200) + 'px';
		} catch ( er ) {}
	},
	
	warn: function( str ) {
		var target = $A(this.element.getElementsByTagName('DIV')).last();
		target.innerHTML = str;
	},
	
	clearWarning: function() {
		this.warn('');
	},
	
	submit: function() {
		var value = $F(Form.getInputs(this.element)[0]);
		if ( value && value.match(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/) ) {
			this.clearWarning();
			document.location.href = this.actionUrl + '&userEmailAddress=' + value + '&source=' + document.location.href + '#' + this.source;
		} else {
			new Effect.Shake($(Form.getInputs(this.element)[0]));
			this.warn('Please enter a valid e-mail address');
		}
	},
	
	inputClick: function() {
		var value = $F(Form.getInputs(this.element)[0]);
		if ( value.match(new RegExp(this.defaultText)) )
			Form.getInputs(this.element)[0].value = '';
	},
	
	inputKeyDown: function() {
		if ( event.keyCode == Event.KEY_RETURN ) {
			this.submit();
			Event.stop( event );
		}
	},
	
	inputBlur: function() {
		if ( $F(Form.getInputs(this.element)[0]) == '' ) {
			Form.getInputs(this.element)[0].value = this.defaultText;
		}
	}
}

var UserCounter = {
	_loaded: false,
	_elements: [],
	numberOfUsers: '',
	
	load: function() {
		this.numberOfUsers = '8000';
		this.fillElements();
		return;
		this._loaded = true;
		new Ajax.Request('/script/system/admin/countuser.asp', {
			method: 'get',
			onSuccess: function(res) {
				var xmlDoc = Document.Xml.create(res.responseText);
				if ( xmlDoc && xmlDoc.documentElement ) {
					var totalCountNode = xmlDoc.documentElement.selectSingleNode('//TotalCount');
				}
				else {
					// hack for opera
					var totalCountNode = {
						text : res.responseText.substring( res.responseText.indexOf("<TotalCount>") + 12, res.responseText.indexOf("</TotalCount>") )
					};
				}
				if ( totalCountNode ) {
					
					this.numberOfUsers = totalCountNode.text;
					this.fillElements();
				}
			}.bind(this)
		});	
	},
	
	writeNumberOfUsers: function(element) {
		element = $(element);
		if ( !element )
			return;
		this._elements.push(element);
		if ( !this._loaded ) {
			this.load();
		} else {
			this.fillElements();
		}
	},
	
	fillElements: function() {
		
		this._elements.each( function(element) {
			//alert( element.outerHTML );
			element.innerHTML = this.numberOfUsers;
		}.bind(this));
	}
}