/* Copyright (C) 2006 CC Open Computer Systems Ltd. */

function sval(sel) {
  if (sel.selectedIndex >= 0) {
    return sel.options[sel.selectedIndex].value;
  }
  return "";
}

function elid(id) {
  return document.getElementById(id);
}

function sdel(sel) {
  var i;
  for (i = sel.options.length-1; i >= 0; i--) {
    if (sel.options[i].selected) {
      sel.options[i] = null;
    }
  }
}

function svallist(sel) {
  var optlist = new Array();
  var alllist = false;
  /* first check "wszystkie" */
  for (i = 0; i < sel.options.length; i++) {
    if (sel.options[i].selected && sel.options[i].value == 'wszystkie') {
      alllist = true;
    }
  }
  for (i = 0; i < sel.options.length; i++) {
    if (sel.options[i].selected || alllist) {
      optlist[optlist.length] = sel.options[i].value;
    }
  }
  return optlist;
}


function vallist(sel) {
  var optlist = new Array();
  for (i = 0; i < sel.options.length; i++) {
   optlist[optlist.length] = sel.options[i].value;
  }
  return optlist;
}

function selvallist(sel,list) {
  var vals = new Object;
  for (i = 0; i < list.length; i++) {
    vals['key:'+list[i]] = 1;
  }
  for (i = 0; i < sel.options.length; i++) {
    if (vals['key:'+sel.options[i].value]) {
      sel.options[i].selected = true;
    }
    else {
      sel.options[i].selected = false;
    }
  }
}

var date_limit_year = 53;

function date_from_short_format(shortYear) {
  shortYear++; shortYear--;
  if (shortYear < 0 || shortYear > 99) return '#';
  if (shortYear < date_limit_year) return 2000+shortYear;
  else return 1900 + shortYear;
}

function date_isleapyear(year) {
  return ((year != 0) && (year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0)));
}

function date_daysinmonth(year,month) {
  if (year == 0) return -1;
  if (month == 1 || month == 3 || month == 5 || month == 7 ||
  month == 8 || month == 10 || month == 12) return 31;
  if (month == 4 || month == 6 || month == 9 || month == 11) return 30;
  if (month == 2) {
    if (date_isleapyear(year)) return 29;
    else return 28;
  }
  return -1;
}

function date_verify(year,month,day) {
  if (year < 1000 || year == 0 || year > 3000) return false;
  if (month < 1 || month > 12) return false;
  if (day < 1 || day > date_daysinmonth(year,month)) return false;
  return true;
}

function date_verify_future(year,month,day) {
  var d = new Date();
  var curr_yr = d.getFullYear(); var curr_mon = d.getMonth()+1; var curr_day = d.getDate();
  if (year < curr_yr ) return false;
  if ( (year == curr_yr)  && (month  < curr_mon) ) return false;
  if ( (year == curr_yr)  && (month  == curr_mon) && (day < curr_day )) return false;
  return true;
}

function multival_split(multivalue) {
  return multivalue.split(";");
}

function multival_join(list) {
  return list.join(";");
}

function selall(sel) {
  for (i = 0; i < sel.options.length; i++) sel.options[i].selected = true;
}

function selval(sel,val) {
  for (i = 0; i < sel.options.length; i++) {
    if (sel.options[i].value == val) sel.options[i].selected = true;
    else sel.options[i].selected = false;
  }
}

function list_del(list,elem) {
  var l = new Array();
  for (i = 0; i < list.length; i++) {
    if (list[i] != elem) {
      l[l.length] = list[i];
    }
  }
  return l;
}

function sort_order_view( col, sort_by, sort_order_desc ) {
  if ( sort_by == col ) {
        document.write( "<img src=\"/peimp/img/" );
        if ( sort_order_desc == '0' ) document.write( "up" );
        else document.write( "down" );
        document.write( ".gif\" width=\"15\" height=\"10\" hspace=\"5\" border=\"0\" alt=\"\">" );
  }
  return 1;
}

function integer_to_string(num,digits) {
  if (!digits) return '#';
  num++;num--;
  num = num.toString();
  var l = num.length;
  if (l > digits) return '#';
  while (l < digits) {
    num = '0'+num;
    l++;
  }
  return num;
}

function safe_match(regex,text) {
  regex.lastIndex = 0;
  return regex.exec(text);
}

function trim(str) {
  if (!str) return '';
  var sIdx = 0;
  var eIdx = str.length;
  while (sIdx < eIdx) {
    if (str[sIdx] == ' ' || str[sIdx] == '\t' || str[sIdx] == '\n') sIdx++;
    else break;
  }
  if (sIdx >= eIdx) return '';
  eIdx--;
  while (eIdx > sIdx) {
    if (str[eIdx] == ' ' || str[eIdx] == '\t' || str[eIdx] == '\n') eIdx--;
    else break;
  }
  return str.substring(sIdx,eIdx-sIdx+1);
}


// Tooltip-like help pop-ups used in forms

var ie=0
if (navigator.appVersion.indexOf("MSIE")!=-1) {
  ie=1;
}

// Tooltip-like help pop-ups used in forms
    function formtooltip(el,flag){
      elem = document.getElementById(el);
      if (flag) {
        if (ie) {
          elem.parentNode.parentNode.style.zIndex=1000;
          elem.parentNode.parentNode.style.borderRight='0px solid #000';
        // ugly , yes .. but neccesary to avoid a small but very annoying bug in IE6
        }
        elem.style.visibility='visible';
      }
      else {
        if (ie) {
          elem.parentNode.parentNode.style.zIndex=1;
          elem.parentNode.parentNode.style.border='none';
        }
        elem.style.visibility='hidden' };
    }

// Tooltip-like help pop-ups used in forms
    function show_hide(el,flag){
      elem = document.getElementById(el);
      if (flag) {
        if (ie) {
          elem.parentNode.parentNode.style.zIndex=1000;
          elem.parentNode.parentNode.style.borderRight='0px solid #000';
        // ugly , yes .. but neccesary to avoid a small but very annoying bug in IE6
        }
        elem.style.display='inline';
      }
      else {
        if (ie) {
          elem.parentNode.parentNode.style.zIndex=1;
          elem.parentNode.parentNode.style.border='none';
        }
        elem.style.display='none';
      }
    }

    function hide_submenu() {
      if ( document.peimp.submenu_active.value == '') {
        if ( document.peimp.submenu_active.value != '' ) {
          o_hide(document.peimp.submenu_active.value);
          document.peimp.submenu_active.value;
        }
      }
    }


    function o_show_submenu( val1, val2 ) {
      o_show(val1);
      var s = getStyleObject(val2);
      s.backgroundColor = '#AAD0E0';
    }

    function o_hide_submenu( val1, val2 ) {
      o_hide(val1);
      var s = getStyleObject(val2);
      s.backgroundColor = '#BCE3F4';
    }

    // disable & enable forms
    function disable_on_off(col) {
      if(col.value.length>0)
      {
        for ( i=0; i<document.peimp.length; i++) {
          if ( document.peimp.elements[i] != col && document.peimp.elements[i].type != 'button' && document.peimp.elements[i].type != 'hidden' &&  document.peimp.elements[i].name != 'archive' ) {
            document.peimp.elements[i].disabled = true;
          }
        }
      }
      else
      {
        for ( i=0; i<document.peimp.length; i++) {
          document.peimp.elements[i].disabled = false;
        }
      }
    }


// clear select forms
function clear_selecte_all() {
  for ( i=0; i<document.peimp.length; i++) {
    if ( document.peimp.elements[i].type == 'select-multiple' ) {
      var sel = document.peimp.elements[i];
      for (j = 0; j<sel.options.length; j++) {
        if ( sel.options[j].value == 'wszystkie' && sel.options[j].selected ) {
          for (k = 0; k<sel.options.length; k++) sel.options[k].selected = false;
          continue;
        }
      }
    }
  }
}


//On scrolling of DIV tag.
function OnDivScroll( elem )
{
  var sel_elem = document.getElementById(elem);

  if (sel_elem.options.length > 5) {
    sel_elem.size=sel_elem.options.length;
  }
  else {
    sel_elem.size=5;
  }
}

function sort_array_text(arr) {
  arr.sort();
}

function _comp_num(a,b) {
  return (a-b);
}

function sort_array_num(arr) {
  arr.sort(_comp_num);
}

function sort_array_comp(arr,comp) {
  arr.sort(comp);
}

function compat_function(f) {
  if (f) return true;
  else return false;
}

function disableForm(theform) {
  if (document.all || document.getElementById) {
    for (i = 0; i < theform.length; i++) {
      var tempobj = theform.elements[i];
      if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset")
      tempobj.disabled = true;
    }
  }
}

function submit_tender_search() {
  clear_selecte_all();
  document.peimp.a.value='search_tender';
  document.peimp.submit();
  document.peimp.a.value='';
}

function submit_invest_search() {
  clear_selecte_all();
  document.peimp.a.value='search_invest';
  document.peimp.submit();
  document.peimp.a.value='';
}

function submit_iwp_search() {
  clear_selecte_all();
  document.peimp.a.value='search_iwp';
  document.peimp.submit();
  document.peimp.a.value='';
}

function submit_imp_create_rss() {
  clear_selecte_all();
  document.peimp.a.value='imp_create_rss';
  document.peimp.submit();
  document.peimp.a.value='';
}

function submit_imp_delete_rss() {
  clear_selecte_all();
  document.peimp.a.value='imp_delete_rss';
  document.peimp.submit();
  document.peimp.a.value='';
}

function calcHeight( val_domain, val_id )
{
  document.domain=val_domain;
  var the_height = document.getElementById(val_id).contentWindow.document.body.scrollHeight;
  document.getElementById(val_id).height = the_height+20;
}

function check_item_list( item, list )
{
//   alert( 'search [' + item + '] on [' + list + ']');
  if ( !item || !list ) return false;
  for ( var i=0; i<list.length; i++ ) if ( list[i] == item ) return true;
  return false;
}

function change_select_by_select_with_map( master_select_id, slave_select_id, slave_full_list, map )
{
  var master = document.getElementById(master_select_id);
  var slave = document.getElementById(slave_select_id);
  var slave_selected_old_list = new Array();
  var slave_new_list = new Array();
  var i=0;
  var j=0;

  if ( !master ) {
    /* elements not found... */
//     alert('MASTER element [' + master_select_id + '] not found!');
    return false;
  }

  if ( !slave ) {
    /* elements not found... */
//     alert('SLAVE element [' + slave_select_id + '] not found!');
    return false;
  }

  /* remember about selected... */
  var count = 0;
  for ( i=0; i<slave.options.length; i++ ) {
    if ( slave.options[i].selected ) {
      slave_selected_old_list[count++] = slave.options[i].value;
    }
  }

  /* clear options list */
  slave.options.length = 0;

  /* create new options list */
  count = 0;
  for ( i=0; i<master.options.length; i++ ) {
    if ( master.options[i].selected ) {
      if ( master.options[i].value == 'wszystkie' ) {
        for ( j=0; j<slave_full_list.length-1; j++ ) {
          slave_new_list[count++] = slave_full_list[j][0];
        }
        break;
      }
      for ( j=0; j<map.length-1; j++ ) {
        if ( master.options[i].value == map[j][0] && !check_item_list( map[j][1], slave_new_list ) ) {
          slave_new_list[count++] = map[j][1];
        }
      }
    }
  }

  /* create new option list (order from slave_full_list) */
  count = 0;
  for ( i=0; i<slave_full_list.length-1; i++ ) {
    if ( check_item_list( slave_full_list[i][0], slave_new_list ) ) {
      slave.options[count++] = new Option( slave_full_list[i][1], slave_full_list[i][0] );
    }
  }

  /* mark as selected new options list */
  for ( i=0; i<slave.options.length; i++ ) {
    slave.options[i].selected = check_item_list( slave.options[i].value, slave_selected_old_list );
  }

  return true;
}

function del_last_char( itemid ) {
  var el=document.getElementById(itemid);
  var lel=el.innerHTML.length;
  if ( lel > 1 ) {
    el.innerHTML=el.innerHTML.substr(0,lel-4);
  }
}



function getCitiName( val ) {
  if ( val ) {
    return CitiList[val][0]
  }
  return false;
}

function getCitiRange( val ) {
  if ( val ) {
    return CitiList[val][1]
  }
  return false;
}

var focusedElement;

function activateElement( el ) {
  focusedElement = el;
}

function promptCityName( keyCode, partName, promptId, resId ) {
  var promptElem = document.getElementById(promptId);
  var count = 0;
  for( i=0; i<CitiList.length; i++ ) {
    if ( CitiList[i][0].toUpperCase() == partName.toUpperCase() ) {
      var resElem = document.getElementById(resId);
      resElem.value = CitiList[i][1];
      return true;
    }
  }
  for( i=0; i<CitiList.length; i++ ) {
    if ( CitiList[i][0].toUpperCase().slice(0,partName.length) == partName.toUpperCase() ) {
      promptElem.options[count++] = new Option( CitiList[i][0], i );
    }
    if ( count>7 ) {
      break;
    }
  }
  for( i=0; count<=7; i++ ) {
    promptElem.options[count++] = new Option( '', '' );
  }
  promptElem.style.display='inline';
  if ( keyCode == 40 ) {
    promptElem.focus();
  }
  return true;
}

function hiddeElement( elId ) {
  el = document.getElementById(elId);
  if ( elId != focusedElement ) {
    el.style.display='none';
  }
}

function updateCity( val, selectorId, citinameId, citirangeId ) {
  var citinameEl = document.getElementById(citinameId);
  var citirangeEl = document.getElementById(citirangeId);
  var citinameEl_value = getCitiName(val);
  var citirangeEl_value = getCitiRange(val);
  if ( citinameEl_value ) {
    citinameEl.value = citinameEl_value;
  }
  if ( citinameEl_value ) {
    citirangeEl.value = citirangeEl_value;
  }
  focusedElement = '';
  hiddeElement(selectorId);
}

// ------------------------------------------------------------------------ //
// ------------------------------------------------------------------------ //
// ------------------------------------------------------------------------ //

// ------------------------------------------------------------------------ //
// ------------------------------------------------------------------------ //
// ------------------------------------------------------------------------ //

function selectCompanyPersonItem( context ) {
  var sel_obj = document.getElementById( 'sel_company_person_' + context );

  if ( sel_obj.value == "" ) return false;

  for ( var i=0; i<sel_obj.options.length; i++ ) {
    var sel_option = sel_obj.options[i];
    if ( sel_option.selected ) {
      break;
    }
  }
  var multisel_obj = document.getElementById( 'multisel_company_person_' + context );
  var tab_obj = document.getElementById( 'company_person_table_' + context );

  /* select element on multiselect */
  for ( var i=0; i<multisel_obj.options.length; i++ ) {
    if ( sel_option.value == multisel_obj.options[i].value ) {
      multisel_obj.options[i].selected = true;
      break;
    }
  }

  /* ad tr to table */
  tr = document.createElement('tr');
  tr.setAttribute( 'id', 'company_person_tr_' + context + sel_obj.value );

  td = document.createElement('td');
  td.appendChild(document.createTextNode(sel_option.text));
  tr.appendChild(td);

  td = document.createElement('td');
  td.setAttribute( 'style', 'text-align: right;' );
  var input_btn = document.createElement('input');
  input_btn.setAttribute( 'type', 'button' );
  input_btn.setAttribute( 'value', '-' );
  input_btn.setAttribute( 'style', 'width: 20px;' );
  input_btn.setAttribute( 'onclick', "unselectCompanyPersonItem('"+context+"', '" + sel_obj.value +"' ); return false;" );
  td.appendChild(input_btn);
  tr.appendChild(td);

  tab_obj.appendChild(tr);

  /* remove item from select */
  sel_obj.removeChild( sel_option );

  sel_obj.value = "";

  return true;
}

// ------------------------------------------------------------------------ //

function unselectCompanyPersonItem( context, item_id ) {
  var sel_obj = document.getElementById( 'sel_company_person_' + context );
  var multisel_obj = document.getElementById( 'multisel_company_person_' + context );
  var tab_obj = document.getElementById( 'company_person_table_' + context );
  var tr_obj = document.getElementById( 'company_person_tr_' + context + item_id );
  var cp_list = new Array();
  var sel_cp_list = new Array();
  var tmp;

  tab_obj.removeChild(tr_obj);

  /* create cp_list & sel_cp_list */
  for ( var i=0; i<multisel_obj.options.length; i++ ) {
    if ( multisel_obj.options[i].value == item_id ) {
      multisel_obj.options[i].selected = false;

      try {
        sel_obj.add(new Option( multisel_obj.options[i].text, multisel_obj.options[i].value ), sel_obj.options[0] );
      }
      catch(e) {
        sel_obj.add(new Option( multisel_obj.options[i].text, multisel_obj.options[i].value ), sel_obj.selectedIndex );
      }
      break;
    }
  }

  sel_obj.value = "";

  return true;
}

// ------------------------------------------------------------------------ //

function item_on_list( item, list ) {
  for ( var i=0; i<list.length; i++ ) {
    if ( item == list[i] ) {
      return true;
    }
  }
  return false;
}

// ------------------------------------------------------------------------ //

function update_select_list( sel_obj, cp_list, sel_cp_list ) {
  for(var i=0; i<cp_list.length; i++) {
    var option = opener.document.createElement('option');
    option.appendChild(opener.document.createTextNode(cp_list[i][1]));
    option.setAttribute('value', cp_list[i][0] );
    if ( item_on_list( cp_list[i][0], sel_cp_list ) ) {
      option.setAttribute('selected', 'selected' );
    }

    sel_obj.appendChild(option);
  }
}

// ------------------------------------------------------------------------ //

function update_select_omit_list( sel_obj, cp_list, sel_cp_list, omit_list ) {
  for(var i=0; i<cp_list.length; i++) {
    if ( item_on_list( cp_list[i][0], omit_list ) ) {
      continue;
    }
    var option = opener.document.createElement('option');
    option.appendChild(opener.document.createTextNode(cp_list[i][1]));
    option.setAttribute('value', cp_list[i][0] );
    if ( item_on_list( cp_list[i][0], sel_cp_list ) ) {
      option.setAttribute('selected', 'selected' );
    }

    sel_obj.appendChild(option);
  }
}

// ------------------------------------------------------------------------ //

function createCompanyPersonControl( context, cp_list, sel_cp_list ) {
  var i=0;


  /* select */
  var sel_cp = opener.document.getElementById('sel_company_person_' + context );
  try {
    /* clear all */
    while ( sel_cp.options.length ) {
      sel_cp.removeChild(sel_cp.options[sel_cp.options.length-1]);
    }
    /* recreate */
    update_select_omit_list( sel_cp, cp_list, '0', '' );
//     if ( sel_cp.options.length > 1 ) {
//       opener.document.getElementById( 'add_company_person_btn_' + context ).onclick();
//     }

    sel_cp.value = '';
  }
  catch ( e ) {
    ;
  }

  return true;
}

// ------------------------------------------------------------------------ //
function delCompanyItem( context, clear_list )
{
  /* clear list */
  for( var i=0; i<clear_list.length; i++ ) {
    try {
      document.getElementById( clear_list[i] ).value = "";
    }
    catch( e ) {
      alert( clear_list[i] );
    }
  }

  /* clear hidden multiselect */
  var multisel_hidden_cp = document.getElementById( 'multisel_company_person_' + context );
  if ( multisel_hidden_cp ) {
    while ( multisel_hidden_cp.options.length ) {
      multisel_hidden_cp.removeChild(multisel_hidden_cp.options[multisel_hidden_cp.options.length-1]);
    }
  }

  /* clear company person table */
  var table = document.getElementById( 'company_person_table_' + context );
  if ( table ) {
    var tr_list = table.childNodes;
    for ( i=0; i<tr_list.length; i++ ) {
      table.removeChild(tr_list[i]);
    }
  }

  /* clear select */
  var sel_cp = document.getElementById('sel_company_person_' + context );
  while ( sel_cp.options.length ) {
    sel_cp.removeChild(sel_cp.options[sel_cp.options.length-1]);
  }
}

// ------------------------------------------------------------------------ //
// ------------------------------------------------------------------------ //

// ------------------------------------------------------------------------ //
// ------------------------------------------------------------------------ //

// ------------------------------------------------------------------------ //
// ------------------------------------------------------------------------ //

function addCompanyPersonItem( context ) {
  var sel_cp = document.getElementById('sel_company_person_' + context );
  var sel_person = "";
  var list = [];

  if ( sel_cp.value == "" ) {
    alert('Wybierz osobę kontaktową.');
    return false;
  }


  for ( var i=0; i<sel_cp.options.length; i++ ) {
    if ( sel_cp.options[i].selected ) {
      sel_person = sel_cp.options[i].text;
      break;
    }
  }

  if ( context == 'tender' ) {
    var contact_obj = document.getElementById('contact');
    sel_person += '\n' + contact_obj.value;
    contact_obj.value = sel_person;
  }
  else
  if ( context == 'iwp_inv' ) {
    var contact_obj = document.getElementById('inv_contact');
    sel_person += '\n' + contact_obj.value;
    contact_obj.value = sel_person;
  }
  else
  if ( context == 'iwp_win' ) {
    var contact_obj = document.getElementById('win_contact');
    sel_person += '\n' + contact_obj.value;
    contact_obj.value = sel_person;
  }
  else
  if ( context == 'it_inv' ) {
    var contact_obj = document.getElementById('inv_person');
    sel_person += '\n' + contact_obj.value;
    contact_obj.value = sel_person;
  }
  else
  if ( context == 'it_contr' ) {
    var contact_obj = document.getElementById('contr_agent');
    sel_person += '\n' + contact_obj.value;
    contact_obj.value = sel_person;
  }
  else
  if ( context == 'it_design' ) {
    var contact_obj = document.getElementById('design_contact');
    sel_person += '\n' + contact_obj.value;
    contact_obj.value = sel_person;
  }

  sel_cp.value = "";

  return false;
}

// ------------------------------------------------------------------------ //

function useCompanyPersonItem( context ) {
  var sel_person = document.getElementById('company_contact_data_' + context ).value;

  if ( sel_person == "" ) {
    alert('Brak danych kontaktowych.');
    return false;
  }

  var list = sel_person.split(', ');

  if ( context == 'tender' ) {
    var contact_obj = document.getElementById('contact');
    sel_person += '\n' + contact_obj.value;
    contact_obj.value = sel_person;
  }
  else
  if ( context == 'iwp_inv' ) {
    var contact_obj = document.getElementById('inv_contact');
    sel_person += '\n' + contact_obj.value;
    contact_obj.value = sel_person;
  }
  else
  if ( context == 'iwp_win' ) {
    var contact_obj = document.getElementById('win_contact');
    sel_person += '\n' + contact_obj.value;
    contact_obj.value = sel_person;
  }
  else
  if ( context == 'it_inv' ) {
    var contact_obj = document.getElementById('inv_person');
    sel_person += '\n' + contact_obj.value;
    contact_obj.value = sel_person;
  }
  else
  if ( context == 'it_contr' ) {
    var contact_obj = document.getElementById('contr_agent');
    sel_person += '\n' + contact_obj.value;
    contact_obj.value = sel_person;
  }
  else
  if ( context == 'it_design' ) {
    var contact_obj = document.getElementById('design_contact');
    sel_person += '\n' + contact_obj.value;
    contact_obj.value = sel_person;
  }

  return false;
}

// ------------------------------------------------------------------------ //

function delCompanyPersonItem( context ) {
  var sel_cp = document.getElementById('sel_company_person_' + context );
  var contact_person = "";

  if ( sel_cp.value == "" ) {
    alert('Wybierz osobę kontaktową.');
    return false;
  }

  for ( var i=0; i<sel_cp.options.length; i++ ) {
    if ( sel_cp.options[i].selected ) {
      contact_person = sel_cp.options[i].text;

      var sel_person = sel_cp.options[i];
      document.peimp.target = 'iframe_' + context;
      document.peimp.make.value = sel_person.value;
      document.peimp.peimp_context.value = context;
      document.peimp.a.value = 'company_person_del_iframe';
      document.peimp.submit();

      document.peimp.target = '_top';
      break;
    }
  }

  if ( contact_person.length == 0 ) {
    return;
  }

  if ( context == 'tender' ) {
    var contact_obj = document.getElementById('contact');
    contact_person += '\n';
    var findstr = new RegExp(contact_person,'g');
    contact_obj.value = contact_obj.value.replace(findstr,"");
  }
  if ( context == 'it_contr' ) {
    var contact_obj = document.getElementById('contr_agent');
    contact_person += '\n';
    var findstr = new RegExp(contact_person,'g');
    contact_obj.value = contact_obj.value.replace(findstr,"");
  }

  return false;
}

// ------------------------------------------------------------------------ //

function delCompanyPersonItemOK( context ) {
  var sel_cp = parent.document.getElementById('sel_company_person_' + context );

  for ( var i=0; i<sel_cp.options.length; i++ ) {
    if ( sel_cp.options[i].selected ) {
      var sel_person = sel_cp.options[i];
      sel_cp.removeChild(sel_person);
      sel_cp.value = "";

      return;
    }
  }

  return false;
}

// ------------------------------------------------------------------------ //

function editCompanyPersonItem( context ) {
  var sel_cp = document.getElementById('sel_company_person_' + context );
  var company_id = "";

  if ( sel_cp.value == "" ) {
    alert('Wybierz osobę kontaktową.');
    return false;
  }

  if ( context == 'tender' ) {
    company_id = document.peimp.organizer_company_id.value;
  }
  else
  if ( context == 'iwp_inv' ) {
    company_id = document.peimp.inv_company_id.value;
  }
  else
  if ( context == 'iwp_win' ) {
    company_id = document.peimp.win_company_id.value;
  }
  else
  if ( context == 'it_inv' ) {
    company_id = document.peimp.inv_company_id.value;
  }
  else
  if ( context == 'it_contr' ) {
    company_id = document.peimp.contr_company_id.value;
  }

  if ( company_id.length == 0 ) {
    alert('Wybierz firmę.');
    return false;
  }

  window.open('','COMPANY_PERSON_EDIT','scrollbars,height=300,width=500,left=200,top=200,resizable=yes').focus();
  document.peimp.target='COMPANY_PERSON_EDIT';
  document.peimp.peimp_context.value = context;
  document.peimp.a.value = 'company_person_edit_popup';
  document.peimp.submit();
  document.peimp.target = '_top';

  return false;
}

// ------------------------------------------------------------------------ //

function editCompanyPersonItemOK( context, cp_id, contact_person ) {
  var sel_cp = opener.document.getElementById('sel_company_person_' + context );
  sel_cp.value = cp_id;

  for ( var i=0; i<sel_cp.options.length; i++ ) {
    if ( sel_cp.options[i].value == cp_id ) {
      sel_cp.options[i].text = contact_person;
      break;
    }
  }

  window.close();

  return false;
}

// ------------------------------------------------------------------------ //

function newCompanyPersonItem( context ) {
  var sel_cp = document.getElementById('sel_company_person_' + context );
  var company_id = "";

  sel_cp.value = "";

  if ( context == 'tender' ) {
    company_id = document.peimp.organizer_company_id.value;
  }
  else
  if ( context == 'iwp_inv' ) {
    company_id = document.peimp.inv_company_id.value;
  }
  else
  if ( context == 'iwp_win' ) {
    company_id = document.peimp.win_company_id.value;
  }
  else
  if ( context == 'it_inv' ) {
    company_id = document.peimp.inv_company_id.value;
  }
  else
  if ( context == 'it_contr' ) {
    company_id = document.peimp.contr_company_id.value;
  }

  if ( company_id.length == 0 ) {
    alert('Wybierz firmę.');
    return false;
  }

  window.open('','COMPANY_PERSON_EDIT','scrollbars,height=300,width=500,left=200,top=200,resizable=yes').focus();
  document.peimp.target='COMPANY_PERSON_EDIT';
  document.peimp.peimp_context.value = context;
  document.peimp.a.value = 'company_person_edit_popup';
  document.peimp.submit();

  document.peimp.target = '_top';

  return false;
}

// ------------------------------------------------------------------------ //

function newCompanyPersonItemOK( context, cp_id, contact_person ) {
  var sel_cp = opener.document.getElementById('sel_company_person_' + context );
  var new_option = opener.document.createElement('option');
  new_option.text = contact_person;
  new_option.value = cp_id;
  try {
    sel_cp.add( new_option, sel_cp.options[0] );
  }
  catch(e) {
    sel_cp.add( new_option, sel_cp.selectedIndex );
  }
  sel_cp.value = cp_id;

  window.close();

  return false;
}

// ------------------------------------------------------------------------ //
// ------------------------------------------------------------------------ //

function open_popup_window() {
  pref='scrollbars,height=' + screen.height + ',width=' + screen.width + ',left=0,top=0,resizable=yes';
  window.open('','POPUP',pref).focus();
  document.peimp.target='POPUP';
  document.peimp.cms_popup.value='cms_popup';
  document.peimp.submit();
  document.peimp.target='_self';
  document.peimp.cms_popup.value='';
}
