var dealers = new Array();
var RESULTDIV = "resultsFeWhereToBuy";

function getDealers()
{
	clearErrorMessages();
	clearElement(RESULTDIV);
	if (formComplete())
	{
		var dealersfound=false;
		var dealermatch;
		var region=getValue("Region");
		var application=getValue("Application");
		var system=getValue("System");
		var product=getValue("Product");
		for (var i=0;i<dealers.length;i++)
		{
			dealermatch=true;
			if (dealers[i].regions.indexOf(region)<0) dealermatch=false;
			if (dealers[i].applications.indexOf(application)<0) dealermatch=false;
			if (dealers[i].systems.indexOf(system)<0) dealermatch=false;
			if (dealers[i].products.indexOf(product)<0 && product!="All") dealermatch=false;
			if (dealermatch) 
			{
				showDealer(dealers[i]);
				dealersfound=true;
			}
		}
		if (!dealersfound) showNoneFound();
	}
	return false;
}

function clearErrorMessages()
{
	document.getElementById("RegionError").innerHTML="";
	document.getElementById("ApplicationError").innerHTML="";
	document.getElementById("SystemError").innerHTML="";
	document.getElementById("ProductError").innerHTML="";
}

function formComplete()
{
	var cmplt=true;
	if (document.getElementById("Region").selectedIndex==0)
	{
		document.getElementById("RegionError").innerHTML=" Please select Region";
		cmplt=false;
	}
	if (document.getElementById("Application").selectedIndex==0)
	{
		document.getElementById("ApplicationError").innerHTML=" Please select Application";
		cmplt=false;
	}
	if (document.getElementById("System").selectedIndex==0)
	{
		document.getElementById("SystemError").innerHTML=" Please select System";
		cmplt=false;
	}
	if (document.getElementById("Product").selectedIndex==0)
	{
		document.getElementById("ProductError").innerHTML=" Please select Product";
		cmplt=false;
	}
	return cmplt;
}

function showDealer(d)
{
	dealerinfo = '<table width="300" cellpadding="0" cellspacing="0" border="0">';
	dealerinfo += '<tr><td width="300">' + d.name + '</td></tr>';
	if (d.address1!="") dealerinfo += '<tr><td width="300">' + d.address1 + '</td></tr>';
	if (d.address2!="") dealerinfo += '<tr><td width="300">' + d.address2 + '</td></tr>';
	dealerinfo += '<tr><td>';
	if (d.city!="") dealerinfo += d.city;
	if (d.city!="" && d.state!="") dealerinfo += ', ';
	if (d.state!="") dealerinfo += d.state;
	if (d.zip!="") dealerinfo += ' ' + d.zip
	dealerinfo += '</td></tr>';
	if (d.country!="") dealerinfo += '<tr><td width="300">' + d.country + '</td></tr>';
	if (d.phone!="") dealerinfo += '<tr><td width="300">' + d.phone + '</td></tr>';
	if (d.fax!="") dealerinfo += '<tr><td width="300">' + d.fax + '</td></tr>';
	if (d.contact!="") dealerinfo += '<tr><td width="300">' + d.contact + '</td></tr>';
	if (d.email!="") dealerinfo += '<tr><td width="300"><a href="mailto:' + d.email + '">' + d.email + '</a></td></tr>';
	if (d.url!="") dealerinfo += '<tr><td width="300"><a href="' + d.url +'" target="_blank">' + d.url + '</a></td></tr>';
	dealerinfo += '</table><br>';
	var div = document.createElement('div');
	div.innerHTML = dealerinfo;
	document.getElementById(RESULTDIV).appendChild(div)
}

function showNoneFound()
{
	var div = document.createElement('div');
	div.innerHTML = "<b>No Dealers Found</b>";
	document.getElementById(RESULTDIV).appendChild(div)
}

function modifyProducts()
{
	var productlist=document.getElementById("Product");
	productlist.options.length=0;
	if (getValue("System")=="Fixed Systems")
	{
		productlist.options[0]=new Option("--Select One--", "", false, false);
		productlist.options[1]=new Option("", "FE-13", false, false);productlist.options[1].innerHTML="FE-13&#153;";
		productlist.options[2]=new Option("", "FE-25", false, false);productlist.options[2].innerHTML="FE-25&#153;";
		productlist.options[3]=new Option("", "FE-36", false, false);productlist.options[3].innerHTML="FE-36&#153;";
		productlist.options[4]=new Option("", "FE-241", false, false);productlist.options[4].innerHTML="FE-241&#153;";
		productlist.options[5]=new Option("", "FE-227", false, false);productlist.options[5].innerHTML="FE-227&#153;";
		productlist.options[6]=new Option("", "FM-200", false, false);productlist.options[6].innerHTML="FM-200&reg;";
		productlist.options[7]=new Option("All", "All", false, false);
	}

	if (getValue("System")=="Portable Extinguishers")
	{
		productlist.options[0]=new Option("--Select One--", "", false, false);
		productlist.options[1]=new Option("", "FE-36", false, false);productlist.options[1].innerHTML="FE-36&#153;";
	}

	if (getValue("System")=="Small Space Enclosures")
	{
		productlist.options[0]=new Option("--Select One--", "", false, false);
		productlist.options[1]=new Option("", "FE-25", false, false);productlist.options[1].innerHTML="FE-25&#153;";
		productlist.options[2]=new Option("", "FE-36", false, false);productlist.options[2].innerHTML="FE-36&#153;";
		productlist.options[3]=new Option("", "FE-227", false, false);productlist.options[3].innerHTML="FE-227&#153;";
		productlist.options[4]=new Option("", "FM-200", false, false);productlist.options[4].innerHTML="FM-200&reg;";
		productlist.options[5]=new Option("All", "All", false, false);
	}
}

function getValue(elementname)
{
	var pickedindex=document.getElementById(elementname).selectedIndex;
	return document.getElementById(elementname).options[pickedindex].value;
}

function clearElement(ename)
{
	e=document.getElementById(ename);
	if ( e.hasChildNodes() )
	{
    		while ( e.childNodes.length >= 1 )
    		{
        		e.removeChild( e.firstChild );       
    		} 
	}
}

function dealer(name,address1,address2,city,state,country,zip,phone,fax,contact,email,url,applications,systems,products,regions)
{
	this.name=name;
	this.address1=address1;
	this.address2=address2;
	this.city=city;
	this.state=state;
	this.country=country;
	this.zip=zip;
	this.phone=phone;
	this.fax=fax;
	this.contact=contact;
	this.email=email;
	this.url=url;
	this.applications=applications;
	this.systems=systems;
	this.products=products;
	this.regions=regions;
}
