function popWin(url,size) {

	var winWidth = 0;
	var winHeight = 0;
	var winSettings = "";

	if (size == "small") {

		winWidth = 200;
		winHeight = 300;

	} else if (size == "medium") {

		winWidth = 300;
		winHeight = 350;

	} else if (size == "large") {

		winWidth = 550;
		winHeight = 400;

	} else if (size == "super") {

		winWidth = 640;
		winHeight = 480;

	} else {

		winWidth = 300;
		winHeight = 350;

	}

	var winSettings = "width=" + winWidth + ",height=" + winHeight + ",scrollbars,resizable,top=35,left=35,screenX=35,screenY=35";

	var newWin = window.open(url, "", winSettings);

}


function changeRegion(regionId, url, formName) {

	if (regionId != -1) {

		window.location.href = url + '?regionId=' + regionId;

	} else {

		alert('Please select a region from the list.');

	}

	var resetList = eval('document.' + formName + '.regionId.selectedIndex = 0');

	return false;

}


function changeDropdown(val, varName, formName) {

	if (val == 0) {

		varName = varName.toLowerCase();

		alert('Please select a ' + varName + ' from the list.');

		var resetList = eval('document.' + formName + '.' + varName + '.selectedIndex = 0');

		var giveFocus = eval('document.' + formName + '.' + varName + '.focus()');

		return false;

	}

}

function highlightMenu(obj) {

	obj.style.color = '#CC9933';
	obj.style.fontWeight = 'bold';

}

function unhighlightMenu(obj) {

	obj.style.color = '#000000';
	obj.style.fontWeight = 'normal';

}

//MAR/MARGENE REGISTRATION FORM VALIDATION FUNCTIONS


var provinceStr = 'AB|BC|MB|NB|NF|NS|NT|NU|ON|PE|SK|QC|YT';

var stateStr = 'AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FL|GA|GM|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY';

function isInteger(value) {

	//verifies that input is an integer.

	return ( value == parseInt(value) )

}


function clearTextInput(element) {

		var formName = element.form.name;
		var elementName = element.name;
		elementStr = 'document.' + formName + '.' + elementName;

		element.value = '';  //Clears the value of the input.

		setTimeout("var goTo = eval(elementStr + '.focus()');", 100);  //Focuses back on the input.

}


function stringOnlySpaces(element) {

	//Checks if a string is composed solely of spaces.  Also returns true if the string has length of zero.

	for (var i = 0; i < element.value.length; i++) {

		if (element.value.charAt(i) != ' ') {

			return false;
			break;

		}

	}

	return true;

}


function checkCountrySelection() {

	if (document.registration.country[document.registration.country.selectedIndex].value == 0) {

		alert('Please select a COUNTRY before filling out the rest of the form.');

		document.registration.country.focus();  //Focus back on country dropdown element.

		return false;

	}

}


function onChangeCountry(countryId) {

	//Tests that a valid country value is chosen from the dropdown list.  Assumes that non-valid choices have value of zero.

	if (countryId == 0) {

		alert('Please select a country from the list.');

		var resetList = eval('document.registration.country.selectedIndex = 0');

		return false;

	}

	return true;

}



function onBlurState(stateAbbrev) {

	//Tests that Canadians choose a province, Americans choose a state, and others choose neither.

	var countryId = document.registration.country[document.registration.country.selectedIndex].value;

	if (countryId == 2 || countryId == 20) {

		if (stateAbbrev == 0) {

			alert('Please select a state or province from the list.');

			var resetList = eval('document.registration.state.selectedIndex = 0');

			return false;

		} else if (countryId == 2) {

			if (provinceStr.indexOf(stateAbbrev) != -1) {

				alert('If from the United States, select a U.S. state from the list.');

				var resetList = eval('document.registration.state.selectedIndex = 0');

				return false;

			}

		} else if (countryId == 20) {

			if (stateStr.indexOf(stateAbbrev) != -1) {

				alert('If from Canada, select a Canadian province from the list.');

				var resetList = eval('document.registration.state.selectedIndex = 0');

				return false;

			}

		}		

	} else {

		alert('If not from Canada or the United States, do NOT select from this list.');

		var resetList = eval('document.registration.state.selectedIndex = 0');

		return false;

	}

	return false;

}


function countryStateMismatch(country, state) {

	//During final validation, checks that the selected country and state are a valid match.

	var countryId = country[country.selectedIndex].value;
	var stateAbbrev = state[state.selectedIndex].value;

	if (countryId != 2 && countryId != 20 && stateAbbrev != 0) {

		return true;

	} else if ( (countryId == 2) && (stateAbbrev != 0) && (stateStr.indexOf(stateAbbrev) == -1) ) {

		return true;

	} else if ( (countryId == 20) && (stateAbbrev != 0) && (provinceStr.indexOf(stateAbbrev) == -1) ) {

		return true;

	}

	return false;

}


function checkPostalCodeLength(strPostalCode, country)  {

	if (country == 2) {


		if ( (strPostalCode.length == 5) || (strPostalCode.length == 10) ) {

			return true;

		} else {

			return false;

		}

	} else if (country == 20) {

		if (strPostalCode.length == 7) {

			return true;

		} else {

			return false;

		}

	} else {

		return true;

	}

	return false;

}


function isZipCode(strZipCode) {

	//U.S ZIP Code (e.g., 20742 or 20742-1234)

	var format = '';

	if (strZipCode.length == 5) {

		format = '#####';

	} else if (strZipCode.length == 10) {

		format = '#####-####';

	}

	for (var i = 0; i < strZipCode.length; i++) {

		if ( format.charAt(i) == '#' && !isInteger(strZipCode.charAt(i)) ) {

			return false;

		} else if ( format.charAt(i) == '-' && strZipCode.charAt(i) != '-' ) {

			return false;

		}

	}

	return true;

}


function isCanadianPostalCode(strPostalCode) {

	//Canadian postal code (e.g., H3Z 2Y7)

	myAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

	if (strPostalCode.charAt(3) != ' ') {

		return false;

	} else if ( isInteger(strPostalCode.charAt(0)) || isInteger(strPostalCode.charAt(2)) || isInteger(strPostalCode.charAt(5)) ) {

		return false;

	} else if ( (myAlphabet.indexOf(strPostalCode.charAt(0)) == -1) || (myAlphabet.indexOf(strPostalCode.charAt(2)) == -1) || (myAlphabet.indexOf(strPostalCode.charAt(5)) == -1) ) {

		return false;

	} else if ( !isInteger(strPostalCode.charAt(1)) || !isInteger(strPostalCode.charAt(4)) || !isInteger(strPostalCode.charAt(6)) ) {

		return false;

	}

	return true;

}


function isPostalCode(element) {

	var strPostalCode = element.value;
	var country = element.form.country.options[element.form.country.selectedIndex].value;

	if (country == 2) {

		if (! (checkPostalCodeLength(strPostalCode,country) && isZipCode(strPostalCode)) ) {

			alert('Error:\n\nThe ZIP Code you entered -- \"' + strPostalCode + '\" -- is incorrect.\n\nU.S. ZIP Codes must be in one of two formats:\n\nstandard 5-digit:  (e.g., 20742)\n\nor\n\nZIP \+ four:  (e.g., 20742-1234).\n');

			clearTextInput(element);

			return false;

		}

	} else if (country == 20) {

		if (! (checkPostalCodeLength(strPostalCode,country) && isCanadianPostalCode(strPostalCode)) ) {

			alert('Error:\n\nThe postal code you entered -- \"' + strPostalCode + '\" -- is incorrect.\n\nCanadian postal codes must be in upper case with one space between the first three and last three characters (e.g., H3Z 2Y7).\n\n');

			clearTextInput(element);

			return false;

		}

	} else {

		return true;

	}

	return false;

}


function isValidEmail(element) {

	var isValid = true;
	var username = '';
	var host = '';
	var hostType = '';

	//Test for commas.

	if (element.value.indexOf(',') != -1) {

		isValid = false;

	} 

	//Test for spaces.

	if (element.value.indexOf(' ') != -1) {

		isValid = false;

	} 

	//Test for double periods.

	if (element.value.indexOf('..') != -1) {

		isValid = false;

	} 

	//Test for at least one @ sign.

	if (element.value.indexOf('@') == -1) {

		isValid = false;

	}

	//Test for presence of two or more @ signs.

	var parts = element.value.split('@');

	if (parts.length > 2) {

		isValid = false;

	}

	if (isValid) {

		username = element.value.substr(0, (element.value.indexOf('@')));
		host = element.value.substr((element.value.indexOf('@') + 1), element.value.length);

	}

	//Test for presence of at least one period in host name.

	if (host.indexOf('.') == -1) {

		isValid = false;

	} 

	//Test for presence of underscores, percent signs, ampersands, or plus signs in the host name.

	if (host.indexOf('_') != -1) {

		isValid = false;

	} 

	if (host.indexOf('%') != -1) {

		isValid = false;

	} 

	if (host.indexOf('&') != -1) {

		isValid = false;

	} 

	if (host.indexOf('+') != -1) {

		isValid = false;

	} 

	//Test that host is of type edu, org, gov, net, com, or int.

	//alert(element.value.charAt(element.value.length - 4));

	//if (element.value.charAt(element.value.length - 4) != '.') {

		//hostType = host.substr(host.length - 3, host.length);

		//if ((hostType != 'edu') && (hostType != 'com') && (hostType != 'org') && (hostType != 'net') && (hostType != 'gov') && (hostType != 'int')) {

			//isValid = false;

		//}

	//}

	if (!isValid) {

		alert('The e-mail address you entered -- ' + element.value + ' -- is not valid.');

		clearTextInput(element);

		return false;

	}

	return true;

}


function radioChecked(element) {

	var isChecked = false;

	for (var i = 0; i < element.length; i++) {

		if (element[i].checked == true) {

			isChecked = true;
			break;

		}

	}

	return isChecked;

}


function validateRegistration(form) {

	var errCount = 0;
	var errMsg = '';

	if ( form.country[form.country.selectedIndex].value == 0 ) {

		errCount++;
		errMsg = errMsg + '\n' + errCount + '. COUNTRY is not selected.';

	} 

	if ( stringOnlySpaces(form.firstName) ) {

		errCount++;
		errMsg = errMsg + '\n' + errCount + '. FIRST NAME is required.';

	}

	if ( stringOnlySpaces(form.lastName) ) {

		errCount++;
		errMsg = errMsg + '\n' + errCount + '. LAST NAME is required.';

	}

	if ( (form.status[5].checked == false) && stringOnlySpaces(form.institution) ) {

		errCount++;
		errMsg = errMsg + '\n' + errCount + '. INSTITUTION is required.  (If none, enter \"n\/a\".)';

	}

	if ( stringOnlySpaces(form.street1) ) {

		errCount++;
		errMsg = errMsg + '\n' + errCount + '. STREET ADDRESS 1 is required.';

	}

	if ( stringOnlySpaces(form.city) ) {

		errCount++;
		errMsg = errMsg + '\n' + errCount + '. CITY is required.';

	}

	if ( form.country[form.country.selectedIndex].value == 2 && form.state[form.state.selectedIndex].value == 0 ) {

		errCount++;
		errMsg = errMsg + '\n' + errCount + '. STATE is required.';

	} else if ( form.country[form.country.selectedIndex].value == 20 && form.state[form.state.selectedIndex].value == 0 ) {

		errCount++;
		errMsg = errMsg + '\n' + errCount + '. PROVINCE is required.';

	}

	if ( stringOnlySpaces(form.postalCode) && ( form.country[form.country.selectedIndex].value == 2 || form.country[form.country.selectedIndex].value == 20 ) ) {

		errCount++;

		if (form.country[form.country.selectedIndex].value == 2) {

			errMsg = errMsg + '\n' + errCount + '. ZIP CODE is required.';

		} else if (form.country[form.country.selectedIndex].value == 20) {

			errMsg = errMsg + '\n' + errCount + '. POSTAL CODE is required.';

		}

	}

	if ( stringOnlySpaces(form.email) ) {

		errCount++;
		errMsg = errMsg + '\n' + errCount + '. E-MAIL ADDRESS is required.';

	}

	if ( !radioChecked(form.status) ) {

		errCount++;
		errMsg = errMsg + '\n' + errCount + '. STATUS is required.';

	}

	if ( countryStateMismatch(form.country, form.state) ) {

		errCount++;
		errMsg = errMsg + '\n' + errCount + '. COUNTRY selection does not match STATE/PROVINCE selection.  Check selections.';

	}

	if (errCount > 0) {

		if (errCount > 1) {

			errMsg = 'ERRORS FOUND:\n' + errMsg;

		} else {

			errMsg = 'ERROR FOUND:\n' + errMsg;

		}

		alert(errMsg);

		errCount = 0;
		errMsg = '';

		return false;

	} else {

		return true;

	}

	return false;

}
