

var g_xmlHttp = null;


//
// file pre-upload 
//

var numberOfPlayers = 2;

function musicianFields() {
	var n = document.getElementById( "numberOfPlayers" ).value;
	var list = document.getElementById( "musiciansList" );
	n = ( n > 10 ? 10 : n );
	n = ( n < 2 ? 2 : n );
	if ( numberOfPlayers < n ) {
		for ( var i = numberOfPlayers; i < n; ++i ) {
			list.innerHTML += singlePlayer( i );
		}
	} else {
		for ( var i = numberOfPlayers; i > n; --i ) {
			list.removeChild( list.lastChild );
		}
	}
	numberOfPlayers = n;
	document.getElementById( "numberOfPlayers" ).value = n;
}

function setDefaultNumber() {
	document.getElementById( "numberOfPlayers" ).value = 2;
}

function singlePlayer( i ) {
	var ret = "<div style=\"font-size:100%;padding-bottom:0.1cm;\"><input title=\"type name in here\" name=\"player_" + i + "\" type=\"text\" size=\"20\" />";
	ret += " <label>on</label> ";
	ret += "<input title=\"type instrument in here\" type=\"text\" name=\"plays_" + i + "\" size=\"20\" /></div>";
	return ret;
}

function trackProducer( who ) {
	var h = "";
	if ( document.getElementById( "isProducer" ).checked ) { 
		h = "<input type=\"text\" name=\"player_0\" size=\"20\" value=\"" + who + "\" />";
	} else {
		h = singlePlayer( 0 );
	}
	document.getElementById( "uploader" ).innerHTML = h;
}


//
// asynchronous file upload
//

function onUploadFile() {
	// do the normal post but replace the iframe with an upload message
	var iframe = window.parent.document.getElementById( "upload_frame" );
	iframe.style.visibility = "hidden";
	iframe.style.width = "0px";
	iframe.style.height = "0px";

	var div = window.parent.document.getElementById( "upload_container" );
	div.style.marginLeft = "1cm";
	var ud = div.appendChild( document.createElement( "div" ) );
	ud.style.textDecoration = "blink";
	ud.appendChild( document.createTextNode( "Uploading..." ) );
	div.appendChild( document.createElement( "div" ) ).appendChild( document.createTextNode( "This could take a minute or so, depending on the file size." ) );
	return true;
}


//
// downloading a file logs a hit in the database
//

function downloadClicked( download_id ) {
	getHttpRequest();
	if ( !g_xmlHttp ) {
		return;
	} 
	g_xmlHttp.open( "POST", "processor.download.php", false );
	g_xmlHttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
	g_xmlHttp.send( "id=" + download_id );
}

//
// Uploads list sorting by column
//

function render( generatorFile, displayFunction, toSend ) {
	getHttpRequest();
	if ( !g_xmlHttp ) {
		return true;
	}
	g_xmlHttp.open( "POST", generatorFile, true );
	g_xmlHttp.onreadystatechange = displayFunction;
	g_xmlHttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
	g_xmlHttp.send( toSend );
	return false;
}


var m_lastColumn = "";
var m_direction = "DESC";

// generic listbuilder
function getList( column, generatorFile, displayFunction, toSend ) {
	if ( column == m_lastColumn ) {
		m_direction = ( m_direction != "DESC" ? "DESC" : "ASC" );
	} else {
		m_direction = "DESC";
	}
	m_lastColumn = column;
	return render( generatorFile, displayFunction, "col=" + column + "&direction=" + m_direction + ( toSend ? "&" + toSend : "" ) );
}

// generic display Function
function onDisplay( displayElement ) {
	if ( g_xmlHttp.readyState == 4 || g_xmlHttp.readyState == "complete" ) { 
		document.getElementById( displayElement ).innerHTML = g_xmlHttp.responseText;
		g_xmlHttp = null;
	} 
}

// special cases

// uploads
function getUploadsList( column ) {
	return getList( column, "uploads.php", function() { onDisplay( "uploadSummary" ) } );
}

// responses
function getResponsesList( column ) {
	return getList( column, "responses.php", function() { onDisplay( "responsesSummary" ) } );
}

// downloads
// list
var m_lastListClass = "";

function getDownloadsList( column, listClass ) {
	// need a different one of these to getList(...) because of listClass, the name of the php class that draws the list
	if ( column == m_lastColumn ) {
		m_direction = ( m_lastListClass != listClass || m_direction != "DESC" ? "DESC" : "ASC" );
	} else {
		m_direction = "DESC";
	}
	m_lastColumn = column;
	m_lastListClass = listClass;
	return render( "downloads.list.php", function() { onDisplay( "downloadsList" ) }, "col=" + column + "&direction=" + m_direction + "&class=" + listClass );
}


// heading
function getDownloads( listClass ) {
	getHttpRequest();
	if ( !g_xmlHttp ) {
		return true;
	}
	g_xmlHttp.open( "POST", "downloads.select.php", false );
	g_xmlHttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
	g_xmlHttp.send( "class=" + listClass );
	document.getElementById( "downloadHeadings" ).innerHTML = g_xmlHttp.responseText;
	return getDownloadsList( 1, listClass );
}


// track response
// form

function getTrackScreen( screenClass, download_id ) {
	getHttpRequest();
	if ( !g_xmlHttp ) {
		return true;
	}
	g_xmlHttp.open( "POST", "track.select.php", false );
	g_xmlHttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
	g_xmlHttp.send( "class=" + screenClass + "&id=" + download_id );
	document.getElementById( "trackForm" ).innerHTML = g_xmlHttp.responseText;
	return false;
}



//
// g_xmlHttpRequest object 
//

function getHttpRequest() {
	if ( !g_xmlHttp ) {
		if ( window.XMLHttpRequest ) {
			g_xmlHttp = new XMLHttpRequest(); // the rest of the world
		} else if ( window.ActiveXObject ) {
			// m$
			try {
				g_xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" );
			} catch ( e ) {
				try {
					g_xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
				} catch ( e ) {}
			}
		}
	}
	if ( !g_xmlHttp ) {
		alert( "In a nutshell, your browser cannot create an XMLHttpRequest object. This is no good. There may well be data missing from this page." );
	}
	return g_xmlHttp;
}