﻿/*
	VERSION:
5/27/2008	1.0.2	Added method `isNothing`.
			1.0.1	Added "omitParen" argument to Renderer.phone(strPhone, omitParen).
			1.0.0	Begin versioning.
*/
var Renderer = {
	phone: function(strPhone, omitParen) {
		//if omitParen is true, phone string is formatted with a dash between area code
		var str = (strPhone+"").replace(/\D/gi, ""); //remove non-digit characters
		str = str.replace(/^1/, ""); //remove leading 1
		switch(str.length) {
			case 10 : //standard with area code
				if( !omitParen ) {
					return "(" + str.substr(0, 3) + ") " + str.substr(3, 3) + "-" + str.substr(6, 4);
				} else {
					return str.substr(0, 3) + "-" + str.substr(3, 3) + "-" + str.substr(6, 4);
				}
				break;
			case 7 : // standard without area code
				return str.substr(0, 3) + "-" + str.substr(3, 4);
				break;
			default : 
				if( str.length > 10 ) {
					//assume there is an extension provided
					if( !omitParen ) {
						return "(" + str.substr(0, 3) + ") " + str.substr(3, 3) + "-" + str.substr(6, 4) + " " + str.substr(10, str.length-1);
					} else {
						return str.substr(0, 3) + "-" + str.substr(3, 3) + "-" + str.substr(6, 4) + " " + str.substr(10, str.length-1);
					}
				} else {
					return '';
				}
		} //end switch
	},
	email: function(strEmail, strSubj) {
		var strEmail = strEmail + '';
		if(strEmail.indexOf("@") == -1 || strEmail.indexOf(".") == -1) {
			return strEmail;
		}
		return '<a href="mailto:' + strEmail + 
				(strSubj !== undefined ? '&subject=" + strSubj' : '') + '">' +
				strEmail + '</a>';
	},
	isNothing: function(str, replacement) {
		if(!isNothing(str)) {
			return str;
		}
		return (replacement === undefined ? '' : replacement);
	},
	titleCase: function toTitleCase(str) {
		//capitalizes first letter of word and names that begin with Mc and Mac
		//skips html tags, nbsp, quotes and periods
		if(typeof(str) != 'string') { return ''; }
		var rexp = /(mc|mac|&nbsp;+|\w+\.*\"*\'*\w*|\<\/?\s*\w+\s*\/?\>)/gi;
		return str.replace(rexp, function(txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
	}
	
};
