//	NS : We use this here and there :
//  SH : Made this more likely to allow valid email addresses - 2008-02-20

var sEmailRegText = 'a-zA-Z0-9\x21\x23\x24\x25\x26\x27\x2a\x2b\x2d\x2f\x3d\x3f\x5e\x5f\x60\x7b\x7c\x7d';
var sEmailRegLocal = '^[' + sEmailRegText + ']+(\x2e+[' + sEmailRegText + ']+)*';

var emailReg = new RegExp(sEmailRegLocal + "@[\\w]\.+[\\w]+[\\w]$");

function activateUser(user_id,email,a){

	if(confirm("Do you want to enable user : " + email)){
		$.get("user-manage.cl",{set_status:'true',status: 'Y',user: user_id});
		
		$("#status_img_"+user_id).attr("src",base+"images/buttons/btn-rate-up-over.gif");
		$("#status_span_"+user_id).empty();
		$("#status_span_"+user_id).append("Active");
		$(a).unbind("click");
		$(a).attr("onclick", "disableUser('"+user_id+"','"+email+"',this);return false;");
	
		return false;
	} else {
		return false;
	}
	return false;
}	//	End function activateUser


function disableUser(user_id,email,a){

	if(confirm("Do you want to disable user : " + email)){
		$.get("user-manage.cl",{set_status:'true',user: user_id,status: 'N'});
			
		$("#status_img_"+user_id).attr("src",base+"images/buttons/btn-rate-down-over.gif");
		$("#status_span_"+user_id).empty();
		$("#status_span_"+user_id).append("Disabled");
		$(a).unbind("click");
		$(a).attr("onclick", "activateUser('"+user_id+"','"+email+"',this);return false;");
		
	
		return false;
		
	} else {
		return false;
	}
	return false;
}	//	End function disableUser


function loginCheck() {
	pass = jQuery.trim($('#password').val());
	user = jQuery.trim($('#username').val());

	if (user.length < 4 || pass.length < 4) {
		if (user.length < 4) {
			$('#loginerror').html('<p class="error">Please ensure your <u>E-mail Address</u> is filled in correctly</p>');
			$('#username').focus();
			$('#CForm').Shake(2);
			return false;
		} else if (pass.length < 4) {
			$('#loginerror').html('<p class="error">Please ensure your <u>Password</u> is filled in correctly</p>');
			$('#password').focus();
			$('#CForm').Shake(2);
			return false;
		}
	} else if (user.indexOf('@') > -1) {
		if (! emailReg.test(user)) {
			$('#loginerror').html('<p class="error">Please ensure your <u>Email Address</u> is typed correctly</p>');
			$('#username').focus();
			$('#CForm').Shake(2);
			return false;
		}
	}
	return true;
}	//	End function loginCheck


function loginReset() {
	$('#loginerror').html('');
//	Return focus to first non hidden field (There's no negative JQ selector
//	for hidden fields) and we may want to focus on text *or* password boxen
	$('#sitelogin input').each(function(){
		if (this.type != 'hidden') {
			this.focus();
			return false
		}
	});
	return true;
}	//		End function loginReset


function signupCheck() {
	captcha = jQuery.trim($('#signup_captcha').val());
	var emailAddy = jQuery.trim($('#signup_email').val());
	if (! emailReg.test(emailAddy)) {
		loginReset();
		$('#loginerror').html('<p class="error">Please check your <u>Email address</u> is typed correctly</p>');
		$('#signup_email').focus();
		$('#CForm').Shake(2);
		return false;
	}
	if (captcha.length < 4) {
		$('#loginerror').html('<p class="error">Please check you typed the <u>Text on the Image</u> correctly</p>');
		$('#signup_captcha').focus();
		$('#CForm').Shake(2);
		return false;
	}
}	//	End function signupCheck


function checkPasswordsMatch(field1, field2) {
	pass1 = jQuery.trim($('#'+field1).val());
	pass2 = jQuery.trim($('#'+field2).val());
	return (pass1 == pass2);
}	//	End function checkPasswordsMatch


function getFieldLength(field) {
	fieldvalue = jQuery.trim($('#'+field).val());
	return (fieldvalue.length);
}	//	End function getFieldLength


function initialPasswordCheck() {
	if (getFieldLength('new_password') == 0) {
		loginReset();
		$('#loginerror').html('<p class="error">Your <u>Password</u> cannot be empty. Please type a password</p>');
		$('#CForm').Shake(2);
		return false;
	}
	if (getFieldLength('new_password') < 4) {
		loginReset();
		$('#loginerror').html('<p class="error">Your <u>Password</u> must be at least 4 characters long. Please type a new password</p>');
		$('#CForm').Shake(2);
		$('#new_password').val('');
		return false;
	}
	if (! checkPasswordsMatch('new_password', 'confirm_new_password')) {
		loginReset();
		$('#loginerror').html('<p class="error">The <u>new and re-typed passwords</u> do not match. Please re-enter these passwords to change them</p>');
		$('#CForm').Shake(2);
		$('#new_password').val('');
		$('#confirm_new_password').val('');
		return false;
	}
	return true;
}	//	End function initialPasswordCheck


function passwordCheck() {
	if (getFieldLength('password_old') < 4) {
		loginReset();
		$('#loginerror').html('Please confirm your existing password to continue.');
		return false;
	}
	if (getFieldLength('new_password') < 4) {
		$('#loginerror').html('Your new Password must be at least 4 characters long.');
		$('#new_password').val('');
		$('#new_password').focus();
		return false;
	}
	if (! checkPasswordsMatch('new_password', 'new_password_confirm')) {
		$('#new_password_confirm').val('');
		$('#new_password').val('');
		$('#new_password').focus();
		$('#loginerror').html('The new and re-typed passwords do not match.<br />Please re-enter these passwords to change them');
		return false;
	}
//	New and existing password cannot match :
	if (checkPasswordsMatch('new_password', 'password_old')) {
		$('#new_password_confirm').val('');
		$('#new_password').val('');
		$('#new_password').focus();
		$('#loginerror').html('The existing and new passwords cannot match.<br />Please enter a different password to change it.');
		return false;
	}
}	//	End function passwordCheck
