/*
  $Id: $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2007 osCommerce

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License v2 (1991)
  as published by the Free Software Foundation.
*/

function refreshVariants() {
  var price = null;
  var availability = null;
  var model = null;

if(typeof(combos) != "undefined") {
  for (c in combos) {
    id = null;

    variants_loop:
    for (group_id in combos[c]['values']) {
      for (value_id in combos[c]['values'][group_id]) {
        if (document.getElementById('variants_' + group_id) != undefined) {
          if (document.getElementById('variants_' + group_id).type == 'select-one') {
            if (value_id == document.getElementById('variants_' + group_id).value) {
              id = c;
            } else {
              id = null;

              break variants_loop;
            }
          }
        } else if (document.getElementById('variants_' + group_id + '_1') != undefined) {
          j = 0;

          while (true) {
            j++;

            if (document.getElementById('variants_' + group_id + '_' + j).type == 'radio') {
              if (document.getElementById('variants_' + group_id + '_' + j).checked) {
                if (value_id == document.getElementById('variants_' + group_id + '_' + j).value) {
                  id = c;
                } else {
                  id = null;

                  break variants_loop;
                }
              }
            }

            if (document.getElementById('variants_' + group_id + '_' + (j+1)) == undefined) {
              break;
            }
          }
        }
      }
    }

    if (id != null) {
      break;
    }
  }
}
  if ((typeof(id) != "undefined") && (id != null)) {
    price = combos[id]['price'];
    availability = productInfoAvailability;
    model = combos[id]['model'];
  } else {
    price = originalPrice;
    availability = productInfoNotAvailable;
    model = '';
  }
  if(document.getElementById('productInfoPrice')) {
    document.getElementById('productInfoPrice').innerHTML = price;
  }
  if(document.getElementById('productInfoAvailability')) {
    document.getElementById('productInfoAvailability').innerHTML = availability;
  }
  if(document.getElementById('productInfoModel')) {
    document.getElementById('productInfoModel').innerHTML = model;
  }
}

function inneWarianty() {
  if(document.forms["s"]) {
    var f = document.forms["s"];
    if(f.elements["moc"]) {
      var moc=f.elements["moc"].value;
    }
    if(f.elements["wymiary"]) {
      var wymiary=f.elements["wymiary"].value;
    }
    if(f.elements["powloka"]) {
      var powloka=f.elements["powloka"].value;
    }
    if(f.elements["podlaczenia"]) {
      var podlaczenia=f.elements["podlaczenia"].value;
    }
    if(typeof(Prod) == "object") {
      var wynik=0;
      for(i in Prod) {
        var OKmoc = 0;
        var OKwymiary = 0;
        var OKpowloka = 0;
        var OKpodlaczenia = 0;
        if(Prod[i].id && (parseInt(Prod[i].id) > 0)) {
          if(moc.length > 0) {
            var moc2 = moc.split("-");
            if((moc2[0] <= Prod[i].moc) && (moc2[1] >= Prod[i].moc)) {
              OKmoc = 1;
            }
          }
          else {
            OKmoc = 1;
          }
          if(wymiary.length > 0) {
            if(wymiary == Prod[i].wymiary) {
              OKwymiary = 1;
            }
          }
          else {
            OKwymiary = 1;
          }
          if(powloka.length > 0) {
            if(powloka == Prod[i].powloka) {
              OKpowloka = 1;
            }
          }
          else {
            OKpowloka = 1;
          }
          if(podlaczenia.length > 0) {
            if(typeof(Prod[i].podlaczenia) == "object") {
              for(j in Prod[i].podlaczenia) {
                if(podlaczenia == Prod[i].podlaczenia[j]) {
                  OKpodlaczenia = 1;
                }
              }
            }
            else {
              OKpodlaczenia = 1;
            }
          }
          else {
            OKpodlaczenia = 1;
          }
          if((OKpodlaczenia == 1) && (OKpowloka == 1) && (OKwymiary == 1) && (OKmoc == 1)) {
            var display = "table-row";
            wynik++;
          }
          else {
            var display = "none";
          }
          if(document.getElementById("prod_"+Prod[i].id)) {
            if(wynik%2) {
              document.getElementById("prod_"+Prod[i].id).className="odd";
            }
            else {
              document.getElementById("prod_"+Prod[i].id).className="even";
            }
            document.getElementById("prod_"+Prod[i].id).style.display=display;
          }
        } // if prod.id
      } // for i in prod
      if(document.getElementById("prodBrak")) {
        if(wynik == 0) {
          document.getElementById("prodBrak").style.display="table-cell";
          document.getElementById("prodBrak").innerHTML="<span style=\"color:red;font-weight:bold;\">Nie znaleziono produktów spełniających kryteria wyszukiwania</span>";
        }
        else {
          document.getElementById("prodBrak").style.display="none";
          document.getElementById("prodBrak").innerHTML="";
        }
      }
    } // if prod
  } // if form
}

function setColor(ipc) {
  if($('#C'+ipc)) {
    $('#C'+ipc).css('border','1px solid orange');
    $('#C'+ipc).css('backgroundColor','#FFFFCC');
    $('input[name="selectedColor"]').val(ipc);
    document.forms['cart_quantity'].elements['variants[4][1]'].value=ipc;
    if(Pcolors && Pcolors.length) {
      for(i in Pcolors) {
        if(Pcolors[i] && Pcolors[i].ipc) {
          if(Pcolors[i].ipc != ipc) {
            if($('#C'+Pcolors[i].ipc)) {
              $('#C'+Pcolors[i].ipc).css('border','1px solid #ccc');
              $('#C'+Pcolors[i].ipc).css('backgroundColor','#fff');
            }
          }
        }
      }
    }
  }
}

function add2cart(products_id,Val) {
if(parseInt(products_id) <= 0) {
              $.pnotify({pnotify_title: 'Produkt nie został dodany',pnotify_text: 'Nie przesłano identyfikatora produktu.',pnotify_type: 'error',pnotify_error_icon: 'ui-icon ui-icon-cart'});
              $('.ui-pnotify-container').corner('5px'); 
              return false;
}
  $('#msgBox').empty(); // clear previous content

if((typeof(Val) == "object")  && (Val.max >0)) {
  $('#msgBox').append('<strong>Produkt wieloelementowy</strong><br />Określ z ilu elementów ma składać się docelowy, zamawiany produkt.<br />(minimalna ilość elementów: '+parseInt(Val.min)+', maksymalna ilość: '+parseInt(Val.max)+', domyślna ilość: '+Val.def+')'); 
  $('#msgBox').append('<br /><br />Ilość elementów: <input type="text" name="ilosc_elementow" value="'+Val.def+'" /><br /><br />');
}
else {
  $('#msgBox').append('<input type="hidden" name="ilosc_elementow" value="0" />');
}
  $('#msgBox').append('<strong>Wybierz kolor produktu</strong><br />Standardowy kolor: C35 / White Silk'); 
  if(Pcolors && Pcolors.length) {
    var colorsList = '<ul id="ListaKolorow">';
    for(i in Pcolors) {
       if(Pcolors[i] && Pcolors[i].ipc) {
         colorsList += '<li id="C'+Pcolors[i].ipc+'" onclick="setColor('+Pcolors[i].ipc+');return false;"><span class="color" style="background-color:#'+Pcolors[i].rgb+';"></span><span class="desc">C'+Pcolors[i].ipc+' / '+Pcolors[i].nazwa+'</span></li>';
       }
    }
    colorsList += '</ul>';
  }
  $('#msgBox').append('<br /><br />'+colorsList+'<input type="hidden" name="selectedColor" value="35" />');

  $('#msgBox').dialog({title:"Uzupełnij informacje dot. zamawianego produktu",buttons:[{text: "Do koszyka",click: function() {
          var selVal= $('input[name="ilosc_elementow"]').val();
          var selCol= $('input[name="selectedColor"]').val();
          if((selCol.length > 0) && (parseInt(selCol) != 35)) {
            var col = "&color="+selCol;
          }
          else {
            var col = "";
          }
          var el ="";
          if((typeof(Val) == "object")  && (Val.max >0)) {
	    if((parseInt(selVal) >= Val.min) && (parseInt(selVal) <= Val.max)) {
               var el="&elements="+parseInt(selVal);
	    }
	    else {
              $.pnotify({pnotify_title: 'Produkt nie został dodany',pnotify_text: 'ilość elementów, z których składa się wybrany produkt musi zawierać się w przedziale od '+Val.min+' do '+Val.max,pnotify_type: 'error',pnotify_error_icon: 'ui-icon ui-icon-cart'});
              $('.ui-pnotify-container').corner('5px'); 
              return false;
	    }
          }
    $.getJSON("/ext/ajax.php?a=add2cart&products_id="+products_id+el+col,
        function(data){
          if(data.status == "OK") {
            if(data.produkt) {
              var produkt = "<b>"+data.produkt+"</b>";
            }
            else {
              var produkt = "Wybrany produkt";
            }
            $.pnotify({pnotify_title: 'Dodano do koszyka',pnotify_text: produkt+' został dodany do koszyka',pnotify_notice_icon: 'ui-icon ui-icon-cart'});
//            $('.ui-pnotify-container').corner('5px');
            $("#koszykContent").fadeOut("fast", function(){
              $("#koszykContent").html(data.cart);
              $("#koszykContent").fadeIn("slow");
            });
//            updateCart(data.cart);
            return true;
          }
          else {
//            return false;
          }
        });
   $(this).dialog("close"); 
  }},{text: "Wstecz",click: function() { $(this).dialog("close"); }}]});
  return true;
}

function add2cartSkip(products_id) {
    $.getJSON("/ext/ajax.php?a=add2cart&products_id="+products_id,
        function(data){
          if(data.status == "OK") {
            if(data.produkt) {
              var produkt = "<b>"+data.produkt+"</b>";
            }
            else {
              var produkt = "Wybrany produkt";
            }
            $.pnotify({pnotify_title: 'Dodano do koszyka',pnotify_text: produkt+' został dodany do koszyka',pnotify_notice_icon: 'ui-icon ui-icon-cart'});
//            $('.ui-pnotify-container').corner('5px');
            $("#koszykContent").fadeOut("fast", function(){
              $("#koszykContent").html(data.cart);
              $("#koszykContent").fadeIn("slow");
            });
//            updateCart(data.cart);
            return true;
          }
          else {
//            return false;
          }
        });
}
