/*
 * Written by: James Rossfeld
 * 4/16/2009 - first version
 *  
 */
function sendPostMessage(url, email, firstname, lastname, formname) {
	try
	{
		var ret = $.post(url,
			{ 
				'p': 'subscribe',
				'id': '2',
				'email': email,
				/*'emailconfirm': "rossfeld@gmail.com",*/
				'attribute1': firstname,
				'attribute2': lastname,
				'attribute3': formname,
				'list[2]': "signup",
				'listname[2]': "Perceptive Development Mailing List",
				'subscribe': "Subscribe to our mailing list"			
			},
			function(data) {
		        processResponse(data);		         					    	
		  	}, 
		  	"html");
	  	if (200 != ret.status)
	  	{
	  	  	alert("An error occured while trying to sign up for the list.  Please try again later.");
            //There was an error signing up for the mailing list\n\n" + ret.status + ": " + ret.statusText);
	  	}
	} catch(err)
	{
		//alert(err.message);
	}
}

function processResponse(data)
{
    if (data.match("<h3>Success</h3>"))
    {
       alert("Thank you.  You have been successfully subscribed to our mailing list."); 
    }
    else
    {
        alert("There was an error subscribing you to the mailing list. We will now redirect you to the subscription form to try again.");
        window.location = '/lists/index.php?p=subscribe&id=2';
    }
    $('#responsetext').html(data);
}

function isInputValid(value, name) {
	if ("" != value) return true;
	alert("Please enter a value for the " + name + ".");
	return false;
}

function isEmailAddressValid(addr){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	return filter.test(addr);
}

function processPostForm(email, firstname, lastname, formname)
{
	if (!isInputValid(email, "Email address")) return false;
	if (!isInputValid(firstname, "First Name")) return false;
	if (!isInputValid(lastname, "Last Name")) return false;
	if (!isEmailAddressValid(email)) {
		alert("Please enter a valid email address.");
		return false;
	}

	//sendPostMessage("/lists/index.php?p=subscribe&id=2&XDEBUG_SESSION_START=ECLIPSE_DBGP",
    sendPostMessage("/lists/index.php?p=subscribe&id=2",
					email, 
					firstname, 
					lastname,
                    formname);

	// always return false here, since we're handling the post ourselves
	return false;
}
