var oForm 			= document.fSolucao;
var matrixPointer 	= 0;
var matrix 			= new Array();
var auxMatrix 		= new Array();
var questionMatrix 	= new Array();
var tipoSolucao;
var hasIP = false; //se cliente solicitou tecnologia IP (p10)

questionMatrix['default']							= [0, 1];

questionMatrix['vozCasa'] 							= [2, 3, 4, 6, 7];
questionMatrix['vozCasaSemVoIP'] 					= [2, 3, 4, 6, 7];

questionMatrix['dadosCasa'] 						= [222, 14];
questionMatrix['dadosCasaComFio'] 					= [222, 14, 240];
questionMatrix['dadosCasaSemFio'] 					= [222, 14, 23];

questionMatrix['vozEmpresa'] 						= [21, 9, 10, 25, 5, 11, 12, 13];

questionMatrix['dadosEmpresa'] 						= [19, 222];
questionMatrix['dadosEmpresaComFio'] 				= [19, 222, 24, 241, 16];
questionMatrix['dadosEmpresaSemFio']				= [19, 222, 18, 23];
questionMatrix['dadosEmpresaComFioSemFio']			= [19, 222, 24, 241, 16, 18, 23];

/*
questionMatrix["dadosEmpresa"] 						= [22];
questionMatrix["dadosEmpresaComFio"] 				= [22, 24, 16, 20];
questionMatrix["dadosEmpresaComFioSemGer"] 			= [22, 24, 20];
questionMatrix["dadosEmpresaSemFio"] 				= [22, 17, 18, 19];
questionMatrix["dadosEmpresaSemFio108"] 			= [22, 17, 18];
*/

function executeStep()
{
	switch(getCurStepId())
	{	
		case 0: // Casa ou empresa		
			if(getCheckedValue(oForm.p_0)  == '')
			{
				RaiseError(0);
				return false;
			}

			if(getCheckedValue(oForm.p_0)  == "0_1")
			{
				tipoSolucao = "Casa";
			}
			else
			{
				tipoSolucao = "Empresa";
			}
			break;			
		case 1: // Voz ou dados
		
			if(getCheckedValue(oForm.p_1)  == '')
			{
				RaiseError(0);
				return false;
			}
						
			switch(getCheckedValue(oForm.p_1))
			{
				case "1_1": // voz
					resetMatrix();					
					setMatrix("voz", questionMatrix["voz" + tipoSolucao]);
					break;
				case "1_2": // dados
					resetMatrix();
					setMatrix("dados", questionMatrix["dados" + tipoSolucao]);
					break;
				case "1_3": // voz e dados
					setMatrix("voz", questionMatrix["voz" + tipoSolucao]);
					setMatrix("dados", questionMatrix["dados" + tipoSolucao]);
					break;
			}//switch p_1 value
			break;			
		case 2: // Casa-Voz-Ramais		
			if(getCheckedValue(oForm.p_2)  == "")
			{
				RaiseError(0); 
				return false;
			}
			break;
		case 3: // Casa-Voz-VoIP		
			if(getCheckedValue(oForm.p_3)  == "")
			{
				RaiseError(0);
				return false;
			}
			if(getCheckedValue(document.fSolucao.p_3) == "3_1")// quer VoIP
			{ 
				setMatrix("voz", questionMatrix["voz" + tipoSolucao]);
				$("div#dv_p_6").show(); // mostrar telefones VoIP
				$("div#dv_p_6_dica").show(); // mostrar dica telefones VoIP
				$("div#dv_p_8").show(); // mostrar sofphone
			}
			else
			{
				setMatrix("voz", questionMatrix["voz" + tipoSolucao + "SemVoIP"]);
				$("div#dv_p_6").hide(); // esconder telefones VoIP
				$("div#dv_p_6_dica").hide(); // esconder dica telefones VoIP
				$("div#dv_p_8").hide(); // esconder sofphone
			}
			break;
			
		case 4:// Casa-Voz-Auto atendimento		
			if(getCheckedValue(oForm.p_4)  == "")
			{
				RaiseError(0);
				return false;
			}
			break;			
		case 25: // Empresa-Voz-Auto atendimento		
			if(getCheckedValue(oForm.p_25)  == "")
			{
				RaiseError(0);
				return false;
			}
			break;			
		case 5: // Casa/Empresa-Voz config remoto		
			if(getCheckedValue(oForm.p_5)  == "")
			{
				RaiseError(0);
				return false;
			}			
			break;			
		case 6: // Casa-Voz-Telefones
			break;			
		case 7: // Casa-Voz-Funcionalidades		
			break;			
		case 8: // Casa-Voz-Softwares		
			break;			
		case 9: // Empresa-Voz-Ramais		
			if(getCheckedValue(oForm.p_9)  == "")
			{
				RaiseError(0);
				return false;
			}
			break;

		case 10: // Empresa-Voz-VoIP
			if(getCheckedValue(document.fSolucao.p_10) == "10_3")// nao quer VoIP
			{
				$("div#dv_p_11").hide(); // esconder telefones VoIP
				$("div#dv_p_13").hide(); // esconder sofphone				
			}
			else
			{
				$("div#dv_p_11").show(); // mostrar telefones VoIP
				$("div#dv_p_13").show(); // mostrar sofphone
			}
			break;
			
		case 11: // Empresa-Voz - Telefones		
			break;			
		case 12: // Empresa-Voz - Equipamentos
			break;			
		case 13: // Empresa-Voz - Softwares		
			break;			
		case 14: // Casa-Dados-Wifi		
			if(getCheckedValue(oForm.p_14)  == "14_1") // com fio
			{
				setMatrix("dados", questionMatrix["dados" + tipoSolucao + "ComFio"]);
			}
			else  // sem fio
			{
				setMatrix("dados", questionMatrix["dados" + tipoSolucao + "SemFio"]);
			} 
			break;			
		case 15: // Casa-Dados-ComFio-Gerenciamento e alimentaocao		
			if(getCheckedValue(oForm.p_15)  == "")
			{
				RaiseError(0);
				return false;
			}
			break;			
		case 24: // Empresa-Dados-ComFio-Gerenciamento		
			if(getCheckedValue(oForm.p_24)  == "")
			{
				RaiseError(0);
				return false;
			}	
			/*		
			if(getCheckedValue(oForm.p_24)  == "24_1") // com gerenciamento
			{
				setMatrix("dados", questionMatrix["dados" + tipoSolucao + "ComFio"]);
			}
			else // sem gerenciamento
			{
				setMatrix("dados", questionMatrix["dados" + tipoSolucao + "ComFioSemGer"]);
			}
			* */
			break;	
		case 241:
			if(getCheckedValue(oForm.p_241)  == "")
			{
				RaiseError(0);
				return false;
			}
			
			if(!($('#p_241_1').val() > 0))
			{
				RaiseError(0, 'Uma rede deve ter pelo menos um equipamento.');
				return false;
			}
			
			var options = '';
			var total = $('#p_241_1').val();
			//alert(total);

			for(var i = 0; i <= total; i++)
			{			
				options += '<option value="' + i + '">' + i + '</option>';
			}//for i		
			$("#p_16_1").html(options);	
			break;			
		case 16: // Casa-Empresa-Dados-ComFio-PoE (power over ethernet)		
			if(getCheckedValue(oForm.p_16)  == "")
			{
				RaiseError(0);
				return false;
			}			
					
			/*
			if(getCheckedValue(oForm.p_16)  == "16_1") // gerenciamento E PoE
			{
				$("div#dv_p_20_1").show(); // até 8
				$("div#dv_p_20_2").show(); // mais de 8 
				$("div#dv_p_20_3").hide(); // até 24
				$("div#dv_p_20_4").hide(); // mais de 24
			}
			else if(getCheckedValue(oForm.p_16)  == "16_2" )// gerenciamento E NÃO PoE
			{ 
				$("div#dv_p_20_1").hide(); // até 8
				$("div#dv_p_20_2").hide(); // mais de 8 
				$("div#dv_p_20_3").show(); // até 24
				$("div#dv_p_20_4").show(); // mais de 24
			} */
			break;			
		case 17: // Empresa-Dados-SemFio-Computadores (tipos e quantidades)		
//			if (  parseInt(oForm.p_17_4.value) > 0 ) { 	// se tem desktop sem Wi-Fi, mostrar pergunta 18
//				setMatrix("dados", questionMatrix["dados" + tipoSolucao + "SemFioDesktopSemWiFi"]);
//			} else {
//				setMatrix("dados", questionMatrix["dados" + tipoSolucao + "SemFio"]);
//			}
			break;			
		case 18: // Casa-Empresa-Dados-SemFio-Banda (300 Mbps ou 108 Mbps)		
			if(getCheckedValue(oForm.p_18)  == "")
			{
				RaiseError(0);
				return false;
			}	
			
			if(getCheckedValue(oForm.p_18) == "18_1") // sim
			{ 
				$("#dv_p_23_1").show();
				$("#dv_p_23_2").hide();
			}
			else // nao
			{
				$("#dv_p_23_1").hide();
				$("#dv_p_23_2").show();
			}
			break;			
		case 19: // Casa-Empresa-Como quer sua rede?
			if(getCheckedValue(oForm.p_19)  == "19_1") // cabeada
			{
				setMatrix('dados', questionMatrix['dadosEmpresaComFio']);
			}//if 19_1
			else if(getCheckedValue(oForm.p_19)  == "19_2") // wifi
			{
				setMatrix('dados', questionMatrix['dadosEmpresaSemFio']);
			}//else if 19_2
			else // mista
			{
				setMatrix('dados', questionMatrix['dadosEmpresaComFioSemFio']);				
			}//else 19_3 
			break;	
		case 21: // Empresa-Voz-Tronco digital R2		
			if(getCheckedValue(oForm.p_21)  == "")
			{
				RaiseError(0);
				return false;
			}
			if(getCheckedValue(oForm.p_21)  == "21_1") // sim
			{ 
				$("#h5_p_9_1").show();
				$("#h5_p_9_2").hide();			
			
				$("div#dv_p_9_1").show();
				$("div#dv_p_9_2").hide();
			}
			else // nao
			{
				$("#h5_p_9_2").show();
				$("#h5_p_9_1").hide();			
				
				$("div#dv_p_9_1").hide();
				$("div#dv_p_9_2").show();
			}
			break;
			
		case 22: // Empresa-Dados-Wi-Fi		
			if(getCheckedValue(oForm.p_22)  == "22_1") // com fio
			{
				setMatrix("dados", questionMatrix["dados" + tipoSolucao + "ComFio"]);
			}
			else // sem fio
			{
				setMatrix("dados", questionMatrix["dados" + tipoSolucao + "SemFio"]);
			} 
			break;	
		case 23: //Casa-Dados-SemFio-Computadores (tipos e quantidades)
			break;			
	}//switch cur step id
		
	if(isLastStep())
	{
		$("div#aba_" + getCurStepId()).hide();
		$("div#endAba").show();
		$("div#bt_previous").hide();
		oForm.submit();
		return false;
	}//if last step
		
	return true;
}//executeStep

function next()
{
	if(!isNextAvailable())return;
	
	if(!executeStep())return;

	$("div#aba_" + getCurStepId()).hide();
	$("div#aba_" + getNextStepId()).show();
	
	matrixPointer++;
	setNavHeaderVisibility();
	setNavButtonVisibility();
}//next

function previous()
{
	if(!isPreviousAvailable())return;

	$("div#aba_" + getCurStepId()).hide();
	$("div#aba_" + getPreviousStepId()).show();
	
	matrixPointer--;
	setNavHeaderVisibility();
	setNavButtonVisibility();
}//previous

function getCurStepId()
{
	return matrix[matrixPointer];
}//getCurStepId

function getNextStepId()
{
	return matrix[matrixPointer + 1];
}//getNextStepId

function getPreviousStepId()
{
	return matrix[matrixPointer - 1];
}//getPreviousStepId

function isNextAvailable()
{
	return (matrixPointer < matrix.length);
}//isNextAvailable

function isPreviousAvailable()
{
	return (matrixPointer > 0);
}//isPreviousAvailable

function isLastStep()
{
	return (matrixPointer == (matrix.length - 1)); 
}//isLastStep

function setNavHeaderVisibility()
{
	multiplyTag(matrix.length,"minhasolucao");
	for(var i = 0; i <= matrixPointer; i++)changeStatus("id_" + (i + 1), 'verde');
	changeStatus("id_" + (matrixPointer + 1), 'amarelo');
}//setNavHeaderVisibility

function setNavButtonVisibility()
{
	if(isNextAvailable())
	{
		if(getCheckedValue(oForm.elements["p_" + getCurStepId()]) == "")disableNext();
			else enableNext();		
	}//isNextAvailable
	else $("div#bt_next").hide();
	
	if(isPreviousAvailable())$("div#bt_previous").show();
		else $("div#bt_previous").hide();
}//setNavButtonVisibility

function setMatrix(type, newQuestionMatrix)
{	
	auxMatrix[type] = newQuestionMatrix;
	matrix = auxMatrix["default"];
	
	if(auxMatrix["voz"].length >= 1)matrix = matrix.concat(auxMatrix["voz"]);	
	if(auxMatrix["dados"].length >= 1)matrix = matrix.concat(auxMatrix["dados"]);

	setFormQuestions();
}//setMatrix

function appendToMatrix(type, questions)
{
	setMatrix(type, auxMatrix[type].concat(questions));
}//appendToMatrix

function setFormQuestions()
{
	multiplyTag(matrix.length,"minhasolucao");
	
	oForm.matrixLength.value = matrix.length;
	oForm.matrix.value = "";
	  
	for(var i = 0; i < matrix.length; i++)oForm.matrix.value += matrix[i] + ",";	
}//setFormQuestions

function resetMatrix()
{
	auxMatrix["voz"] = new Array();
	auxMatrix["dados"] = new Array();
	setMatrix("default", questionMatrix["default"])
}//resetMatrix

function load()
{
	for(var i = 0; i <= 21; i++)$("div#aba_" + i).hide();
	resetMatrix();

	$("div#aba_" + getCurStepId()).show();
	
	multiplyTag(matrix.length,"minhasolucao");
	changeStatus('id_1','bltIntroamarelo');
	
	setNavButtonVisibility();	
	
	$("div#bt_next").show();
	$("div#bt_next_disabled").hide();	
}//load

function getCheckedValue(radioObj)
{
	if(!radioObj)return "Not a radio object";
	
	var radioLength = radioObj.length;
	if(radioLength == undefined)
	{
		if(radioObj.checked)return radioObj.value;
			else return "";
	}	
	for(var i = 0; i < radioLength; i++)if(radioObj[i].checked)return radioObj[i].value;	
	return "";
} //getCheckedValue

function enableNext()
{
	$("div#bt_next_disabled").hide();
	$("div#bt_next").show();	
}//enableNext

function disableNext()
{
	$("div#bt_next").hide();
	$("div#bt_next_disabled").show();
}//disableNext

function RaiseError(step, msg)
{
	if(msg == undefined)msg = "Selecione uma op&ccedil;&atilde;o"; 
	alert(msg);
}//RaiseError

load();
