/*
 *     The contents of this file are subject to the Mozilla Public License
 *     Version 1.1 (the "License"); you may not use this file except in
 *     compliance with the License. You may obtain a copy of the License at
 *     http://www.mozilla.org/MPL/
 *
 *     Software distributed under the License is distributed on an "AS IS"
 *     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 *     License for the specific language governing rights and limitations
 *     under the License.
 *
 *     The Original Code is A3l-system.
 *
 *     The Initial Developer of the Original Code is A3l.
 *     Portions created by Anton V. Arkhipov (A3l) are Copyright (C)
 *     Dataport Systems. All Rights Reserved.
 *
 *     Contributor(s): Anton V. Arkhipov (A3l).
 */
var delay = 500;
var MTim = null;
var menu = null;

$(document).ready(function() {
    $('div.h_menu a').hover(
        function(){
            $(this).parents('div.h_menu').find('div.hidden').removeClass('full');
            $('div.h_menu').removeClass('bg_black');
            $('div.h_menu div.full').fadeOut(400);
            $(this).parents('div.h_menu').find('div.hidden').addClass('full');
            $(this).parents('div.h_menu').addClass('bg_black');
            $(this).parents('div.h_menu').find('div.hidden').fadeIn(300);
        },
        null
        );
    $('div.h_menu').hover(
        function(){
            clearTimeout(MTim)
        },
        function(){
            MTim = self.setTimeout("ClearMenu()", delay)
        }
    );
});

function ClearMenu() {
  $('div.h_menu').removeClass('bg_black');
  $('div.h_menu div.hidden').fadeOut(400);
}

function HideNO() {
  $('tr.may_hide').fadeOut(300);
  $('#v1').addClass('hidden');
  $('#v2').removeClass('hidden');
}
function ShowNO() {
  $('tr.may_hide').fadeIn(300);
  $('#v2').addClass('hidden');
  $('#v1').removeClass('hidden');
}
function GetCost() {
  var count = $('#licence_count').attr("value");
  var len = count.length;
  if (len == 0 | count == 0) {
    alert("Без количества лицензий покупка, увы, не возможна =(");
    return;
  }

  var digits = "0123456789";

  for(i=0; i <= len; i++){
    if (digits.indexOf(count.charAt(i))<0){
      alert("Столько лицензий вам никто не продаст...");
      return;
    }
  }
  var text = "";
  var cost = 0;
  if ($('#licence_type').val() == 'am') {
    text += "<h4>Расчёт стоимости для "+escape(count)+" Advanced Management Licence:</h4>";
    text += "<p>Базовая лицензия - <b>111 440 руб.</b></p>";
    if (count > 1)
      cost = (count-1) *111440;
    if (count > 11)
      cost = (count-1) *100295;
    if (count > 26)
      cost = (count-1) *94725;
    if (count > 51)
      cost = (count-1) *83580;
    if (count > 76)
      cost = (count-1) *72435;
    if (count > 161)
      cost = (count-1) *66865;
    if (count > 501)
      cost = (count-1) *61290;
    if (cost > 0)
      text += "<p>Дополнительные пользователи - <b>"+ Format(cost) +" руб.</b></p>";
    cost += 111440;
    text += "<p><u>Сумма по лицензии - <b>"+ Format(cost) +" руб.</b></u></p>";
  }
  if ($('#licence_type').val() == 'be') {
    text += "<h4>Расчёт стоимости для "+escape(count)+" Business Essential Licence:</h4>";
    text += "<p>Базовая лицензия - <b>63 000 руб.</b></p>";
    //cost += 2650;
    if (count > 1 && count <= 11)
      cost = (count-1) *63000;
    if (count > 11 && count <= 26)
      cost = (count-1) *56705;
    if (count > 26 && count <= 51)
      cost = (count-1) *53560;
    if (count > 51 && count <= 76)
      cost = (count-1) *47265;
    if (count > 76 && count <= 161)
      cost = (count-1) *40940;
    if (count > 161 && count <= 501)
      cost = (count-1) *37795;
    if (count > 501)
      cost = (count-1) *34645;
    if (cost > 0)
      text += "<p>Дополнительные пользователи - <b>"+ Format(cost) +" руб.</b></p>";
    cost += 63000;
    text += "<p><u>Сумма по лицензии - <b>"+ Format(cost) +" руб.</b></u></p>";
  }
  text += "<p>Подписка на обновления на 1 год - <b>"+ Format(cost*0.16*1.18) +" руб.</b><br />";
  text += "Из них НДС - <b>"+ Format(cost*0.16*0.18) +" руб.</b></p>";

  text += "<p><u>Общая сумма включая НДС: <b>"+ Format((cost*0.16*1.18)+cost) +" руб.</b></u></p>";
  $('#answer').html(text);
}
function Format(amount)
{
  var a = (Math.round(amount * 100) / 100);
  var n = ""+escape(Math.round(a));
  var result = "";
  if (n.length > 3){
    while(n.length > 3) {
      result = " " + n.substr(n.length-3, 3)+ result;
      n = n.substr(0,n.length-3);
    }
    result = n + result;
  }
  else
    result = n;
  return result;
}
function FullInfo(code) {
  $('#'+code+' div.description').addClass('hidden');
  $('#'+code+' div.about').removeClass('hidden');
  var text = '<a class="ajax" href="javascript:MinInfo(\''+code+'\');">Общее описание</a> &nbsp;&nbsp;&nbsp; <b>Подробное описание</b>';

  $('#'+code+' div.links').html(text);
}

function MinInfo(code) {
  $('#'+code+' div.description').removeClass('hidden');
  $('#'+code+' div.about').addClass('hidden');
  var text = '<b>Общее описание</b> &nbsp;&nbsp;&nbsp; <a class="ajax" href="javascript:FullInfo(\''+code+'\');">Подробное описание</a>';

  $('#'+code+' div.links').html(text);
}
