/* Copyright (C) 2006 CC Open Computer Systems Ltd.
 *
 * Author: Grzegorz Kaczor <grzegorz.kaczor@cc.com.pl>
 *
 * PEIMP Tender specification insert form
 *
 * Should be included on the page once
 *
 */

/* remember to include specification_data.js! */

function specinsert_split_multiid(multiid) {
  return multiid.split("_");
}

function specinsert_join_multiid(listofids) {
  return listofids.join("_");
}

function specinsert_fill_d_industry(sel) {
  var i;
  
  sel.options.length = 0;
  for (i = 0; i < D_industries_by_name.length; i++) {

    sel.options[sel.options.length] = new Option(
        D_industry[D_industries_by_name[i]],
        ""+D_industries_by_name[i],
        false,
        false);        
  }
  
}

function specinsert_fill_d_specification(sel,id) {
  sel.options.length = 0;
  if (!id) return;

  if (D_specification_by_industry[id]) {

  }
  else {
    alert('Bad industry ID: '+id);
    return;
  }

  var i;
  sel.options[sel.options.length] = new Option("","0",true,true);
  //sel.options[sel.options.length] = new Option (  'Wszystkie                      ', 'wszystkie', true, true );
  //sel.options[sel.options.length-1].style.width = '150px';

  for (i = 1; i < D_specification_by_industry[id].length; i++) {
    sel.options[sel.options.length] =
        new Option(
          D_specification[D_specification_by_industry[id][i]],
          ""+D_specification_by_industry[id][i],
          false,
          false);
  }
}

function specinsert_makelist(textlist) {
  var txt = textlist.join(", ");

  txt = trim(txt);

  if (txt.length > 0) {
    txt = " // " + txt;
  }

  return txt;
}

function specinsert_insert_industry(indsel,specsel,selsel) {
  var ind_id = sval(indsel);
  if (!ind_id) {
    alert('Wybierz bran¿ê.');
    return;
  }
  if (specinsert_contains_id(selsel,ind_id)) {
    if (confirm('Ta bran¿a ju¿ jest zdefiniowana. Nadpisaæ?')) {
      sdel(selsel);
    }
    else {
      return;
    }
  }
  var idlist = new Array();
  idlist[0] = ind_id;
  for (i = 0; i < specsel.options.length; i++) {
    if (specsel.options[i].selected) {
      idlist[idlist.length] = specsel.options[i].value;
    }
  }

  if (idlist.length > 2) idlist = list_del(idlist,"0");

  var textlist = new Array();
  for (i = 1; i < idlist.length; i++) {
    textlist[textlist.length] = D_specification[idlist[i]];
  }

  selsel.selectedIndex = -1;

  selsel.options[selsel.options.length] =
      new Option(
        D_industry[ind_id] + specinsert_makelist(textlist),
        specinsert_join_multiid(idlist),
        false,
        true);

  specinsert_editselection(indsel,specsel,selsel);
}

function specinsert_compute_names(selsel) {
  for (i = 0; i < selsel.options.length; i++) {
    idlist = specinsert_split_multiid(selsel.options[i].value);
    if (idlist.length > 0) {
      ind_id = idlist[0];
      textlist = new Array();
      for (j = 1; j < idlist.length; j++) {
        textlist[textlist.length] = D_specification[idlist[j]];
      }
      selsel.options[i].text = D_industry[ind_id]+ specinsert_makelist(textlist);
    }
  }
}

function specinsert_contains_id(selsel,id) {
  for (i = 0; i < selsel.options.length; i++) {
    var idlist = specinsert_split_multiid(selsel.options[i].value);
    if ((idlist.length > 0) && (idlist[0] == id)) {
      selsel.options[i].selected = true;
      return true;
    }
  }
  return false;
}

function specinsert_submitprep(selsel) {
  if (!selsel) return;
  selall(selsel);
}

function specinsert_editselection(indsel,specsel,selsel) {

  var s = sval(selsel);
  if (!s) {
    indsel.selectedIndex = -1;
    specinsert_fill_d_specification(specsel,"");
    return;
  }

  var idlist = specinsert_split_multiid(s);

  if (idlist.length == 0) {
    indsel.selectedIndex = -1;
    specinsert_fill_d_specification(specsel,"");
    return;
  }

  selval(indsel,idlist[0]);
  specinsert_fill_d_specification(specsel,idlist[0]);
  idlist[0] = "";

  var idxobj = new Object;
  for (i = 0; i < idlist.length; i++) {
    idxobj["s:"+idlist[i]] = 1;
  }

  if (idlist.length == 1) {
    idxobj['s:0'] = 1;
  }

  for (i = 0; i < specsel.options.length; i++) {
    if (idxobj["s:"+specsel.options[i].value]) {
      specsel.options[i].selected = true;
    }
    else {
      specsel.options[i].selected = false;
    }
  }
}

function specinsert_search_mark_specifications(industry_select,spec_select) {

  var sel_industries = new Object;
  var sel_specs = new Object;

  var s_id_list = vallist(spec_select);
  for (i = 0; i < s_id_list.length; i++) {
    var ind = D_specification_industry[s_id_list[i]];
    if (ind) {
      sel_industries['i:'+ind] = true;
    }
    sel_specs['d:'+s_id_list[i]] = true;
  }
  // zero specification select
  spec_select.options.length = 0;
  spec_select.options[0] = new Option (  'Wszystkie                      ', 'wszystkie', false, false );
  spec_select.options[0].style.width = '150px';

  // dump common specifications
  for (i in D_all_ind_specifications) {
    var sel = false;
    if (sel_specs['d:'+i]) sel = true;
    spec_select.options[spec_select.options.length] =
        new Option(
          D_specification[i],
          i,
          false,
          sel
        );
  }

  for (i = 0; i < industry_select.options.length; i++) {
    var ind_id = industry_select.options[i].value;
    industry_select.options[i].selected = (
        industry_select.options[i].selected ||
        (sel_industries['i:'+ind_id]));

    if (industry_select.options[i].selected) {
      if (D_specification_by_industry[ind_id]) {
        for (j = 1; j < D_specification_by_industry[ind_id].length; j++) {
          if (D_all_ind_specifications[D_specification_by_industry[ind_id][j]]) {
            continue;
          }
          var sel = false;
          if (sel_specs['d:'+D_specification_by_industry[ind_id][j]]) {
            sel = true;
          }
          spec_select.options[spec_select.options.length] =
              new Option (
              D_specification[D_specification_by_industry[ind_id][j]],
              D_specification_by_industry[ind_id][j],
              false,
              sel
          );
        }
      }
    }
  }

}

function specinsert_get_industry_specifications(industry_select,spec_select) {
  var sel_specs = new Object;
  var s_id_list = svallist(spec_select);
  for (i = 0; i < s_id_list.length; i++) {
    sel_specs['d:'+s_id_list[i]] = true;
  }

  // zero specification select
  spec_select.options.length = 0;
  spec_select.options[0] = new Option (  'Wszystkie                      ', 'wszystkie', false, false );
  spec_select.options[0].style.width = '150px';

  // dump common specifications
  for (i in D_all_ind_specifications) {
    var sel = false;
    if (sel_specs['d:'+i]) sel = true;
    spec_select.options[spec_select.options.length] =
        new Option(
        D_specification[i],
    i,
    false,
    sel
        );
  }
  var ind_id_list = svallist(industry_select);
  for (i = 0; i < ind_id_list.length; i++ ) {
    ind_id = ind_id_list[i];
    if (D_specification_by_industry[ind_id]) {
      for (j = 1; j < D_specification_by_industry[ind_id].length; j++) {
        if (D_all_ind_specifications[D_specification_by_industry[ind_id][j]]) {
          continue;
        }
        var sel = false;
        if (sel_specs['d:'+D_specification_by_industry[ind_id][j]]) {
          sel = true;
        }
        spec_select.options[spec_select.options.length] =
            new Option (
            D_specification[D_specification_by_industry[ind_id][j]],
            D_specification_by_industry[ind_id][j],
            false,
            sel
          );
      }
    }
  }
}

/* new specifications - 3 part control */

function specctrl_new_context() {

  ctxt = new Object;

  // for efficiency, selected specifications are stored
  // in a two-level tree structure

  return ctxt;
}

function specctrl_get_ind_spec_sel( ctxt, control, industry ) {

  if (!ctxt) return;

  control.options.length = 0;

  industry++;
  industry--;

  if (!industry) {
    selspecs = false;
  }
  else {
    selspecs = ctxt['i:'+industry];
  }

  nullspecs = ctxt['i:-'];

  if (!selspecs) selspecs = new Object;
  if (!nullspecs) nullspecs = new Object;

  // append null industry specifications
  for (sp_id in D_all_ind_specifications) {

    if (!sp_id) continue;

    s = (nullspecs['s:'+sp_id] == 'T');

    control.options[control.options.length] = new Option(
        D_specification[sp_id],
        sp_id,
        false,
        s);
  }

  // append industry specifications
  if (industry) {
    for (i = 1; i < D_specification_by_industry[industry].length; i++) {
      sp_id = D_specification_by_industry[industry][i];

      if (D_all_ind_specifications[sp_id]) {
        continue; // all industry specs already selected
      }

      s = (selspecs['s:'+sp_id] == 'T');

      control.options[control.options.length] = new Option(
          D_specification[sp_id],
          sp_id,
          false,
          s);
    }
  }
  else {
  }

}

function specctrl_spec_update( ctxt, control, industry ) {

  if (!ctxt) return;

  industry++;
  industry--;

  sels = new Object;
  nsels = new Object;

  for (i = 0; i < control.options.length; i++) {
    if (control.options[i].selected) {
      if (D_all_ind_specifications[control.options[i].value]) {
        // all industry spec
        nsels['s:'+control.options[i].value] = 'T';
      }
      else {
        sels['s:'+control.options[i].value] = 'T';
      }
    }
  }

  ctxt['i:-'] = nsels;
  if (industry) {
    ctxt['i:'+industry] = sels;
  }
}

function specctrl_add_industries( ctxt, industry_list, control ) {

  if (!ctxt) return;

  sel = new Object;
  newList = new Array;

  sv = sval(control);

  for (i = 0; i < control.options.length; i++) {
    sel['i:'+control.options[i].value] = 'T';
    newList[newList.length] = control.options[i].value;
  }

  for (i = 0; i < industry_list.options.length; i++) {
    if (industry_list.options[i].selected) {
      if (sel['i:'+industry_list.options[i].value] == 'T') continue;
      newList[newList.length] = industry_list.options[i].value;
    }
  }

  sort_array_num(newList);

  control.options.length = 0;
  for (i = 0; i < newList.length; i++) {
    control.options[control.options.length] = new Option(
        D_industry[newList[i]],
    newList[i],
    false,
    false);
  }

  selval( control, sv );
}

function specctrl_sub_industries( ctxt, control ) {

  if (!ctxt) return;

  for (i = control.options.length-1; i >= 0; i--) {
    if (control.options[i].selected) {
      ctxt['i:'+control.options[i].value] = false;
      control.options[i] = null;
    }
  }

}

function specctrl_get_sel_specs( ctxt ) {

  if (!ctxt) return "";

  arr = new Array();

  for (i in ctxt) {
    for (s in ctxt[i]) {
      if (s.substr(0,2) == 's:') {
        arr[arr.length] = s.substr(2);
      }
    }
  }

  sort_array_num( arr );

  return arr.join(';');
}

function specctrl_get_sel_industries( ctxt, control ) {

  if (!ctxt) return "";

  arr = vallist( control );

  sort_array_num( arr );

  return arr.join(';');
}

function specctrl_initialize( ctxt, industries, specifications, industry_control, specification_control, industry_list_control ) {

  if (!ctxt) return;

  industry_control.options.length = 0;
  specification_control.options.length = 0;
  industry_list_control.options.length = 0;
  
  specinsert_fill_d_industry( industry_list_control ); 

  neI = false;

  if (industries.length > 0) {
    i_arr = industries.split(';');
  }
  else {
    i_arr = new Array();
  }

  if (specifications.length > 0) {
    v_spec = specifications.split(';');
  }
  else {
    v_spec = new Array();
  }

  var vSel = new Object;
  for (i = 0; i < i_arr.length; i++) {
    vSel['i:'+i_arr[i]] = 'T';
  }

  // get additional industries from specifications
  // and initialize specifications at the same time

  for (i = 0; i < v_spec.length; i++) {

    v_spec[i]++;
    v_spec[i]--;

    if (!v_spec[i]) continue; // wrong value - ignore

    // get specification industry
    if (D_all_ind_specifications[v_spec[i]]) {
      vD = false;
    }
    else {
      vD = D_specification_industry[v_spec[i]];
    }

    if (vD && (!neI)) neI = vD;

    // append to industry list
    if (vD) {
      if (vSel['i:'+vD] != 'T') i_arr[i_arr.length] = vD;
      vSel['i:'+vD] = 'T';
    }

    if (!vD) vD = '-';
    // append specification
    vObj = ctxt['i:'+vD];
    if (!vObj) vObj = new Object;
    vObj['s:'+v_spec[i]] = 'T';
    if (vD != '-') ctxt['i:'+vD] = vObj;
  }

  // specifications initialized, now industries
  sort_array_num( i_arr );

  for (i = 0; i < i_arr.length; i++) {
    industry_control.options[industry_control.options.length] = new Option(
        D_industry[i_arr[i]],
        i_arr[i],
        false,
        false);
  }

  if ((!neI) && (i_arr.length > 0)) {
    selval(industry_control, i_arr[0]);
  }
  else {
    selval(industry_control, neI);
  }

  specctrl_get_ind_spec_sel( ctxt, specification_control, sval(industry_control));
}
