function fnChangeCurrency(currencyID) {
  $.ajax({
    type: 'GET',
    url: '/functions.php?structureID=88',
    data: 'currencyID='+currencyID,
    success: function(msg){
      window.location.reload();
    }
  });
}


function fnSetOrderData(param, value, reload) {
  $.ajax({
    type: 'GET',
    url: '/functions.php?structureID=72',
    data: param + '=' + value,
    success: function(msg){
      if (reload) {
        window.location.reload();
      }
    }
  });
}


function fnAddOrderRow(item_id, quantity, add, popup) {
  $('#shopping_cart').css("display", "none");
  $('#loader').css("display", "block");
  $.ajax({
    type: 'POST',
    url: '/functions.php?structureID=73',
    data: 'item_id=' + item_id + '&quantity=' + quantity + '&modify=' + add,
    success: function(msg){
      $('#loader').css("display", "none");
      $('#shopping_cart').css("display", "block");
      var jscomp = eval( "(" + msg + ")" );
      if ($('#cart_row_amount_'+item_id) && $('#cart_row_quantity_'+item_id)) {
        if (jscomp.row_amount <= 0) {
          window.location.reload();
        }
        $('#cart_row_amount_'+item_id).html(jscomp.row_amount + '<span>' + jscomp.row_vat + '</span>');
        $('#cart_row_quantity_'+item_id).val(jscomp.row_quantity);
      }
      for (var i in jscomp.update_items) {
        if ($('#cart_row_amount_'+jscomp.update_items[i].item_id) && $('#cart_row_quantity_'+jscomp.update_items[i].item_id)) {
          $('#cart_row_amount_'+jscomp.update_items[i].item_id).html(+jscomp.update_items[i].row_amount + '<span>' + +jscomp.update_items[i].row_vat + '</span>');
        }
      }
      if ($('#order_row_amount['+item_id+']') && $('#order_row_quantity['+item_id+']')) {
        $('#order_row_amount['+item_id+']').html(jscomp.row_amount + '<span>' + jscomp.row_vat + '</span>');
        $('#order_row_quantity['+item_id+']').val(jscomp.row_quantity);
      }
      if ($('#cart_row_quantity_MnM')) {
        $('#cart_row_quantity_MnM').val(jscomp.row_quantity_MnM);
      }

      $('#trantsport_price').html(jscomp.trantsport.to_pay_wov + '<span>' + jscomp.trantsport.to_pay_vat + '</span>');
      $('#trantsport_name').html(jscomp.trantsport.name + '<span>' + jscomp.trantsport.barcode + '</span>');
      $('#trantsport_discount').html(jscomp.trantsport.discount);

      if ($('#total_price') && $('#total_amount')) {
        $('#total_price').html('<strong>' + jscomp.total_price + '</strong>');
        $('#total_amount').html('<strong>' + jscomp.total_amount + '</strong>');        
      }
      if ($('#total_brand_price')) {
        $('#total_brand_price').html('<strong>' + jscomp.total_brand_price + '<span>' + jscomp.total_brand_vat + '</span></strong>');
      }
      $('#cart_items').html(jscomp.total_quantity);
      $('#cart_amount').html(jscomp.total_amount);      
      if (popup) {
        $('#cart_popup').dialog('open');
      }
    }
  });
}


function fnCalculateOrderQty(item_id, max_qty, stock) {
  if (max_qty * 1 > stock * 1) {
    var order_qty = max_qty * 1 - stock * 1;
  } else {
    var order_qty = 0;
  }
  $('#order_row_quantity['+item_id+']').value = order_qty;
  fnAddOrderRow(item_id, order_qty);
}


function fnGetSubLocations(location_id, level, selected) {
  $.ajax({
    type: 'GET',
    url: '/functions.php?structureID=80',
    data: 'itemID=' + location_id,
    success: function(msg){
      if (document.getElementById('locations_'+level)) {
        var sublocation = document.getElementById('locations_'+level);
        while(sublocation.hasChildNodes()){
          sublocation.removeChild(sublocation.childNodes[0])
        }
        if (msg.length > 0) {
          var jscomp = eval( "(" + msg + ")" );
          for (i = 0; i < jscomp.length; i++) {
            if (jscomp[i].id == selected) {
              sublocation.options[i] = new Option( jscomp[i].name, jscomp[i].id, true, true);
            } else {
              sublocation.options[i] = new Option( jscomp[i].name, jscomp[i].id);
            }
          }
          level++;
          fnGetSubLocations(sublocation.value, level);
        }
      } else {
        level--;
        if (document.getElementById('trantsport') && document.getElementById('locations_'+level)) {
          fnGetTrantsport(document.getElementById('locations_'+level).value, document.getElementById('trantsport').value);        
        }
      }
    }
  });
}


function fnGetParentLocation(location_id, level) {
  $.ajax({
    type: 'GET',
    url: '/functions.php?structureID=123',
    data: 'itemID=' + location_id,
    success: function(msg){
      if (document.getElementById('locations_'+level)) {
        var locations = document.getElementById('locations_'+level);
        while(locations.hasChildNodes()){
          locations.removeChild(locations.childNodes[0]);
        }
        if (msg.length > 0) {
          var jscomp = eval( "(" + msg + ")" );
          for (i = 0; i < jscomp.length; i++) {
            locations.options[i] = new Option( jscomp[i].name, jscomp[i].id, jscomp[i].selected, jscomp[i].selected );
          }
          if (jscomp[0].parent_id > 0) {
            level--;
            fnGetParentLocation(jscomp[0].parent_id, level);
          }
        }
      }
    }
  });
}


function fnGetLocation(path_id, select_id, name, location_id, lang_id, readonly) {
  $.ajax({
    type: 'GET',
    url: '/functions.php?structureID=624',
    data: 'itemID=' + location_id + '&langID=' + lang_id + '&selectID=' + select_id + '&pathID=' + path_id + '&name=' + name + '&readonly=' + readonly,
    success: function(msg){
      var jscomp = eval( "(" + msg + ")" );
      $('#' + path_id).html(jscomp.path);
      $('#' + select_id).html(jscomp.select);
      if (document.getElementById('trantsport')) {
        if (jscomp.id > 0) {
          fnGetTrantsport(jscomp.id, document.getElementById('trantsport').value);
        } else {
          var trantsport = document.getElementById('trantsport');
          while(trantsport.hasChildNodes()) {
            trantsport.removeChild(trantsport.childNodes[0])
          }
          trantsport.options[0] = new Option( jscomp.text );
        }
      }
    }
  });
}


function fnGetTrantsport(location_id, trantsport_id) {
  $.ajax({
    type: 'GET',
    url: '/functions.php?structureID=228',
    data: 'itemID=' + location_id,
    success: function(msg){
      var trantsport = document.getElementById('trantsport');
      while(trantsport.hasChildNodes()) {
        trantsport.removeChild(trantsport.childNodes[0])
      }
      if (msg.length > 0) {
        var jscomp = eval( "(" + msg + ")" );
        for (i = 0; i < jscomp.length; i++) {
          if (jscomp[i].var_id == trantsport_id) {
            trantsport.options[i] = new Option( jscomp[i].name, jscomp[i].var_id, true, true );
          } else {
            trantsport.options[i] = new Option( jscomp[i].name, jscomp[i].var_id );
          }
        }
        fnGetTrantsportPrice(document.getElementById('trantsport').value);
      }
    }
  });
}


function fnGetTrantsportPrice(trantsport_id) {
  $('#shopping_cart').css("display", "none");
  $('#loader').css("display", "block");
  $.ajax({
    type: 'GET',
    url: '/functions.php?structureID=2155',
    data: 'itemID=' + trantsport_id,
    success: function(msg){
      $('#loader').css("display", "none");
      $('#shopping_cart').css("display", "block");
      if (msg.length > 0) {
        var jscomp = eval( "(" + msg + ")" );
        $('#trantsport_price').html(jscomp.trantsport.to_pay_wov + '<span>' + jscomp.trantsport.to_pay_vat + '</span>');
        $('#trantsport_name').html(jscomp.trantsport.name + '<span>' + jscomp.trantsport.barcode + '</span>');
        $('#trantsport_discount').html(jscomp.trantsport.discount);
        if (trantsport_id) {
          $('#trantsport_line').css("display", "block");
        } else {
          $('#trantsport_line').css("display", "none");
        }

        if ($('#total_price') && $('#total_amount')) {
          $('#total_price').html('<strong>' + jscomp.total_price + '</strong>');
          $('#total_amount').html('<strong>' + jscomp.total_amount + '</strong>');        
        }
        $('#cart_items').html(jscomp.total_quantity);
        $('#cart_amount').html(jscomp.total_amount);
      }
    }
  });
}


function fnAddFilterParam(cat_id) {
  if (!document.getElementById("brand_" + cat_id).checked) {
    document.getElementById("brand_" + cat_id).checked = true;
    document.getElementById("image_" + cat_id).src = "/gfx/iconCheckboxChecked.gif";
  } else {
    document.getElementById("brand_" + cat_id).checked = false;
    document.getElementById("image_" + cat_id).src = "/gfx/iconCheckbox.gif";      
  }
}


function fnSendInvoice(email) {
  $.ajax({
    type: 'GET',
    url: '/saada-ettemaksuarve/',
    data: 'email='+email,
    success: function(msg){
      var jscomp = eval( "(" + msg + ")" );
      $('#email_area').html(jscomp.msg);
    }
  });
}


function fnJoinNewsletter(email, langID) {
  $.ajax({
    type: 'GET',
    url: '/liitu-uudiskirjaga/',
    data: 'email=' + email + '&langID=' + langID,
    success: function(msg){
      var jscomp = eval( "(" + msg + ")" );
      fnOpenPopup(jscomp.msg, '', 240, 160);
    }
  });
}



function fnOpenPopup(msg, title, width, height) {
  $('#msg_area').html(msg);
  if (title) {
    $('#msg_popup').dialog('option', 'title', title);
  }
  if (width) {
    $('#msg_popup').dialog('option', 'width', width);
  }
  if (height) {
    $('#msg_popup').dialog('option', 'height', height);
  }
  $('#msg_popup').dialog('open');
}



function fnMakeAll(checked) {
  var checks = document.getElementsByTagName("input");
  for (i = 0; i < checks.length; i++) {
    if (checks[i].type == "checkbox") {
	    checks[i].checked = checked;
	  }
	}
}


function fnShowImage(image) {
  $('#img_popup').html("<center><img src='" + image + "' style='padding-top: 20px;' /></center>");
  $('#img_popup').dialog('open');
}


function selectLocation(id, name, select_id) {
  $.ajax({
    type: 'GET',
    dataType: 'json',
    url: '/functions.php?structureID=2122&parentID=' + id + '&prefix=' + encodeURI(name),
    success: function(obj){
      if (obj.locations != 1) {
        $('#' + select_id).multiselect('destroy');
        $('#' + select_id + ' > option').remove();
        if (id != 0) {
          $('#' + select_id).append('<option value="0">' + $('#cancel_text').attr('value') + '</option>');
        }
        $.each(obj.locations, function(key, item) {
          $('#' + select_id).append('<option value="' + item.id + '">' + obj.prefix + item.name + '</option>');
        });
        $('#' + select_id).multiselect({
          multiple: false,
//          header: (id > 0) ? '<a href="#" onclick="javascript: selectLocation(0, \'\')">Reset</a>' : false,
          header: false,
          selectedList: 1,
          minWidth: 300,
          noneSelectedText: obj.prefix,
          click: function(event, ui){
            if (ui.checked) {
              selectLocation(ui.value, ui.text, select_id);
            }
          }
        });
        $('#' + select_id).multiselect('open');
      } else {
        $('#' + select_id).multiselect('destroy');
        $('#' + select_id + ' > option').remove();
        $('#' + select_id).append('<option value="0">' + $('#cancel_text').attr('value') + '</option>');
        $('#' + select_id).append('<option value="' + id + '" selected="selected">' + name + '</option>');
        $('#' + select_id).multiselect({
          multiple: false,
          header: false,
          selectedList: 1,
          minWidth: 300,
          click: function(event, ui){
            if (ui.checked) {
              selectLocation(ui.value, ui.text, select_id);
            }
          },
        });
        $('#' + select_id).multiselect('close');
      }
      if (document.getElementById('trantsport')) {
        fnGetTrantsport(id);
      }
    }
  });
}


$(document).ready(function() {
  $('#cart_popup').dialog({
    autoOpen: false,
    height: 180,
    width: 360,
    modal: true
  });
  $('#img_popup').dialog({
    autoOpen: false,
    height: 500,
    width: 500,
    modal: true
  });
  $('#msg_popup').dialog({
    autoOpen: false,
    modal: true
  });
  $('#msg_popup').dialog({
    beforeClose: function(event, ui) {
      $('#msg_area').html('');    
    }
  });
  $('.advertSidebar').floatingbanner();

  $("#location, #del_location, #jur_location").multiselect({
    multiple: false,
    header: false,
    selectedList: 1,
    minWidth: 300,
    click: function(event, ui){
      if (ui.checked) {
        selectLocation(ui.value, ui.text, $(this).attr("id"));
      }
    },
    open: function(event, ui){
      if ($(this).children("option").size() == 1) {
        selectLocation(0, '', $(this).attr("id"));
      }
    }
  });
});
