function add_fav(id) {
    new Ajax.Request('/favorites-handler/',
    {
	parameters: {'action': 'add', 'id': id},
	method: 'post',
	onSuccess: function(response) {
	    result = response.responseJSON;
	    if(result.success) {
	        alert("Added to favorites.");
	    } else {
	        alert(result.message);
	    }
	},
	onFailure: function(response) {
	    alert("An unspecified error occurred: " + response.status);
	}
    });
    return false;
}

function del_fav(id) {
    var y = confirm("Are you sure you want to delete this?");
    if(!y) {
	return false;
    }
    new Ajax.Request('/favorites-handler/',
    {
	parameters: {'action': 'del', 'id': id},
	method: 'post',
	onSuccess: function(response) {
	    result = response.responseJSON;
	    if(result.success) {
	        window.location.reload();
	    } else {
	        alert(result.message);
	    }
	},
	onFailure: function(response) {
	    alert("An unspecified error occurred: " + response.status);
	}
    });
    return false;
}

function move_fav(id) {
    new Ajax.Request('/favorites-handler/',
    {
	parameters: {'action': 'top', 'id': id},
	method: 'post',
	onSuccess: function(response) {
	    result = response.responseJSON;
	    if(result.success) {
	        window.location.reload();
	    } else {
	        alert(result.message);
	    }
	},
	onFailure: function(response) {
	    alert("An unspecified error occurred: " + response.status);
	}
    });
    return false;
}

function copyToClipboard () {
    var element = $('stationery');
    if(typeof netscape != 'undefined') { // does the netscape security object exist?
	try {
	    copyToClipboardFF(element);
	} catch (e) {
	    // alert("Although you seem to have Firefox, I can't do what you're asking. " + e);
	    myLightWindow.activateWindow(
		    {
			href : '/firefox_help2.html',
			width: 600,
			height: 400,
			title: 'Firefox Help'
		    }
		    );
	}
    } else if (document.body.createControlRange) {
	try {
	    copyToClipboardIE(element);
	} catch (e) {
	    alert("Although you seem to have Internet Explorer, but I can't do what you're asking." + e);
	}
    } else {
	alert("Sorry, this feature doesn't seem to be supported by your browser.  We recommend Firefox 3+ or Internet Explorer 8+.");
    }

    return false;
}

function copyToClipboardIE (element) 
{
    var div = element;
    div.contentEditable = 'true';
    var controlRange;
    if (document.body.createControlRange) {
	controlRange = document.body.createControlRange();
	controlRange.addElement(div);
	div.removeAttribute("contentEditable");
	controlRange.execCommand('Copy');
    }
    return false;
}

    function copyToClipboardFF(element) {
	var textHtml = element.innerHTML;
	netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
	// make a copy of the HTML
	var htmlstring = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
	if (!htmlstring) return false; // couldn't get string obj
	htmlstring.data = textHtml;	        

	// add Unicode & HTML flavors to the transferable widget
	var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
	if (!trans) return false; //no transferable widget found

	trans.addDataFlavor("text/html");
	trans.setTransferData("text/html", htmlstring, textHtml.length * 2); // *2 because it's unicode	

	// copy the transferable widget!
	var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"].getService(Components.interfaces.nsIClipboard);
	if (!clipboard) return false; // couldn't get the clipboard
	clipboard.setData(trans, null, Components.interfaces.nsIClipboard.kGlobalClipboard);

	return true;
    }


