function rp(p)
{
    window.location = p;
}

function getProductById(id)
{
    for(var k, k=0; k<= parent.parent.frames['menu'].products.length; k++) {
        if(parent.parent.frames['menu'].products[k][0] == id) {
            return parent.parent.frames['menu'].products[k];
        }
    }
    return false;
}

function getColorsById(id)
{
    var p = getProductById(id);
    return p[2];
}

function getSizeById(id)
{
    var p = getProductById(id);
    var t = new Array();
    t[0] = p[3];
    t[1] = p[4];
    return t;
}

function getSizeSelectBoxOptions(id)
{
    var a = getSizeById(id);
    var h = '<select class="orderSize" id="pSize' + id + '">';
    for(var k, k=a[0]; k<= a[1]; k++) {
        h += '<option value="' + k + '">' + k + '</option>';
    }
    return h + '</select>';
}

function getColorSelectBoxOptions(id)
{
    var a = getColorsById(id);
    if(a.length == 0) {
        return '';
    }
    var h = '<select class="orderColor" id="pColor' + id + '">';
    for(var k, k=0; k < a.length; k++) {
        h += '<option value="' + k + '">' + a[k] + '</option>';
    }
    return h + '</select>';
}

function getProductProps(id)
{
    var h = '';
    h += getSizeSelectBoxOptions(id);
    h += ' ';
    h += getColorSelectBoxOptions(id);
    h += ' Aantal: <input class="orderNum" type="text" id="pNum' + id + '" value="" />' 
    document.write(h);
}

function getInfoFrom(id, pre)
{
    if(document.getElementById(pre + id)) {
        return document.getElementById(pre + id).value;
    }
    return false;
}

function rptError(m)
{
    alert(m);
}

function orderProduct(id) 
{
    var size = getInfoFrom(id, 'pSize');
    
    var color = -1;
    
    color = getInfoFrom(id, 'pColor');
    if(color == false) {
        color = -1;
    }
    
    var quant = getInfoFrom(id, 'pNum');
    if(quant.length == 0 || parseInt(quant) <= 0) {
        rptError('Het aantal moet groter zijn dan 0');
        return;
    }
    if(typeof(parent.parent.frames['menu'].basket[id]) == 'undefined') {
        parent.parent.frames['menu'].basket[id] = new Array();
    }
    if(typeof(parent.parent.frames['menu'].basket[id][parseInt(size)]) == 'undefined') {
        parent.parent.frames['menu'].basket[id][parseInt(size)] = new Array();
    }
    if(color >= 0) {
        parent.parent.frames['menu'].basket[id][parseInt(size)][parseInt(color)] = parseInt(quant);
    }
    else {
        parent.parent.frames['menu'].basket[id][parseInt(size)][0] = parseInt(quant);
    }
    
    updateBasketView('order');
}

function initShop()
{
    updateBasketView('form');
}

function removeAll()
{
    parent.parent.parent.frames['menu'].basket = new Array();
}

function removeProduct(iR, mR, nR) 
{
    iR = parseInt(iR);
    mR = parseInt(mR);
    nR = parseInt(nR);
    
    var tmp = new Array();
    for(i in parent.parent.frames['menu'].basket) {
        for(m in parent.parent.frames['menu'].basket[i]) {
            for(n in parent.parent.frames['menu'].basket[i][m]) {
                 if(i != iR || m != mR || n != nR) {
                     if(typeof(tmp[i]) == 'undefined') {
                         tmp[i] = new Array();
                     }                 
                     if(typeof(tmp[i][m]) == 'undefined') {
                         tmp[i][m] = new Array();
                     }
                     tmp[i][m][n] = parent.parent.frames['menu'].basket[i][m][n];
                 }
            }
        }
    }
    removeAll();
    parent.parent.frames['menu'].basket = tmp;
    updateBasketView('form');
}

function updateBasketView(location)
{
    var h = '<ul class="prList">';
    var i, n, m, o, c;
    var o = 0;
    if(parent.parent.frames['menu'].basket.length > 0) {
        for(i in parent.parent.frames['menu'].basket) {
            var pr = getProductById(i);
            if(o == 0) {
                o = 1;
                c = 'prLow';
            }
            else {
                o = 0;
                c = 'prHigh';
            }
            h += '<li class="' + c + '"><span class="high">' + pr[1] + '</span>';
            for(m in parent.parent.frames['menu'].basket[i]) {
                for(n in parent.parent.frames['menu'].basket[i][m]) {
                    if(pr[2].length == 0) {
                        h += '<br /><span class="prTitle">Maat:</span> ' + m + '<br /><span class="prTitle">Aantal:</span> ' + parent.parent.frames['menu'].basket[i][m][n];
                    }
                    else {
                        h += '<br /><span class="prTitle">Maat:</span> ' + m + '<br /><span class="prTitle">Kleur:</span> ' + pr[2][n] + '<br /><span class="prTitle">Aantal:</span> ' + parent.parent.frames['menu'].basket[i][m][n];
                    }
                    h += '<br /><div class="prRem"><a href="javascript:removeProduct(' + i + ', ' + m + ', ' + n + ');"><img src="./images/buttons/delete.gif" alt="Verwijder dit product" /></a></div>';
                    h += '<input type="hidden" name="product[]" value="' + pr[1] + ' Maat: ' + m + ' Kleur: ' + pr[2][n] + ' Aantal: ' + parent.parent.frames['menu'].basket[i][m][n] + '" />'
                }
            }
            h += '</li>';
        }
    }
    else {
        h += '<li class="prLow">U heeft nog geen producten geselecteerd.</li>';
    }
    h += '</ul>';    
    
    if(location == 'order') {
        parent.parent.frames['formulier'].document.getElementById('orderBasket').innerHTML = h;
    }
    if(location == 'form') {
        document.getElementById('orderBasket').innerHTML = h;
    }    
}

function getAllProductsForForm()
{
    var h = '';
    var i, n, m, o, c;
    var f = document.getElementById('producten');
    
    if(parent.parent.frames['menu'].basket.length > 0) {
        for(i in parent.parent.frames['menu'].basket) {
            var pr = getProductById(i);
            h +=  pr[1] + '\n';
            for(m in parent.parent.frames['menu'].basket[i]) {
                for(n in parent.parent.frames['menu'].basket[i][m]) {
                    if(pr[2].length == 0) {
                        h += 'Maat: ' + m + '\nAantal: ' + parent.parent.frames['menu'].basket[i][m][n];
                    }
                    else {
                        h += 'Maat: ' + m + '\nKleur: ' + pr[2][n] + '\nAantal: ' + parent.parent.frames['menu'].basket[i][m][n];
                    }
                    h += '\n\n';
                }
            }
            h += '-------------------------------------------------------\n\n';
        }
    }
    else {
        h += 'Geen producten geselecteerd.';
    }
    f.value = h;  
}
