var bestillinger = [];
var currentCat = '';

function orderBrochure()
{
    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;

    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, '');
    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+= '\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 = [];

    //mochi the data away
    var qry = queryString({'order':b});
    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;
   //document.write("<a href='" + u + "'>x</a>");
   var d = doSimpleXMLHttpRequest(u);
   d.addCallbacks(displayCategoryList, loadCategoryListError);
}

function displayCategoryList(sel)
{
   document.getElementById('dynamic_list').innerHTML = sel.responseText;
}

function loadCategoryListError(err)
{
	alert(err.number);
}
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;
}


