﻿// Colors are hard coded on home page since this is a sample only
// but will need to be passed in on product page
function setColor(selector) {

    var node = navigator.userAgent.match(/MSIE [^9]/i) ? 0 : 1;

    //    var test = $(selector.childNodes[node]).css("background-color");

    // Set color
    $('#divSample').css({ "background-color": $(selector.childNodes[node]).css("background-color") });
    $('.colors').css({ "border": "1px solid #000000" });

    $(selector.childNodes[node]).css({ "border": "2px solid blue" }); // IE Fix
}

// We need to retrieve the messages based on the title selection
function loadMessages() {
    var selectedTitle = $('#ddlTitles');
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

    if (sPage == '')
        sPage = "Default.aspx";

    if (($(selectedTitle).selectedIndex != -1) && ($(selectedTitle).selectedIndex != 0)) {
        var selectedValue = "|" + $(selectedTitle).val();
        $.post(sPage, { func: "getData", paramArr: selectedValue }, function (returndata) { CallBack(returndata); }, "text");

    }
}

// Message is being selected and we need to write it to the divText
function setMessage(message) {
    var divText = $('#divText');
    var message = $('#ctl00_ContentPlaceHolder1_txtMessage');

    $(divText).html($("#ddlMessages option:selected").text());

    if (message.value != '') {
        $(divText).html(message.value);
    }
}

// We are setting the font color based on the onclick event of the color selector
function setTextColor(fontColor) {
    $('#divText').css({ "color": fontColor });
}
function CallBack(returnData) {

    var data = new Array();
    var elements = new Array();
    var str = returnData;

    // Clear Listbox
    var ddMessages = document.getElementById('ddlMessages'); // This one stays old skool
    clearListbox(ddMessages);

    // Contains Id, DisplayCategoryId, Message
    data = str.split('|');

    for (var i = 0; i <= data.length - 1; i++) {
        var tmp = data[i].split(',');
        ddMessages.options.add(new Option(tmp[2], tmp[0]));
    }
    var newOption = document.createElement("option");
    newOption.value = "0";
    newOption.innerHTML = "Please Select";
    ddMessages.insertBefore(newOption, ddMessages.firstChild);
    ddMessages.selectedIndex = 0;
}

// Clears dropdownlist on returning data
function clearListbox(lb) {
    for (var i = lb.length - 1; i >= 0; i--) {
        lb.options[i] = null;
    }
    lb.selectedIndex = -1;
}

// We are going to make a call to load the shooping cart session
function addtoCart() {

    var quanta = "";

    var divSelectorError = $('#divSelectionError');

    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);    

    //alert('background = ' + backgroundColor + ' fontcolor = ' + fontColor + ' message = ' + message);
    $(divSelectorError).html("Please make all appropriate selections for your purchase");
    var initError = $(divSelectorError).html().length;

    var backgroundColor = $('#divSample').css("background-color");
    var fontColor = ""; // document.getElementById('divText').style.color;

    var message = document.getElementById('divText').innerHTML;
    var _select = $("#ddlMessages option:selected").text()
    var _selectionType = $('#ctl00_ContentPlaceHolder1_lblProductName').html();

    //    var mess_message = message.replace(/\s/g, "");

    var _quantum = 0;
    $('.quantum').each(function () {
        // Check value
        var _q = parseInt($(this).val());
        if (isNaN(_q)) {
            _q = 0;
        }

        if (_q > 0)
            quanta += "|" + $(this).next('input[type=hidden]').val() + "," + _q;

        // increment
        _quantum += _q;
    });

    if (_quantum < 1) $(divSelectorError).html($(divSelectorError).html() + "<br />Quantity");

    if (message == undefined || message.replace(/\s/g, "") == "" || message == "Please Select") {
        // Try update
        message = $('#ctl00_ContentPlaceHolder1_txtMessage').val();
        if (message == undefined || message.replace(/\s/g, "") == "") {

            $(divSelectorError).html($(divSelectorError).html() + "<br />Message");

        }
    }

    if (backgroundColor == undefined || backgroundColor.replace(/\s/g, "") == "" || backgroundColor == "transparent") {
        $(divSelectorError).html($(divSelectorError).html() + "<br />Shirt Color");
    }

    if ($(divSelectorError).html().length > initError) {
        $(divSelectorError).css({ "visibility": "visible" });
        return false;
    }

    var param = '|' + backgroundColor + '|' + fontColor + '|' + message + '|' + _selectionType;

    // Calls Ajax and posts values to Shopping Cart Session
    $.post(sPage, { func: "postToCart", paramArr: param, table: quanta }, function () { window.location = "/DisplayCart.aspx"; });

}

function emptyCallback() { }


