$(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;
	});
});

	
glob_cChallenge = "";
glob_cResponse = "";

function signupFirstStepDone()
{
	glob_cChallenge = $('#recaptcha_challenge_field').attr("value");
	glob_cResponse = $('#recaptcha_response_field').attr("value");

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

function tosAccepted() {
		postdata = 'txtFullName='+encodeURIComponent($('#txtFullName').val());
		postdata += '&txtEmail='+encodeURIComponent($('#txtEmail').val());
		postdata += '&txtAbout='+encodeURIComponent($('#txtAbout').val());
		postdata += '&contact='+encodeURIComponent($('#txtContact:checked').val());
		postdata += '&company='+encodeURIComponent($('#txtCompany').val());
		postdata += '&phone='+encodeURIComponent($('#txtPhone').val());
		postdata += '&title='+encodeURIComponent($('#txtTitle').val());
		
		$.ajax( {
			type: 'POST',
			url: 'signup_post.php',
			data: postdata,
			success: function(msg) {
/*
	cChallenge = $('#recaptcha_challenge_field').attr("value");
	cResponse = $('#recaptcha_response_field').attr("value");
*/
		getdata = 'name='+encodeURIComponent($('#txtFullName').val());
		getdata += '&email='+encodeURIComponent($('#txtEmail').val());
		getdata += '&about='+encodeURIComponent($('#txtAbout').val());
		getdata += '&challenge='+encodeURIComponent(glob_cChallenge);
		getdata += '&response='+encodeURIComponent(glob_cResponse);
		
				$('#frmSignupMsg').html(msg);
				$('#frmSignupDiv').hide();
				$('#frmSignupMsg').show();

		$.ajax( {
			type: 'GET',
			url: '/register/RegisterKey.php?' + getdata,
			success: function(msg) {
				if (msg == "3")
				{
					$('#frmSignupMsg').html("The ReCaptcha security verification text you entered failed to validate.  Please try registering again.  If you believe you are receiving this message in error, please contact us at: support@alchemyapi.com<br/><br/>");
				}
				else if (msg == "2")
				{
					$('#frmSignupMsg').html("It appears you are attempting to create multiple API keys.  Creation of multiple duplicate API keys is prohibited by the AlchemyAPI Terms of Use.  If you believe you are receiving this message in error, please contact us at: support@alchemyapi.com<br/><br/>");
				}
				else 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) {
	return false;

	 var emailReg = "^[\\w-_\.\+]*[\\w-_\.\+]\@[\\w]\.+[\\w]+[\\w]$";
	 var regex = new RegExp(emailReg);
	 //return 
	if (true == regex.test(str))
	{
	  /* var yahooReg = ".*yahoo\.com";
	   var regex2 = new RegExp(yahooReg);
	
	   if (true == regex2.test(str))
	   {
		alert("Yahoo email is not allowed due to spam registration attacks.");
		return false;
	   }*/

	   return true;
	}

	return false;
}

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');
	}
	
	e = $('#txtPhone', frm);
	m = $('#txtPhoneErr');
	if (m && e && e.val() == '') {
		m.css('display','block');
		valid = false;
	} else if (m) {
		m.css('display','none');
	}
	
	e = $('#txtTitle', frm);
	m = $('#txtTitleErr');
	if (m && e && e.val() == '') {
		m.css('display','block');
		valid = false;
	} else if (m) {
		m.css('display','none');
	}
	
	e = $('#txtCompany', frm);
	m = $('#txtCompanyErr');
	if (m && e && e.val() == '') {
		m.css('display','block');
		valid = false;
	} else if (m) {
		m.css('display','none');
	}
	
	e = $('#txtAbout', frm);
	m = $('#txtAboutErr');
	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;
	} else if (m) {
		m.css('display','none');
	}

	m = $('#spamEmailErr');
	var yahooReg = ".*dea-ac.com";
	var regex2 = new RegExp(yahooReg);
	if (true == regex2.test(e.val()))
	{
		b = false;
		m.css('display','block');
		valid = false;
		return valid;
	} else if (m) {
		m.css('display','none');
	}
	
	m = $('#spamEmailErr');
	yahooReg = ".*mailinator.com";
	regex2 = new RegExp(yahooReg);
	if (true == regex2.test(e.val()))
	{
		b = false;
		m.css('display','block');
		valid = false;
		return valid;
	} else if (m) {
		m.css('display','none');
	}
	
	m = $('#spamEmailErr');
	yahooReg = ".*trashmail.net";
	regex2 = new RegExp(yahooReg);
	if (true == regex2.test(e.val()))
	{
		b = false;
		m.css('display','block');
		valid = false;
		return valid;
	} 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;
	}

	m = $('#txtConfirmCaptchaErr');
	if ("" == $('#recaptcha_response_field').attr("value")) {
		m.css('display', 'block');
		valid = false;
	} else {
		m.css('display', 'none');
	}

        if (valid == true)
        {
   	    signupFirstStepDone();
        }

	return valid;
}

	/*cChallenge = $('#recaptcha_challenge_field').attr("value");
	cResponse = $('#recaptcha_response_field').attr("value");

        $.ajax({
    		type: "GET",
    		url: "/api/sec_check.php?recaptcha_challenge_field=" + encodeURIComponent(cChallenge) +
			"&recaptcha_response_field=" + encodeURIComponent(cResponse),
		dataType: "text",
		success: function(data) {
			if (data == "0")
			{
				m = $('#txtConfirmCaptchaMatchErr');
				m.css('display', 'block');
				valid = false;
			}
			else signupFirstStepDone();
		},
		error: function() {
			m = $('#txtConfirmCaptchaCommErr');
			m.css('display', 'block');
			valid = false;
		}
	});*/


