FASTREP = {
	debug: false,
	key: false,
	companyID: false,
	main: 'fr-store-locator',
	form: 'fr-store-locator-form',
	prepend: 'fr-store-locator-form-prepend',
	submitValue: 'Find Stores',
	message: {
		'prepend': '',
		'novalue': 'You must provide a zip, store name, or city and state in order to find stores.',
		'badzip': 'Your zip code must be a valid 5 digit US zip code.',
		'nostore': 'No stores were found.',
		'resultstext': 'There are #COUNT# stores.'
	},
	states: ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'],
	watchKey: function() {
		if(FASTREP.key === false || FASTREP.companyID === false) {
			setTimeout(FASTREP.watchKey, 10);
		} else {
			FASTREP.render();
		}
	},
	render: function(showPrep) {
		var strform = '',
			state_cnt = 0,
			state_len = FASTREP.states.length;
		if(FASTREP.message.prepend && !(showPrep === false)) {
			strform += '<p id="' + FASTREP.prepend + '">' + FASTREP.message.prepend + '</p>';
		}
		strform += '<form method="POST" action="#" onsubmit="FASTREP.getStores(); return false;" id="' + FASTREP.form + '">';
		strform += ' <div>';
		strform += '  <label>';
		strform += '   <span>Search by Zip:</span>';
		strform += '   <input type="text" id="fr-store-locator-zip" size="5">';
		strform += '  </label>';
		strform += ' </div>';
		strform += ' <div>';
		strform += '  <label>';
		strform += '   <span>Search by Store Name</span>';
		strform += '   <input type="text" id="fr-store-locator-name" size="15">';
		strform += '  </label>';
		strform += ' </div>';
		strform += ' <div id="fr-store-locator-city-state">';
		strform += '  <label id="fr-store-locator-city-label">';
		strform += '   <span>Search by City</span>';
		strform += '   <input type="text" id="fr-store-locator-city" size="15">';
		strform += '  </label>';
		strform += '  <label id="fr-store-locator-state-label">';
		strform += '   <span>And State</span>';
		strform += '   <select id="fr-store-locator-state">';
		strform += '    <option value="">Select</option>';		
		for(state_cnt = 0; state_cnt < state_len; state_cnt++) {
			strform += '    <option value="' +  FASTREP.states[state_cnt] + '">' + FASTREP.states[state_cnt] + '</option>';
		}
		strform += '   </select>';
		strform += '  </label>';
		strform += ' </div>';
		strform += ' <div>';
		strform += '  <input type="submit" value="' + FASTREP.submitValue + '" id="fr-store-locator-submit">';
		strform += ' </div>';
		strform += '</form>';
		FASTREP.main.innerHTML = strform;
		FASTREP.form = document.getElementById(FASTREP.form);
	},
	getStores: function() {
		var zip = document.getElementById('fr-store-locator-zip').value,
			store = document.getElementById('fr-store-locator-name').value,
			city = document.getElementById('fr-store-locator-city').value,
			state = document.getElementById('fr-store-locator-state').value,
			hasValue = (zip || store || city || state),
			isValid = false,
			script = document.createElement('script'),
			qs = 'http://api.fastrep.com/?format=json&callback=FASTREP.showStores&api_key=' + FASTREP.key + '&method=fr.reports.getStoreLocator&company_id=' + FASTREP.companyID;
		if(!!hasValue) {
			if(!!zip && !zip.match(/^\d{5}$/)) {
				alert(FASTREP.message.badzip);
			} else {
				isValid = true;
			}
		} else {
			alert(FASTREP.message.novalue);
		}
		if(isValid) {
			if(!!zip) {
				qs += '&store_zip=' + zip;
			} 
			if(!!store) {
				qs += '&store_name=' + store;
			}
			if(!!city) {
				qs += '&store_city=' + city;
			}
			if(!!state) {
				qs += '&store_state_abbr=' + state;
			}
			script.src = qs;
			script.id = 'fr-store-locator-reply';
			document.getElementsByTagName('head')[0].appendChild(script);
			FASTREP.main.innerHTML = 'Searching...';
		}
	},
	showStores: function(ret) {
		var strRes = '',
			ret_cnt = 0,
			ret_cur = false;
		document.getElementsByTagName('head')[0].removeChild(document.getElementById('fr-store-locator-reply'));
		if(FASTREP.debug) {
			alert(ret.query);
		}
		if(ret.total > 0) {
			strRes += '<p>' + FASTREP.message.resultstext.replace(/#COUNT#/g, ret.total) + '</p>';
			strRes += '<ul id="fr-store-locator-results">';
			for(ret_cnt in ret.response) {
				ret_cur = ret.response[ret_cnt];
				strRes += '<li><b>' + ret_cur.store_name + '</b><br>' + ret_cur.store_address + '<br>' + ret_cur.store_city + ', ' + ret_cur.store_state_abbr + ' ' + ret_cur.store_zip + (ret_cur.store_phone != '000-000-0000' ? '<br>Phone: ' + ret_cur.store_phone : '') + '<br><a href="http://maps.google.com/maps?q=' + escape(ret_cur.store_address + ' ' + ret_cur.store_city + ', ' + ret_cur.store_state_abbr + ' ' + ret_cur.store_zip + ' (' + ret_cur.store_name + ')') + '" target="_blank">Get Directions</a></li>';
			}
			strRes += '</ul>';
			FASTREP.form = FASTREP.form.id;
			FASTREP.main.innerHTML = '';
			FASTREP.render(false);
			FASTREP.main.innerHTML = strRes + FASTREP.main.innerHTML;
		} else {
			FASTREP.form = FASTREP.form.id;
			FASTREP.main.innerHTML = '';
			FASTREP.render();
			FASTREP.main.innerHTML = FASTREP.message.nostore + FASTREP.main.innerHTML;
		}
	}
};

FASTREP.init = function() {
	document.write('<div id="' + FASTREP.main + '"></div>');
	FASTREP.main = document.getElementById(FASTREP.main);
	FASTREP.watchKey();
}();