function wordIndex(string, word) {
    var wordIndex = string.indexOf(word);
    if (wordIndex != -1) {
        if ((wordIndex > 0 && string.charAt(wordIndex - 1) != ' ') || (wordIndex + word.length < string.length && string.charAt(wordIndex + word.length) != ' ')) {
            wordIndex = -1;
        }
    }
    return wordIndex;
}

function hasClass(id, classname) {
    var object = document.getElementById(id);
    return wordIndex(object.className, classname) != -1;
}

function addClass(id, classname) {
    var object = document.getElementById(id);
    var hideIndex = wordIndex(object.className, classname);
    if (hideIndex == -1) {
        var objectClassName = object.className;
        if (object.className.length > 0) {
            objectClassName = objectClassName + " ";
        }
        objectClassName = objectClassName + classname;
        object.className = objectClassName;
    }
}

function removeClass(id, classname) {
    var object = document.getElementById(id);
    var hideIndex = wordIndex(object.className, classname);
    if (hideIndex != -1) {
        var startIndex = hideIndex;
        var endIndex = startIndex + classname.length;
        if (startIndex > 0 && object.className.charAt(startIndex - 1) == ' ') {
            startIndex -= 1;
        } else if (endIndex < object.className.length && object.className.charAt(endIndex) == ' ') {
            endIndex += 1;
        }
        var objectClassName = object.className.substring(0, startIndex) + object.className.substring(endIndex);
        object.className = objectClassName;
    }
}

function show(id) {
    removeClass(id, "hide");
}

function hide(id) {
    addClass(id, "hide");
}

function setClass(id, className) {
    var object = document.getElementById(id);
    object.className = className;
}

function setContent(id, content) {
    var object = document.getElementById(id);
    object.innerHTML = content;
}

function setChecked(id, checked) {
    var object = document.getElementById(id);
    object.checked = checked;
}

function setDisabled(id, disabled) {
    var object = document.getElementById(id);
    object.disabled = disabled;
}

function getValue(id) {
    var object = document.getElementById(id);
    return object.value;
}

function setValue(id, value) {
    var object = document.getElementById(id);
    object.value = value;
}

function contains(list, object) {
    for (var i in list) {
        if (list[i] == object) {
            return true;
        }
    }
    return false;
}

function getPriceView(price) {
    return Math.floor(price / 100);
}

function getPriceString(price) {
    return getCleanPriceString(price) + " р.";
}

function getCleanPriceString(price) {
    return getPriceView(price) + (price % 100 != 0 ? 1 : 0);
}

function getDiscountString(discount) {
    return getCleanDiscountString(discount) + " р.";
}

function getCleanDiscountString(discount) {
    return getPriceView(discount);
}

function getSuffix(value, suffixes) {
    var rest = value % 100;
    var suffix;
    if (rest >= 11 && rest <= 19) {
        suffix = suffixes[2];
    } else {
        rest = value % 10;
        if (rest == 1) {
            suffix = suffixes[0];
        } else if (rest == 2 || rest == 3 || rest == 4) {
            suffix = suffixes[1];
        } else {
            suffix = suffixes[2];
        }
    }
    return suffix;
}

function getAvailabilityValue(availability, suffixes) {
    if (availability >= 1 && availability <= 7) {
        return availability + " " + getSuffix(availability, suffixes);
    } else if (availability >= 8 && availability <= 14) {
        return "в течение двух недель";
    } else if (availability >= 15 && availability <= 30) {
        return "в течение месяца";
    } else if (availability == 31) {
        return "временно отсутствует в продаже";
    } else {
        return "снят с продажи";
    }
}

function getAvailabilityString(availability) {
    return getAvailabilityValue(availability, ["рабочий день", "рабочих дня", "рабочих дней"]);
}

function getShortAvailabilityString(availability) {
    return getAvailabilityValue(availability, ["день", "дня", "дней"]);
}

function getItemsQuantityString(quantity) {
    return quantity + " " + getSuffix(quantity, ["товар", "товара", "товаров"]);
}

function isNumber(value) {
    return value.match(/^\d+$/);
}

var lastFocusedContainer = null;
var lastFocusedInput = null;
var lastFocusedMessages = null;

function onFocusFormTextField(idContainer, idInput, idMessages) {
    if (lastFocusedContainer != null) {
        if (hasClass(lastFocusedContainer, "error_selected")) {
            removeClass(lastFocusedContainer, "error_selected");
            addClass(lastFocusedContainer, "error");
        }
        hide(lastFocusedMessages);
    }

    if (hasClass(idContainer, "error")) {
        removeClass(idContainer, "error");
        addClass(idContainer, "error_selected");
    }
    show(idMessages);
    lastFocusedContainer = idContainer;
    lastFocusedInput = idInput;
    lastFocusedMessages = idMessages;
}

function onBlurFormTextField(idContainer, idInput, idMessages) {
/*    if (hasClass(idContainer, "error_selected")) {
        removeClass(idContainer, "error_selected");
        addClass(idContainer, "error");
    }
    hide(idMessages);*/
}

function setFocusOnLoad(formName) {
    var focusedInput = null;
    var form = null;
    for (var i = 0; i < document.forms.length; i++) {
        var f = document.forms[i];
        if (f.name == formName) {
            form = f;
            break;
        }
    }
    if (form != null) {
        for (var i = 0; i < form.length; i++) {
            var element = form.elements[i];
            if (element.id != null && element.id.indexOf("Input") != -1) {
                if (focusedInput == null && element.value.length == 0) {focusedInput = element;}
                var containerId;
                containerId = element.id.substring(0, element.id.indexOf('Input')) + "Field";
                if (document.getElementById(containerId) != null && hasClass(containerId, "error")) {
                    focusedInput = element;
                    break;
                }
            }
        }
    }
    if (focusedInput != null) {
        focusedInput.focus();
    }
}

/* Message library - analog ru.albemuth.util.Message java class */
function MessageStatus(status, order, passes) {
    this.status = status;
    this.order = order;
    this.passes = passes;
}
MessageStatus.prototype.isHigherThen = function(status) {
    return status == null || this.order > status.order;
}
MessageStatus.INFO = new MessageStatus("info", 0, true);
MessageStatus.WARN = new MessageStatus("warning", 1, true);
MessageStatus.DATA_NEED = new MessageStatus("data_need", 2, false);
MessageStatus.ERROR = new MessageStatus("error", 3, false);

function MessageItem(status, message) {
    this.status = status;
    this.message = message;
}

function calculateStatus(status, items) {
    for (var i in items) {
        if (items[i] != null && items[i].status.isHigherThen(status)) {
            status = items[i].status;
        }
    }
    return status;
}

function ObjectItem() {
    this.items = [];
}
ObjectItem.prototype.addItem = function(propertyKey, status, message) {
    if (status.isHigherThen(this.status)) {
        this.status = status;
    }
    this.items[propertyKey] = new MessageItem(status, message);
}
ObjectItem.prototype.removeItem = function(propertyKey) {
    var item = this.items[propertyKey];
    if (item != null) {
        //this.items.splice(propertyKey, 1); //wtf? why id don't work?
        this.items[propertyKey] = null;
        if (item.status.order == this.status.order) {
            this.status = calculateStatus(MessageStatus.INFO, this.items);
        }
    }
    return item;
}
ObjectItem.prototype.getItem = function(propertyKey) {
    return this.items[propertyKey];
}

function Message() {
    this.status = MessageStatus.INFO;
    this.objectItems = [];
}
Message.prototype.getStatus = function(objectKey) {
    var objectItem = this.objectItems[objectKey];
    if (objectItem != null) {
        return objectItem.status;
    } else {
        return MessageStatus.INFO;
    }
}
Message.prototype.addItem = function(objectKey, propertyKey, status, message) {
    if (status.isHigherThen(this.status)) {
        this.status = status;
    }
    var objectItem = this.objectItems[objectKey];
    if (objectItem == null) {
        objectItem = new ObjectItem();
        this.objectItems[objectKey] = objectItem;
    }
    objectItem.addItem(propertyKey, status, message);
}
Message.prototype.removeItem = function(objectKey, propertyKey) {
    var objectItem = this.objectItems[objectKey];
    if (objectItem != null) {
        var item = objectItem.removeItem(propertyKey);
        if (item != null && item.status.order == this.status.order) {
            this.status = MessageStatus.INFO;
            for (var i in this.objectItems) {
                if (this.objectItems[i] != null) {
                    this.status = calculateStatus(this.status, this.objectItems[i].items);
                }
            }
        }
        /*if (objectItem.items.length == 0) {
            //this.objectItems.splice(objectKey, 1); //wtf? why id don't work?
            this.objectItems[objectKey] = null;
        }*/
        return item;
    } else {
        return null;
    }
}
Message.prototype.getItem = function(objectKey, propertyKey) {
    var objectItem = this.objectItems[objectKey];
    if (objectItem != null) {
        return objectItem.getItem(propertyKey);
    } else {
        return null;
    }
}
Message.prototype.getItems = function(objectKey) {
    var objectItem = this.objectItems[objectKey];
    if (objectItem != null) {
        return objectItem.items;
    } else {
        return [];
    }
}

var commonJsLoaded = true;