/*
 * Quick and dirty sequential form logic.
 * (Extracted from the main cbq sequential logic)
 */
var backButton    = "";
var progressLabel = "";
var nextButtonCss = "";

$(document).ready(function() {
	if($("[name='ApplySequentialFormLogic']").val() != 'FTRS') return false;
	var cSeqAttr = undefined, $backBtn = undefined, $nextBtn = undefined, 
		seqOrd = {}, rseqOrd = {}, progressTotal=1, progressIndex = 0, pMin = 0, pBrd = 100;
	//initialize
	var tmp = undefined;
	var ques = $("[id^='seq_']").each(function(){
		var attr = $(this).attr('id').substring(4);
		if(tmp != undefined){
			seqOrd[tmp] = attr;
			rseqOrd[attr] = tmp;
		}
		tmp = attr;
		if(cSeqAttr == undefined) cSeqAttr = attr;
	});
	if(ques.length == 0){
		var progress = $("form").prepend("<div id='progress'><label>" + progressLabel + "</label><h6>"
				+ "<span>&nbsp;</span></h6></div>");
		return;
	}
	var submitBtn = $("#submit").hide();
	//Setup the progress bar
	var progress = $("form").prepend("<div id='progress'><label>" + progressLabel + "</label><h6>"
			+ "<span style='width:"
			+ ((progressIndex/progressTotal)*100)+"%'>&nbsp;</span></h6></div>");
	pMin = parseInt((progress.find('span').css('min-width').replace(/px/g,'')
			/progress.find('h6').css('width').replace(/px/g,''))*100);
	if(isNaN(pMin)) pMin = 0;
	pBrd = parseInt((progress.find('span').css('max-width').replace(/px/g,'')
			/progress.find('h6').css('width').replace(/px/g,''))*100) - pMin;
	if(isNaN(pBrd)) pBrd = 100;
	//attach event handlers to the questions
	ques.filter(":not(:last)").each(function(){
		var attr = $(this).attr('id').substring(4);
		bindTrigger(attr, function(){
			if(readFormValues(cSeqAttr).length==0){showInputError(cSeqAttr); return false;};
			cSeqAttr = seqOrd[attr];
			if(seqOrd[cSeqAttr] == undefined){
				submitBtn.show();
				$nextBtn.hide();
			}
			if(cSeqAttr == undefined) return;
			$("[id$='_"+attr+"']").hide();
	    	$("[id$='_"+cSeqAttr+"']").show();
			$("#progress").find('span').width((pMin+(++progressIndex/progressTotal)*pBrd)+'%');
			if(readFormValues(cSeqAttr).length==0) $nextBtn.hide();
			else if(seqOrd[cSeqAttr]!=undefined) $nextBtn.show();
			$backBtn.show();
		});
	});
	//Hide except for the first question
	ques.filter(":gt(0)").hide();
	$backBtn = $("<a class='button back' href='#'>&lt; " + backButton + "</a>").click(function(){
    	$("[id$='_"+cSeqAttr+"']").hide();
		cSeqAttr = rseqOrd[cSeqAttr];
		if(rseqOrd[cSeqAttr] == undefined) 
			$backBtn.hide();
    	$("[id$='_"+cSeqAttr+"']").show();
		$("#progress").find('span').width((pMin+(--progressIndex/progressTotal)*pBrd)+'%');
		$nextBtn.show();
		submitBtn.hide();
		if(readFormValues(cSeqAttr).length==0) $nextBtn.hide();
		else $nextBtn.show();
		return false;
	});
	$nextBtn = $("<a class='" + nextButtonCss + "' href='#'>Next &gt;</a>").click(function(){
		if(readFormValues(cSeqAttr).length==0){ showInputError(cSeqAttr);return false;}
		$("[id$='_"+cSeqAttr+"']").hide();
		cSeqAttr = seqOrd[cSeqAttr];
		if(seqOrd[cSeqAttr] == undefined){
			submitBtn.show();
			$nextBtn.hide();
		}
		if(cSeqAttr == undefined) return;
    	$("[id$='_"+cSeqAttr+"']").show();
		$("#progress").find('span').width((pMin+(++progressIndex/progressTotal)*pBrd)+'%');
		$backBtn.show();
		if(readFormValues(cSeqAttr).length==0) $nextBtn.hide();
		else if(seqOrd[cSeqAttr] != undefined) $nextBtn.show();
		return false;
	});
	progressTotal = ques.length;
	submitBtn.after($nextBtn);
	$nextBtn.hide();
	if(readFormValues(cSeqAttr).length > 0)
		$nextBtn.show();
	submitBtn.after($backBtn);
	$backBtn.hide();
	function bindTrigger(name, func){
		var prnt;
		//Absolutely unnecessary conversions b/w Criteria and Form notations
		if(''+name == 'Static09')
			prnt = $("[name='SP']"); 
		else if(''+name == 'Static11')
			prnt = $("[name='CN']"); 
		else
			prnt = $("[name='"+name+"']"); 
		//The click vs change is for IE which doesn't trigger 'change' until you 'blur'
		var type = prnt.attr('type');
		//Somehow the normal binding process is acting up when we 
		//attach multiple handlers on the same field
		prnt.bind((type=='radio'||type=='checkbox')?'click':'change', func);
	}
	function readFormValues(fld){
		var flds = $("[name='"+fld+"']");
		//If the field is not on the form return 'undefined'
		if(flds.length == 0) return undefined;
		//If the field type is radio or checkbox, read only checked values
		if(flds.attr('type')=='radio' || flds.attr('type')=='checkbox')
			flds = flds.filter(":checked");
		var vals = [];
		flds.each(function(){
			if(flds.attr('type')=='select-one'){
				if(!(/select/i.test($(this).val())))
					vals.push($(this).val());
			}else vals.push($(this).val());
		});
		return vals;
	}
});
function showInputError(attr){
//	console.log('Error: attr='+attr);
}

