function isInvalidateCommentRequest () {
	
	var errors = new Array();
	
	if($F('name') == "" )
		errors.push(this.messages.invalidName);
		
	if( $F('email') == "" || $F('email').length < 7 || 
		!$F('email').include(".")  || !$F('email').include("@") || $F('email').include(";") || $F('email').include(":") 
	  )
		errors.push(this.messages.invalidEmail);
		
	if( !$F('website').empty() && (!$F('website').startsWith("http://") && !$F('website').startsWith("https://"))  )
		errors.push(this.messages.invalidWebsite);	
		
	if( !$F('website').empty() && (!$F('website').include(".") || $F('website').length < 12) )
		errors.push(this.messages.invalidWebsite);	
		
	if($F('comment') == "" )
		errors.push(this.messages.invalidComment);	
		
	if($F('captcha') == "" || $F('captcha').length != 5 )
		errors.push(this.messages.invalidCaptcha);			
		
	errors = errors.uniq();
	if( errors.size() == 0 )
		return false;
	
	var msg = this.messages.followingErrorsOccured + '\n\n';
	errors.each( function(e){msg += e+'\n'} );
	
	alert(msg);
	return true;
};

function showCommentForm(){
	
	if($('comment_form').visible()){
		$('comment_form').fade();
		return;
	}
	
	
	offset = $('comment_form').cumulativeOffset();
	
	x = (document.viewport.getWidth() - $('comment_form').getWidth() ) / 2;
	y = (document.viewport.getHeight() - $('comment_form').getHeight() ) / 2;
	
	$('comment_form').setStyle( {top: y + "px", left: x + "px"}  );
	$('comment_form').appear();
}

function sendCommentForm(){

	if(isInvalidateCommentRequest() )
		return;
		
	var url = '/pages/writecomment/' + $F('company_id');
	
	var params = new Hash();
	params.set('name', $F('name'));
	params.set('email', $F('email'));
	params.set('company_id', $F('company_id'));
	params.set('website', $F('website'));
	params.set('comment', $F('comment'));
	params.set('captcha', $F('captcha'));
	
	$('form-loading').appear();
	$('form-errors').hide();
	
	new Ajax.Request(url, {
		method: 'get',
		parameters: params, 
		onSuccess: function(transport) {
			
			//alert('success' + transport.responseText);
			//$('comment_form').update(transport.responseText);
			
			if( transport.responseText == "invalid-captcha" )
			{
				$('form-errors').update(messages.invalidCaptcha);
				$('form-errors').appear();
				$('form-loading').hide();
				return;
			}
			
			if( transport.responseText == "invalid-email" )
			{				
				$('form-errors').update(messages.invalidEmail);
				$('form-errors').appear();
				$('form-loading').hide();
				return;
			}			
			
			if( transport.responseText == "missing-form-field" )
			{				
				$('form-errors').update("unknown error");
				$('form-errors').appear();
				$('form-loading').hide();
				return;
			}		
			
			
			if( transport.responseText == "comment-created" )
			{			
				$('comment_form').hide();
				$('comment_form').update(this.messages.commentSend);
				$('ajaxmessagebox').update(this.messages.commentSend);		
				$('ajaxmessagebox').appear();		
				new Effect.Highlight('ajaxmessagebox', { 
					startcolor: '#ffff99',
					endcolor: '#ffffff' });
				return;
			}
			
			alert('Failure, please try again or contact the support');
			
		},
		
		onFailure: function(transport) {
			
			alert('Failure, please try again or contact the support');
			//alert( transport.responseText );
			$('form-loading').fade();

		}		
	});


}