function jsArrayToPhpArray(a)
{	
	var a_php = "";

	for (var i = 0; i<a.length; i++){		
		a_php = a_php + "i:" + i + ";";
		
		var text = '';
		if (typeof(a[i].text) != 'undefined'){
			text = escape(a[i].text);
		}
				
		a_php = a_php + "a:3:" + '{s:11:"question_id";i:' + a[i].question_id  + ';s:9:"answer_id";i:' + a[i].answer_id + ';s:4:"text";s:' + text.length + ':"' + text +'";}';
	}
	a_php = "a:" + a.length + ":{" + a_php + "}";
		
	return a_php;
}


function doVote(ajaxUrl, pollId)
{		
	var data = [];
	var sendGet = true;
				
	$(".questionIds").each(function(id){			
		idQuestion = $(this).val();
		$("#question" + idQuestion).html('');
		
		//Если данный вопрос должен иметь один вариант ответа
		if ($(":radio[name=question" + $(this).val() + "]").size()){
			if ($(":radio[name=question" + $(this).val() + "]").filter(":checked").size()){				
				var obj = {};
				obj.question_id = idQuestion;
				obj.text = '';
				obj.answer_id = $(":radio[name=question" + $(this).val() + "]").filter(":checked").val();
				data.push(obj);
			}else{
				sendGet = false;
				$("#question" + $(this).val()).html('<span style="color:red;">Выберите вариант ответа</span>');
			}
		}

		//Если данный вопрос может иметь несколько вариантов
		else if($(":checkbox[name=question" + $(this).val() + "]").size()){
			if ($(":checkbox[name=question" + $(this).val() + "]").filter(":checked").size()){
				$(":checkbox[name=question" + $(this).val() + "]").filter(":checked").each(function(id){
					var obj = {};
					obj.question_id	= idQuestion;
					obj.text = '';
					obj.answer_id = $(this).val();
					data.push(obj);
				});
			}
			else{
				sendGet = false;
				$("#question" + $(this).val()).html('<span style="color:red;">Выберите вариант ответа</span>');
			}			
		}
		
		//Открытый вопрос
		else{
			if ($(":text[name=question" + $(this).val() + "]").val().length){
				var obj = {};
				obj.question_id	= idQuestion;
				obj.answer_id = 0;
				obj.text = $(":text[name=question" + $(this).val() + "]").val();					
				data.push(obj);
			}
			else{
				sendGet = false;
				$("#question" + $(this).val()).html('<span style="color:red;">Введите вариант ответа</span>');
			}				
		}
	});
	
	if (sendGet){
		var php_array = jsArrayToPhpArray(data);
		
		$("#poll_block").css('background-image', 'url("pix/ajax-loader.gif")').css('background-repeat', 'no-repeat').css('background-position', "center center").fadeTo('fast', 0.3);			
		
		$.get(ajaxUrl, {data : php_array, poll_id: pollId} , function(data){				
			$("#poll_block").css('background-image', '').css('background-repeat', '').css('background-position', "").fadeTo('fast', 1);
			$("#poll_block").html(data);
		});
	}	
}
