var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var whitespace = " \t\n\r";

function notempty(elm)
{
	var retval=false;
	switch(elm.type)
	{
		case "select-one":
			if(elm.selectedIndex > 0)
				retval=true;
		break;
		case "text":
			if(!isEmpty(elm.value))
				retval=true;
		break;
	}
	return retval;
}

function validpasswd(elm)
{
	var retval=false;
	if(isAlphanumeric(elm.value,false))
	{
		if(elm.value.length > 3 && elm.value.length < 21)
			retval=true;
	}
	return retval;
}

function validuname(elm)
{
	var retval=false;
	var pval= stripCharsNotInBag(elm.value,lowercaseLetters+uppercaseLetters+digits+".-_");

	if(pval.length > 3 && pval.length < 21)
			retval=true;
	elm.value=pval;
	return retval;
}

function validemail(elm,mtok)
{
	var retval=false;
	if(isEmail(elm.value,mtok))
		retval=true;
	return retval;
}

function validint(elm,min,max)
{
	var retval=false;
	if(isIntegerInRange (elm.value, min, max, false))
		retval=true;
	return retval;
}

function isEmpty(s)
{
	return ((s == null) || (s.length == 0));
}

function isAlphabetic (s,emptyok)
{
	var i;
	
	if (isEmpty(s)) 
		return emptyok;
	
	// Search through string's characters one by one
	// until we find a non-alphabetic character.
	// When we do, return false; if we don't, return true.
	
	for (i = 0; i < s.length; i++)
	{   
	// Check that current character is letter.
		var c = s.charAt(i);
		if (!isLetter(c))
			return false;
	}
	// All characters are letters.
	return true;
}

function isAlphanumeric(s, emptyok)
{
	var i;
	
	if (isEmpty(s)) 
		return emptyok;
	// Search through string's characters one by one
	// until we find a non-alphanumeric character.
	// When we do, return false; if we don't, return true.
	for (i = 0; i < s.length; i++)
	{   
		// Check that current character is number or letter.
		var c = s.charAt(i);
		if (! (isLetter(c) || isDigit(c) ) )
			return false;
	}
	// All characters are numbers or letters.
	return true;
}

function isEmail(s,emptyok)
{
	if (isEmpty(s)) 
		return emptyok;
   
    // is s whitespace?
    if (isWhitespace(s))
			return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { 
			i++;
    }

    if ((i >= sLength) || (s.charAt(i) != "@"))
			return false;
    else
			i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    {
			i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != "."))
			return false;
    else
			return true;
}

function isLetter (c)
{
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{
	return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c)
{
	return (isLetter(c) || isDigit(c))
}

function isWhitespace (s)
{
	var i;
	
	// Is s empty?
	if (isEmpty(s))
		return true;
	// Search through string's characters one by one
	// until we find a non-whitespace character.
	// When we do, return false; if we don't, return true.
	for (i = 0; i < s.length; i++)
	{   
	// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1)
			return false;
	}
	// All characters are whitespace.
	return true;
}

function isInteger(s,emptyok)
{
	var i;
	if (isEmpty(s)) 
		return emptyok;

// Search through string's characters one by one
// until we find a non-numeric character.
// When we do, return false; if we don't, return true.

	for (i = 0; i < s.length; i++)
	{   
// Check that current character is number.
		var c = s.charAt(i);
		if (!isDigit(c))
			return false;
	}
// All characters are numbers.
	return true;
}

function isIntegerInRange (s, a, b, emptyok)
{
	if (isEmpty(s)) 
		return emptyok;
	// Catch non-integer strings to avoid creating a NaN below,
	// which isn't available on JavaScript 1.0 for Windows.
	if (!isInteger(s, false))
		return false;

	// Now, explicitly change the type to integer via parseInt
	// so that the comparison code below will work both on 
	// JavaScript 1.2 (which typechecks in equality comparisons)
	// and JavaScript 1.1 and before (which doesn't).
	var num = parseInt (s);
	return ((num >= a) && (num <= b));
}

// Removes all characters which appear in string bag from string s.
function stripCharsInBag (s, bag)
{
	var i;
	var returnString = "";
	
	// Search through string's characters one by one.
	// If character is not in bag, append to 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;
}

// Removes all characters which do NOT appear in string bag from string s.
function stripCharsNotInBag (s, bag)
{
	var i;
	var returnString = "";
	
	// Search through string's characters one by one.
	// If character is in bag, append to 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 getRadioButtonValue(radio)
{
	var found=null;
	for (var i = 0; i < radio.length; i++)
	{
		if(radio[i].checked)
		{
			found=radio[i].value;
			break;
		}
	}
	return found;
}

function getRadioState(radio)
{
	var found=false;
	for (var i = 0; i < radio.length; i++)
  {
		if (radio[i].checked)
		{
			found=true;
			break;
		}
	}
	return found;
}

function getBoxState(chkbox)
{
		return chkbox.checked;
}

function clrBoxes(boxid,boxcnt)
{
	for(var x=1; x <= boxcnt;x++)
	{
		curbox=boxid + x;
		document.getElementById(curbox).checked=false;
	}
	curbox=boxid + "-dmb";
	document.getElementById(curbox).checked=true;
}

function chkBoxSet(setid,boxcnt)
{
	var mt=true;
	for(var x=1; x <= boxcnt;x++)
	{
		curbox=setid + x;
		if(document.getElementById(curbox).checked)
			mt=false;
	}
	curbox=setid + "-dmb";
	if(mt)
	{
		document.getElementById(curbox).checked=true;
	}
	else
	{
		document.getElementById(curbox).checked=false;
	}
}