
// RegExp pattern for a valid email address
var emailPattern = /^\s*[a-zA-Z0-9_.%+-]+\@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9]{2,4})\s*$/;

// Generically get object
function getObj(id)
{
	var node;
	if (document.getElementById)
		node = document.getElementById(id);
	else if (document.all)
		node = document.all[id];
		
	return node;
}

// Submit feedback form
function submitReportForm(theForm, referrer)
{
	var valid = true;
	var message = "";
	var category = theForm.category.value;
	var summary = theForm.summary.value;
	var description = theForm.description.value
	var email = theForm.custom_field_2.value;
	
	theForm.custom_field_3.value = (navigator.userAgent) ? navigator.userAgent : "N/A";
	theForm.custom_field_4.value = (String(document.referrer) != '') ? document.referrer 
		: ((String(referrer) != '') ? referrer : location.href);
			
	if (category == "")
	{
		message += "Please choose a category.\n";
		valid = false;
	}
	if (summary.match(/^\s*$/))
	{
		message += "Please enter a description.\n";
		valid = false;
	}
	if (description.match(/^\s*$/))
	{
		message += "Please enter a comment.\n";
		valid = false;
	}
	if (email != "" && !email.match(emailPattern))
	{
		message += "Please enter a valid email address.\n";
		valid = false;
	}
	
	if (!valid) alert("Warning:\n"+message);
	return valid;
}

// Submit email form
function submitEmailForm(theForm, referrer)
{
	var valid = true;
	var warning_msg = "";
	var name_from = theForm.name_from.value;
	var email_from = theForm.email_from.value;
	var email_to = theForm.email_to.value;
	
	theForm.rfr.value = (String(document.referrer) != '') ? document.referrer 
		: ((String(referrer) != '') ? referrer : location.href);
	
	if (name_from.match(/^\s*$/))
	{
		warning_msg += "Please enter your name.\n";
		valid = false;
	}
	if (!validateEmail(email_from))
	{
		warning_msg += "Please enter your valid email address.\n";
		valid = false;
	}
	if (!validateEmail(email_to, true))
	{
		warning_msg += "Please enter your friend's valid email address.\n";
		valid = false;
	}
		
	if (!valid) alert("Warning:\n"+warning_msg);
	return valid;
}

// Submit comment form
function submitCommentForm(theForm, referrer)
{
	var valid = true;
	var warning_msg = "";
	var name_from = theForm.name_from.value;
	var email_from = theForm.email_from.value;
	var email_to = theForm.email_to.value;
	var message = theForm.message.value;
	
	theForm.rfr.value = (String(document.referrer) != '') ? document.referrer 
		: ((String(referrer) != '') ? referrer : location.href);
	
	if (name_from.match(/^\s*$/))
	{
		warning_msg += "Please enter your name.\n";
		valid = false;
	}
	if (!validateEmail(email_from))
	{
		warning_msg += "Please enter your valid email address.\n";
		valid = false;
	}
	if (message.match(/^\s*$/))
	{
		warning_msg += "Please enter a comment.\n";
		valid = false;
	}
		
	if (!valid) alert("Warning:\n"+warning_msg);
	return valid;
}

// Create popup window for the given slideshow URL
/*function popupSlideshow(url)
{
	window.open(url, 'slideshow', 'width=800,height=600,resizable=0,scrollbars=0,status=0');
}*/

// Sends an email, based on the user id
function sendEmail(id, host)
{
	var hostname = "virginia.edu";
	if (host != null && host != "")
		hostname = host;
		
	location = "mailto:"+id+"@"+hostname;
}

// Writes an inline email address, based on the user id
function displayEmail(id, host)
{
	var hostname = "virginia.edu";
	if (host != null && host != "")
		hostname = host;
		
	document.write(id+"@"+hostname);
}

// Validates an email address, or list of comma-separated addresses, limit 20
function validateEmail(emailStr, allowMultiple)
{
	var valid = true;
	if (allowMultiple == null) allowMultiple = false;
	
	var emailList = emailStr.split(",");
		
	if (!allowMultiple) {
		valid = emailStr.match(emailPattern);
	} else if (emailList.length > 20) {
		valid = false;
	} else {	
		for (var i=0; i < emailList.length; i++) {
			if (!emailList[i].match(emailPattern)) {
				valid = false;
				break;
			}
		}
	}
	
	return valid;
}

// Converts an HTML string to a formatted plain text string
function htmlToText(htmlStr)
{
	var text = htmlStr;
	text = text.replace(/(<br\s*\/?>)|(<\/p>)/ig, "\n\n");
	text = text.replace(/<\/?[\w.:-]+(\s+[^>]+)?\/?>/g, "");
	text = text.replace(/&nbsp;/g, " ");
	text = text.replace(/&quot;/g, "\"");
	text = text.replace(/&apos;/g, "\'");
	text = text.replace(/&lt;/g, "<");
	text = text.replace(/&gt;/g, ">");
	text = text.replace(/&amp;/g, "&");
	
	return text;
}

// Changes the refinement for the related search results
function submitRefinement(theSelect, url)
{
	var newUrl = url + theSelect.options[theSelect.selectedIndex].value;
	if (newUrl.charAt(newUrl.length-1) == '.') {
		newUrl = newUrl.substr(0, newUrl.length-1);
	}
	location.href = newUrl;
}

// Create popup window within the page, using Lightbox, for the given slideshow URL
function popupSlideshow(url)
{
	var id = url.indexOf("?") != -1 ? url.substring(0, url.indexOf("?")) : url;
	id = id.indexOf("/") != -1 ? url.substring(url.lastIndexOf("/")) : id;
	id = encodeURIComponent( id );
	
	var div = $("div[id="+id+"]");

	if (div.size() == 0) {
		div = $("<div/>").attr("id", id).hide();
		
		$.ajax({
			'url': url,
			'success': function (data, textStatus) {
				div.appendTo("body").append(data);
				div.find("a").addClass('lightbox');
				div.find(".lightbox").lightbox();
				div.find(".lightbox:first").click();
			}
		});		
	}
	else
	{
		div.find(".lightbox:first").click();	
	}
}

function loadPageTools(options)
{
	if ( 
		$('body.cp.newsstory', document).length > 0 
		&& $('body.cpSf', document).length == 0
		&& document.title.toLowerCase().indexOf("file not found") == -1)
	{
		$.getScript("http://artsandsciences.virginia.edu/js/jquery/jquery.pagetools.js", function() {
			$(document).pageTools({slim: true, icms: true, feedbackProject: options.project, ip: options.ip});
		});
	}
}