// wait for the DOM to be loaded 
$(document).ready(function() { 
	$('#signup').ajaxForm({
		beforeSubmit: validateSignup,
		resetForm: true,
		success: function() {alert('Thanks for your support!'); tracking('<img src="http://ad.afy11.net/ad?mode=2&mgId=1000000323015&ac=0&av=0&cMode=0&rand='+getR()+'" width="1" height="1"><img src="http://ad.afy11.net/ad?mode=2&mgId=1000000344115&ac=0&av=0&cMode=0&rand='+getR()+'" width="1" height="1"><img src="http://ad.afy11.net/ad?mode=2&mgId=1000000344015&ac=0&av=0&cMode=0&rand='+getR()+'" width="1" height="1"><img src="http://ad.afy11.net/ad?mode=2&mgId=1000000416215&ac=0&av=0&cMode=0&rand='+getR()+'" width="1" height="1" />'); document.location='/';}
	});
});

function checkemail(address) {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(address)) {return true;}
	else {return false;}
}

function checkzip(z) {
	var filter=/(^\d{5}$)|(^\d{5}-\d{4}$)/;
	if (filter.test(z)) {return true;}
	else {return false;}
}

function validateSignup(formData, jqForm, options) {
	// formData is an array of objects representing the name and value of each field 
	// that will be sent to the server;  it takes the following form: 
	// 
	// [ 
	//     { name:  username, value: valueOfUsernameInput }, 
	//     { name:  password, value: valueOfPasswordInput } 
	// ] 
	// 
	// To validate, we can examine the contents of this array to see if the 
	// username and password fields have values.  If either value evaluates 
	// to false then we return false from this method. 
 
	for (var i=0; i < formData.length; i++) { 
		if (!formData[i].value) { 
			alert('Please enter values for all fields'); 
			return false; 
		} 
	}
	if (!checkemail(formData[2].value)) {
		alert('Please enter a valid email address');
		return false;
	}
	if (!checkzip(formData[4].value)) {
		alert('Please enter a valid zipcode');
		return false;
	}
	/*
	if we got here we were successful, so add in the img tag now
	*/
	var url = 'http://server1.emailcampaigns.net/autoadd/?c=6599&email='+encodeURIComponent(formData[2].value)+'&lid=8&first_name='+encodeURIComponent(formData[0].value)+'&last_name='+encodeURIComponent(formData[1].value)+'&zip='+encodeURIComponent(formData[4].value);
	jQuery('#secondsignup').html('<img src="'+url+'" width="1" height="1" alt="" />').show();	
}

function validatePetition(formData, jqForm, options) {
	// formData is an array of objects representing the name and value of each field 
	// that will be sent to the server;  it takes the following form: 
	// 
	// [ 
	//     { name:  username, value: valueOfUsernameInput }, 
	//     { name:  password, value: valueOfPasswordInput } 
	// ] 
	// 
	// To validate, we can examine the contents of this array to see if the 
	// username and password fields have values.  If either value evaluates 
	// to false then we return false from this method. 
 
	for (var i=0; i < formData.length; i++) { 
		if (!formData[i].value) { 
			alert('Please enter values for all fields'); 
			return false; 
		} 
	}
	if (!checkemail(formData[3].value)) {
		alert('Please enter a valid email address');
		return false;
	}
	if (!checkzip(formData[2].value)) {
		alert('Please enter a valid zipcode');
		return false;
	}
}

function tracking(ttags) {
	$("#trackingpixels").html('<p><span class="trtags">'+ttags+'</span></p>').show();
}

function getR() {
	return new Date().getTime();
}