// -*-Java-*- Please leave this line in, It sets the Emacs editing mode.

// Write a cookie
function WriteCookie (cookieName, cookieValue, expiry){
    var expDate = new Date();

    if(expiry) {
        expDate.setTime (expDate.getTime() + expiry);
        document.cookie = cookieName + "=" + escape (cookieValue) +
            "; expires=" + expDate.toGMTString();
    } else {
        document.cookie = cookieName + "=" + escape (cookieValue);
    }
}

// Read a named cookie value
function ReadCookie (CookieName) {
    var CookieString = document.cookie;
    var CookieSet = CookieString.split (';');
    var SetSize = CookieSet.length;
    var CookiePieces;
    var ReturnValue = "";
    var x = 0;

    for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++) {

        CookiePieces = CookieSet[x].split ('=');

        if (CookiePieces[0].substring (0,1) == ' ') {
            CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length);
        }

        if (CookiePieces[0] == CookieName) {
            ReturnValue = CookiePieces[1];
        }

    }

    return unescape( ReturnValue );
}

// Format a currency value and a kludge for Netscape 2 number rounding.
function alterError(value) {
    if (value<=0.99) {
        newPounds = '0';
    } else {
        newPounds = parseInt(value);
    }
    newPence = parseInt((value+.0008 - newPounds)* 100);
    if (eval(newPence) <= 9) newPence='0'+newPence;
    newString = newPounds + '.' + newPence;
    return (newString);
}

// Add an item to the shopping basket
function buyItem(newRef, newArtist, newTitle, newFormat, newPrice) {
    var twoDays = 1000 * 60 * 60 * 24 * 2;
    if (confirm('Add  ' + newTitle + ' by ' + newArtist + '  to basket?')) {

        WriteCookie("TheBasket", ReadCookie("TheBasket") + newRef+"\t"+newArtist+"\t"+newTitle+"\t"+newFormat+"\t"+newPrice+"\n", twoDays);
    }
}

// Empty the shopping basket
function resetShoppingBasket() {
    WriteCookie("TheBasket", "");
}

// Display shopping basket in a table.
var totprice = 0;

function showItems() {
    var 
        startIdx = 0,
        endIdx = 0,
        fieldStart = 0,
        fieldEnd = 0,
        fieldNum = 0,
        item = "",
        itemNum = 0,
        field = "";

    fulllist = ReadCookie("TheBasket");
    totprice = 0;

    if(fulllist.length == 0){
        document.writeln('<p><font color="red" size="+2">Your shopping basket is empty</font>.');
        document.writeln('If you would like to add some items, please <a href="browse.html">browse</a> our listings and hit the <font color="blue"><b>Buy</b></font> links.');
        return;
    }

    document.writeln('<center>');
    document.writeln('<table width="80%" align="center" border=0 cellspacing=2 cellpadding=3>');
    document.writeln('<tr bgcolor="aeefef"><th><b>RefNum</b></th><th><b>Artist</b></th><th><b>Title</b></th><th><b>Format</b></th><th><b>Price</b></th><th><b>Action</b></th></tr>');
    while(startIdx < fulllist.length){
        endIdx = fulllist.indexOf('\n', startIdx);
        item = fulllist.substring(startIdx, endIdx);
        fieldNum = fieldStart = 0;
        document.writeln("<tr>");
        while(fieldStart < item.length){
            fieldEnd = item.indexOf('\t', fieldStart);

            if(fieldEnd == -1)
                fieldEnd = item.length;

            field = item.substring(fieldStart, fieldEnd);

            if(fieldNum == 4){
                document.writeln('<td align="right" bgcolor="#86cfff">' + field + "</td>");
                totprice = totprice + eval(field);
            }else if(fieldNum == 0)
                document.writeln('<td align="right" bgcolor="#86cfff">' + field + "</td>");
            else
                document.writeln('<td bgcolor="#86cfff">' + field + "</td>");
            
            if(item.charAt(fieldEnd) == '\n')
                break;

            fieldNum++;
            fieldStart = fieldEnd + 1;
        }
        document.writeln('<td align="center" bgcolor="#aeefef">'
                         + '<a href="javascript:removeItem('
                         + itemNum
                         + ')">Remove</a></td>');
        document.writeln("</tr>");
        startIdx = endIdx + 1;
        itemNum++;
    }
    document.writeln('<tr bgcolor="#aeefef">');
    document.writeln('<td colspan=4 align="right">Total cost of items:');
    document.writeln('<td align="right" bgcolor="#86cfff">&pound;' + alterError(totprice));
    document.writeln("</table>");
    document.writeln('</center>');
    document.writeln('<center><form>');
    document.writeln('<input type="button" name="clear" value="Empty Basket" onClick="clearBasket()">');
    document.writeln('</form></center>');
}

// Romove one item from the shopping basket
function removeItem(removeItemNum) {
    var fulllist = ReadCookie("TheBasket");
    var startIdx = 0,
        endIdx = 0,
        item = null,
        itemNum = 0,
        newList = "";

    while(startIdx < fulllist.length){
        endIdx = fulllist.indexOf('\n', startIdx);
        item = fulllist.substring(startIdx, endIdx);
        if(itemNum != removeItemNum)
            newList = newList + item + '\n';
        startIdx = endIdx + 1;
        itemNum++;
    }

    WriteCookie("TheBasket", newList, 1000*60*60*2);
    self.location.reload();
}

// clearBasket() - removes all items from the basket
function clearBasket() {
    if (confirm('Are you sure you wish to clear the basket')) {
        top.menu.WriteCookie("TheBasket", "");
        self.location.reload();
    }
}
