
//Global Vars
var mID = 0;
var questionContactID = 0;
var questionFormLoaded = false;

function insertLoadingImage() {
	var imgLoading = new Element( 'img' ).setProperties({ src: 'images/system/loading.gif', alt: 'Loading...' });
	$( 'questionFormSection' ).adopt( new Element( 'br' ), imgLoading );
}

window.addEvent('domready', function() {
	
	// Form absenden
	var requestPost = new Request.HTML({ url: 'index.php?com=sale&view=articlecontact&id=' + questionContactID + '&mid=' + mID,
		onSuccess: function( html ) {
			$( 'questionFormSection' ).set( 'text', '' );
			$( 'questionFormSection' ).adopt( new Element( 'br' ) );
			$( 'questionFormSection' ).adopt( html );
			// Submit Button
			$( 'questionFormSubmit' ).addEvent( 'click', function( event ) {
				// browser does'nt follow the link
				event.stop();
				insertLoadingImage();
				requestPost.post( $( 'questionForm' ) );
			});
		},
		onFailure: function() {
			$( 'questionFormSection' ).set( 'text', '' );
			$( 'questionFormSection' ).adopt( new Element( 'br' ) );
			$( 'questionFormSection' ).appendText( 'The request failed!' );
		}
	});
	
	// Form laden
	var requestForm = new Request.HTML({ url: 'index.php?com=sale&view=articlecontact&id=' + questionContactID + '&mid=' + mID,
		onSuccess: function( html ) {
			$( 'questionFormSection' ).set( 'text', '' );
			$( 'questionFormSection' ).adopt( new Element( 'br' ) );
			$( 'questionFormSection' ).adopt( html );
			// Submit Button
			$( 'questionFormSubmit' ).addEvent( 'click', function( event ) {
				// browser does'nt follow the link
				event.stop();
				insertLoadingImage();
				requestPost.post( $( 'questionForm' ) );
			});
		},
		onFailure: function() {
			$( 'questionFormSection' ).set( 'text', '' );
			$( 'questionFormSection' ).adopt( new Element( 'br' ) );
			$( 'questionFormSection' ).appendText( 'The request failed!' );
		}
	});
	
	// Link Form laden
	$( 'loadQuestionForm' ).addEvent( 'click', function( event ) {
		// browser does'nt follow the link
		event.stop();
		
		if( !questionFormLoaded ) {
			insertLoadingImage();
			requestForm.get();
		} else {
			$( 'questionFormSection' ).set( 'text', '' );
		}
		
		questionFormLoaded = !questionFormLoaded;
	});
});

