// finds if a string is blank (nothing but spaces)
function isblank(v) {
	var blank = true;
	for (i = 0; i < v.length; i++) {
		if (v.charAt(i) != ' ') { blank = false; }
	}
	return blank;
}

// finds if a form value is empty
function isempty(x) {
	if (x.value == "" || isblank(x.value)) { return true; }
	else { return false; }
}

// finds if a select box has not been changed
function unchanged(x) {
	if (x.selectedIndex == 0) { return true; }
	else { return false; }
}

// finds if a radio button is not checked
function unchecked(x) {
	var unchecked = true;
	for (i = 0; i < x.length; i++) {
		if (x[i].checked) { unchecked = false; }
	}
	return unchecked;
}

// finds the selected radio button
function radioSelected(x) {
	var selected = false;
	for (i = 0; i < x.length; i++) {
		if (x[i].checked) { selected = i; }
	}
	return selected;
}

// validate account create
function vac(frm) {
	var n =	 frm.length;	// number of inputs in the form
	var errors = "The following errors were found:\n";			// error message to be alerted
	var emails = 0;				// number of invalid email addresses
	var valid = true;			// whether or not form is valid
	
	if (isempty(frm.fname)) { errors += "First name is blank\n"; }
	if (isempty(frm.lname)) { errors += "Last name is blank\n"; }
	if (isempty(frm.email)) { errors += "E-mail is blank\n"; }
	
	var role = radioSelected(frm.role);
	if (role !== false) {
		switch (frm.role[role].value) {
			case '1':
				if (unchanged(frm.company) || frm.company.options[frm.company.selectedIndex].value == 0) { errors += "Company not selected\n"; }
				break;
			case '2':
				if ((unchanged(frm.company) || frm.company.options[frm.company.selectedIndex].value == 0) 
						&& (unchanged(frm.association) || frm.association.options[frm.association.selectedIndex].value == 0)) {
							errors += "Company or Association not selected\n";
				}
				break;
			case '3':
				if (unchanged(frm.association) || frm.association.options[frm.association.selectedIndex].value == 0) { errors += "Association not selected\n"; }
				break;
			default:
				break;
		}
	}
	else {
		alert("Please note that access to the LIFE Idea Exchange is limited to companies that are financial supporters of the LIFE Foundation. If your organization is a LIFE Member Company and you’re experiencing difficulty logging into the Exchange site, please contact lifeexchange@lifefdn.org.  Thank you.");
		return false;
	}
	
	if (isempty(frm.login)) { errors += "Login is blank\n"; }
	if (isempty(frm.password)) { errors += "Password is blank\n"; }
	if (frm.password.value != frm.cpassword.value) { errors += "Passwords do not match\n"; }
	
	if (errors != "The following errors were found:\n") {
		alert(errors);
		valid = false;
	}
	
	return valid;
}

// validate account edit
function vae(frm) {
	var n =	 frm.length;	// number of inputs in the form
	var errors = "The following errors were found:\n";			// error message to be alerted
	var emails = 0;				// number of invalid email addresses
	var valid = true;			// whether or not form is valid
	
	if (isempty(frm.fname)) { errors += "First name is blank\n"; }
	if (isempty(frm.lname)) { errors += "Last name is blank\n"; }
	if (isempty(frm.email)) { errors += "E-mail is blank\n"; }
	if (unchanged(frm.company)) { errors += "Company not selected\n"; }
	
	if (!isempty(frm.opassword) && isempty(frm.npassword)) { errors += "New password is blank\n"; }
	
	if (errors != "The following errors were found:\n") {
		alert(errors);
		valid = false;
	}
	
	return valid;
}
