﻿var repCollection = new CollectionOrder();  //     Associative array

function updatePrice(obj, repID) {
    var repKey = "rep" + repID;
    if (obj.checked) {    
        window.repCollection.add(repKey, repKey);
    }
    else {
        window.repCollection.remove(repKey);
    }
    refreshCart();
    //alert(repCollection.toString());
    repCollection.save();
    //setCookie("IrmShoppingItems", repCollection.toString());
}
/// Updates contents in shopping cart
function refreshCart() {
    var price;
    var title;
    var iHTML = "";
    var totPrice = 0;
    var rkeys = window.repCollection.getKeys();
    
    try {
        for (var rkey in rkeys) {
            price = oReports.reports[rkeys[rkey]].price;
            title = oReports.reports[rkeys[rkey]].title;
            iHTML += "<div class='basketLeft'>" + title + "</div><div class='basketRight'>" + price + "</div>";
            totPrice = totPrice + price;
        }
    }
    catch(ex)
    {
        iHTML += "<span class='error'>Error:" + ex + '</span>';
    }

    var el = document.getElementById("lblTotal");

    if (el != null && typeof (el) == 'object') {
        el.innerHTML = totPrice
    }
    el = document.getElementById("basketItems");
    if (el != null && typeof (el) == 'object') {
        el.innerHTML = iHTML;
    }
}

///checkOut
function checkOut() {
    var prodIds = window.repCollection.getKeys();
//    if((window.repCollection != null)) {        
//        var rkeys = window.repCollection.getKeys();
//        for (var rkey in rkeys) {
//            if((oReports.reports != null)) {
//                prodIds += window.repCollection.getItem(rkeys[rkey]));
//         
//            }
//        }
//    }
    if(prodIds.length > 0) {
        window.document.location = "order.aspx?prod="+prodIds;
    } else {
        alert("Du måste välja en rapport innan du går till kassan");
    }
    
}

function loadPage() {

    var el;
    //alert(window.repCollection.getKeys());
    window.repCollection.load();
    var rkeys = window.repCollection.getKeys();
    for (var rkey in rkeys) {
        if((oReports.reports != null)) {
            el = document.getElementById("check_" + window.repCollection.getItem(rkeys[rkey]));
            if (el != null && typeof (el) == 'object') {
                el.checked = true;
            }
        }
    }
    refreshCart();
}

///CollectionOrder is an ordered associative array
function CollectionOrder() {
    var collection = {};
    var order = [];
    var STORAGE_NAME = "IrmShoppingItems";
    
    this.add = function(property, value) {
        if (!this.exists(property)) {
            collection[property] = value;
            order.push(property);
        }
    }
    this.remove = function(property) {
        //collection[property] = null;
        delete collection[property];
        var ii = order.length;
        while (ii-- > 0) {
            if (order[ii] == property) {
                //order.length--;
                delete order[ii]; // = null;                
                break;
            }
        }
    }
    //dependancy setCookie in BaseScripts.js
    //Saves the state
    this.save = function() {
        var colString = this.getKeys();
        setCookie(STORAGE_NAME, colString);
    }
    //Loads the last state
    //dependancy oReports to be defined in page
    this.load = function() {
        var savedString = readCookie(STORAGE_NAME);        
        if (oReports.reports != null) {
            if (savedString != null) {
                var rkeys = savedString.split(",");
                for (var rkey in rkeys) {
                    if ( oReports.reports[rkeys[rkey]]!=null ) {
                        this.add(rkeys[rkey], oReports.reports[rkeys[rkey]].id);
                    }
                }
            }
        } else { alert("error:No reports defined"); }
    }
    
    this.toString = function() {
        var output = [];
        output = this.getKeys();
        return output;
    }

    this.getKeys = function() {
        var keys = [];
        for (var ii = 0; ii < order.length; ++ii) {
            if (order[ii] != null) {
                keys.push(order[ii]);
            }
        }
        return keys;
    }

    this.getItem = function(property) {
        return collection[property];
    }

    this.setItem = function(property, value) {
        if (value != null) {
            collection[property] = value;
        }
        var ii = order.length;
        while (ii-- > 0) {
            if (order[ii] == property) {
                order[ii] = null;
                order.push(property);
                break;
            }
        }

    }

    this.exists = function(property) {
        return collection[property] != null;
    }
}
