$(document).ready(function(){

	$("#probleemCause1").change(function(){loadCause2();});
	$("#probleemCause2").change(function(){loadSolution();});

	loadCause1();
});

function initForms(){					
	//addHistory("initForms();");
	
	$("#description").empty();
	$("#solution").empty();

	$("#stap2").hide();
	$("#stap3").hide();
	$("#stap4").hide();
	$("#stap5").hide();
}

function loadCause1(){	
	initForms();
	addHistory("loadCause1();");

  $.getJSON("/turbos-parts/turbos/problemanalysis/jsonCalls.asp", {action: "cause1", language: language},
	function(data){

		if(data.rows.length > 0){
			$("#probleemCause1").empty();
			
			$.each(data.rows, function(i, item){
           		$("#probleemCause1").append($("<option/>").attr("value", item.cause1).html(item.cause1));
			});

			initial = $("<option class='initial' selected='selected'/>").attr("value", "").html(cause1Initial);
      		$("#probleemCause1").prepend(initial);
			setTimeout('initial.attr("selected", "selected");', 150);
			setTimeout('$("#probleemCause1").val(initial);', 150);
		}
	}
  );
}


function loadCause2(){	
	
	initForms();

	if($("#probleemCause1").val() == ""){
		return;
	}
		
	addHistory("loadCause2();");

  $.getJSON("/turbos-parts/turbos/problemanalysis/jsonCalls.asp", {action: "cause2", cause1: $("#probleemCause1").val(), language: language},
	function(data){

		if(data.rows.length > 0){
			$("#probleemCause2").empty();
			
			$.each(data.rows, function(i, item){
           		$("#probleemCause2").append($("<option/>").attr("value", item.cause2).html(item.cause2));
			});

			initial = $("<option class='initial' selected='selected'/>").attr("value", "").html(cause2Initial);
      		$("#probleemCause2").prepend(initial);
			setTimeout('initial.attr("selected", "selected");', 150);
			setTimeout('$("#probleemCause2").val(initial);', 150);
			
			$("#stap2").show();
			$("#stap3").show();
		}else{
			initForms();
		}
	}
  );
}

function loadSolution(){	

	$("#stap4").hide();
	$("#stap5").hide();
	$("#description").empty();
	$("#solution").empty();

	if($("#probleemCause2").val() == ""){
		return;
	}
		
	addHistory("loadSolution();");

  $.getJSON("/turbos-parts/turbos/problemanalysis/jsonCalls.asp", {action: "solution", cause1: $("#probleemCause1").val(), cause2: $("#probleemCause2").val(), language: language},
	function(data){

		if(data.rows.length > 0){
			
			var desc = "";
			var sol  = "";
			$.each(data.rows, function(i, item){
				if(i = 1){
					if(desc != item.description){ 
						desc = item.description;
	           			$("#description").append($("<p/>").html(item.description));
					}
					if(sol != item.solution){
						sol  = item.solution;
	           			$("#solution").append($("<p/>").html(item.solution));
					}
				}
			});
			$("#stap4").show();
			$("#stap5").show();
		}else{
			initForms();
		}
	}
  );
}

var currentState = '';
function handleHistory(state){
	if(state != currentState){
		//alert(state);
		currentState = state;
		setTimeout(state, 100);
		//console.log(state);
	}
}
function addHistory(state){
    $("#historyFrame").attr("src", "/include/history.asp?action="+state);
    currentState = state;
}