// FUNCTIONS TO CONTROL GOOGLE MAP DISPLAY ON PAGE
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
String.prototype.toTitleCase = function() { 
	var ls = this.toLowerCase();
	var la = ls.split(' ');
	for (var i=0; i<la.length; i++) la[i] = la[i].charAt(0).toUpperCase()+la[i].slice(1);
	return la.join(' ');
}

// Add encodeURIComponent and decodeURIComponent capability for old browsers (e.g. Win IE 5.0, Mac IE 5.x). Uses escape/unescape and converts '+' to/from '%2B'
if (!window.encodeURIComponent) encodeURIComponent = function (s) { return escape(s).replace(/\+/, '%2B'); };
if (!window.decodeURIComponent) decodeURIComponent = function (s) { return unescape(s.replace(/%2B/, '+')); };

// Native XMLHttpRequest (AJAX) object for IE
if (!window.XMLHttpRequest && window.ActiveXObject) {
	window.XMLHttpRequest = function() {
		try { return new ActiveXObject('Microsoft.XMLHTTP'); } 
		catch (e) { }
		return null;
	};
}

function classAdd (element, theclass) {
	if (!element) return;
	if (!element.className) element.className = '';
	var reg = new RegExp('(^| )'+theclass+'( |$)', 'g');
	if (element.className.search(reg) == -1) element.className = (element.className + ' ' + theclass).trim();
}

function classRemove (element, theclass) {
	if (!element) return;
	if (!element.className) return;
	var reg = new RegExp('(^| )'+theclass+'( |$)', 'g');
	element.className = element.className.replace(reg, ' ').trim();
}

function classExists (element, theclass) {
	if (!element) return false;
	if (!element.className) return false;
	var reg = new RegExp('(^| )'+theclass+'( |$)', 'g');
	return reg.test(element.className);
}


function resize16x9 (element) {
	if (typeof element == 'undefined') return;
	if (typeof element == 'string') {
		if (element = document.getElementById(element));
		else return;
	}
	width = element.clientWidth;
	height = Math.round((width/16)*9);
	element.style.height = height+'px';
}

/************* MAPPING ***************/

function showMap (insertafter, width, latitude, longitude, caption) {
	if (!insertafter) return true;
	if (!width) var width = '100%';
	if (!latitude) return true;
	if (!longitude) return true;
	if (!caption) var caption = '';
	if (insertafter.nextSibling != null && classExists(insertafter.nextSibling, 'map')) { 
		if (insertafter != null) classRemove(insertafter, 'active');
		insertafter.nextSibling.parentNode.removeChild(insertafter.nextSibling);
	}
	else {
		classAdd(insertafter, 'active');
		var div = document.createElement('DIV');
		div.className = 'map';
		div.style.width = width;
		div.style.height = '1px';
		insertafter.parentNode.insertBefore(div, insertafter.nextSibling);
		resize16x9(div);
		div.innerHTML = ('<iframe src="http://www.biosecuritybank.com/includes/map.htm?latitude='+encodeURIComponent(latitude)+'&amp;longitude='+encodeURIComponent(longitude)+'&amp;caption='+encodeURIComponent(caption)+'" width="100%" height="100%" frameborder="0" scrolling="no"></iframe>');
	}
	if (insertafter.blur) insertafter.blur(); 
	return false;
}
