koszyk=new Tkoszyk();

function Tkoszyk()
{
  this[0]=null;
}

function Tpozycja(id,kod,nazwa,cn,cb,ilosc)
{
  this.id=id;
  this.kod=kod;
  this.nazwa=nazwa;
  this.cn=cn;
  this.cb=cb;
  this.ilosc=ilosc;
}

function dokoszyka(id,kod,nazwa,cn,cb)
{
  for(i=0;koszyk[i];i++)
    if(koszyk[i].id==id){
      l=prompt('TEN TOWAR JEST JUŻ W KOSZYKU\nMożesz podać nową liczbę zamawianych sztuk:',koszyk[i].ilosc);
      if(invalid(l))return;
      if(l==0){ 
        delete koszyk[i]; /* ludzie czesto o tym zapominaja */
        do{koszyk[i]=koszyk[i+1];}while(koszyk[++i]);
        kalkuluj();  
        return;
      }
      koszyk[i].ilosc=l;
      kalkuluj();
      return;
    }
  l=prompt('Podaj liczbę zamawianych sztuk:',1);
  if(invalid(l))return;
  if(l>0){
    koszyk[i]=new Tpozycja(id,kod,nazwa,cn,cb,l);
    koszyk[i+1]=null;
    kalkuluj();
  }
}

function invalid(l)
{
  if((l==null)) return true;
  if(l.length==0) {
    alert('Nie wprowadziłeś liczby zamawianych sztuk.');
    return true;
  }
  if(l>-1&&l<10001&&l==Math.round(l))return false;
  alert('Wprowadź liczbę całkowitą od 0 do 10000');
  return true;
}

function zkoszykadokoszyka(id)
{
  dokoszyka(id);
  pokazkoszyk();
}

function pokazkoszyk()
{
  w_koszykstart();
  if(koszyk[0]){
    i=-1;
    w_listastart();
    while(koszyk[++i]){
      
//
//czyszczenie starych wpisow stworzonych przez cofniecie rpzegladarki history back
     if(formatc((koszyk[i].ilosc*koszyk[i].cb))=="N,aN"){ 
        delete koszyk[i]; /* ludzie czesto o tym zapominaja */
        do{koszyk[i]=koszyk[i+1];}while(koszyk[++i]);
        kalkuluj();  
        return;
      }

      w_pozycja(i);
    }
  w_listastop();
  } else w_pustykoszyk();
  w_koszykstop();
}

function kalkuluj()
{
  for(i=0,netto=0,brutto=0;koszyk[i];i++){
    netto+=(koszyk[i].cn*koszyk[i].ilosc);
    brutto+=(koszyk[i].cb*koszyk[i].ilosc);
  }
  w_razem();
}


function formatc(c)
{  
  t=""+c; /* ach ten brak typow */
  if(t.length==0)return "0,00";
  if(t.length==1)return "0,0"+t;
  if(t.length==2)return "0,"+t;
  return t.substring(0,t.length-2)+","+t.substring(t.length-2,t.length);
}
/********** koniec **********/


/*** Grafika ***/

function w_koszykstart() {
  towary.document.open();

  towary.document.writeln("<html>");
  towary.document.writeln("<head>");
  towary.document.writeln("<meta name=Language content=pl>");
  towary.document.writeln("<meta http-equiv='content-type' content='text/html; charset=iso-8859-2'>");
  towary.document.writeln("<title>Tekst</title>");

  towary.document.writeln("<script language='JavaScript'>");
  towary.document.writeln("function val_str(pole,tekst,maximum,minimum) {");
  towary.document.writeln("  trim(pole);");
  towary.document.writeln("  if (document.forms[0].elements[pole].value==''){");
  towary.document.writeln("    if(minimum > 0){");
  towary.document.writeln("      document.forms[0].elements[pole].focus();");
  towary.document.writeln("      alert(\"Pole z\" + tekst + \" musi być wypełnione.\");");
  towary.document.writeln("           return false;");
  towary.document.writeln("        } else return true;");
  towary.document.writeln("  }");
  towary.document.writeln("  if (!minimum) minimum = 0;");
  towary.document.writeln("  if (document.forms[0].elements[pole].value.length < minimum || document.forms[0].elements[pole].value.length > maximum){");
  towary.document.writeln("      document.forms[0].elements[pole].focus();");
  towary.document.writeln("                if (minimum != maximum) alert (\"Pole z \" + tekst + \" musi zawierać od \" + minimum + \" do \" + maximum + \" znaków.\");");
  towary.document.writeln("                 else alert (\"Pole z\" + tekst + \" musi mieć dokładnie \" + maximum + \" znaków.\");");
  towary.document.writeln("                return false;");
  towary.document.writeln("        } ");
  towary.document.writeln("  return true;");
  towary.document.writeln("}");
  towary.document.writeln("function trim(pole){");
  towary.document.writeln("   str = document.forms[0].elements[pole].value;");
  towary.document.writeln("        p1 = 0;");
  towary.document.writeln("        while (str.charAt(p1) == ' ') p1++;");
  towary.document.writeln("        p2 = str.length - 1;");
  towary.document.writeln("        while (str.charAt(p2) == ' ') p2--;");
  towary.document.writeln("        nowy = '';");
  towary.document.writeln("        for (i = p1; i <= p2; i++) nowy = nowy + str.charAt(i);");
  towary.document.writeln("        document.forms[0].elements[pole].value = nowy;");
  towary.document.writeln("        ");
  towary.document.writeln("   return true;");
  towary.document.writeln("}");  
  
  towary.document.writeln("function validateForm(){");      
  towary.document.writeln("        if (!val_str('nazwa_klienta',' nazwiskiem osoby składającej zamówienie', 150, 3)) return false;");
  towary.document.writeln("        if (!val_str('telefon',' telefonem kontaktowym', 30, 7)) return false;");
  towary.document.writeln("        if (!val_str('ulica',' adresem przesyłki', 150, 3)) return false;");
  towary.document.writeln("        if (!val_str('miasto',' adresem przesyłki', 150, 3)) return false;");

  towary.document.writeln("        if (!val_str('transport','e sposobem dostarczenia towaru', 35, 3)) return false;");


  towary.document.writeln("        if (!document.forms[0].Upowazniam.checked) { alert ('Jedynie po zapoznaniu i zaakceptowaniu regulaminu sklepu możesz dokonać zakupów.'); return false }");
  towary.document.writeln(" return true");      
  towary.document.writeln("}");      
  towary.document.writeln("</script>");      

  towary.document.writeln("</head>");
  towary.document.writeln("<body bgcolor=#D5EAFF>");

}

function w_listastart() {
  towary.document.writeln("<form action=sklep_wyslij.php method=get target='_top' onSubmit='return validateForm();'>");
  //fffff
 // towary.document.writeln("<p><strong> Koszt przesyłki pocztowej zawiera się w cenie produktu.<br> Przy większej ilości zamówień możliwy rabat.</strong></p>");

 // towary.document.writeln("<br><p align='center'><a href='sklep.php'>Powrót do sklepu</a></p>");

towary.document.writeln("<table border='0' cellPadding='0' cellSpacing='1' style='FONT-FAMILY: arial; FONT-SIZE: 9pt' width='100%'><TBODY><tr><td width='70%'><p align='center'><a href='sklep.php'>Powrót do sklepu</a></p></td><td vAlign='top' width='20%'>Regulamin sklepu</td><td vAlign='top' width='10%'>O nas</td></tr></TBODY></table><br>");

  towary.document.writeln("<center>");
  towary.document.writeln("<table width=97% cellspacing=0 cellpadding=0 border=0>");
  towary.document.writeln("<tr bgcolor='#000000'>");
  towary.document.writeln("        <td width=75%><font color='#FFFFFF'>Zawartość koszyka</font></td>");
  towary.document.writeln("        <td width=1></td>");
  towary.document.writeln("        <td ></td>");
  towary.document.writeln("        <td width=1></td>");
  towary.document.writeln("        <td colspan=3 valign=bottom></td>");
  towary.document.writeln("</tr>");
  towary.document.writeln("<tr bgcolor='#000080'>");
  towary.document.writeln("        <td ><font color='#FFFFFF'>Nazwa towaru/grupy</font></td>");
  towary.document.writeln("        <td width=1 ></td>");
  towary.document.writeln("        <td  align=center><font color='#FFFFFF'>&nbsp;Ilość&nbsp;</font></td>");
  towary.document.writeln("        <td width=1  align=right></td>");
//  towary.document.writeln("        <td  align=center>netto</td>");
//  towary.document.writeln("        <td width=1  valign=bottom></td>");
  towary.document.writeln("        <td  align=center><font color='#FFFFFF'>Cena z VAT</font></td>");
  towary.document.writeln("        <td width=1  valign=bottom></td>");
  towary.document.writeln("        <td><font color='#FFFFFF'>Zmień</font></td>");
  towary.document.writeln("</tr>");  
}

function w_pozycja(i) {
  //kolor tła wiersza
  kolor = (i%2) ? "#53C7FF" : "#99CCFF";


  towary.document.writeln("<tr>");
  towary.document.writeln("        <td width=75% bgcolor=" + kolor + " >" + koszyk[i].nazwa + "</td>");
  towary.document.writeln("        <td bgcolor=" + kolor + "></td>");
  towary.document.writeln("        <td bgcolor=" + kolor + " align=center >" + koszyk[i].ilosc + "</td>");
  towary.document.writeln("        <td bgcolor=" + kolor + " align=right></td>");
//  towary.document.writeln("        <td bgcolor=" + kolor + "  align=center>" + formatc(koszyk[i].ilosc) + "</td>");
//  towary.document.writeln("        <td bgcolor=" + kolor + " align=right></td>");
  towary.document.writeln("        <td bgcolor=" + kolor + "  align=center>" + formatc((koszyk[i].ilosc*koszyk[i].cb)) + "</td>");
  towary.document.writeln("        <td bgcolor=" + kolor + "></td>");
  towary.document.writeln("        <td bgcolor=" + kolor + "><a href=\"javascript:parent.zkoszykadokoszyka('"+koszyk[i].id+"')\"><IMG SRC=obrazki/kosz.gif WIDTH=36 HEIGHT=30 BORDER=0 alt='Zmień Zawartość koszyka'></a></td>");
  towary.document.writeln("</tr>");
  towary.document.writeln("<tr>");
  towary.document.writeln("<td colspan=7 height=1 </td>");
  towary.document.writeln("</tr>");

  towary.document.writeln("<input type=hidden name="+koszyk[i].id+" value=\""+koszyk[i].cn+":"+koszyk[i].nazwa+":"+koszyk[i].ilosc+"\">");

}

function w_listastop() {

  towary.document.writeln("<tr>");
  towary.document.writeln("    <td bgColor='#000080' width=75% colspan=3>");
  towary.document.writeln("      <p align=right><font color='#FFFFFF'>RAZEM :</font></td>");
  towary.document.writeln("    <center>");
  towary.document.writeln("    <td align='right' bgColor='#000080'>&nbsp;</td>");
  towary.document.writeln("    <td align='middle' bgColor='#000080'><font color='#FFFFFF'>" + formatc(netto) + "</font></td>");
  towary.document.writeln("    <td bgColor='#000080'>&nbsp;</td>");
  towary.document.writeln("    <td bgColor='#000080'>&nbsp;</td>");
  towary.document.writeln("    </tr>");


  towary.document.writeln("</table><p>");
  
  towary.document.writeln("<table width=700 cellspacing=0 cellpadding=0 border=0>");
  towary.document.writeln("<tr>");
  towary.document.writeln("        <td  height=30 colspan=5>Formularz zamówienia</td>");
  towary.document.writeln("</tr>");
  towary.document.writeln("<tr>");
  towary.document.writeln("        <td width=60%  align=right>Nazwisko osoby skladającej zamówienie, nazwa firmy:&nbsp;&nbsp;</td>");
  towary.document.writeln("        <td width=40%  ><input type=text name='nazwa_klienta' size=25></td>        ");
  towary.document.writeln("</tr>");

  towary.document.writeln("<tr>");
  towary.document.writeln("        <td align=right> telefon kontaktowy, do potwierdzenia zamówienia:&nbsp;&nbsp;<br> np. 071 3366252&nbsp;&nbsp;</td>");
  towary.document.writeln("        <td><input type=text name='telefon' size=25></td>        ");
  towary.document.writeln("</tr>");

  towary.document.writeln("<tr><td colspan='6'>Adres na który będzie wysłana przesyłka:</td></tr>");

  towary.document.writeln("<tr>");
  towary.document.writeln("        <td align=right> ulica:&nbsp;&nbsp;</td>");
  towary.document.writeln("        <td><input type=text name='ulica' size=25></td>        ");
  towary.document.writeln("</tr>");

  towary.document.writeln("<tr>");
  towary.document.writeln("        <td align=right> miasto i kod:&nbsp;&nbsp;</td>");
  towary.document.writeln("        <td><input type=text name='miasto' size=25></td>        ");
  towary.document.writeln("</tr>");

  towary.document.writeln("<tr>");
  towary.document.writeln("        <td align=right> NIP:&nbsp;&nbsp;</td>");
  towary.document.writeln("        <td><input type=text name='nip' size=25></td>        ");
  towary.document.writeln("</tr>");

  towary.document.writeln("<tr>");
  towary.document.writeln("        <td align=right><select onChange='transp.value=this.options[this.selectedIndex].value' name='transport' size='1'>");
  towary.document.writeln("  <option selected>Wybierz sposób dostarczenia zamówienia</option>");
  towary.document.writeln("  <option value='gratis'>Wrocław - transport gratis</option>");
  towary.document.writeln("  <option value='10 zł'> okolice Wrocławia - do 20 km</option>");
  towary.document.writeln("  <option value='35 zł'> Firma przewozowa - powyżej 20 km</option></select>&nbsp;&nbsp;</td>");
  towary.document.writeln("        <td><input disabled type='text' name='transp' size=25></td>");
  towary.document.writeln("</tr>");

  towary.document.writeln("<tr>");
  towary.document.writeln("        <td colspan=5 height=1 ></td>");
  towary.document.writeln("</tr>");
  towary.document.writeln("<tr>");
  towary.document.writeln("                <td><br>Zapoznałem się z treścią regulaminu i w pełni go akceptuę.</td>");
  towary.document.writeln("                <td><br><input type=checkbox name=Upowazniam value='Akceptuje'>&nbsp;&nbsp;Akceptuje</td>");
  towary.document.writeln("</tr>");
  towary.document.writeln("<tr>");
  towary.document.writeln("        <td colspan=5 height=1 ></td>");
  towary.document.writeln("</tr>");
  towary.document.writeln("<tr>");
  towary.document.writeln("        <td  height=30 colspan=5>                ");
  towary.document.writeln("        </td>");
  towary.document.writeln("</tr>");
  towary.document.writeln("<tr>");
  towary.document.writeln("        <td width=60%  >Dodatkowe uwagi:</td>");
  towary.document.writeln("        <td width=40%  ><textarea name=uwagi wrap=no rows=3 cols=25></textarea></td>        ");
  towary.document.writeln("</tr>");
  towary.document.writeln("<tr>");
  towary.document.writeln("        <td colspan=5 height=1 ></td>");
  towary.document.writeln("</tr>");
  towary.document.writeln("<tr>");
  towary.document.writeln("        <td colspan=2 align=center><input type=submit value=' Zamawiam '></td>");
  towary.document.writeln("</tr>");
  towary.document.writeln("</table><br>");  
  
  towary.document.writeln("</form>");
  towary.document.writeln("</center>");

  towary.document.writeln("<br><table cellspacing='0' cellpadding='0' border='0' align='center' width='100%'");
towary.document.writeln("style='font-weight: bold'>");
towary.document.writeln("  <tr>");
towary.document.writeln("    <td bgcolor='#000000'><img src width='1' height='1'></td>");
towary.document.writeln("  </tr>");
towary.document.writeln("  <tr>");
towary.document.writeln("    <td width='100%' valign='middle' bgcolor='#F5E8B4'><p align='center'><small>");
towary.document.writeln("    <font face='Arial'>projekt i wykonanie XenoComp 2002 </font></small></td>");
towary.document.writeln("  </tr>");
towary.document.writeln("</table>");
}

function w_pustykoszyk() {
  towary.document.writeln("<center>");
  towary.document.writeln("<table cellspacing=0 cellpadding=0 border=0>");
  towary.document.writeln("<tr><td height=20></td></tr>");
  towary.document.writeln("<tr align='center'>");
  towary.document.writeln("	<td width=83><IMG SRC=obrazki/pusty_koszyk.gif WIDTH=73 HEIGHT=69></td>");
  towary.document.writeln("</tr>");
  towary.document.writeln("<tr align='center'>");
  towary.document.writeln("<td><big><big><b>Koszyk jest pusty</b></big></big><br><br><a href='sklep.php'><font face='Arial'><b>Powrót do sklepu</b></font></a></p></td>");
  towary.document.writeln("</tr>");
  towary.document.writeln("</table>");
  towary.document.writeln("</center>");  
}

function w_koszykstop() {
  towary.document.writeln("</body>");
  towary.document.writeln("</html>");  
  towary.document.close();
}

function w_razem() {
  parent.razem.document.open();
  
  parent.razem.document.writeln("<html>");
  parent.razem.document.writeln("<head>");
  parent.razem.document.writeln("<meta name=Language content=pl>");
  parent.razem.document.writeln("<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-2\">");

  parent.razem.document.writeln("<title>Koszyk</title>");

  parent.razem.document.writeln("<body bgcolor=#D5EAFF background='bann1.gif' leftmargin=0 link=#D5EAFF vlink=#FFFF00 alink=#00FF00>");


  parent.razem.document.writeln("        <table width=214 height=50 cellspacing=0 cellpadding=0 border=0>");
  parent.razem.document.writeln("        <tr>");
  parent.razem.document.writeln("                <td align='center' valign='middle' width='150'><font face='Arial' size='2'><a href='javascript:parent.pokazkoszyk()'><b>Zawartość koszyka</b></a></font></td>");

  parent.razem.document.writeln("                <td align='center' valign='middle' rowspan='2'><font face='Arial'><a href='javascript:parent.pokazkoszyk()'>");


  if (netto <= 0) parent.razem.document.writeln("<img SRC='obrazki/pusty_koszyk.gif'");
  else  parent.razem.document.writeln("<img SRC='obrazki/kosz.gif'");

parent.razem.document.writeln("BORDER=0 ALT='Zawartość koszyka' width=36 height=30></a></font></td>");


  parent.razem.document.writeln("        </tr>");
  parent.razem.document.writeln("        <tr>");
  parent.razem.document.writeln("       	<td  align=center width=150><font face='Arial' size='2' color='#FFFFFF'>" + formatc(netto) + " zł</font></td>");

  if (netto >= 60000000) parent.razem.document.writeln("Transport na nasz koszt");
  if (netto >= 500000000) parent.razem.document.writeln(". RABAT 10%");
  else if (netto >= 300000000) parent.razem.document.writeln(". RABAT 5%");
  parent.razem.document.writeln("</td>        ");
  parent.razem.document.writeln("        </tr>");
  parent.razem.document.writeln("        </table>");
  parent.razem.document.writeln("</body>");
  parent.razem.document.writeln("</html>");  

  parent.razem.document.close();
}
