var editCommentID;
var editCommentTable;

enablerating = 1;
enableratingsend = 1;

function getHttpRequest()
{
	var handle = false; 
	
	try
	{    // Firefox, Opera 8.0+, Safari
		handle = new XMLHttpRequest();
	}
	catch( e )
	{    // Internet Explorer
		try
		{
			handle = new ActiveXObject( "Msxml2.XMLHTTP" );
		}
		catch( e )
		{
			try
			{
				handle = new ActiveXObject( "Microsoft.XMLHTTP" );
			}
			catch( e )
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	return handle;
}

function handleRequest_setShadow( handle ) 
{ 
	if( handle.readyState != 4 ) return;
	
	if (handle.status && handle.status != 200)
	{ 
		alert('Error! index_track_ajax - ' + handle.status ); 
		return;
	}
	
	xmlDoc = handle.responseXML;
	
	document.getElementById( "shadow1_str" ).innerHTML = xmlDoc.getElementsByTagName( "name" )[0].childNodes[0].nodeValue;
	document.getElementById( "shadow1_id" ).value = xmlDoc.getElementsByTagName( "id" )[0].childNodes[0].nodeValue;
	document.getElementById( "shadow1_arrow" ).innerHTML = '<img src="' + xmlDoc.getElementsByTagName( "arrowimg" )[0].childNodes[0].nodeValue + '" width="10" height="10" />';
	document.getElementById( "shadow2_str" ).innerHTML = xmlDoc.getElementsByTagName( "name" )[1].childNodes[0].nodeValue;
	document.getElementById( "shadow2_id" ).value = xmlDoc.getElementsByTagName( "id" )[1].childNodes[0].nodeValue;
	document.getElementById( "shadow2_arrow" ).innerHTML = '<img src="' + xmlDoc.getElementsByTagName( "arrowimg" )[1].childNodes[0].nodeValue + '" width="10" height="10" />';
	
	if( xmlDoc.getElementsByTagName( "id" )[0].childNodes[0].nodeValue == 0 && xmlDoc.getElementsByTagName( "id" )[1].childNodes[0].nodeValue == 0 )
	{
		// Båda id är 0 - Gör Watch Replay knappen disabled
		document.getElementById( "watchreplaybtn" ).disabled = true;
	}
	else
	{
		// Någon id är vald - Gör Watch Replay knappen enabled
		document.getElementById( "watchreplaybtn" ).disabled = false;		
	}
}

function handleRequest_commentFetch( handle ) 
{ 
	if( handle.readyState != 4 ) return;
	
	if (handle.status && handle.status != 200)
	{ 
		alert('Error! index_track_ajax - ' + handle.status ); 
		return;
	}
	
	document.getElementById( "comment_" + editCommentTable + "_" + editCommentID ).innerHTML = handle.responseText;
}

function handleRequest_ratingSelect( handle ) 
{ 
	if( handle.readyState != 4 ) return;
	
	if (handle.status && handle.status != 200)
	{ 
		alert('Error! index_track_ajax - ' + handle.status ); 
		return;
	}
	
	enableratingsend = 1;
}

function ajax_selectShadow( shadowid )
{
	var http_request = getHttpRequest(); 

  http_request.onreadystatechange = function() { handleRequest_setShadow( http_request ); }; 
	
	http_request.open( "GET", "ajax.php?do=selectShadow&id=" + shadowid + "&no-cache=" + Math.random(), true );
	http_request.send( null );
}

function ajax_clearShadow( id )
{
	var http_request = getHttpRequest(); 

  http_request.onreadystatechange = function() { handleRequest_setShadow( http_request ); }; 
	
	http_request.open( "GET", "ajax.php?do=clearShadow&id=" + id + "&no-cache=" + Math.random(), true );
	http_request.send( null );
}

function ajax_nextShadow( id )
{
	var http_request = getHttpRequest(); 

  http_request.onreadystatechange = function() { handleRequest_setShadow( http_request ); }; 
	
	http_request.open( "GET", "ajax.php?do=nextShadow&id=" + id + "&no-cache=" + Math.random(), true );
	http_request.send( null );
}

function ajax_editcomment( table, trackid, more, page, id )
{
	// Kom ihåg vilket race id och table som man editerar i en global variabel. Detta så man vet var man ska placera htmlkoden som man får tillbaka från php skriptet.
	editCommentID = id; 
	editCommentTable = table;
	
	var http_request = getHttpRequest(); 

  http_request.onreadystatechange = function() { handleRequest_commentFetch( http_request ); }; 

	http_request.open( "GET", "ajax.php?do=commentFetch&id=" + id + "&trackid=" + trackid + "&more=" + more + "&page=" + page + "&no-cache=" + Math.random(), true );
	http_request.send( null );
}

function ratingstarover( sid )
{
	if( enablerating == 1 )
	{
		counter = 5;
		while( counter < 6 && counter > 0 )
		{
			if( counter > sid )
			{
				document.getElementById( 'ratingstar' + counter ).src = ratingstar_off;
//				document.getElementById( 'ratingstar' + counter ).src = 'http://m3demo.shacknet.nu/images/ratingstar_off.gif';
			} else {
				document.getElementById( 'ratingstar' + counter ).src = ratingstar_on;
//				document.getElementById( 'ratingstar' + counter ).src = 'http://m3demo.shacknet.nu/images/ratingstar_on.gif';
			}
			counter--;
		}
	}
}

function ratingstardown( sid, tid )
{
	enablerating = 1;
	
	if( enableratingsend == 1 )
	{
		ratingstarover( sid );
		
		var http_request = getHttpRequest(); 

  	http_request.onreadystatechange = function() { handleRequest_ratingSelect( http_request ); }; 
	
		http_request.open( "GET", "ajax.php?do=setRating&trackid=" + tid + "&rating=" + sid + "&no-cache=" + Math.random(), true );
		http_request.send( null );
		
		enableratingsend = 0;
	}
	
	enablerating = 0;
}




