/*******************************************

Owner: Nick Scott
Date Modified: Feb 2011

Description: 
	These are functions that are called from the LS Fest Site screens in order to process the ajax requests. And to create dialogs.

********************************************/

$(function() {
	$("a", "#headerEmailSignup").button();
	$('#headerEmailSignup').show();
	$("a", "#headerEmailSignup").click(function(){addEmail()})
	
	$( "#alertDialog" ).dialog({
		resizable: false,
		title:'!Alert',
		height:'auto',
		modal: true,
		autoOpen:false,
		buttons: {
			'Close':function(){ 
				$(this).dialog("option", "close", 'blind'); 
				$(this).dialog("close")
			}
		}
	});
	
	$( "#newsSignupDialog" ).dialog({
		resizable: false,
		title:'Holley LS Fest News Signup',
		height:'auto',
		modal: true,
		autoOpen:false,
		buttons: {
			"Signup": function() {
				$( this ).dialog( "close" );
			},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		}
	});
	
});

function isEmail(email)
{
	var emailFilter = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
	return emailFilter.test(email)
}

function addEmail()
{
	$('#newsSignupDialog').dialog({buttons: { 'Signup':function(){
		//signup function
		if(isEmail($('#newsSignupEmail').val()))
		{
			jQuery.ajax({
				url:"/LSFest/NewsSignup.asp"
				, dataType:"jsonp"
				, error:function(r){ 
					$('#alertText').replaceWith('<p id="alertText" style="text-align:left;"><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Error Occured '+r.responseText+'</p>');
					//$("#alertDialog").dialog("option", "show", 'blind');
					$("#alertDialog" ).dialog("option", "buttons", { 'Close':function(){ $(this).dialog("option", "show", 'blind'); $(this).dialog("close")} });
					$('#newsSignupDialog').dialog("option", "show", 'blind'); 
					$('#newsSignupDialog').dialog("close");
					$( "#alertDialog" ).dialog("open");
				}
				, success: function(r){ 
					//alert("Ajax Returns " + r.status); 
					if(r.status == "error")
					{
						$('#alertDialog').dialog( "option", "title", '!Alert');
						$('#alertText').replaceWith('<p id="alertText" style="text-align:left;"><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>'+r.message+'</p>');
						$("#alertDialog").dialog("option", "show", 'blind');
						$("#alertDialog" ).dialog("option", "buttons", { 'Close':function(){ $(this).dialog("option", "show", 'blind'); $(this).dialog("close")} });
						$('#newsSignupDialog').dialog("option", "show", 'blind'); 
						$('#newsSignupDialog').dialog("close");
						$( "#alertDialog" ).dialog("open");
					}
					$('#newsSignupDialog').dialog("option", "show", 'blind'); 
					$('#newsSignupDialog').dialog("close");
					$('#newsSignupFirstName').val('');
					$('#newsSignupLastName').val('');
					$('#newsSignupEmail').val('');
				}
				, data:{helper:"AddToEmailList", ContactFirstName:$('#newsSignupFirstName').val(), ContactLastName:$('#newsSignupLastName').val(), emailAddress:$('#newsSignupEmail').val(), keepFresh:Math.random()} 
				});
				$( this ).dialog( "close" );
		}
		else
		{
			$('#alertText').replaceWith('<p id="alertText" style="text-align:left;"><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Please enter a valid email address.</p>');
			//$("#alertDialog").dialog("option", "show", 'blind');
			$("#alertDialog" ).dialog("option", "buttons", { 'Close':function(){ $(this).dialog("option", "show", 'blind'); $(this).dialog("close")} });
			$('#newsSignupDialog').dialog("option", "show", 'blind'); 
			$('#newsSignupDialog').dialog("close");
			$( "#alertDialog" ).dialog("open");
		}
	}
	, 'Close':function(){ 
		//close function
		$(this).dialog("option", "show", 'blind'); 
		$(this).dialog("close");
		$('#newsSignupName').val('');
		$('#newsSignupEmail').val('');
	} }});
	$('#newsSignupDialog').dialog("open");
	$('#newContactFirstName').focus();	
}


