var zips = new Array( ); 
zips[0] = new Array( 'Mountain Springs Spas', '310 N First Street', 'Hamilton', 'MT', 'USA', 59840, '(406) 363-6032', '', '');
zips[1] = new Array( 'Mountain Springs Spas', '3912 Brooks Street', 'Missoula', 'MT', 'USA', 59804, '406) 251-6742', '', '');
zips[2] = new Array( 'Best Spas Of Wyoming', '2035 E. Yellowstone', 'Casper', 'WY', 'USA', 82601, '(307) 261-9916', 'Fax:  473-5878', '');
zips[3] = new Array( 'Eureka Furniture', '1026 U.s. Hwy 93 N', 'Eureka', 'MT', 'USA', 59917, '(406) 297-2114', '', '');
zips[4] = new Array( 'Frontier Spas', '1927 Hwy. 2 West', 'Havre', 'MT', 'USA', 59501, '(406) 265-1670', 'Fax:  265-3113', '');
zips[5] = new Array( 'Jack Fay Spa & Repair', 'Box 678 - 413 2nd Ave Sw', 'Ronan', 'MT', 'USA', 59864, '(406) 676-3380', 'Fax:  676-3382', '');
zips[6] = new Array( 'Mountain Springs Spas', '1950 Euclid Avenue', 'Helena', 'MT', 'USA', 59601, '(406) 442-4881', '', '');
zips[7] = new Array( 'Mountain Springs Spas', '1023 East Idaho', 'Kalispell', 'MT', 'USA', 59901, '(406) 755-5000', 'Fax:  755-5000', '');
zips[8] = new Array( 'Seattle Pool & Spa', '22309 7th Ave. South Ste. 1', 'Des Moines', 'WA', 'USA', 98198, '(206) 824-9490', 'Fax:  824-6590', '');
zips[9] = new Array( 'Super Spaco', '18101 Street Route 9', 'Snohomish', 'WA', 'USA', 98296, '(425) 485-1314', 'Fax: 360-668-5989', '');
zips[10] = new Array( 'Arrow Pool & Spa', '106 South 2nd', 'Walla Walla', 'WA', 'USA', 99362, '(509) 526-3500', '', '');
zips[11] = new Array( 'Char-el', '23456 N.e. Wh Garner Rd', 'Yacolt', 'WA', 'USA', 98675, '(360)256-4435', '', '');
zips[12] = new Array( 'Chelan', '917 East Woodin', 'Chelan', 'WA', 'USA', 98816, '(509)682-1615', '', '');
zips[13] = new Array( 'Crystal Clear Pool & Spa', '6235 N. Sunshine St.', 'Coeur D\' Alene', 'ID', 'USA', 83815, '(208)660-0441', '', '');
zips[14] = new Array( 'Davison Spas', '333 Howard', 'Driggs', 'ID', 'USA', 83422, '(208)399-7727', '', '');
zips[15] = new Array( 'Sunshine Pools', '11496 S.e. 362', 'Boring', 'OR', 'USA', 97009, '(503)668-8598', '', '');
zips[16] = new Array( 'Spas & Saunas Of Yakima', '211 S. 1st St.', 'Yakima', 'WA', 'USA', 98901, '(509)248-4378', '', '');
zips[17] = new Array( 'Mountain Springs Spas', '2132 Grand Ave.', 'Billings', 'MT', 'USA', 59102, '(406)256-1900', '', '');
zips[18] = new Array( 'Backyard &amp; Patio Pools', '1824 N. Lake Drive', 'Lexington', 'SC', 'USA', 29072, '(803)957-6876', '(803)808-0736', '');
zips[19] = new Array( 'Davison Spas', '60 S 1st E', 'Driggs', 'ID', 'USA', 38422, '(208)354-SPAS(7727)', '', '');
zips[20] = new Array( 'Quality Spas', '9103 N Division St # A', 'Spokane', 'WA', 'USA', 99218, '(509)466-7727', '(509)466-4400', 'spokanequalityspas.com');
			
		var countries = new Array();
		//	countries[i] = '0-Country|1-Name|2-Address|3-Street|4-City|5-Zip|6-Phone|7-Fax';
		countries[0] = new Array( 'Luton Swimming Pools', '86/88 Old Beford Road', 'Luton', '', 'England', 'LU2 7PD', '011-441582454647', 'Fax:011-441582611054', '');
		countries[1] = new Array( 'Spatec Norge AS', 'Besøksadresse', 'Sankt Hallvardsveg 13,3400 Lier', '', 'Norway', '', '+47 32 84 54 54', 'Mob: +47 911 61 707', 'spatec.no');
		countries[2] = new Array( 'Wellness 2000', 'Bvba Fn 2001 Oudestraat 28  ', '2820 Bonheiden', '', 'Belgium', 'BTW BE 0417 280 439', 11015551139, '', '');
			
			/*	
			countries[0] = 'Belgium|Wellness 2000|BVBA FN 2001 Oudestraat 28|2820 Bonheiden|BTW BE|0417 280 439|11015551139';
			countries[1] = 'Norway|Spabutikken Polarbad|Thoning Qwestensat 24|7044 Throndheim||114790618168|47 73911801';
			countries[2] = 'England|Luton Swimming Pools|86/88 Old Bedford Road|Luton|LU2 7PD|011-441582454647|011-441582611054';
			*/

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function addCellToRow( content, row, className ){

		var cell = document.createElement('td');
		//var text = document.createTextNode(content);
		cell.className = className;
		cell.appendChild( content );
		row.appendChild( cell );
}

function displayStore( store ){
	var div     = document.getElementById('dealers');

	

	var tmp = store.split(',');
	
	var text = '<h2 style="font-family: sans-serif; color: #728EEF; font-size: 16px; margin: 15px 0 10px 0; padding: 0;">'+tmp[0]+'</h2>';	
	text += tmp[1]+'<br />'; // Address
	text += tmp[2]+', ' +tmp[3]+ '  ' +tmp[5]+'<br />'; // City, State Zip
	text += 'Phone: '+tmp[6]+'<br />'; // Phone
	if (typeof tmp[7] != 'undefined') {
		text += 'Fax: '+tmp[7]+'<br />'; // Fax
	}
	if (typeof tmp[8] != 'undefined') {
		text += '<a href="http://'+tmp[8]+'" target="_blank">'+tmp[8]+'</a><br />'; // Fax
	}
	text += '<br />';
					
	div.innerHTML = text;				
	
}

			
function lookup() {

	
	var country = document.getElementById('country').value;
	var text = '';

	if (country == 'USA') {

		var zip = document.getElementById('zipcode').value;
		if ( (zip == '') || (zip == 'Enter Zip Code') || (zip.length != 5) ) {
			alert('You must enter a five-digit zip code to search.');
			return false;
		}
		var text = '<h2 style="font-family: sans-serif; color: #728EEF; font-size: 16px; margin: 15px 0 10px 0; padding: 0;">Dealers located near '+zip+'</h2>';



		var trip = false; // was a match found?
		for (i = 0; i < zips.length; i++) {
			if ( zips[i].substr(0,3) == zip.substr(0,3) ) {
				var tmp = zips[i].split('|');
				if (trip) { text += '<br/ >'; }
				text += '<strong>'+tmp[1]+'</strong><br />'; // Name
				text += tmp[2]+'<br />'; // Address
				text += tmp[3]+', '+tmp[4]+' '+tmp[0]+'<br />'; // City, State Zip
				text += 'Phone: '+tmp[5]+'<br />'; // Phone
				if (typeof tmp[6] != 'undefined') {
					text += 'Fax: '+tmp[6]+'<br />'; // Fax
				}
//				text += '<br />';
				trip = true;
			}
		}
		if (!trip) {
			text += '<strong>Sorry, no dealers were found near '+zip+'.</strong>';
		}


	} else {

		for (i = 0; i < countries.length; i++) {
			var pos = countries[i].indexOf('|');
			if ( countries[i].substr(0,pos) == country ) {

			}
		}
	}
	div.innerHTML = text;
}


function searchRetailers( ){

	//$ is basically document.getElementById

	var dark = false;
	var storesMatched = 0;
	
	var countryElement = $( 'country' );
    var zipElement = $( 'zipcode' );	
	
	var retailerTable = document.createElement( 'table' );	
	var retailerTableBody = document.createElement( 'tbody' );
	
	retailerTable.id = "retailerTable";
	var titleRow = document.createElement('tr');
	
	var nameTitle =  document.createElement('th');
	nameTitle.innerHTML = "Name";
	nameTitle.className = "storeName";
	titleRow.appendChild( nameTitle );
	
	/*
	var addressTitle =  document.createElement('th');
	addressTitle.innerHTML = "Address";
	addressTitle.className = "storeAddress";	
	titleRow.appendChild( addressTitle );
	*/
	
	var cityTitle =  document.createElement('th');
	cityTitle.innerHTML = "City";
	cityTitle.className = "storeCity";	
	titleRow.appendChild( cityTitle );	
	
	var zipTitle =  document.createElement('th');
	zipTitle.innerHTML = "Zip";
	zipTitle.className = "storeZip";	
	titleRow.appendChild( zipTitle );	
		

	retailerTableBody.appendChild( titleRow );
	retailerTable.cellPadding = 0;
	retailerTable.cellSpacing = 0;	

	var showCountry = false;
	var showUSA = false;		
	var showAll = false;	
	
	if( countryElement.value == "USA" )
	{
		showUSA = true;
	}
	else if( countryElement.value == "ALL" )
	{
		showAll = true;
		
	}
	else
	{
		showCountry = true;
	}
	
	
	if( showUSA || showAll )
	{
		var zipsArray = $A(zips);

		zipsArray.each( 
			function( zip ){				
				

				
				var zipLimiter = zipElement.value.length;
				if( zipLimiter > 3 )
				{
					zipLimiter = 3;
				}
				
				if( (zipElement.value.substring( 0, zipLimiter ) == zip[5].toString().substring( 0, zipLimiter )) || showAll )
				{
					var row = document.createElement('tr');
					
					if( dark )
					{
						dark = false;
					}
					else
					{
						dark = true;
						row.className = "darkRow";
					} 
					
					var link = document.createElement( 'a' );
					link.innerHTML = zip[0];
					link.href = "javascript:displayStore(\"" + zip + "\")";
					
					
					
					addCellToRow( link, row, "storeName" );
					//addCellToRow( document.createTextNode(data[2]), row, "storeAddress" );
					addCellToRow( document.createTextNode(zip[2]), row, "storeCity" );
					addCellToRow( document.createTextNode(zip[5]), row, "storeZip" );
					retailerTableBody.appendChild( row );
					storesMatched++;

					
				}
			}		
			
		);

		if( (storesMatched == zipsArray.size()) || (storesMatched == 0) )
		{
			//Eliminating old retailer body.
			retailerTableBody = document.createElement( 'tbody' );
			var row = document.createElement('tr');
			var cell = document.createElement('td');
			cell.className = "enter_zip";
			if( storesMatched > 0 )
			{
				cell.appendChild( document.createTextNode("Please enter a zip code with in the US.") );
			}
			else
			{
				cell.innerHTML = "<p>We do not have a dealer within 60 miles of your zip code.</p>" +
								 "<p>Please contact Mountain Springs Spas by<br />" +
								 "Phone 1-800-256-1994 or 406-777-2181 or<br />"
								 +" email <a href='mailto:info@mountainspringsspa.com'>info@mountainspringsspa.com</a> </p>";
			}
			row.appendChild( cell )			
			retailerTableBody.appendChild(row);
			
			
		}		
		
		


	}
	
	if( showCountry || showAll )
	{
		var countryArray = $A(countries);	
		
		countryArray.each( 
			function( country ){
		
				//var data = country.split('|');
				
				if( (country[4] == countryElement.value) || showAll )
				{
					var row = document.createElement('tr');
					
					if( dark )
					{
						dark = false;
					}
					else
					{
						dark = true;
						row.className = "darkRow";
					} 					
					
					var link = document.createElement( 'a' );
					link.innerHTML = country[0];
					link.href = "javascript:displayStore(\"" + country + "\")";
					
					
					
					addCellToRow( link, row, "storeName" );
					addCellToRow( document.createTextNode(country[2]), row, "storeCity" );					
					addCellToRow( document.createTextNode(country[5]), row, "storeZip" );
					retailerTableBody.appendChild( row );
				}
			}		
		);
		

	}
 			
	
	

	
	

			
	retailerTable.appendChild( retailerTableBody );
	var contentArea = $( "country" ).parentNode;
	
	currentTable = $( retailerTable.id );
	
	if( currentTable )
	{
	
	  contentArea.removeChild( currentTable );
      contentArea.appendChild( retailerTable );
	  //contentArea.replaceChild( retailerTable, currentTable );
	}else{
	  contentArea.appendChild( retailerTable );
	}
	
	
	
	//$('dealers').appendChild( retailerTable );		
		
	
	

}

function toggleZip( ) {
	if (document.getElementById('country').value == 'USA') {
		$('zip_display').style.display = "";
		$('zip_display').value = "";		
	} else {
		$('zip_display').style.display = "none";
		$('zip_display').value = "";
	}
}
