var NXCContactForm = new Class({
	
	Implements: [Events],

	initialize: function( formID, sendmailURL ) {
		this.form         = $( formID );
		this.sendmailURL  = sendmailURL;

		this.prepareHTML();
		this.prepareEvents();
	},
	
	prepareHTML: function() {
		this.contactFormBlock = this.form.getParent().getParent();		
	},
	
	prepareEvents: function() {
		this.form.addEvent( 'submit', function( e ) {			
			e.stop();
			this.startAJAX();
			
			new Request.JSON( {
				url: this.sendmailURL, 
				onSuccess: function( response ) {
					response = new Hash( response );
					if( response.status == 1 ) {
						this.contactFormBlock.getElements( 'div.contactform-success' ).setStyle( 'display', 'block' );
						this.contactFormBlock.getElements( 'div.contactform-form' ).setStyle( 'display', 'none' );
					} else {
						var errorsBlock = this.contactFormBlock.getElements( 'div.contactform-errors' )[0];
						errorsBlock.setStyle( 'display', 'block' );
						var errorsList  = errorsBlock.getElements( 'ul.errors-list' )[0];
						errorsList.empty();

						new Hash( response.errors ).each( function( errorText ) {						
							new Element( 'li', {
								'html': errorText
							} ).inject( errorsList );
						} );
					}
					this.stopAJAX();
				}.bind( this )
			} ).send( this.form.toQueryString() );
		}.bind( this ) );
	},
	
	startAJAX: function() {		 
		this.contactFormBlock.getElements( 'div.contactform-ajaxloader' )[0].setStyle( 'display', 'block' );
		this.contactFormBlock.getElements( 'div.contactform-errors' )[0].setStyle( 'display', 'none' );
		this.contactFormBlock.getElements( 'div.contactform-success' )[0].setStyle( 'display', 'none' );
	},
	
	stopAJAX: function() {
		this.contactFormBlock.getElements( 'div.contactform-ajaxloader' )[0].setStyle( 'display', 'none' );
	}
} );