function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call
	if( !!li.extra ) var sValue = li.extra[0];	
	// otherwise, lets just display the value in the text box
	else var sValue = li.selectValue;	
	
	$('#nameId').val(sValue);
	//loadNameSong(sValue);
	
	//alert("The value you selected was: " + sValue);
	}

function selectItem(li) {
	findValue(li);
	}

function formatItem(row) {
	return row[0] + " " + row[2] + "";
	//return row[0];
	}
	
function loadNameSong(nameId) {
	$.ajax({
		type: "POST",
		url: "func.php",
		data: "nameId="+nameId,
		success: function(returned){	
			$("#songs").html(returned);				
			}
		});	
	}

// list all names for selected song (this is sfs_song.id NOT sfs_link_name.id)
function listNames(songId) {		
	$("#listNames").html('Loading');
	$.ajax({
		type: "POST",
		url: "func.php",
		data: "action=listNames&songId="+songId,
		success: function(returned){	
			$("#listNames").html(returned);				
			}
		});			
	}	


function addToBasket(songId) {		
	$.ajax({
		type: "POST",
		url: "basket.php",
		data: "action=add&songId="+songId,
		success: function(returned){	
			loadBasket();			
			}
		});			
	}	

function removeFromBasket(id) {		
	$.ajax({
		type: "POST",
		url: "basket.php",
		data: "action=remove&id="+id,
		success: function(returned){	
			loadBasket();			
			}
		});			
	}
	
function loadBasket() {		
	$.ajax({
		type: "POST",
		url: "basket.php",
		data: "action=load",
		success: function(returned){	
			$("#basket").html(returned);				
			}
		});			
	}	

function doClear(theText) {
	if (theText.value == theText.defaultValue) {
		theText.value = ""
		}
	}
	
function formSubmit(formName) {	
	
	formNameSplit = formName.split("_");
	oid = formNameSplit[1];
	
	if ( !$("#email_"+oid).val()) {
		alert("Please enter a valid email address");
		return;
		} else {
		email = $("#email_"+oid).val();
		}
		
	if ( !$("#subject_"+oid).val()) {
		alert("Please enter a subject");		
		return;
		} else {
		subject = $("#subject_"+oid).val();
		}
	
	msg = $("#message_"+oid).val();
	sid = $("#sid_"+oid).val();
	
	$.ajax({
		type: "POST",
		url: "func.php",
		data: "action=email&oid="+oid+"&sid="+sid+"&email="+email+"&subject="+subject+"&msg="+msg,
		success: function(returned){	
			
			if (returned == 'success') {
				$("#email_"+oid).val('');
				$("#subject_"+oid).val('');
				$("#message_"+oid).val('');
				alert("Your email to "+email+" has been sent!");
			} else {
				alert("There was a problem sending your email, please ty again");
				}
			}
		});		
	
	
	}