﻿var errorElm = "javMsg";

function deleteChildren(element) {
    var i;

    if (element) {
        if (element.childNodes) {
            for (i=element.childNodes.length - 1; i>=0; i--) {
                element.removeChild(element.childNodes[i]);
            }
        }
    }
}

function displayError(errortxt) {
	var javmsg = document.getElementById(errorElm);
	var txtnode;
	var brnode;
	var arr;
	var i, j;

	if ("" == errortxt) {
		return;
	}

	if (javmsg) {
		deleteChildren(javmsg);
		if (-1 != errortxt.indexOf("@BR@")) {
			arr = errortxt.split("@BR@");
			for (j=0; j<arr.length; j++) {
				if (0 != j) {
					brnode = document.createElement("BR");
					javmsg.appendChild(brnode);
				}

				txtnode = document.createTextNode(arr[j]);
				javmsg.appendChild(txtnode);
			}
		} else {
			txtnode = document.createTextNode(errortxt);
			javmsg.appendChild(txtnode);
		}

		javmsg.style.display = "";
	}
}

function validate_required(field, errortxt) {
	with (field) {
		if ((null == value) || ("" == value)) {
			displayError(errortxt);
			return false;
		} else {
			return true;
		}
	}
}

function validate_required_select(select_field, errortxt) {
	with (select_field) {
		if (0 == selectedIndex) {
			displayError(errortxt);
			return false;
		} else {
			return true;
		}
	}
}

function validate_required_checked(field, errortxt) {
	with (field) {
		if (!checked) {
			displayError(errortxt);
			return false;
		} else {
			return true;
		}
	}
}

function validate_minlength(field, minlength, errortxt) {
	with (field) {
		if ((null == value) || ("" == value)) {
			displayError(errortxt);
			return false;
		} else if (value.length < minlength) {
			displayError(errortxt);
			return false;
		} else {
			return true;
		}
	}
}

function validate_fieldmatch(field1, field2, errortxt) {
	if (field1.value != field2.value) {
			displayError(errortxt);
			return false;
	} else {
		return true;
	}
}

function validate_trial(thisform) {

	var usernameError = "Please enter a user name.";
	var passwordError = "You must enter a password of at least four characters.";
	var passwordMatchError = "Your passwords do not match.";
	var firstnameError = "Please enter your first name.";
	var lastnameError = "Please enter your last name.";
	var orgnameError = "Please enter your organization name.";
	var secretQuestionError = "Please select your secret question.";
	var secretAnswerError = "Please enter a secret answer.";
	var emailError = "Please enter your email address.";
	var websiteError = "Please enter your web site URL.";
	var phoneError = "Please enter your phone number.";
	var timezoneError = "Please select your time zone.";
	var countryError = "Please select your country.";
	var stateError = "Please select your state.";
	var termsError = "You must agree to the terms and conditions.";
	var errorString = "";
	var valResult;

	with (thisform) {
		if (false === validate_required(username, "")) {
			username.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += usernameError;
		}

		if (false === validate_required(password1, "")) {
			password1.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += passwordError;
		}

		if (false == validate_fieldmatch(password1, password2, "")) {
			password1.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += passwordMatchError;
		}

		if (false === validate_required(firstname, "")) {
			firstname.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += firstnameError;
		}

		if (false === validate_required(lastname, "")) {
			lastname.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += lastnameError;
		}

		if (false === validate_required(orgname, "")) {
			orgname.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += orgnameError;
		}

		if (false === validate_required_select(secretquestion, "")) {
			secretquestion.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += secretQuestionError;
		}

		if (false === validate_required(secretanswer, "")) {
			secretanswer.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += secretAnswerError;
		}

		if (false === validate_required(email, "")) {
			email.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += emailError;
		}

		if (false === validate_required(url, "")) {
			url.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += websiteError;
		}

		if (false === validate_required(phone, "")) {
			phone.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += phoneError;
		}

		if (false === validate_required_select(timezone, "")) {
			timezone.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += timezoneError;
		}

		if (false === validate_required_select(ddlCountry, "")) {
			ddlCountry.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += countryError;
		}

		if (("1" == ddlCountry.options[ddlCountry.selectedIndex].value) &&
			(false === validate_required_select(ddlState, ""))) {
			ddlState.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += stateError;
		}

		if (false === validate_required_checked(terms, "")) {
			terms.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += termsError;
		}
	}

	if ("" != errorString) {
		//alert(errorString);
		displayError(errorString);
		return false;
	}

	return true;
}

function validate_contact(thisform) {

	var firstnameError = "Please enter your first name.";
	var lastnameError = "Please enter your last name.";
	var orgnameError = "Please enter your organization name.";
	var emailError = "Please enter your email address.";
	var phoneError = "Please enter your phone number.";
	var deptError = "Please select the department you wish to contact.";
	var messageError = "Please enter a message.";
	var errorString = "";
	var valResult;

	with (thisform) {
		if (false === validate_required(firstname, "")) {
			firstname.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += firstnameError;
		}

		if (false === validate_required(lastname, "")) {
			lastname.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += lastnameError;
		}

		if (false === validate_required(email, "")) {
			email.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += emailError;
		}

		if (false === validate_required(phone, "")) {
			phone.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += phoneError;
		}

		if (false === validate_required(orgname, "")) {
			orgname.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += orgnameError;
		}

		if (false === validate_required_select(dept, "")) {
			dept.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += deptError;
		}

		if (false === validate_required(message, "")) {
			message.focus();
			if ("" != errorString) {
				errorString += "@BR@";
			}

			errorString += messageError;
		}
	}

	if ("" != errorString) {
		//alert(errorString);
		displayError(errorString);
		return false;
	}

	return true;
}

