	/* INIT */
	var browser=navigator.appName;
	
	window.addEvent('domready', function()
	{
		window.styled_inputs = new Array();
		var all_inputs = document.getElementsByTagName( "INPUT" ); var radios = new Object(); 
		for (var i=0; i<all_inputs.length; i++ )
		{
			if ( all_inputs[i].className.substr( 0, 6 ) ==  "styled" )
			{
				var classes = all_inputs[i].className.split( " " );
				var c = classes[1];
				var t = all_inputs[i].type;
				if ( t=='checkbox' )
				{
					if ( all_inputs[i].id ) window.styled_inputs[all_inputs[i].id] = new CheckBox( all_inputs[i], all_inputs[i].name, all_inputs[i].value, all_inputs[i].title,  all_inputs[i].checked?1:0, {Checked: c+'_checkbox_checked', Unchecked: c+'_checkbox_unchecked'} );
					else new CheckBox( all_inputs[i], all_inputs[i].name, all_inputs[i].value, all_inputs[i].title,  all_inputs[i].checked?1:0, {Checked: c+'_checkbox_checked', Unchecked: c+'_checkbox_unchecked'} );
				}
				else if ( t=='radio' )
				{

					if ( radios[all_inputs[i].name] )
					{
						radios[all_inputs[i].name].AddChoice( all_inputs[i].title, all_inputs[i].value, all_inputs[i].checked?1:0, all_inputs[i] );
					}
					else
					{
						radios[all_inputs[i].name] = new RadioMenu( all_inputs[i], all_inputs[i].name, {Checked: c+'_radio_checked', Unchecked: c+'_radio_unchecked'}, parseInt(all_inputs[i].lang) );
						radios[all_inputs[i].name].AddChoice( all_inputs[i].title, all_inputs[i].value, all_inputs[i].checked?1:0, all_inputs[i] );

						if ( all_inputs[i].id ) window.styled_inputs[all_inputs[i].id] = radios[all_inputs[i].name];
					}
				}
			}
		}
		if(browser!='Microsoft Internet Explorer'){
		var all_selects = document.getElementsByTagName( "SELECT" ); var selects = new Object(); 
		for ( i=0; i<all_selects.length; i++ )
		{
			if ( all_selects[i].className.substr( 0, 6 ) ==  "styled" )
			{
				classes = all_selects[i].className.split( " " );
				var c = classes[1]; var c2 = classes[2];
				selects[all_selects[i].name] = new SelectMenu( all_selects[i], all_selects[i].name, "", 0, { Main: c+"_select"+ " "+c2, Box: c+"_select_box", OptionsHolder: c+"_select_holder", Option: c+"_select_option", OptionHover: c+"_select_option_hover" } );

				var all_options = all_selects[i].options;
				$each( all_options, function( value, key ) { selects[all_selects[i].name].AddOption( value.innerHTML, value.value, value.selected?1:0, value.disabled?1:0 ); });
			}
		}
		
		$each( selects, function( value, key ) { value.Write(); });
		}
		$each( radios, function( value, key ) { value.Write(); });
	});
	
	/* --------------------------------------------------------------------------------

	Constructor:
	--------------
		CheckBox
		(
			ContainerID : String,
			HiddenInputName : String,
			CheckboxValue : *,
			CheckboxLabel : *,
			Checked : Boolean,
			CssStyles : Object { Checked, Unchecked }
		);

	-------------------------------------------------------------------------------- */

	function CheckBox(Cont, Name, Value, Label, Checked, Styles)
	{
		this.Cont = Cont.parentNode;
		this.Input = document.createElement('INPUT');
		this.Box = new Element('DIV');
		this.IsChecked = Checked ? true : false;
		this.Value = Value;

		with(this.Input)
		{
			setAttribute('type', 'hidden');
			setAttribute('name', Name);
			setAttribute('value', this.IsChecked ? Value : '');
		}

		with(this.Box)
		{
			className = this.IsChecked ? Styles.Checked : Styles.Unchecked;
			innerHTML = Label;
			if ( Label );
			{
				style.paddingLeft = "23px";
			}
		}

		Cont.style.display = "none";
		this.Cont.insertBefore( this.Input, Cont );
		this.Cont.insertBefore( this.Box, Cont );
		this.Cont.removeChild( Cont );

		var Self = this;

		this.Set = function( v )
		{
			v = parseInt( v );
			if ( !v )
			{
				this.Box.className = Styles.Unchecked;
				Self.Input.value = '';
			}
			else
			{
				this.Box.className = Styles.Checked;
				Self.Input.value = Self.Value;
			}
			Self.IsChecked = v;
		}

		this.Box.onclick = function()
		{
			if(Self.IsChecked)
			{
				this.className = Styles.Unchecked;
				Self.Input.value = '';
			}
			else
			{
				this.className = Styles.Checked;
				Self.Input.value = Self.Value;
			}

			Self.IsChecked = !Self.IsChecked;
			if ( document.selection ) document.selection.empty();
			else if ( window.getSelection ) window.getSelection().removeAllRanges();
		}
	}


	/* --------------------------------------------------------------------------------

	Constructor:
	--------------
		RadioMenu
		(
			ContainerID : String,
			HiddenInputName : String,
			CssStyles : Object { Checked, Unchecked }
		);

	Methods:
	-----------
		AddChoice
		(
			ChoiceLabel : String,
			ChoiceValue : *
		);

		Write();

	-------------------------------------------------------------------------------- */

	function RadioMenu(Cont, Name, Styles, NewLine)
	{
		this.Cont = Cont.parentNode;
		this.Items = new Array();
		this.Choices = new Array();
		this.FormName = Name;
		this.Selected = 0;
		this.Previous = 0;
		this.Input = document.createElement('INPUT');
		this.Css = Styles;
		this.NewLine = NewLine;

		with(this.Input)
		{
			setAttribute('type', 'hidden');
			setAttribute('name', Name);
			setAttribute('value', '');
		}

		this.Cont.appendChild(this.Input);
	}

	RadioMenu.prototype.AddChoice = function(ChoiceLabel, ChoiceValue, Selected, Item)
	{
		this.Choices.push
		(
			{
				Label : ChoiceLabel,
				Value : ChoiceValue,
				Sel : Selected
			}
		);
		this.Items[this.Items.length] = Item;
	}

	RadioMenu.prototype.SetValue = function(ChoiceID)
	{
		this.Input.value = this.Choices[ChoiceID].Value;
	}

	RadioMenu.prototype.Set = function(ChoiceID)
	{
		var Self = this;
		Self.SetValue( ChoiceID );
		Self.Previous = Self.Selected;
		Self.Selected = ChoiceID;
		$(Self.FormName + '_' + Self.Previous).className = Self.Css.Unchecked;
		$(Self.FormName + '_' + Self.Selected).className = Self.Css.Checked;
	}

	RadioMenu.prototype.Write = function()
	{
		var Self = this;
		var Len = this.Choices.length;
		this.SetValue(0);

		for(var i = 0; i < Len; i++)
		{
			var NL = document.createElement('br'); NL.className = "bent";
			var C = new Element('DIV');
			C.onselectstart = new Function('return false');
			C.innerHTML = this.Choices[i].Label;
			C.id = this.FormName + '_' + i;
			C.ID = i;
			if ( this.Choices[i].Sel )
			{
				this.SetValue( i );
				C.className = this.Css.Checked;
				this.Selected = i;
			}
			else C.className = this.Css.Unchecked;
			C.inject( $(this.Items[0]), 'before' );
			if ( this.NewLine==1 ) NL.inject( C, 'after' );

			C.onclick = function()
			{
				Self.SetValue(this.ID);
				Self.Previous = Self.Selected;
				Self.Selected = this.ID;
				$(Self.FormName + '_' + Self.Previous).className = Self.Css.Unchecked;
				$(Self.FormName + '_' + Self.Selected).className = Self.Css.Checked;
			}
		}
		$each( this.Items, function( value, key ) { $(value).dispose(); } );
	}


	/* --------------------------------------------------------------------------------

	Constructor:
	--------------
		SelectMenu
		(
			ContainerID : String,
			HiddenInputName : String,
			DefaultLabel : *,
			DefaultValue : *,
			CssStyles : Object { Main, Box, OptionsHolder, Option, OptionHover }
		);

	Methods:
	-----------
		AddOption
		(
			OptionLabel : *,
			OptionValue : *,
			Selected : Boolean
		);

		SetValue
		(
			Label : *,
			Value : *
		);

		Write();

	-------------------------------------------------------------------------------- */

	var ZIndex = 300;

	function SelectMenu(Cont, Name, Label, Value, Styles)
	{
		this.Options = new Array();
		this.CSS = Styles;

		this.Cont = new Element( "div" );
		this.Cont.inject( Cont, 'before' );
		this.Item = Cont;

		this.Holder = document.createElement('DIV');
		this.Holder.style.position = 'relative';

		this.Cont.className = Styles.Main;

		this.Box = document.createElement('DIV');
		this.Box.className = Styles.Box;
		this.Box.innerHTML = Label;

		this.Input = document.createElement('INPUT');
		with(this.Input)
		{
			setAttribute('type', 'hidden');
			setAttribute('name', Name);
			setAttribute('value', Value);
		}

		this.OptHolder = document.createElement('DIV');
		this.OptHolder.className = Styles.OptionsHolder;

		this.Cont.appendChild(this.Holder);
		this.Holder.appendChild(this.Input);
		this.Holder.appendChild(this.Box);
		this.Holder.appendChild(this.OptHolder);

		with(this.OptHolder.style)
		{
			position = 'absolute';
			zIndex = ++ZIndex;
			display = 'none';
		}
	}

	SelectMenu.prototype.AddOption = function( Label, Value, Selected, Disabled )
	{
		this.Options.push
		(
			{
				Label : Label,
				Value : Value,
				Disabled : Disabled
			}
		);

		if ( Selected )
		{
			this.SetValue( Label,  Value );
		}
	}

	SelectMenu.prototype.SetValue = function(Label, Value)
	{
		this.Box.innerHTML = Label;
		this.Input.value = Value;
	}

	SelectMenu.prototype.Write = function()
	{
		var Self = this;
		var Len = this.Options.length;

		for(var i = 0; i < Len; i++)
		{
			var Opt = document.createElement('DIV');

			Opt.className = this.CSS.Option;
			Opt.innerHTML = this.Options[i].Label;
			Opt.MyValue = this.Options[i].Value;
			Opt.Disabled = this.Options[i].Disabled;

			Opt.onmouseover = function()
			{
				this.className = Self.CSS.OptionHover;
			}

			Opt.onmouseout = function()
			{
				if ( this.Disabled ) this.className = Self.CSS.Option + "_disabled";
				else this.className = Self.CSS.Option;
			}

			if ( this.Options[i].Disabled ) Opt.className = this.CSS.Option + "_disabled";

			if ( !this.Options[i].Disabled ) Opt.onclick = function()
			{
				Self.SetValue(this.innerHTML, this.MyValue);
				Self.OptHolder.style.display = 'none';

				if(typeof Self.onChange == 'function')
				{
					Self.onChange(this.MyValue);
				}
			}

			this.OptHolder.appendChild( Opt );

			this.Item.dispose();
		}

		this.Box.onclick = function()
		{
			if(Self.OptHolder.style.display == 'none')
			{
				Self.OptHolder.style.display = 'block';
				Self.OptHolder.style.zIndex = ZIndex+1000;
			}
			else
			{
				Self.OptHolder.style.display = 'none';
			}
		}
	}


	/* --------------------------------------------------------------------------------

	Constructor:
	--------------
		AjaxForm
		(
			FormRef : Form reference,
			HiddenInputName : String,
			DefaultLabel : *,
			DefaultValue : *,
			CssStyles : Object { Box, OptionsHolder, Option, OptionHover }
		);

	Methods:
	-----------
		AddOption
		(
			OptionLabel : *,
			OptionValue : *,
			Selected : Boolean
		);

		SetValue
		(
			Label : *,
			Value : *
		);

		Write();

	-------------------------------------------------------------------------------- */

	function AjaxForm( FormRef, callback )
	{
		
		if ( !( !FormRef.lang || FormRef.lang=='undefined' || FormRef.lang=='NULL' || FormRef.lang=='0' ) ) return false;

		FormRef.submited = 0;
		FormRef.onSubmit = 0;
		FormRef.callback = callback;
		resp = ''; 
		
		FormRef.onSubmit = function ( e )
		{
			//Prevents the default submit event from loading a new page.
			if ( e ) e.stop();
			if ( this.submited ) return false;
			if ( this.locked==1 ) return false;
			//Empty the log and show the spinning indicator.
			this.set( 'send', {onComplete: function(response)
			{
				//log.removeClass('ajax-loading');
				FormRef.loader.dispose();
				FormRef.fade( 1 );

				FormRef.submited = 0;
				
				resp = JSON.decode(response);
				if ( !FormRef.callback ) displayMessages( resp.messages );
				else { window.tmp = resp; FormRef.callback(resp); }
			}});
			this.fade( 0.3 ); //this.readOnly = true;
			t = this.offsetTop; l = this.offsetLeft;
			h = Math.round(this.offsetHeight-32)/2; w = Math.round(this.offsetWidth-32)/2;

			this.loader = new Element( 'div', {'class': 'loader', 'styles': { 'top': t+h+'px', 'left': l+w+'px' }});
			this.loader.inject( this, 'before' );
			this.send();
			this.submited = 1;
			return false;
		}
		$(FormRef).addEvent( 'submit', FormRef.onSubmit );
		return this;
	}

	function displayMessages( messages )
	{
		var msgs = "";
		if ( messages ) for (var i=0; i<messages.length; i++ )
		{	
			redirect = messages[i].redirect;
			type = messages[i].type;
			switch ( type )
			{
				case '1': roar.alert( '', messages[i].message, 'fail' ); break;
				case '2': roar.alert( '', messages[i].message, 'success' ); break;
				default: roar.alert( '', messages[i].message, 'warning' ); break;
			}
			if(redirect != false) {
				setTimeout( "window.location = redirect", 2000 );
			}

		}
	}

	var roar = false;
	window.addEvent('domready', function()
	{
		roar = new Roar( {
			duration: 5000,
			position: 'upperLeft',
			margin: {x: 0, y: 0},
			offset: 1,
			className: 'roar',
			opacity: 1,
			full_width: 1
		} );
		var forms = document.getElementsByTagName( 'form' );
		for (var i=0; i<forms.length; i++ ) { 
			if(forms[i].className != 'noAjax') {
				AjaxForm ( forms[i], forms[i].onsubmit );
			}
		}
	});