function validateRequiredAndPasswords(e) {
	//assume everything will pass
	var ok = true;
	
	//reset from any previous validation failures
	$('err_msg').update('');
	$$('.required').invoke('setStyle', {border:'1px solid #bbb'});
	$('password').setStyle({border:'1px solid #bbb'});

	//check all required elements first
	$$('.required').each(function(f){
		if ($F(f) == null || $F(f).blank()) {
			ok = false;
			if (f.type == 'checkbox')
				f.up('div').setStyle({border:'1px solid red'});
			else
				f.setStyle({border:'1px solid red'});
			$('err_msg').update('Fields highlighted in red must be filled in<br />');
		}
	});
	
	//then check passwords match
	if ($F('password') != $F('confirm_password')) {
		ok = false;
		$('err_msg').insert('Passwords do not match<br />');
		$('password').setStyle({border:'1px solid red'});
	}
	
	if (!ok)
		e.stop();
	else
		return true;
}

function openTermsWindow(e) {
	e.stop();
	window.open('terms_pop.php', 'terms_window', 'height=500px,width=500');
}

function hivTestSearch(e) {
	e.stop();
	var z = $F('zip');
	var r = $F('radius');
	
	if (z.blank() || r.blank() || r == '0') {
		alert('Please provide a ZIP code and search radius');
		return false;
	} else {
		window.open("hiv_search.php?zip=" + z + "&radius=" + r, 
					'test_search_window', 
					"left=20,top=30,width=970,height=600,toolbar=0,resizable=0,scrollbars=1");
	}
}

document.observe('dom:loaded', function(){	
	$$('form.validate_required_and_passwords').invoke('observe', 'submit', validateRequiredAndPasswords);
	
	if ($('open_terms'))
		$('open_terms').observe('click', openTermsWindow);
	
	if ($('submit_test_search'))
		$('submit_test_search').observe('click', hivTestSearch);
	
	var curves = {
		tl: { radius: 10 },
		tr: { radius: 10 },
		br: { radius: 10 },
		bl: { radius: 10 },
		antiAlias: true,
		autoPad: false
	}
	
	$$('.curvy').each(function(c){
		var curve = new curvyCorners(curves, c);
		curve.applyCornersToAll();
	});
});
