// JavaScript Document
$(document).ready(function() {
	$('#lw_login_button').click(function() {
		if($('#email_login').val() == '') {
			alert('Please input email');
		}
		else if($('#password_login').val() == '') {
			alert('Please input password');
		}
		else {
			$('#lw_login_form').submit();
		}
	}).css({cursor: 'pointer'});
	
	$('#password_login').keypress(function(e) {
		if(e.keyCode == 13) {
			if($('#email_login').val() == '') {
				alert('Please input email');
			}
			else if($('#password_login').val() == '') {
				alert('Please input password');
			}
			else {
				$('#lw_login_form').submit();
			}
		}
	});
	
	$('.delete_item').click(function() {
		var answer = confirm("Are you sure you want to delete selected item?");
		if (answer) {
			window.location.href = $(this).attr('href');
		}
		return false;
	});

	if($('#event_date').length > 0) {
		$('#event_date').datepicker({
			changeMonth: true,
			changeYear: true,
			showOn: 'button',
			buttonImage: '../ui-lightness/images/calendar.gif',
			buttonImageOnly: true,
			autoSize: true
		});
	}

	$('#pass1').val('').keyup(function(){	
		
		var pass1 = $('#pass1').val();
		var pass2 = $('#pass2').val();
		var user = $('#email').val();
		var pwsL10n = {
			empty: "&nbsp;",
			short: "Very weak",
			bad: "Weak",
			good: "Medium",
			strong: "Strong"
	}
	
	$('#pass-strength-result').removeClass('short bad good strong');
	
	if ( ! pass1 ) {
			 $('#pass-strength-result').html( pwsL10n.empty );
			 return;
	}
	
	var strength = passwordStrength(pass1, user, pass2);
	
	if ( 2 == strength )
		$('#pass-strength-result').addClass('bad').html( pwsL10n.bad );
	else if ( 3 == strength )
		$('#pass-strength-result').addClass('good').html( pwsL10n.good );
	else if ( 4 == strength )
		$('#pass-strength-result').addClass('strong').html( pwsL10n.strong );
	else
		// this catches 'Too short' and the off chance anything else comes along
		$('#pass-strength-result').addClass('short').html( pwsL10n.short );
	});
});

function passwordStrength(password1, username, password2) {
	var shortPass = 1, badPass = 2, goodPass = 3, strongPass = 4, mismatch = 5, symbolSize = 0, natLog, score;

	// password 1 != password 2
	if ( (password1 != password2) && password2.length > 0)
		return mismatch

	//password < 4
	if ( password1.length < 4 )
		return shortPass

	//password1 == username
	if ( password1.toLowerCase() == username.toLowerCase() )
		return badPass;

	if ( password1.match(/[0-9]/) )
		symbolSize +=10;
	if ( password1.match(/[a-z]/) )
		symbolSize +=26;
	if ( password1.match(/[A-Z]/) )
		symbolSize +=26;
	if ( password1.match(/[^a-zA-Z0-9]/) )
		symbolSize +=31;

	natLog = Math.log( Math.pow(symbolSize, password1.length) );
	score = natLog / Math.LN2;

	if (score < 40 )
		return badPass

	if (score < 56 )
		return goodPass

    return strongPass;
}

function redirectToParentCategoryIndex() {
	window.location.href = '?goto=parentCategory';
}

function redirectToCategoryIndex() {
	window.location.href = '?goto=category';
}

function redirectToItemIndex() {
	window.location.href = '?goto=item';
}

function redirectToEventIndex() {
	window.location.href = '?goto=event';
}

function redirectToProfileIndex(UID) {
	window.location.href = 'profile.php?UID=' + UID;
}

function redirectToAffiliateIndex() {
	window.location.href = '?goto=affiliate';
}

function redirectToMemberIndex() {
	window.location.href = '?goto=member';
}

