jQuery.fn.fadeToggle = function(speed, easing, callback) { 
    return this.animate({opacity: 'toggle'}, speed, easing, callback); 
};

function toggleClientLogin() {
	$('#top_nav .get_a_trial_button a').fadeToggle(150);
	$('#client_login_surround').slideToggle(250);
	$('ul#top_nav li.client_login_button a').toggleClass('collapse');

	if ($('ul#top_nav li.client_login_button a').text() == 'Close') {
		$('ul#top_nav li.client_login_button a').text('Client login');
	} else {
		$('ul#top_nav li.client_login_button a').text('Close');
	}

	return false;
}

function checkClientLoginForm() {
	var errors = [];

	if ($('#client_username').val().length < 3) {
		errors.push('Please provide your username.  Your username is at least 3 characters long.');
	}

	if ($('#client_password').val().length < 3) {
		errors.push('Please provide your password.  Your password is at least 6 characters long.');
	}

	if (errors.length > 0) {
		var message = '<p><strong>You need to correct these errors before you can log in:</strong></p><ul>';
		for (var i = 0; i < errors.length; i++) {
			message += '<li>' + errors[i] + '</li>';
		}
		message += '</ul>';
		messageBox(message, null, {title: 'Whoops!'});

		return false;
	} else {
		return true;
	}
}

function checkLoginForm() {
	var errors = [];

	if ($('#username').val().length < 3) {
		errors.push('Please provide your username.  Your username is at least 3 characters long.');
	}

	if ($('#password').val().length < 3) {
		errors.push('Please provide your password.  Your password is at least 6 characters long.');
	}

	if (errors.length > 0) {
		var message = '<p><strong>You need to correct these errors before you can log in:</strong></p><ul>';
		for (var i = 0; i < errors.length; i++) {
			message += '<li>' + errors[i] + '</li>';
		}
		message += '</ul>';
		messageBox(message, null, {title: 'Whoops!'});

		return false;
	} else {
		return true;
	}
}

var currentPageNumber = 1;
var changingPage = false;
function showPage(container, cls, pages, page, duration, numPerGroup, totalResults, numPerPage, callback, callbackName, onlyShowFirst) {
	if (!changingPage) {
		_gat._getTracker()._trackPageview(window.location.href + '#page-' + page);

		changingPage = true;
		//alert('showPage ' + container + '  ' + cls + '  ' + pages + '  ' + page + '  ' + duration + '  ' + numPerGroup);
		if (!numPerGroup || numPerGroup < 1) {
			numPerGroup = 3;
		}
		if (isNaN(parseInt(page))) {
			if (page == 'first') {
				page = 1;
				currentPageNumber = page;
			} else if (page == 'next') {
				if (currentPageNumber < $('#' + container + ' .' + cls).length) {
					currentPageNumber++;
				}
				page = currentPageNumber;
			} else if (page == 'prev') {
				if (currentPageNumber > 0) {
					currentPageNumber--;
				}
				page = currentPageNumber;
			} else if (page == 'last') {
				page = $('#' + container + ' .' + cls).length;
				currentPageNumber = page;
			}
		} else {
			currentPageNumber = page;
		}

		var windowScrollDuration = 150;
		if (duration == undefined) {
			duration = 150;
			windowScrollDuration = duration;
		} else if (duration == 0) {
			windowScrollDuration = 150;
		} else {
			windowScrollDuration = duration;
		}

		if (!numPerPage) {
			numPerPage = 10;
		}

		var current_page_id = $('#' + container + ' .' + cls + '.current_page').attr('id');
		if (!current_page_id) {
			current_page_id = container + '_' + cls + '_1';
		}

		$('#' + container + '_' + cls + '_' + page).addClass('current_page');
		$('#' + container).scrollTo($('#' + current_page_id), {
			duration:0,
			axis:'y'
		});

		$('#' + container).height($('#' + container + '_' + cls + '_' + page).height());
		$('#' + container).scrollTo($('#' + container + '_' + cls + '_' + page), {
			duration:duration,
			axis:'y',
			onAfter:function() {
				$('#' + container).scrollTo(0, 0);
				$('#' + container + ' .' + cls + '.current_page').each(function() {
					if (this.id != container + '_' + cls + '_' + page) {
						$(this).removeClass('current_page');
					}
				});
				changingPage = false;
			}
		});

		if ($(window).scrollTop() > $('#scrollToHere').offset().top) {
			$(window).scrollTo($('#scrollToHere'), {
				duration:windowScrollDuration,
				axis:'y'
			});
		}
		$('div.pagination_' + container).html(generatePagination(container, cls, pages, page, duration, numPerGroup, totalResults, numPerPage, callbackName, onlyShowFirst));

		if (callback) {
			callback(container, cls, pages, page, numPerPage);
		}
	}

	return false;
}

function generatePagination(container, cls, pages, currentPage, duration, numPerGroup, totalResults, numPerPage, callback, onlyShowFirst) {
	//alert('generatePagination ' + container + '  ' + cls + '  ' + pages + '  ' + currentPage + '  ' + duration + '  ' + numPerGroup);
	if (pages == 0) {
		pages = 1;
	}
	if (!numPerGroup || numPerGroup < 1) {
		numPerGroup = 3;
	}
	if (currentPage == undefined || isNaN(currentPage)) {
		currentPage = 1;
	}
	if (duration == undefined) {
		duration = 150;
	}
	if (!numPerPage) {
		numPerPage = 10;
	}
	if (onlyShowFirst != true) {
		onlyShowFirst = false;
	}

	var html = '';
	html += '<span class="paginationButtons">'
			+ '<a style="display:none;" class="pagination_' + container + '_first" href="#" onclick="javascript:return showPage(\'' + container + '\', \'' + cls + '\', ' + pages + ', \'first\', ' + duration + ', ' + numPerGroup + ', ' + totalResults + ', ' + numPerPage + ', ' + callback + ', \'' + callback + '\', ' + (onlyShowFirst ? 'true' : 'false') + ');"><span class="symbol">&#xab; </span>First</a>'
			+ '<a style="display:none;" class="pagination_' + container + '_prev" href="#" onclick="javascript:return showPage(\'' + container + '\', \'' + cls + '\', ' + pages + ', \'prev\', ' + duration + ', ' + numPerGroup + ', ' + totalResults + ', ' + numPerPage + ', ' + callback + ', \'' + callback + '\', ' + (onlyShowFirst ? 'true' : 'false') + ');"><span class="symbol">&#x3c; </span>Prev</a>';

	var firstEllipsisPrinted = false;
	var secondEllipsisPrinted = false;
	for (var i = 1; i <= pages; i++) {
		if (((onlyShowFirst != true && i <= numPerGroup) || (onlyShowFirst == true && i == 1)) || (i >= currentPage - Math.ceil(numPerGroup / 2) && i <= currentPage + Math.ceil(numPerGroup / 2))/* || (i >= pages - (numPerGroup - 1) && i <= pages)*/) {
			html += '<a class="pagination_' + container + '_' + i + (i == currentPage ? ' active' : '') + '" href="#" onclick="javascript:return showPage(\'' + container + '\', \'' + cls + '\', ' + pages + ', ' + i + ', ' + duration + ', ' + numPerGroup + ', ' + totalResults + ', ' + numPerPage + ', ' + callback + ', \'' + callback + '\', ' + (onlyShowFirst ? 'true' : 'false') + '); currentPageNumber = ' + i + '">' + i + '</a>';
		} else if (((onlyShowFirst != true && i > numPerGroup) || (onlyShowFirst == true && i > 1)) && i < currentPage - (numPerGroup - 2) && !firstEllipsisPrinted) {
			html += '<a href="#" class="ellipsis" onclick="javascript:return false;">&#x2026;</a>';
			firstEllipsisPrinted = true;
		} else if (((onlyShowFirst != true && i < pages - (numPerGroup - 1)) || (onlyShowFirst == true && i < pages - 1)) && i > currentPage + (numPerGroup - 2) && !secondEllipsisPrinted) {
			html += '<a href="#" class="ellipsis" onclick="javascript:return false;">&#x2026;</a>';
			secondEllipsisPrinted = true;
		}
	}

	var startNumber = ((currentPage - 1) * numPerPage) + 1;
	var endNumber;
	if (currentPage == pages) {
		endNumber = totalResults;
	} else {
		endNumber = (currentPage * numPerPage);
	}

	html += '<a class="pagination_' + container + '_next" href="#" onclick="javascript:return showPage(\'' + container + '\', \'' + cls + '\', ' + pages + ', \'next\', ' + duration + ', ' + numPerGroup + ', ' + totalResults + ', ' + numPerPage + ', ' + callback + ', \'' + callback + '\', ' + (onlyShowFirst ? 'true' : 'false') + ');">Next<span class="symbol"> &#x3e;</span></a>'
			+ '<a style="display:none;" class="pagination_' + container + '_last" href="#" onclick="javascript:return showPage(\'' + container + '\', \'' + cls + '\', ' + pages + ', \'last\', ' + duration + ', ' + numPerGroup + ', ' + totalResults + ', ' + numPerPage + ', ' + callback + ', \'' + callback + '\', ' + (onlyShowFirst ? 'true' : 'false') + ');">Last<span class="symbol"> &#xbb;</span></a></span>';
	if (totalResults) {
		html += '<span class="paginationSummary"><strong class="' + container + '_pageNumber">' + startNumber + '</strong> to <strong class="' + container + '_totalPages">' + endNumber + '</strong> of <strong class="' + container + '_pageNumber">' + totalResults + '</strong></span>';
	} else {
		html += '<span class="paginationSummary">Page <strong class="' + container + '_pageNumber">' + currentPage + '</strong> of <strong class="' + container + '_totalPages">' + pages + '</strong></span>';
	}

	return html;
}

function checkTrialForm() {
	var errors = [];
	$('#error_box').slideUp(150);

	if ($('#name').val().length == 0) {
		errors.push('You must provide your name.');
	}

	if ($('#email_address').val().length == 0) {
		errors.push('You must provide your email address.');
	}

	if ($('#organisation').val().length == 0) {
		errors.push('You must provide your organisation.');
	}

	if ($('#telephone').val().length == 0) {
		errors.push('You must provide your telephone number.');
	}

	if ($('#city').val().length == 0) {
		errors.push('You must provide your city.');
	}

	if ($('#country_code').val().length == 0) {
		errors.push('You must provide your country.');
	}

	if (errors.length) {
		$('#error_box ul').html('<li>' + errors.join('</li><li>') + '</li>');
		$('#error_box').slideDown(150);

		return false;
	} else {
		return true;
	}
}

function checkTextField(field) {
	if (field.value.length == 0) {
		$('#' + field.id + '_tick').hide();
		$('#' + field.id + '_cross').show();
	} else {
		$('#' + field.id + '_tick').show();
		$('#' + field.id + '_cross').hide();
	}
}

function checkPasswordField(field) {
	if (field.value.length < 7) {
		$('#' + field.id + '_tick').hide();
		$('#' + field.id + '_cross').show();
	} else {
		$('#' + field.id + '_tick').show();
		$('#' + field.id + '_cross').hide();
	}
}

function checkEmailField(field) {
	if (!field.value.match(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/)) {
		$('#' + field.id + '_tick').hide();
		$('#' + field.id + '_cross').show();
	} else {
		$('#' + field.id + '_tick').show();
		$('#' + field.id + '_cross').hide();
	}
}

function checkPostalCodeField(field, country_code) {
	if (country_code.length == 0) {
		return;
	}

	if (field.value.length == 0 || (country_code == 'GBR' && !field.value.match(/^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/))) {
		$('#' + field.id + '_tick').hide();
		$('#' + field.id + '_cross').show();
	} else {
		$('#' + field.id + '_tick').show();
		$('#' + field.id + '_cross').hide();
	}
}

function checkSelectField(field) {
	if (field.selectedIndex == 0) {
		$('#' + field.id + '_tick').hide();
		$('#' + field.id + '_cross').show();
	} else {
		$('#' + field.id + '_tick').show();
		$('#' + field.id + '_cross').hide();
	}
}

