function maybeActivateCPButton() {
	var cpButton = $('compare_prices_button');

	if ($F('wad_signup_zip') != '' && !$('gift_cert_p').checked) {
		cpButton.disabled = false;
	} else {
		cpButton.disabled = true;
	}
}

function validateCPForm() {
	var errors = new Array();
	var zip = document.getElementById('wad_signup_zip').value;
	var email = document.getElementById('wad_signup_email').value;
	var gift_cert_p = document.getElementById('gift_cert_p');

	if (!gift_cert_p) {
		var cb = null;
	} else {
		var cb = document.getElementById('gift_cert_p').value;
	}

	// validate zip code
	if (zip == '' || !zip.match(/^\d{5,}/)) {
		errors.push("<li>Please enter a valid numeric zip code.</li>");
	}
	
	// check checkbox state
	if (cb != null) {
		if (email == '') {
			errors.push("<li>Please enter your email address so we can send you your gift certificate.</li>");
		} else if (cb != null && !email.match(/^.+@.+\..+/)) {
			errors.push("<li>Your email address does not appear to be valid. Please update it and try again.</li>");
		} 
	} else if (!email.match(/^.+@.+\..+/)) {
		// no checkbox on the page means email is req'd (FLv1)
		errors.push("<li>Please enter a valid email address.</li>");
	}
	
	var wad_error_text = document.getElementById('wad_error_text');
	if (errors.length > 0) {
		var errText = 'There is a problem with your input:\n<ul>' + errors.join('\n') + '\n</ul>';
		wad_error_text.innerHTML = errText;
		wad_error_text.style.display = 'block';

		return false;
	} else {
		wad_error_text.style.display = 'none';

		return true;
	}
}
function initCPDialog() {
	var showModal = true;
	if (YAHOO.env.ua.gecko > 0 && YAHOO.env.ua.gecko < 1.9) {
		showModal = false;
	}

	// prepare the compare prices signup dialog box
	cPSignupDialog = new YAHOO.widget.Dialog('signup_dialog_bestprice', { visible: true,
			                                          fixedcenter: true,
								  close: false,
								  modal: showModal,
								  hideaftersubmit: false,
								  postmethod: 'form',
								  zIndex: 2000  } );
	cPSignupDialog.render();

	// hook up validation
	cPSignupDialog.validate = validateCPForm;
	
	// if user closes the form, send them back to previous page
	Event.observe('close_button','click',function() {
		window.location = backLink;
	});
}

YAHOO.util.Event.onDOMReady(initCPDialog);
