function doFieldInfo(FieldID) {
	var new_win;
	fieldInfoWin = window.open('/includes/field_info.cfm?getIt=' + FieldID, 'FieldInfoWindow', 'toolbar=no,width=400,height=225,status=no,scrollbars=no,resizable=yes,menubar=no,location=no');
	fieldInfoWin.focus();
}

// this function automatically generates a 'suggested' userID for the form
function autoUserID(theField) {
	var userID;
	var theForm = theField.form;
	userID = theForm.FirstName.value.substring(0,1) + theForm.LastName.value.substring(0,15);

	// remove spaces and special chars
	userID = userID.replace(/'/g, "");
	userID = userID.replace(/\s/g, "");
	userID = userID.replace(/-/g, "");

	// make it lowercase
	theForm.UserID.value = userID.toLowerCase();
}

function showHideLoginFields(theField) {
    var showit = false;
    var theForm = theField.form;
    for (i=0; i < theForm.randomLogin.length; i++) {
        if ( (theForm.randomLogin[i].checked) && (theForm.randomLogin[i].value == 0) ) {
            showit = true;
        }
    }
    
    if (showit) {
        showObj('loginLayer');
        autoUserID(theField);
    } else {
        hideObj('loginLayer');
    }
}

// this function is here to check to see that the password is at least 4 characters long
function _at_least_4_char(obj) {
	if (obj.value.length < 4)
		return false;
	else
		return true;
}

function do_onError(form_object, input_object, object_value, error_message) {
	alert(error_message);
	return false;
}

function chk_hasValue(obj, obj_type) {
	if (obj_type == "TEXT") {
		if (obj.value.length == 0)
			return false;
		else
			return true;
	} else if (obj_type == "SELECT") {
		for (i=0; i < obj.length; i++) {
			if (obj.options[obj.selectedIndex].value == "--")
				return false;
			}
	   	return true;
	} else if (obj_type == "RADIO") {
		for (i=0; i < obj.length; i++) {
			if (obj[i].checked)
				return true;
		}
		return false;
	}
}

function do_checkinteger(object_value) {
	//Returns true if value is a number or is NULL
	//otherwise returns false

	if (object_value.length == 0)
		return true;

	//Returns true if value is an integer defined as
	//   having an optional leading + or -.
	//   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

	//The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
	//Was it a decimal?
	if (check_char < 1)
	return chk_checkNumber(object_value);
	else
	return false;
	}


function chk_numberRange(object_value, min_value, max_value) {
	// check minimum
	if (min_value != null) {
		if (object_value < min_value)
			return false;
	}

	// check maximum
	if (max_value != null) {
		if (object_value > max_value)
			return false;
		}

	//All tests passed, so...
	return true;
}


function chk_checkNumber(object_value) {
	//Returns true if value is a number or is NULL
	//otherwise returns false

	if (object_value.length == 0)
		return true;

	//Returns true if value is a number defined as
	//   having an optional leading + or -.
	//   having at most 1 decimal point.
	//   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

	//The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
	//Was it a decimal?
	if (check_char == 1)
		decimal = true;
	else if (check_char < 1)
		return false;

	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++) {
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1) {
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		} else if (check_char == 0) {
			if (decimal || digits)
				trailing_blank = true;
		// ignore leading blanks

		} else if (trailing_blank)
			return false;
		else
			digits = true;
	}
	//All tests passed, so...
	return true
}


function do_checkRange(object_value, min_value, max_value) {
	//if value is in range then return true else return false

	if (object_value.length == 0)
		return true;

	if (!chk_checkNumber(object_value)) {
		return false;
	} else {
		return (chk_numberRange((eval(object_value)), min_value, max_value));
	}

	//All tests passed, so...
	return true;
}


function do_checkPhone(object_value) {
	if (object_value.length == 0)
		return true;

	if (object_value.length != 12)
		return false;

	// check if first 3 characters represent a valid area code
	if (!chk_checkNumber(object_value.substring(0,3)))
		return false;
	else if (!chk_numberRange((eval(object_value.substring(0,3))), 100, 1000))
		return false;

	// check if area code/exchange separator is either a'-' or ' '
	if (object_value.charAt(3) != "-" && object_value.charAt(3) != " ")
		return false

	// check if  characters 5 - 7 represent a valid exchange
	if (!chk_checkNumber(object_value.substring(4,7)))
		return false;
	else if (!chk_numberRange((eval(object_value.substring(4,7))), 100, 1000))
		return false;

	// check if exchange/number separator is either a'-' or ' '
	if (object_value.charAt(7) != "-" && object_value.charAt(7) != " ")
		return false;

	// make sure last for digits are a valid integer
	if (object_value.charAt(8) == "-" || object_value.charAt(8) == "+")
		return false;
	else {
		return (do_checkinteger(object_value.substring(8,12)));
	}
}

function checkmyForm(theForm) {
	if  (!chk_hasValue(theForm.FirstName, "TEXT" )) {
		if  (!do_onError(theForm, theForm.FirstName, theForm.FirstName.value, "You must enter a FIRST NAME to continue.")) {
			theForm.FirstName.focus();
			return false;
		}
	}

	if  (!chk_hasValue(theForm.LastName, "TEXT" )) {
		if  (!do_onError(theForm, theForm.LastName, theForm.LastName.value, "You must enter a LAST NAME to continue.")) {
			theForm.LastName.focus();
			return false;
		}
	}

	if  (!do_checkPhone(theForm.Phone.value)) {
		if  (!do_onError(theForm, theForm.Phone, theForm.Phone.value, "You must enter a PHONE NUMBER, including area code, in the following format\n\n	 888-555-1234")) {
			theForm.Phone.focus();
			return false;
		}
	}


	// if specifying a login id
	if (theForm.randomLogin) {
		for (i=0; i < theForm.randomLogin.length; i++) {
		if (theForm.randomLogin[i].checked)
			if (theForm.randomLogin[i].value == 0)
				chkUserID = true;
		}
		
		if (chkUserID) {
			// check to see that userid is at least 4 characters
			if (!_at_least_4_char(theForm.userID)) {
				alert("The LOGIN ID must contain at least 4 characters and may NOT contain spaces, hyphens or other special characters.");
				theForm.userID.focus();
				return false;
			}
		
			if  (!chk_hasValue(theForm.passwd1, "TEXT" )) {
				if  (!do_onError(theForm, theForm.passwd1, theForm.passwd1.value, "You must enter a PASSWORD to continue.")) {
					theForm.passwd1.focus();
					return false;
				}
			}
		
			// check to see that password is at least 4 characters
			if (!_at_least_4_char(theForm.passwd1)) {
				alert("The PASSWORD must contain at least 4 characters and may NOT contain spaces, hyphens or other special characters.");
				theForm.passwd1.value = '';
				theForm.passwd2.value = '';
				theForm.passwd1.focus();
				return false;
			}
		
			// check password confirmation
			if (theForm.passwd2.value != theForm.passwd1.value) {
				alert("Your confirmation of the PASSWORD does not match the first instance of the password you provided.");
				theForm.passwd1.value = '';
				theForm.passwd2.value = '';
				theForm.passwd1.focus();
				return false;
			}		
		}
	}

	return true;
}
