

/*This is my javascript trim function.  It is used mainly to trim the latitude and longitude coordinates */
function trim(str)
{
	// left
	str = str.replace(/^\s+/,"");
	// right
	str = str.replace(/\s+$/,"");
	
	return str;
}

function addNonBreakSpaces(str)
{
	while(str.indexOf(" ") > -1)
	{
		str = str.replace(" ", "&nbsp;");
	}
	return str;
}

function findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
	    while(1) 
	    {
	      curleft += obj.offsetLeft;
	      if(!obj.offsetParent)
	        break;
	      obj = obj.offsetParent;
	    }
	else if(obj.x)
	    curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
	    while(1)
	    {
	      curtop += obj.offsetTop;
	      if(!obj.offsetParent)
	        break;
	      obj = obj.offsetParent;
	    }
	else if(obj.y)
	    curtop += obj.y;
	return curtop;
}
  
function CreateValidator(id)
{
	var valid = new Validation(id, {
		onSubmit: false,
		useTitles: true
	});
	return valid;
}

function saveRoute()
{
	if (CreateValidator('save_route').validate()) {
		new Ajax.Request('mapwalk/save', {
			asynchronous: true,
			evalScripts: true,
			parameters: Form.serialize($('save_route')),
			onSuccess: function(t){
				Modalbox.hide();
			}
		});
	}
}

function sendFeedback()
{
	if (CreateValidator('feedback').validate()) {
		new Ajax.Request('mapwalk/feedback', {
			asynchronous: true,
			evalScripts: true,
			parameters: Form.serialize($('feedback'))
		});
	}
}

function sendContact()
{
	if (CreateValidator('contact_us').validate()) {
		new Ajax.Request('mapwalk/contact', {
			asynchronous: true,
			evalScripts: true,
			parameters: Form.serialize($('contact_us'))
		});
	}
}

function shareRoute()
{ 
	if (CreateValidator('send_email').validate()) {
		new Ajax.Request('/email/send_email', {
			asynchronous: true,
			evalScripts: true,
			parameters: Form.serialize($('send_email'))
		});
		Modalbox.hide();
	}
}


function round(value)
{
	return Math.round(value * 100)/ 100;
}

function removeItem(array, item){
	var i = 0;
	while (i < array.length) {
		if (array[i] == item) {
			array.splice(i, 1);
		}
		else {
			i++;
		}
	}
	return array;
}

function showPointer(message, target, offset){
		offset = typeof offset == "object" && offset.length == 2 ? offset : [-5, -5];
		var target = $(target);
		var pointer = new Element("div", {
			id: "pointer"
		}).update([message, "<a href='#' onclick='return false' class='pointer-close'>x</a>"].join(""));
		
		var arrow = new Element("canvas", {
			id: "pointer_arrow",
			height: "23",
			width: "23"
		});
		pointer.setStyle({
			display: "none",
			left: [target.cumulativeOffset()[0] + target.getDimensions().width + (15 + offset[0]), "px"].join(""),
			top: [target.cumulativeOffset()[1] + target.getDimensions().height + (15 + offset[1]), "px"].join("")
		});
		arrow.setStyle({
			display: "none",
			left: [target.cumulativeOffset()[0] + target.getDimensions().width + offset[0], "px"].join(""),
			top: [target.cumulativeOffset()[1] + target.getDimensions().height + offset[1], "px"].join("")
		});
		$(document.body).insert(pointer);
		$(document.body).insert(arrow);
		
		
		var ctx = null;
		if (typeof window.G_vmlCanvasManager != "undefined") {
			ctx = window.G_vmlCanvasManager.initElement(arrow).getContext();
		}
		else {
			ctx = arrow.getContext('2d');
		}
		ctx.beginPath();
		ctx.lineWidth = 2;
		ctx.strokeStyle = "rgb(202, 48, 0);"
		ctx.fillStyle = "rgb(255, 225, 216);"
		ctx.moveTo(0, 0);
		ctx.lineTo(23, 16);
		ctx.lineTo(16, 23);
		ctx.lineTo(0, 0);
		ctx.fill();
		ctx.closePath();
		ctx.beginPath();
		ctx.moveTo(0, 0);
		ctx.lineTo(23, 16);
		ctx.stroke();
		ctx.closePath();
		ctx.beginPath();
		ctx.moveTo(0, 0);
		ctx.lineTo(16, 23);
		ctx.stroke();
		ctx.closePath();
		
		pointer.appear({
			duration: 2,
			to: .8
		});
		arrow.appear({
			duration: 2
		});
		pointer.select("a.pointer-close").first().observe("click", this.closePointer);
	}
	
	function closePointer(){
		if ($('pointer') == null) 
			return;
		$('pointer').fade({
			afterFinish: function(e){
				e.element.remove();
			}
		});
		$('pointer_arrow').fade({
			afterFinish: function(e){
				e.element.remove();
			}
		});
	}
	
	function addTabIndex(id){
		var tabIndex = 1;
		var formElements = $(id).getElements();
		//console.log
		for (var i = 0; i < formElements.length; i++) {
			if (formElements[i].type != "hidden" && formElements[i].style.display != "none" && formElements[i].style.visibility != "hidden") 
				formElements[i].setAttribute("tabindex", tabIndex + i);
		}
		
		$(id).select("div.button a").first().setAttribute("tabindex", formElements.length + 1);
		
		for (var i = 0; i < formElements.length; i++) {
			if (!formElements[i].readOnly && formElements[i].type != "hidden" && formElements[i].style.display != "none" && formElements[i].style.visibility != "hidden") {
				formElements[i].focus();
				return;
			}
		}
	}
    