﻿//
// (с) SiteLeader, 2005-2008 || http://www.siteleader.ru
//
  
//////////////////////////////////
// class 'work_class'
//
function work_class(owner_name, id, name, calc_db) {
  this.owner_name = owner_name;
  this.id = id;
  this.name = name;
  this.work_id = 0;
  this.model = 0;
  this.last_container = null;
  this.summa = 0;
  this.calc_db = calc_db;
}

work_class.prototype.get_work_select = function(container) {
  if (!this.calc_db) return null;
  var out = '<option value="0"> - Выберите вид работ - </option>';
  for (var i in calc_db['work']) out += '<option value="' + i + '"' + (i == this.work_id ? ' selected' : '') + '>' + this.calc_db['work'][i][0] + '</option>';
  out = '<select class="work-sel" id="work-sel-' + this.id + '" onchange="javascript:' + this.name + '.set_work(this.value)">' + out + '</select>';
  if (container && (container = document.getElementById(container))) container.innerHTML = out;
  return out;
}

work_class.prototype.get_cost_body = function(container) {
  var tmp, work_comment, item, price, out_job = '', out_spare = '';
  if (this.work_id && this.calc_db['work'][this.work_id]) work_comment = this.calc_db['work'][this.work_id][1];
  if (!this.calc_db || !(tmp = this.calc_db['work_ext'][this.work_id])) return null;
  if (!(this.model = eval(this.owner_name + '.model'))) { alert('Выберите модель!'); return null; }
  this.summa = 0;
  var multi_spare = 0;
  for (var i = 0, m = 0; i < tmp.length; i++) {
    if (this.calc_db['item'][tmp[i][0]][1] == 'spare' && this.calc_db['item'][tmp[i][0]][3] == '0') {
      if (multi_spare = (m == 1)) break;
      m = 1;
    }
  }
  for (var i = 0; i < tmp.length; i++) {
    if (!tmp[i][0]) continue;
    if (!(item = this.calc_db['item'][tmp[i][0]])) continue;
    if (this.model == '0' || !(price = this.calc_db['price'][this.model][tmp[i][0]])) continue;
    if (item[1] == 'job') {
      //if (tmp[i][1] == '0' && !(tmp[i][2] = confirm('Работа "' + item[0] + '" не является обязательной.\nВключить её? (стоимость: ' + price[1] + ' руб.)'))) continue;
      //out_job += '<tr><td>Работа: <span class="work">"' + item[0] + '"</span></td><td class="cost"><span class="price-1">' + price[1] + (price[0] > 1 ? ' x ' + price[0] : '') + '</span> руб.</td></tr>';
      if (item[5]) this.summa += price[1] * price[0];
      out_job += tmp[i][1] == '0' ? '<tr><td width="23px"><input type="checkbox" value="1" onclick="javascript:' + this.name + '.set_required(' + i + ')"' + (item[5] ? ' checked' : '') + ' /></td>' : '<tr><td width="23px"><input type="checkbox" value="1" checked disabled /></td>';
      out_job += '<td>Работа: <span class="work">"' + item[0] + '"</span></td><td class="cost"><span class="price-1">' + price[1] + (price[0] > 1 ? ' x ' + price[0] : '') + '</span> руб.</td></tr>';
    } else if (item[1] == 'spare') {
      //if (tmp[i][1] == '0' && !(tmp[i][2] = confirm('Зап. часть "' + item[0] + '" не является обязательной.\nВключить её? (стоимость: ' + price[1] + ' руб.)'))) continue;
      if (this.calc_db['item'][item[4]] && !this.calc_db['item'][item[4]][5]) continue;
      var p = ' стоимость <span class="price-1">' + price[1] + '</span> руб. ' + item[2];
      this.summa += price[0] * price[1];
      if (item[3] != '0' || multi_spare) out_spare += '<tr><td>' + item[0] + ':  </td><td class="w" colspan="2"><span class="price-1">' + price[0] + '</span> ' + item[2] + ';' + p + '</td></tr>';
      else {
        var options = '';
        for (var k = 1; k <= 10; k++) options += '<option value="' + k + '"' + (k == price[0] ? ' selected' : '') + '>' + k + '</option>';
        out_spare += '<tr><td>' + item[0] + ':  </td><td class="inp"><select id="' + this.name + '-spare-'+ i +'" onchange="javascript:' + this.name + '.set_count(' + i + ',this.value)' + '">' + options + '</select></td><td class="cost">' + p + '</td></tr>';
      }
//      else out_spare += '<tr><td>' + item[0] + ':  </td><td class="inp"><input type="text" id="' + this.name + '-spare-'+ i +'" value="' + price[0] + '" onkeypress="javascript:setTimeout(function(){alert(this.value)}, 50);;return true;" onchange="javascript:' + this.name + '.set_count(' + i + ',this.value)' + '" /></td><td class="cost">' + p + '</td></tr>';
    }
  }
  out_job = '<table class="work-cost">' + out_job + '</table>'; 
  if (work_comment) out_job += '<h3>Примечание:</h3>' + work_comment;
  if (out_spare) out_spare = '<h3>Зап. части:</h3><table class="spare-cost">' + out_spare + '</table>'; 
  out_job += out_spare;
  if (container && (container = document.getElementById(container))) container.innerHTML = out_job;
  return out_job;
}

work_class.prototype.set_count = function(field, value) {
  var tmp = this.calc_db['work_ext'][this.work_id];
  for (var i = 0; i < tmp.length; i++)
    if (this.calc_db['item'][tmp[i][0]][1] == 'job' || i == field) this.calc_db['price'][this.model][tmp[i][0]][0] = value;
  //alert(field + ' => ' + value);
  if (this.last_container) this.last_container.innerHTML = this.get_body(this.last_container);
  eval(this.owner_name + '.update_summa()');
}

work_class.prototype.set_required = function(field) {
  var tmp = this.calc_db['work_ext'][this.work_id];
  for (var i = 0; i < tmp.length; i++)
    if (i == field)
      this.calc_db['item'][tmp[i][0]][5] = this.calc_db['item'][tmp[i][0]][5] ? 0 : 1;
//  alert(field);
  if (this.last_container) this.last_container.innerHTML = this.get_body(this.last_container);
  eval(this.owner_name + '.update_summa()');
}

work_class.prototype.get_body = function(container) {
  var tmp = this.get_cost_body();
  if (tmp == null) tmp = '';
  var out = '<div class="item"><div class="box">' + this.get_work_select() + '<div class="total">Итого стоимость услуги<br />' + 
    (isNaN(this.summa) ? '<span class="price-2">Ошибка</span>' : '<span class="price-2">' + this.summa + '</span> руб.') + '</div>' + tmp;
  if (this.model > 0) out += '<div class="delete"><a href="#i" onclick="javascript:if(confirm(\'Удалить работу?\'))' + this.owner_name + '.delete_work(' + this.id + ')' + '">удалить</a></div>';
  if (container && (container = document.getElementById(container))) {
    container.innerHTML = out;
    this.last_container = container;
  }
  return out;
}

work_class.prototype.set_work = function(value) {
  this.work_id = value;
  if (this.last_container) this.last_container.innerHTML = this.get_body(this.last_container);
  eval(this.owner_name + '.update_summa()')
}

work_class.prototype.get_print_preview = function() {
  ///////////////
  var tmp, item, price, out_job = '', out_spare = '';
  if (!this.calc_db || !(tmp = this.calc_db['work_ext'][this.work_id])) return '';
  if (!(this.model = eval(this.owner_name + '.model'))) { return ''; }
  this.summa = 0;
  var multi_spare = 0;
  for (var i = 0, m = 0; i < tmp.length; i++) {
    if (this.calc_db['item'][tmp[i][0]][1] == 'spare' && this.calc_db['item'][tmp[i][0]][3] == '0') {
      if (multi_spare = (m == 1)) break;
      m = 1;
    }
  }
  for (var i = 0; i < tmp.length; i++) {
    if (!tmp[i]) continue;
    if (!(item = this.calc_db['item'][tmp[i][0]])) continue;
    if (this.model == '0' || !(price = this.calc_db['price'][this.model][tmp[i][0]])) continue;
    if (tmp[i][1] == '0' && !tmp[i][2]) continue;
    if (item[1] == 'job') {
      if (item[5]) {
        this.summa += price[1] * price[0];
        out_job += '<tr><td>Работа: "' + item[0] + '"</td><td style="text-align:right"><strong>' + price[1] + (price[0] > 1 ? ' x ' + price[0] : '') + '</strong> руб.</td></tr>';
      }
    } else if (item[1] == 'spare') {
      if (this.calc_db['item'][item[4]] && !this.calc_db['item'][item[4]][5]) continue;
      var p = ' стоимость <strong>' + price[1] + '</strong> руб. ' + item[2];
      this.summa += price[0] * price[1];
      out_spare += '<tr><td>' + item[0] + ':  </td><td colspan="2" style="text-align:right"><strong>' + price[0] + '</strong> ' + item[2] + ';' + p + '</td></tr>';
    }
  }
  out_job = '<table width="100%" cellpadding="0" cellspacing="0" style="font-size:9pt">' + out_job + '</table>'; 
  if (out_spare) out_spare = '<p style="margin:0.5em 0;font-weight:bold">Зап. части:</p><table width="100%" cellpadding="0" cellspacing="0" style="font-size:9pt">' + out_spare + '</table>'; 
  out_job += out_spare;
  ///////////////
  if (out_job == null) tmp = '';
  var out = '<p style="font-weight:bold;margin:0.5em 0;">' + calc_db['work'][this.work_id][0] + '</p>' + out_job + '<p style="margin:0.5em 0;text-align:right">Итого стоимость услуги: ' + (isNaN(this.summa) ? '<strong>Ошибка</strong>' : '<span style="font-size:12pt;font-weight:bold">' + this.summa + '</span> руб.</p>');
  return '<div style="margin-bottom:1.5em;font-size:9pt">' + out + '<hr /></div>';
}


//<hr /><p style="font-size:8pt;margin:1em 0">Расчет произведен на сайте: <a href="http://9954422.ru">http://9954422.ru</a><br />
//Телефоны: (495) 995-44-22, (495) 995-74-22<br />
//E-mail: <a href="mailto:info@9954422.ru">info@9954422.ru</a></p>


//////////////////////////////////
// class 'price_class'
//
function price_class(name, calc_db, w_type) {
  this.id = Math.round(Math.random() * 1048576);
  this.name = name;
  this.works = new Array();
  this.model = 0;
  this.last_container = null;
  this.calc_db = calc_db;
  this.w_type = w_type;
  this.summa = 0;
}

price_class.prototype.get_model_select = function(container) {
  if (!this.calc_db) return null;
  var out = '<option value="0"> - Выберите модель - </option>';
  for (var i in calc_db['model']) out += '<option value="' + i + '">' + calc_db['model'][i] + '</option>';
  out = '<select class="model-sel" id="model-sel-' + this.id + '" onchange="javascript:' + this.name + '.set_model(this.value)">' + out + '</select>';
  if (container && (container = document.getElementById(container))) container.innerHTML = out;
  return out;
}

price_class.prototype.set_model = function(value) {
  //alert('set model: ' + value);
  this.model = value;
  if (this.last_container) this.last_container.innerHTML = this.get_work_box(this.last_container);
  this.draw();
}

price_class.prototype.get_work_box = function(container) {
  if (!this.works.length) return null;
  var out = '', work_id = this.id;
  for (var i = 0; i < this.works.length; i++, work_id++) out += '<div id="work-id-' + work_id + '"></div>';
  out += '<div class="add-box"><div class="add-item"><a href="#i" onclick="javascript:' + this.name + '.add_work()' + '">[ + ] добавить работу</a></div>' + 
    '<div class="total" id="total">' + this.update_summa() + '</div></div>';
  if (container && (container = document.getElementById(container))) {
    container.innerHTML = out;
    this.last_container = container;
  }
  return out;
}

price_class.prototype.update_summa = function() {
  var t = document.getElementById('total');
  this.summa = this.get_summa();
  //////////
  var preview = this.get_print_preview();
  document.getElementById('print-container').value = preview;
  document.getElementById('print-container-2').value = preview;
  //////////
  this.summa = this.get_summa();
  return t ? t.innerHTML = '<span class="itog">ИТОГО:</span> ' + 
    (isNaN(this.summa) ? '<span class="price-2">Ошибка</span>' : '<span class="price-2">' + this.summa + '</span> руб.') : '';
}

price_class.prototype.draw = function() {
  if (!this.works.length) return;
  var work_id = this.id;
  for (var i = 0; i < this.works.length; i++, work_id++) if (this.works[i]) this.works[i].get_body('work-id-' + work_id);
  this.update_summa();
}

price_class.prototype.add_work = function() {
  this.works.push(new work_class(this.name, this.works.length, this.name + '.works[' + this.works.length + ']', this.calc_db));
  if (this.last_container) this.last_container.innerHTML = this.get_work_box(this.last_container);
  this.draw();
}

price_class.prototype.delete_work = function(work_id) {
  if (this.works.length < 2 || !this.works[work_id]) return;
  this.works[work_id] = null;
  //this.works.splice(work_id, 1);
  if (this.last_container) this.last_container.innerHTML = this.get_work_box(this.last_container);
  this.draw();
}

price_class.prototype.get_summa = function() {
  var summa = 0;
  for (var i = 0; i < this.works.length; i++) summa += this.works[i] ? this.works[i].summa : 0;
  return this.summa = summa;
}

price_class.prototype.get_print_preview = function() {
  var body = '';
  for (var i = 0; i < this.works.length; i++) body += this.works[i] ? this.works[i].get_print_preview() : '';
  return '<h3 style="text-align:center;">Расчет стоимости' + (this.w_type == 'basket' ? ' кузовного' : ' слесарного') + ' ремонта</h3>' + (this.model ? '<h4>Модель: ' + calc_db['model'][this.model] + '</h4>' : '') + body + '<p style="text-align:right;font-size:12pt;font-weight:bold">ИТОГО: ' + 
    (isNaN(this.summa) ? '<strong>Ошибка</strong>' : '<span style="font-size:14pt">' + this.summa + '</span> руб. </p>');
}

//////////////////////////////////
// create_calc
//
function create_calc(name, calc_db, select_container, work_container, w_type) {
  var obj = new price_class(name, calc_db, w_type);
  obj.get_model_select(select_container);
  obj.add_work();
  obj.get_work_box(work_container);
  obj.draw();
  return obj;
}

