<!--

/********* INIT SECTION *************/

var items = new Array();     // Items start at index 1.
var numitems = 0;

function Item(co, d, c, q) {
    this.code = co;
    this.desc = d;
    this.price = c;
    this.quantity = q;
}

/******** COOKIE SECTION *****************/

function writeCookie(name_of_cookie) {
    if (numitems > 0) {
        var cookieExp = new Date();
        cookieExp.setTime(cookieExp.getTime() + 365 * 24 * 60 * 60 * 1000); // Expires in 1 yr.
        var cookie_i = name_of_cookie + "=";
        for (var i=1; i <= numitems; i++)
            cookie_i += escape(items[i].desc) + "|" + items[i].price + "|" + items[i].quantity + ":";
        cookie_i += ";path=/;expires=" + cookieExp.toGMTString() + ";";
//        alert(cookie_i);
        document.cookie = cookie_i;
    }
}


function readCookie() {
    numitems = 0;
    if (document.cookie) {
        var theCookie = document.cookie;
        var binA = theCookie.split("=");
        if (binA[1]) {
            var binB = binA[1].split(";");    // Gets item list.
            var cookieItems = binB[0].split(":");
            for (var i=1; i < cookieItems.length; i++) {
                var item_prop = cookieItems[i-1].split("|");
                addItem(unescape(item_prop[0]), item_prop[1], item_prop[2]);
            }
        }
    }
//    alert(document.cookie);
}


/********* TROLLEY/CART SECTION *************/

function validateItem(pid, testval) {
    if (isNaN(testval) || (testval < 1)) {
        alert("You have entered an improper quantity for " + items[pid].code);
        return items[pid].quantity;
    }
    else
        return testval;
}


function itemExists(p) {
    flag = false;
    for (var i=1; i<=numitems; i++)   // Cycle thru all items and match with name.
        if (items[i].desc == p)
            flag = true;
    return flag;
}


function addItem(code, desc, cost, quantity) {
    if (itemExists(desc))
        alert("The item '" + desc + "' is already in your shopping basket");
    else {
        items[++numitems] = new Item(code, desc, cost, quantity);
//      alert("item " + numitems + " added");
//        writeCookie("castlegold");
        parent.main.order.location.reload();
   }
}


function removeItem(cindex) {
    if (confirm("Are you sure you wish to remove '"+items[cindex].desc+"' from your basket?")) {
        delete items[cindex];   // Delete item from memory. Sets to UNDEFINED (but space is still there).
        if (numitems > 1)
            for (var i=cindex; i<numitems; i++)
                items[i] = items[i+1];         // Shift items down one slot in array.
        --numitems;
//        writeCookie("castlegold");
        parent.main.order.location.reload();
    }
}


function clearCart() {
    if (confirm("Are you sure you wish to remove all items from your shopping basket?")) {
        for (var i=1; i <= numitems; i++)
            delete items[i];
        numitems = 0;    // Simply reset to 0. Will overwrite existing items anyway.
//        writeCookie("castlegold");
        parent.main.order.location.reload();
    }
}


function updateCart() {
    with (parent.main.order.document) {

// First... update basket.

            for (var u=1; u<=numitems; u++)   // Thanks to our hidden field on the trolley side, we can start at u=1.
                items[u].quantity = validateItem(u, cartform.qty[u].value);
    } // end with

// Next... delete all items with a quantity of 0 or LESS.
// NOTE: Removed b/c of error checking of qty.value < 1.

//    c = 1;
//   while (c <= numitems) {
//        (items[c].quantity <= 0) ? removeItem(c) : ++c;
//    }

//    writeCookie("castlegold");
    parent.main.order.location.reload();         // Refresh cart frame.
}


function displayCost(c) {
// Little function to display cost, eg 8.80 instead of 8.8
    numStr = String(c);
    numSpl = numStr.split(".");
    if (numSpl.length == 1)       // eg. value = 80
        return numStr;
    else {
        if (numSpl[1].length < 2)
            numSpl[1] *= 10;           // Add another zero on the end.
        newStr = numSpl[0] + "." + numSpl[1];
        return newStr;
    }
}


function completeOrder() {
    winOptions = "alwaysRaised=yes,location=no,status=yes,titlebar=no,"+
                 "toolbar=no,scrollbars=yes,height=350,width=640,resizable=yes";
    ordWin = window.open("redirect2.asp", "ordWin", winOptions);
//    ordWin = window.open("../../cart/redirect.html", "ordWin", winOptions);
}


function initTrolley() {
//    readCookie();
    parent.main.order.location.href='fr_cart.html';
}

//-->

