var Vote = function (config)
{
	this.send = function (posturl)
	{	
		var param = '';
				
		for(var prop in config)
		{
			param += prop + '=' + config[prop]+'&';
		}
		param = param.substring(0, param.length-1);
		
		jQuery.post(posturl, param, this.success, 'json');
	};
	
	this.success = function (data)
	{
		try {
			data = eval(data);
			if(data.error==1) throw new Error(data.msg);	
			{
				var optioncount = $('#toVote').find('#touvoteval:checked');
				var piao;

				optioncount.each(function(){
					piao = $(this).parent().find('#option-count').html();
					piao = parseInt(piao)+1;
					$(this).parent().find('#option-count').html(piao);
				});
				
				alert(data.msg);
			}
		} catch (e) {
			alert(e.message);
		}	
	};
};

function topostvote(subject_id)
{	
	try {
		var strval 	= '';
		var posturl	= url('default::vote/postvote');

		$('#toVote').find('#touvoteval:checked').each(function(){
			strval += $(this).val()+',';	
		});
		strval = strval.substring(0,strval.length-1);
		
		var config = {
			'ids'		: strval,
			'subject_id': subject_id
		};
		
		var lo = new Vote(config);
		lo.send(posturl);
	} catch (e) {
		alert(e.message);
	}
};
