// from http://www.quirksmode.org/viewport/compatibility.html
function getWindowWidth() {
  if (self.innerWidth)  // all except Explorer
      return self.innerWidth;
  else if (document.documentElement && document.documentElement.clientWidth)  // Explorer 6 Strict Mode
      return document.documentElement.clientWidth;
  else if (document.body)  // other Explorers
      return document.body.clientWidth;
}
function getWindowHeight() {
  if (self.innerHeight)  // all except Explorer
      return self.innerHeight;
  else if (document.documentElement && document.documentElement.clientHeight)  // Explorer 6 Strict Mode
      return document.documentElement.clientHeight;
  else if (document.body)  // other Explorers
      return document.body.clientHeight;
}

function getScrollTop() {
if (self.pageYOffset)  // all except Explorer
    return self.pageYOffset;
else if (document.documentElement && document.documentElement.scrollTop)  // Explorer 6 Strict
    return document.documentElement.scrollTop;
else if (document.body)  // all other Explorers
    return document.body.scrollTop;
}



// Setze SideBar Hintergrundfarbe OnMouse
function SideBarBgColor(element, style, focused) {
  if ( focused ) {
    element.style.backgroundColor = '#FFFFFF';
    element.style.border = '1px solid #C0C0C0';
  } else {
    element.style.backgroundColor = '#F0F0F0';
    element.style.border = '1px solid #F0F0F0';
  }
}
// Setze Farbe z.B. bei Buttons
function SetElementColor(id, color) {
  var elem = document.getElementById(id);
  if (elem)
    elem.style.color = color;
};

// Setze Hintergrund-Farbe z.B. bei Buttons
function SetElementBackColor(id, color) {
  var elem = document.getElementById(id);
  if (elem)
    elem.style.backgroundColor = color;
};

// Schaltet Sichtbarkeit des Elements um
function ToggleVisible(id) {
  var elem = document.getElementById(id);
  elem.style.display = (elem.style.display == "block") ? "none" : "block";
};

// Schaltet Sichtbarkeit des Elements um
function Show(id) {
  var elem = document.getElementById(id);
  elem.style.display = "block";
};

// Schaltet Sichtbarkeit des Elements um
function Hide(id) {
  var elem = document.getElementById(id);
  elem.style.display = "none";
};

// Forward to Url after short delay
function handleClick(Url) {
  setTimeout('window.location.href = "' + Url + '"', 1000);
}
// Start download and forward to Url after short delay
function handleClick2(Download, Url) {
  location.href = Download;
  handleClick(Url);
}

// Preload images
document.PreLoadedImages = new Array();
function preloadImage(im) {
  var i = new Image();
  i.src = im;
  return i;
};
function preloadButtonImage(path, id, state) {
  document.PreLoadedImages[id][state] = preloadImage(path + '/' + id + '_' + state + '.gif');
};
function preloadButtonImages(path, id) {
  document.PreLoadedImages[id] = new Array();
  preloadButtonImage(path, id, 0);
  preloadButtonImage(path, id, 1);
  preloadButtonImage(path, id, 2);
};
function xpe(id, state) {
  var elem = document.getElementById(id);
  if (elem)
    elem.src = document.PreLoadedImages[id][state].src;
  return false;
};

// Order
function GetLicPrice(block_id, product_id, qty) {
  switch (block_id + '_' + product_id) {
    case 'pa_rg_lc':
      if (qty < 5)
        return 69.95;
      if (qty < 10)
        return 59.95;
      if (qty < 25)
        return 54.95;
      if (qty < 50)
        return 49.95;
      if (qty < 100)
        return 39.95;
      if (qty < 200)
        return 34.95;
      if (qty < 500)
        return 29.95;
      if (qty < 1000)
        return 24.95;
      else
        return 19.95;
    case 'pa_rg_up_lc':
      if (qty < 5)
        return 34.95;
      if (qty < 10)
        return 29.95;
      if (qty < 25)
        return 27.95;
      if (qty < 50)
        return 25.95;
      if (qty < 100)
        return 19.95;
      if (qty < 200)
        return 17.95;
      if (qty < 500)
        return 14.95;
      if (qty < 1000)
        return 12.95;
      else
        return  9.95;
    case 'pa_rg_2y':
    case 'pa_rg_up_2y':
    case 'pa_rg_rn_lc':
      if (qty < 5)
        return 29.95;
      if (qty < 10)
        return 25.95;
      if (qty < 25)
        return 23.95;
      if (qty < 50)
        return 21.95;
      if (qty < 100)
        return 16.95;
      if (qty < 200)
        return 14.95;
      if (qty < 500)
        return 12.95;
      if (qty < 1000)
        return 10.95;
      else
        return  8.95;
    case 'grahl_cd':
      return 12.95;
    case 'pa_st_lc':
      if (qty < 5)
        return 29.90;
      if (qty < 10)
        return 24.90;
      if (qty < 25)
        return 22.90;
      if (qty < 50)
        return 19.90;
      if (qty < 100)
        return 14.90;
      if (qty < 200)
        return  9.99;
      if (qty < 500)
        return  4.99;
      if (qty < 1000)
        return  2.99;
      else
        return  1.99;
    case 'pa_st_up_lc':
      if (qty < 5)
        return 19.90;
      if (qty < 10)
        return 16.90;
      if (qty < 25)
        return 14.90;
      if (qty < 50)
        return 12.90;
      if (qty < 100)
        return  9.90;
      if (qty < 200)
        return  4.99;
      if (qty < 500)
        return  2.99;
      if (qty < 1000)
        return  1.59;
      else
        return  0.99;
    case 'pa_st_2y':
    case 'pa_st_up_2y':
    case 'pa_st_rn_lc':
      if (qty < 5)
        return 14.90;
      if (qty < 10)
        return 12.90;
      if (qty < 25)
        return 10.90;
      if (qty < 50)
        return  8.90;
      if (qty < 100)
        return  6.90;
      if (qty < 200)
        return  3.90;
      if (qty < 500)
        return  1.90;
      if (qty < 1000)
        return  1.20;
      else
        return  0.75;
  }
}

function FormatPrice(price, decimal_separator) {
  value = price;
//  value = parseInt(value * 100) / 100;
  value = value.toFixed(2);
  value = value.replace(/\./, decimal_separator);
  while (value.match(/^(\d+)(\d{3}\b)/)) {
      value = value.replace(/^(\d+)(\d{3}\b)/, RegExp.$1 + ('.' == decimal_separator ? ',' : '.') + RegExp.$2);
  }
  return value.toString();
}

function OrderRecalc(block_id, decimal_separator) {
  var elemQty = document.getElementById(block_id + '_lc_Qty');
  var elemLicPrice = document.getElementById(block_id + '_lc_Price');
  var elem2Y = document.getElementById(block_id + '_2y_Check');
  var elemCD = document.getElementById(block_id + '_cd_Check');
  var elem2YPrice = document.getElementById(block_id + '_2y_Price');
  var elemTotal = document.getElementById(block_id + '_Total');
  var elemTotal2 = document.getElementById(block_id + '_Total2');

  qty = (elemQty ? elemQty.value : 0);

  // Einzelpreis
  price = GetLicPrice(block_id, 'lc', qty);
  if (elemLicPrice)
    elemLicPrice.innerHTML = FormatPrice(price, decimal_separator);
  // Wartungspreis
  price = GetLicPrice(block_id, '2y', qty);
  if (elem2YPrice)
    elem2YPrice.innerHTML = FormatPrice(price, decimal_separator);

  // Total
  price = GetLicPrice(block_id, 'lc', qty) * qty;
  if (elem2Y && elem2Y.checked)
    price += GetLicPrice(block_id, '2y', qty) * qty;
  if (elemCD && elemCD.checked)
    price += GetLicPrice('grahl', 'cd', qty);
  if (elemTotal)
    elemTotal.innerHTML = FormatPrice(price, decimal_separator);
  if (elemTotal2)
    elemTotal2.innerHTML = FormatPrice(price-20, decimal_separator);
}

function OrderRecalc_BAK(block_id, decimal_separator) {
  var elemQty = document.getElementById(block_id + '_lc_Qty');
  if (!elemQty) return;
  var elemLicPrice = document.getElementById(block_id + '_lc_Price');
  if (!elemLicPrice) return;
  var elem2Y = document.getElementById(block_id + '_2y_Check');
  if (!elem2Y) return;
  var elemCD = document.getElementById(block_id + '_cd_Check');
  if (!elemCD) return;
  var elem2YPrice = document.getElementById(block_id + '_2y_Price');
  if (!elem2YPrice) return;
  var elemTotal = document.getElementById(block_id + '_Total');
  if (!elemTotal) return;

  // Einzelpreis
  price = GetLicPrice(block_id, 'lc', elemQty.value);
  elemLicPrice.innerHTML = FormatPrice(price, decimal_separator);
  // Wartungspreis
  price = GetLicPrice(block_id, '2y', elemQty.value);
  elem2YPrice.innerHTML = FormatPrice(price, decimal_separator);

  // Total
  price = GetLicPrice(block_id, 'lc', elemQty.value) * elemQty.value;
  if (elem2Y.checked)
    price += GetLicPrice(block_id, '2y', elemQty.value) * elemQty.value;
  if (elemCD.checked)
    price += GetLicPrice('grahl', 'cd', elemQty.value);
  elemTotal.innerHTML = FormatPrice(price, decimal_separator);
}