var xmlDataSet = "";

$(document).ready(function(){
	
	var rand=Math.floor(Math.random()*999999999999999999999);
	
	$.ajax({
		type: "GET",
		url: "static/dispatch.xml",
		data: "r=" + rand,
		dataType: "xml",
		success: function(xmlData){
			xmlDataSet = xmlData;
			getGACCS();
		}
	})
	
	// EaTIS form submit
    $("#dplsubmit").click(function(){
            if($("#dpl").val() != "none"){
                window.open("static/dispatch_priority_lists/eatis/" + $("#dpl").val());
            }
            return false;
    });
    
    $('#getpdf').click(function(){
		var f = $('#lists option:selected');
		if(f.val() != ""){
			window.open("static/dispatch_priority_lists/" + f.val());	
		}		
	})
});

function getGACCS(){
		$('gacc',xmlDataSet).each(function(i){
			gaccname = $(this).find("gacc_name").text();
			gacccode = $(this).find("gacc_code").text()
			$("#gaccs").append("<option value='" + gacccode + "'>" + gaccname + "</option>");
			$("#gaccs").children(":first").text("Select a Geographic Area").attr("selected", true);
		});
		$("#gaccs").change(function(){
			if($("#gaccs").val() != ""){
				$("#dclist select").attr("disabled", false);
				$('#lists').attr("disabled", true);
				$('#lists').empty();
				$('#lists').append("<option value=''>Select a Dispatch Priority List</option>");
				getDispatchLists();
			} else {
				$("#dclist select").attr("disabled", true);
				$("#dispatchcenters").empty();
				$("#dispatchcenters").append("<option value=''>Select a Dispatch Center</option>");
				$('#listlist button').attr("disabled", true)
				$('#lists').attr("disabled", true);
				$('#lists').empty();
				$('#lists').append("<option value=''>Select a Dispatch Priority List</option>");
			}
		});
}

function getDispatchLists(){
	//reset the list to it's base incase the GACC was changed
	$("#dispatchcenters").empty();
	$("#dispatchcenters").append("<option value=''>Select a Dispatch Center</option>");
	gacc = $("#gaccs").val();
	$('gacc_code',xmlDataSet).each(function(y){
			if($(this).text() == gacc){
				$(this).siblings().each(function(d){
					dcn = $(this).find("dispatch_center_name").text();
					if(dcn != ''){
						$("#dispatchcenters").append("<option value='" + dcn + "'>" + dcn + "</option>");	
					}					
				});
			}
	});
	$("#dispatchcenters").change(function(){
		if($("#dispatchcenters").val() != ""){
			$('#lists').attr("disabled", false);
			$('#listlist button').attr("disabled", false);
			getLists();
		} else {
			$('#lists').attr("disabled", true);
			$('#listlist button').attr("disabled", true);
			$('#lists').empty();
			$('#lists').append("<option value=''>Select a Dispatch Priority List</option>");
		}
	})
}

function getLists(){
	//reset the list to it's base incase the dispatch center was changed
	var lists = new Array();
	$('#lists').empty();
	$('#lists').append("<option value=''>Select a Dispatch Priority List</option>");
	dc = $('#dispatchcenters').val();
	$('dispatch_center_name',xmlDataSet).each(function(g){
		if($(this).text() == dc){
			$(this).siblings().each(function(i){
				rtn = $(this).find("resource_type_name").text();				
				file = $(this).find("filename").text();
				var list = new Array();
				list[0]= rtn;
				list[1]= file;
				lists.push(list);
			});
			lists.sort(multisort);
			var len = lists.length;
			for (var i=0; i<len; i++){
				$('#lists').append("<option value='" + lists[i][1] + "'>" + lists[i][0] + "</option>");
			}
		}
	});
	
}

function checkLists(){
	var f = $('#lists option:selected');
	if(f.val() != ""){
		console.log('window open');
		window.open("static/dispatch_priority_lists/" + f.val());	
	}	
}

function multisort(a,b){
	var x = a[0].toLowerCase();
    var y = b[0].toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}















