// Form Validation (ck20010209)

// whitespace characters
var whitespace = " \t\n\r";

function stripWhitespace (s)
{
        var i = 0;
        var newString;
        // strip leading whitespace
        while ((i < s.length) && charInString (s.charAt(i), whitespace))
                i++;
        s = s.substring (i, s.length);
        // strip tailing whitespace
        i = s.length - 1;
        while ((i > 0) && charInString (s.charAt(i), whitespace))
                i--;
        s = s.substr (0, (i + 1));
        return s;
}

function isChar (s)
{
	var i;
	s = stripWhitespace(s);
	for (i = 0; i < s.length; i++)
	{
		// Do we have a Char?
		var c = s.charAt(i);
		if (!( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )) return false;
	}
	return true;
}

function isEmpty (s)
{
	if ( (s != null) || (s.length != 0) )
	{
		var i;
		for(i = 0; i < s.length; i++)
		{
			var c = s.charAt(i);
			if (whitespace.indexOf(c) == -1) return false;
		}
	}
	return true;
}

function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}

function isInRange (s,range1,range2)
{
	s = stripWhitespace(s);
	if (s >= range1 && s <= range2) return true;
	else return false;
}	

function isValidPhone (s)
{
	var i;
	s = stripWhitespace(s);
	for (i = 0; i < s.length; i++)
	{
		//  Check that current character is number.
		var c = s.charAt(i);
		if (!( ((c >= "0") && (c <= "9")) || (c == ".") || (c == "-") || (c == "(") || (c == ")") )) return false;
	}
	// All characters are numbers.
	return true;
}


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

function isEmail (s)
{
	// there must be >= 1 character before @, so we
	// start looking at character position 1
	// (i.e. second character)

	var i = 1;
	s = stripWhitespace(s);
	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 isPassword (s)
// must have six chars with at least 1 number and 1 character
{
        var i=0,
        int_flag=0,
        char_flag=0,
        min_password_length = 6;

        s = stripWhitespace(s);

        // run through all the chars looking for a number and non-number
        for (i=0; i < s.length; i++)
        {
                if ( isInteger(s.charAt(i)) )
                {
                        int_flag = 1;
                }
                if ( isChar(s.charAt(i)) )
                {
                        char_flag = 1;
                }
        }

        if ( int_flag != 1 || char_flag != 1 || s.length < min_password_length )
        {
                return false;
        }

        // must be good
        return true;
}

function radioChecked (fs)
// loop through all the buttons in form f named s and see if one is checked
{
	for (i=0; i < fs.length; i++) {
		if (fs[i].checked) { return true; }
	}
	return false;
}
