function showError(key, errors) {
	for (var i in errors)
		$('<span title="'+key+'" style="display: none">'+errors[i]+'</span>').appendTo('#errors');
}


function createErrorTooltips() {
	$('#errors span').each(function () {
		var myError = $(this).text();

		if($(this).attr('title').length > 0) {

			$('#'+$(this).attr('title')).qtip({
				 content: {
					 text: myError,
					 title: {
					 }
				 },
				 position: {
					my: 'center left',  // Position my top left...
					at: 'center right', // at the bottom right of...
					target: $('#'+$(this).attr('title')).parent() // my target
				 },
				 style: {
					classes: 'login-tooltip ui-tooltip-red ui-tooltip-rounded ui-tooltip-shadow',
					tip: {
					 corner: 'left center'
					}
				 },
				 show: {
					 event: false, // Don't specify a show event...
					 ready: true   // ... but show the tooltip when ready
				 },
				 hide: {
					 event: false,
				 }

			});
		} else {
			$('.general-form-errors').qtip({
				 content: {
					 text: $(this),
					 title: {
					 }
				 },
				 position: {
					my: 'center left',  // Position my top left...
					at: 'center right', // at the bottom right of...
					target: $('.general-form-errors') // my target
				 },
				 style: {
					classes: 'tooltip-no-input-field login-tooltip ui-tooltip-red ui-tooltip-rounded ui-tooltip-shadow',
					tip: {
					 corner: false,
					}
				 },
				 show: {
					 event: false, // Don't specify a show event...
					 ready: true   // ... but show the tooltip when ready
				 },
				 hide: {
					 event: false,
				 }

			});
		}
	});

	$('input').bind('keypress click focus', function() { $('.qtip.ui-tooltip').qtip('hide'); });
}

function validateForm(form) {
	$.ajax({
		type: 'POST',
		url: '/register/',
		dataType: 'json',
		data: {
			'accept': $(form).find("#accept").is(':checked'),
			'name': $(form).find("#name").val(),
			'email': $(form).find("#email").val(),
			'confirm': $(form).find("#confirm").val()
		},
		success: function(data){
			$('#errors').empty();
			if (!data.success) {
				for (var i in data.errors) {
					showError(i, data.errors[i]);
				}
				createErrorTooltips();
			} else {
				window.location = '/demo-account-created/';
			};
		},
		error: function(data){
			showError('', 'Connection error. Please contact support@medisapiens.com to create demo account.');
		}
	});
	return false;
}

function validateGeneralForm(form, url, success_url, error_message) {
	var data = {};
	$(form).find('input, textarea').each(function(){
		data[$(this).attr('name')] = $(this).val();
	});
	$.ajax({
		type: 'POST',
		url: url,
		dataType: 'json',
		data: data,
		success: function(data){
			$('#errors').empty();
			if (!data.success) {
				for (var i in data.errors) {
					showError(i, data.errors[i]);
				}
				createErrorTooltips();
			} else {
				window.location = success_url;
			};
		},
		error: function(data){
			showError('', error_message);
		}
	});
	return false;
}

function validateDashboardContactForm(form) {
	return validateGeneralForm(form,
			'/dashboard/contact/',
			'/cancer-dashboard-contact-sent/',
			'Connection error. Please contact support@medisapiens.com to get contacted.');
}

function validateGeneReportForm(form) {
	return validateGeneralForm(form,
			'/ist-online/gene-report/',
			'/gene-report-sent/',
			'Connection error. Please contact support@medisapiens.com to get gene report.');
}

$(function(){
	//$('.styled').selectbox();
});

