﻿/// <reference path="scripts.htm" />
//!-----------------------------------------------------------------------
//! orderbot.cart.js
//! Copyright (c) 2008 Nearsoft, Inc. All rights reserved.
//!-----------------------------------------------------------------------
var MessagesHandler = Class.create({
    initialize: function(container, list) {
        this.container = container;
        this.list = list;
        this.effectTimeout = 6;
        this.messageType = { error: 'error', info: 'info', success: 'success' };
    },
    showMessage: function(message) {
        //        var val = { "errormessage": message, "isPlainMessage": true };
        //        this.add(val);
        
        this.showMessage(message, this.messageType.error);

    },
    showMessage: function(message, type) {
        
        var div = $(this.container);
        if (!div.hasClassName(type)) {
            div.addClassName(type);
        }
        var val = { "errormessage": message, "isPlainMessage": true };
        this.add(val);
    },
    hideMessage: function(message) {
        var val = { "errormessage": message, "isPlainMessage": true };
        this.remove(val);

    },
    show: function() {
        var container = $(this.container);
        if (!container.visible()) {
            container.show();
            //new Effect.Highlight(container);   
        }
    },
    hide: function(clear) {
        if (clear) {
            var ul = $(this.list);
            while (ul.firstChild) ul.removeChild(ul.firstChild);
        }
        var container = $(this.container);
        if (container.visible()) {
            container.hide();
        }
    },
    add: function(val) {
        var li = this.getValidatorListItem(val);
        if (li) return;
        this.createValidatorListItem(val);
        this.show();
    },
    remove: function(val) {
        var li = this.getValidatorListItem(val);
        if (li) {
            var ul = $(this.list);
            ul.removeChild(li);
            var liArray = ul.select('li');
            if (/*ul.childNodes*/liArray.length == 0) {
                this.hide();
            }
        }
    },
    getValidatorListItem: function(val) {
        var ul = $(this.list);
        var li = null;
        $A(ul.getElementsByTagName('li')).each(function(e) {
            if (e.validator == val ||
                e.validator.errormessage.toLowerCase() == val.errormessage.toLowerCase()) {
                li = e; throw $break;
            }
        });
        return li;
    },
    createValidatorListItem: function(val) {
        var ul = $(this.list);
        var li = document.createElement("li");
        li.validator = val;
        li.innerHTML = val.errormessage; // changes innerText to innerHTML for pass format on messages :)
        ul.appendChild(li);
        if (this.onCreateValidatorListItem) {
            this.onCreateValidatorListItem(li);
        }
        return li;
    },
    //this method is made to be overwritten
    onCreateValidatorListItem: function(li) {
    }
});

// override Microsoft methods (WebUIValidation.js)
MessagesHandler.addMethods({
    ValidatorUpdateDisplay: function(val) {
        if (val.isvalid) {
            this.remove(val);
        }
        else {
            this.add(val);

        }
        if (typeof (val.display) == "string") {
            if (val.display == "None") {
                return;
            }
            if (val.display == "Dynamic") {
                val.style.display = val.isvalid ? "none" : "inline";
                return;
            }
        }
        if ((navigator.userAgent.indexOf("Mac") > -1) &&
            (navigator.userAgent.indexOf("MSIE") > -1)) {
            val.style.display = "inline";
        }
        //val.style.visibility = val.isvalid ? "hidden" : "visible";
        val.style.display = val.isvalid ? "none" : "";
    }
});
