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

/* remember to include util.js before */
/* remember to include validate.js before */

var srcinsert_compval_split_re = /^(\d{0,10})_(\d{4,4})_(\d{1,2})_(\d{1,2})(?:_(\d{4,4})_(\d{3,4})_(\d{1,6}))?$/;
/*
 * config consts of:
 * form name
 * name of selected source list field
 * name of source dictionary select
 */
var srcinsert_configs = new Object;

function srcinsert_new(siKey,selSelect,srcSelect,bzpYearSelect,bzpNumberText,bzpPositionText,activationIds) {
  var config = new Array(4);
  config[0] = siKey;
  config[1] = selSelect;
  config[2] = srcSelect;
  /* now BZP related things */
  config[3] = bzpYearSelect;
  config[4] = bzpNumberText;
  config[5] = bzpPositionText;
  var actObj = new Object;
  var ids = activationIds.split(",");
  if (ids.length == 1 && ids[0] == "") {
    /* ignore */
  }
  else {
    for (i = 0; i < ids.length; i++) {
      ids[i]++; ids[i]--;
      actObj['id_'+ids[i]] = '1';
    }
  }
  config[6] = actObj;
  srcinsert_configs[siKey] = config;
}

function srcinsert_new_simple(siKey,selSelect,srcSelect) {
  var config = new Array(4);
  config[0] = siKey;
  config[1] = selSelect;
  config[2] = srcSelect;
  srcinsert_configs[siKey] = config;
}


function srcinsert_actcheck(siKey) {
  var config = srcinsert_configs[siKey];
  if (!config) {
    alert('No config at '+siKey);
    return;
  }
  selValue = sval(elid(config[2]));
  selValue++;selValue--;
  if (config[6]['id_'+selValue]) {
    elid(config[3]).disabled = false;
    elid(config[4]).disabled = false;
    elid(config[5]).disabled = false;
  }
  else {
    elid(config[3]).disabled = true;
    elid(config[4]).disabled = true;
    elid(config[5]).disabled = true;
  }
}

function srcinsert_getopttext(sel,val) {
  var i;

  for (i = 0; i < sel.options.length; i++) {
    if (sel.options[i].value == val) return sel.options[i].text;
  }

  return 0;
}

function srcinsert_computename(compval,sel) {
  var id;
  var year;
  var month;
  var day;
  var sname;

  var m = srcinsert_compval_split_re.exec(compval);
  if (m) {
    id = m[1];
    year = m[2];
    month = m[3];
    day = m[4];
    bzp_year = m[5];
    bzp_no = m[6];
    bzp_pos = m[7];

    /* find source name */
    sname = srcinsert_getopttext(sel,id);
    if (!sname) {
      sname = "(nazwa źródła nieznana)";
    }

    month++; month--;
    if (month < 10) month = '0'+month;
    day++; day--;
    if (day < 10) day = '0'+day;

    if (bzp_year) {
      return ''+year+'.'+month+'.'+day+' - '+sname+' '+bzp_year+'/'+bzp_no+', poz. '+bzp_pos;
    }
    else {
      return ''+year+'.'+month+'.'+day+' - '+sname;
    }
  }
  else {
    return "(zły format klucza źródła)";
  }
}

/* fill selSelect names according to values */
function srcinsert_fillnames(siKey) {
  var config = srcinsert_configs[siKey];
  if (!config) {
    alert('SRCINSERT: no key ['+siKey+']');
    return;
  }

  var selsel = document.getElementById(config[1]);
  if (!selsel) {
    alert('SRCINSERT: no element with id ['+config[1]+']');
    return;
  }
  var srcsel = document.getElementById(config[2]);
  if (!srcsel) {
    alert('SRCINSERT: no element with id ['+config[2]+']');
    return;
  }

  var i;
  for (i = 0; i < selsel.options.length; i++) {
    selsel.options[i].text = srcinsert_computename(
        selsel.options[i].value,
    srcsel);
  }
}

function srcinsert_writecompval(id,year,month,day) {
  return ''+id+'_'+year+'_'+month+'_'+day;
}

function srcinsert_writecompval_bzp(id,year,month,day,bzp_year,bzp_no,bzp_pos) {
  return ''+id+'_'+year+'_'+month+'_'+day+'_'+integer_to_string(bzp_year,4)+
      '_'+integer_to_string(bzp_no,3)+
      '_'+bzp_pos;
}

function srcinsert_addsrc(siKey,id,year,month,day) {

  if (!id) {
    alert('Wybierz rodzaj źródła.');
    return;
  }

  if (!year) {
    alert('Podaj rok publikacji.');
    return;
  }

  if (!month) {
    alert('Podaj miesiąc publikacji.');
    return;
  }

  if (!day) {
    alert('Podaj dzień publikacji.');
    return;
  }

  month++; month--;
  if (month < 10) month = '0'+month;
  day++; day--;
  if (day < 10) day = '0'+day;

  if (!date_verify(year,month,day)) {
    alert('Podana data '+year+'-'+month+'-'+day+' jest niepoprawna.');
    return;
  }

  var config = srcinsert_configs[siKey];
  if (!config) {
    alert('SRCINSERT: no key ['+siKey+']');
    return;
  }

  /* check if additional fields should be verified */
  var actObj = config[6];
  if (actObj['id_'+id]) {
    
    if (!js_validate_value_regex(sval(elid(config[3])),/^\d{4,4}$/,'Podaj poprawny rok BZP.')) {
      return;
    }
    
    if (!js_validate(config[4],'text',/^\d{1,3}$/,'Podaj poprawny numer BZP.',true)) {
      return;
    }
    
    if (!js_validate(config[5],'text',/^\d{1,6}$/,'Podaj poprawny numer pozycji BZP.',true)) {
      return;
    }
  }
  
  var selsel = document.getElementById(config[1]);
  if (!selsel) {
    alert('SRCINSERT: no element with id ['+config[1]+']');
    return;
  }
  var srcsel = document.getElementById(config[2]);
  if (!srcsel) {
    alert('SRCINSERT: no element with id ['+config[2]+']');
    return;
  }

  var i;
  for (i = 0; i < selsel.options.length; i++) {
    var m = srcinsert_compval_split_re.exec(selsel.options[i].value);
    if (m) {
      if (m[1] == id) {
        alert('Takie źródło już istnieje!');
        return;
      }
    }
  }

  if (actObj['id_'+id]) {
    val = srcinsert_writecompval_bzp(id,year,month,day,
    sval(elid(config[3])),elid(config[4]).value,elid(config[5]).value);
  }
  else {
    val = srcinsert_writecompval(id,year,month,day);
  }
  var name = srcinsert_computename(val,srcsel);

  selsel.options[selsel.options.length] = new Option(name,val,false,false);
}

function srcinsert_addsrc_shortdate(siKey,id,shortDate) {
  var date_yymmdd_re = /^(\d{2,2})(\d{2,2})(\d{2,2})$/;

  if (!js_validate_value_date_yymmdd(shortDate,'Podaj poprawną datę publikacji źródła.')) {
    return;
  }
  var m = safe_match(date_yymmdd_re,shortDate);
  if (!m) {
    return;
  }
  return srcinsert_addsrc(
      siKey,
      id,
      date_from_short_format(m[1]),
      m[2],
      m[3]);
}


function srcinsert_submitprep() {
  var i;
  var j;
  var selsel;

  for (siKey in srcinsert_configs) {
    selsel = document.getElementById(srcinsert_configs[siKey][1]);
    if (selsel) {
      for (j = 0; j < selsel.options.length; j++) {
        selsel.options[j].selected = true;
      }
    }
  }

}


function srcinsert_pp() {
//  if ( (sval(elid("source"))) == 445 ) /* ogl z internetu - Ugly ! */
//    document.peimp.pp_position.disabled=false;
//  else
//    document.peimp.pp_position.disabled=true;
}
