var websiteLinks;
var loadDisp = new Element('div', {
	"styles": {
		'text-align':'center',
		'background':'url(/images/ajax-loader-lg.gif) no-repeat center bottom',
		'margin':'10px auto',
		'width':'220px',
		'height':'35px',
		'color':'#999'
	}
});
loadDisp.set("text", "Please Wait...");

window.addEvent('domready', function(){
     var companyABC = $$('#browse_company span a');
	var contactABC = $$('#browse_contact span a');
	var searchForm = $('affFrm');
	var searchButton = $('searchSubmit');
	var getAffiliates = new Request.HTML({
		method: 'get',
		url: "/views/xhrGetAffiliates.cfm",
		update: $('aff'),
		data: searchForm,
		onRequest: function(){
			$('aff').empty();
			loadDisp.injectInside('aff');
		},
		onComplete: function(){
			// loadDisp needs to be recreated at the end of a request
			loadDisp = new Element('div', {"styles": {'text-align':'center','background':'url(/images/ajax-loader-lg.gif) no-repeat center bottom','margin':'10px auto','width':'220px','height':'35px','color':'#999'}});
			loadDisp.set("text", "Please Wait...");
			websiteLinks = $$('#aff table td a.newwindow');
          	websiteLinks.each(function(item, index){
          	     item.addEvent("click",  function(event){
          	          event = new Event(event);
          	          event.stop();
          	          window.open(this.href);
          	     });
          	});
		}
	});
	
	searchForm.addEvent('submit', function(event) {
	     // Stop the form from submitting
	     event = new Event(event);
	     event.stop();
	});
	companyABC.each(function(item, index){
		item.addEvent("click", function(event){
			// Set the hidden forms fields for the request
			$('company').setProperty("value", this.get('text'));
			// NULL the opposite fields
			$('contact').setProperty("value", "");
			// Show the headings (only if they are already hidden which is when the page first loads)
			if ($('table_headings').hasClass("nodisplay")) {
				$('table_headings').removeClass("nodisplay");
			}
			getAffiliates.send();
		});
	});
	contactABC.each(function(item, index){
		item.addEvent("click", function(event){
			// Set the hidden forms fields for the request
			$('contact').setProperty("value", this.get('text'));
			// NULL the opposite fields
			$('company').setProperty("value", "");
			// Show the headings (only if they are already hidden which is when the page first loads)
			if ($('table_headings').hasClass("nodisplay")) {
				$('table_headings').removeClass("nodisplay");
			}
			getAffiliates.send();
		});
	});
	searchButton.addEvent("click", function(){
		// First NULL the hidden fields
		$('company').setProperty("value", "");
		$('contact').setProperty("value", "");
		// Show the headings (only if they are already hidden which is when the page first loads)
		if ($('table_headings').hasClass("nodisplay")) {
			$('table_headings').removeClass("nodisplay");
		}
		getAffiliates.send();
	});
});

