var pdf_brochure_url      = 'frontend-g4/tools/pdf_engine.php';
var pdf_download_url      = 'frontend-g4/tools/pdf_retrieve.php';
var pdf_check_url         = 'frontend-g4/tools/pdf_check.php';
var pdf_brochure_vars     = '';
var pdf_brochure_method   = '';
var pdf_loading_width     = 200;
var pdf_loading_height    = 100;
var pdf_loading_msg       = 'De brochure wordt gegenereerd. Een moment geduld a.u.b.';
var pdf_acrobat_msg       = '<a href="http://www.adobe.com/nl/products/acrobat/readstep2.html" target="_blank" title="download adobe reader"><img src="frontend-g4/img/get_adobe_reader.gif" border="0" alt="download adobe reader" /></a>';
var pdf_close_msg         = '<a href="javascript:void(0);" onclick="removeLoadingScreenPDF();return false;">sluiten</a>';
var pdf_error_msg         = 'Er is een fout opgetreden.';
var pdf_show_loading      = false;
var pdf_show_acrobat      = true;
var pdf_show_close        = true;
var pdf_show_loading_img  = true;

function setShowAcrobatMsg(bool)
{
  pdf_show_acrobat = bool;
}

function setShowCloseMsg(bool)
{
  pdf_show_close = bool;
}

function setShowLoadingImg(bool)
{
  pdf_show_loading_img = bool;
}

function setLoadingSizePDF(width,height)
{
  pdf_loading_width = width;
  pdf_loading_height = height;
}

function setLoadingMsgPDF(msg)
{
  pdf_loading_msg = msg;
}

function setAcrobatMsgPDF(msg)
{
  pdf_acrobat_msg = msg;
}

function setCloseMsgPDF(msg)
{
  pdf_close_msg = msg;
}

function setErrorMsgPDF(msg)
{
  pdf_error_msg = msg;
}

function openPDF(method, loadingMessage, vars)
{
  pdf_brochure_vars     = vars;
  pdf_brochure_method   = method;
  pdf_show_loading      = loadingMessage;
  
  remoteRequest(pdf_check_url + '?' + vars, '', 'checkedPDF', 'GET');
  
  document.body.style.cursor = 'wait';
}

function checkedPDF()
{
  if (http_request && http_request.readyState == 4 && http_request.status == 200)
  {
    if(http_request.responseText=='false')
    {
      if(pdf_show_loading)
      {
        createLoadingScreenPDF();
      }
      remoteRequest(pdf_brochure_url + '?' + pdf_brochure_vars, '', 'generatedPDF', 'GET');
    }
    else
    {
      pdf_show_loading = false;
      sendToBrowserPDF();
    }
  }
}

function sendToBrowserPDF()
{
  if(!pdf_show_loading || document.getElementById('pdfLoadingScreen'))
  {
    switch(pdf_brochure_method)
    {
      case 'inline':
        inlinePDF();
        break;
      default:
        attachmentPDF();
        break;
    }
    
    removeLoadingScreenPDF();
  }
}

function generatedPDF()
{
  if (http_request && http_request.readyState == 4)
  {
    if (http_request.status == 200)
    {
      if(http_request.responseText=='pdf generated' || http_request.responseText.indexOf('Status code:200')!=-1)
      {
        sendToBrowserPDF();
      }
      else
      {
        //show error message
        if(loadingContent = document.getElementById('pdfLoadingContent'))
        {
          loadingContent.innerHTML = '<p>' + pdf_error_msg + '</p><div style="display:none;">' + http_request.responseText + '</div>';
        }
        if(loadingImg = document.getElementById('pdfLoadingImage'))
        {
          loadingImg.style.backgroundImage = 'none';
        }
      }
      document.body.style.cursor = 'default';
    }
  }
}

function positionLoadingScreenPDF()
{
  if(loading=document.getElementById('pdfLoadingScreen'))
  {
    var windowSize = getWindowSize();
    var left = ((windowSize[0] - pdf_loading_width) / 2)  + document.documentElement.scrollLeft;
    var top  = ((windowSize[1] - pdf_loading_height) / 2.5)  + document.documentElement.scrollTop;
    loading.style.position  = 'absolute';
    loading.style.top       = top + 'px';
    loading.style.left      = left + 'px';
  }
}

function createLoadingScreenPDF()
{
  var windowSize = getWindowSize();
  var height     = ((document.body.scrollHeight > windowSize[1]) ? document.body.scrollHeight : windowSize[1]) + 200;
  var opacity    = 60;
  
  var div = document.createElement("div");
  div.setAttribute("id", "pdfFillScreen");  
  div.style.position        = 'absolute';
  div.style.zIndex          = '500';
  div.style.top             = '-100px';
  div.style.left            = '0px';
  div.style.height          = height + 'px';
  div.style.width           = windowSize[0] + 'px';
  div.style.backgroundColor = '#000000';
  div.style.opacity         = (opacity / 100); 
  div.style.MozOpacity      = (opacity / 100); 
  div.style.KhtmlOpacity    = (opacity / 100); 
  div.style.filter          = "alpha(opacity=" + opacity + ")"; 
  document.body.appendChild(div);
  document.body.style.overflow  = 'hidden';
  
  var loading = document.createElement("div");
  loading.setAttribute("id", "pdfLoadingScreen");
  loading.style.zIndex    = '501';
  loading.style.height    = pdf_loading_height + 'px';
  loading.style.width     = pdf_loading_width + 'px';
  
  var contentDiv = document.createElement("div");
  contentDiv.setAttribute("id", "pdfLoadingContent");
  contentDiv.innerHTML = pdf_loading_msg;
  loading.appendChild(contentDiv);
  
  if(pdf_show_loading_img)
  {
    var imgDiv = document.createElement("div");
    imgDiv.setAttribute("id", "pdfLoadingImage");
    loading.appendChild(imgDiv);
  }
  
  if(pdf_show_acrobat || pdf_show_close)
  {
    var footerDiv = document.createElement("div");
    footerDiv.setAttribute("id", "pdfLoadingFooter");

    if(pdf_show_close)
    {
      var closeDiv = document.createElement("div");
      closeDiv.setAttribute("id", "pdfLoadingClose");
      closeDiv.innerHTML = pdf_close_msg;
      footerDiv.appendChild(closeDiv);
    }
    
    if(pdf_show_acrobat)
    {
      var acrobatDiv = document.createElement("div");
      acrobatDiv.setAttribute("id", "pdfLoadingAcrobat");
      acrobatDiv.innerHTML = pdf_acrobat_msg;
      footerDiv.appendChild(acrobatDiv);
    }
    
    loading.appendChild(footerDiv);
  }
  
  document.body.appendChild(loading);
  addEvent(window,'scroll',positionLoadingScreenPDF);
  positionLoadingScreenPDF();
}

function removeLoadingScreenPDF()
{
  removeElement('pdfFillScreen');
  removeElement('pdfLoadingScreen');
  document.body.style.overflow  = 'auto';
  document.body.style.cursor    = 'default';
}

function inlinePDF()
{
  var width         = 600;
  var height        = 600;
  
  popup = openPopup(pdf_download_url + '?' + pdf_brochure_vars,width,height,'yes','brochure');
  if(popup && !popup.focus)
  {
    popup.focus();
  }
}

function attachmentPDF()
{
  document.location = pdf_download_url + '?' + pdf_brochure_vars;
}

