$(document).ready( function() {
	var frm = $(document.frmSignup);
	frm.submit(function() {
		var postdata='';
		var i=0;
		if (!validateSignupForm(frm)) {
			return false;
		}

		$('#frmSignup').hide();
		$('#divTOS').show();

		return false;
	});
});

function tosAccepted() {
		postdata = 'txtFullName='+$('#txtFullName').val();
		postdata += '&txtEmail='+$('#txtEmail').val();
		postdata += '&txtAbout='+$('#txtAbout').val();
		
		$.ajax( {
			type: 'POST',
			url: 'signup_post.php',
			data: postdata,
			success: function(msg) {

		getdata = 'name='+encodeURIComponent($('#txtFullName').val());
		getdata += '&email='+encodeURIComponent($('#txtEmail').val());
		getdata += '&about='+encodeURIComponent($('#txtAbout').val());
		
				$('#frmSignupMsg').html(msg);
				$('#frmSignupDiv').hide();
				$('#frmSignupMsg').show();

		$.ajax( {
			type: 'GET',
			url: '/register/RegisterKey.php?' + getdata,
			success: function(msg) {
				if (msg == "0")
				{
					$('#frmSignupMsg').html("A pending registration already exists in our system for this e-mail address.  Please check your e-mail for a message with the subject: \"Your AlchemyAPI Registration\".  This message contains information on how to finish the registration process and activate your API key.<br/><br/>");
				}
			}
		} );


			}
		} );

}

function validEmail(str) {
	 var emailReg = "^[\\w-_\.\+]*[\\w-_\.\+]\@[\\w]\.+[\\w]+[\\w]$";
	 var regex = new RegExp(emailReg);
	 return regex.test(str);
}

function validateSignupForm(frm) {
	var valid = true;
	var e;
	
	e = $('#txtFullName', frm);
	m = $('#txtFullNameErr');
	if (m && e && e.val() == '') {
		m.css('display','block');
		valid = false;
	} else if (m) {
		m.css('display','none');
	}
	
	b = true;
	e = $('#txtEmail', frm);
	m = $('#txtEmailErr');
	if (m && e && (e.val() == '' || !validEmail(e.val()))) {
		b = false;
		m.css('display','block');
		valid = false;
		valid = false;
	} else if (m) {
		m.css('display','none');
	}
	
	e = $('#txtConfirmEmail', frm);
	m = $('#txtConfirmEmailErr');
	if (b) {
		if (e.val() != $('#txtEmail', frm).val()) {
			m.css('display', 'block');
			valid = false;
		} else {
			m.css('display', 'none');
		}
	} else {
		m.css('display', 'block');
		valid = false;
	}
	
	return valid;
}
