var signup_dialog = null;
$(document).ready(function(){
	signup_dialog = jQuery('<div><p>Subscribe to our newsletter</p>'+
				'<div class="textbox_wrapper" style="align-left:">'+
				'	<table width="100%" cellpadding="0" cellspacing="0">'+
				'		<tr>'+
				'			<td width="22%" height="28" align="left" valign="middle" nowrap="nowrap">&nbsp;&nbsp;Name* :</td>'+
				'			<td width="78%"><input name="newsletter_name" type="text" class="purple_textbox" id="newsletter_name" /></td>'+
				'			</tr>'+
				'		</table>'+
				'</div>'+
				'<div class="textbox_wrapper">'+
				'	<table width="100%" cellpadding="0" cellspacing="0">'+
				'		<tr>'+
				'			<td width="22%" height="28" align="left" valign="middle" nowrap="nowrap">&nbsp;&nbsp;Email* :</td>'+
				'			<td width="78%"><input name="newsletter_email" type="text" class="purple_textbox" id="newsletter_email" /></td>'+
				'			</tr>'+
				'		</table>'+
				'</div>'+
				'</div>').dialog({
	   autoOpen:false,
	   modal: true,
	   resizable: false,
	   title:'Newsletter Signup',
	   width: 325,
		buttons:{
            	"Ok": function(){
               	var name = $.trim($('#newsletter_name').val());
               	var email = $.trim($('#newsletter_email').val());
               	do_newsletter_signup(name,email);
               	},
               "Cancel": function(){
               	signup_dialog.dialog('close');
            	}
            }
	});
});
   function newsletter_dialog(){
   	$('#newsletter_name').val('');
   	$('#newsletter_email').val('');
   	signup_dialog.dialog('open');
	}
   
   function do_newsletter_signup(name,email){
          if(!name || name == ''){
               show_dialog("Please enter your name","Validation Error",{
	            	"Ok": function(){
								close_dialog();
								$('#newsletter_name').focus();
	               	}
	            });
          }else if(!validate_email(email) || email == ''){
               show_dialog("Please enter a valid email address","Validation Error",{
	            	"Ok": function(){
								close_dialog();
								$('#newsletter_email').focus();
	               	}
	            });
          }else{
            $('#newsletter_email_error').hide();
           	signup_dialog.dialog('close');
         	$.ajax({
         	   type: "get",
         	   url: "script/ajax_mailchimp.php?email="+encodeURIComponent(email)+'&name='+encodeURIComponent(name) ,
         	   dataType: "xml",
         	   error: function(msg){
         		   alert('error:'+msg.status+' : '+msg.statusText+' : '+msg.responseText,'Communications Error');
         		},
         	   success: function(xmlDocument){
                  var result = $('result',xmlDocument).text();
                  if(parseInt(result) == 1){
                     show_dialog("Thank you! Please check your email for your confirmation letter","Newsletter Signup Success!");
                  }else{
                     show_dialog("There was an error submitting your email address to the list:"+'<br /><em>'+$('errorMessage',xmlDocument).text()+'</em>',"Newsletter Error");
                  }
         	   }
            }); /* $.ajax */
          }
   } 
