function isUndefined(theObject) {
	if (typeof(theObject) == 'undefined') {
		return true
	} else {
		return false
	}
}

function openPopUp(popurl, width, height) {
	winpops = window.open(popurl,"","width=" + width + ",height=" + height)
}

function emptyBox(theBox) {
	if (isUndefined(theBox))
		return true;
		
	if ((theBox.value == null) || (theBox.value.length == 0))
		return true;
	else
		return false;
}

function emptyRadio(theRadioGroup) {
	if (isUndefined(theRadioGroup))
		return true;
	
	if (! isUndefined(theRadioGroup.length)) {
		for(var i = 0; i < theRadioGroup.length; i++) {
			if (theRadioGroup[i].checked)
				return false;
		}
	} else {
		if (theRadioGroup.checked)
			return false;
	}
	return true;
}

function checkTwoBoxes(theForm, boxOneName, boxTwoName, errorMessage) {
	var boxOne = theForm.elements[boxOneName];
	var boxTwo = theForm.elements[boxTwoName];
	
	if (isUndefined(boxOne) || isUndefined(boxTwo))
		return true;
	
	if (! isUndefined(boxOne.length)) {
		for(var i = 0; i < boxOne.length; i++) {
			if (emptyBox(boxOne[i]) ^ emptyBox(boxTwo[i])) {
				alert(errorMessage);
				
				if (emptyBox(boxOne[i]))
					boxOne[i].focus();
				else
					boxTwo[i].focus();
					
				return false;
			}
		}
	} else {
		if (emptyBox(boxOne) ^ emptyBox(boxTwo)) {
			alert(errorMessage);
			
			if (emptyBox(boxOne))
				boxOne.focus();
			else
				boxTwo.focus();
				
			return false;
		}
	}
	return true;
}

function countFilledBoxes(theForm, boxName) {
	var theBox = theForm.elements[boxName];
	var count = 0;
	
	if (isUndefined(theBox))
		return count;
	
	if (! isUndefined(theBox.length)) {
		for(var i = 0; i < theBox.length; i++) {
			if (! emptyBox(theBox[i]))
				count++;
		}
	} else {
		if (! emptyBox(theBox))
			count++;
	}
	
	return count;
}


function stripCharsInBag (s, bag) {
	var i;
    var returnString = "";

    for (i = 0; i < s.length; i++) {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function getRandomNum(lbound, ubound) {
	return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}

function getRandomChar(number, lower, upper, other, extra) {
	var numberChars = "23456789";
	var lowerChars = "abcdefghjkmnpqrstuvwxyz";
	var upperChars = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
	var otherChars = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";
	var charSet = extra;
	
	if (number == true)
		charSet += numberChars;
		
	if (lower == true)
		charSet += lowerChars;
		
	if (upper == true)
		charSet += upperChars;
		
	if (other == true)
		charSet += otherChars;
		
	return charSet.charAt(getRandomNum(0, charSet.length));
}

function getPassword(length, extraChars, firstNumber, firstLower, firstUpper, firstOther, latterNumber, latterLower, latterUpper, latterOther) {
	var rc;
	
	do {
		rc = "";
		if (length > 0)
			rc = rc + getRandomChar(firstNumber, firstLower, firstUpper, firstOther, extraChars);
			
		for (var idx = 1; idx < length; ++idx) {
			rc = rc + getRandomChar(latterNumber, latterLower, latterUpper, latterOther, extraChars);
		}
	} while (! checkPasswordSilent(rc))
	
	return rc;
}

function genPassword(textBox) {
	if (isUndefined(textBox))
		return;
		
	textBox.value = getPassword(8, "", true, true, true, false, true, true, true, false)
}

function checkPassword(passBox) {
	var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var numeric = "0123456789"
	var password;
	
	if (isUndefined(passBox))
		return true;
	
	password = passBox.value;
	
	if (password.length < 8) {
		alert('Passwords must be at least 8 characters long');
		passBox.focus();
		return false;
	}
	
	if ((stripCharsInBag(password, alpha) == password) || (stripCharsInBag(password, numeric) == password)) {
		alert('Passwords must contain a mixture of alpha and numeric characters');
		passBox.focus();
		return false;
	}	
		
	return true;
}

function checkPasswordSilent(password) {
	var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var numeric = "0123456789"
	
	if (password.length < 8) {
		return false;
	}
	
	if ((stripCharsInBag(password, alpha) == password) || (stripCharsInBag(password, numeric) == password)) {
		return false;
	}	
		
	return true;
}
