// Registration validation.  This is separate from a lightbox that handles the form
public_path = "";
var lusername = false, lpassword = false;
var email = false, username = false, password = false, password_confirm = false, agreement = false;
var submitOK = false;
$(document).ready(function () {
	$('#login').submit(function() {
		$('#errors').empty();
		$('input.logcheck').each( function() {
			var name = $(this).attr('id');
			var $vSpan = $('#v'+name);
			var len = $(this).val().length;
			window[name] = ( len > 0 );
			//alert( 'checking '+$(this).attr('name')+', status = '+window[name] );
			$vSpan.empty();
			if( len <= 0 )
				$vSpan.append('Cannot be empty');
		});
		if( lusername && lpassword ) {
			$.ajax({	// Can't use .post because we need asynch: false
				type: "POST",
				url: loginserver,
				data: "field=login&username="+$('#lusername').val()+"&password="+$('#lpassword').val(),
				dataType: "json",
				async: false,
				success: function( resp ) {
					if( resp.ok == true ) {
						if($('#action').val() == 'vote')
							$.post(public_path+'/voting/vote/', {rate: $('#rate').val(), item_id: $('#item_id').val(), member_id: resp.memberid, action: $('#action').val()});
						if($('#action').val() == 'addcomment')
							$.post(public_path+'/comments/addComment', {comment: $('#comment').val(), item_id: $('#item_id').val(), member_id: resp.memberid, action: $('#action').val()});
						if($('#action').val() == 'loginthenfollow')
							$.post(public_path+'/account/loginthenfollow/'+ft, {follow: fl, member_id: resp.memberid, action: $('#action').val()});
						if($('#action').val() == 'loginthenfavorite')
							$.post(public_path+'/project/loginthenfavorite/'+fv, {favoriteme: fv, member_id: resp.memberid, action: $('#action').val()});
						if($('#action').val() == 'addquestion')
							$.post(public_path+'/questions/ask_a_question', {question: $('#question').val(), category_id: $('#category_id').val(), member_id: resp.memberid, action: $('#action').val()});
						$('#errors').empty().append('Logging in now...').addClass('green');
						//alert( 'Setting submitOK true' );
						submitOK = true;
					} else
						$('#errors').empty().append(resp.msg).addClass('red');
				}
			 });
		}
		//alert( 'Returning '+submitOK );
		return submitOK;
	});

	$('#registration').submit(function() {
//		alert('processing registration form');
		$('#errors').empty().append('Processing registration now...').addClass('green');
		return true;
	});

	// When all input fields contain valid data, the Submit image can be changed and the input can be enabled.
	$('input.regcheck').not('input.password').blur(function() {
		var $vSpan = $('#v'+$(this).attr('name'));
		var name = $(this).attr('name');
		if( this.value != '' ) {
			if( this.value != this.lastValue ) {
				$vSpan.removeClass('red').removeClass('green').addClass('checking');
				$vSpan.removeClass('error').html('<img src="'+public_path+'/images/ajax-loader.gif" height="16" width="16" style="padding-left: 5px; vertical-align:middle;" /> checking...');
				$.post( loginserver, { field: $(this).attr('name'), value: this.value },
					function( resp ) {
						window[name] = resp.ok;
                                                
						$vSpan.empty();
						$vSpan.append(resp.msg);
						if( resp.ok == true )
							$vSpan.removeClass('red').addClass('green');
						else
							$vSpan.removeClass('green').addClass('red');
						}, 'json' );
			}
			this.lastValue = this.value;
			regSubmitOK();
		}
	});

	$('input.password').blur(function () {
		if( this.value != this.lastValue ) {
			var $vSpan = $('#v'+$(this).attr('name'));
			$vSpan.empty();
			var name = $(this).attr('name');
			var len = $(this).val().length;
			window[name] = false;
			if( !$(this).attr('alt') ) {
				if( len >= 0 && len < 4 )
					$vSpan.append( 'Too short (min 4 characters)' ).removeClass('green').addClass('red');
				else if( len > 15 )
					$vSpan.append( 'Too long (max 15 characters)' ).removeClass('green').addClass('red');
				else {
					window[name] = true;
					$vSpan.removeClass('red').removeClass('green');
				}
			} else if( len > 0 ) {
				if( $(this).val() == $('#'+$(this).attr('alt')).val() ) {
					window[name] = true;
					$vSpan.append( 'Nice job!' ).removeClass('red').addClass('green');
				} else {
					$vSpan.append( 'Passwords do not match' ).removeClass('green').addClass('red');
					this.value = this.lastValue;
				}
			}
			this.lastValue = this.value;
			regSubmitOK();
		}
	});

	$('#agreem').change(function () {
		agreement = $(this).attr('checked');
		regSubmitOK();
	});

//	$('#regSubmit').hover(function(event) {
//		createTooltip.call(this, event);
//	});
	
	$('a.tandc').click(function(){
		$('#tandc').toggle();
		this.blur();	// Kill further link processing
		return false;
	});
});

function createTooltip(event){
	$('<div class="tooltip">Please agree to the Terms & Conditions to continue.<div>').appendTo('body');
	positionTooltip(event);
}

function positionTooltip(event){
	var tPosX = event.pageX  -10;
	var tPosY = event.pageY -100;
	$('div.tooltip').css({top: tPosY, left: tPosX});
}

function regSubmitOK() {
	//alert( 'regSubmitOK: '+email+', '+username+', '+password+', '+password_confirm+', '+agreement );
	var submit = $('#regSubmit');
	if( email && username && password && password_confirm && agreement )
		submit.attr( 'src', public_path+'/images/submit.gif' ).removeAttr('disabled');
             
        else
		submit.attr( 'src', public_path+'/images/submitbtn.gif' ).attr( 'disabled', 'disabled' );
}



