var whichButton, focusElementId;

$(document).ready(function() {
	initForms();
});

function initForms() {
	// associate buttons with form elements
	whichButton = new Array();
	whichButton['txtsidesearch'] = 'ucAllChartsHolderFatCityStyle1_btnSearch';
	whichButton['UcSingleSubscriptionSignUp1_txtSignUpEmail'] = 'UcSingleSubscriptionSignUp1_btnSubmit';
	whichButton['UcSingleSubscriptionSignUp1_ddSignUpEmailType'] = 'UcSingleSubscriptionSignUp1_btnSubmit';
	$("#txtsidesearch, #UcSingleSubscriptionSignUp1_txtSignUpEmail, #UcSingleSubscriptionSignUp1_ddSignUpEmailType").focus( function() {
		focusElementId = $(this).attr("id");
	});
	
	// add onkeypress event handler
	$("form").keypress(function(e) {
		// if [enter] key
		if(e.which == 13) {
			buttonId = whichButton[focusElementId];
			// trigger click event on appropriate button
			if ( buttonId != undefined ) {
				// alert("click: " + buttonId);
				$("#" + buttonId).click();
			}
			return false;
		} else {
			return true;
		}
	});
	
	// add onclick event handler to search button
	$("#ucAllChartsHolderFatCityStyle1_btnSearch").click( function() {
		clearSearchField(); // function from autocomplete
		var dat = $("#txtsidesearch").val();
		RedirectToSearch(dat);
	});
	
	// add onclick event handler to register button
	$("#UcSingleSubscriptionSignUp1_btnSubmit").click( function() {
		$("form").submit();
	});
}

function RedirectToSearch(dat) {
	// alert("dat: " + encodeURIComponent(dat));
	window.location = '/sales/stocksearch.aspx?keyword=' + encodeURIComponent(dat);
}
