// Restricted Market Zip Code Lookup engine for www.mgic.com
//
// Guideline PDFs are defined as guidelines_pdf in /static/restrictedmarkets.js

function isDefined(object) {
	return (typeof(object) == 'undefined') || (object == null) ? false : true;
} // isDefined()

function removeSpaces(string) {
	return string.split(' ').join('');
} // removeSpaces()

function getCountyList(states, filter) {
	var html = '';
	states.each(
		function(state) {
			state.counties.each(
				function(county) {
					if (filter == 'tier1') {
						if (county.restricted && (county.restricted_tier == "1")) {
							html += '<li>' + county.name + ' County, ' + state.code + '</li>';
						}
					}
					if (filter == 'tier2') {
						if (county.restricted && (county.restricted_tier == "2")) {
							html += '<li>' + county.name + ' County, ' + state.code + '</li>';
						}
					}
					else if (filter == 'unrestricted') {
						if (!county.restricted) {
							html += '<li>' + county.name + ' County, ' + state.code + '</li>';
						}
					}
					else if (filter == 'none') {
						html += '<li>' + county.name + ' County, ' + state.code + '</li>';
					}
				}
			);
		}
	);
	return html;
} // getCountyList()

function showResult(geoZipInfo) {
	var html = '<h2>Results</h2>';

//	alert('restricted: ' + geoZipInfo.counts.restricted + ', nonrestricted: ' + geoZipInfo.counts.nonrestricted);

	if ((geoZipInfo.counts.restricted == 0) && (geoZipInfo.counts.nonrestricted == 0)) {
		html += '<p>The ZIP code entered does not appear to be a valid ZIP code.&nbsp; Please check your entry.</p>';
	}
	else if ((geoZipInfo.counts.restricted > 0) && (geoZipInfo.counts.nonrestricted > 0)) {
		html += '<p class="both">Please verify the county to determine which guidelines apply.</p>';
		html += '<p><span class="restricted">Restricted</span><br />';
		if (geoZipInfo.counts.restricted_tier1 > 0) {
			//BA-569 
			//html += '<em><a href="' + guidelines_pdf['tier_one'] + '" target="_blank">Tier One Restricted Markets Guidelines apply</a></em>';
			html += '<em><a href="' + guidelines_pdf['tier_one'] + '" target="_blank">Restricted Markets Guidelines apply</a></em>';
			html += '<ul style="margin-top:.25em;">' + getCountyList(geoZipInfo.states, 'tier1') + '</ul>';
		}
		if (geoZipInfo.counts.restricted_tier2 > 0) {
			//BA-569
			//html += '<p class="tier2"><em><a href="' + guidelines_pdf['tier_two'] + '" target="_blank">Tier Two Restricted Markets Guidelines apply</a></em></p>';
			html += '<p class="tier2"><em><a href="' + guidelines_pdf['tier_two'] + '" target="_blank">Restricted Markets Guidelines apply</a></em></p>';
			html += '<ul style="margin-top:.25em;">' + getCountyList(geoZipInfo.states, 'tier2') + '</ul>';
		}
		html += '</p>';
		html += '<p><span class="not_restricted">Nonrestricted</span><br /><em><a href="' + guidelines_pdf['standard'] + '" target="_blank">Standard Guidelines apply</a></em>';
		html += '<ul style="margin-top:.25em;">' + getCountyList(geoZipInfo.states, 'unrestricted') + '</ul>';
		html += '</p>';
	}
	else {
		if (geoZipInfo.counts.restricted == 0) {
			// all non-restricted
			html += '<p><span class="not_restricted">Nonrestricted</span><br /><em><a href="' + guidelines_pdf['standard'] + '" target="_blank">Standard Guidelines apply</a></em></p>';
			html += '<ul style="margin-top:.25em;">' + getCountyList(geoZipInfo.states, 'none') + '</ul>';
		}
		else {
			// all restricted
			if ((geoZipInfo.counts.restricted_tier1 > 0) && (geoZipInfo.counts.restricted_tier2 > 0)) {
				html += '<p class="both">Please verify the county to determine which guidelines apply.</p>';
			}
			html += '<p><span class="restricted">Restricted</span><br />';
			if (geoZipInfo.counts.restricted_tier1 > 0) {
				//BA-569 
				//html += '<em><a href="' + guidelines_pdf['tier_one'] + '" target="_blank">Tier One Restricted Markets Guidelines apply</a></em>';
				html += '<em><a href="' + guidelines_pdf['tier_one'] + '" target="_blank">Restricted Markets Guidelines apply</a></em>';
				html += '<ul style="margin-top:.25em;">' + getCountyList(geoZipInfo.states, 'tier1') + '</ul>';
			}
			if (geoZipInfo.counts.restricted_tier2 > 0) {
				//BA-569 
				//html += '<p class="tier2"><em><a href="' + guidelines_pdf['tier_two'] + '" target="_blank">Tier Two Restricted Markets Guidelines apply</a></em></p>';
				html += '<p class="tier2"><em><a href="' + guidelines_pdf['tier_two'] + '" target="_blank">Restricted Markets Guidelines apply</a></em></p>';
				html += '<ul style="margin-top:.25em;">' + getCountyList(geoZipInfo.states, 'tier2') + '</ul>';
			}
			html += '</p>';
		}
	}

	$('zip_results').update(html);
} // showResult()

function showError() {
	var msg = '<h2>Results</h2>' +
              'Our system is experiencing a temporary problem.&nbsp; ' +
              'Your business is important to us.&nbsp; ' +
              'Please retry the ZIP code search.&nbsp; ' +
              'We apologize for the inconvenience.';
	$('zip_results').update(msg);
} // showError()

function checkZip() {
	var zipCode = $F('zip');
	if (zipCode.length < 5) {
		alert('Please enter a ZIP Code.');
		return;
	}
	else {
		var ajax = new Ajax.Request(
			'/geoservice/checkzip.htm',
			{
				method: 'get',
				parameters: {
					zip: zipCode
				},
				onSuccess: function(transport) {
					var response = transport.responseText || "";
//					alert("response: '" + response + "'");
					try {
						var json = response.evalJSON(true);
						showResult(json);
					}
					catch (e) {
						alert("Problem retrieving restricted market information.\n\n" + e);
						showError();
					}
				},
				onFailure: function() {
					alert('Unable to retrieve restricted market information.');
					showError();
				}
			}
		);
	}
} // checkZip()

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=580,height=580,left = 0,top = 0');");
} //popUp()

