/* $Id: sign_up1.js,v 1.6 2008/10/22 14:55:25 adamg Exp $ */

//-------------------------------------------------------------------
// isDigit(value)
//   Returns true if value is a 1-character digit
//   stolen from: http://www.mattkruse.com/javascript/validations/source.html
//-------------------------------------------------------------------


var ERROR_USERNAME = 'usernameError';
var ERROR_PASSWORD = 'passwordError';

function isDigit(num) {
	if (num.length>1){return false;}
	var string="1234567890";
	if (string.indexOf(num)!=-1){return true;}
	return false;
}
	
function displayHelp(divId, show){
	var helpDiv = document.getElementById(divId);
	if(helpDiv != null){
		if(show){
			helpDiv.style.display = 'block';
		}else{
			helpDiv.style.display = 'none';
		}
	}
}
		
function setError(pName, errorText){
	var errorSection = document.getElementById(pName);
	if(errorSection != null){
		errorSection.innerHTML = errorText;
	}
}


function clearError(pName){
	var errorSection = document.getElementById(pName);
	if(errorSection != null){
		errorSection.innerHTML = '';
	}
}

function clearErrors(){
	clearError(ERROR_USERNAME);
	clearError(ERROR_PASSWORD);
}

function displayError(rowName){
	var element = document.getElementById(rowName);
	if(rowName != null){
		element.style.display = '';
	}
}


function checkValues() {
	
	clearErrors();

	var username = document.getElementById("user_name").value;
	if (username == "") {
		setError(ERROR_USERNAME, 'You must enter a username');
		displayError("usernameErrorRow");
		return false;
	}
	
	if (username.length < 5 || username.length > 10) {
		setError(ERROR_USERNAME, "Username must be between 5 and 10 characters long.");
		displayError("usernameErrorRow");
		return false;
	}
	
	// make sure we have an appropriate number of digits
	var ctr = 0;
	for(x = 0; x < username.length; x++) {
		if (isDigit(username.charAt(x)))
			ctr++;
	}

	if (ctr > 9) {
		setError(ERROR_USERNAME, 'Your username has too many numbers in it.');
		displayError("usernameErrorRow");
		return false;
	}

	var pass1 = document.getElementById("password").value;
	var pass2 = document.getElementById("password2").value;
	if (pass1.length == 0) {
		setError(ERROR_PASSWORD, 'You must enter a password.');
		displayError("passwordErrorRow");
		return false;
	}
	if (pass1.length < 5 || pass1.length > 10) {
		setError(ERROR_PASSWORD, "Password must be between 5 and 10 characters long.");
		displayError("passwordErrorRow");
		return false;
	}
	if (pass1 != pass2) {
		setError(ERROR_PASSWORD, 'Passwords do not match.');
		displayError("passwordErrorRow");
		return false;
	}
	if (pass1 == username) {
		setError(ERROR_PASSWORD, 'Password and username must not be the same.');
		displayError("passwordErrorRow");
		return false;
	}

	return true;
}

function processAlternateusernameDialog() {
	// TODO: JM - examine interface : seems to me this would be more effective if the 3rd option, "enter another username", was a 3rd radio button instead of a seperate section.  It might be confusing to a user what will be picked if one of the radio buttons is selected AND text is put in there. 
	var newUsername = "testfg1";
	if (document.forms.altUsernameForm.elements.namedItem("altNameManual").value != "") {
		newUsername = document.forms.altUsernameForm.elements.namedItem("altNameManual").value;
	} else if (document.forms.altUsernameForm.elements.namedItem("altName1").checked) {
		newUsername = document.forms.altUsernameForm.elements.namedItem("altName1").value;
	} else if (document.forms.altUsernameForm.elements.namedItem("altName2").checked) {
		newUsername = document.forms.altUsernameForm.elements.namedItem("altName2").value;
	}
	document.forms.mainForm.elements.namedItem("user_name").value = newUsername;
	displayHelp('tooltipTaken', false);
	document.forms.mainForm.elements.namedItem("user_name").focus();
	// TODO: JM - Kind of annoying side-effect of this dialog: you can tab and click back to the back-end form and work with it if you want... doesn't allow any hacks, but it might confuse or screw up some people
	document.forms.mainForm.elements.namedItem("sub1").click();
}