function addHTML(html,parent){
	var newdiv = document.createElement("div");
	newdiv.innerHTML = html;
	var container = document.getElementById(parent);
	container.appendChild(newdiv);
}

function bindStateClicks(){
	$(".statelink").bind('click',function(event){
		event.preventDefault();
		$.get(this.href,{},function(response){ 
			$('#alertStates').html(response);
			bindStateClicks();
		})	
	})
}

$(document).ready(function() {
    $("#formAlert").ajaxForm({
		success: function(responseText){
			$.fancybox({
				'content' : responseText,
				'autoDimensions' : false,
				'width' : 640,
				'height' : 480,
				'overlayColor' : '#000',
				'overlayOpacity' : 0.5,
				'onComplete' : function() {
					$.get("/_common/NewsAlertSignup.asp?action=GetStates",{},function(response){ 
						addHTML(response,"alertStates");
						//$('#alertStates').html(response);
						bindStateClicks();
					});
					$("#formAlertFinal").ajaxForm({
						success: function(response){
							$("#alertResults").html("");
							addHTML(response,"alertResults");
							$(".delstatelink").bind('click',function(event){
								event.preventDefault();
								parentDiv = this.parentNode;
								$.get(this.href,{},function(response){ 
									$(parentDiv).hide();
								})
							})
						}
					});
				}
			});
		}
	});
	$("#divAlert").show(); 
});
