var request;

function handleLinkClick(evt, url, objectId)
{
    _cancelEvent(evt);
    
    // Ajax stuff... (needs to return null if not a file (or something!))
    doAjaxRequest('/Ajax/InternalLinkHandler.ashx',
     'url=' + url + '&id=' + objectId, 
     linkClickAjaxHandler );
}

function linkClickReturnHandler(isFile, newUrl, vanityName)
{
    if (isFile)
    {
        // call google-analytics
        pageTracker._trackPageview(vanityName);
    }
    
    document.location = newUrl;
}

doAjaxRequest = function(url, params, completeHandler) {
    var req = request = getXmlHttpRequest();
    req.onreadystatechange = completeHandler;
	req.open("POST", url, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send(params);
}

releaseXmlRequest = function() {
    this.req = null;
}

function linkClickAjaxHandler() {
    var xhr = request;
	if(xhr.readyState == 4) {
	    try {
		    eval(xhr.responseText);
		    releaseXmlRequest();
		} catch(e) {
		    alert("Something went wrong with the AJAX request \n" + e.message);
		}
	}
}

function notLoggedIn()
{
    //Somehow they've become logged out so refresh and let the server handle it.
    refreshParent();
}

function errorMessage(msg)
{
    alert(msg);
}

function addToBasket(libref, partner, year, month, day, controlId)
{
    doAjaxRequest('/Ajax/BasketHandler.ashx',
        'action=add' + 
        '&libref=' + libref + 
        '&partner=' + partner +
        '&year=' + year +
        '&month=' + month +
        '&day=' + day +
        '&controlId=' + controlId,
        AjaxHandler );
}

function addClipBinToBasket(binId, controlId)
{
    doAjaxRequest('/Ajax/BasketHandler.ashx',
        'action=add' + 
        '&binId=' + binId +
        '&controlId=' + controlId,
        AjaxHandler );
}


function addClipBinToBasketHome(binId, controlId)
{
    doAjaxRequest('/Ajax/BasketHandler.ashx',
        'action=add&home=1' + 
        '&binId=' + binId +
        '&controlId=' + controlId,
        AjaxHandler );
}

function deleteClipBin(binId, controlId)
{
    doAjaxRequest('/Ajax/GeneralHandler.ashx',
        'action=deleteclipbin' + 
        '&binId=' + binId +
        '&controlId=' + controlId,
        AjaxHandler );
}

function renameClipBin(name, binId)
{
    doAjaxRequest('/Ajax/GeneralHandler.ashx',
        'action=renameclipbin' + 
        '&binId=' + binId +
        '&newname=' + name,
        AjaxHandler );
}

function AjaxHandler()
{
    var xhr = request;
	if(xhr.readyState == 4) {
	    try {
		    eval(xhr.responseText);
		    releaseXmlRequest();
		} catch(e) {
		    alert("Something went wrong with the AJAX request \n" + e.message);
		}
	}
}

function itemAddedToBasket(controlId)
{
    document.getElementById(controlId).src = '/i/inBasketButton.gif';
    // Do fade or an effect of some sort
}

function itemAddedToBasketHome(controlId)
{
    document.getElementById(controlId).innerHTML = 'Added to Basket';
    // Do fade or an effect of some sort
}



function refreshParent()
{
    location.reload(true);
}

function DeleteUser(controlId)
{
    var userId = document.getElementById("userId").value;
    
    doAjaxRequest('/Ajax/GeneralHandler.ashx',
        'action=deleteuser' +
        '&userId=' + userId +
        '&controlId=' + controlId ,
        AjaxHandler );
}

function GeneralMessage(controlId, messageText, reloadWindow)
{
    if (reloadWindow)
        location.reload(true);
    else
        document.getElementById(controlId).innerHtml = messageText;
}

function DeleteClipFromBasket(clipId)
{
    doAjaxRequest('/Ajax/BasketHandler.ashx',
        'action=delete' +
        '&clipId=' + clipId,
        AjaxHandler );
}

function DeleteAllClipsFromBasket()
{
    doAjaxRequest('/Ajax/BasketHandler.ashx',
        'action=deleteall',
        AjaxHandler );
}

function DeleteClipFromClipbin(key)
{   
    doAjaxRequest('/Ajax/GeneralHandler.ashx',
        'action=deleteclip' +
        '&key=' + key ,
        AjaxHandler );
}

function DeleteAllClipsFromClipbin(key)
{   
    doAjaxRequest('/Ajax/GeneralHandler.ashx',
        'action=deleteallclips' +
        '&key=' + key ,
        AjaxHandler );
}