

//######## Constants ###################


//####################################
// calling functions
function add_track(primaryid,playlist_id){
	var post_string = 'method=add_to_playlist&';
	post_string +='primaryid='+primaryid+'&';
	post_string += 'playlist_id='+playlist_id;
	ping(post_string);
}
function remove_track(primaryid,playlist_id){
	var post_string = 'method=remove_from_playlist&';
	post_string +='primaryid='+primaryid+'&';
	post_string += 'playlist_id='+playlist_id;
	ping(post_string);
}

function add_cd(cdnumber,playlist_id){
	var post_string = 'method=add_cd_to_playlist&';
	post_string +='cdnumber='+cdnumber+'&';
	post_string += 'playlist_id='+playlist_id;
	ping(post_string);
}
function remove_cd(cdnumber,playlist_id){
	var post_string = 'method=remove_cd_from_playlist&';
	post_string +='cdnumber='+cdnumber+'&';
	post_string += 'playlist_id='+playlist_id;
	ping(post_string);
}
var pingAJAX;
function ping(post_string) {
	// Instantiate an object
	// Test to see if XMLHttpRequest is a defined object type for the user's browser
	// If not, assume we're running IE and attempty to instantiate the MS XMLHTtp object
	// Don't be confused by the ActiveXObject indicator. Use of this code will not trigger
	// a security alert since the ActiveXObject is baked into IE and you aren't downloading it
	// into the IE runtime engine
	//myText = encode(post_string);

	if ( window.XMLHttpRequest ) {
		pingAJAX = new XMLHttpRequest();
	} else {
		pingAJAX = new ActiveXObject("MSXML2.XMLHTTP");
	}

	// In Javascript, everything is an object. Functions are objects, everything inherits from Object
	// So assigning onreadystatechange to pingCallback means that you can call pingCallback by doing the following
	// pingAJAX.onreadystatechange( "blahblah")
	pingAJAX.onreadystatechange = pingCallback;

	// The open statement initializes the request. In this example, we'll just pass the value in the URL.
	pingAJAX.open( 'POST', '/ajax/router.php', true );
	pingAJAX.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // sending it as encoded formdata
	pingAJAX.setRequestHeader("Content-length",post_string.length); // we need to specify the length of the contents
	pingAJAX.setRequestHeader("Connection","close"); // Connection is to be closed after transfer
	// Send request to the server
	pingAJAX.send(post_string); // This time, we need to send the text.
}
function pingCallback() {
	// Called from ping
	if ( pingAJAX.readyState == 4 ){
		
		this.XMLfrag = pingAJAX.responseXML;
		this.tMethod = this.XMLfrag.getElementsByTagName('function');
		switchText = this.tMethod[0].firstChild.nodeValue;
		switch (switchText){
			case "error":handle_error(this.XMLfrag);
			break;
			case "added_to_playlist" : added_to_playlist(this.XMLfrag);
			break;	
			case "removed_from_playlist" : removed_from_playlist(this.XMLfrag);
			break;	
			case "added_cd_to_playlist" : added_cd_to_playlist(this.XMLfrag);
			break;	
			case "removed_cd_from_playlist" : removed_cd_from_playlist(this.XMLfrag);
			break;	
			
			default : alert('no such function');
			break;
		}
	}

}

function handle_error(XMLfrag){
	this.tMethod = XMLfrag.getElementsByTagName('error');
		errorText = this.tMethod[0].firstChild.nodeValue;
		alert(errorText);
	
}

function added_to_playlist(XMLfrag){
	// get the key for the button or link
	var primaryID = XMLfrag.getElementsByTagName('primaryid')[0].firstChild.nodeValue;
	span_id = 'PL_'+primaryID;
	
	var linkspan = document.getElementById(span_id);
	
	linkspan.innerHTML = "<a href=\"javascript:remove_track("+primaryID+active_playlist_id_text+ ");\"><span class=\"inPlaylist\">in playlist!</span></a>";
	
}
function removed_from_playlist(XMLfrag){
	// get the key for the button or link
	var primaryID = XMLfrag.getElementsByTagName('primaryid')[0].firstChild.nodeValue;

	span_id = 'PL_'+primaryID;
	
	var linkspan = document.getElementById(span_id);
	
	linkspan.innerHTML = "<a href=\"javascript:add_track("+primaryID+active_playlist_id_text+");\">add to playlist</a>";
	
}

function added_cd_to_playlist(XMLfrag){
	var cdnumber = XMLfrag.getElementsByTagName('cdnumber')[0].firstChild.nodeValue;
	span_id = 'PL_'+cdnumber;
	var linkspan = document.getElementById(span_id);
	linkspan.innerHTML = "<a href=\"javascript:remove_cd('"+cdnumber+"'"+active_playlist_id_text+ ");\"><span class=\"inPlaylist\">in playlist!</span></a>";
}
function removed_cd_from_playlist(XMLfrag){
	// get the key for the button or link
	var cdnumber = XMLfrag.getElementsByTagName('cdnumber')[0].firstChild.nodeValue;

	span_id = 'PL_'+cdnumber;
	
	var linkspan = document.getElementById(span_id);
	
	linkspan.innerHTML = "<a href=\"javascript:add_cd('"+cdnumber+"'"+active_playlist_id_text+");\">add CD to playlist</a>";
	
}

