var thumbnailToggle = new Boolean(true);
var shopSavedAddresses = new Array();

function toggleCoverThumbnail(img, tag)
{
    if (!img || !tag)
    {
        return;
    }

    var basedir = document.getElementById('basedir').value;
    tag.src = (thumbnailToggle ?
        basedir + '/images/thumbnail/' + img + '?width=350' :
        basedir + '/images/thumbnail/' + img + '?width=150');
    thumbnailToggle = !thumbnailToggle;
    tag.style['cursor'] = (thumbnailToggle ? '-moz-zoom-in' : '-moz-zoom-out');
}

function addToCart(id, lang, button)
{
    if (!id)
    {
        return;
    }

    if (button)
    {
        button.disabled = true;
        button.style['opacity'] = '0.5';
    }

    var basedir = document.getElementById('basedir').value;
    window.location.href = basedir + '/' + lang + '/add_to_cart?title=' + id;
}


function deleteFromCart(id)
{
    if (!id)
    {
        return;
    }

    if (!(f = document.getElementById('cartForm')))
    {
        return;
    }

    f.reset();

    if (e = document.getElementById('cartQuantities' + id))
    {
        e.value = '0';
    }

    f.submit();
}


function shopCheckoutChangeAddressType(t)
{
    if (!t)
    {
        return;
    }

    if (e = document.getElementById('shopAddress_user'))
    {
        e.style['opacity'] = 0.25;
        e.style['filter'] = 'alpha(opacity=25)';
    }

    if (e = document.getElementById('shopAddress_new'))
    {
        e.style['opacity'] = '0.25';
        e.style['filter'] = 'alpha(opacity=25)';

        if ((t != 'new') && (c = document.getElementById('shopAddressSavenew')))
        {
            c.checked = false;
        }
    }

    if (e = document.getElementById('shopAddress_saved'))
    {
        e.style['opacity'] = '0.25';
        e.style['filter'] = 'alpha(opacity=25)';
    }

    if (e = document.getElementById('shopAddress_' + t))
    {
        e.style['opacity'] = '1.0';
        e.style['filter'] = 'alpha(opacity=100)';
    }
}


function shopAddSavedAddress(i, n, s, p, c, l, t, id, e, basedir, lang)
{
    var nv = (lang == "sv" ? "Namn" : "Nimi");
    var sv = (lang == "sv" ? "Gatuadress" : "Katuosoite");
    var pv = (lang == "sv" ? "Postnummer" : "Postinumero");
    var cv = (lang == "sv" ? "Paikkakunta" : "Paikkakunta");
    var lv = (lang == "sv" ? "Land" : "Maa");
    var lvn = (lang == "sv" ? "Finland" : "Puhelin");
    var tv = (lang == "sv" ? "Telefon" : "Puhelin");
    var ev = (lang == "ev" ? "E-post" : "Sähköposti");

    shopSavedAddresses[i] =
        (n ?
            "<input id=\"shopAddressName3\" type=\"hidden\" name=\"name3\" value=\"" + n + "\" />" + n + "<br />\n" :
            "<input id=\"shopAddressName3\" class=\"shop\" type=\"text\" name=\"name3\" value=\"" + nv + "\" style=\"margin-bottom: 5px; width: 100px; color: #999999;\" onfocus=\"if (this.value == '" + nv + "') { this.value = ''; this.style['color'] = '#000000'; }\" onblur=\"if ((this.value == '') || (this.value == '" + nv + "')) { this.value = '" + nv + "'; this.style['color'] = '#999999'; }\" /><br />\n") +
        (s ?
            "<input id=\"shopAddressStreet3\" type=\"hidden\" name=\"street3\" value=\"" + s + "\" />" + s + "<br />\n" :
            "<input id=\"shopAddressStreet3\" class=\"shop\" type=\"text\" name=\"street3\" value=\"" + sv + "\" style=\"margin-bottom: 5px; width: 100px; color: #999999;\" onfocus=\"if (this.value == '" + sv + "') { this.value = ''; this.style['color'] = '#000000'; }\" onblur=\"if ((this.value == '') || (this.value == '" + sv + "')) { this.value = '" + sv + "'; this.style['color'] = '#999999'; }\" /><br />\n") +
        (p ?
            "<input id=\"shopAddressPostalcode3\" type=\"hidden\" name=\"postalcode3\" value=\"" + p + "\" />" + p + "<br />\n" :
            "<input id=\"shopAddressPostalcode3\" class=\"shop\" type=\"text\" name=\"postalcode3\" value=\"" + pv + "\" style=\"margin-bottom: 5px; width: 100px; color: #999999;\" onfocus=\"if (this.value == '" + pv + "') { this.value = ''; this.style['color'] = '#000000'; }\" onblur=\"if ((this.value == '') || (this.value == '" + pv + "')) { this.value = '" + pv + "'; this.style['color'] = '#999999'; }\" /><br />\n") +
        (c ?
            "<input id=\"shopAddressCity3\" type=\"hidden\" name=\"city3\" value=\"" + c + "\" />" + c + "<br />\n" :
            "<input id=\"shopAddressCity3\" class=\"shop\" type=\"text\" name=\"city3\" value=\"" + cv + "\" style=\"margin-bottom: 5px; width: 100px; color: #999999;\" onfocus=\"if (this.value == '" + cv + "') { this.value = ''; this.style['color'] = '#000000'; }\" onblur=\"if ((this.value == '') || (this.value == '" + cv + "')) { this.value = '" + cv + "'; this.style['color'] = '#999999'; }\" /><br />\n") +
        (l ?
            "<input id=\"shopAddressCountry3\" type=\"hidden\" name=\"country3\" value=\"" + l + "\" />" + l + "<br />\n" :
            "<input id=\"shopAddressCountry3\" class=\"shop\" type=\"text\" name=\"country3\" value=\"" + lv + "\" style=\"margin-bottom: 5px; width: 100px;\" readonly=\"readonly\" /><br />\n") +
        (t ?
            "<input id=\"shopAddressPhone3\" type=\"hidden\" name=\"phone3\" value=\"" + t + "\" />" + t + "<br />\n" :
            "<input id=\"shopAddressPhone3\" class=\"shop\" type=\"text\" name=\"phone3\" value=\"" + tv + "\" style=\"margin-bottom: 5px; width: 100px; color: #999999;\" onfocus=\"if (this.value == '" + tv + "') { this.value = ''; this.style['color'] = '#000000'; }\" onblur=\"if ((this.value == '') || (this.value == '" + tv + "')) { this.value = '" + tv + "'; this.style['color'] = '#999999'; }\" /><br />\n") +
        (e ?
            "<input id=\"shopAddressEmail3\" type=\"hidden\" name=\"email3\" value=\"" + e + "\" />" + e + "<br />\n" :
            "<input id=\"shopAddressEmail3\" class=\"shop\" type=\"text\" name=\"email3\" value=\"" + ev + "\" style=\"margin-bottom: 5px; width: 100px; color: #999999;\" onfocus=\"if (this.value == '" + ev + "') { this.value = ''; this.style['color'] = '#000000'; }\" onblur=\"if ((this.value == '') || (this.value == '" + ev + "')) { this.value = '" + ev + "'; this.style['color'] = '#999999'; }\" /><br />\n") +
        "<input type=\"button\" onclick=\"window.location.href = '" + basedir + "/" + lang + "/consumer/webshop/delete_saved_address/" + id + "';\" value=\"Ta bort\" style=\"float: right; font-size: 11px;\" />\n" +
        "<div style=\"height: 5px; font-size: 1px; clear: both;\">&nbsp;</div>\n";
}


function shopDisplaySavedAddress(i)
{
    if (!(e = document.getElementById("savedAddress")))
    {
        return;
    }

    if ((i >= 0) && shopSavedAddresses[i])
    {
        e.innerHTML = shopSavedAddresses[i];
    }
    else
    {
        e.innerHTML = "";
    }
}


function shopCheckoutSubmitAddress(lang, button)
{
    if (!(n = document.getElementById('shopAddressName')))
    {
        return;
    }

    if (!n.value ||
        (n.style['color'] && (n.style['color'] != 'rgb(0, 0, 0)') && (n.style['color'] != '#000000')))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa nimen.' : 'Du gav inget namn.');
        n.focus();
        return;
    }

    if (!(s = document.getElementById('shopAddressStreet')))
    {
        return;
    }

    if (!s.value ||
        (s.style['color'] && (s.style['color'] != 'rgb(0, 0, 0)') && (s.style['color'] != '#000000')))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa katuosoitteen.' : 'Du gav ingen gatuadress.');
        s.focus();
        return;
    }

    if (!(p = document.getElementById('shopAddressPostalcode')))
    {
        return;
    }

    if (!p.value ||
        (p.style['color'] && (p.style['color'] != 'rgb(0, 0, 0)') && (p.style['color'] != '#000000')))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa postinumeron.' : 'Du gav inget postnummer.');
        p.focus();
        return;
    }

    if (!(c = document.getElementById('shopAddressCity')))
    {
        return;
    }

    if (!c.value ||
        (c.style['color'] && (c.style['color'] != 'rgb(0, 0, 0)') && (c.style['color'] != '#000000')))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa paikkakunnan.' : 'Du gav ingen ort.');
        c.focus();
        return;
    }

    if (!(l = document.getElementById('shopAddressCountry')))
    {
        return;
    }

    if (!l.value ||
        (l.style['color'] && (l.style['color'] != 'rgb(0, 0, 0)') && (l.style['color'] != '#000000')))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa maan.' : 'Du gav inget land.');
        l.focus();
        return;
    }

    if (!(t = document.getElementById('shopAddressPhone')))
    {
        return;
    }

    if (!t.value ||
        (t.style['color'] && (t.style['color'] != 'rgb(0, 0, 0)') && (t.style['color'] != '#000000')))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa puhelinnumeron.' : 'Du gav inget telefonnummer.');
        t.focus();
        return;
    }

    if (!(e = document.getElementById('shopAddressEmail')))
    {
        return;
    }

    if (!e.value ||
        (e.style['color'] && (e.style['color'] != 'rgb(0, 0, 0)') && (e.style['color'] != '#000000')))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa sähköpostiosoitteen.' : 'Du gav ingen e-postadress.');
        e.focus();
        return;
    }

    if (!(bd = document.getElementById('shopAgeDay')) ||
        !(bm = document.getElementById('shopAgeMonth')) ||
        !(by = document.getElementById('shopAgeYear')))
    {
        return;
    }

    bd = Number(bd.value);
    bm = Number(bm.value);
    by = Number(by.value);

    if (!validDate(bd, bm, by))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa oikean syntymäajan.' : 'Du gav inte en korrekt födelsetid.');
        return;
    }

    while (by < 1900)
    {
        by += 100;
    }

    var today = new Date();
    var cd = today.getDate();
    var cm = today.getMonth() + 1;
    var cy = today.getFullYear();
    var age = cy - by - (bm > cm ? 1 : (bm == cm ? (bd > cd ? 1 : 0) : 0));

    if (age < 18)
    {
        alert(lang == 'fi' ? 'Sinun täytyy olla ainakin 18 vuotta vanha.' : 'Du måste vara minst 18 år gammal.');
        return;
    }

    if (f = document.getElementById('shopAddressForm'))
    {
        if (button && button.style)
        {
            button.style['opacity'] = '0.5';
            button.disabled = true;
        }

        f.submit();
    }
}


function shopCheckoutSubmitAddress2(lang, button)
{
    if ((e = document.getElementById('shopAddressTypeUser')) && e.checked)
    {
        w = '1';
    }
    else if ((e = document.getElementById('shopAddressTypeNew')) && e.checked)
    {
        w = '2';
    }
    else if ((e = document.getElementById('shopAddressTypeSaved')) && e.checked)
    {
        w = '3';
    }
    else
    {
        alert(lang == "fi" ? "No option checked" : "No option checked");
        return;
    }

    if (!(n = document.getElementById('shopAddressName' + w)) ||
        !(n2 = document.getElementById('shopAddressName')))
    {
        alert('No name');
        return;
    }

    if (!n.value ||
        (n.style['color'] && (n.style['color'] != 'rgb(0, 0, 0)') && (n.style['color'] != '#000000')))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa nimen.' : 'Du gav inget namn.');
        n.focus();
        return;
    }
    else
    {
        n2.value = n.value;
    }

    if (!(s = document.getElementById('shopAddressStreet' + w)) ||
        !(s2 = document.getElementById('shopAddressStreet')))
    {
        alert('No street');
        return;
    }

    if (!s.value ||
        (s.style['color'] && (s.style['color'] != 'rgb(0, 0, 0)') && (s.style['color'] != '#000000')))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa katuosoitteen.' : 'Du gav ingen gatuadress.');
        s.focus();
        return;
    }
    else
    {
        s2.value = s.value;
    }

    if (!(p = document.getElementById('shopAddressPostalcode' + w)) ||
        !(p2 = document.getElementById('shopAddressPostalcode')))
    {
        alert('No postal code');
        return;
    }

    if (!p.value ||
        (p.style['color'] && (p.style['color'] != 'rgb(0, 0, 0)') && (p.style['color'] != '#000000')))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa postinumeron.' : 'Du gav inget postnummer.');
        p.focus();
        return;
    }
    else
    {
        p2.value = p.value;
    }

    if (!(c = document.getElementById('shopAddressCity' + w)) ||
        !(c2 = document.getElementById('shopAddressCity')))
    {
        alert('No city');
        return;
    }

    if (!c.value ||
        (c.style['color'] && (c.style['color'] != 'rgb(0, 0, 0)') && (c.style['color'] != '#000000')))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa paikkakunnan.' : 'Du gav ingen ort.');
        c.focus();
        return;
    }
    else
    {
        c2.value = c.value;
    }

    if (!(l = document.getElementById('shopAddressCountry' + w)) ||
        !(l2 = document.getElementById('shopAddressCountry')))
    {
        alert('No country');
        return;
    }

    if (!l.value ||
        (l.style['color'] && (l.style['color'] != 'rgb(0, 0, 0)') && (l.style['color'] != '#000000')))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa maan.' : 'Du gav inget land.');
        l.focus();
        return;
    }
    else
    {
        l2.value = l.value;
    }

    if (!(t = document.getElementById('shopAddressPhone' + w)) ||
        !(t2 = document.getElementById('shopAddressPhone')))
    {
        alert('No phone');
        return;
    }

    if (!t.value ||
        (t.style['color'] && (t.style['color'] != 'rgb(0, 0, 0)') && (t.style['color'] != '#000000')))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa puhelinnumeron.' : 'Du gav inget telefonnummer.');
        t.focus();
        return;
    }
    else
    {
        t2.value = t.value;
    }

    if (!(e = document.getElementById('shopAddressEmail' + w)) ||
        !(e2 = document.getElementById('shopAddressEmail')))
    {
        alert('No e-mail');
        return;
    }

    if (!e.value ||
        (e.style['color'] && (e.style['color'] != 'rgb(0, 0, 0)') && (e.style['color'] != '#000000')))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa sähköpostiosoitteen.' : 'Du gav ingen e-postadress.');
        e.focus();
        return;
    }
    else
    {
        e2.value = e.value;
    }

    if (f = document.getElementById('shopAddressForm'))
    {
        if (button && button.style)
        {
            button.style['opacity'] = '0.5';
            button.disabled = true;
        }

        f.submit();
    }
    else
    {
        alert('No form');
    }
}


/*
function shopCheckoutChangeDeliveryType(t)
{
    if (!t)
    {
        return;
    }

    if (e = document.getElementById('shopDelivery_postal'))
    {
        e.style['opacity'] = 0.25;
        e.style['filter'] = 'alpha(opacity=25)';
    }

    if (e = document.getElementById('shopDelivery_transpoint'))
    {
        e.style['opacity'] = '0.25';
        e.style['filter'] = 'alpha(opacity=25)';
    }

    if (e = document.getElementById('shopDelivery_' + t))
    {
        e.style['opacity'] = '1.0';
        e.style['filter'] = 'alpha(opacity=100)';
    }
}


function shopCheckoutSubmitDelivery(lang)
{
    if (f = document.getElementById('shopCheckoutDeliveryForm'))
    {
//        this.style['display'] = 'none';
        f.submit();
    }
}


function shopCheckoutChangePaymentType(t)
{
    if (!t)
    {
        return;
    }

    if (e = document.getElementById('shopPayment_nordeasolo'))
    {
        e.style['display'] = 'none';
    }

    if (e = document.getElementById('shopPayment_creditcard'))
    {
        e.style['display'] = 'none';
    }

    if (e = document.getElementById('shopPayment_' + t))
    {
        e.style['display'] = 'block';
    }
}
*/


function checkoutToggleTermsConfirm(cb)
{
    if (!cb)
    {
        return;
    }

    if (!(e = document.getElementById('checkoutContinue')))
    {
        return;
    }

    if (cb.checked)
    {
        e.style['opacity'] = '1.0';
        e.disabled = false;
    }
    else
    {
        e.style['opacity'] = '0.5';
        e.disabled = true;
    }
}


/*
 *  Handling of multiple pages of title lists.
 */
var lastPage = 1;

function previousPage()
{
    if (!lastPage || (lastPage < 2))
    {
        return;
    }

    if (n = document.getElementById('recordList' + (lastPage - 1)))
    {
        if (p = document.getElementById('recordList' + lastPage))
        {
            p.style['display'] = 'none';
        }

        lastPage--;
        n.style['display'] = 'block';

        updateNavigators();
    }
}

function nextPage()
{
    if (!lastPage)
    {
        return;
    }

    if (n = document.getElementById('recordList' + (lastPage + 1)))
    {
        if (p = document.getElementById('recordList' + lastPage))
        {
            p.style['display'] = 'none';
        }

        lastPage++;
        n.style['display'] = 'block';

        updateNavigators();
    }
}

function updateNavigators()
{
    if (a = document.getElementById('recordListNavigatorNext'))
    {
        if (m = document.getElementById('recordList' + (lastPage + 1)))
        {
            a.style['display'] = 'inline';
        }
        else
        {
            a.style['display'] = 'none';
        }
    }

    if (a = document.getElementById('recordListNavigatorPrevious'))
    {
        if (m = document.getElementById('recordList' + (lastPage - 1)))
        {
            a.style['display'] = 'inline';
        }
        else
        {
            a.style['display'] = 'none';
        }
    }

    if (c = document.getElementById('recordListPageCount'))
    {
        c.innerHTML = lastPage;
    }
}


var browsePage = 0;
var browseLastPage = 0;

function browseNext(id, page, lpage)
{
    if (!browsePage)
    {
        browsePage = page;
    }

    if (!browseLastPage)
    {
        browseLastPage = lpage;
    }

    if (!(ln = document.getElementById('browseListNext')) ||
        !ln.innerHTML)
    {
        return;
    }

    if (!(lc = document.getElementById('browseListCurrent')))
    {
        return;
    }

    if (lp = document.getElementById('browseListPrevious'))
    {
        lp.innerHTML = lc.innerHTML;
    }

    lc.innerHTML = ln.innerHTML;
    browsePage++;
    fetchNewNext(id);
    updateBrowseNavigators();
}

function browsePrevious(id, page, lpage)
{
    if (!browsePage)
    {
        browsePage = page;
    }

    if (!browseLastPage)
    {
        browseLastPage = lpage;
    }

    if (!(lp = document.getElementById('browseListPrevious')) ||
        !lp.innerHTML)
    {
        return;
    }

    if (!(lc = document.getElementById('browseListCurrent')))
    {
        return;
    }

    if (ln = document.getElementById('browseListNext'))
    {
        ln.innerHTML = lc.innerHTML;
    }

    lc.innerHTML = lp.innerHTML;
    browsePage--;
    fetchNewPrevious(id);
    updateBrowseNavigators();
}

function browseTo(id, page, lpage)
{
    browsePage = page;

    if (!browseLastPage)
    {
        browseLastPage = lpage;
    }

    if (!(l = document.getElementById('browseListCurrent')))
    {
        return;
    }

    var basedir = document.getElementById('basedir').value;
    l.innerHTML = '<div style="text-align: center; padding: 50px;"><img src="' + basedir + '/images/graphics/wait.gif" alt="" title="" /></div>';
    var url = basedir + '/webshop_getbrowselist.php';
    var args =
        'id=' + id +
        '&page=' + browsePage;
    var data = 'browseListCurrent';
    HTTPRequest(insertNewList, url, 'POST', args, data);

    fetchNewPrevious(id);
    fetchNewNext(id);
    updateBrowseNavigators();
}

function updateBrowseNavigators()
{
    if (!browsePage || !browseLastPage)
    {
        return;
    }

    if (a = document.getElementById('browseNavigatorNext'))
    {
        a.style['display'] = (browsePage < browseLastPage ? 'inline' : 'none');
    }

    if (a = document.getElementById('browseNavigatorPrevious'))
    {
        a.style['display'] = (browsePage > 1 ? 'inline' : 'none');
    }

    if (c = document.getElementById('browsePageCount'))
    {
        c.innerHTML = browsePage;
    }
}

function fetchNewPrevious(id)
{
    if (!(l = document.getElementById('browseListPrevious')))
    {
        return;
    }

    l.innerHTML = '';
    var basedir = document.getElementById('basedir').value;
    var url = basedir + '/webshop_getbrowselist.php';
    var args =
        'id=' + id +
        '&page=' + (browsePage - 1);
    var data = 'browseListPrevious';
    HTTPRequest(insertNewList, url, 'POST', args, data);
}

function fetchNewNext(id)
{
    if (!(l = document.getElementById('browseListNext')))
    {
        return;
    }

    l.innerHTML = '';
    var basedir = document.getElementById('basedir').value;
    var url = basedir + '/webshop_getbrowselist.php';
    var args =
        'id=' + id +
        '&page=' + (browsePage + 1);
    var data = 'browseListNext';
    HTTPRequest(insertNewList, url, 'POST', args, data);
}

function insertNewList(list, where)
{
    if (list && where && (l = document.getElementById(where)))
    {
        l.innerHTML = list;
    }
}


function validDate(d, m, y)
{
    vd = new Date(y, m - 1, d, 12, 0, 0);

    return ((vd.getDate() == d) && (vd.getMonth() == m - 1) && (vd.getFullYear() == y));
}

