var shopSavedAddresses = new Array();


function viewProductImage()
{
    if (!(e = document.getElementById('shopProductImage')))
    {
        return;
    }

    newSrc = e.src;
    newSrc = newSrc.replace(/(.*)thumbnail\.php\?image=([^&]+)&width=.*/, '$1$2');

    if (newSrc && (newSrc != e.src))
    {
        window.open(newSrc, 'productImage');
    }
}


function switchProductImage(img)
{
    if (!img || !(e = document.getElementById('shopProductImage')))
    {
        return;
    }

    e.src = e.src.replace(/(.*)\/[^/]+/, '$1/' + img);
}


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

    if (!(qe = document.getElementById('quantity' + id)) || !qe.value || (Number(qe.value) < 1))
    {
        q = 1;
    }
    else
    {
        q = Number(qe.value);
    }

    window.location.href = '/' + lang + '/consumer/webshop/add_to_cart?product=' + id + '&quantity=' + q;
}


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, basedir, lang)
{
    nv = (lang == "sv" ? "Namn" : "Nimi");
    sv = (lang == "sv" ? "Gatuadress" : "Katuosoite");
    pv = (lang == "sv" ? "Postnummer" : "Postinumero");
    cv = (lang == "sv" ? "Paikkakunta" : "Paikkakunta");
    lv = (lang == "sv" ? "Land" : "Maa");
    lvn = (lang == "fi" ? "Suomi" : "Finland");
    tv = (lang == "sv" ? "Telefon" : "Puhelin");

    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; color: #999999;\" onfocus=\"if (this.value == '" + lv + "') { this.value = '" + lvn + "'; this.select(); this.style['color'] = '#000000'; }\" onblur=\"if ((this.value == '') || (this.value == '" + lv + "')) { this.value = '" + lv + "'; this.style['color'] = '#999999'; }\" /><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") +
        "<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 displaySavedAddress(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'] != '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'] != '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'] != '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'] != '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'] != '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'] != 'rgb(0, 0, 0)') && (t.style['color'] != '#000000')))
    {
        alert(lang == 'fi' ? 'Unohdit kirjoittaa puhelinnumeron.' : 'Du gav inget telefonnummer.');
        t.focus();
        return;
    }

    if (f = document.getElementById('shopAddressForm'))
    {
        if (button && button.style)
        {
            button.style['display'] = 'none';
        }

        f.submit();
    }
}


function shopCheckoutSubmitAddress2(lang)
{
    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" ? "" : "");
        return;
    }

    if (!(n = document.getElementById('shopAddressName' + w)) ||
        !(n2 = 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;
    }
    else
    {
        n2.value = n.value;
    }

    if (!(s = document.getElementById('shopAddressStreet' + w)) ||
        !(s2 = 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;
    }
    else
    {
        s2.value = s.value;
    }

    if (!(p = document.getElementById('shopAddressPostalcode' + w)) ||
        !(p2 = 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;
    }
    else
    {
        p2.value = p.value;
    }

    if (!(c = document.getElementById('shopAddressCity' + w)) ||
        !(c2 = 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;
    }
    else
    {
        c2.value = c.value;
    }

/*
    if (!(l = document.getElementById('shopAddressCountry' + w)) ||
        !(l2 = 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;
    }
    else
    {
        l2.value = l.value;
    }
*/

    if (!(t = document.getElementById('shopAddressPhone' + w)) ||
        !(t2 = 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;
    }
    else
    {
        t2.value = t.value;
    }

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


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';
    }
}
