$( document ).ready( function()
{
	$(".form-item").append( $("<div></div>").addClass( "form-message" ) );
	//$( "#_interactemail" ).blur( function() { validate( "email", this ); } );
	$( "#_homeemail" ).blur( function() { validate( "email", this ); } );
	$( "#_authcode" ).blur( function() { validate( "numeric", this ); } );	
	$( "#_submit" ).click( submit );
});

function validate( type, object )
{
	if( type == "numeric" )
	{
		if( NumberValidator( $(object).val() ) )
		{
			$(object).removeClass("invalid").parent().find(".form-message").text("");
		} else {
			$(object).addClass( "invalid" ).parent().find( ".form-message" ).text("Invalid number.");
		}
	}
	if( type == "email" )
	{
		if( EmailValidator( $(object).val() ) )
		{
			$(object).removeClass( "invalid" ).parent().find( ".form-message" ).text("");
		} else {
			$(object).addClass( "invalid" ).parent().find( ".form-message" ).text("Invalid email address.");
		}
	}
}

function submit()
{
	//validate( "email", $( "#_interactemail" ) );
	validate( "email", $( "#_homeemail" ) );
	validate( "numeric", $( "#_authcode" ) );
	if( $("#registration-form .invalid").size() > 0 || $("#_authcode").val().length != 4 )
	{
		$.prompt('<strong>Invalid Submission</strong> <p class="normal">The values you submittd are invalid. Please make sure that:<ul><li>All fields are completed.</li><li>All email addresses are formatted properly (eg. username@domain.com).</li><li>The last four digits of your SSN only contains numbers.</li><li>The last four digits of your SSN is four digits long.</li></ul></p>', { prefix: identity, loaded: function(){ $('#flash-sponsers').hide(); }, callback: function(){ $('#flash-sponsers').show(); } });
		return;
	}
	/**
	$.ajax(
	{
		type: "POST",
		dataType: "xml",
		url: "/CTE/services/cte-services.php?cachebust=" + new Date().getTime(),
		data: { method: "PreRegister", type: "M", salt: $( "#salt" ).val(), hash: $( "#hash" ).val(), homeemail: $( "#_homeemail" ).val(), interactemail: $( "#_interactemail" ).val(), authcode: $( "#_authcode" ).val() },
		success: function( xml )
		{
			var type = $( xml ).find( "type:first" ).text();
			if( type == "failure" )
				$.prompt('<strong>' + $( xml ).find( "title:first" ).text() + '</strong> <p class="normal">' + $( xml ).find( "body:first" ).text() + '</p>', { prefix: identity, loaded: function(){ $('#flash-sponsers').hide(); }, callback: function(){ $('#flash-sponsers').show();$( "#_submit" ).removeAttr( "disabled" ); } } );
			else if( type == "success" )
				$.prompt('<strong>' + $( xml ).find( "title:first" ).text() + '</strong> <p class="normal">' + $( xml ).find( "body:first" ).text() + '</p>', { prefix: identity, loaded: function(){ $('#flash-sponsers').hide(); }, callback: function(){ window.location = '/CTE/index.php/mp.html#nosplash'; } } );
		},
		error: function( XMLHttpRequest, textStatus, errorThrown )
		{
			alert( "ERROR: [apply|submit] " + textStatus + ": " + errorThrown );
		}
	});
	**/
	$.ajax(
	{
		type: "POST",
		dataType: "json",
		url: "/ebn/registration/pre_register/",
		data: { email_address: $( "#_homeemail" ).val(), last_4_ssn: $( "#_authcode" ).val() },
		success: function( json )
		{
			var r = json;
			if( r.result == "fail" )
				$.prompt('<strong>Request Failed</strong> <p class="normal">' + r.message + '</p>', { prefix: identity, loaded: function(){ $('#flash-sponsers').hide(); }, callback: function(){ $('#flash-sponsers').show();$( "#_submit" ).removeAttr( "disabled" ); } } );
			else if( r.result == "success" )
			{
				window.location = r.data;
			}
		},
		error: function( XMLHttpRequest, textStatus, errorThrown )
		{
			alert( "ERROR: [apply|submit] " + textStatus + ": " + errorThrown );
		}
	});
	$( "#_submit" ).attr("disabled","disabled");
}

function NumberValidator( n )
{
	if( n == '' ) return false;
	var nChars = "-0123456789.";
	var isNumber = true;
	var c;
	for( var i = 0; i < n.length && isNumber == true; i++ )
	{
		c = n.charAt( i );
		if( nChars.indexOf( c ) == -1 ) isNumber = false;
	}
	return isNumber;
}

function EmailValidator( e )
{
	var emailReg = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/gi;
	var regex = new RegExp( emailReg );
	return regex.test( e );
}

