// Standard Watershed Javascript include file with basic, commonly used
// functions



function addEvent(elementID, eventType, callback, useCapture) {
    // cross-browser event assigner for IE5+, NS6+ and Mozilla/Gecko
    // By Scott Andrew
    //
    // elementID: the ID of the element to attach the listener to;
    // eventType: the type of event to be notified of - such as load, mousedown, click;
    // callback: the function reference to be called when the event happenss
    // useCapture: prevent event propogation
    
    if (elementID.addEventListener) {
        // Most browsers except IE
        elementID.addEventListener(eventType, callback, useCapture);
        return true;
    } else if (elementID.attachEvent) {
        // Used by IE
        var result = elementID.attachEvent('on' + eventType, callback);
        return result;
    } else {
        // IE5 Macintosh
        elementID['on' + eventType] = callback;
    }
}


function getElementsByClass(searchClass, node, tag) {
    // Returns an array of elements found with the class name
    //
    // searchClass: a string of the class name
    // node: optional - the DOM node to search in
    // tag: optional - the type of tag to return. passing null returns all
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}



function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}


function watershedTimeFormattedStringForDate(aDate) {
    // Takes a JS Date and returns the local time formatted as 0800
    hourString = aDate.getHours() + '';
    if (hourString.length == 1) hourString = "0" + hourString;   
    minuteString = aDate.getMinutes() + '';
    if (minuteString.length == 1) minuteString = "0" + minuteString;
    theTime =  hourString + minuteString;
    return theTime;
}



function truncateString(aString, maxCharacters) {
    // Truncates a string, if necessary, to a certain amount of
    // characters and adds an elipsis. Will shorten the String to 
    // the nearest word
    //
    // aString: the string to truncate
    // maxCharacters: the maximum number of characters as an int
    if (!aString) return "";
    if (maxCharacters < 3) return "";
    if (aString.length < maxCharacters) return aString;
    
    truncatedString = "";
    words = aString.split(" ");
    
    if (words.length == 1) return aString.substring(0, maxCharacters) + "&#8230;";
    for (i=0; i < words.length; i++) {
        // See if we can add the new word
        if (truncatedString.length + words[i].length + 1 > maxCharacters) {
            // the string would be too long
            return truncatedString + "&#8230;";
        } else {
            // add the word
            truncatedString = truncatedString + " " + words[i];
        }
    }


        return truncatedString + "&#8230;";

}

function capitalizeString(str_sentence)
{
    return str_sentence.replace(/\b[a-z]/g, convertToUpper);
    //return str_sentence.toLowerCase().replace(/\b[a-z]/g, convertToUpper);
    function convertToUpper()
    {
        return arguments[0].toUpperCase();
    }
}






