if( window.$ ) {
	$(function() {
        function addActiveClass() {
            this.className += ' active';
        }

        function removeActiveClass() {
            this.className = this.className.replace( 'active', '' );
        }

        function addLIActiveClass() {
            var target = this;
            while( target && target.nodeName && target.nodeName != 'FORM' && target.nodeName != 'BODY' ) {
                if( target.nodeName == 'LI' ) {
                    addActiveClass.call( target );
                    break;
                }

                target = target.parentNode;
            }
        }

        function removeLIActiveClass() {
            var target = this;
            while( target && target.nodeName && target.nodeName != 'FORM' && target.nodeName != 'BODY' ) {
                if( target.nodeName == 'LI' ) {
                    removeActiveClass.call( target );
                    break;
                }

                target = target.parentNode;
            }
        }

        $( 'form' ).find( 'input:text, input:password, select, textarea' ).bind({
            focus : addLIActiveClass,
            blur : removeLIActiveClass
        })
        .bind({
            focus : addActiveClass,
            blur : removeActiveClass
        })
        .end().find( 'input:checkbox, input:radio' ).bind({
            focus : addLIActiveClass,
            blur : removeLIActiveClass
        })
    
	});
}

/* ----------------------------------------------------------------------- */

FormController = function( data ) {
	FormController.superClass.apply( this, arguments );
	this.forms = new Array();
	this.window = false;

	if(
		window[ 'Window' ] &&
		Window.superClass &&
		Window.superClass == Glyph
	) {
		this.window = new Window({
			'hidden': true,
			'closable': true,
			'center': true,
			'block': true,
			'width': 400
		});
	}

	for( var c in data ) {
		if( data[ c ][ 'id' ] ) {
			if( !this.getEl( data[ c ][ 'id' ] ) ) continue;
			this.addForm( data[ c ] );
		}
	}

	this.addHandler( window, 'resize', this.reBuild );
}
FormController.inheritsFrom( Glyph );

/* ----------------------------------------------------------------------- */

FormController.prototype.isValid = function() {
	var status = true;
	for( var c in this.forms ) {
		if( !this.forms[ c ].isValid() ) var status = false;
	}
	return status;
}

/* ----------------------------------------------------------------------- */

FormController.prototype.addForm = function( data ) {
	var form = new Form( data );
	if( !form ) return false;
	this.forms[ data[ 'id' ] ] = form;
	
	var _self = this;
	form.attachObserver( 'Submit', function() {
		if( _self.processing ) return false;
		form.onSubmit();
	}, this );

	var _self = this;
	if( form[ 'data' ][ 'handler' ] ) {
		form.setHandler( function() {
			this[ 'data' ][ 'handler' ].call( _self, this );
		});
	}

	form.attachObserver( 'InputStatusChanged', this.onInputStatusChanged, this );
	return form;
}

/* ----------------------------------------------------------------------- */

FormController.prototype.getForm = function( key ) {
	return this.forms[ key ];
}

/* ----------------------------------------------------------------------- */

FormController.prototype.removeForm = function( key ) {
	var form = this.getForm( key );
	if( !form ) return false;

	delete this.forms[ key ];
	return true;
}

/* ----------------------------------------------------------------------- */

FormController.prototype.setAlert = function( formId, inputId, flag, msg, code ) {
	var form = this.getForm( formId );
	if( !form || !form.getInput ) return false;

	var input = form.getInput( inputId );
	if( !input || !input.setAlert ) return false;

	input.setError( flag, msg, code );
}

/* ----------------------------------------------------------------------- */

FormController.prototype.setAlertByName = function( formId, inputName, flag, msg, code ) {
	var form = this.getForm( formId );
	if( !form || !form.getInput ) return false;

	var input = form.getInputByName( inputName );
	if( !input ) {
		return false;
	}

	if( input instanceof Array ) {
		for( var c in input ) {
			if( !input[ c ].setError ) continue;
			input.setError( flag, msg );
		}
	} else {
		if( !input.setError ) return false;
		input.setError( flag, msg );
	}
}

/* ----------------------------------------------------------------------- */

FormController.prototype.setLoad = function( flag ) {
	this.processing = flag;
	var processPad = this.getEl( 'ajax_load_ico' );

	if( processPad ) {
		this.setStyle({'display': ( flag ? 'block' : 'none' )}, processPad );
		return true;
	}
	return false;
}

/* ---------------------------------- blockSubmit  --------------------------------- */

FormController.prototype.blockSubmit = function( flag ) {
	this.processing = flag;
	var btns = document.getElementsByTagName("button");

	if( btns ) {
		for( i in btns ) {
			if( btns[i] && btns[i].type == "submit" ) {
				if( flag ) {

				    this.setClassName( 'disabled', true, btns[i] );
				    this.setProperty({'disabled': 'disabled'}, btns[i] );
				}

				else {
                    this.setClassName( 'disabled', false, btns[i] );	
                    btns[i].removeAttribute('disabled');
				}

			}
		}
		return true;
	}
	return false;
}

/* ----------------------------------------------------------------------- */

FormController.prototype.handleError = function( code ) {
	this.setLoad(false);
	this.blockSubmit( false );
	if(
		_text &&
		this.window
	) {
		this.window.fill( _text( code || 'ERROR' ) ).setTitle( _text( 'ERROR_TITLE' ) ).show();
	}
}

/* ----------------------------------------------------------------------- */

FormController.prototype.reBuild = function() {
	for( var c in this.forms ) {
		this.forms[ c ].reBuild();
	}
}

/* ----------------------------------------------------------------------- */

FormController.prototype.showPart = function( id, flag ) {
	var part = this.getEl( id );
	if( part ) {
		this.setClassName( 'hidden', !flag, part );
	}
}


/**
 * findInput
 */
FormController.prototype.findInput = function( input_name ) {
    for( var c in this.forms ) {
        var input = this.forms[c].getInput( input_name );
        if( input )
            return input;
    }

    return false;
}

/* -------------------------------------------------------------------------------------------------------------*/

FormController.prototype.setInputChangedObserver = function( id, flag, handler, context ) {
	var input = this.form.getInput( id ),
		context = context || this;
	if(
		input &&
		input instanceof Glyph
	) {
		if( flag ) {
			var already = false;
			if( input[ 'Observers' ][ 'Changed' ] ) {
				for( var c in input[ 'Observers' ][ 'Changed' ] ) {
					if( 
						input[ 'Observers' ][ 'Changed' ][ c ][ 'Context' ] == context &&
						input[ 'Observers' ][ 'Changed' ][ c ][ 'Action' ] == handler
					) {
						already = true;
					}
				}
			}

			if( !already ) {
				input.attachObserver( 'Changed', handler, context );
			}
		} else {
			input.detachObserver( 'Changed', handler, context );
		}
	}
}


FormController.prototype.updateCaptcha = function( id, url ) {
	var captcha = this.getEl( id );
	if(
		captcha &&
		captcha.nodeName == 'IMG'
	) {

		captcha.src = ( url ? url : '/' + PageStatus.Lang() + '/my_captcha/' ) + '?renew=1&base=' + Math.random() * 10000;
	}
}

/* ----------------------------------------------------------------------- */
/* ----------------------------------------------------------------------- */

Form = function( data ) {
	Form.superClass.apply( this, arguments );
	if( !this.getEl( data[ 'id' ] ) ) return false;
	this.id = data[ 'id' ];
	this.data = data;
	this.element = this.getEl( this.id );
	this.setHandler();
	this.status = null;
	this.processing = false;
	
	this.inputs = new Array();
	for( var c in data[ 'options' ] ) {
		this.addInput( data[ 'options' ][ c ] );
	}

	this.addHandler( this.element, 'submit', this.check );
	this.addHandler( this.element, 'submit', this.notify, 'onSubmit' );
}
Form.inheritsFrom( Glyph );

/* ----------------------------------------------------------------------- */

Form.prototype.setHandler = function( handler ) {
	var _self = this;
	this.onSubmit = handler || function() { 
		_self.submit.call( _self );
	}
}

/* ----------------------------------------------------------------------- */

Form.prototype.removeAlerts = function() {
    for( var i in this.inputs ) {
        if( this.inputs[i].alert ) {
            this.inputs[i].removeAlert();
        }
    }
}

/* ----------------------------------------------------------------------- */

// fires native form submit
Form.prototype.submit = function( params ) {
    if( window.GAO && GAO.userIdentityParams ) {
        for( var c in GAO.userIdentityParams ) {
            this.addParam( c, GAO.userIdentityParams[ c ] );        
        }
    }

    for( var c in params ) {
		this.addParam( c, params[ c ] );
	}

	this.element.submit();
}

/* ----------------------------------------------------------------------- */

Form.prototype.addParams = function( params ) {
	for( var c in params ) {
		this.addParam( c, params[ c ] );
	}
}

/* ----------------------------------------------------------------------- */

Form.prototype.addParam = function( key, data ) {
	this.createNode( 'input', this.element, {'type': 'hidden', 'value': data.toString(), 'name': key});
	//var html = this.element.innerHTML;
    //this.element.innerHTML = html + '<input type="hidden" value="' + data + '" name="' + key + '">';	
}

/* ----------------------------------------------------------------------- */

Form.prototype.check = function( params, ev ) {
	var status = true;
    
	for( var c in this.inputs ) {
		var input = this.inputs[ c ];
		if( !input.check() ) {
			try{
				console.log(input);
			}catch(e){}
			status = false;
			if( input.input && !last_input ) var last_input = input; // for focus on last wrong input
		}
	}

	if( 
		!status &&
		last_input &&
		last_input.input &&
		window.$ && $.scrollTo
	) {
		//last_input.input.focus();
		$.scrollTo( last_input.input, 600 );
		if( $( last_input.input ).is( ':visible' ) )
		    last_input.input.focus();
	}

	if( status ) {
		this.notify( 'Submit' );
	}
	this.preventDefault( ev );
	return status;
}

/* ----------------------------------------------------------------------- */

Form.prototype.addInput = function( data ) {
	if( this.getInput( data[ 'id' ] || data[ 'name' ] ) ) return false;

	if( data[ 'type' ] != 'radio' && data[ 'type' ] != 'text' ) {
		var element = this.getEl( data[ 'id' ] );
		if( !element ) return false;
		var input = false;

		switch( element[ 'type' ] ) {
			case 'select-one':input = this.addSelect( element, data[ 'options' ] );break;
			case 'checkbox':input = this.addCheckBox( element, data[ 'options' ] );break;
			default:input = this.addTextInput( element, data[ 'options' ] );
		}
	} else if( data['type'] == 'radio' ) {
		input = this.addRadio( data );
	}
	if( !input ) return false;

     // add form to inner input
    if( data && data['options'] && data['options']['check'] )
        input.form = this;


	this.inputs[ data[ 'id' ] || data[ 'name' ] ] = input;
	input.attachObserver( 'Changed', this.onInputChanged, this );

	if( input.preCheck ) input.preCheck();

	return input;
}

Form.prototype.addInputs = function( obj_inputs ) {
	for( var c in obj_inputs ) {
		var input_name = obj_inputs[c]['id'] || obj_inputs[c]['name'];
		if( !this.getInput( input_name ) ) {
			this.addInput( obj_inputs[c] );
		}
	}
}

/* ----------------------------------------------------------------------- */

Form.prototype.onInputChanged = function( params, input ) {
	if(
		input &&
		input.isValid &&
		input.setAlert
		
	) {
		var isValid = input.isValid();
		if( isValid != null ) {
			input.setAlert( isValid );
		}
	}

	this.notify( 'InputChanged', input );
}

/* ----------------------------------------------------------------------- */

Form.prototype.onInputStatusChanged = function( flag, input ) {
	this.notify( 'InputStatusChanged', input );
}

/* ----------------------------------------------------------------------- */

Form.prototype.addTextInput = function( element, data ) {
	var input = new TextInput( element, data );

	input.attachObserver( 'StatusChanged', this.onInputStatusChanged, this );
	input.attachObserver( 'Typing', this.onInputChanged, this );

	if( !input.isTemplated() && input.getValue() ) {
		input.check();
	}

	return input;
}

/* ----------------------------------------------------------------------- */

Form.prototype.addCheckBox = function( element, data ) {
	var checkbox = new CheckBox( element, data );
	return checkbox;
}

/* ----------------------------------------------------------------------- */

Form.prototype.addRadio = function( data ) {
	var radio = new Radio( data );
	if( !radio.elements.length )
		return false;
	radio.attachObserver( 'StatusChanged', this.onInputStatusChanged, this );
	return radio;
}

/* ----------------------------------------------------------------------- */

Form.prototype.blockSubmit = function( flag ) {
	this.processing = flag;
	var btns = this.element.getElementsByTagName("button");

	if( btns ) {
		for( i in btns ) {
			if( btns[i] && btns[i].type == "submit" ) {
				if( flag ) {

				    this.setClassName( 'disabled', true, btns[i] );
				    this.setProperty({'disabled': 'disabled'}, btns[i] );
				}

				else {
                    this.setClassName( 'disabled', false, btns[i] );
                    btns[i].removeAttribute('disabled');
				}

			}
		}
		return true;
	}
	return false;
}

/* ----------------------------------------------------------------------- */

Form.prototype.addSelect = function( element, data ) {
	var select = new Select( element, data );

	select.attachObserver( 'StatusChanged', this.onInputStatusChanged, this );
	select.attachObserver( 'Typing', this.onInputChanged, this );

	if( select.checkMethod() ) select.check();

	return select;
}

/* ----------------------------------------------------------------------- */

Form.prototype.removeInput = function( key ) {
	var input = this.getInput( key );
	if( !input ) return false;

	this.setClassName( 'error', false, input.input );
	input.detach();

	delete this.inputs[ key ];
	input.notify( 'Removed' );
	return true;
}

Form.prototype.removeInputs = function( arr_inputs ) {
	if( arr_inputs instanceof Array )
        for( var c in arr_inputs )
            this.removeInput( arr_inputs[c] );
}

/* ----------------------------------------------------------------------- */

Form.prototype.getInput = function( key ) {
	return this.inputs[ key ];
}

/* ----------------------------------------------------------------------- */

Form.prototype.getInputByName = function( name ) {
	var result = new Array();
	for( var c in this.inputs ) {
		if( this.inputs[ c ].getName() == name ) {
			result.push( this.inputs[ c ] );
		}
	}

	if( result[ 'length' ] == 1 ) result = result[ 0 ];
	return result;
}

/* ----------------------------------------------------------------------- */

Form.prototype.remove = function() {
	for( var c in this.inputs ) {
		this.removeInput( this.getInput( c ) );
	}
	this.element.parentNode.removeChild( this.element );
	return true;
}

/* ----------------------------------------------------------------------- */

Form.prototype.getValues = function() {
	var result = new Object();
	for( var c in this.inputs ) {
		var cInput = this.inputs[ c ];
		result[ cInput.getName() ] = cInput.getValue();
	}
	return result;
}

/* ----------------------------------------------------------------------- */

Form.prototype.isValid = function() {
	for( var c in this.inputs ) {
		if(
			this.inputs[ c ].isValid && 
			!this.inputs[ c ].isValid()
		) return false;
	}
	return true;
}

/* ----------------------------------------------------------------------- */

Form.prototype.setFieldError = function( error ) {
    var first_error_input; //
	for( var c in error ) {
		var input = this.getInput( c );
		if(
			input &&
			input.setError
		) {
		    if( !first_error_input )
		        first_error_input = input;
			input.setError( false, _text( error[ c ] ) );
		}
	}

	return first_error_input;
}

/* ----------------------------------------------------------------------- */

Form.prototype.reBuild = function() {
	for( var c in this.inputs ) {
		if(
			this.inputs[ c ] &&
			this.inputs[ c ].alert &&
			this.inputs[ c ].alert instanceof Popup
		) {
			this.inputs[ c ].alert.setPosition();
		}
	}
}


/* ----------------------------------------------------------------------- */
/* ----------------------------------------------------------------------- */

Input = function( input, data ) {
	Input.superClass.apply( this, arguments );
	this.element = input;
	this.input = input;

	this.status = null;
	this.type = input[ 'type' ];
	this.data = data;
	this.id = input[ 'id' ];

	if( data ) {
		if( data[ 'error_to' ] != undefined ) {
			this.errPlace = this.getEl( data[ 'error_to' ] );
		}
		
		if(
			data[ 'check' ] &&
			data[ 'check' ] instanceof Function
		) {
		    //this.form =
			this.checkMethod = function() {
				return data[ 'check' ].call( this, this.input );
			}
		}
	}
}
Input.inheritsFrom( Glyph );

/* ----------------------------------------------------------------------- */

Input.prototype.disable = function( flag ) {
	this.disabled = flag;
	this.input[ 'disabled' ] = flag;
}

/* ----------------------------------------------------------------------- */

Input.prototype.getValue = function() {
	return this.input.value;
}

/* ----------------------------------------------------------------------- */

Input.prototype.setValue = function( value ) {}

/* ----------------------------------------------------------------------- */

Input.prototype.getVisibleValue = function() {
	return this.getValue();
}

/* ----------------------------------------------------------------------- */

Input.prototype.isValid = function() {
	return this.status;
}

/* ----------------------------------------------------------------------- */

Input.prototype.preCheck = function() {}

/* ----------------------------------------------------------------------- */

Input.prototype.check = function( params, ev ) {

	var os = this.status;
	if( ev ) {
		var code = ev.charCode || ev.keyCode;
		if( code == 9 ) return false;
	}

	var result = this.checkMethod.call( this, this.input ) && ( this.checkFunction ? this.checkFunction.call( this, this.input ) : true );
	if( result == null ) return false;    

	if( this.checkFunction && !this.checkFunction.call( this, this.input ) ) {
		this.setError( result, this.errMsg[ 'ILLEGAL' ] );
	}
	else
        this.setError( result );    

	if( !( os === this.status ) ) {
		this.notify( 'StatusChanged', this.status );
	}
	return this.status;
}

/* ----------------------------------------------------------------------- */

Input.prototype.setError = function( flag, msg ) {
	if( this.setAlert ) this.setAlert( flag, msg );
	this.status = flag;
	if(
		this.alert && 
		this.alert instanceof Alert
	) {
		this.alert.set( this.status );
		if( this.status ) {
			this.alert.hide( this.input );
		} else {
			this.alert.show( this.input );
		}
	}
	this.setClassName( 'error', !flag, this.input );
}

/* ----------------------------------------------------------------------- */

Input.prototype.getName = function() {
	return this.input[ 'name' ];
}

/* ----------------------------------------------------------------------- */

Input.prototype.checkMethod = function() {}

/* ----------------------------------------------------------------------- */

Input.prototype.detach = function() {}

/* ----------------------------------------------------------------------- */

Input.prototype.findLabel = function() {
	var id = this.input[ 'id' ];
	var allLabels = document.getElementsByTagName( 'LABEL' );
	for( var i = 0; i < allLabels.length; i ++ ) {
		var label = allLabels[ i ];
		if( id == label.getAttribute( 'for' ) ) {
			return label;
		}
	}
	return false;
}

/* ----------------------------------------------------------------------- */
/* ----------------------------------------------------------------------- */

TextInput = function( input, data ) {
	TextInput.superClass.apply( this, arguments );
    if(data) {
         this.alertType = data.alertType;
    } else {
        this.alertType = null;
    }

	this.templated = false;
	this.template = ( data && data[ 'template' ] && _text( data[ 'template' ] ) ) || false;
	if(
		this.template &&
		this.getValue() == ''
	) { //if value set via html value="" we should not apply template
		this.setTemplate( true );
	}

	if( this.getValue() === this.template ) this.templated = true;

	this.alert = false;
	if( data ) {
		if( typeof this.data[ 'alert' ] == 'string' ) {
			this.errMsg = {
				'UNKNOWN': this.data[ 'alert' ]
			}
		} else {
			this.errMsg = this.data[ 'alert' ];
		}

		if( data[ 'reg' ] ) {
			this.pattern = new RegExp( data[ 'reg' ] );
		}
		
		if( data[ 'maxlength' ] ) {
			this.setProperty({'maxlength': data[ 'maxlength' ] + ''}, this.input );
		}

		if( this.type == 'password' && data[ 'checkPassword' ] && typeof data[ 'checkPassword' ] == 'function' )
			this.addHandler( this.input, 'keyup', data[ 'checkPassword' ] );
	}

	this.addHandler( this.input, 'focus', function() {
		if( this.isTemplated() ) {
			this.setTemplate( false );
		}
	});

	this.addHandler( this.input, 'blur', function() {
		if( 
			this.getValue() == '' &&
			this.template
		) {
			this.setTemplate( true );
		}
	});

	var _self = this;

	this.addHandler( this.input, 'keyup', this.notify, 'Changed' );
	this.addHandler( this.input, 'keydown', this.notify, 'Typing' );
	this.addHandler( this.input, 'blur', this.notify, 'Blur' );
	this.addHandler( this.input, 'change', this.check );
	this.addHandler( this.input, 'change', this.notify, 'Changed' );
	this.addHandler( this.input, 'change', function() {_self.notify( 'Change' )} );
}
TextInput.inheritsFrom( Input );

/* ----------------------------------------------------------------------- */

TextInput.prototype.setTemplate = function( flag ) {
	if( flag ) {
		this.input.value = this.template;
		this.templated = true;
	} else {
		this.templated = false;
		this.input.value = '';
	}
	this.setClassName( 'template', flag, this.input );
}

/* ----------------------------------------------------------------------- */

TextInput.prototype.isTemplated = function() {
	return this.templated;
}

/* ----------------------------------------------------------------------- */

TextInput.prototype.setValue = function( value ) {
	this.input.value = value + '';
}

/* ----------------------------------------------------------------------- */

TextInput.prototype.setAlert = function( flag, msg, code ) {

	//if( this.errPlace === false ) return false;
	if( !this.alert )
        if(this.alertType == 'euforex') {
            this.alert = new EUForexAlert( this.input, this.errPlace );
        } else {
            this.alert = new Alert( this.input, this.errPlace );
        }

    this.alert.hide();
    
	if( flag ) {
		if( msg != undefined ) {
			this.alert.fill( _text( msg ) );
		} else {
			this.alert.hide();
		}
	} else {
		var dcode = code || 'UNKNOWN';

		if(
			!code &&
			!this.status
		) {
			var value = this.getValue();
			var isEmpty = ( 
				!value || 
				value == '' ||
				this.isTemplated() ||
				false
			);
			dcode = ( isEmpty ? 'EMPTY' : 'INCORRECT' );
		}

		if( this.errMsg ) {
			this.alert.fill(
				_text(
									msg ||
					this.errMsg[ dcode ] ||
					this.errMsg[ 'UNKNOWN' ]
				)
			);
		}
	}
    
	this.alert.set( flag );
	//this.alert.setClassName( 'correct', flag );
}

/* ----------------------------------------------------------------------- */

TextInput.prototype.detach = function() {
	this.removeAlert();
	
	//this.removeHandler( this.input, 'keyup', this.check );
	this.removeHandler( this.input, 'keyup', this.notify, 'Changed' );
	this.removeHandler( this.input, 'keydown', this.notify, 'Typing' );
	this.removeHandler( this.input, 'change', this.check );
	this.removeHandler( this.input, 'change', this.notify, 'Changed' );
}

/* ----------------------------------------------------------------------- */

TextInput.prototype.removeAlert = function() {
	if( !this.alert ) return false;
	this.alert.remove();
	this.alert = false;
	return true;
}

/* ----------------------------------------------------------------------- */

TextInput.prototype.preCheck = function() {
	if(
		this.getValue() &&
		!this.isTemplated()
	) {
		this.check();
	}
}

/* ----------------------------------------------------------------------- */

TextInput.prototype.checkMethod = function() {
	var value = this.getValue();
	if( this.isTemplated() ) value = '';
	if(
		value &&
		value.replace
	) {
		value = value.replace( /^\s\s*/, '' ).replace( /\s\s*$/, '' );
	}
	return this.pattern ? this.pattern.test( value ) : true;
}


/* ----------------------------------------------------------------------- */

TextInput.prototype.getValue = function() {
	return ( this.isTemplated() ? '' : this.input.value );
}

/* ----------------------------------------------------------------------- */
/* ----------------------------------------------------------------------- */

CheckBox = function( input, data ) {
	CheckBox.superClass.apply( this, arguments );

	this.addHandler( this.input, 'click', this.check );
	this.addHandler( this.input, 'change', this.notify, 'Changed' );

	if( data && data.not_required ) {
	    this.getValue = this.getNotRequiredValue;
	}
}
CheckBox.inheritsFrom( Input );

/* ----------------------------------------------------------------------- */

CheckBox.prototype.getNotRequiredValue = function() {
	return this.input[ 'checked' ] || 'null';
}

CheckBox.prototype.getValue = function() {
	return this.input[ 'checked' ];
}

/* ----------------------------------------------------------------------- */

CheckBox.prototype.checkMethod = function() {
	return this.getValue();
}

/* ----------------------------------------------------------------------- */

CheckBox.prototype.detach = function() {
	this.removeHandler( this.input, 'change', this.check );
	this.removeHandler( this.input, 'change', this.notify, 'Changed' );
}

/* ----------------------------------------------------------------------- */
/* ----------------------------------------------------------------------- */

Select = function( element, data ) {
	Select.superClass.apply( this, arguments );
}
Select.inheritsFrom( TextInput );

/* ----------------------------------------------------------------------- */

Select.prototype.getVisibleValue = function() {
	for( var i = 0; i < this.input[ 'options' ].length; i++ ) {
		var option = this.input[ 'options' ][ i ];
		if( option.getAttribute( 'value' ) == this.getValue() ) {
			return option.innerHTML;
		}
	}
	return false;
}

/* ----------------------------------------------------------------------- */

Select.prototype.setValue = function( value ) {
	var el = this.input;
	if( el ) {
		var idx = 0;
		var options = el.getElementsByTagName( 'OPTION' );
		for( var i = options.length; i; i-- ) {
			var option = options[ i - 1 ];
			if( option[ 'value' ] == value ) {
				idx = ( i - 1 );
				break;
			}
		}

		if( options[ idx ] ) {
			this.setProperty({'selected': 'selected'}, options[ idx ] );
			options[ idx ][ 'selected' ] = true;
		}
	}
}

/* ----------------------------------------------------------------------- */

Select.prototype.preCheck = function() {
	if( this.isValid() ) this.check();
}

/* ----------------------------------------------------------------------- */

Select.prototype.checkMethod = function() {
	if(
		this.getValue() == 'empty' ||
		this.getValue() == undefined ||
		this.getValue() === ''
	) return false;

	return true;
}

/* -------------------------------------------------------------------------------------------------------------*/

Select.prototype.setOptions = function( data, flag_empty ) {
	if( data ) {
		if( !flag_empty )
            this.createOption({
                'name': '------',
                'value': 'empty'
            });

		for( var c in data ) {
			if( data[ c ][ 'group' ] ) {
				this.createGroup( data[ c ] );
			} else {
				this.createOption( data[ c ] );
			}
		}

		this.disable( false );
		if( this.checkMethod() ) {
			this.check();
		}
		return true;
	}
	return false;
}

/* -------------------------------------------------------------------------------------------------------------*/

Select.prototype.createOption = function( data, group ) {
	var option = this.createNode( 'option', group || this.input, false, data[ 'name' ] + '' );
	if( data[ 'value' ] != undefined ) {
		this.setProperty({'value': data[ 'value' ]}, option );
	}

	if( data[ 'selected' ] ) {
		this.setProperty({'selected': 'selected'}, option );
	}

	if( data['class'] ) {
        this.setClassName( '' + data['class'], true , option );
	}

	if( data[ 'ban' ] ) {
		this.setClassName( 'ban', true , option );
	}
}

/* -------------------------------------------------------------------------------------------------------------*/

Select.prototype.createGroup = function( data ) {
	var group = this.createNode( 'optgroup', this.input );
	if( data[ 'group' ] ) {
		this.setProperty({'label': data[ 'group' ]}, group );
	}

	if( data[ 'options' ] ) {
		for( var c in data[ 'options' ] ) {
			this.createOption( data[ 'options' ][ c ], group );
		}
	}
}

/* -------------------------------------------------------------------------------------------------------------*/

Select.prototype.removeOptions = function() {

	var childs = this.input.childNodes;
	for( var i = childs.length; i; i-- ) {
		this.removeOption( childs[ i - 1 ] )
	}

	this.status = false;
	this.notify( 'Changed' );
	this.removeAlert();
	this.disable( true );
	this.setClassName( 'error', false, this.input );
	this.notify( 'OptionsRemoved' );
}

/* -------------------------------------------------------------------------------------------------------------*/

Select.prototype.removeOption = function( option ) {
	return this.input.removeChild( option );
}

/* -------------------------------------------------------------------------------------------------------------*/

Select.prototype.getGroup = function() {
	var value = this.getValue();
	if( !value ) return false;

	var option = false;
	for( var i = 0; i < this.input.options.length; i++ ) {
		if( this.input.options[ i ] && this.input.options[ i ][ 'value' ] == value ) {
			var option = this.input.options[ i ];
			break;
		}
	}
	
	if(
		option &&
		option.parentNode &&
		option.parentNode.nodeName == 'OPTGROUP'
	) {
		return option.parentNode
	}
	return false;
}

/* ----------------------------------------------------------------------- */

Radio = function( data ) {
	Radio.superClass.apply( this, arguments );
	this.elements = document.getElementsByName( data[ 'name' ] );
	this.selected = false;

	for( var i = 0; i < this.elements.length; i++ ) {
		if( this.elements[ i ][ 'checked' ] ) {
			this.selected = this.elements[ i ];
		}
	}

	this.data = ( data && data[ 'options' ] ) || false;
	this.type = 'radio';
	this.id = data[ 'name' ];
	this.name = data[ 'name' ];
	this.status = null;
	this.alert = false;

	if( this.data ) {
		if( typeof this.data[ 'alert' ] == 'string' ) {
			this.errMsg = {
				'UNKNOWN': this.data[ 'alert' ]
			}
		} else {
			this.errMsg = this.data[ 'alert' ];
		}
		
		if( this.data[ 'error_to' ] !== undefined ) {
			this.errPlace = this.getEl( data[ 'options' ][ 'error_to' ] ) || false;
		}
	}

	for( var i = 0; i < this.elements.length; i++ ) {
		var element = this.elements[ i ];
		this.addHandler( element, 'click', this.onElementClick, element );
	}
}
Radio.inheritsFrom( Glyph );

/* ----------------------------------------------------------------------- */

Radio.prototype.onElementClick = function( el ) {

}

Radio.prototype.onElementClick = function( el ) {
	var _self = this;
	this.selected = el;
	this.check();
	window.setTimeout( function() {
		_self.notify( 'Changed' );
	}, 0 );
}

/* ----------------------------------------------------------------------- */

Radio.prototype.findLabel = function() {
	if( !this.selected ) return false;
	var id = this.selected[ 'id' ];
	var allLabels = document.getElementsByTagName( 'LABEL' );
	for( var i = 0; i < allLabels.length; i ++ ) {
		var label = allLabels[ i ];
		if( id == label.getAttribute( 'for' ) ) {
			return label;
		}
	}
	return false;
}

/* ----------------------------------------------------------------------- */

Radio.prototype.getValue = function() {
	if( !this.selected ) return false;
	return this.selected.value;
}

/* ----------------------------------------------------------------------- */

Radio.prototype.getVisibleValue = function() {
	if( !this.selected ) return false;
	return this.findLabel().innerHTML;
}

/* ----------------------------------------------------------------------- */

Radio.prototype.setValue = function( value ) {
	if( this.selected ) {
		this.selected.checked = false;
		this.selected.removeAttribute( 'checked' );
		this.selected = false;
	}

	for( var i = 0; i < this.elements.length; i++ ) {
		var element = this.elements[ i ];

		if( element[ 'value' ] == value ) {
			if( document.createEvent ) {
				var ev = document.createEvent( 'UIEvents' );
				ev.initEvent( 'click', false, false );
				element.dispatchEvent( ev );
			} else if( document.createEventObject ) {
				var ev = document.createEventObject();
				element.fireEvent( 'onclick', ev );
			}

			element.setAttribute( 'checked', 'checked' );
			element.checked = true;
			this.selected = element;
		}
	}
	
	if( !this.selected ) {
		this.status = false;
	}
}

/* ----------------------------------------------------------------------- */

Radio.prototype.setAlert = function( flag, msg ) {
	if( this.errPlace === false ) return;
	if( !this.alert ) {
		this.alert = new Alert( false, this.errPlace );
	}

	if( flag ) {
		if( msg != undefined ) {
			this.alert.fill( msg );
		} else {
			this.alert.hide();
		}
	} else {
		var dcode = 'UNKNOWN';

		if( !this.status ) {
			dcode = ( this.getValue() ? 'INCORRECT' : 'EMPTY' );
		}

		this.alert.fill(
			_text(
				msg ||
				this.errMsg[ dcode ] ||
				this.errMsg[ 'UNKNOWN' ]
			)
		);
	}

	this.alert.set( flag );
}


/* ----------------------------------------------------------------------- */

Radio.prototype.check = function( params, ev ) {
	var os = this.status;
	if( ev ) {
		var code = ev.charCode || ev.keyCode;
		if( code == 9 ) return false;
	}
	this.setError( this.checkMethod() );
	if( !( os === this.status ) ) {
		this.notify( 'StatusChanged', this.status );
	}

	return this.status;
}

/* ----------------------------------------------------------------------- */

Radio.prototype.getName = function() {
	return this.name;
}

/* ----------------------------------------------------------------------- */

Radio.prototype.checkMethod = function() {
	return Boolean( this.getValue() );
}

/* ----------------------------------------------------------------------- */

Radio.prototype.detach = function() {
	var _self = this;
	
	for( var i = 0; i < this.elements.length; i++ ) {
		var element = this.elements[ i ];
		this.removeHandler( element, 'click', this.onElementClick, element );
	}
	this.removeAlert();
}

/* ----------------------------------------------------------------------- */

Radio.prototype.removeAlert = function() {
	if( !this.alert ) return false;
	this.alert.remove();
	this.alert = false;
	return true;
}

/* ----------------------------------------------------------------------- */

Radio.prototype.setError = function( flag, msg ) {
	if( this.setAlert ) this.setAlert( flag, msg );
	this.status = flag;
	if(
		this.alert && 
		this.alert instanceof Alert
	) {
		this.alert.set( this.status );
		if( this.status ) {
			this.alert.hide();
		} else {
			this.alert.show();
		}
	}
}

/* ----------------------------------------------------------------------- */

Radio.prototype.isValid = function() {
	return this.status;
}

/* ----------------------------------------------------------------------- */

Alert = function( input, target ) {
	this.input = input;
	Alert.superClass.apply( this, [ ( target || input ) ] );
	this.element = this.createNode( 'span', undefined, {'class': 'alert'}); //png iesizing-crop
	
	this.setClassName( 'alert-wrap', true, this.popup );
	var popup = this.createNode( 'span', this.popup, {'class': 'alert-popup'});
	var inner = this.createNode( 'span', popup, {'class': 'hint_text'});
	this.createNode( 'span', inner, {'class': 'top_border_l'}, '&nbsp;' );
	this.createNode( 'span', inner, {'class': 'top_border_r'}, '&nbsp;' );
	this.createNode( 'span', inner, {'class': 'bottom_border_r'}, '&nbsp;' );
	this.createNode( 'span', inner, {'class': 'arrow'}, '&nbsp;' );

	this.popupInner = this.createNode( 'span', inner );

	if( target && target.appendChild ) {
		target.appendChild( this.element );
	} else if( target === undefined ) {
		if(
			input &&
			input.parentNode &&
			input.parentNode.insertBefore
		) {
			input.parentNode.insertBefore( this.element, input.nextSibling );
		}
	}

	this.hide();
	this.setPosition();

	if( this.input ) {
		this.addHandler( this.input, 'mouseover', this.show, this.input );
		this.addHandler( this.input, 'mouseout',  this.hide );
		this.addHandler( this.input, 'blur',  this.hide );
	}

	this.addHandler( this.element, 'mouseover', this.show, this.element );
	this.addHandler( this.element, 'mouseout', this.hide );
	this.addHandler( this.element, 'blur', this.hide );
}
Alert.inheritsFrom( Popup );

/* ----------------------------------------------------------------------- */

Alert.prototype.hide = function() {
	this.setStyle({display: 'none'}, this.popup );
    this.flag_shown = false;
}

Alert.prototype.show = function( target, ev ) {
	this.stopPropagation( ev );
	if(
		this.status ||
		(
			target &&
			( 
				target.offsetWidth == 0 ||
				target.offsetHeight == 0
			)
			
		)
	) return false;
	
	
	this.setPosition( target );
	this.setStyle({'display': 'block'}, this.popup );
    this.flag_shown = false;

	if( window.timingAlert ) {
		var _self = this;
		setTimeout( function() { _self.hide() }, 1500 );
	}
}

/* ----------------------------------------------------------------------- */

Alert.prototype.setPosition = function( target ) {
	var offset = this.offset( target || this.input || this.element );
	if(
		offset &&
		offset[ 'top' ] &&
		offset[ 'left' ]
	) {
		this.setStyle({
			'top': offset[ 'top' ] + 'px',
			'left': offset[ 'left' ] + 'px'
		}, this.popup );
	}
}

/* ----------------------------------------------------------------------- */

Alert.prototype.remove = function() {
	if( !this.element ) return false;
	if( this.inner ) this.element.removeChild( this.inner );
	this.inner = false;
	if(
		this.element &&
		this.element.parentNode
	) {
		this.element.parentNode.removeChild( this.element );
	}
	
	if(
		this.popup &&
		this.popup.parentNode
	) {
		this.popup.parentNode.removeChild( this.popup );
	}

	if( this.input ) {
		this.removeHandler( this.input, 'mouseover', this.show, this.input );
		this.removeHandler( this.input, 'mouseout', this.hide );
		this.removeHandler( this.input, 'blur', this.hide );
	}

	this.removeHandler( this.element, 'mouseover', this.show, this.element );
	this.removeHandler( this.element, 'mouseout', this.hide );
	this.removeHandler( this.element, 'blur', this.hide );

	return true;
}

/* ----------------------------------------------------------------------- */

Alert.prototype.fill = function( msg ) {
	this.setContent( msg, this.popupInner );
}

/* ----------------------------------------------------------------------- */

Alert.prototype.set = function( flag ) {
	this.status = Boolean( flag );
	this.setClassName( 'correct', flag );
}





EUForexAlert = function( input, target ) {

	this.input = input;
    this.classname = 'false';
    this.noticeDiv = document.createElement('div');
    this.input.parentNode.parentNode.appendChild(this.noticeDiv);
	this.hide();

	if( this.input ) {

		this.addHandler( this.input, 'mouseover', this.show, this.input );


		this.addHandler( this.input, 'blur',  this.hide1 );

	}

}
EUForexAlert.inheritsFrom( Glyph );

/* ----------------------------------------------------------------------- */

EUForexAlert.prototype.show = function( target, ev ) {
    this.noticeDiv.className = '';

}

/* ----------------------------------------------------------------------- */


/* ----------------------------------------------------------------------- */

EUForexAlert.prototype.remove = function() {
	if( this.input ) {
		this.removeHandler( this.input, 'mouseover', this.show, this.input );

		this.removeHandler( this.input, 'blur', this.hide1 );
	}

    return true;
}


EUForexAlert.prototype.hide1 = function( msg ) {
    this.noticeDiv.className  = this.classname
}


EUForexAlert.prototype.hide = function( msg ) {}

EUForexAlert.prototype.fill = function( msg ) {}


EUForexAlert.prototype.set = function( flag ) {
    this.status = Boolean( flag );
    this.classname = flag ? 'true' : 'false';
}


window.setTimeout( function() {
    window.PageStatus && PageStatus.PreloadImg([
		'/static/interface/img/hint/border_left.png',
		'/static/interface/img/hint/border_right.png',
		'/static/interface/img/hint/top_border_left.png',
		'/static/interface/img/hint/top_border_right.png',
		'/static/interface/img/hint/bottom_border_right.png',
		'/static/interface/img/hint/arrow.png',
		'/static/interface/img/hint/close.png',
		'/static/interface/img/forms/correct.png',
		'/static/interface/img/forms/uncorrect.png',
		'/static/interface/img/grey-btn-r.png',
		'/static/img/ajax-loader.gif'
	]);
}, 1000 );
