$(function() {
	$('#INFO-LEAVE-FORM').click(function() {
		show_survey();
	})
	
	$('#close').click(function() {
		hide_survey();
	})
	
	$('.blockOverlay').live('click', function() {
		$('#container').unblock();
	})
	
	$('#submit').click(function() {
		var posty = JSON.stringify(gather_survey());
		if (posty != 'false') {
			$(this).attr("disabled", true);
			$.post(
				'/survey/submit/',
				{
					data: posty
				},
				function() {
					hide_survey();
				}
			);
		}
	})
	
	$('#Take-Video-Tour').click(function() {
		window.open('/vidshow/show_inst');
	})
	
	$('#close').hover(
		function() {
			this.src = '/images/red_cross.gif';
		},
		function() {
			this.src = '/images/gray_cross.gif';
		}
	)

	$('#INFO-LEAVE-FORM a').click(function() {
		$(this).parent().click();
		return false;
	})
})

function gather_survey() {
	var valid = validate();
	if(valid == true) {
		var data = {};
		$("#survey :radio").each(function() {
			if (this.checked)
				data[this.name] = this.value;
		});
		
		$("#followup_flag").each(function() {
			if (this.checked) {
				data['followup_period'] = $('#followup_period').val();
			}
		})
		
		$("#imp_q, #why_n_what").each(function() {
			data[this.id] = this.value;
		})

		$('#survey :text').each(function() {
			data[this.name] = this.value;
		})
		
		return data;
	}
	else
		return false;
}
	
function show_survey() {
	$('#container').block({
		message: $('#survey'),
		css: {
			top: '240px',
			width: '238px',
			left: '420px',
			cursor: 'default',
			border: 'none',
			background: 'transparent'
		},
		overlayCSS: {
			cursor: 'default',
			backgroundColor: 'white'
		}
	});
	
	var y = $(window).height() / 2.0 - $('.blockMsg').outerHeight() / 2 + $(window).scrollTop();
	if (y < 0)
		y = 25;
	$('.blockMsg').css('top', y);
}

function hide_survey() {
	$('#container').unblock();
}

function mailCheck(mailfield) {
	var email = mailfield.val();
	if (email != undefined) {
		var email = trim(email);
		var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
		var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;
		if (!emailFilter.test(email)) {
			alert("Please enter a valid e-mail address");
			return false;
		}
		else if (email.match(illegalChars)) {
			alert("Please enter a valid e-mail address");
			return false;
		}
		else
			return true;
	}
}

function trim(s) {
	return s.replace(/^\s+|\s+$/, '');
}

function validate() {
	var fine = true;
	fine = mailCheck($("#survey #email"));
	if (fine == false)
		return fine;
	var deselected = 0;
	$('#survey :radio').each(function() {
		if (this.checked)
			return false;
		else
			deselected++;
	})
	if (deselected == $('#survey :radio').length)
		fine = false;
	if ($('#name').val() == '') {
		alert('Please enter your name');
		return false;
	}
	else if ($('#imp_q').val() == '')
		fine = false;
	else if ($('#why_n_what').val() == '')
		fine = false;
	if (fine == false)
		alert('Please answer all questions');
	return fine;
}