
function show_postcomment() { 
	Element.toggle('post_comment');
	// new Effect.Highlight('post_comment');
	document.location.href=("#post_comment_a");
	return true;
}

function popme(link) {
	width = (arguments[1] ? arguments[1] : 400)
	height = (arguments[2] ? arguments[2] : 500)
	append = (arguments[3] ? arguments[3] : "")
	var win = window.open(link.href + append, "popup", "width="+width+",height="+height+",scrollbars=yes,resizable=yes,status=no,menubar=no")
	win.focus();
	return false;
}

function accept_invite() {
	val = document.getElementById("agree_to_tandc").checked;
	
	if (val == true) {
		return true;
	} else {	
		Element.show('tandc');
		return false;
	}
}

function validate_signup_form() {
	if (!document.getElementById("agree_to_tandc").checked) {
		alert("You must agree to the Terms & Conditions to join Sailing Networks.");
		// new Effect.Highlight("tandc");
		return false;
	}
}

function send_message() { 
	Element.show('send_message');
	document.location.href=("#send");
	return false;
}

function add_to_friends(friend_id) {
	var url = '/my/contacts/get_add_friend/'+ friend_id;
    
	var myAjax = new Ajax.Updater('member_'+ friend_id, url, {method: 'get', parameters: ''}); 
	return false;
}

function hide_add_friend(friend_id) {
	$('member_'+ friend_id).innerHTML = "";
}

function friend_add_loading(friend_id) {
	document.getElementById("add_friend_loading_"+ friend_id).style.display = "inline";
}

function friend_add_complete(friend_id, friend_name) {
	var div = document.getElementById("member_"+ friend_id);
	document.getElementById("add_friend_button_"+ friend_id).style.display = "none";
	if(div)
		div.innerHTML = "<h3>"+ friend_name + " has been added to your contacts</h3>";
	
	/*interval = setInterval(function() {Element.toggle("member_"+ friend_id)}, 300);
	clearInterval(interval);*/
}

function add_upload_row(object_name,show_title) {
	var box = $("file_fields");
	var children = child_nodes_using_tag(box, "LI");

	if (children.length < 10) {
		var input = document.createElement('input');
		input.setAttribute("name", object_name +"[photo"+ (child_nodes_using_tag(box, "LI").length + 1) +"]");
		input.setAttribute("type", "file");
		input.setAttribute("size", 20);
		input.setAttribute("class", "upload_field");

		var span = false
		var title = false
		if(show_title)
		{
			span = document.createElement( 'span' );
			span.innerHTML = ' Title ';
			title = document.createElement('input');
			title.setAttribute("name", object_name +"[title"+ (child_nodes_using_tag(box, "LI").length + 1) +"]");
			title.setAttribute("type", "text");
			title.setAttribute("style", "width:120px;");
		}
		
		var para = document.createElement('li');
                para.setAttribute("id", object_name +"_lister"+ (child_nodes_using_tag(box, "LI").length + 1));
		para.appendChild(input);
		if(title)
		{
			para.appendChild(span);
			para.appendChild(title);
		}
		box.appendChild(para);
	}
}

function uix_add_upload_row(object_name,show_title,show_credits) {
	var box = $("file_fields");
	var children = child_nodes_using_tag(box, "ii");

	if (children.length < 10) {
		var input = document.createElement('input');
		input.setAttribute("name", object_name +"[photo"+ (child_nodes_using_tag(box, "LI").length + 1) +"][file]");
		input.setAttribute("type", "file");
		input.setAttribute("size", 20);
		input.setAttribute("class", "upload_field");

		var span = false
		var title = false
		if(show_title)
		{
			span = document.createElement( 'span' );
			span.innerHTML = ' Title ';
			title = document.createElement('input');
			title.setAttribute("name", object_name +"[photo"+ (child_nodes_using_tag(box, "LI").length + 1) +"][title]");
			title.setAttribute("type", "text");
			title.setAttribute("style", "width:120px;");
		}
		var cspan = false
		var ctitle = false
		if(show_credits)
		{
			cspan = document.createElement( 'span' );
			cspan.innerHTML = ' Credits ';
			ctitle = document.createElement('input');
			ctitle.setAttribute("name", object_name +"[photo"+ (child_nodes_using_tag(box, "LI").length + 1) +"][credit]");
			ctitle.setAttribute("type", "text");
			ctitle.setAttribute("style", "width:120px;");
		}
		
		var para = document.createElement('li');
        para.setAttribute("id", object_name +"_lister"+ (child_nodes_using_tag(box, "LI").length + 1));
		para.appendChild(input);
		if(title)
		{
			para.appendChild(span);
			para.appendChild(title);
		}
		if(ctitle)
		{
			para.appendChild(cspan);
			para.appendChild(ctitle);
		}
		box.appendChild(para);
	}
}


function remove_upload_row(object_name) {	
  var box = $("file_fields");
  var children = child_nodes_using_tag(box, "li");
  var dd = $(object_name +"_lister"+children.length);
  box.removeChild(dd);
}

function child_nodes_using_tag(object, tag) {
	elements = new Array();
	for (_iterator=0; _iterator < object.childNodes.length; _iterator++) {
		if (object.childNodes[_iterator].tagName == tag) elements.push(object.childNodes[_iterator])
	}	
	
	return elements;
}

function preview_item() {
	var form = $("edit_form");
	var input = document.createElement('input');
	input.setAttribute("name", "object[preview]");
	input.setAttribute("type", "hidden");
	input.setAttribute("value", 1);

	form.appendChild(input);
	
	return true;
}


var original_link = null;

function show_revealable(caller) {
	var target = caller.parentNode.parentNode;
	if ((max = arguments[1]) > 0)
		for (i=0; i < max; i++)
			target = target.parentNode;

	for (i = 0; i < target.childNodes.length; i++) {
		obj = target.childNodes[i];
		if (obj.className == "revealable") {
			if (obj.style.display == "" || obj.style.display == "none") {
				obj.style.display = "block";
				
				original_link = caller.parentNode.innerHTML;
				caller.parentNode.innerHTML = caller.innerHTML + ' <a href="#" onclick="return hide_revealable(this,'+ max +');" class="close">(close)</a>'
			}
		}
	}
	
	return false;
}

function hide_revealable(caller) {
	var target = caller.parentNode.parentNode;
	if ((max = arguments[1]) > 0)
		for (i=0; i < max; i++)
			target = target.parentNode;

	for (i = 0; i < target.childNodes.length; i++) {
		if (target.childNodes[i].className == "revealable") {
			target.childNodes[i].style.display = "none";
		}
	}
	
	caller.parentNode.innerHTML = original_link;
	return false;
}

function add_section_to_available_list(sender) {
	var value = sender.value;
	if (sender.checked) {
		var text = sender.parentNode.childNodes[2].innerHTML;
		var option = document.createElement("option");
		option.setAttribute("value", value);
		option.text=text;
		$("available_sections").appendChild(option);
	} else {
		var options = $("available_sections").childNodes;
		for (var i=0; i < 	options.length; i++) {
			if (options[i].value == value) {
				$("available_sections").removeChild(options[i]);
			}
		}
	}
}

/* Test in place selection support (ian) */

function setupCategoryEditor(el, url, opturl) {
    var editor=new Ajax.InPlaceEditor(el, url);
    Object.extend(editor, {
        createEditField: function() {
            var text=this.getText();

            var field=document.createElement("select");
            field.name="value";

            this.editField=field;
            this.form.appendChild(this.editField);

            new Ajax.Request(opturl, {
                onSuccess: function(req) {
					var cats = req.responseText.split('\n');
					for(i=0;i<cats.length;i++)
					{
						var bits = cats[i].split('|');
                        var op=document.createElement("option");
                        op.value=bits[0];
                        op.text=bits[1];
                        if(window.ActiveXObject) {
                            field.options.add(op);
                        } else {
                            field.appendChild(op);
                        }
                        // Select the current item
                        if(op.text == text) {
                            field.selectedIndex=idx;
                        }
                    }
                }
                });
        }
    });
}

/* Partial organisation selector support */

function px_get_organisations( objectname, fieldname ) {
	// Create and build ORGANISATIONS list
	var url = '/my/account/px_get_organisations';
	var params = 'type='+$F(fieldname+'_type_id');
	params += '&objectname='+objectname;
	params += '&fieldname='+fieldname;
	var myAjax = new Ajax.Updater(fieldname+'organisations', url, {method: 'get', parameters: params}); 
}

/* Partial location editor support */

function px_add_region( objectname, fieldname )
{
	// -- Create new region and refresh the region list --
	var region_name = $F(fieldname+'create_region');
	var url = '/my/account/px_get_regions';
	var params = 'regionname='+region_name;
	params += '&objectname='+objectname;
	params += '&fieldname='+fieldname;
	params += '&country='+$F(fieldname+'_country_id');
	var myAjax = new Ajax.Updater(fieldname+'regions', url, {method: 'post', parameters: params }); 
	
	// Following $F(...) line shouldn't work, as I can't see how the above ajax call would have finished in time
	// to have updated the DOM with the new _region_id value, but it seems to work ok, so never mind....
	
	// -- Refresh location list --
	var url = '/my/account/px_get_locations';
	var params = 'region='+$F(fieldname+'_region_id');
	params += '&objectname='+objectname;
	params += '&fieldname='+fieldname;
	var myAjax = new Ajax.Updater(fieldname+'locations', url, {method: 'get', parameters: params }); 
}

function px_add_location( objectname, fieldname )
{
	// -- Adds a new location and rebuilds the location list with the new item selecetd --
	var region_name = $F(fieldname+'create_location');
	var url = '/my/account/px_get_locations';
	var params = 'newlocation='+region_name;
	params += '&region_id='+$F(fieldname+'_region_id');
	params += '&fieldname='+fieldname;
	params += '&objectname='+objectname;	
	var myAjax = new Ajax.Updater(fieldname+'locations', url, {method: 'post', parameters: params}); 
}

function px_get_countries( objectname, fieldname ) {
	// -- Update countries list --
	px_set_continent( objectname, fieldname, $F(fieldname+'_continent_id'));
	
	// -- Blank regions and locations lists --
	var url = '/my/account/px_get_regions';
	var params = 'objectname='+objectname;
	params += '&fieldname='+fieldname;
	var myAjax = new Ajax.Updater(fieldname+'regions', url, {method: 'get', parameters: params}); 

	var url = '/my/account/px_get_locations';
	var params = 'objectname='+objectname;
	params += '&fieldname='+fieldname;
	new Ajax.Updater(fieldname+'locations', url, {method: 'get', parameters: params}); 	
}

// -------- xlocation partial support --------

function xlocation_get_countries( objectname, must_select, prefix ) {
	
	// -- Show spinners --
	Element.show( objectname+'_'+prefix+'countries_spinner' );
	Element.show( objectname+'_'+prefix+'regions_spinner' );
	Element.show( objectname+'_'+prefix+'locations_spinner' );
	
	// -- Disable selects --
	$( objectname+'['+prefix+'country_id]' ).disabled = true;
	$( objectname+'['+prefix+'region_id]' ).disabled = true;
	$( objectname+'['+prefix+'location_id]' ).disabled = true;
	
	// -- Update countries list --
	var params = 'continent='+$F(objectname+'['+prefix+'continent_id]')+'&objectname='+objectname+'&must_select='+must_select+'&prefix='+prefix;
	new Ajax.Updater(prefix+'countries', '/my/account/xlocation_get_countries', {method: 'get', parameters: params}); 

	// -- Update regions list --
	var params = 'objectname='+objectname+'&must_select='+must_select+'&prefix='+prefix;
	new Ajax.Updater(prefix+'regions', '/my/account/xlocation_get_regions', {method: 'get', parameters: params}); 

	// -- Update locations list --
	var params = 'objectname='+objectname+'&must_select='+must_select+'&prefix='+prefix;
	new Ajax.Updater(prefix+'locations', '/my/account/xlocation_get_locations', {method: 'get', parameters: params}); 
}

function xlocation_get_regions( objectname, must_select, prefix ) {

	// -- Show spinners --
	Element.show( objectname+'_'+prefix+'regions_spinner' );
	Element.show( objectname+'_'+prefix+'locations_spinner' );

	// -- Disable selects --
	$( objectname+'['+prefix+'region_id]' ).disabled = true;
	$( objectname+'['+prefix+'location_id]' ).disabled = true;

	// -- Update regions list --
	var params = 'country='+ $F(objectname+'['+prefix+'country_id]') + '&objectname='+objectname+'&must_select='+must_select+'&prefix='+prefix;
	new Ajax.Updater(prefix+'regions', '/my/account/xlocation_get_regions', {method: 'get', parameters: params}); 
	
	// -- Update locations list --
	var params = 'objectname='+objectname+'&must_select='+must_select+'&prefix='+prefix;
	new Ajax.Updater(prefix+'locations', '/my/account/xlocation_get_locations', {method: 'get', parameters: params}); 
}


function xlocation_get_locations( objectname, must_select, prefix ) {

	var region_id = $F(objectname+'['+prefix+'region_id]');
	if(region_id=='_other')
	{
		Element.show(prefix+'add_region');
	}
	else
	{
		// -- Show spinners --
		Element.show( objectname+'_'+prefix+'locations_spinner' );
		
		// -- Disable selects --
		$( objectname+'['+prefix+'location_id]' ).disabled = true;
				
		Element.hide(prefix+'add_region');
		var params = 'region='+ region_id + '&objectname='+objectname+'&must_select='+must_select+'&prefix='+prefix;
		new Ajax.Updater(prefix+'locations', '/my/account/xlocation_get_locations', {method: 'get', parameters: params}); 
	}
}

function xlocation_changed( objectname, prefix ) {
	var value = $F(objectname+'['+prefix+'location_id]');
	if(value=='_other')
	{
		Element.show(prefix+'add_location');
	}
	else
	{
		Element.hide(prefix+'add_location');
	}
}

function xlocation_add_location( objectname, must_select, prefix )
{
	// -- Adds a new location and rebuilds the location list with the new item selecetd --
	
	// -- Show spinners --
	Element.show( objectname+'_'+prefix+'locations_spinner' );
	
	// -- Disable bits --
	$( objectname+'['+prefix+'location_id]' ).disabled = true;
	$( prefix+'create_location' ).disabled = true;
	
	var region_name = $F(prefix+'create_location');
	var params = 'newlocation=' + region_name + '&region_id='+$F(objectname+'['+prefix+'region_id]') + '&objectname='+objectname+'&must_select='+must_select+'&prefix='+prefix;
	new Ajax.Updater(prefix+'locations', '/my/account/xlocation_get_locations', {method: 'post', parameters: params}); 
}

function xlocation_add_region( objectname, must_select, prefix )
{
	// -- Create new region and refresh the region list --
	
	// -- Show spinners --
	Element.show( objectname+'_'+prefix+'regions_spinner' );
	Element.show( objectname+'_'+prefix+'locations_spinner' );
	
	// -- Disable bits --
	$( objectname+'['+prefix+'region_id]' ).disabled = true;
	$( prefix+'create_region' ).disabled = true;
	$( objectname+'['+prefix+'location_id]' ).disabled = true;
	$( prefix+'create_location' ).disabled = true;
	
	var region_name = $F(prefix+'create_region');
	var params = 'regionname='+region_name;
	params += '&objectname='+objectname;
	params += '&country='+$F(objectname+'['+prefix+'country_id]')+'&must_select='+must_select+'&prefix='+prefix;
	new Ajax.Updater(prefix+'regions', '/my/account/xlocation_get_regions', {method: 'post', parameters: params }); 
	
	// Following $F(...) line shouldn't work, as I can't see how the above ajax call would have finished in time
	// to have updated the DOM with the new _region_id value, but it seems to work ok, so never mind....
	
	// -- Refresh location list --
	var params = 'region='+$F(objectname+'['+prefix+'region_id]') + '&objectname='+objectname+'&must_select='+must_select+'&prefix='+prefix;
	new Ajax.Updater(prefix+'locations', '/my/account/xlocation_get_locations', {method: 'get', parameters: params }); 
}

// -------- end --------

function px_set_continent( objectname, fieldname, continent_id) {
	// Create and build COUNTRIES list
	var url = '/my/account/px_get_countries';
	var params = 'continent='+continent_id;
	params += '&objectname='+objectname;
	params += '&fieldname='+fieldname;
	var myAjax = new Ajax.Updater(fieldname+'countries', url, {method: 'get', parameters: params}); 
}

function px_get_regions( objectname, fieldname ) {
	px_set_region( objectname, fieldname, $F(fieldname+'_country_id'), 0);
}

function px_set_region( objectname, fieldname, country_id, region_id) {
	// Create and build REGIONS list
	var url = '/my/account/px_get_regions';
	var params = 'country='+ country_id;
	params += '&objectname='+objectname;
	params += '&fieldname='+fieldname;
	if (region_id > 0) params += "&region="+ region_id;
	var myAjax = new Ajax.Updater(fieldname+'regions', url, {method: 'get', parameters: params}); 
}

function px_get_location( objectname, fieldname ) {
	var value = $F(fieldname+'_region_id');
	if(value=='_other')
	{
		Element.show(fieldname+'add_region');
	}
	else
	{
		Element.hide(fieldname+'add_region');
		px_set_location( objectname, fieldname, value, 0);
	}
}

function px_set_location(objectname,fieldname,region_id,location_id) {
	// Create and build LOCATIONS list
	var url = '/my/account/px_get_locations';
	var params = 'region='+ region_id;
	params += '&objectname='+objectname;
	params += '&fieldname='+fieldname;
	if (location_id > 0) params += "&location="+ location_id;
	var myAjax = new Ajax.Updater(fieldname+'locations', url, {method: 'get', parameters: params}); 
}

function px_location_changed( objectname, fieldname ) {
	var value = $F(fieldname);
	if(value=='_other')
	{
		Element.show(fieldname+'add_location');
	}
	else
	{
		Element.hide(fieldname+'add_location');
	}
}


/* Member profile */
function get_regions() {
	set_region($F('member_country_id'), 0);
}

function set_region(country_id, region_id) {
	var url = '/my/account/get_regions';
	var params = 'country='+ country_id
	if (region_id > 0) params += "&region="+ region_id

	var myAjax = new Ajax.Updater('regions', url, {method: 'get', parameters: params}); 
}

function get_positions() {
	count = document.getElementById('interest_count').value;
	checked = Array()
	for (i=0; i < count; i++) {
	    box = document.getElementById('member_interests_'+i);
	    if (box.checked) {
	        checked.push(box.value)
	    }
	}
	
	set_positions(checked.toString())
}

function set_positions(interest_ids) {
	var url = '/my/account/get_positions';
	var params = 'interests='+ interest_ids

	var myAjax = new Ajax.Updater('positions', url, {method: 'get', parameters: params}); 
}

function manage_other( fieldname, object )
{
	if(object=='')
		listname = fieldname;
	else
		listname = object+'_'+fieldname;
			
	var i = $(listname).selectedIndex;
	if($(listname).options[i].value=='0')
		Element.show(fieldname+'_otherx');
	else
		Element.hide(fieldname+'_otherx');
}

/* Remove framing scumbags 
if (top.location != location&&!enable_frames) {
  top.location.href = document.location.href;
}
*/

/* Helpers */

function request_loading(name) {
	Element.show(name + '_spinner');
}

function request_loaded(name) { 
	Element.hide(name + '_spinner')
}

function add_complete(name, response_text) { 
	request_loaded(name);
	new Insertion.Top(name + '_list', response_text);

	return false;
}

function remove_complete(name, id) {
	request_loaded(name);
	Element.remove($(name + '_' + id));
}

/* ---- Send to a friend --- */

	function show_send_to_friend()
	{
	    if ($('send_to_friend').style.display == "none")
		{
			$('send_to_non_member').style.display = 'none';
	        Element.show("send_to_friend");
			$( 'send_to_friend_done' ).style.display = 'none';
	    }
	    return false;
	}

	function hide_send_to_friend() {
	    Element.hide("send_to_friend");
	    return false;
	}

	function send_page_loading() {
		$('sending_message').style.display = "inline";
	}

	function send_page_complete() {	
		$('send_to_friend').style.display = 'none';
		$('sending_message').style.display = "none";
		Element.show( 'send_to_friend_done' )
	}

	function to_changed() {
		if($('send_to').options[$('send_to').selectedIndex].value=='')
		{
			Element.show( 'send_to_non_member' )
		}
		else
		{
			$('send_to_non_member').style.display = 'none';
		}
	}


function manage_show( what )
{
	if(what=='all')
	{
		Element.show('boats');
		Element.show('organisations');
		Element.show('classifieds');
		Element.show('lifts');
	}
	else if(what=='boats')
	{
		Element.show('boats');
		Element.hide('organisations');
		Element.hide('classifieds');
		Element.hide('lifts');
	}
	else if(what=='organisations')
	{
		Element.hide('boats');
		Element.show('organisations');
		Element.hide('classifieds');
		Element.hide('lifts');
	}
	else if(what=='classifieds')
	{
		Element.hide('boats');
		Element.hide('organisations');
		Element.show('classifieds');
		Element.hide('lifts');
	}
	else if(what=='lifts')
	{
		Element.hide('boats');
		Element.hide('organisations');
		Element.hide('classifieds');
		Element.show('lifts');
	}
}

	
/* ---- Add to my network ---- */

function open_add_contact()
{
	$('contact_message').disabled = false;
	$('contact_submit').disabled = false;
	$('contact_message').value = '';
	Element.show( 'add_contact_message' );
}

function start_add_contact()
{
	$('contact_message').disabled = true;
	$('contact_submit').disabled = true;
	Element.show( 'add_contact_spinner' )
}

function done_add_contact()
{
	Element.hide( 'add_contact_spinner' )
	Element.hide( 'add_contact_link' )
	close_add_contact();
}

function close_add_contact()
{
	Element.hide( 'add_contact_message' );
}