NOIXSearchRequest = function(){
	this.init.apply( this, arguments );
}
NOIXSearchRequest.prototype = {
	searchbox: null,

	searchbutton: null,

	domain: null,

	tld: null,
	
	init: function( searchId, domainId, buttonId )
	{
		this.searchbox = $( searchId );
		this.searchbutton = $( buttonId );
		this.domain = $( domainId );
		this.tld = $( 'domain_search_clb_hide' );
		
		this.registerEvents();
	},

	registerEvents: function()
	{
		var that = this;
		this.searchbox.addEvent( 'keydown', function( event ){
			if( event.key == 'enter' ){
				that.findDomain();
			}
		});
		this.searchbutton.addEvent( 'click', function( event ){
			that.findDomain();
			event.stop();
		});
	},

	findDomain: function()
	{
		var that = this;
		if( !this.verifyInputs() ){
			return false;
		}
		var pars = $( 'form_search_domain' ).toQueryString( );

		this.changeLoadFeedBack( '' );

		var request = new Request.JSON( {
			url: 'index.php?option=com_ajax&task=searchdomain',

			method: 'post',

			data: pars,

			onSuccess: function( response )
			{
				that.changeLoadFeedBack( 'none' );
				that.updateFeedBack( response );
			},

			onError: function( response )
			{
				that.changeLoadFeedBack( 'none' );
				Aviso( response );
			}
		}).get();
	},

	verifyInputs: function()
	{
		if( !Validation.verifyLengh( this.searchbox, 3 ) ){
			Aviso( 'Digite um domínio válido' );
			return false;
		}

		if( !Validation.verifyLengh( this.tld, 3 ) || this.tld.value == 'Selecione o TLD' ){
			Aviso( 'Informe o TLD' );
			return false;
		}
		return true;
	},

	changeLoadFeedBack: function( displayValue )
	{
		$( 'load_info' ).style.display = displayValue;

		$( 'domain_search_text' ).disabled = !$( 'domain_search_text' ).disabled;
		$( 'domain_search_button' ).disabled = !$( 'domain_search_button' ).disabled;
	},

	updateFeedBack: function( response )
	{
		if( response.status == 'true' ){
			$( 'domain_result' ).set( 'text', response.message  );
		}
		else{
			$( 'domain_result' ).set( 'text', 'domínio não disponível'  );
		}
	}
};

function validaFormBuscaDominio(){
	var form = document.getElementById('form_search_domain');
	var input = document.getElementById('domain_search_text');
	
	if( input.value == '' || input.value == 'www.meudominio' ){
		Aviso('Informe um domínio válido.');
		return false;
	}
	
	form.submit();
}
