// 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(e, Url) {
  //test for Opera/x.x or Opera x.x (ignoring remaining decimal places)
  if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
    // capture X.x portion and store as a number
    //var oprversion=new Number(RegExp.$1);
    return;
  }

  // Do not bubble up event
  if (!e) var e = window.event;
  e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();

  setTimeout('window.location.href = "' + Url + '"', 1000);
}
// Start download and forward to Url after short delay
function handleClick2(e, Download, Url) {
  location.href = Download;
  handleClick(e, 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

// Alle Preise USD/EUR, GBP, CAD
function GetLicPriceArr(block_id, product_id, qty) {
  switch (block_id + '_' + product_id) {
    case 'pa_rg_lc':
      if (qty < 5)
        return new Array(69.95, 59.95, 79.95);
      if (qty < 10)
        return new Array(59.95, 51.95, 69.95);
      if (qty < 25)
        return new Array(54.95, 47.95, 63.95);
      if (qty < 50)
        return new Array(49.95, 43.95, 57.95);
      if (qty < 100)
        return new Array(39.95, 34.95, 45.95);
      if (qty < 200)
        return new Array(34.95, 29.95, 40.95);
      if (qty < 500)
        return new Array(29.95, 25.95, 34.95);
      if (qty < 1000)
        return new Array(24.95, 21.95, 28.95);
      else
        return new Array(19.95, 16.95, 22.95);
    case 'pa_rg_up_lc':
      if (qty < 5)
        return new Array(34.95, 29.95, 39.95);
      if (qty < 10)
        return new Array(29.95, 25.95, 34.95);
      if (qty < 25)
        return new Array(27.95, 24.45, 32.95);
      if (qty < 50)
        return new Array(25.95, 22.95, 29.95);
      if (qty < 100)
        return new Array(19.95, 17.95, 22.95);
      if (qty < 200)
        return new Array(17.95, 15.45, 20.95);
      if (qty < 500)
        return new Array(14.95, 12.95, 17.95);
      if (qty < 1000)
        return new Array(12.95, 10.95, 14.95);
      else
        return new Array( 9.95,  8.95, 11.95);
    case 'pa_rg_2y':
    case 'pa_rg_up_2y':
    case 'pa_rg_rn_lc':
      if (qty < 5)
        return new Array(29.95, 25.95, 34.95);
      if (qty < 10)
        return new Array(25.95, 22.95, 29.95);
      if (qty < 25)
        return new Array(23.95, 20.95, 27.95);
      if (qty < 50)
        return new Array(21.95, 18.95, 25.95);
      if (qty < 100)
        return new Array(16.95, 14.95, 19.95);
      if (qty < 200)
        return new Array(14.95, 12.95, 17.95);
      if (qty < 500)
        return new Array(12.95, 10.95, 14.95);
      if (qty < 1000)
        return new Array(10.95,  9.95, 12.95);
      else
        return new Array( 8.95,  7.95, 10.95);
    case 'pa_st_lc':
      if (qty < 5)
        return new Array(29.90, 25.90, 34.90);
      if (qty < 10)
        return new Array(24.90, 21.90, 28.90);
      if (qty < 25)
        return new Array(22.90, 19.90, 26.90);
      if (qty < 50)
        return new Array(19.90, 16.90, 22.90);
      if (qty < 100)
        return new Array(14.90, 12.90, 17.90);
      if (qty < 200)
        return new Array( 9.99,  8.69, 11.49);
      if (qty < 500)
        return new Array( 4.99,  4.29,  5.79);
      if (qty < 1000)
        return new Array( 2.99,  2.59,  3.49);
      else
        return new Array( 1.99,  1.69,  2.29);
    case 'pa_st_up_lc':
      if (qty < 5)
        return new Array(19.90, 16.90, 22.90);
      if (qty < 10)
        return new Array(16.90, 14.90, 19.90);
      if (qty < 25)
        return new Array(14.90, 12.90, 17.90);
      if (qty < 50)
        return new Array(12.90, 10.90, 14.90);
      if (qty < 100)
        return new Array( 9.90,  8.59, 11.90);
      if (qty < 200)
        return new Array( 4.99,  4.39,  5.75);
      if (qty < 500)
        return new Array( 2.99,  2.59,  3.45);
      if (qty < 1000)
        return new Array( 1.59,  1.39,  1.85);
      else
        return new Array( 0.99,  0.85,  1.15);
    case 'pa_st_2y':
    case 'pa_st_up_2y':
    case 'pa_st_rn_lc':
      if (qty < 5)
        return new Array(14.90, 12.90, 17.90);
      if (qty < 10)
        return new Array(12.90, 10.90, 14.90);
      if (qty < 25)
        return new Array(10.90,  9.90, 12.90);
      if (qty < 50)
        return new Array( 8.90,  7.90, 10.90);
      if (qty < 100)
        return new Array( 6.90,  5.99,  7.90);
      if (qty < 200)
        return new Array( 3.90,  3.39,  4.50);
      if (qty < 500)
        return new Array( 1.90,  1.69,  2.20);
      if (qty < 1000)
        return new Array( 1.20,  0.99,  1.39);
      else
        return new Array( 0.75,  0.59,  0.89);
    case 'ac_rg_lc':
        return new Array(19.95, 19.95, 19.95);
    case 'sc_pe_lc':
        return new Array(19.95, 19.95, 19.95);
    case 'sc_pr_lc':
        return new Array(29.95, 29.95, 29.95);
    default:
        return new Array( 0.00,  0.00,  0.00);
    case 'grahl_cd':
    case 'grahl_cd_lc':
        return new Array(12.95, 10.95, 13.95);
  }
}

function GetCurrIndex(currency) {
  switch (currency) {
    case 'EUR':
    case 'USD':
      return 0;
    case 'GBP':
      return 1;
    case 'CAD':
      return 2;
  }
}

function GetLicPrice(block_id, product_id, currency, qty) {
  var arr = GetLicPriceArr(block_id, product_id, qty);
  return arr[GetCurrIndex(currency)];
}


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, currency) {
  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');
  var elemDiscount = document.getElementById(block_id + '_Discount');
  var elemDiscountValue = document.getElementById(block_id + '_DiscountValue');
  var elemDiscount2Value = document.getElementById(block_id + '_Discount2Value');
  var elemDiscountPValue = document.getElementById(block_id + '_DiscountPValue');

  qty = (elemQty ? elemQty.value : 0);
  discount = (elemDiscountValue ? elemDiscountValue.value : 0);
  discount2 = (elemDiscount2Value ? elemDiscount2Value.value : 0);
  discountP = (elemDiscountPValue ? elemDiscountPValue.value : 0);

  // Einzelpreis
  price = GetLicPrice(block_id, 'lc', currency, qty);
  if (elemLicPrice)
    elemLicPrice.innerHTML = FormatPrice(price, decimal_separator);
  // Wartungspreis
  price = GetLicPrice(block_id, '2y', currency, qty);
  if (elem2YPrice)
    elem2YPrice.innerHTML = FormatPrice(price, decimal_separator);

  // Total
  price = GetLicPrice(block_id, 'lc', currency, qty) * qty;
  if (elem2Y && elem2Y.checked)
    price += GetLicPrice(block_id, '2y', currency, qty) * qty;
  if (elemCD && elemCD.checked)
    price += GetLicPrice('grahl', 'cd', currency, qty);

  if (elemTotal)
    elemTotal.innerHTML = FormatPrice(price - discount2, decimal_separator);

  if (elemDiscount)
    elemDiscount.innerHTML = FormatPrice(price * discountP/100 + discount, decimal_separator);

  if (elemTotal2)
    elemTotal2.innerHTML = FormatPrice(price * (1 - discountP/100) - discount, decimal_separator);
}

