var SelectedAnswer;

function SubmitSurvey(QuestionID,SurveyID,TotalAnswers,SurveyTitle)
{
	//Check if already vote
	var cookVal = getCookie("DynamicSurvey");
	if(cookVal!= null & cookVal == SurveyID)
	{
		alert('ניתן להצביע רק פעם אחת לסקר זה');
		return;
	}
	
	if(!CheckFormSurvey(QuestionID))return;

	var Params = "SurveyID=" + SurveyID + "&QuestionID=" + QuestionID + "&SelectedAnswer=" + SelectedAnswer;
	
	$.ajax({
		type: "POST",
		url: "/Survey/SurveyActions.aspx",
		data:Params,
		success: function(Res){
				if(Res == "1")
					window.open("/DynamicSurveyStat.aspx?SID=" + SurveyID + "&title=" + SurveyTitle + "/","","toolbar=no,resizable=1,left=200,top=100,height=420,width=" + (120 + parseInt(TotalAnswers) * 60));
			}
		});
}

function getCookie(name) {
	var dc = document.cookie;	  
	var prefix = name + '=';
	var begin = dc.indexOf('; ' + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
		begin += 2;
	var end = document.cookie.indexOf(';', begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}


function CheckFormSurvey(QueID)
{
	var ControlName = "DynamicSurvey1";
	if(typeof($('input[@id$="DynamicSurvey1_Question'+ QueID + '_0"]').get(0)) == 'undefined')
		ControlName = "Dynamicsurvey2";
	
	var QuestionID = ControlName +"_Question" + QueID;
	
	var list = 0,i = 0;
	$('input[@id^="' + QuestionID + '"]').each(function(){list++;});
	
    SelectedAnswer = 0;
    
	if(list == 0) //List Of Radio Button. For Example: 4
	{
		$('input[@id*="' + QuestionID + '"]').each(function(){
			if($(this).attr('checked') == true)
			{
				SelectedAnswer = i;
				return;				
			}
			i++;	
		});
	}
	else
	{
		$('input[@id^="' + QuestionID + '"]').each(function(){
			if($(this).attr('checked') == true)
			{
				SelectedAnswer = i;
				return;
			}
			i++;
		});
	}

	if(SelectedAnswer == 0)
	{
		alert("יש לענות על הסקר");
		return false;	
	}
	return true;
}

	
