$(document).ready(function() {
	$('#showLoginBut').click(function(){
		$('#showLoginBut').parent().slideUp('fast');
		$('#loginDivInner').slideDown('fast');
		return false;
	});
	$('#cancelLogin').click(function(){
		$('#showLoginBut').parent().slideDown('fast');
		$('#loginDivInner').fadeOut('fast');
		return false;
	});
});

function setUserMessage(div,messageClass,message){
	div.html("<p class='user_message "+messageClass+"'>"+message+"</p>").show('fast');
	setTimeout(function(){div.hide('fast')},3000);
}

function registerUser(){
	//get the message div
	var messageDiv = $('#registerMessage');
	messageDiv.hide();
	
	//get the username
	var email = $('input#email').val();
	if(email==""){
		setUserMessage(messageDiv,'warning','Please enter an email address');
		return false;
	}
	else if(email.length < 5 && email.indexOf('@')!=-1 && email.indexOf('.')!=-1){
		setUserMessage(messageDiv,'warning','Invalid email address');
		return false;
	}
	
	//get the first password
	var password1 = $('input#password').val();
	
	//get the second password
	var password2 = $('input#passwordConfirm').val();
	
	//perform password validations
	if((password1=="")||(password2=="")){
		setUserMessage(messageDiv,'warning','Please enter both the passwords');
		return false;
	}else if(password1!==password2){
		setUserMessage(messageDiv,'warning','The two passwords do not match');
		return false;
	}else if(password1.length < 6){
		setUserMessage(messageDiv,'warning','Password must be atleast 6 characters');
		return false;
	}
	
	//get the invitation code
	var invitationCode = $('input#invitationCode').val();
	if (invitationCode==""){
		setUserMessage(messageDiv,'warning','Please enter the invitation code');
		return false;
	}
	
	//get the chosen plan
	var chosenPlan = $('#plan').val();
	
	//get the checkbox status
	var checkStatus = $('input#termsAgreed')[0].checked;
	
	//get the declaration status and perform validation
	if (!checkStatus){
		setUserMessage(messageDiv,'warning','Please read the terms and mark the checkbox');
	}
	else{		
		var actionsDiv = $('#actionsCell');
		var origActionsHTML = actionsDiv.html();
		actionsDiv.html('<p><img src="images/loadingAnimation.gif" alt="Loading..."/></p>');
		//send the ajax request to register user details
		var queryString = 'email='+email+'&pw='+password1+'&plan='+chosenPlan+"&key="+invitationCode;
		$.getJSON('./registerUser.php', queryString, function(json){
			if(json.status==0){
				//messageDiv.html("<p class='user_message success'>The changes were saved successfully</p>").show('fast');
				document.forms.paymentForm.encrypted.value = json.encrypted;
				origActionsHTML = '<p class="user_message">Proceeding to payment..</p>';
				actionsDiv.html(origActionsHTML);
				document.forms.paymentForm.submit();
			}
			else if(json.status==1){
				setUserMessage(messageDiv,'warning','The email address is already registered!');
			}
			else if(json.status==2){
				setUserMessage(messageDiv,'warning','The invitation code provided has expired!');
			}
			else{
				setUserMessage(messageDiv,'warning','There was some problem with registration. Please try again.');
			}
			actionsDiv.html(origActionsHTML);
		});
	}
	return false;
}