// Set these to determine colours to shade table rows if good or bad
var element_good = "#333333";
var element_bad = "#CC3300";

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function returnGood()
{	return element_good
}

function isEmail(s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return false;
       else return (isEmail.arguments[1] == true);

    if (s.indexOf(" ")!=-1) 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 set_good(the_element) {
	if (the_element) the_element.style.borderColor = "black";
	if (the_element) the_element.style.borderWidth = "1px";
	if (the_element) the_element.style.borderStyle = "solid";
}

function set_bad(the_element) {
	if (the_element) the_element.style.borderColor = "red";
	if (the_element) the_element.style.borderWidth = "2px";
	if (the_element) the_element.style.borderStyle = "dashed";
}

function textbox_OK(the_box, the_row, good_colour) {
	if ( the_box.value != "" ) { // If text box is not empty, this field is OK.
		set_good(the_row);
		return true;
	}	else { // Otherwise set the background color to bad and set gotowhich if necessary.
		set_bad(the_row);
		if (gotowhich == "") { gotowhich = the_box; }
		return false;
	}
}

function email_OK(email_box, the_row, good_colour) {
	if ( isEmail(email_box.value) ) { // Check for a valid email address
		set_good(the_row);
		return true;
	}	else { // If invalid, set the background color to bad and set gotowhich if necessary.
		set_bad(the_row);
		if (gotowhich == "") { gotowhich = email_box; }
		return false;
	}
}

function any_selected(radio_group) {
	var i;

	for (i=0; i < radio_group.length; i++)
		if (radio_group[i].checked == true) return true;
	return false;
}

function radio_OK(radio_group, the_row, good_colour) {
	if ( any_selected(radio_group) ) { // If any are selected, this field is OK.
		set_good(the_row);
		return true;
	}	else { // Otherwise set the background color to bad and set gotowhich if necessary.
		set_bad(the_row);
		if (gotowhich == "") { gotowhich = radio_group[0]; }
		return false;
	}
}

function select_OK(select_box, the_row, good_colour) {
	if ( select_box.selectedIndex != 0 ) { // If select index is not 0, this field is OK.
		set_good(the_row);
		return true;
	}	else { // Otherwise set the background color to bad and set gotowhich if necessary.
		set_bad(the_row);
		if (gotowhich == "") { gotowhich = select_box; }
		return false;
	}
}