// White Space!!!
function trim(s){
	return s.replace(/^\s*(.*?)\s*$/,"$1");
}

// User login form
function loginForm(form) {
	var username = form.username;
	var password = form.password;
	var redirect = form.redirect;
	
	// Username field empty
	if(trim(username.value) == "") {
		$(username).css('background', '#FBE3E4').bind('keydown', function(){
			$(this).css('background', '#FFFFFF');
		});
		$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
			$(this).html('Please enter your username.').addClass('error').fadeIn('slow');
		});
		return false;
	
	// Password field empty
	} else if(trim(password.value) == "") {
		$(password).css('background', '#FBE3E4').bind('keydown', function(){
			$(this).css('background', '#FFFFFF');
		});
		$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
			$(this).html('Please enter your password.').addClass('error').fadeIn('slow');
		});
		return false;
	
	// Success!
	} else {
		$.post('/account/loginPost',
			{username: username.value, password: password.value},
			function(data) {
				if (data == '') {
					document.location = redirect.value;
					
				} else {
					$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
						$(this).html(data).addClass('error').fadeIn('slow');
					});	
				}
			});
	}
	
	return false;
}

// Create reply form for comments section
function createReplyForm(surveyid, comment_id, parent) {
	$.post('/content/createReplyForm',
		{surveyid: surveyid, comment_id: comment_id, parent: parent},
		function(data) {
			$('#reply_barrier_' + comment_id).show().html(data);
			$('#comment_button_reply_' + comment_id).attr('disabled','disabled').text('Replying...');
			$('.comment_button_cancel_' + comment_id)
				.bind('click', function() {
					$('#form_comment_reply_' + comment_id).remove();
					$('#comment_button_reply_' + comment_id).removeAttr('disabled').text('Reply');
				});
		});
}

// User comment reply form
function commentReplyForm(form) {
	var reply = form.reply;
	var surveyid = form.surveyid;
	var comment_id = form.comment_id;
	var parent = form.parent;
	var accountid = form.accountid;
	var username = form.username;
	
	// Username field empty
	if(trim(username.value) == "") {
		$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
			$(this).html('Please enter a username.').addClass('error').fadeIn('slow');
		});
		return false;
	
	// Reply field empty
	} else if(trim(reply.value) == "") {
//		$(reply).css('background', '#FBE3E4').bind('keydown', function(){
//			$(this).css('background', '#FFFFFF');
//		});
		$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
			$(this).html('Please enter a reply.').addClass('error').fadeIn('slow');
		});
		return false;
	
	// Success!
	} else {
		$.post('/content/commentReply',
			{reply: reply.value, surveyid: surveyid.value, comment_id: comment_id.value, parent: parent.value, accountid: accountid.value, username: username.value},
			function(data) {
				$('#reply_barrier_' + comment_id.value).html('Thanks for your comment!').removeClass('error').addClass('success').fadeIn('slow');
				$('#comment_button_reply_' + comment_id.value).val('Replied');
			});
	}

	return false;
}

// User comment form
function commentForm(form) {
	var comment = form.comment;
	var surveyid = form.surveyid;
	var accountid = form.accountid;
	var username = form.username;
	
	// Username field empty
	if(trim(username.value) == "") {
		$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
			$(this).html('Please enter a username.').addClass('error').fadeIn('slow');
		});
		return false;
	
	// Comment field empty
	} else if(trim(comment.value) == "") {
		$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
			$(this).html('Please enter a comment.').addClass('error').fadeIn('slow');
		});
		return false;
	
	// Success!
	} else {
		$.post('/content/comment',
			{comment: comment.value, surveyid: surveyid.value, accountid: accountid.value, username: username.value},
			function(data) {
				$('#comment_form').html('Thanks for your comment!').removeClass('error').addClass('success').fadeIn('slow');
			});
	}

	return false;
}

// Build replies block under comment
function build_replies(surveyid, comment_id) {
	$.post('/content/buildReplies',
		{surveyid: surveyid, comment_id: comment_id},
		function(data) {
			$('#comment_row_' + comment_id).append(data).find('.view_replies')
				.removeAttr('onclick').bind('click', function() {
					hide_replies(comment_id);
				}).text('Hide replies');
		});
}

// Hide reply block under comment
function hide_replies(comment_id) {
	$('#comment_row_' + comment_id).find('.view_replies').bind('click', function() {
			show_replies(comment_id);
		}).text('Show replies');
	$('#reply_block_' + comment_id).hide();
}

// Show reply block under comment
function show_replies(comment_id) {
	$('#comment_row_' + comment_id).find('.view_replies').bind('click', function() {
			hide_replies(comment_id);
		}).text('Hide replies');;
	$('#reply_block_' + comment_id).show();
}

// Rate a comment
function vote(surveyid, comment_id, rating) {
	$.post('/content/commentVote',
		{surveyid: surveyid, comment_id: comment_id, rating: rating},
		function(data) {
			var current_score = parseInt($('#comment_post_score_' + comment_id).text());
			if (rating > 0) {
				$('#comment_post_score_' + comment_id).text((current_score + 1));
				$('#comment_vote_positive_' + comment_id).html('<img src="http://www.what-noway.com/public/img/vote-check.png"/>');
 				$('#comment_vote_negative_' + comment_id).html('<img src="http://www.what-noway.com/public/img/vote-cross-grey.png"/>');
			} else {
				$('#comment_post_score_' + comment_id).text((current_score - 1));
				$('#comment_vote_positive_' + comment_id).html('<img src="http://www.what-noway.com/public/img/vote-check-grey.png"/>');
				$('#comment_vote_negative_' + comment_id).html('<img src="http://www.what-noway.com/public/img/vote-cross.png"/>');
			}
		});
}

function drawPieChart(columns, values, params) {
	var data = new google.visualization.DataTable();
	
	for(i = 0; i < columns.length; i++) {
		data.addColumn(columns[i][0], columns[i][1]);
	}
	
	data.addRows(values.length);
	
	for(i = 0; i < values.length; i++) {
		for(j = 0; j < values[i].length; j++) {
			data.setValue(i, j, values[i][j]);
		}
	}

	var chart = new google.visualization.PieChart(document.getElementById(params['element']));
	chart.draw(data, {
			width: params['width'],
			height: params['height'],
			colors: params['colors'],
			legend: params['legend'],
			legendFontSize: params['legendFontSize'],
			legendTextColor: params['legendTextColor'],
			legendBackgroundColor: params['legendBackgroundColor'],
			title: params['title'],
			enableTooltip: params['enableTooltip'],
			is3D: params['is3D']
		});
}

function drawBarChart(columns, values, params) {
	var data = new google.visualization.DataTable();
	
	for(i = 0; i < columns.length; i++) {
		data.addColumn(columns[i][0], columns[i][1]);
	}
	
	data.addRows(values.length);
	
	for(i = 0; i < values.length; i++) {
		for(j = 0; j < values[i].length; j++) {
			data.setValue(i, j, values[i][j]);
		}
	}

	var chart = new google.visualization.ImageBarChart(document.getElementById(params['element']));
	chart.draw(data, {
			width: params['width'],
			height: params['height'],
			colors: params['colors'],
			legend: params['legend'],
			title: params['title']
		});
}

/* Survey form - add one to ranged survey option field */
function rangeAddOne(optionid) {
	var current_value = parseInt($('#form_vote_survey').find('.option-' + optionid).val());
	
	if (current_value < 5) {
		$('#form_vote_survey').find('.option-' + optionid).val(current_value + 1);
	}
}

/* Survey form - minus one to ranged survey option field */
function rangeMinusOne(optionid) {
	var current_value = parseInt($('#form_vote_survey').find('.option-' + optionid).val());
	
	if (current_value > 0) {
		$('#form_vote_survey').find('.option-' + optionid).val(current_value - 1);
	}
}

/* Survey form - add one to weighted survey option field */
function weightedAddOne(optionid) {
	var points_remaining = parseInt($('.points_remaining').html());
	var current_value = parseInt($('#form_vote_survey').find('.option-' + optionid).val());
	
	if (points_remaining > 0) {
		$('#form_vote_survey').find('.option-' + optionid).val(current_value + 1);
		$('.points_remaining').html(points_remaining - 1);
	}
}

/* Survey form - minus one to weighted survey option field */
function weightedMinusOne(optionid) {
	var points_remaining = parseInt($('.points_remaining').html());
	var current_value = parseInt($('#form_vote_survey').find('.option-' + optionid).val());
	
	if (points_remaining < 5 && current_value > 0) {
		$('#form_vote_survey').find('.option-' + optionid).val(current_value - 1);
		$('.points_remaining').html(points_remaining + 1);
	}
}

$(document).ready( function() {
	
});

