// Used in Newsletter pages with forms

function swapPreview(which) {
	var previewImg = document.getElementById("previewImg");
	var imgSrc = which.href;
	if (previewImg && imgSrc) {
		previewImg.src = imgSrc;
		return false;
	}
	return true;
}

function isBlank(str) {
	return (!str || str == "");
}
function isValidEmail(str) {
	var filter  = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})$/;
	return (filter.test(str));
}
function hasNewsletters() {
	var checks = document.getElementById("checkboxes");
	if (checks) {
		var inputs = checks.getElementsByTagName("input");
		for (var i=0; i < inputs.length; i++) {
			if (inputs[i].checked) {
				return true;
			}
		}
		return (inputs.length == 0);
	}
	return true;
}


function validateForm(which) {
	var error = "";
	if (!hasNewsletters()){
		error += "Please select at least one newsletter.\n\n";
	}
	if (which.email) {
		var email = which.email.value;
		if (isBlank(email)) {
			error += "Please enter an email address.\n\n";
		}
		else if (!isValidEmail(email)){
			error += "Invalid email address.  Address must be in the form 'name@domain.com'.\n\n";
		}
	}
	if (error != "") {
		alert(error);
		return false;
	}
	return true;
}

function validateForm2(which) {
	var errors = new Array(0);
	if (which.email) {
		var email = which.email.value;
		if (isBlank(email)) {
			errors.push("Please enter your new email address.");
		}
		else if (!isValidEmail(email)) {
			errors.push("Invalid email address.  Address must be in the form 'name@domain.com'.");
		}		
	}
	if (errors.length > 0) {
		var email_label = document.getElementById("email_label");
		if (email_label) {
			email_label.className = "error";
		}
		writeErrors(errors);
		return false;
	}
	return true;
}

function validateForm3(which) {
	var errors = new Array(0);
	if (which.email) {
		var email = which.email.value;
		if (isBlank(email)) {
			errors.push("Please enter your new email address.");
		}
		else if (!isValidEmail(email)) {
			errors.push("Invalid email address.  Address must be in the form 'name@domain.com'.");
		}		
	}
	if (errors.length > 0) {
		var email_label = document.getElementById("email_label");
		if (email_label) 
		writeErrors(errors);
		return false;
	}
	return true;
}

function writeErrors(errors) {
	var errorBox = document.getElementById("errorBox");
	if (errorBox) {
		var html = '<div class="errorMsg">';
		for (var i=0; i < errors.length; i++) {
			html += '<p>' + errors[i] + '</p>';
		}
		html += '</div>';
		errorBox.innerHTML = html;
	}
}

function initNForm() {
	var form = document.getElementById("fnews");
	if (form) {
		var anchors = form.getElementsByTagName("a");
		for (var i=0; i < anchors.length; i++) {
			if (anchors[i].parentNode.className == "preview") {
				anchors[i].onclick = function() {
					return swapPreview(this);
				}
			}
		}
	}
}

tii_callFunctionOnWindowLoad(function(event) {
	initNForm();
});
