// Vote on survey form (1 - Simple)
function voteSimpleSurveyForm(form) {
	var surveyid = form.surveyid.value;
	var accountid = form.accountid.value;
	var username = form.username.value;
	var option = $('#form_vote_survey :checked').val();
	
	// Option fields empty
	if(typeof(option) == 'undefined') {
		$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
			$(this).html('Please choose an option.').addClass('error').fadeIn('slow');
		});
		return false;
	
	// Success!
	} else {
		$.post('/archive/votePost',
			{surveyid: surveyid, accountid: accountid, username: username, options: option, score: 1},
			function(data) {
				window.location.reload(true);
			});
	}
	
	return false;
}

// Vote on survey form (2 - Ranked)
function voteRankedSurveyForm(form) {
	var surveyid = form.surveyid.value;
	var accountid = form.accountid.value;
	var username = form.username.value;
	var option_array = new Array();
	var score_array = new Array();
	var score = $('#sortable :li').length;
	
	$('#sortable :li').each(function(n) {
		option_array.push($(this).attr('class'));
		score_array.push(score--);
	});
	
	$.post('/archive/votePost',
		{surveyid: surveyid, accountid: accountid, username: username, 'options[]': option_array, 'score[]': score_array},
		function(data) {
			window.location.reload(true);
		});
	
	return false;
}

// Vote on survey form (3 - Range)
function voteRangeSurveyForm(form) {
	var surveyid = form.surveyid.value;
	var accountid = form.accountid.value;
	var username = form.username.value;
	var options = $('#form_vote_survey :input.tally');
	var option_array = new Array();
	var score_array = new Array();
	
	$.each(options, function(index, element) {
		option_array.push(element.name);
		score_array.push(element.value);
	});
	
	$.post('/archive/votePost',
		{surveyid: surveyid, accountid: accountid, username: username, 'options[]': option_array, 'score[]': score_array},
		function(data) {
			window.location.reload(true);
		});
	
	return false;
}

// Vote on survey form (4 - Approval)
function voteApprovalSurveyForm(form) {
	var surveyid = form.surveyid.value;
	var accountid = form.accountid.value;
	var username = form.username.value;
	var options = $('#form_vote_survey :input.option');
	var option_count = 0;
	var option_array = new Array();
	
	$.each(options, function(index, element) {
		if ($(element).is(':checked')) {
			option_array.push($(element).val());
			option_count++;
		}
	});
	
	// Option fields empty
	if(option_count == 0) {
		$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
			$(this).html('Please choose at least one option.').addClass('error').fadeIn('slow');
		});
		return false;
		
	// Success!
	} else {
		$.post('/archive/votePost',
			{surveyid: surveyid, accountid: accountid, username: username, 'options[]': option_array, score: 1},
			function(data) {
				window.location.reload(true);
			});
	}
	
	return false;
}

// Vote on survey form (5 - Weighted)
function voteWeightedSurveyForm(form) {
	var surveyid = form.surveyid.value;
	var accountid = form.accountid.value;
	var username = form.username.value;
	var options = $('#form_vote_survey :input.tally');
	var option_array = new Array();
	var score_array = new Array();
	
	$.each(options, function(index, element) {
		option_array.push(element.name);
		score_array.push(element.value);
	});
	
	$.post('/archive/votePost',
		{surveyid: surveyid, accountid: accountid, username: username, 'options[]': option_array, 'score[]': score_array},
		function(data) {
			window.location.reload(true);
		});
	
	return false;
}

$(document).ready( function() {
	
	// Sort option fields for ranked survey types
	$('#sortable').sortable({
		placeholder: 'placeholder'
	});
});

