var loop = 1;
var step = 1;
var stepBack = 0;
var idString = '#';
var dontStartLoop = 'true';
var nextStep = '';
var commonProcess = false;
var forCommonBlock = '';
var forDiffBlock = Array();
var forDiffBlockStr = '';
var forCommonShelves = '';
var forDiffShelves = Array();
var loopStep = 0;
var oldimagename = '';
var mainLoop = '';
var forBlock2 = Array();
var forBlock2Index = 0;
var quesText = Array();
var stepArr = Array();
quesText[2] = ' for third zone';
quesText[1] = ' for second zone';
quesText[0] = ' for first zone';

var commonQuesText = Array();
commonQuesText[2] = ' for both zones';
commonQuesText[3] = ' for all three zones';

function showStep(thisObj) {
	var stepList = $(thisObj).attr('stepList');
	if ($(thisObj).attr('stepno') == '2' || $(thisObj).attr('stepno') == '1') {
/*
		alert("loop="+loop);
		alert("step="+step);
		alert("stepBack="+stepBack);
		alert("idString="+idString);
		alert("dontStartLoop="+dontStartLoop);
		alert("nextStep="+nextStep);
		alert("commonProcess="+commonProcess);
		alert("forCommonBlock="+forCommonBlock);
		alert("forDiffBlock="+forDiffBlock);
		alert("forDiffBlockStr="+forDiffBlockStr);
		alert("forCommonShelves="+forCommonShelves);
		alert("forDiffShelves="+forDiffShelves);
		alert("loopStep="+loopStep);
		alert("oldimagename="+oldimagename);
		alert("forBlock2="+forBlock2);
		alert("forBlock2Index="+forBlock2Index);
		alert("quesText="+quesText);
*/
		stepBack = 0;
		idString = '#';
		dontStartLoop = 'false';
		nextStep = '';
		commonProcess = false;
		forCommonBlock = '';
		forDiffBlock = Array();
		forDiffBlockStr = '';
		forCommonShelves = '';
		forDiffShelves = Array();
		loopStep = 0;
		forBlock2Index = 0;
		stepArr = Array();
		mainLoop = $(thisObj).attr('loop');
		loop = $(thisObj).attr('loop');
	}
	if (stepList == undefined && stepArr[loop-1] != undefined) {
		loop--;
		stepList = stepArr[loop-1];
	}
	if (thisObj != '' && stepList != '' && stepList != undefined && $(thisObj).attr('loop') >= 0) {
		if (stepList.match('#') != null) {
			var blockArr = stepList.split('#');
		} else {
			var blockArr = Array();
			blockArr[0] = stepList;
		}
		if ($(thisObj).attr('forBlock2')) {
			if ($(thisObj).attr('forBlock2').match(',')) {
				forBlock2 = $(thisObj).attr('forBlock2').split(',');
			} else {
				forBlock2[0] = $(thisObj).attr('forBlock2');
			}
		}
		if ($(thisObj).attr('forBlock3Index')) {
			forBlock3Index = $(thisObj).attr('forBlock3Index');
		} else {
			forBlock3Index = 0;
		}
		if ($(thisObj).attr('stepArr')) {
			stepArr = $(thisObj).attr('stepArr').split(',');
		}
		if ($(thisObj).attr('common')) {
			dontStartLoop = $(thisObj).attr('common');
		} else if (blockArr[0] == 'block2') {
			dontStartLoop = 'true';
		}
		if ($(thisObj).attr('nextStep') != undefined ) {
			nextStep = $(thisObj).attr('nextStep');
		} else if (nextStep != '' && blockArr[0] == 'block2') {
		//	nextStep = '';
		} else {
			nextStep = '';
		}
		if ($(thisObj).attr('loop')) {
			loop = $(thisObj).attr('loop');
		}
		if (stepArr[loop-1] != undefined) {
			nextStep = stepArr[loop-1];
		}
		if ($(thisObj).attr('loopStep')) {
			loopStep = $(thisObj).attr('loopStep');
		}
		if ($(thisObj).attr('forCommonBlock')) {
			forCommonBlock = $(thisObj).attr('forCommonBlock');
		}
		if ($(thisObj).attr('forDiffBlock') != undefined && $(thisObj).attr('forDiffBlock') =='test') {
			$(thisObj).attr('forDiffBlock', forDiffBlockStr+',');
		} else {
			forDiffBlockStr = $(thisObj).attr('forDiffBlock');
		}
		if ($(thisObj).attr('forDiffBlock') != undefined) {
			var temp = $(thisObj).attr('forDiffBlock').split(',');
			var j=0;
			for (i=temp.length-1; i>-1 ;i-- )	{
				forDiffBlock[j] = temp[i];
				j++;
			}
		}
		if (forCommonShelves == 'blockBlank') {

		} else if ($(thisObj).attr('forCommonShelves') != undefined) {
			forCommonShelves = String($(thisObj).attr('forCommonShelves'));
		}
		if ($(thisObj).attr('forDiffShelves')) {
			var temp = $(thisObj).attr('forDiffShelves').split(',');
			var j=0;
			for (i=temp.length-1; i>-1 ;i-- )	{
				forDiffShelves[j] = temp[i];
				j++;
			}
		}
		//alert('--'+forCommonShelves+'--'+loop);
		var id = $(thisObj).parents('.step').attr('id');

		clearTail(thisObj, id);
		step++;
		if (blockArr[0] == 'block6' && forCommonShelves == ' ') {
			if (stepArr[loop-1] != undefined) {
				blockArr[0] == stepArr[loop-1];
			} else if (loop == 1) {
				blockArr[0] = 'blockBlank';
			}
		} else if (blockArr[0] == 'block6' && forCommonShelves == 'blockBlank') {
			blockArr[0] = 'blockBlank';
		} else if (blockArr[0] == 'block3') {
			blockArr[0] == stepArr[loop-2];
		}
		var html = $('#step'+blockArr[0]).attr('innerHTML');
		html = html.replace(/name=[a-zA-Z0-9]+/gi,'name=step'+step);
		html = '<table width="100%" border="0" cellpadding="0" cellspacing="0" class="step indent0" id="step'+step+'" >'+html+'</table>';
		$('#'+id).after(html);
		$('#step'+step).children().children().children().children().children().children(':first').children('.bullet').html(step);
		var quesTempText = String($('#step'+step).children().children().children().children().children().children(':first').children('.txt11').html());

		if (blockArr[0] == 'block3' && loop && dontStartLoop == 'false') {
			loopStep++;
		}
		if (blockArr[0] == 'block4') {
			quesTempText = quesTempText.replace('(for)', commonQuesText[loop]);
		} else if (dontStartLoop == 'false') {
			quesTempText = quesTempText.replace('(for)', quesText[parseInt(mainLoop)-loop]);
		} else if (dontStartLoop == 'true') {
			quesTempText = quesTempText.replace('(for)', '');
		}
		$('#step'+step).children().children().children().children().children().children(':first').children('.txt11').html(quesTempText);
		var loop1 = loop;
		if (loop == 0) {
			loop1 = 1;
		}
		var allChoices = $('#step'+step).children().children('.choices');
		var length = allChoices.length;
		for (i=0;i<length;i++) {
			if (blockArr[0] == 'block3' && forCommonBlock != '' && forCommonBlock.match('#'+(i+1)+'#') && dontStartLoop == 'true') {
				$(allChoices[i]).hide();
			} else if (blockArr[0] == 'block3' && forDiffBlock.length && forDiffBlock[loop1-1].match('#'+(i+1)+'#') && dontStartLoop == 'false') {
				$(allChoices[i]).hide();
			} else if (blockArr[0] == 'block3' && forDiffBlock.length && forDiffBlock[loop1].match('#'+(i+1)+'#') && dontStartLoop == 'true') {
				$(allChoices[i]).hide();
			} else if (blockArr[0] == 'block2' && forBlock2.length && forBlock2[0].match('#'+(i+1)+'#')) {
				$(allChoices[i]).hide();
			} else if (blockArr[0] == 'block6' && forCommonShelves != '' && forCommonShelves.match('#'+(i+1)+'#') && dontStartLoop == 'true') {
				$(allChoices[i]).hide();
			} else if (blockArr[0] == 'block6' && forDiffShelves.length && forDiffShelves[loop1-1].match('#'+(i+1)+'#') && dontStartLoop == 'false') {
				$(allChoices[i]).hide();
			} else if (stepList.match('#'+(i+1)+'#')) {
				$(allChoices[i]).hide();
			}
			var optObj = $(allChoices[i]).children(':first').next().children('input');
			$(allChoices[i]).children(':first').next().children('input').attr('name', 'step'+step);
			if ($(allChoices[i]).children(':first').next().children('input').attr('stepList') == 'blockBlank' && stepArr[loop-2] != undefined) {
				$(allChoices[i]).children(':first').next().children('input').attr('stepList', stepArr[loop-2]);
				$(allChoices[i]).children(':first').next().children('input').attr('loop', loop-1);
			}
			if ($(allChoices[i]).children(':first').next().children('input').attr('stepList') != undefined && $(allChoices[i]).children(':first').next().children('input').attr('stepList').match(blockArr[0]) && loop && dontStartLoop == 'true') {
				$(allChoices[i]).children(':first').next().children('input').attr('stepList', 'blockBlank');
				$(allChoices[i]).children(':first').next().children('input').attr('cleartail', '1');
			}
			if (nextStep != '' && blockArr[0] == 'block2') {
				$(allChoices[i]).children(':first').next().children('input').attr('stepList', nextStep);
			} else if (blockArr[0] == 'block2') {
				if (stepArr[loop-2] != undefined) {
					$(allChoices[i]).children(':first').next().children('input').attr('stepList', stepArr[loop-2]);
				}
			}
			if (blockArr[0] == 'block6') {
				if (stepArr[loop-2] != undefined) {
					$(allChoices[i]).children(':first').next().children('input').attr('stepList', stepArr[loop-2]);
				} else {
					$(allChoices[i]).children(':first').next().children('input').attr('stepList', '');
				}
			}
		}
		if (blockArr[0] == 'block6' && loop && dontStartLoop == 'false') {
			loop--;
		} else if (blockArr[0] == 'block3' && loop && dontStartLoop == 'false' && forCommonShelves == ' ') {
			loop--;
		}
		allChoices = $('#step'+step).children().children('.choices');
		for (i=0;i<allChoices.length;i++) {
			var trChildren = null;
			trChildren = $(allChoices[i]).children('.txt11');
			var trChildrenTemp = $(allChoices[i]).html();
			if (trChildren.length > 1) {
				var tdChildren = $(trChildren[1]).children();
			}
			if (blockArr[0] == 'block3' && ((i==7 || i==6) )) {
				$(tdChildren[0]).attr('stepList', "");
				$(tdChildren[0]).attr('clearTail', "1");
			} else if (blockArr[0] == 'block6' && loop=='0') {
				$(tdChildren[0]).attr('clearTail', "1");
			}

			if (blockArr[0] == 'block6' && dontStartLoop == 'true') {
				$(tdChildren[0]).attr('stepList', "");
				$(tdChildren[0]).attr('clearTail', "1");
			} else if (blockArr[0] == 'block3' && dontStartLoop == 'true') {
				//$(tdChildren[0]).attr('showModelNo', loop);
			} else if (blockArr[0] == 'block5' && dontStartLoop == 'true') {
				//$(tdChildren[0]).attr('showModelNo', loop);
				$(tdChildren[0]).attr('clearTail', "1");
			}
			if ($(tdChildren[0]).attr('loop') == undefined || $(tdChildren[0]).attr('loop') == '0') {
				$(tdChildren[0]).attr('loop', loop);
			}
			$(tdChildren[0]).attr('loopStep', loopStep);
			$(tdChildren[0]).attr('name', 'step'+step);
			$(tdChildren[0]).attr('stepNo', step);
		}
		if ($(thisObj).attr('nextStep') != undefined ) {
			nextStep = $(thisObj).attr('nextStep');
		} else if (nextStep != '' && blockArr[0] == 'block2') {
			nextStep = '';
		} else {
			nextStep = '';
		}
	}
/*

		alert("loop="+loop);
		alert("step="+step);
		alert("stepBack="+stepBack);
		alert("idString="+idString);
		alert("dontStartLoop="+dontStartLoop);
		alert("nextStep="+nextStep);
		alert("commonProcess="+commonProcess);
		alert("forCommonBlock="+forCommonBlock);
		alert("forDiffBlock="+forDiffBlock);
		alert("forDiffBlockStr="+forDiffBlockStr);
		alert("forCommonShelves="+forCommonShelves);
		alert("forDiffShelves="+forDiffShelves);
		alert("loopStep="+loopStep);
		alert("oldimagename="+oldimagename);
		alert("forBlock2="+forBlock2);
		alert("forBlock2Index="+forBlock2Index);
		alert("quesText="+quesText);
*/
	showModelNo($(thisObj).attr('showModelNo'));
	if ($(thisObj).attr('clearTail') != '0' && stepList == undefined ) {
		clearTail(thisObj, 'step'+$(thisObj).attr('stepno'));
	} else if ($(thisObj).attr('clearTail') != '0') {
		clearTail(thisObj, 'step'+step);
	}
	if ($('#step'+step)) {
		var count = 0;
		var allChoices = $('#step'+step).children().children('.choices');
		for (i=0;i<allChoices.length;i++) {
			if ($(allChoices[i]).css('display') != 'none') {
				count++;
			}
		}
		if (count==1) {
			for (i=0;i<allChoices.length;i++) {
				if ($(allChoices[i]).css('display') != 'none') {
					var temp = $(allChoices[i]).children();
					var temp = $(temp[1]).children();
					temp[0].checked = true;
					$(temp[0]).click();
				}
			}
		}
	}
	return true;
}




function showModelNo(cnt) {
	var allSteps = $('#main_content').children().children().children().children('.step');
    var startHiding = false;
	var html = '';
	var modelNo = 'H';
	var imgName = 'H';
	var removeUnderscore = 0;
	var firstCommon = 0;
	var lastCommon = 0;
	var actLoop = 0;

    for (var i=1;i<=allSteps.length;i++) {
        if (allSteps[i-1].id && allSteps[i-1].id.match('stepblock') ==  null) {
			var allOpts = $('#'+allSteps[i-1].id).children().children();
			for(j=1; j<allOpts.length; j++) {
				var temp = $(allOpts[j]).children();
				var opts = $(temp[1]).children();
				if (opts[0].checked == true) {
					removeUnderscore = ($(opts[0]).attr('removeUnderscore') != undefined ? $(opts[0]).attr('removeUnderscore') : removeUnderscore);
					firstCommon = ($(opts[0]).attr('firstCommon') != undefined ? $(opts[0]).attr('firstCommon') : firstCommon);
					lastCommon = ($(opts[0]).attr('lastCommon') != undefined ? $(opts[0]).attr('lastCommon') : lastCommon);
					actLoop = (i == 14 && $(opts[0]).attr('loop') != undefined ? $(opts[0]).attr('loop') : actLoop);
					var tmp = opts[0].value.split('#');
					if (tmp[1] != undefined) {
						if (tmp[2] != null && tmp[2] != undefined) {
							imgName += tmp[2];
						}
						if (removeUnderscore == '1' && tmp[0].substr(0,1) == '-' && i == 16) {
							tmp[0] = tmp[0].replace('-', '');
						}
						modelNo += (tmp[0] != '' && tmp[0] != null ? tmp[0]: '');
						html = ( (html != '' && tmp[1] ? html+', ':'')+tmp[1]);
					}
				}
			}
        } else if (allSteps[i-1].id) {
			var allOpts = $('#'+allSteps[i-1].id+' input');
			for(j=0; j<allOpts.length; j++) {
				allOpts[j].checked = false;
			}
		}
    }
	var tmp = modelNo.split('-');
	if (parseInt(firstCommon) > 0 && tmp.length > 1) {
		modelNo = tmp[0]+'-'+tmp[1];
		for(var i=0; i<parseInt(firstCommon) && tmp[2] != undefined; i++) {
			modelNo += '-'+tmp[2];
		}
		if (actLoop == 3 && tmp[3] != undefined) {
			modelNo += '-'+tmp[3];
		}
	}
	var tmp = modelNo.split('-');
	if (parseInt(lastCommon) > 0 && tmp.length > 1) {
		modelNo = tmp[0]+'-'+tmp[1];
		if (tmp[2] != undefined) {
			modelNo += '-'+tmp[2];
		}
		for(var i=0; i<parseInt(lastCommon) && tmp[3] != undefined; i++) {
			modelNo += '-'+tmp[3];
		}
	}
	if (imgName != '') {
		ajaxRequest(imgName, oldimagename);
	}
	$('#step31').html('<span class="headmaroon">'+modelNo+'</span>');
	$('#modelLabel2').html('<span class="headmaroon">'+modelNo+'</span>');
	$('#dealerLink').html('<a href="/dealer-locator.php?buildModel='+modelNo+'" target="_blank">Click to find a dealer</a>');
	return true;
}

function clearTail(thisObj, id) {
	var allSteps = $('#main_content').children().children().children().children();
	var startRemoving = false;
	for (i=0;i<allSteps.length;i++) {
		if (startRemoving == true) {
			if (allSteps[i].id) {
				$('#'+allSteps[i].id).remove();
				step--;
			}
		}
		if(allSteps[i].id == id) {
		   startRemoving = true;
		}
	}
}

function clearAll() {
	var allSteps = $('#main_content').children().children().children().children('.step');
	for (i=0;i<allSteps.length;i++) {
		if (allSteps[i].id) {
			$('#'+allSteps[i].id).hide();
			if(allSteps[i].id == 'step1') {
				$('#'+allSteps[i].id).show();
			}
		}
	}
}