var strAjaxPathSearch = '/search/search.ajax.php';

// ============================================================================
// ============================================================================
// ============================================================================
var s_loading_html = '<div align="center"><img src="/images/ajax-loader.gif" alt="Loading data"></div>';

var s_loading_html2 = '<div align="center"><img src="/images/ajax-loader.gif" alt="Loading data">Your submission is being processed, please wait until complete before closing the browser or navigating away from this page</div>';

// ============================================================================
// ============================================================================
// ============================================================================
function a_z_click(event, element){
	var s_letter = element.id.match(/^a-z-([A-Z]|0-9)$/)[1];
	$('pnl-company-list-data').update(s_loading_html);
	new Ajax.Updater({ success: 'pnl-company-list-data', failure: 'pnl-errorzone' }, strAjaxPathSearch, {
		evalScripts: true,
		method:'get',
		parameters: {
			'do': 'company_list_data',
			'letter': s_letter || ''
		}
	});

	$$('td.a-z-some').invoke('removeClassName','active');
	$('a-z-' + s_letter).addClassName('active');
	$('pnl-company-list-data').show();
	$('pnl-company-detail').hide();
}


// ============================================================================
// ============================================================================
// ============================================================================
function filter_by_species(){
	$('pnl-product-type').update(s_loading_html);

	$('pnl-home').hide();
	$('pnl-filters').show();

	// --------------------------------------------------------------------------
	// Correctly set the visibility of the 3 step navigation sections so that the
	//  current step is visible and the others are hidden
	// --------------------------------------------------------------------------
	$('pnl-filters-info-step1').hide();
	$('pnl-filters-info-step2').show();
	$('pnl-filters-info-step3').hide();

	$('pnl-species').hide();
	$('pnl-product-type').show();
	$('pnl-company-detail').hide();

	var h_params = $('frm-filter').serialize(true);
	h_params['do'] = 'product_types_by_species';
	new Ajax.Updater(
	{ success: 'pnl-product-type', failure: 'pnl-errorzone' },
	strAjaxPathSearch, {
		evalScripts: true,
		method:'post',
		parameters: h_params
	});

}

// ============================================================================
// ============================================================================
// ============================================================================
function filter_by_product_types(){
	// Stop observing the trigger events to avoid recursion
	Event.stopObserving('btn-filter-by-product-types','click',filter_by_product_types);
	Event.stopObserving('btn-filter-by-product-types2','click',filter_by_product_types);

	var h_params       = $('frm-filter').serialize(true);
	h_params['do'] = 'company_by_species_and_product_types';
	$('pnl-company-list').update(s_loading_html);
	$('pnl-home').hide();
	$('pnl-species').hide();
	$('pnl-product-type').hide();
	$('pnl-company-list').show();

	// --------------------------------------------------------------------------
	// Correctly set the visibility of the 3 step navigation sections so that the
	//  current step is visible and the others are hidden
	// --------------------------------------------------------------------------
	$('pnl-filters-info-step1').hide();
	$('pnl-filters-info-step2').hide();
	$('pnl-filters-info-step3').show();


	// Hide all of the results action panels
	$('pnl-print-details').hide();
	$('pnl-email-details').hide();
	$('pnl-contact-companies').hide();
	// Hide the printable results
	$('pnl-company-print').hide();


	$('pnl-company-detail').hide();

	new Ajax.Updater({ success: 'pnl-company-list', failure: 'pnl-errorzone' }, strAjaxPathSearch, {
		evalScripts: true,
		method:'post',
		parameters: h_params
	});
}

// ============================================================================
// ============================================================================
// ============================================================================
function company_a_z(){
	$('pnl-company-list').update(s_loading_html);
	new Ajax.Updater({ success: 'pnl-company-list', failure: 'pnl-errorzone' }, strAjaxPathSearch, {
		evalScripts: true,
		method:'get',
		parameters: {
			'do': 'company_a_z'
		},
		onComplete: function(){
			$('a-z-0-9').addClassName('active');
		}
	});
	displayTab('pnl-company-list');
}


// ============================================================================
// ============================================================================
// ============================================================================
function species_list(b_latin){
	// The species list is loaded on arrival at the page and is sorted by common name
	// If the user requests the species list again we do not need to reload it from the database. We can just show the div again
	// the other benifit of doing this is that the user's specie selections are preserved
	if(!b_latin){
		$('btn-filter-by-latin').hide();
		$('btn-filter-by-common').show();
		$('pnl-species-latin').hide();
		$('pnl-species-common').show();
	}
	else{
		$('btn-filter-by-latin').show();
		$('btn-filter-by-common').hide();
		$('pnl-species-latin').show();
		$('pnl-species-common').hide();
	}

	// --------------------------------------------------------------------------
	// Correctly set the visibility of the 3 step navigation sections so that the
	//  current step is visible and the others are hidden
	// --------------------------------------------------------------------------
	$('pnl-filters-info-step1').show();
	$('pnl-filters-info-step2').hide();
	$('pnl-filters-info-step3').hide();

	$('pnl-home').hide();
	$('pnl-product-type').hide();
	$('pnl-company-list').hide();
	$('pnl-company-detail').hide();
	$('pnl-species').show();
	$('pnl-filters').show();
	return;
}

// ============================================================================
// ============================================================================
// ============================================================================
function company_detail(i_id){
	$('pnl-company-detail').update(s_loading_html);
	$('pnl-company-detail').show();
	new Ajax.Updater({ success: 'pnl-company-detail', failure: 'pnl-errorzone' }, strAjaxPathSearch, {
		evalScripts: true,
		method:'get',
		parameters: {
			'do': 'company_detail',
			id: i_id
		}
	});
	$('pnl-filters').hide();
	$('pnl-product-type').hide();
	$('pnl-company-list').hide();
}

/*
NEW ADDITIONS 25/02/2010
Event handlers for the 3 new buttons on the search interface
btn-print-details     -> click -> print_details_click
btn-email-details     -> click -> email_details_click
btn-contact-companies -> click -> contact_companies_click
*/

// ============================================================================
// ============================================================================
// ============================================================================
function print_details_click(){

	// Show the correct action panel
	$('pnl-print-details').show();
	$('pnl-email-details').hide();
	$('pnl-contact-companies').hide();

	// Show the correct results panel
	$('pnl-company-list').hide();
	$('pnl-company-detail').hide();
	$('pnl-company-print').show();

	var h_params       = $('frm-filter').serialize(true);
	h_params['do'] = 'company_list_print';
	$('pnl-company-print').update(s_loading_html);


	new Ajax.Updater({ success: 'pnl-company-print', failure: 'pnl-errorzone' }, strAjaxPathSearch, {
		evalScripts: true,
		method:'post',
		parameters: h_params
	});
	return;
}
// ============================================================================
// ============================================================================
// ============================================================================
function print_details_now_click(){
	window.print();
}
// ============================================================================
// ============================================================================
// ============================================================================
function print_details_close_click(){
	$('pnl-print-details').hide();

	// Show the correct results panel
	$('pnl-company-list').show();
	$('pnl-company-print').hide();
	return;
}

// ============================================================================
// ============================================================================
// ============================================================================
function email_details_click(){
	// Show the correct  action panel
	$('pnl-print-details').hide();
	$('pnl-email-details').show();
	$('pnl-contact-companies').hide();

	// Show the correct results panel
	$('pnl-company-list').show();
	$('pnl-company-detail').hide();
	$('pnl-company-print').hide();


	// Clear any previous response
	$('pnl-email-details-response').update("");
	$('pnl-email-details-response').hide();


	return;
}

// ============================================================================
// ============================================================================
// ============================================================================
function email_details_send_click(){
	$('pnl-email-details-response').update("");
	$('pnl-email-details-response').show();

	var strEmailAddress = $('email_details_user_email').getValue();
	if (!IsValidEmail(strEmailAddress)){
		debugit("The email address is not valid");
		return;
	}

	// Serialize the form, add a few params and attempt to send
	var h_params      = $('frm-filter').serialize(true);
	h_params['do']    = 'email_details_send';
	h_params['email'] = strEmailAddress;


	new Ajax.Request(
	strAjaxPathSearch,
		{
			method:'get',
			asycnhronous:true,
			parameters: h_params,
			evalscripts:true,

			onCreate:    function(transport)            {}, // End onCreate

			onException: AjaxRequestException, // End onException

			onFailure:   function(transport)            {}, // End onFailure

			onSuccess:   function(transport)            {
				$('pnl-email-details-response').update("The requested list has been emailed to you at " + strEmailAddress);
			}, // End onSuccess

			onComplete:  function(transport)            {}  // End onComplete

		} // End Options
	); // End Ajax.Request

	return;
}


// ============================================================================
// ============================================================================
// ============================================================================
function contact_companies_click(){
	// Show the correct panel
	$('pnl-contact-companies').show();
	$('pnl-print-details').hide();
	$('pnl-email-details').hide();
	// Show the correct results panel
	$('pnl-company-list').show();
	$('pnl-company-detail').hide();
	$('pnl-company-print').hide();

	// Clear any previous response
	$('pnl-contact-companies-response').update("");
	$('pnl-contact-companies-response').hide();


	return;
}




// ============================================================================
// ============================================================================
// ============================================================================
function contact_companies_send_click(){
	// clear the response panel
	//$('pnl-contact-companies-response').update("");
	//$('pnl-contact-companies-response').update("");
	$('pnl-contact-companies-response').update(s_loading_html2);
	// show the response panel
	$('pnl-contact-companies-response').show();
	var strEmailAddress = $('contact_companies_user_email').getValue();
	var strComments = $('contact_companies_comments').getValue();
	var intErrors = 0;
	if (!IsValidEmail(strEmailAddress)){
		debugit("The email address is not valid");
		intErrors++;
	}
	if (strComments === ""){
		debugit("Please include a message to the companies you are contacting");
		intErrors++;
	}
	if(intErrors>0){return;}

	// Serialize the form, add a few params and attempt to send
	var h_params      = $('frm-filter').serialize(true);
	h_params['do']    = 'contact_companies';
	h_params['email'] = strEmailAddress;
	h_params['commants'] = strComments;


	new Ajax.Request(
	strAjaxPathSearch,
		{
			method:'get',
			asycnhronous:true,
			parameters: h_params,
			evalscripts:true,

			onCreate:    function(transport)            {
			}, // End onCreate

			onException: AjaxRequestException, // End onException

			onFailure:   function(transport)            {
			}, // End onFailure

			onSuccess:   function(transport)            {
				$('pnl-contact-companies-response').update(transport.responseText);
			}, // End onSuccess

			onComplete:  function(transport)            {}  // End onComplete

		} // End Options
	); // End Ajax.Request
	return;
}

