/* (c)Scientec Internet Applications + Media GmbH - www.scientec.de */

din = ["","A4","A3","A2","A1","A0","2A0","city","181"];
var sessionid = "";
var rabattsatz = "";
var stumsatz = "";
var rabattsatzcalc = "";
var projektname = "";
var projektid = "";
var cind = -1;
var newflag = "";
var jobs = [];
function validate(language)
{
  document.formular.wname.value=window.name;
  document.formular.submit();
}
function newproject(language)
{
  if (!document.projektform.projektname.value)
  {
    alert('Bitte geben Sie einen Namen für das Projekt an.');
    document.projektform.projektname.focus();
  }
  else
  {
    var wcont = window.name.split("-!-");
    window.name = wcont[0]+"-!-"+document.projektform.projektname.value+"-!--!--!--!-";
    document.formular.projekt.value = document.projektform.projektname.value;
    document.projektform.projektname.value = "";
    init(language);
    cind = jobs.length;
    jobs[jobs.length]=[];
    newflag = 1;
    makeCalculator(language);
  }
}
function init(language,nopub)
{
  jobs = [];
  var cnewind = "";
  newflag = 1;
  if (window.name.indexOf("-!-") != -1)
  {
    var wcont = window.name.split("-!-");
    var perscont = wcont[0].split("--");
    sessionid = perscont[0];
    rabattsatz = perscont[1];
    stumsatz = perscont[2];
    if (rabattsatz)
    {
      rabattsatzcalc = rabattsatz.replace(/,/,".");
    }
    if (sessionid && nopub != "1")
    {
      document.getElementById('projektarchiv').style.display = '';
    }
    projektname = wcont[1];
    projektid = wcont[4];
    if (nopub != "1")
    {
      document.getElementById('contents').innerHTML = '';
    }
    var wjobs = wcont[3].split("-:-");
    for(var i = 0; i < wjobs.length-1; i++)
    {
      jobs[i] = [];
      var cjobs = wjobs[i].split("::");
      jobs[i]['motiv'] = cjobs[1];
      jobs[i]['medien'] = cjobs[2];
      jobs[i]['abmess'] = cjobs[3];
      jobs[i]['formate'] = cjobs[4];
      jobs[i]['flaeche'] = cjobs[5];
      jobs[i]['mfolder'] = cjobs[6];
      jobs[i]['mitem'] = cjobs[7];
      jobs[i]['divreihe'] = cjobs[8].split(',');
      jobs[i]['spezifikationen'] = cjobs[9];
      jobs[i]['verwendung'] = cjobs[10];
      jobs[i]['preis'] = cjobs[11];
      jobs[i]['motivbearbeitung'] = cjobs[12];
      jobs[i]['motivanzahl'] = cjobs[13];
      jobs[i]['text'] = cjobs[14];
      jobs[i]['anzahl'] = cjobs[15];
    }
    showbasket(language,nopub);
    if (nopub != "1")
    {
      if (projektname)
      {
        document.getElementById('lascheAktuell').style.display='';
        document.getElementById('aktuellesProjekt').style.display='';
        document.getElementById('projekthead').style.display='';
        document.getElementById('projekthead').innerHTML="Name: "+projektname;
        document.getElementById('neuerjob').style.display='';
        document.getElementById('speichern').style.display='';
      }
      else
      {
        document.getElementById('lascheAktuell').style.display='none';
        document.getElementById('aktuellesProjekt').style.display='none';
        document.getElementById('projekthead').style.display='none';
        document.getElementById('speichern').style.display='none';
        document.getElementById('neuerjob').style.display='none';
        document.getElementById('speichern').style.display='none';
      }
    }
  }
  if (nopub != "1")
  {
    if (!sessionid)
    {
      document.getElementById('speichern').style.display='none';
    }
    document.getElementById('auftragsverwaltung').src='/cgi-bin/custom/projekte.pl?language='+language+'&sessionid='+sessionid;
    if (projektname)
    {
      document.formular.projekt.value=projektname;
      document.formular.projektid.value=projektid;
    }
    else
    {
      if (document.getElementById('projektname'))
      {
        document.getElementById('projektname').style.display='';
      }
    }
    if (!rabattsatz)
    {
      if (document.getElementById('lascheNeu'))
      {
        document.getElementById('lascheNeu').style.display='none';
      }
      document.getElementById('neuesprojekt').style.display='none';
      document.getElementById('branchendef').style.display='block';
    }
  }
}

function setbranche(language)
{
  if (document.branchenauswahl.branche.options[document.branchenauswahl.branche.selectedIndex].value)
  {
    var chelp = document.branchenauswahl.branche.options[document.branchenauswahl.branche.selectedIndex].value.split("||");
    var wcont = window.name.split("-!-");
    var perscont = wcont[0].split("--");
    perscont[1] = chelp[1];
    window.name = perscont[0]+"--"+perscont[1]+"--"+perscont[2]+"-!-"+wcont[1]+"-!-"+wcont[2]+"-!-"+wcont[3]+"-!-"+wcont[4]+"-!-";
    document.getElementById('lascheneu').style.display='';
    document.getElementById('neuesprojekt').style.display='';
    document.getElementById('branchendef').style.display='none';
  }
  else
  {
    if (language == 'd')
    {
      alert("Bitte wählen Sie die Branche aus.");
    }
    else
    {
      alert("Please select your branch.");
    }
    document.branchenauswahl.branche.focus();
  }
}

function showbasket(language,nopub)
{
 if (nopub != "1")
 {
   document.getElementById('contents').style.display='';
 }
 medienflaeche = [];
 spezflaeche = [];
 spezanzahl = [];
 var gesamtpreis = 0;
 var html = "";
 var wname = sessionid+"--"+rabattsatz+"--"+stumsatz+"-!-"+projektname+"-!-"+jobs.length+"-!-";
 if (jobs.length)
 {
   if (jobs[jobs.length-1]['flaeche'] == undefined)
   {
     jobs.splice(jobs.length-1, 1);
   }
 }
 if (jobs.length)
 {
  for(var i = 0; i < jobs.length; i++)
  {
    medienflaeche[jobs[i]['medien']] = 0;
    spezflaeche[jobs[i]["medien"]] = [];
    spezanzahl[jobs[i]["medien"]] = [];
    if (jobs[i]['spezifikationen'])
    {
      var cspez = jobs[i]['spezifikationen'].split(",");
      for(var ii = 0; ii < cspez.length; ii++)
      {
        if (cspez[ii])
        {
          spezflaeche[jobs[i]["medien"]][cspez[ii]] = 0;
          spezanzahl[jobs[i]["medien"]][cspez[ii]] = 0;
        }
      }
    }
  }
  var preis = 0;
  var cmotivpreis = 0;
  var motivpreis = 0;
  var jobmotivpreis = 0;
  var rabattmotive = 0;
  var kundenrabatt = 0;
  var mwst = 0;
  var motive = ',';
  var motivanzahl = [];
  var jobspezpreis = [];
  motivanzahl['original'] = 0;
  motivanzahl['datei'] = 0;
  motivanzahl['smp'] = 0;
  for(var i = 0; i < jobs.length; i++)
  {
    var j = i+1;
    motivanzahl[jobs[i]['motivbearbeitung']] += jobs[i]['motivanzahl']*1;
    if (jobs[i]['motivanzahl'] > 1)
    {
      var maxanz = 0;
      var cind = -1;
      for(var k = 0; k < motivbearbeitung['anzahl'].length; k++)
      {
        if (motivbearbeitung['anzahl'][k] >= maxanz && motivbearbeitung['anzahl'][k] <= jobs[i]['motivanzahl'])
        {
          maxanz = motivbearbeitung['anzahl'][k];
          cind = k;
        }
      }
      if (cind >= 0)
      {
        preis = motivbearbeitung[jobs[i]['motivbearbeitung']][cind]*jobs[i]['motivanzahl'];
        cmotivpreis = motivbearbeitung[jobs[i]['motivbearbeitung']][cind]*jobs[i]['motivanzahl'];
        jobmotivpreis = motivbearbeitung[jobs[i]['motivbearbeitung']][cind]*jobs[i]['motivanzahl'];
      }
    }
    else
    {
      jobmotivpreis = motivbearbeitung[jobs[i]['motivbearbeitung']][0]*1;
      preis += motivbearbeitung[jobs[i]['motivbearbeitung']][0]*1;
      if (motive.indexOf(jobs[i]['motiv']) == -1 || !jobs[i]['motiv'])
      {
        motive += jobs[i]['motiv']+',';
        cmotivpreis += motivbearbeitung[jobs[i]['motivbearbeitung']][0]*1;
      }
      else
      {
        rabattmotive += motivbearbeitung[jobs[i]['motivbearbeitung']][0]*1;
        motivanzahl[jobs[i]['motivbearbeitung']] -= 1;
      }
    }
    wname += i+"::"+jobs[i]['motiv']+"::"+jobs[i]['medien']+"::"+jobs[i]['abmess']+"::"+jobs[i]['formate']+"::"+jobs[i]['flaeche']+"::"+jobs[i]['mfolder']+"::"+jobs[i]['mitem']+"::"+divreihe+"::"+jobs[i]['spezifikationen']+"::"+jobs[i]['verwendung']+"::"+jobs[i]['preis']+"::"+jobs[i]['motivbearbeitung']+"::"+jobs[i]['motivanzahl']+"::"+jobs[i]['text']+"::"+jobs[i]['anzahl']+"-:-";
    gesamtpreis += jobs[i]['preis']*1;
    medienflaeche[jobs[i]['medien']] += jobs[i]['flaeche']*1;
    var maxf = 0;
    var cind = -1;
    for(var k = 0; k < mflaechen[jobs[i]['medien']].length; k++)
    {
      if (mflaechen[jobs[i]['medien']][k] >= maxf && mflaechen[jobs[i]['medien']][k] <= jobs[i]['flaeche']*1)
      {
        maxf = mflaechen[jobs[i]['medien']][k]*1;
        cind = k;
      }
    }
    if (cind >= 0)
    {
      var jobmedienpreis = mpreise[jobs[i]['medien']][cind]*jobs[i]['flaeche'];
    }
    var cspez = jobs[i]['spezifikationen'].split(",");
    for(var ii = 0; ii < cspez.length; ii++)
    {
      if (cspez[ii])
      {
        spezflaeche[jobs[i]["medien"]][cspez[ii]] += jobs[i]["flaeche"]*1;
        spezanzahl[jobs[i]["medien"]][cspez[ii]] += jobs[i]["anzahl"]*1;
      }
    }
    if (jobs[i]['verwendung'] == "spezial")
    {
      html += "<h4 class='printjobName'>"+jobs[i]["mfolder"]+" : "+jobs[i]["mitem"]+"</h4><hr class=\"hidden\" size=1><div class=printjob>";
      html += "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"width:506px\"><tr><td colspan=\"2\"><b>"+jobs[i]["text"]+"</td><td>&nbsp;</td></tr>";
      if (jobs[i]["flaeche"] > 0)
      {
        html += "<tr><td width=\"5%\"><b>"+vok['anzahl']+":</b></td><td>"+jobs[i]["flaeche"]+"</td><td>&nbsp;</td></tr>";
      }
    }
    else
    {
      html += "<h4 class='printjobName'>"+vok['printjob']+" "+j+"</h4><hr class=\"hidden\" size=1><div class=printjob>";
      html += "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"width:506px\"><tr><td><b>"+vok['motiv']+":</b></td><td>";
      if (jobs[i]['motivanzahl'] > 1)
      {
        html += text['anzahldermotive']+": "+jobs[i]["motivanzahl"]+"</td><td></td></tr>";
      }
      else
      {
        if (jobs[i]["motiv"])
        {
          html += jobs[i]["motiv"]+"</td><td></td></tr>";
        }
      }
      html += "<tr><td><b>"+text['motivanlieferung']+"</b></td>";
      if (jobs[i]["motivbearbeitung"] == "original")
      {
        html += "<td>"+text['motivanoriginal']+"</td><td align=\"right\">"+formatPrice(jobmotivpreis)+" &euro;</td></tr>";
      }
      if (jobs[i]["motivbearbeitung"] == "datei")
      {
        html += "<td>"+text['motivandatei']+"</td><td align=\"right\">"+formatPrice(jobmotivpreis)+" &euro;</td></tr>";
      }
      if (jobs[i]["motivbearbeitung"] == "smp")
      {
        html += "<td>"+text['motivanvorhanden']+"</td><td align=\"right\">"+formatPrice(jobmotivpreis)+" &euro;</td></tr>";
      }
//      html += jobs[i]["verwendung"]+"<br>";
//      html += "</div>";
      html += "<tr><td><b>"+text['formate']+":</b></td><td>";
      html += "<table border=0 cellpadding=0 cellspacing=0>";
      var abmess = jobs[i]['abmess'].split("-!!-");
      var rowcount = 0;
      for(var j = 0; j < abmess.length; j++)
      {
        if (rowcount == 2)
        {
          rowcount = 0;
          if (abmess[j] && abmess[j-1] && abmess[j-2])
          {
            html += "<tr><td>"+vok['breite']+": "+abmess[j-1]+" cm</td><td>&nbsp;"+vok['hoehe']+":"+abmess[j]+" cm</td><td>&nbsp;"+vok['auflage']+":"+abmess[j-2]+"</td></tr>";
          }
        }
        else
        {
          rowcount++;
        }
      }
      var formate = jobs[i]['formate'].split("-!!-");
      var rowcount = 0;
      for(var j = 0; j < formate.length; j++)
      {
        if (rowcount == 1)
        {
          rowcount = 0;
          if (din[formate[j]] && formate[j-1])
          {
            html += "<tr><td colspan=2>"+din[formate[j]]+"</td><td>&nbsp;"+vok['auflage']+":"+formate[j-1]+"</td></tr>";
          }
        }
        else
        {
          rowcount++;
        }
      }
      html += "</table></td><td></td></tr>";
      html += "<tr><td valign=\"bottom\"><b>"+text['gesamtflaeche']+":</b></td><td> "+formatPrice(jobs[i]["flaeche"])+" m<sup>2</sup></td><td></td></tr>";
      html += "<tr><td valign=\"top\"><b>"+text['mediumtit']+":</b></td><td> "+jobs[i]["mfolder"]+" : "+jobs[i]["mitem"]+"</td><td align=\"right\" valign=\"top\" width=\"80\">"+formatPrice(jobmedienpreis)+" &euro;</td></tr>";
      var cspez = jobs[i]['spezifikationen'].split(",");
      for(var ii = 0; ii < cspez.length; ii++)
      {
        if (cspez[ii])
        {
          var maxf = 0;
          var cind = -1;
          if (sstueck[cspez[ii]])
          {
            for(var k = 0; k < sflaechen[cspez[ii]].length; k++)
            {
              if (sflaechen[cspez[ii]][k] >= maxf && sflaechen[cspez[ii]][k] <= jobs[i]["anzahl"]*1)
              {
                maxf = sflaechen[cspez[ii]][k]*1;
                cind = k;
              }
            }
            if (cind >= 0)
            {
              jobspezpreis[ii] = spreise[cspez[ii]][cind]*jobs[i]["anzahl"]*1;
            }
          }
          else
          {
            for(var k = 0; k < sflaechen[cspez[ii]].length; k++)
            {
              if (sflaechen[cspez[ii]][k] >= maxf && sflaechen[cspez[ii]][k] <= jobs[i]["flaeche"]*1)
              {
                maxf = sflaechen[cspez[ii]][k]*1;
                cind = k;
              }
            }
            if (cind >= 0)
            {
              jobspezpreis[ii] = spreise[cspez[ii]][cind]*jobs[i]["flaeche"]*1;
            }
          }
          html += "<tr><td valign=\"top\"><b>"+divktit[jobs[i]['divreihe'][ii-1]]+":</b></td><td> "+tit['si'+cspez[ii]]+"</td><td align=\"right\" width=\"80\" valign=\"top\">"+formatPrice(jobspezpreis[ii])+" &euro;</td></tr>";
        }
      }
    }
    html += "<tr><td colspan=3><hr size=1></td></tr>";
    html += "<tr><td><b>"+text['summedruckposition']+":</b></td><td></td><td align=\"right\" width=\"80\">"+formatPrice(jobs[i]["preis"])+" &euro;</td></tr>";
    html += "<tr><td colspan=3>&nbsp;</td></tr></table>";
    html += "<div class=printjobbuttons>";
    html += "<a href=\"#top\" onclick=\"cnewind = '';cind = "+i+";newflag = 0;makeCalculator('"+language+"');\" class=button><button>"+text['jobopen']+"</button></a>";
    html += "<a href=\"#top\" onclick=\"if (jobs[jobs.length-1]['flaeche'] != undefined){cnewind = jobs.length;jobs[jobs.length]=[];}cind = "+i+";newflag = 0;makeCalculator('"+language+"');\" class=button><button>"+text['jobduplizieren']+"</button></a>";
    html += "<a href=\"#\" onclick=\"deletejob("+i+")\" class=button><button>"+text['jobloeschen']+"</button></a>";
    html += "</div>";
    html += "</div>";
  }
  maxanz = 0;
  cind = -1;
  for(k = 0; k < motivbearbeitung['anzahl'].length; k++)
  {
    if (motivbearbeitung['anzahl'][k] >= maxanz && motivbearbeitung['anzahl'][k] <= motivanzahl['original'])
    {
      maxanz = motivbearbeitung['anzahl'][k]*1;
      cind = k;
    }
  } 
  if (cind >= 0)
  {
    motivpreis += motivbearbeitung['original'][cind]*motivanzahl['original'];
  }
  maxanz = 0;
  cind = -1;
  for(k = 0; k < motivbearbeitung['anzahl'].length; k++)
  {
    if (motivbearbeitung['anzahl'][k] >= maxanz && motivbearbeitung['anzahl'][k] <= motivanzahl['datei'])
    {
      maxanz = motivbearbeitung['anzahl'][k]*1;
      cind = k;
    }
  }
  if (cind >= 0)
  {
    motivpreis += motivbearbeitung['datei'][cind]*motivanzahl['datei'];
  }
  maxanz = 0;
  cind = -1;
  for(k = 0; k < motivbearbeitung['anzahl'].length; k++)
  {
    if (motivbearbeitung['anzahl'][k] >= maxanz && motivbearbeitung['anzahl'][k] <= motivanzahl['smp'])
    {
      maxanz = motivbearbeitung['anzahl'][k]*1;
      cind = k;
    }
  } 
  if (cind >= 0)
  {
    motivpreis += motivbearbeitung['smp'][cind]*motivanzahl['smp'];
  }
  rabattmotive = rabattmotive - motivpreis*1 + cmotivpreis*1; 
  for (var y in medienflaeche)
  {
    var maxf = 0;
    var cind = -1;
    for(var k = 0; k < mflaechen[y].length; k++)
    {
      if (mflaechen[y][k] >= maxf && mflaechen[y][k] <= medienflaeche[y]*1)
      {
        maxf = mflaechen[y][k]*1;
        cind = k;
      }
    }
    if (cind >= 0)
    {
      preis += mpreise[y][cind]*medienflaeche[y];
//        alert("m"+medienflaeche[y]);
//        alert(mpreise[y][cind]);
    }
    for (var z in spezflaeche[y])
    {
      var maxf = 0;
      var cind = -1;
      if (sstueck[z])
      {
        for(var k = 0; k < sflaechen[z].length; k++)
        {
          if (sflaechen[z][k] >= maxf && sflaechen[z][k] <= spezanzahl[y][z]*1)
          {
            maxf = sflaechen[z][k]*1;
            cind = k;
          }
        }
        if (cind >= 0)
        {
          preis += spreise[z][cind]*spezanzahl[y][z];
        }
      }
      else
      {
        for(var k = 0; k < sflaechen[z].length; k++)
        {
          if (sflaechen[z][k] >= maxf && sflaechen[z][k] <= spezflaeche[y][z]*1)
          {
            maxf = sflaechen[z][k]*1;
            cind = k;
          }
        }
        if (cind >= 0)
        {
          preis += spreise[z][cind]*spezflaeche[y][z];
        }
      }
    }
  }
//  alert(preis);
  rabattpreis = gesamtpreis-preis*1;
  if (rabattpreis < 0.001)
  {
    rabattpreis = 0;
  }
  html += "<hr class=\"hidden\" size=1><table width=\"100%\" style=\"width:515px\">";
  html += "<tr><td><b>"+text['summe']+":</b></td><td align=\"right\" valign=\"bottom\" width=\"100\">"+formatPrice(gesamtpreis)+" &euro;</td></tr>";
  if (rabattpreis)
  {
    html += "<tr><td>"+text['rabattspezifikationen']+": </td><td align=\"right\" valign=\"bottom\" width=\"100\">-"+formatPrice(rabattpreis)+" &euro;</td></tr>";
  }
  if (rabattmotive)
  {
    html += "<tr><td>"+text['rabattmotive']+":</td><td align=\"right\" valign=\"bottom\" width=\"100\">-"+formatPrice(rabattmotive)+" &euro;</td></tr>";
  }
  preis -= rabattmotive*1;
  if (preis < minderlimit)
  {
    if (preis+minderpreis > minderlimit)
    {
      minderpreis = minderlimit - preis;
    }
    html += "<tr><td>"+text['mindermengenzuschlag']+"</td><td align=\"right\" valign=\"bottom\" width=\"100\">"+formatPrice(minderpreis)+" &euro;</td></tr>";
    preis += minderpreis;
  }
  if (gesamtpreis != preis)
  {
    html += "<tr><td colspan=2><hr size=1></td></tr>";
    if (rabattsatz > 0)
    {
      html += "<tr><td valign=\"bottom\"></td><td align=\"right\" valign=\"bottom\" width=\"100\">"+formatPrice(preis)+" &euro;</td></tr>";
    }
    else
    {
      html += "<tr><td valign=\"bottom\">"+text['gesamtpreis']+":</td><td align=\"right\" valign=\"bottom\" width=\"100\">"+formatPrice(preis)+" &euro;</td></tr>";
    }
  }
  if (rabattsatzcalc > 0)
  {
    kundenrabatt = preis*rabattsatzcalc/100;
    preis = preis*(100-rabattsatzcalc)/100;
    html += "<tr><td valign=\"bottom\">"+rabattsatz+"% "+text['kundenrabatt']+":</td><td align=\"right\" valign=\"bottom\" width=\"100\">-"+formatPrice(kundenrabatt)+" &euro;</td></tr>";
    html += "<tr><td colspan=2><hr size=1></td></tr>";
    html += "<tr><td valign=\"bottom\">"+text['gesamtpreis']+":</td><td align=\"right\" valign=\"bottom\" width=\"100\">"+formatPrice(preis)+" &euro;</td></tr>";
  }
  if (stumsatz != "N")
  {
    mwst = preis*mwstsatz/100;
    html += "<tr><td valign=\"bottom\">"+text['mwst']+":</td><td align=\"right\" valign=\"bottom\" width=\"100\">"+formatPrice(mwst)+" &euro;</td></tr>";
    mwst = preis*(1+mwstsatz/100);
  }
  else
  {
    mwst = preis;
  }
  html += "<tr><td colspan=2><hr size=1></td></tr>";
  html += "<tr><td valign=\"bottom\"><b>"+text['brutto']+":</b></td><td align=\"right\" valign=\"bottom\" width=\"100\">"+formatPrice(mwst)+" &euro;</td></tr>";
  html += "</table>";
  if (nopub != "1")
  {
    document.getElementById('contents').innerHTML = html;
  }
  top.window.name = wname+"-!-"+projektid+"-!-"+html+"-!-"+formatPrice(preis);
 }
 else
 {
  if (nopub != "1")
  {
    document.getElementById('contents').innerHTML = '';
  }
  top.window.name = wname+"-!--!--!-";
 }
}

function deletejob(cind)
{
  jobs.splice(cind, 1);
  showbasket();
}

function formatPrice(p){	//
 if (!parseInt(p)) {p = "0"+p;}
 pf = (p - parseInt(p))*100;
 if (pf == 0)
 {
   pf = "00";
 }
 else
 {
   pf = parseInt(pf+0.5);
   if (pf < 10)
   {
     pf = "0"+String(pf);
   }
 }
 if (pf == 100)
 {
   p++;
   return parseInt(p)+",00";
 }
 else
 {
   return parseInt(p)+","+pf;
 }
}

