function clearBox(input) {
	if(input.value==input.defaultValue) {
		input.value = '';	
	}
}

function resetBox(input) {
	if(!input.value) {
		input.value = input.defaultValue;	
	}
}

function validateEmail(email){
	var myEmail = email.value;

	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myEmail))){
		alert('Please enter a valid email address');
		return false;
	}
	return true;
}

// Search postcode-to-coords
function submitForm() {
	var geocoder = null;
	var form_id = '#search_form';
	var postcode = $('#search_form input[name=search\[postcode\]]').val();
	var latitude_field = "#search_form input[name=search\[latitude\]]";
	var longitude_field = "#search_form input[name=search\[longitude\]]";
	
	if(postcode == 'Postcode') {
		return true;
	} 
	else {
		if (GBrowserIsCompatible()) {
			geocoder = new GClientGeocoder();	
			
			if(postcode.toLowerCase()=="w1") postcode = "W1D";
			if(postcode.toLowerCase()=="ec") postcode = "EC1V";
			if(postcode.toLowerCase()=="ec1") postcode = "EC1V";
			if(postcode.toLowerCase()=="ec4") postcode = "EC4M";
			if(postcode.toLowerCase()=="wc1") postcode = "WC1B";
			if(postcode.toLowerCase()=="wc2") postcode = "WC2H";	
			if(postcode.toLowerCase()=="sw1") postcode = "SW1Y";	
			
			geocoder.getLatLng(postcode+',UK', function(point) {
				
				if (!point) {
					alert("The post code entered could not be found");
				} 
				else {
					var latitude = point.lat();
					var longitude = point.lng();
					$(latitude_field).val(latitude);
					$(longitude_field).val(longitude);
					$(form_id).submit();
				}
			});				
		}
	}
	
	return false;
}

$( function() {
	$('#showAdvanced').click(function() {
		$('#advancedOptions').slideToggle(500);
		return false;
	});

	$('#unsubscribe').click( function() {
		var decision = confirm("Are you sure you would like to unsubscribe to all job alerts?");
		if(decision == true) return true;
		else return false;
	});

	$('#toggleRegions').click( function() {
		var is_checked = $(this).attr('checked');
		var regions_select = $(this).parent().parent().children('select');
		if(is_checked === true) {
			/* CHECKED */
			regions_select.attr('disabled', 'disabled');
			regions_select.children().attr("selected", "selected");
			$('#allRegions').val('all');
		}
		else {
			/* UNCHECKED */	
			regions_select.removeAttr('disabled');
			regions_select.children().attr("selected", "");
			$('#allRegions').val('');
		}
	});

	$('#evoluted img').css( "opacity", 0.2 );
	$('a#evoluted')
		.mouseover(function() {
												$(this).children().stop().animate( {opacity: 1}, 750 ) 
												})
		.mouseout(function() { 
												$(this).children().stop().animate( {opacity: 0.2}, 750 ) 
												});	
	fadeGallery('div.fade-gallery');
});

/*--- fade gallery ---*/
function fadeGallery(rotate){
	var stay_time = 7500; //in ms
	var change_speed = 600; //in ms
	var container = rotate; // default - 'div.fade-gallery'
	
	$(container).each(function(){
		var _hold = $(this);
		var _f = true;
		var _t;
		var _list = _hold.find('li');
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		if(_list.length > 1){
			if(_f && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time);
			}
		}
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_list.eq(_a).removeClass('active').animate({opacity: 0},{ queue:false, duration: change_speed});
				_list.eq(_ind).addClass('active').animate({opacity: 1},{ queue:false, duration: change_speed});
				_a = _ind;
			}
			if(_f && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time+change_speed);
			}
		}
	});
}

/* Zest Business Group Homepage */
/* Update the occupations select box depending what site is selected */
function updateOccupations(site_id) {
	var id = site_id;
	$.get("/ajax/updateOccupations.php", { "id" : id }, function(data) {
			$("#selectOccupation").children().remove();
			$("#selectOccupation").append(data);
		});
}

/* Verify search parameters have been entered */
function verifySearch(items) {
	var sector = $('#groupSearch #selectSector').val();
	var occupation = $('#groupSearch #selectOccupation').val();
	var region = $('#groupSearch #selectRegion').val();
	var error = '';
	
	if(sector == '' || !sector) error = 'Please select a job sector\r\n';
	if(occupation == '' || !occupation) error += 'Please select an occupation\r\n';
	if(region == '' || !region || region == 0) error += 'Please select a region';
	
	if(error != '') {
		alert('The following errors occured with your search request:\r\n'+error);
		return false;
	}
	return true;
}
