
var xhrgetTT = null;
var resultsTimergetTT = null;

function getTT()
{		
	clearTimeout( resultsTimergetTT );
	
	if (xhrgetTT)
	{
		xhrgetTT.abort();
	} 
 
	resultsTimergetTT = setTimeout(
		function(){
                        $("#resultTable").html("<center><br><br><br><br><img src='http://www.feeditout.com/SoftwareTesting/big.gif' /></center>");
			xhrgetTT = $.get("http://www.feeditout.com/tools/tt.php", 
			{ 
				id: $('#idNumber').val()
				}, 
				function(data)
				{
					$('#resultTable').html(data);
                                        $('#idRight').text("");
		   		}
			);
		},
		500
	);	
}


function getCourses()
{			
	$('#nmodules').attr('disabled', 'disabled');
	$("#ncoursesi").html("<img src='http://www.feeditout.com/SoftwareTesting/big.gif' width='15' height='15' />");
	$("#nresult").empty();

	clearTimeout( resultsTimergetTT );
	
	if (xhrgetTT)
	{
		xhrgetTT.abort();
	} 
 
	resultsTimergetTT = setTimeout(
		function(){                       
			xhrgetTT = $.get("http://www.feeditout.com/tools/notes.php", 
			{ 
				ncourses: 'true'
				}, 
				function(data)
				{
					$("#ncoursesi").html("");
					$('#ncourses').empty();
					$('#ncourses').append($("<option></option>").attr("value",'').text('Select a course')); 
					//$('#ncourses').append($("<option></option>").attr("value",'all').text('All courses')); 

					var courses = data.split('|');
					for( var r = 0; r < courses.length -1; r++ )
					{
						$('#ncourses').append($("<option></option>").attr("value",courses[r]).text(courses[r])); 
					}
		   		}
			);
		},
		500
	);	
}


function getModules( val )
{		
	if( val == "" )
	{
		return;
	}

	$("#nresult").empty();
	$('#nmodules').attr('disabled', 'disabled');	
	$("#nmodulesi").html("<img src='http://www.feeditout.com/SoftwareTesting/big.gif' width='15' height='15' />");

	clearTimeout( resultsTimergetTT );
	
	if (xhrgetTT)
	{
		xhrgetTT.abort();
	} 
 
	resultsTimergetTT = setTimeout(
		function(){       			              
			xhrgetTT = $.get("http://www.feeditout.com/tools/notes.php", 
			{ 
				nmodules: val
				}, 
				function(data)
				{
					$('#nmodules').empty();
					$("#nmodulesi").html("");
					$('#nmodules').append($("<option></option>").attr("value",'').text('Select a module')); 
					$('#nmodules').append($("<option></option>").attr("value",'all').text('All modules')); 

					var modules = data.split('|');
					for( var r = 0; r < modules.length -1; r++ )
					{
						$('#nmodules').append($("<option></option>").attr("value",modules[r]).text(modules[r])); 
					}
					$('#nmodules').removeAttr('disabled');
		   		}
			);
		},
		500
	);	
}



function getDocuments( course, module )
{		
	if( module == "" )
	{
		return;
	}

	$("#nresult").html("<center><br><br><br><br><img src='http://www.feeditout.com/SoftwareTesting/big.gif' /></center>");

	clearTimeout( resultsTimergetTT );
	
	if (xhrgetTT)
	{
		xhrgetTT.abort();
	} 
 
	resultsTimergetTT = setTimeout(
		function(){       			              
			xhrgetTT = $.get("http://www.feeditout.com/tools/notes.php", 
			{ 
				dmodules: module,
				dcourses: course
				}, 
				function(data)
				{
					$("#nresult").html(data);	
					$('.goToTop').click(function() 
					{
					     $( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
					});												
		   		}
			);
		},
		500
	);	
}



function quickView( doc )
{
    $("#quickView").html("<h5>Quick View</h5><hr class='myHr'><iframe src=\"http://docs.google.com/viewer?url=" + doc + "&amp;embedded=true\" width=\"100%\" height=\"500\" frameborder=\"0\" style=\"min-width:305px;\" class=\"gde-frame\"></iframe></iframe>");
    $( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
}


$(document).ready(function()
{
    $('#idNumber').keyup( function()
    {  
       if( /^\d+$/.test( $('#idNumber').val() ) )
       {
           var id = $('#idNumber').val();
           if( id.length > 6  && id.length < 9 )
           {
               $('#idRight').text("standy");
               getTT();
           }
           else
           {
               $('#idRight').text("Invalid ID");
           }
       }
       else
       {
           $('#idRight').text("ID not numeric");
       }
    });    

    if ($('#ncourses').length) 
    {
        getCourses();    
        $('#ncourses').change(function() 
        {
	     if( $(this).val() =="" )
	         return;

	     getModules($(this).val());
	});

	$('#nmodules').change(function() 
        {
	     if( $(this).val() == "" )
	         return;

	     getDocuments( $('#ncourses').val() , $(this).val() );
	});
    }
});


