<!--//
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function adjustCase(val) {
        newVal = '';
        val = val.split(' ');
        for(var c=0; c < val.length; c++) 
		{
			newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length) + ' ';
        }
        return newVal;
}
function cleanForDisplay(toClean)
{
	var cleanedUp = toClean;
	cleanedUp = toClean.replace("_"," ");
	cleanedUp = adjustCase(cleanedUp);
	return cleanedUp.trim();
}
function makeWeekDay(day)
{
	var dayOut = "";
	switch (day)
	{
		case "Sun":
			dayOut = "Sunday";
			break;
		case "Mon":
			dayOut = "Monday";
			break;
		case "Tue":
			dayOut = "Tuesday";
			break;
		case "Wed":
			dayOut = "Wednesday";
			break;
		case "Thu":
			dayOut = "Thursday";
			break;
		case "Fri":
			dayOut = "Friday";
			break;
		case "Sat":
			dayOut = "Saturday";
			break;
		default: 
			dayOut = "";
			break;
	}	
	return dayOut; 
}
function makeDay(day)
{
	var dayOut = "";
	if (day == 1||day == 21||day ==31)
	{
		dayOut = day + "st";
	}
	else if (day == 2||day == 22)
	{
		dayOut = day + "nd";
	}
	else if (day == 3||day == 23)
	{
		dayOut = day + "rd";
	}
	else
	{
		dayOut = day + "th";
	}
	return dayOut;
}

function removeSpaces(string) 
{
	return string.split(' ').join('');
}
function initialCap(value) 
{
	return (value.substr(0, 1).toUpperCase() + value.substr(1,value.length));
}
function checkString(strIn)
{
	if (strIn.length > 0)
	{
		var strOut = "";
		return initialCap(strIn);
	}
	else
	{
		return "Unknown";
	}
}
function fixMonth(month)
{
	monthOut = "";
	switch (removeSpaces(month))
	{
		case "Jan":
			monthOut = "January";
			break;
		case "Feb":
			monthOut = "February";
			break;
		case "Mar":
			monthOut = "March";
			break;
		case "Apr":
			monthOut = "April";
			break;
		case "May":
			monthOut = "May";
			break;
		case "Jun":
			monthOut = "June";
			break;
		case "Jul":
			monthOut = "July";
			break;
		case "Aug":
			monthOut = "August";
			break;
		case "Sep":
			monthOut = "September";
			break;
		case "Oct":
			monthOut = "October";
			break;
		case "Nov":
			monthOut = "November";
			break;
		case "Dec":
			monthOut = "December";
			break;
		default:
			break;
	}
	return monthOut;
}
function makeMonthString(month)
{
	monthString = "";
	switch (month)
	{
		case "01":
			monthString = "January";
			break;
		case "02":
			monthString = "February";
			break;
		case "03":
			monthString = "March";
			break;
		case "04":
			monthString = "April";
			break;
		case "05":
			monthString = "May";
			break;
		case "06":
			monthString = "June";
			break;
		case "07":
			monthString = "July";
			break;
		case "08":
			monthString = "August";
			break;
		case "09":
			monthString = "September";
			break;
		case "10":
			monthString = "October";
			break;
		case "11":
			monthString = "November";
			break;
		case "12":
			monthString = "December";
			break;
		default:
			break;
	}
	return monthString;
}
function getNumOfDaysInMonth(month, year)
{
	// Do a quick sanity check on the month/year
	if ( ((month < 1) || (month > 12)) || (year == 0) )
	{
		return -1;
	}
	
	// Calculate february's number of days based off year
	var february_days = 28;
	if (month == 2)
	{	
		// check for a leap year every 4 years not every 100th year
		// but still every 400th year
		if (((year%4) == 0) && (!(year%100 == 0) || (year%400 == 0))) {
			february_days = 29;
		}
	}
	switch(month)
	{
		// April, September, June, and November
		case 9: case 4: case 6: case 11:
			return 30;
			break;
		// February
		case 2: 
			return february_days;
			break;
		// All other months have 31 days
		default:
			return 31;
			break;
	}
	// We should have already returned
	return -1;
}
function fixEventDate(eventDate)
{
	evtDateParts = eventDate.split(", ");
	if (evtDateParts.length >= 3)
	{
		eventDayOfWeek = evtDateParts[0];
		eventMonthDay = evtDateParts[1];
		eventYear = evtDateParts[2];
		
		if (eventMonthDay)
		{
			eventMonthDayParts = eventMonthDay.split(" ");
			if (eventMonthDayParts.length >= 2)
			{
				eventMonth = fixMonth(eventMonthDayParts[0]);
				eventDay = eventMonthDayParts[1];
				eventDate = eventDayOfWeek + ", " + eventMonth + " " + makeDay(eventDay) + ", " + eventYear;
			}
		}
	}
	return eventDate;
}
function fixInternationalDate(eventDate)
{
	evtString = "";
	evtDateParts = eventDate.split("-");
	if (evtDateParts.length >= 3)
	{
		evtYear = evtDateParts[0];
		evtMonth = evtDateParts[1];
		evtDay = evtDateParts[2];
		evtMonthString = makeMonthString(evtMonth);
		evtDayString = makeDay(parseInt(evtDay,10));
		evtString = evtMonthString + " " + evtDayString + ", " + evtYear;
	}
	return evtString;
}
function condenseSummary(summaryFull, SUMMARY_SIZE)
{
	var index = 0;
	var current = summaryFull.substr((SUMMARY_SIZE+index),1);
	
	while ( !(current == " " || current == "," || current == "." || current ==";" || current ==":") && index < summaryFull.length )
	{
		index++;
		current = summaryFull.substr((SUMMARY_SIZE+index),1);
	}
	
	// Return condensed summarys
	return summaryFull.substr(0,(SUMMARY_SIZE + index));
}

function getResourceByID( resID )
{
	for (var i = 0; i < resourcesArray.length; i++)
	{
		if (resourcesArray[i].id == resID)
		{
			// Return the resource, if we've found a match
			return resourcesArray[i];
		}
	}
	// Return nothing if we've reached the end
	return null;
}

function checkEmail(data) {
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(data);
}

function checkUrl(data) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(data);
}

function xtractFile(data){
	var m = data.match(/(.*)\/([^\/\\]+)(\.\w+)$/);	
	if ((m != null) && (m.length >= 3))
	{
		return {path: m[1], file: m[2], extension: m[3]}
	}
	else
	{
		return "";
	}
}

function getIconType(type)
{
	var iconType = "";
	
	switch (type)
	{
		case ".jpg":case ".png":case ".gif":case ".jpeg":
			iconType = "icon_photo";
			break;
			
		case ".pdf":
			iconType = "icon_pdf";
			break;
			
		case ".mpg":case ".mpeg":case ".avi":case ".wmv":case ".mov":case ".swf":case ".flv":
			iconType = "icon_video";
			break;
	
		case ".mp3":case ".wav":case ".aiff":case ".au":case ".midi":
		case ".wma":case ".mp4":case ".m4p":case ".mpga":case ".snd":
			iconType = "icon_audio";
			break;
		
		default:
			iconType = "icon_info";
			break;
	};
	
	return iconType;
}

function getLinkText( type )
{
	switch (type)
	{
		case "icon_info":
			return "Go to link";
			break;
		case "icon_email":
			return "Email for more info";
			break;
		case "icon_photo":
			return "View photo";
			break;
		case "icon_video":
			return "See video";
			break;
		case "icon_audio":
			return "Listen to audio";
			break;
		case "icon_pdf":
			return "View PDF";
			break;
		default:
			return "Go to link";
			break;
	};
}

function fixMedia( media )
{
	var mediaFixed = "";
	
	var rootPath = "/home/4/b/d/27430/27430/public_html/test/";
	var start = rootPath.length;
	var regexp = /\/home\/4\/b\/d\/27430\/27430\/public_html\/test\//
	
	if (regexp.test(media))
	{
		mediaFixed = media.substr(start,media.length - (start - 1));
	}
	else
	{
		mediaFixed = media;
	}
	return mediaFixed;
}

function getMediaInfo( media )
{	
	var mediaInfo = "";
	
	if (media.length > 0) 
	{
		var iconType="";
		var isEmail = false;
		var isLink = false;
	 	var parsedMedia = [];
		
		if (checkEmail(media))
		{
			isEmail = true;
			iconType = "icon_email";
		}
		else if (checkUrl(media))
		{
			isLink = true;
			iconType = "icon_info";
		}
		else
		{
			// Extract the file path information
			parsedMedia = xtractFile(media);
			
			if (parsedMedia.length != 0)
			{
				isLink = true;
				iconType = getIconType(parsedMedia.extension);
			}
		}
		
		mediaInfo = "<img src=\"images/" + iconType + ".png\" width=\"25\" height=\"25\" alt=\"" 
			+ iconType + "\" border=\"0\" align=\"absmiddle\" />&nbsp<a href=\"" + (isEmail ? "mailto:" : "") 
			+ fixMedia(media) + "\">" + getLinkText(iconType) + "</a>";
	} 
	else 
	{ 
		mediaInfo = ""; 
	}
	
	return mediaInfo;
}

function showResource( resID )
{
	var res = getResourceByID( resID );
	var htmlOutput = "";
	
	var media1 = getMediaInfo(res.media);
	var media2 = getMediaInfo(res.mediaAlt);
	
	// Only form the content when we have a resource item
	if (res != null)
	{
		htmlOutput += "<h3>" + res.title + "</h3>"
		if (res.summary.length > 0)
		{
			htmlOutput += "<h4>Summary:</h4><p>" + res.summary + "</p>";
		}
		if (res.category.length > 0)
		{
			htmlOutput += "<h4>Category:</h4><p>" + cleanForDisplay(res.category) + "</p>";
		}
		if (res.date.length > 0)
		{
			htmlOutput += "<h4>Date:</h4><p>" + res.date + "</p>";
		}
		if (res.type.length > 0)
		{
			htmlOutput += "<h4>Type:</h4><p>"	+ cleanForDisplay(res.type) + "</p>";
		}
		if (res.author.length > 0)
		{
			htmlOutput += "<h4>Author:</h4><p>" + res.author + "</p>";
		}
		if (media1.length > 0)
		{
			htmlOutput += "<h4>Media:</h4><p style=\"vertical-align: top;\">" + media1;
		}
		if (media2.length > 0)
		{
			if (media1.length > 0)
			{
				htmlOutput += "<br />" + media2 + "</p>";
			}
			else
			{
				htmlOutput += "<h4>Media:</h4><p style=\"vertical-align: top;\">" + media2 + "</p>";
			}
		}
		else if (media1.length > 0)
		{
			htmlOutput += "</p>";
		}
	}
	// Only update the element if we have html to put there
	if (htmlOutput.length > 0)
	{
		var element = document.getElementById("resource_item");
		if (element)
		{
			element.innerHTML = htmlOutput;
		}
	}
	return true;
}

function Resource(resID, resType, resCat, resTitle, resDate, resAuthor, resStatus, resMedia, resMediaAlt, resSummary)
{
	this.id = resID;
	this.type = resType;
	this.category = resCat;
	this.title = resTitle;
	this.date = resDate;
	this.author = resAuthor;
	this.status = resStatus;
	this.media = resMedia;
	this.mediaAlt = resMediaAlt;
	this.summary = resSummary;
}

function parseResourceItem(resourceItem)
{
	var resourceType = resourceItem.getElementsByTagName('type')[0].firstChild;
	(resourceType != null) ? (resourceType = resourceType.data) : (resourceType = "");
	
	var resourceID = resourceItem.getElementsByTagName('id')[0].firstChild;
	(resourceID != null) ? (resourceID = resourceID.data) : (resourceID = "");
	
	var resourceCategory = resourceItem.getElementsByTagName('category')[0].firstChild;
	(resourceCategory != null) ? (resourceCategory = resourceCategory.data) : (resourceCategory = "");
	
	var resourceTitle = resourceItem.getElementsByTagName('title')[0].firstChild;
	(resourceTitle != null) ? (resourceTitle = resourceTitle.data) : (resourceTitle = "");
	
	var resourceDate = resourceItem.getElementsByTagName('contentDate')[0].firstChild;
	(resourceDate != null) ? (resourceDate = resourceDate.data) : (resourceDate = "");
	
	var resourceAuthor = resourceItem.getElementsByTagName('author')[0].firstChild;
	(resourceAuthor != null) ? (resourceAuthor = resourceAuthor.data) : (resourceAuthor = "");
	
	var resourceStatus = resourceItem.getElementsByTagName('status')[0].firstChild;
	(resourceStatus != null) ? (resourceStatus = resourceStatus.data) : (resourceStatus = "");
	
	var resourceSummary = resourceItem.getElementsByTagName('summary')[0].firstChild;
	(resourceSummary != null) ? (resourceSummary = resourceSummary.data) : (resourceSummary = "");

	var resourceMedia = resourceItem.getElementsByTagName('media')[0].firstChild;
	(resourceMedia != null) ? (resourceMedia = resourceMedia.data) : (resourceMedia = "");

	var resourceMediaAlt = resourceItem.getElementsByTagName('mediaAlternate')[0].firstChild;
	(resourceMediaAlt != null) ? (resourceMediaAlt = resourceMediaAlt.data) : (resourceMediaAlt = "");

	var r = new Resource( resourceID, resourceType, resourceCategory, resourceTitle, resourceDate, 
						  resourceAuthor, resourceStatus, resourceMedia, resourceMediaAlt, resourceSummary);
	
	return r;
}

// Content object
function Feature(title,summary,path)
{
	this.featureTitle = title;
	this.featureSummary = summary;
	this.featureImagePath = path;
}
function isBlank(s)
{
	for ( var i = 0; i < s.length; i++ )
	{
		var c = s.charAt(i);
		if ( (c != ' ') &&
		     (c != '\n') &&
			 (c != '\t') &&
			 (c != '\r') )
		{
			return false;
		}
	}
	return true;
}
function isValidEmail(s)
{
	var bFoundName = false;
	var bFoundAtSymbol = false;
	var bFoundDomain = false;
	var bFoundPeriod = false;
	var bFoundExtension = false;
	
	var bInvalidChar = false;
	
	// Define a starting position
	var lastPos = 0;
	var iPos = 0;
	
	// Look for characters
	for (iPos = 0; iPos < s.length; iPos++)
	{
		// Record the current character
		var c = s.charAt(iPos);
		
		// If we don't have an at symbol yet, look for valid chars
		if ( c != '@' )
		{
			if ( ( c >= 'a' && c <= 'z' ) || // Look for lower case characters
			     ( c >= 'A' && c <= 'Z' ) || // Look for upper case characters
				 ( c >= '0' && c <= '9' ) || // Look for numerals
				 ( c == '!' || c == '$' || c == '\'' || 
				   c == '*' || c == '+' || c == '-' || 
				   c == '/' || c == '=' || c == '?' || 
				   c == '^' || c == '_' || c == '`' || 
				   c == '{' || c == '}' || c == '|' || 
				   c == '~' ) || // Look for these special chars
				   ( iPos != 0 && iPos != (s.length - 1) &&  c == '.' ) // Period not first or last
			   )
			{
				bFoundName = true;
			}
			else
			{
				bInvalidChar = true;
				break;
			}
		}
		else
		{
			if (iPos > 0)
			{
				bFoundAtSymbol = true;
				lastPos = iPos;
			}
			break;
		}
	}
	// Look for characters after @ symbol
	if (bFoundName && bFoundAtSymbol)
	{
		// Increment past the symbol
		++lastPos;
		// Loop through the rest
		for (iPos = lastPos; iPos < s.length; iPos++)
		{
			// Record the current character
			var c = s.charAt(iPos);
			
			if ( ( c >= 'a' && c <= 'z' ) || // Look for lower case characters
			     ( c >= 'A' && c <= 'Z' ) || // Look for upper case characters
				 ( c >= '0' && c <= '9' ) || // Look for numerals
				 ( iPos != 0 && iPos != (s.length - 1) &&  c == '.' ) || // Period not first or last
				 ( iPos != 0 && iPos != (s.length - 1) &&  c == '-' ) // Hyphen not first or last
			   )
			{
				bFoundDomain = true;
				
				if (iPos > lastPos)
				{
					if (c == '.')
					{
						bFoundPeriod = true;
					}				
				}
				if (bFoundPeriod && c != '.')
				{
					bFoundExtension = true;
				}
			}
			else
			{
				bInvalidChar = true;
				break;
			}
		}
	}
	// Check that we met all conditions
	if ( 
	     bFoundName && bFoundAtSymbol && 
	     bFoundDomain && bFoundPeriod && 
		 bFoundExtension && !bInvalidChar
	   )
	{
		return true;
	}	
	return false;
}
function isResponseValid(doc)
{
	var root = doc.documentElement;
	var xml_response;
	var data_element;
	var noErrorFound = true;
	// Get the root node's first child
	if (root.hasChildNodes())
	{
		// Assign the child nodes as xml response
		xml_response = root.childNodes;
	}
	// Look at only the first node
	if (xml_response[0].hasChildNodes())
	{
		// Get the nodes contained in the <xmlresponse>
		data_element = xml_response[0].childNodes;
		
		for (var i = 0; i < data_element.length || !noErrorFound; i++)
		{
			// Check for the node <error>
			if ( data_element[i].nodeName == "error" )
			{
				// We found at least one error while processing response
				noErrorFound = false;
			}
		}
	}
	return noErrorFound;
}

function isValidPhone(phone)
{
    // Check for correct phone number
    rePhoneNumber = new RegExp(/^\(*[1-9]\d{2}\)*\s*\-*\.*\s*\d{3}\-*\.*\d{4}$/);

    if (!rePhoneNumber.test(phone)) {
         return false;
    }

	return true;
}
//-->