function formatTime(time) {
	if (/^\d{1,2}:\d{2} [AP]M - \d{1,2}:\d{2} [AP]M$/.test(time)) {
		return time.replace(/0(\d:)/g, '$1').replace(/ ([AP]M)/g, '$1').toLowerCase();
	} else if (time == 'Closed') return 'Closed';
	else return 'N/A';
}

String.prototype.firstCap = function() {
	return this.toLowerCase().replace(/\b[a-z]/g, convrt);
	function convrt() {	return arguments[0].toUpperCase(); }
}

function formatMiles(num) {
	num = parseInt(num.replace(/0*/, ''));
	return isNaN(num) ? '' : num/100 + ' miles';
}

function writeResultRow(result, rowClass, title) {
	var rowHtml = '';
	
	rowHtml += '<tr class="'+ rowClass + '">';

	rowHtml += '<td class="dr">';
	rowHtml += result.name.firstCap();
	if (title != '' && result.name.indexOf(' ') > -1) rowHtml += ',&nbsp;' + title;
	rowHtml += '<br/>';
	rowHtml += formatMiles(result.distance);
	rowHtml += '</td>';

	rowHtml += '<td class="address">';
	if (result.address1 != '') rowHtml += result.address1.firstCap() +'<br/>';
	if (result.address2 != '') rowHtml += result.address2.firstCap() +'<br/>';
	rowHtml += result.city.firstCap() + ', ' + result.state.toUpperCase() + ' ' + result.zip + '<br/>';
	if (result.areaCode == '') rowHtml += '(Phone number not available)';
	else rowHtml += '(' + result.areaCode + ') ' + result.phone + '<br/>';
	var completeAddress = result.address2 == '' ? result.address1 : result.address1 + ', ' + result.address2;
	completeAddress = completeAddress.replace('#', '%23');
	var maplink = "http://maps.yahoo.com/maps_result?addr="+completeAddress+"&amp;csz="+result.city+"%2C"+result.state+"%20"+result.zip+"&amp;country=us";
	var	dirlink = "http://maps.yahoo.com/dd_result?newaddr="+FILTER_ADDRESS+"&amp;taddr="+completeAddress+"&amp;csz="+FILTER_CITY+"%20"+FILTER_STATE+"%20"+FILTER_ZIP_ADDRESS+"&amp;country=us&amp;tcsz="+result.city+"%20"+result.state+"%20"+result.zip+"&amp;tcountry=us";
	rowHtml += '<div class="location">';
	rowHtml += '<a href="' + maplink + '" target="_blank">view map</a> | ';
	rowHtml += '<a href="' + dirlink + '" target="_blank">get directions</a>';
	rowHtml += '</div>';
	rowHtml += '</td>';
	
	rowHtml += '<td class="hours"><table>';
	rowHtml += '<tr><td>M </td><td>' + formatTime(result.hours.mon) + '</td></tr>';
	rowHtml += '<tr><td>T </td><td>' + formatTime(result.hours.tues) + '</td></tr>';
	rowHtml += '<tr><td>W </td><td>' + formatTime(result.hours.weds) + '</td></tr>';
	rowHtml += '<tr><td>TH </td><td>' + formatTime(result.hours.thurs) + '</td></tr>';
	rowHtml += '<tr><td>F </td><td>' + formatTime(result.hours.fri) + '</td></tr>';
	rowHtml += '<tr><td>SA </td><td>' + formatTime(result.hours.sat) + '</td></tr>';
	rowHtml += '<tr><td>SU </td><td>' + formatTime(result.hours.sun) + '</td></tr>';
	rowHtml += '</table></td>';
	
	rowHtml += '<td class="details">';
	if (result.performsExams) rowHtml += '<div>Performs Exams</div>';
	if (result.dispensesMaterials) rowHtml += '<div>Dispenses Materials</div>';
	rowHtml += '<div>Languages: ';
	if (result.languages == '') rowHtml += 'N/A';
	else rowHtml += result.languages.replace(/,$/, '').firstCap();
	rowHtml += '</div>';
	if (result.DVPParticipant) rowHtml += '<div>Discount Vision Program Participant</div>';
	if (result.wholesalePrices) rowHtml += '<div>Retail benefits will be converted to wholesale-equivalent prices at this location.</div>';
	rowHtml += '</td>'

	rowHtml += '</tr>';		
	
	return rowHtml;
}

var searchLocation = '';

Event.observe(window, 'load', function() {
	document.search.FILTER_ZIP_ADDRESS.value = FILTER_ZIP_ADDRESS;
	document.search.FILTER_ADDRESS.value = FILTER_ADDRESS;
	document.search.FILTER_CITY.value = FILTER_CITY;
	document.search.FILTER_STATE.value = FILTER_STATE;
	document.search.FILTER_NAME.value = FILTER_NAME;
	
	for (var i = 0; i < document.search.FILTER_MILES.length; i++) {
		if (document.search.FILTER_MILES[i].value == FILTER_MILES) {
			document.search.FILTER_MILES.selectedIndex = i;
			break;
		}
	}
	
	searchLocation =  
	(
		(FILTER_FNAME != '' || FILTER_LNAME != '')
			? "providers named " + FILTER_FNAME + ' ' + FILTER_LNAME
			: (FILTER_NAME != '' ? FILTER_NAME : "all providers")
	) +
	" located within " + FILTER_MILES +	" miles of " +
	FILTER_ADDRESS +
	(FILTER_ADDRESS != '' && FILTER_CITY != '' ? ',' : '') +
	(FILTER_CITY != '' ? " " + FILTER_CITY : '') +
	(FILTER_CITY != '' && FILTER_STATE != '' ? ',' : '') +
	(FILTER_STATE != '' ? " " + FILTER_STATE : '') +
	" " + FILTER_ZIP_ADDRESS;
	
	$('refineMsg').update("Search for " + searchLocation);
	$('criteriaSummaryPrint').update("Customized provider directory of:<br/>" + searchLocation);
	
	if (ophthalmologists.length + opticians.length + optometrists.length == 0) {
		$('resultSummary').update('<h2>No Results</h2>Your search identified no Ophthamologists, Opticians, or Optometrists.  Please change your address or criteria or increase your search criteria.');
	} else {
		$('resultSummary').update('Your search identified ' +
			'<a href="#ophthalmologists">' + ophthalmologists.length + ' Ophthalmologists</a>, '+
			'<a href="#opticians">' + opticians.length + ' Opticians</a> and ' +
			'<a href="#optometrists">' + optometrists.length + ' Optometrists</a>.' + 
			' Please scroll down to see detailed information on each provider displayed alphabetically within each profession.');
	}
	
	var html = '';
	if (ophthalmologists.length > 0) {
		$('ophthalmologistResults').style.display = 'block';
		for (i = 0; i < ophthalmologists.length; i++)
			html += writeResultRow(ophthalmologists[i], i % 2 == 0 ? 'row2' : 'row1', 'MD');
		$('ophthalmologistDisplay').update(html);
	}
	
	html = '';
	if (opticians.length > 0) {
		$('opticianResults').style.display = 'block';
		for (i = 0; i < opticians.length; i++)
			html += writeResultRow(opticians[i], i % 2 == 0 ? 'row2' : 'row1', '');
		$('opticianDisplay').update(html);
	}
	
	html = '';
	if (optometrists.length > 0) {
		$('optometristResults').style.display = 'block';
		for (i = 0; i < optometrists.length; i++)
			html += writeResultRow(optometrists[i], i % 2 == 0 ? 'row2' : 'row1', 'OD');
		$('optometristDisplay').update(html);
	}
	
	$('waitingBlind').style.display = 'none';
});

function createPDF() {	
	$('waitingBlind').style.display = 'block';
	
	html = '<input type="hidden" name="searchLocation" value="'+searchLocation+'">';
	html += '<input type="hidden" name="documentTitle" value="Participating Provider Directory">';
	for (i = 0; i < ophthalmologists.length; i++) html += writeHiddenInput(ophthalmologists[i], 'ophthalmologist');
	for (i = 0; i < opticians.length; i++) html += writeHiddenInput(opticians[i], 'optician');
	for (i = 0; i < optometrists.length; i++) html += writeHiddenInput(optometrists[i], 'optometrist');

	document.createPDFForm.update(html);
	
	document.createPDFForm.submit();

	$('waitingBlind').style.display = 'none';
}

function writeHiddenInput(result, title) {
	rowHtml = '';
	
	rowHtml += '<input type="hidden" name="type[]" value="'+title+'">';
	rowHtml += '<input type="hidden" name="name[]" value="'+result.name+'">';
	rowHtml += '<input type="hidden" name="distance[]" value="'+result.distance+'">';
	rowHtml += '<input type="hidden" name="address1[]" value="'+result.address1+'">';
	rowHtml += '<input type="hidden" name="address2[]" value="'+result.address2+'">';
	rowHtml += '<input type="hidden" name="city[]" value="'+result.city+'">';
	rowHtml += '<input type="hidden" name="state[]" value="'+result.state+'">';
	rowHtml += '<input type="hidden" name="zip[]" value="'+result.zip+'">';
	rowHtml += '<input type="hidden" name="areaCode[]" value="'+result.areaCode+'">';
	rowHtml += '<input type="hidden" name="phone[]" value="'+result.phone+'">';
	rowHtml += '<input type="hidden" name="performsExams[]" value="'+(result.performsExams ? 'Yes' : 'No')+'">';
	rowHtml += '<input type="hidden" name="dispensesMaterials[]" value="'+(result.dispensesMaterials ? 'Yes' : 'No')+'">';
	rowHtml += '<input type="hidden" name="languages[]" value="'+result.languages+'">';
	rowHtml += '<input type="hidden" name="DVPParticipant[]" value="'+(result.DVPParticipant ? 'YES' : 'NO')+'">';
	rowHtml += '<input type="hidden" name="wholeSalePrices[]" value="'+(result.wholesalePrices ? 'YES' : 'NO')+'">';
	rowHtml += '<input type="hidden" name="mon[]" value="'+result.hours.mon+'">';
	rowHtml += '<input type="hidden" name="tues[]" value="'+result.hours.tues+'">';
	rowHtml += '<input type="hidden" name="weds[]" value="'+result.hours.weds+'">';
	rowHtml += '<input type="hidden" name="thurs[]" value="'+result.hours.thurs+'">';
	rowHtml += '<input type="hidden" name="fri[]" value="'+result.hours.fri+'">';
	rowHtml += '<input type="hidden" name="sat[]" value="'+result.hours.sat+'">';
	rowHtml += '<input type="hidden" name="sun[]" value="'+result.hours.sun+'">\n';
	
	return rowHtml;
}