<!--
// =================================================================================
// File: Subscribe.js
//
// Purpose: Handle Email Subscription -- note that the alerting of errors for now is
//			also done in this function.
//
// =================================================================================
function Subscribe_ValidateSub(f) {
	var bOk = true, bNameOk = true, bEmailOk = true, errMsg = "", el;
	var version = parseFloat(navigator.appVersion);

	if (f.contact.value == "") bNameOk = false;
	if (f.email.value == "") bEmailOk = false;

	if (bNameOk == false) {
		errMsg = "Please provide your name.\n"
		el = f.contact;
	}
	if (bEmailOk == false) {
		errMsg += "You";
		if (bNameOk == false) errMsg += " also";
		errMsg += " need to provide a valid email address.\n-- An example email address is: comments@globalissues.org\n";
		if (bNameOk) el = f.email;
	}

	bOk = (bNameOk && bEmailOk);
	if (!bOk) {
		alert(errMsg);
		if (version > 3) el.focus();
	}
	else {
		f.email.value = f.email.value.toLowerCase();

		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(f.email.value)) {
			bOk = false;
			alert('There seems to be an error or typo in the email address entered.\n\nPlease check that the format is correct.\nAn example of an email address is:\n\tcomments@globalissues.org');
			if (version > 3) f.email.focus();
		}
		else {
			var listName;
			if (f.list_name[0].checked)
				listName = f.list_name[0].value;
			else if (f.list_name[1].checked)
				listName = f.list_name[1].value;
			// for Non IE4 browsers and below, it seems like spaces are sent as spaces in a querystring,
			// rather than converted to the standard %20 etc... Bummer! The next code caters for that...
			//if (!(version >= 4 && browser == "Microsoft Internet Explorer"))
			var strContact = Subscribe_ReplaceChrs(f.contact.value, " ", "%20");
			f.redirect.value += "?email=" + f.email.value + "&contact=" + strContact + "&list_name=" + listName;
		}
	}
	return bOk;
}

function Subscribe_ReplaceChrs(strText, chrOut, chrIn) {
	while (strText.indexOf(" ") >= 0) {
		strText = Subscribe_ReplaceChr(strText, " ", "%20");
	}
	return strText;
}

function Subscribe_ReplaceChr(strText, chrOut, chrIn) {
	var strTextOrig = strText, returnText;
	var i, spacePos = strText.indexOf(" ");
	for (i = spacePos; i < strText.length; i++)
		if (strText.charAt(i) == chrOut)
			returnText = strTextOrig.substring(0,i) + chrIn + strTextOrig.substring(i + 1);
	return returnText;
}
<!-- end script -->
