function replaceText(text, textarea) {
	//IE support 
	if (document.selection) {	
		textarea.focus();
		sel = document.selection.createRange(); 
		sel.text = text; 
		textarea.focus();
	}	
	//Mozilla/Firefox/Netscape 7+ support 
	else if (textarea.selectionStart || textarea.selectionStart == '0') {	
		var startPos = textarea.selectionStart; 
		var endPos = textarea.selectionEnd; 
		var scrollTop = textarea.scrollTop;
		textarea.value = textarea.value.substring(0, startPos)
		                 + text
						 + textarea.value.substring(endPos, textarea.value.length); 
		textarea.focus();
		textarea.selectionStart = startPos + text.length;
		textarea.selectionEnd = startPos + text.length;
		textarea.scrollTop = scrollTop;
	}
	else { 
		textarea.value += text; 
		textarea.focus();
	}

}

// Remember the current position.
function storeCaret(text)
{
	// Only bother if it will be useful.
	if (typeof(text.createTextRange) != 'undefined')
		text.caretPos = document.selection.createRange().duplicate();
}

	
	/**var IE = document.all?true:false;
	//if (!IE) document.captureEvents(Event.MOUSEMOVE)
	//document.onmousemove = getMouseXY;
	//document.onmouseover = getMouseXY;
	//document.onmouseout = getMouseXY;
	var tempX = 0;
	var tempY = 0;
	function getMouseXY(e) {
		if (IE) { // grab the x-y pos.s if browser is IE
			tempX = event.clientX + document.body.scrollLeft;
			tempY = event.clientY + document.body.scrollTop;
		}
		else {  // grab the x-y pos.s if browser is NS
			if (!e) var e = window.event;

			tempX = e.pageX;
			tempY = e.pageY;
		} 
		if (tempX < 0){tempX = 0;}
		if (tempY < 0){tempY = 0+26;} 
				
		getElement('help_tip').style.top=(tempY)+'px'; 
		getElement('help_tip').style.left=(tempX+1)+'px'; 

		return true;
	} */ //Using Prototype to do it is a better way 
	
	function getElement(aID){ 
     return (document.getElementById) ? document.getElementById(aID)
                                      : document.all[aID];
   } 
   
   
   function on_off(id) {
		
		if (getElement(id).style.display=='none') {
			preview_element=getElement(id);
		 	preview_element.style.display='inline'
		 	//preview_element.style.borderWidth=1;
			//preview_element.background-color: #EFEFEF;
			//preview_element.background-repeat: repeat-x;
			//preview_element.border: 1px solid #7A7777;
    		//background-position: bottom;
		} else {
			getElement(id).style.display='none'; 
		}
		return true;
   }

//*******************PREVIEW**********************
   var preview=false;
   var preview_observer=null;
   var ajax_form_id=null;
   var div_to_replace=null;
   var request_url=null;
   var ajax_field_id=null;
   var params=null;
   var ajax_just_field_flag=null;
   
   function preview_on_off (div_id, form_id, ajax_url, field_id, just_field_flag) {
   	request_url=ajax_url;
   	div_to_replace=div_id;
   	ajax_form_id=form_id;
	ajax_field_id=field_id;
	ajax_just_field_flag=just_field_flag;
   	if (!preview) {
		preview=true;
		if (preview_observer==null) {	
			preview_observer=new Form.Element.Observer(ajax_field_id, 1, ajax_preview);
		}
		ajax_preview();					
	} else {
		preview=false;
	}
	on_off(div_id);
   } 

   function ajax_preview() {
   	if (!preview) return;
	if (ajax_just_field_flag==false) {
		params=Form.serialize(ajax_form_id);			
	} else {
		params=Form.Element.serialize(ajax_field_id);
		params=params.sub(/.*=/, "body=", count = 1);
		//alert("serialized="+params);
		//params="body=" + getElement(ajax_field_id).text;
	}
	new Ajax.Updater(div_to_replace, request_url, {asynchronous:true, evalScripts:true, method: 'post', parameters: params});			
   }
//*******************PREVIEW END**********************

//   var d = new Date();
//   var curr_date = d.getDate();
//   var month = d.getMonth()+1;
//   var year = d.getFullYear();

   
   function updateCalendarNext() {
   	if(month==12) {
		month=1;
		++year;
	} else {
	   ++month;
	}	
	var params="mob_id="+mob_id+"&year="+year+"&month="+month;
   	//pass these two to the call to the action (below)
	hideEventList();
	new Ajax.Updater('calendar_month', '/inline/show_calendar/', {asynchronous:true, evalScripts:true, method: 'get', parameters: params})
   }
   
   function updateCalendarBack() {
   	if(month==1) {
		month=12;
		--year;
	} else {
	   --month;
	}
	var params="mob_id="+mob_id+"&year="+year+"&month="+month;
	hideEventList();
	new Ajax.Updater('calendar_month', '/inline/show_calendar/', {asynchronous:true, evalScripts:true, method: 'get', parameters: params})
   }
   
   var ajaxReqShow='true';
   

   
   function showHelp(m_event, id) {
   	//console.log("showAjaxEffect");
	mouseX=Event.pointerX(m_event);
	mouseY=Event.pointerY(m_event); 
	getElement('help_tip').style.top=(mouseY)+'px'; 
	getElement('help_tip').style.left=(mouseX+1)+'px'; 
   	new Ajax.Updater('help_tip', '/inline/help/'+id, {asynchronous:true, evalScripts:true, onComplete:function(request){new Effect.Appear('help_tip', {duration:0.3});}});
   }
   
   function hideHelp() {
   	pausecomp(1000);
	new Effect.Fade('help_tip', {duration:0.1}) 
	//getElement('help_tip').style.display='none'; //not helping
   }
   
	function showEventList(m_event, date) {
		mouseX=Event.pointerX(m_event);
	    mouseY=Event.pointerY(m_event);
		getElement('event_list').style.top=(mouseY)+'px'; 
	    getElement('event_list').style.left=(mouseX+1)+'px'; 
   		new Ajax.Updater('event_list', '/inline/event_list?mob_id='+mob_id+'&date='+date, {asynchronous:true, evalScripts:true, onComplete:function(request){new Effect.BlindDown('event_list', {duration:0.5});}});
	}
	
	function hideEventList() {
		new Effect.Fold('event_list', {duration:0.3}) 	
	}
	
	function showNearbyActivities(distance, time, lat, lng) {
   		new Ajax.Updater('nearby_activities', '/inline/nearby_activities?distance='+distance+'&time='+time+'&lat='+lat+'&lng='+lng, {asynchronous:true, evalScripts:true});
	}
	
// www.sean.co.uk

	function pausecomp(millis)
	{
		var date = new Date();
		var curDate = null;
		
		do { curDate = new Date(); }
		while(curDate-date < millis);
	}
