$(document).ready(function(){
	$('#signature').bind("keyup paste", function(e){ setCharLimit(e,maxSigLength) });
});

function submitSettings(editpage) {
	if (!editpage || editpage == 'details') {
		// check current password has been entered
		if ( ($('#newpassword1').val() || $('#newpassword2').val()) && !$('#currentpassword').val() ) {
			updateStatus('<strong>Please enter your current password to continue.</strong>');
			$('#currentpassword').focus();
		    return false;
		// check email
		} else if ( !checkEmail('#newemail') ) {
			updateStatus('<strong>Please enter a valid e-mail address</strong>');
			$('#newemail').focus();
		    return false;
		// check passwords match if entered
		} else if ( !fieldMatch('#newpassword1','#newpassword2') ) {
			updateStatus('<strong>Your new passwords do not match, please try re-typing them.</strong>');
			$('#newpassword1').focus();
		    return false;
		// password long enough
		} else if ( ($('#newpassword1').val() || $('#newpassword2').val()) && !lenCheck('#newpassword1',6) ) {
			updateStatus('<strong>Your password needs to be 6 or more characters long.</strong><br />Please type a longer password.');
			$('#newpassword1').focus();
			return false;
		}
	}
	document.activeform.submit()
	return false;
}
function submitReg() {
	// text fields
	if ( !$('#username').val() || !$('#password').val() || !$('#email').val() || !$('#name').val() ) {
		updateStatus('<strong>Please make sure all the fields are filled in</strong>');
		$('#username').focus();
		return false;
	}
	// check birthday is entered
	else if ( !$('#birthday').val() || !$('#birthmonth').val() || !$('#birthyear').val() ) {
		updateStatus('<strong>Please enter your date of birth</strong><br />You can restrict who sees your birthday after you join.');
		$('#birthday').focus();
		return false;
	}
	// check e-mail is proper format
	else if (!checkEmail('#email')) {
		updateStatus('<strong>Please enter a valid e-mail address</strong><br />You will need a working e-mail address to verify and use your account.');
		$('#email').focus();
	    return false;
	}
	// check password is long enough
	else if (!lenCheck('#password',6)) {
		updateStatus('<strong>Your password must be at least six characters long</strong>');
		$('#password').val('');
		$('#password').focus();
		return false;
	}
	document.regform.submit()
	return false;
}
function submitResend() {
	// check field filled in
	if ( !$('#email').val() ) {
		updateStatus('<strong>Please enter your e-mail address to continue</strong>');
		$('#email').focus();
		return false;
	// check e-mail is proper format
	} else if (!checkEmail('#email')) {
		updateStatus('<strong>Please enter a valid e-mail address</strong>');
		$('#email').focus();
	    return false;
	}
	document.regform.submit()
	return false;
}
function submitChangePass() {
	// check password is long enough
	if ( !lenCheck('#password',6) ) {
		updateStatus('<strong>Your password must be at least six characters long</strong>');
		$('#password').val('');
		$('#password').focus();
		return false;
	// check passwords match
	} else if ( !fieldMatch('#password','#password2') ) {
		updateStatus('<strong>Your passwords do not match</strong>');
		$('#password').focus();
		return false;
	}
	document.regform.submit()
	return false;
}
function checkEmail(emailId) {
	var re = /^[+\w-.&]+\@[\w-.&]+\.\w+/;
	return ( $(emailId).val() && !re.test($(emailId).val()) ) ? false : true;
}
function fieldMatch(field1Id,field2Id) {
	return (($(field1Id).val() || $(field2Id).val()) && $(field1Id).val() !== $(field2Id).val()) ? false : true;
}
function lenCheck(fieldId,minLen) {
	return ( $(fieldId).val().length < minLen ) ? false : true;
}