function toggleTip(tip,action) {
  var thing = document.getElementById(tip);
  if (action == 'hide') {
    new Effect.Fade(thing);  
  } else {
    new Effect.Appear(thing);
  }
}

function password() {
  x=document.getElementById('vo_password').value;
  y=document.getElementById('vo_password1').value;
  alert(x);
  alert(y);
  if (x!=y) {
    Effect.BlindDown('passwords_do_not_match');
    return false;
  }
}

function validateField(sectionId, fieldName, type, success, fail) {
	var originalForm = document.forms['FORM_NEW_USER'];
	var form = document.forms['validate'];
    form.elements['type'].value = type;
	form.elements['value'].value = originalForm.elements[fieldName].value;
    if (success == null) {
        success = "";
	}
	if (fail == null) {
		fail = "";
	}
    form.elements['success'].value = success;
	form.elements['fail'].value = fail;
    reloadSectionWithForm(sectionId, form);
	showIfNotBlank(sectionId);
}
function checkUsername() {
	checkUsernameFunction();
}
function checkUsernameFunction() {
	validateField('username_field_validation', 'vo(username)', 'username', '/common/new_user_ajax_username_success.jsp', '/common/new_user_ajax_username_fail.jsp');
	Effect.Appear('username_field_validation', {delay:.25});
}
var shouldCheckEmail = false;
function checkEmail() {
	if (shouldCheckEmail) {
		var form = document.forms['FORM_NEW_USER'];
		var email = form.elements['vo(email)'].value;
		if (email.length == 0) {
			shouldCheckEmail = false;
			fnHide("invalid_email");
			return;
		}
		window.setTimeout('checkEmailFunction()', 500);
	}
}
function checkEmailFunction() {
	var form = document.forms['FORM_NEW_USER'];
	var email = form.elements['vo(email)'].value;
	if ((email.indexOf(".") < email.length - 1) && (email.indexOf("@") > 0)) {
		fnHide("invalid_email");
	} else {
		fnShow("invalid_email");
	}
}
function checkPasswordLength() {
	var form = document.forms['FORM_NEW_USER'];
	var password1 = form.elements['vo(password)'].value;
	if (password1.length < 5) {
		fnShow("password_length_error");
	} else {
		fnHide("password_length_error");
	}
}
function checkPasswordStrength() {
	window.setTimeout('checkPasswordStrengthFunction()', 500);
}
function checkPasswordStrengthFunction() {
	var form = document.forms['FORM_NEW_USER'];
	var password1 = form.elements['vo(password)'].value;
	if (password1 < 1) {
		fnHide("tip_password");
	}

	var strength = getPasswordStrength(password1);
	var message = "";
	if (password1.length > 5) {
		fnHide("password_length_error");
	}
	if (password1.length < 5) {
		return;
	} else if (strength > 70) {
		message = "<span class=\"pwd_strong\">Strong and hearty. Good job!</span>";
	} else if (strength > 30) {
		message = "<span class=\"pwd_ok\">Ok... but could be better.  A symbol like # or ! will make it stronger.</span>";
	} else {
		message = "<span class=\"pwd_weak\">Weak. Try adding another 2+ characters and a symbol like # or !</span>";
	}
	fnShow("tip_password");
	document.getElementById("password_strength_message").innerHTML = message;

}

function checkPasswordsMatch() {
	window.setTimeout('checkPasswordsMatchFunction()', 1000);
}

function checkPasswordsMatchFunction() {
	var form = document.forms['FORM_NEW_USER'];
	var password1 = form.elements['vo(password)'].value;
	var password2 = form.elements['vo(password1)'].value;
	if (password1.length == 0) {
		return;
	}
	if (password2.length < password1.length) {
		return;
	}
	if (password1 != password2 ) {
		fnShow("passwords_do_not_match");
	} else {
		fnHide("passwords_do_not_match");
	}

}

// Function taken from Mozilla Code:
// http://lxr.mozilla.org/seamonkey/source/security/manager/pki/resources/content/password.js
function getPasswordStrength(pw) {
    // Here is how we weigh the quality of the password
    // number of characters
    // numbers
    // non-alpha-numeric chars
    // upper and lower case characters

    //  alert("password='" + pw +"'");

    //length of the password
    var pwlength=(pw.length);
    if (pwlength>5)
        pwlength=5;


    //use of numbers in the password
    var numnumeric = pw.replace (/[0-9]/g, "");
    var numeric=(pw.length - numnumeric.length);
    if (numeric>3)
        numeric=3;

    //use of symbols in the password
    var symbols = pw.replace (/\W/g, "");
    var numsymbols=(pw.length - symbols.length);
    if (numsymbols>3)
        numsymbols=3;

    //use of uppercase in the password
    var numupper = pw.replace (/[A-Z]/g, "");
    var upper=(pw.length - numupper.length);
    if (upper>3)
        upper=3;


    var pwstrength=((pwlength*10)-20) + (numeric*10) + (numsymbols*15) + (upper*10);

    // make sure we're give a value between 0 and 100
    if ( pwstrength < 0 ) {
        pwstrength = 0;
    }

    if ( pwstrength > 100 ) {
        pwstrength = 100;
    }

    return pwstrength;
}

function toggleDataCollection(inUS) {
	if (inUS) {
		fnShow("collect_zipcode");
		fnHide("collect_country");
	}
	if (!inUS) {
		fnShow("collect_country");
		fnHide("collect_zipcode");
	}
}