var bestillinger = [];
var currentCat = '';

function orderBrochure() {

    try { console.log(' - order brochure - ') } catch(err){; }
    var brochure = '';

    var name = document.order.name.value;
    var company = document.order.company.value;
    var address = document.order.address.value;
    var postbox = document.order.postbox.value;
    var city = document.order.city.value;
    var proffesion = document.order.proffesion;
    var phone = document.order.phone.value;
    var email = document.order.email.value;

    name = name.replace(/^\s+|\s+$/g, '');
    company = company.replace(/^\s+|\s+$/g, '');
    address = address.replace(/^\s+|\s+$/g, '');
    postbox = postbox.replace(/^\s+|\s+$/g, '');
    city = city.replace(/^\s+|\s+$/g, '');
    phone = phone.replace(/^\s+|\s+$/g, '');
    email = email.replace(/^\s+|\s+$/g, '');

    proffesion = proffesion.options[proffesion.selectedIndex].value;

    if (name == '' || address == '' || postbox == '' || city == '') {
        alert(PLEASE_FILL_UP_THE_FORM);
        return;
    }

    //collect values
    for (var i = 0; i < bestillinger.length; i++) {
        if (brochure != '') brochure += '\n';
        brochure += bestillinger[i][1] + ' ' + bestillinger[i][0] + '\n';
    }

    var b = company + '\n';
    b += 'att. ' + name + '\n';
    b += address + '\n';
    b += postbox + ' ' + city + '\n';
    b += phone + '\n';
    b += email + '\n';
    b += proffesion + '\n';
    b += '\n\n';
    b += brochure;

    //show wait box, hide the form and clear the order list
    document.getElementById('order_meat').style.display = 'none';
    document.getElementById('iconwait').style.display = '';
    bestillinger = [];

    var country_code = document.domain;
    country_code = country_code.split('.');
    country_code = country_code[country_code.length - 1];

    //mochi the data away
    var qry = queryString({'order':b,'cc':country_code});
    var req = getXMLHttpRequest();
    req.open('POST', '/umbraco/plugins/brochure/order.aspx', true);
    req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    var d = sendXMLHttpRequest(req, qry);

    d.addCallback(orderProcessed);
}
function orderProcessed(result) {
    document.getElementById('iconwait').style.display = 'none';
    document.getElementById('order_confirmed').style.display = '';
}

function bestilBrochure(brochure) {

    for (var i = 0; i < bestillinger.length; i++) {
        if (bestillinger[i][0] == brochure) {
            bestillinger[i][1]++;
            renderBestillinger();
            return;
        }
    }
    bestillinger[bestillinger.length] = [brochure, 1];
    renderBestillinger();
}
function renderBestillinger() {
    document.getElementById('order_confirmed').style.display = 'none';
    document.getElementById('order_meat').style.display = '';

    if (bestillinger.length == 0) {
        document.getElementById('order_form').style.display = 'none';
        return;
    }

    var str = '<table>';
    for (var i = 0; i < bestillinger.length; i++) {
        str += '<tr>';
        str += '<td><input size="2" type="text" value="' + bestillinger[i][1] + '" ';
        str += ' id="order_item_' + i + '"></td><td>';
        str += bestillinger[i][0] + ' <a href="javascript:rem(' + i + ')">[x]</a> ';
        str += '</td></tr>';
    }
    str += '</table>';
    document.getElementById('ordered_items').innerHTML = str;
    document.getElementById('order_form').style.display = '';
}
function rem(idx) {
    var tmp = [];
    for (var i = 0; i < bestillinger.length; i++) {
        if (i != parseInt(idx)) tmp[tmp.length] = bestillinger[i];
    }
    bestillinger = tmp;
    renderBestillinger();

}
function updateBrochureCategory(catid) {
    loadCategoryList(catid);
    var el = document.getElementById('tab_' + catid);
    el.style.backgroundColor = '#cdcdcd';
    if (currentCat != '') {
        document.getElementById('tab_' + currentCat).style.backgroundColor = '#e3e3e3';
    }
    currentCat = catid;
}

function loadCategoryList(id) {
    var u = BROCHURE_LIBRARY_URL + '?alttemplate=brochure%20list&cat=' + id;
    var d = doSimpleXMLHttpRequest(u);
    d.addCallbacks(displayCategoryList);
}
function displayCategoryList(sel) {
    document.getElementById('dynamic_list').innerHTML = sel.responseText;
}
function brochureprinting() {
    var u = document.location.href;
    u = u.split('?');
    u = u[0];
    u += '?alttemplate=brochure%20list&print=1';
    if (typeof currentCat != 'undefined' && currentCat != '') u += '&cat=' + currentCat;

    w = window.open(u, 'print', 'width=750,height=600,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no');
    w.focus();
}
function brochurepdf() {
    var title = 'Brochure';
    var u = document.location.href;
    u = u.split('?');
    u = u[0];
    u += '?alttemplate=brochure%20list&print=1';
    if (typeof currentCat != 'undefined' && currentCat != '') u += '&cat=' + currentCat;

    var p = '/umbraco/plugins/pdf/export.aspx';
    p += '?title=' + escape(title);
    p += '&url=' + escape(u);
    document.location.href = p;
}

