$(function(){
	$("#btnSubmit").click(function(){
		$("#Option").val("Save");
		$("#msgHolder").html('<img src="/images/indicator.gif">');
		if($("#ssRequired").val()=="1"){
			var bAnyError = false;
			$("#frmSurvey input:visible").each(function(){
				if($(this).val()==""){
					alert("Ooops! Enter missing value first..");
					$(this).focus();
					bAnyError = true;
					return false;
				}
			});
		}
		if(bAnyError){
			$("#msgHolder").html("");
			return false;
		}
		var data		= $("#frmSurvey").formSerialize();
		var nextVisible	= parseInt($("#nextVisible").val())+1;
		$.post(
			$("#scriptName").val(),
			data,
			function(txt){
				var parts = txt.split("{|}");
				if(parts.length!=2){
					// next question is coming
					$("#surveyHolder").html(parts[0]);
					$("#breadcrumb").val(parts[1]);
					$("#sid").val(parts[2]);
					$("#qid").val(parts[3]);
					$("#ssRequired").val(parts[4]);					
					$("#msgHolder").html("");
					bindPagination();
					$("#nextVisible").val(nextVisible);
					if(parts[4]=="1"){
						$("#btnLater").hide();
					}else{
						$("#btnLater").show();
					}
				}else{
					// some message is coming
					$("#msgHolder").html(parts[0]);
					if(parts[1]=="HIDE"){
						$("#frmSurvey").hide();
					}
				}
				txt = null;
			}
		)
		return false;
	});
	$("#btnLater").click(function(){
		$("#msgHolder").html('<img src="/images/indicator.gif">');
		$("#Option").val("ContLater");
		var data = $("#frmSurvey").formSerialize();
		$.post(
			$("#scriptName").val(),
			data,
			function(txt){
				$("#msgHolder").html(txt);
				$("#frmSurvey").hide();
				bindCmdCont();
				txt = null;
			}
		)
		return false;
	});
	bindPagination();
});
function bindCmdCont(){
	$("#cmdCont").click(function(){
		$("#msgHolder").html("");
		$("#frmSurvey").show();
		return false;
	});
}
function bindPagination(){
	var ssid		= $("#ssid").val()!="" ? parseInt($("#ssid").val()) : 0;
	var ssRequired	= ($("#ssRequired").val()!="" && typeof $("#ssRequired")[0] != "undefined") ? parseInt($("#ssRequired").val()) : 0;

	if(ssid!=0 && ssRequired==0){
		// suvery questions on one page - pagination
		var nrOfQuestions	= parseInt($("#nrOfQuestions").val());
		var nextVisible		= 0;
		$(".prev")
			.click(function(){
				var nrOfQuestion	= parseInt($(this).attr("id").split("_")[1]);
				if(nrOfQuestion>1){
					nrOfQuestion--;
				}
				$(".questionHolder").hide();
				$("#question_"+nrOfQuestion).show();
				nextVisible	= nrOfQuestion + 1;
				$("#nextVisible").val(nextVisible);
				return false;
			});
		$(".next")
			.click(function(){
				var nrOfQuestion	= parseInt($(this).attr("id").split("_")[1]);
				if(nrOfQuestion<nrOfQuestions){
					nrOfQuestion++;
				}
				$(".questionHolder").hide();
				$("#question_"+nrOfQuestion).show();
				nextVisible	= nrOfQuestion + 1;
				nextVisible = nextVisible > nrOfQuestions ? nrOfQuestions : nextVisible;
				$("#nextVisible").val(nextVisible);
				return false;
			});
		$(".questionHolder:visible .next").hide();
		var nextVisible = parseInt($("#nextVisible").val());
		if(nextVisible>0){
			$(".questionHolder").hide();
			$("#question_"+nextVisible).show();
		}
		$(".questionHolder:visible")
			.not(":first")
			.hide();
	}
}