  function attach_a_file(project_id, attachable_type, attachable_id)
  {
	var name = 'attachments_' + attachable_type + '_' + attachable_id;
	var attachment_div = $(name);
	if (attachment_div.style.display == 'none')
	{
		//run the ajax request to get the topics:
		new Ajax.Updater(name, '/projects/'+project_id+'/files?attachable_type=' + attachable_type + '&attachable_id=' + attachable_id,{
				method:'get',
				onSuccess:function(){
				  new Effect.Appear(name,{duration:0.2});
				}
			});
	}
	else
	{
	   new Effect.Fade(name,{duration:0.2});
	}
  }

  function attach(project_id,file_upload_id, attachable_type, attachable_id, ticked)
  {
	if (ticked)
	{
		new Ajax.Request('/projects/'+project_id+'/files/attach?file_upload_id=' + file_upload_id + '&attachable_type=' + attachable_type + '&attachable_id=' + attachable_id + '&attach=' + ticked,{
			method:'post'
		}); 
	}
	else
	{
		new Ajax.Request('/projects/'+project_id+'/files/unattach?file_upload_id=' + file_upload_id + '&attachable_type=' + attachable_type + '&attachable_id=' + attachable_id + '&attach=' + ticked,{
			method:'delete'
		}); 
	}
  }
  
  function checkLength(field, maxlimit, counterfield) 
  {
    if ($(field).value.length > maxlimit) 
      // if too long...trim it!
      $(field).value = $(field).value.substring(0, maxlimit);
    // otherwise, update 'characters left' counter
    else
      $(counterfield).value = (maxlimit - $(field).value.length) + ' chrs left';
  }
  
  function set_watermark(project_id, unit_id, topic_id, image_src)
  {
  	new Ajax.Request('/projects/' + project_id + '/units/'+unit_id+'/topics/' + topic_id + '/set_watermark?watermark=' + image_src,
		{
			method:'put',
			onSuccess:function(){
			}
		});
	var iframe = get_iframe_document('shred_iframe');
	//alert(iframe.body.innerHTML);
	var organise_div = iframe.getElementById('organise_shreds');
	//alert(organise_div);
	organise_div.setStyle({'background':"url('/images/units/watermarks/"+image_src+"')"});
  }
	function get_iframe_document( iframe_name )
	{
		var iframe = $(iframe_name);
		//alert(iframe);
		if (iframe.document)
		{
			return iframe.document;
		}
		else
		{
			return iframe.contentDocument;
		}
	
	}
	function sanitize_text_field(textfield,display_span_id)
	{
	    var new_str = ''
		var textfield_str = textfield.value;
		//go through each character in the string and make sure it's a letter:
		for (i=0; i < textfield_str.length; i++)
		{
			if (isAlphabet(textfield_str[i]))
			{
				new_str += textfield_str[i].toLowerCase();
			}
		}
		textfield.value = new_str;
		$(display_span_id).update(new_str);
	}
	function isAlphabet(str)
	{
		var alphaExp = /^[a-zA-Z0-9_-]+$/;
		if(str.match(alphaExp)){
			return true;
		}else{
			return false;
		}
	}
	
  function edit_status(icon, statusable_type, statusable_id)
  {	
	icon.toggle();
	current_status = icon.name;
	var form_html = '<form method="post" id="status_form_'+statusable_type+'_'+statusable_id+'" onsubmit="save_status(this,\''+statusable_type+'\',\''+statusable_id+'\');return false;">' +
	  '<input type="text" id="status_title_'+statusable_type+'_'+statusable_id+'" name="status[title]" value="' + current_status + '" class="status_textfield" /></form>';
	$('status_div_' + statusable_type + '_' + statusable_id).update(form_html);
	$('status_title_' + statusable_type + '_' + statusable_id).focus();
	$('status_title_' + statusable_type + '_' + statusable_id).select();
  }
  function save_status(form, statusable_type, statusable_id)
  {
	new Ajax.Updater('status_div_' + statusable_type + '_' + statusable_id,'/statuses/?statusable_type=' + statusable_type + '&statusable_id=' + statusable_id,
	  {
		method:'post',
		parameters:Form.serialize(form),
		onLoading:function(){
		  $('status_title_'+statusable_type+'_'+statusable_id).blur();
		  $('status_title_'+statusable_type+'_'+statusable_id).disable();
		},
		onSuccess:function(){
		  $('status_div_' + statusable_type + '_' + statusable_id).update('');
		}
	  });
  }
  
  //These TWO functions NOT YET in use
  //May be used by app/views/applications/show 
  //          and  applications_helper - "field_edit" method
  function edit_field(field_to_edit, field_value, enrolment_application, enrolment_application_id)
  {	
	var form_html = '<form method="put" id="field_edit_form_'+enrolment_application_id+'" onsubmit="save_field_data(this,\''+enrolment_application+'\',\''+enrolment_application_id+'\');return false;">' +
	  '<input type="text" id="field_contents_'+enrolment_application_id+'" name="enrolment_application['+field_to_edit+']" value="' + field_value + '" class="enrolment_application_textfield" /></form>';
	alert(form_html);
	$('enrolment_application_field_div_' + enrolment_application_id).update(form_html);
	//$('application_field_title_'+ enrolment_application_id).focus();
	//$('application_field_title_'+ enrolment_application_id).select();
  }
  function save_field_data(form, enrolment_application, enrolment_application_id)
  {
	new Ajax.Updater('enrolment_application_field_div_' + enrolment_application_id,'/applications/?application_id=' + enrolment_application_id,
	  {
		method:'post',
		parameters:Form.serialize(form),
		onLoading:function(){
		  //$('application_field_title_'+ enrolment_application_id).blur();
		  //$('application_field_title_'+ enrolment_application_id).disable();
		},
		onSuccess:function(){
		  $('enrolment_application_field_div_' + enrolment_application_id).update('');
		}
	  });
  }

  function showAddress()
  {
    $('q').blur();
    var address = $F('q');
    new Ajax.Updater('search_results', '/agencies/search/' + address, {method:'get'});
    var geocoder = new GClientGeocoder();
    geocoder.getLatLng(address, function(point) {
      if (!point) {
      } else {
        map.setCenter(point, 10);
      }});
    return false;
  }
  
  function lo(marker, agency_id){
    //get the agency profile and load it into an overlay
    marker.openInfoWindowHtml('<h3>Loading profile...</h3>');
    new Ajax.Request('/agencies/' + agency_id + '/map_overlay', {
      method: 'get',
      onComplete: function(response){
        marker.openInfoWindowHtml(response.responseText);
      }
    });
  }