YAHOO.namespace("edmunds.core.storage");

YAHOO.edmunds.core.storage.FormStorage = function(formName, valueDelim) {
    this.formName = formName;
    this.storageField = formName+"store";
    this.DELIM = "0xcafebabe";
    this.valueDelim = valueDelim;
    this.cacheKey = new HashMap();
};

YAHOO.edmunds.core.storage.FormStorage.prototype = {
    constructor : YAHOO.edmunds.core.storage.FormStorage,

    // if store element is not already exist on page at page load, then it will not be available if leaving page
    safeInit: function(){
        if (!document.getElementById(this.storageField)){
            var formTag = document.createElement("form");
            formTag.setAttribute("id", this.formName);
            formTag.style.display="none";
            var txtAreaTag = document.createElement("textarea");
            txtAreaTag.setAttribute("id", this.storageField);
            txtAreaTag.appendChild(document.createTextNode("empty"));
            formTag.appendChild(txtAreaTag);
            // this must be load after DOM is ready, otherwise IE chokes
            document.body.appendChild(formTag);
        }
    },
    storeData : function(key, value){
        this.safeInit();
        var storage = document.getElementById(this.storageField);
        var combinedData = storage.value;
        var new_combinedData = "";

        if (combinedData == null || combinedData == "empty") {
            new_combinedData = this.DELIM + key + this.valueDelim + value;
        } else {
            var begin = combinedData.indexOf(this.DELIM + value + this.valueDelim);
            if (begin != -1) {
                if (RegExp) {
                    var oldvalue = new RegExp(key + this.valueDelim + "[^" + this.DELIM + "]*");
                    var newvalue = key + this.valueDelim + value;
                    new_combinedData = combinedData.replace(oldvalue, newvalue);
                } else {
                    // Backwards compatibility for old browsers that might
                    // not support Regular Expressions.
                    var end = combinedData.indexOf(this.DELIM, begin + 1);
                    new_combinedData = this.DELIM + key + this.valueDelim + value;
                    if (begin == 0 && end == -1) {
                        // we replace the whole field.
                    } else if (begin == 0) {
                        // replaced item was at the beginning of the string.
                        new_combinedData += combinedData.substring(end);
                    } else if (end == -1) {
                        // replaced item was at the end of the string.
                        new_combinedData += combinedData.substring(0, begin);
                    } else {
                        // replace item was in middle of the string.
                        new_combinedData += combinedData.substring(0, begin);
                        new_combinedData += combinedData.substring(end);
                    }
                }
            } else {
                // Item is not in the combined string, so just append it.
                new_combinedData = combinedData + this.DELIM + key + this.valueDelim + value;
            }
        }
        storage.value = new_combinedData;
        this.cacheKey.add(key, true);
    },

    getData : function(key) {
        this.safeInit();
        var prefix = key + this.valueDelim;
        var storage = document.getElementById(this.storageField);
        var combinedData = storage.value;
        if (combinedData == null) {
            return null;
        } else {
            var begin = combinedData.indexOf(this.DELIM + prefix);
            if (begin == -1) {
                return null;
            } else {
                begin += this.DELIM.length + prefix.length;
            }

            var end = combinedData.indexOf(this.DELIM, begin);
            if (end == -1) { end = combinedData.length; }
            var result = combinedData.substring(begin, end)
            return result;
        }
    },


    loadCacheKey: function(){
        this.cacheKey.clear();
        this.safeInit();
        var combData = document.getElementById(this.storageField).value;
        if (combData && combData != 'empty'){
            var val = combData.split(this.DELIM);
            for (var _cnt = 0; _cnt<val.length; _cnt++) {
                this.cacheKey.add(val[_cnt].split(this.valueDelim)[0], true);
            }
        }
    },

    cacheExists : function(key) {
        if (this.cacheKey.size() == 0) {
            this.loadCacheKey();
        }
        return this.cacheKey.contains(key);
    }
}