﻿var ProdCode = "";

function GetColours(ProductCode) {
    ProdCode = ProductCode;
    $("select[id$=Colours] > option").remove();
    $.ajax(
            { type: "POST",
                url: "/uc/SCS/GetSCS.aspx?ProductCode=" + ProductCode,
                dataType: "xml",
                success: function (data) {
                    var select = $('#Colours');
                    select.append("<option value=''></option>");
                    var count = getCount(data, { "Colour": "Colour" });
                    if (count.Colour > 0) {
                        $(data).find("Colour").each(function () {
                            var select = $('#Colours');
                            if ($.browser.msie) { var title = this.text; } else { var title = this.textContent; }
                            document.getElementById('Colours').style.display = '';
                            select.append("<option value='" + title + "'>" + title + "</option>");
                        }
                        );
                        if (count.Colour > 1) {
                            document.getElementById('Colours').style.display = '';
                            select.children(":first").text("Please select...").attr("selected", true);
                        } else { document.getElementById("Colours").style.display = 'none'; GetSizes(); }

                        if (count.Colour == 1 && $('#Colours option:selected').val() != "") { document.getElementById('Colours').style.display = ''; }
                    } else {
                        document.getElementById("Colours").style.display = 'none';
                        GetSizes();
                    }
                }
            }
            );
}

function GetSizes() {
    var varColour = $('#Colours option:selected').val();
    $("select[id$=Sizes] > option").remove();
    $.ajax(
            { type: "POST",
                url: "/uc/SCS/GetSCS.aspx?ProductCode=" + ProdCode + "&Colour=" + varColour,
                dataType: "xml",
                success: function (data) {
                    var select = $('#Sizes');
                    select.append("<option value=''></option>");
                    var count = getCount(data, { "Size": "Size" });
                    if (count.Size > 0) {
                        $(data).find("Size").each(function () {
                            var select = $('#Sizes');
                            if ($.browser.msie) { var title = this.text; } else { var title = this.textContent; }
                            select.append("<option value='" + title + "'>" + title + "</option>");
                        }
                        );
                        if (count.Size > 1) {
                            document.getElementById('Sizes').style.display = '';
                            select.children(":first").text("Please select...").attr("selected", true);
                        } else { document.getElementById("Sizes").style.display = 'none'; GetStyles(); }

                        if (count.Size == 1 && $('#Sizes option:selected').val() != "") { document.getElementById('Sizes').style.display = ''; }
                    } else {
                        document.getElementById("Sizes").style.display = 'none';
                        GetStyles();
                    }
                }
            }
            );
}

function GetStyles() {
    var varColour = $('#Colours option:selected').val();
    var varSize = $('#Sizes option:selected').val();
    $("select[id$=Styles] > option").remove();
    $.ajax(
            { type: "POST",
                url: "/uc/SCS/GetSCS.aspx?ProductCode=" + ProdCode + "&Colour=" + varColour + "&Size=" + varSize,
                dataType: "xml",
                success: function (data) {
                    var select = $('#Styles');
                    var count = getCount(data, { "Style": "Style" });
                    if (count.Style > 0) {
                        $(data).find("Style").each(function () {
                            var select = $('#Styles');
                            if ($.browser.msie) { var title = this.text; } else { var title = this.textContent; }
                            select.append("<option value='" + title + "'>" + title + "</option>");
                        }
                         );
                        if (count.Style > 1) {
                            document.getElementById('Styles').style.display = '';
                            select.children(":first").text("Please select...").attr("selected", true);
                        } else { document.getElementById("Styles").style.display = 'none'; GetStockRecord(); }

                        if (count.Style == 1 && $('#Styles option:selected').val() != "") { document.getElementById('Styles').style.display = 'none'; }
                    } else {
                        document.getElementById("Styles").style.display = 'none';
                        GetStockRecord();
                    }
                }
            }
            );
}

function getCount(xml, nodes) {
    var response = {};
    for (var node in nodes) {
        response[node] = $(nodes[node], xml).length;
    }
    return response;
}

function GetStockRecord() {
    var varColour = $('#Colours option:selected').val();
    var varSize = $('#Sizes option:selected').val();
    var varStyle = $('#Style option:selected').val();

    if (varColour == undefined) { varColour = ''; }
    if (varSize == undefined) { varSize = ''; }
    if (varStyle == undefined) { varStyle = ''; }

     $.ajax(
        { type: "POST",
            url: "/uc/SCS/GetSCS.aspx?ProductCode=" + ProdCode + "&Colour=" + varColour + "&Size=" + varSize + "&Style=" + varStyle,
            dataType: "xml",
            success: function (data) { 
                var count = getCount(data, { "Stock": "Stock" });
                if (count.Stock == 1) {
                    if ($.browser.msie) {
                        $(data).find("Stock").each(function () { document.getElementById('StockID').value = this.text; document.getElementById('AddStockButton').style.display = ''; });
                        $(data).find("Prices").each(function () { document.getElementById(ProdCode).innerHTML = '&pound;' + this.text; });
                    }
                    else {
                        $(data).find("Stock").each(function () { document.getElementById('StockID').value = this.textContent; document.getElementById('AddStockButton').style.display = ''; });
                        $(data).find("Prices").each(function () { document.getElementById(ProdCode).innerHTML = '&pound;' + this.textContent; });
                    }
                    }
                }
        })
    }

    function CheckTextAreaSize() {

        var Field = document.getElementById("CustomerNotes");
        var Limit = 30

        if (Field.value.length > Limit) {
            Field.value = Field.value.substring(0, Limit);
        }
    }
