﻿/*-----------JoinedProduct start--------------*/
function JoinedProduct(id, group, brand, logo, name, text, priceHeading, image, thumb, firstProductName, firstProductModel, firstProductID, firstProductPrice, firstProductCardPrice, firstProductTeamPrice, firstProductSizes, secondProductName, secondProductModel, secondProductID, secondProductPrice, secondProdctCardPrice, secondProductTeamPrice, secondProductSizes) {
    if (id == null) { this.id = -1; } else { this.id = id; }
    this.group = "" + group;
    this.brand = "" + brand;
    if (logo == null) { this.logo = 0; } else { this.logo = logo; }
    this.name = "" + name;
    this.text = "" + text;
    this.priceHeading = "" + priceHeading;
    if (image == null) { this.image = 0; } else { this.image = image; }
    if (thumb == null) { this.thumb = 0; } else { this.thumb = thumb; }
    this.firstProductName = "" + firstProductName;
    this.firstProductModel = "" + firstProductModel;
    this.firstProductID = "" + firstProductID;
    this.firstProductPrice = "" + firstProductPrice;
    this.firstProdctCardPrice = "" + firstProductCardPrice;
    this.firstProductTeamPrice = "" + firstProductTeamPrice;
    this.firstProductSizes = "" + firstProductSizes;
    this.secondProductName = "" + secondProductName;
    this.secondProductModel = "" + secondProductModel;
    this.secondProductID = "" + secondProductID;
    this.secondProductPrice = "" + secondProductPrice;
    this.secondProdctCardPrice = "" + secondProdctCardPrice;
    this.secondProductTeamPrice = "" + secondProductTeamPrice;
    this.secondProductSizes = "" + secondProductSizes;
}

JoinedProduct.prototype.save = function (productsArray, joinedProductsArray, guiDocument, comment) {
    //find proper index of joinedProduct among other joined products
    var ix = joinedProductsArray.length;
    if (this.isInArray(joinedProductsArray))
        ix = this.indexInArray(joinedProductsArray);

    //insert or update joinedProduct among other joined products
    joinedProductsArray[ix] = this;

    //update gui
    for (var i in productsArray) {
        if (productsArray[i][1] != this.group)
            continue;
        if (productsArray[i][18] != this.firstProductID)
            continue;

        this.addJoinedComment(guiDocument, productsArray[i][18], comment.replace(/productName/, this.secondProductName), productsArray);
        break;
    }
    for (var i in productsArray) {
        if (productsArray[i][1] != this.group)
            continue;
        if (productsArray[i][18] != this.secondProductID)
            continue;

        this.addJoinedComment(guiDocument, productsArray[i][18], comment.replace(/productName/, this.firstProductName), productsArray);
        break;
    }

    /*var elm = parent.document.getElementById("product" + _product[2] + "_JoinedProductMessage");
    if (elm != null) {
    elm.innerHTML = "<br />(<%=Resources.Resource.JoinedWith %> " + _joinedProduct.secondProductName + ")";
    elm.style.display = "";
    }

    elm = parent.document.getElementById("product" + _secondProduct[2] + "_JoinedProductMessage");
    if (elm != null) {
    elm.innerHTML = "<br />(<%=Resources.Resource.JoinedWith %> " + _joinedProduct.firstProductName + ")";
    elm.style.display = "";
    }*/
}

JoinedProduct.prototype.remove = function (guiDocument, joinedProductsArray, productsArray) {
    for (var i in productsArray) {

        if (productsArray[i][1] != this.group)
            continue;

        if (productsArray[i][18] == this.firstProductID)
            this.removeJoinedComment(guiDocument, productsArray[i][2]);
        else if (productsArray[i][18] == this.secondProductID)
            this.removeJoinedComment(guiDocument, productsArray[i][2]);
    }

    //modify products
    for (i in productsArray) {

        if (parseInt(productsArray[i][17]) != this.id)
            continue;

        productsArray[i][17] = '';
    }

    //remove from joined products
    for (i in joinedProductsArray) {

        if (joinedProductsArray[i].id != this.id)
            continue;

        joinedProductsArray.splice(i, 1);
    }
}

/*JoinedProduct.prototype.addJoinedComment = function (guiDocument, productCount, comment) {
var elm = guiDocument.getElementById("product" + productCount + "_JoinedProductMessage");
if (elm != null) {
elm.innerHTML = "<br />" + comment;
elm.style.display = "";
}
}*/
JoinedProduct.prototype.addJoinedComment = function (guiDocument, productId, comment, productsArray) {
    var productCount = "";
    for (i in productsArray) {
        if (productId == productsArray[i][18]) {
            productCount = productsArray[i][2];
            break;
        }
    }

    if (productCount.length == 0)
        return;

    var elm = guiDocument.getElementById("product" + productCount + "_JoinedProductMessage");
    if (elm != null) {
        elm.innerHTML = "<br />" + comment;
        elm.style.display = "";
    }
}

JoinedProduct.prototype.removeJoinedComment = function (guiDocument, productCount) {
    var elm = guiDocument.getElementById("product" + productCount + "_JoinedProductMessage");
    if (elm != null) {
        elm.style.display = "none";
        elm.innerHTML = "";
    }
}

JoinedProduct.prototype.load = function (joinedProductsArray, id) {
    for (i in joinedProductsArray) {

        if (joinedProductsArray[i].id != id)
            continue;

        this.id = joinedProductsArray[i].id;
        this.group = joinedProductsArray[i].group;
        this.brand = joinedProductsArray[i].brand;
        this.logo = joinedProductsArray[i].logo;
        this.name = joinedProductsArray[i].name;
        this.text = joinedProductsArray[i].text;
        this.priceHeading = joinedProductsArray[i].priceHeading;
        this.image = joinedProductsArray[i].image;
        this.thumb = joinedProductsArray[i].thumb;
        this.firstProductName = joinedProductsArray[i].firstProductName;
        this.firstProductModel = joinedProductsArray[i].firstProductModel;
        this.firstProductID = joinedProductsArray[i].firstProductID;
        this.firstProductPrice = joinedProductsArray[i].firstProductPrice;
        this.firstProdctCardPrice = joinedProductsArray[i].firstProdctCardPrice;
        this.firstProductTeamPrice = joinedProductsArray[i].firstProductTeamPrice;
        this.firstProductSizes = joinedProductsArray[i].firstProductSizes;
        this.secondProductName = joinedProductsArray[i].secondProductName;
        this.secondProductModel = joinedProductsArray[i].secondProductModel;
        this.secondProductID = joinedProductsArray[i].secondProductID;
        this.secondProductPrice = joinedProductsArray[i].secondProductPrice;
        this.secondProdctCardPrice = joinedProductsArray[i].secondProdctCardPrice;
        this.secondProductTeamPrice = joinedProductsArray[i].secondProductTeamPrice;
        this.secondProductSizes = joinedProductsArray[i].secondProductSizes;
    }
}

JoinedProduct.prototype.getNextIdInArray = function (joinedProductsArray) {
    var id = -1;
    var tmpId = 0;

    for (i in joinedProductsArray) {

        tmpId = parseInt(joinedProductsArray[i].id);

        if (id < tmpId)
            id = tmpId;
    }

    return id + 1;
}

JoinedProduct.prototype.isInArray = function (joinedProductsArray) {
    if (this.indexInArray(joinedProductsArray) >= 0)
        return true;

    return false;
}

JoinedProduct.prototype.indexInArray = function (joinedProductsArray) {
    for (i in joinedProductsArray) {
        if (joinedProductsArray[i].id != this.id)
            continue;

        return i;
    }

    return -1;
}
/*-----------JoinedProduct end---------------*/

/*-----------ProductOrder start--------------*/
function ProductOrder(id, date, club, name, reference, salesman) {
    if (id == null) { this.id = 0; } else { this.id = id; }
    this.name = "" + name;
    this.club = club;
    this.reference = reference;
    this.date = date;
    this.salesman = salesman;
}
ProductOrder.prototype.sort = function (value_a, value_b) {
    if (value_a > value_b)
        return 1;
    else if (value_a < value_b)
        return -1;
    else
        return 0;
}
ProductOrder.prototype.sortById = function (a, b) {
    return a.sort(a.id, b.id);
}
ProductOrder.prototype.sortByName = function (a, b) {
    return a.sort(a.name.toLowerCase(), b.name.toLowerCase());
}
ProductOrder.prototype.sortByDate = function (a, b) {
    return a.sort(a.date.toLowerCase(), b.date.toLowerCase());
}
ProductOrder.prototype.sortByClub = function (a, b) {
    return a.sort(a.club.toLowerCase(), b.club.toLowerCase());
}
ProductOrder.prototype.sortByReference = function (a, b) {
    return a.sort(a.reference.toLowerCase(), b.reference.toLowerCase());
}
ProductOrder.prototype.sortBySalesman = function (a, b) {
    return a.sort(a.salesman.toLowerCase(), b.salesman.toLowerCase());
}
/*----------------------ProductOrder end----------------------*/
