function editallInputCount (event) {
	
	var inp = Event.element(event);
	var remaining = $(inp).next(1).down('span');
	var maxLength = inp.readAttribute('maxlength');
	if (inp.value.length>maxLength*1) inp.value=inp.value.substring(0,maxLength*1);
	var remainingValue = maxLength-inp.value.length;
	remaining.update(remainingValue.toString());
}


document.observe("dom:loaded", function() {

	if($('all_matchboxes')) {
		$('all_matchboxes').observe('click', function(event){
		var checkboxes = [];
		checkboxes = $$('.matchbox_checkbox').each(function(e){ 
													if(e.type == 'checkbox') 
													checkboxes.push(e) 
												});

			if($F('all_matchboxes')=="on") {
				checkboxes.each(function(e){ e.checked = 1 });
			} else {
				checkboxes.each(function(e){ e.checked = 0 });
			};
		})
	}

	// Save search
 	if ($('savesearch')) {

		$('savesearch').observe('submit', function(event){
	
			if ($F('savesearchlegend') == "Give your search a name..." ) { 
				alert('You need to enter a search name ');
				$('savesearchlegend').clear();
				Form.Element.focus('savesearchlegend');
				Event.stop(event);
			} else {

				$('savesearchresponse').update('');
				Event.stop(event);
				var form = Event.element(event);
				$(form).request({
				   onComplete: function(response){
				    $('savesearchresponse').update(response.responseText);
					$('savesearchlegend').value = "Give your search a name...";
					// Now make another call to update saved search options
		  			new Ajax.Updater('searchlist' , '/data/divinematch/scripts/ajax_savesearch.php', { 
					  method: 'get',
					  parameters: {action: 'updateoptions'} 
					});
				   }
				});
			}
		});

		$('savesearchlegend').observe('blur', function(event){
		if ($F('savesearchlegend') == "" ) { 
			$('savesearchlegend').value = "Give your search a name...";
			Event.stop(event);}

		});

		$('savesearchlegend').observe('focus', function(event){
		if ($F('savesearchlegend') == "Give your search a name..." ) { 
			$('savesearchlegend').clear();
			Event.stop(event);}

		});
	}

	if ($('searchlist')) {
		$('searchlist').observe('change', function(event){
			eval("window.location = '/data/divinematch/scripts/savedsearch.php?id="+ $('searchlist').options[$('searchlist').selectedIndex].value+"'");
			Event.stop(event);
		});
	}

// Event booking
 	if ($('eventbooking')) {

		$('eventbooking').observe('submit', function(event){
	
			if ($F('name').length < 1 || $F('address').length < 1 || $F('town').length < 1 || $F('county').length < 1 || $F('postcode').length < 1 || $F('telephone').length < 1 || $F('email').length < 1) { 
				alert('You need to complete all required field');
				Event.stop(event);
			} 
		});
	}

// Contact Form
 	if ($('contact')) {

		$('contact').observe('submit', function(event){
	
			if ($('contact_email') && $F('contact_email').length < 1 ) { 
				alert('Please provide your email address');
				Form.Element.focus('contact_email');
				Event.stop(event);
			} else 
				if ($F('contact_message') == "What\'s on your mind?" || $F('contact_message').length < 1) { 
				alert('You need to enter a message');
				$('contact_message').clear();
				Form.Element.focus('contact_message');
				Event.stop(event);
			}
		});

		if ($F('contact_message') == "" ) { 
			$('contact_message').value = "What\'s on your mind?";
		}

		$('contact_message').observe('blur', function(event){
		if ($F('contact_message') == "" ) { 
			$('contact_message').value = "What\'s on your mind?";
			Event.stop(event);}
		});

		$('contact_message').observe('focus', function(event){
		if ($F('contact_message') == "What\'s on your mind?" ) { 
			$('contact_message').clear();
			}
		});
	}

// Add new photos form
 	if ($('uploadphotos')) {

		textElements = $('uploadphotos').getInputs('text');
		textElements.each( function(currentInput){
			currentInput.observe('focus', function(event){
				if ( currentInput.value == 'write an optional caption...' ){
					currentInput.value = '';
					
				}
				
			});
			currentInput.observe('blur', function(event){
				if ( currentInput.value == '' ){
					currentInput.value = 'write an optional caption...';
				}
				
			});
		});

	}


	// Add new photos form
 	if ($('managephotos')) {

		$$('.editcaptionform').each( function(currentElement){
			currentElement.observe('submit', function(currentElement){
				Event.stop(currentElement);

				var form = Event.element(currentElement);

				myForm = $(this);

				$(form).request({
					method: 'get',
					onSuccess: function(response){
						var theResponse = response.responseText;

						myForm.previous('.caption').down('span').update(theResponse);
						myForm.hide();
						myForm.previous('.caption').show();
					}
				});
			});

			currentElement.hide();
		});


		// EVENT click 'edit caption' | ACTION hide current caption and show caption edit form
		$$('.editcaption').each( function(currentElement){
			currentElement.observe('click', function(currentElement){
				$(this).up(0).previous(0).down('.editcaptionform').show();
				$(this).up(0).previous(0).down('.caption').hide();
				Event.stop(currentElement);
			});
		});

	}

	// Blogs delete confirm
 	if ($$('a.deleteblog')) {
		$$('a.deleteblog').invoke('observe', 'click', function(event) {
		fRet = confirm('Are you sure you want to delete this blog?');
		if (fRet != true){
			Event.stop(event);
		}
		});

	}


	// Find all countable input fields and setup observes to check chars remaining
	$$('input.countable').each(function(inp) {
		Event.observe(inp,'keyup',editallInputCount);
		Event.observe(inp,'blur',editallInputCount);
	});

	// Find all countable textareas and setup observes to check chars remaining
	$$('textarea.countable').each(function(inp) {
		Event.observe(inp,'keyup',editallInputCount);
		Event.observe(inp,'blur',editallInputCount);
	});


	// Check for myprofile the basics
 	if ($('signup_relationship_status')) {

		set_relationship_status($('signup_relationship_status'));
		signup_desired($('signup_relationship_status'));
		

	}


});


signup_desired = function(currentElement){
	currentElement.observe('change', function(currentEvent){
		Event.stop(currentEvent);
		set_relationship_status(currentElement);
	});
}

function set_relationship_status(currentElement) {

	if(currentElement.value=="2" || currentElement.value=="3" || currentElement.value=="4") {
		$('looking_for').innerHTML = "<select name=\"desired_looking_for\" id=\"signup_desired_looking_for\"><option value=\"741\">Looking to make new friends</option></select>";
	} else {
		$('looking_for').innerHTML = "<select name=\"desired_looking_for\" id=\"signup_desired_looking_for\"><option selected=\"selected\" value=\"740\">Looking for relationship possibilities</option>\n<option value=\"741\">Looking to make new friends</option></select>";
	}

}


