﻿var bLinkClicked = false;
/* ------------------------------------------------------------- */
/* ------------------------------------------------------------- */

var arAssemblyItemCategory = new Array();
var arAssemblyItemId = new Array();
var arAssemblyItemName = new Array();
var arAssemblyItemPrice = new Array();
function toggleAssemblyItem(assemblyId, categoryId, maxItemCount, userAssemblyGuid, itemId, itemName, itemPrice) {
    var chkbxs = document.aspnetForm['assembly_' + assemblyId + '_' + categoryId + '_checkbox'];
    if (chkbxs == undefined) {
        alert(  'We regret that there\'s been a problem making your selection.\n\n' +
                'Please reload the page and try again.\n\n' +
                'If the problem continues, please contact us with these details: problem making selection for [assembly_' + assemblyId + '_' + categoryId + '_checkbox]')
        return false;
    }
    var doInsert = false;
    var chkCount = 0;
    var chkbx;
    for (var i = 0; i < chkbxs.length; i++) {
        if (chkbxs[i].checked == true) {
            chkCount += 1;
            //alert('chkbxs[i].checked == true = ' + (chkbxs[i].checked == true) + ' and chkbxs[i].value = ' + chkbxs[i].value + ' and itemId = ' + itemId);
            if (chkbxs[i].value == itemId) {
                chkbx = chkbxs[i];
                doInsert = true;
            }
        }
    }
    if (doInsert) {
        //check this won't exceed the maximum number of items...
        if (maxItemCount < chkCount) {
            chkbx.checked = false;
            alert('You have exceeded the maximum number of selections (' + maxItemCount + ') for this category.');
        } else {
            //add the item to the arrays...
            arAssemblyItemCategory[arAssemblyItemCategory.length] = categoryId;
            arAssemblyItemId[arAssemblyItemId.length] = itemId;
            arAssemblyItemName[arAssemblyItemName.length] = itemName;
            arAssemblyItemPrice[arAssemblyItemPrice.length] = itemPrice;
        }
    } else {
        //remove the item from the arrays...
        for (var i = 0; i < arAssemblyItemId.length; i++) {
            if (arAssemblyItemId[i] == itemId) {
                arAssemblyItemCategory.splice(i, 1);
                arAssemblyItemId.splice(i, 1);
                arAssemblyItemName.splice(i, 1);
                arAssemblyItemPrice.splice(i, 1);
            }
        }
    }
    //find the output div...
    var e = document.getElementById('UserAssemblyItems_' + assemblyId);
    if (arAssemblyItemId.length == 0) {
        var e2 = document.getElementById('EmptyAssemblyText');
        e.innerHTML = e2.innerHTML;
        return;
    }
    //output each item listed in the arrays...
    var jsUncheckBox = '';
    var jsToggleItem = '';
    var html = '';
    var totalPrice = 0;
    for (var i = 0; i < arAssemblyItemId.length; i++) {
        var chkbxs = document.aspnetForm['assembly_' + assemblyId + '_' + arAssemblyItemCategory[i] + '_checkbox'];
        for (var ii = 0; ii < chkbxs.length; ii++) {
            if (chkbxs[ii].value == arAssemblyItemId[i]) {
                jsUncheckBox = 'document.aspnetForm[\'assembly_' + assemblyId + '_' + arAssemblyItemCategory[i] + '_checkbox\'][' + ii + '].checked=false;'
            }
        }
        jsToggleItem = 'toggleAssemblyItem(\'' + assemblyId + '\', \'' + arAssemblyItemCategory[i] + '\', ' + maxItemCount + ', \'' + userAssemblyGuid + '\', \'' + arAssemblyItemId[i] + '\', \'' + arAssemblyItemName[i] + '\', \'' + arAssemblyItemPrice[i] + '\');';
/*
        jsToggleItem = 
            'toggleAssemblyItem(
                \'' + assemblyId + '\', 
                \'' + arAssemblyItemCategory[i] + '\',
                ' + maxItemCount + ', 
                \'' + userAssemblyGuid +      '\', 
                \'' + arAssemblyItemId[i] +    '\', 
                \'' + arAssemblyItemName[i] +  '\', 
                \'' + arAssemblyItemPrice[i] + '\'
            );';
*/
        html +=
                '<div class="UserAssemblyItem">' +
                    '<input type="hidden" name="UserAssemblyGuid" value="' + userAssemblyGuid + '" />' +
                    '<input type="hidden" name="AssemblyId" value="' + assemblyId + '" />' +
                    '<input type="hidden" name="SKU" value="' + arAssemblyItemId[i] + '" />' +
                    '<input type="hidden" name="Quantity" value="1" />' +
                    '<span class="ItemId">' + arAssemblyItemId[i] + '</span>' +
                    '<span class="ItemName">' + arAssemblyItemName[i] + '</span>' +
                    '<span class="ItemPrice">' + FormatCurrency(arAssemblyItemPrice[i]) + '</span>' +
                    '<div class="RemoveLink">' +
                        '<a href="javascript:' + jsUncheckBox + jsToggleItem + '">remove</a>' + 
                    '</div>' +
                '</div>';
        totalPrice += parseFloat(arAssemblyItemPrice[i]);
    }
    e.innerHTML =
        '<div class="UserAssembly">' +
            '<div class="UserAssemblyItems">' +
                html +
            '</div>' +
        '</div>' +
        '<div class="UserAssemblyTotalPrice">' +
            '<input name="UserAssemblyPrice" type="hidden" value="' + totalPrice + '">' +
            FormatCurrency(totalPrice) +
        '</div>';
}

/* ------------------------------------------------------------- */
/* ------------------------------------------------------------- */
function FormatCurrency(amount) {
    var delimiter = ","; // replace comma if desired
    var amt = new String(amount);
    a = amt.split('.', 2);
    var d = a[1];
    if (isNaN(d)) { d = '00'; }
    d = String(d + '00').substring(0, 2);
    var i = parseInt(a[0]);
    if (isNaN(i)) { a[0] = 0; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while (n.length > 3) {
        var nn = n.substr(n.length - 3);
        a.unshift(nn);
        n = n.substr(0, n.length - 3);
    }
    if (n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if (d.length < 1) { amount = n; }
    else { amount = n + '.' + d; }
    amount = minus + amount;
    return '£' + amount;
}
// end of function CommaFormatted()
/* ------------------------------------------------------------- */
/* ------------------------------------------------------------- */

////////////////////////////////////////////////////////////////////////////////
function strsubstno(stringToSearch, startMarker, arrayOfReplacements, endMarker) {
    var i = 0;
    var needle = startMarker + i + endMarker
    var haystack = new String(stringToSearch);
    var needleRegEx = new RegExp(needle, "gm");
    while ((haystack.indexOf(needle) != -1) | (i <= arrayOfReplacements.length)) {
        haystack = haystack.replace(needleRegEx, arrayOfReplacements[i]);
        i += 1;
        needle = startMarker + i + endMarker
        needleRegEx = new RegExp(needle, "gm");
    }
    return haystack;
}
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
function CreateBookmarkLink() {
    title = 'Totally Nourish (' + window.location + ') the freedom of good health, the possibilities of nature';
    url = window.location;
    if (window.sidebar) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title, url, "");
        alert('Thank you for bookmarking this page.');
    } else if (window.external) { // IE Favorite
        window.external.AddFavorite(url, title);
        alert('Thank you for adding this page to your favourites.');
    } else if (window.opera && window.print) { // Opera Hotlist
        return true;
    }
}
////////////////////////////////////////////////////////////////////////////////

