// $Name:  $
// $Header: /home/cvs/DC5/dc5_barchasp/www/js/dc_main.js,v 1.4 2007/07/13 15:58:37 thoms Exp $

var dialoglayer;


// This function is automatically called by CBE's window.onload function

function windowOnload()
{ // Initializing the dialog layer

  dialoglayer = document.getElementById('dialoglayer').cbe;
  dialoglayer.resizeTo(600, 500);
  dialoglayer.moveTo('n', 0, true);

  // Initializing the main menu

  if (document.all && document.getElementById)
	{ var navRoot = document.getElementById('dcMainNavMenu');

	  if (navRoot)
		{ for (i = 0; i < navRoot.childNodes.length; i++)
			{ var node = navRoot.childNodes[ i ];

			  if (node.nodeName == 'LI')
				{ node.onmouseover = function()
					{ this.className += ' over';
					  DC_Main_PopupVisibility('off');
					}

				  node.onmouseout = function()
					{ this.className = this.className.replace(' over', '');
					  DC_Main_PopupVisibility('on');
					}
				}
			}
		}
	}
}


function DC_Main_PopupVisibility(mode)
{ // The non-vanishing select-box is an Internet Explorer bug.
  // this is a bit dirty workaround: set visibility of affected box
  // to 'hidden' when popup div appears.

  if (browser_type != 'IE')
	return;

  for (var j = 0; j < document.forms.length; j++)
	for (var i = 0; i < document.forms[ j ].length; i++)
	  { var sfield = document.forms[ j ].elements[ i ];

		if (sfield.type == 'select-one')
		  sfield.className = mode;
		else if (sfield.type == 'select-multiple')
		  sfield.className = mode;
	  }
}


function DC_Main_InitWindowName(win)
{ if (win.name.substr(0, 6) != 'dc5win')
	{ var now = new Date();
	  win.name = 'dc5win' + now.getTime();
	  return 1;
	}

  return 0;
}


function DC_Main_WriteWindowName(win)
{ for (i = 0; i < document.forms.length; i++)
	if (document.forms[ i ].elements[ 'window_name' ])
	  document.forms[ i ].elements[ 'window_name' ].value = win.name;
}


function DC_Main_HideMethodWin()
{ dialoglayer.slideTo( dialoglayer.offsetLeft(), -1200, 500);
  dialoglayer.moveTo('n',0 , true);
}


function DC_Main_ShowCalendar(x,y,form_name,field,dialogmode)
{ var value = document.forms[ form_name ].elements[ field ].value;

  // XXX clean this up
  var url =
	DC_WWW
	+ 'assistant/calendar.php?form=' + encodeURIComponent(form_name)
	+ '&field=' + encodeURIComponent(field)
	+ '&value=' + encodeURIComponent(value);

  DC_Dialog_Open(dialogmode, x, y, false, url);
}


function DC_Main_ShowSaveQuery(x,y,form_name,field,dialogmode,pageid)
{ var value = document.forms[ form_name ].elements[ field ].value;

  // XXX clean this up
  var url =
	DC_WWW
	+ 'savequery.php?form=' + encodeURIComponent(form_name)
	+ '&field='  + encodeURIComponent(field)
	+ '&value='  + encodeURIComponent(value)
	+ '&pageid=' + encodeURIComponent(pageid);

  DC_Dialog_Open(dialogmode, x, y, false, url);
}


function DC_Main_OpenAssistant(dialogmode, dlg_width, dlg_height, winoptions, assistant_url, form_name, field_name, querystr)
{ val = '';

  if (document.forms[ form_name ].elements[ field_name ].value)
	val = document.forms[ form_name ].elements[ field_name ].value;

  url =
	DC_WWW + assistant_url +
	'?env[form_name]=' + encodeURIComponent(form_name) +
	'&env[field_name]=' + encodeURIComponent(field_name) +
	'&env[value]=' + encodeURIComponent(val) +
	'&env[callback]=DC_Main_AssistantCallback' +
	'&' + querystr;

  DC_Dialog_Open(dialogmode, dlg_width, dlg_height, winoptions, url);
}


function DC_Main_AssistantCallback(env, values, labels, opt)
{ var element = document.forms[ env[ 'form_name' ] ].elements[ env[ 'field_name' ] ];

  // Split thesaurus hierarchy values into multiple target fields

  var do_hierarchy = false;

  if ((opt[ 'hierarchy_field0' ]) && (opt[ 'hierarchy_separator' ]))
	if (! opt[ '_no_hierarchy_' ])
	  do_hierarchy = true;

  if (do_hierarchy)
	{ DC_Main_AssistantCallbackHierarchy(env, values, labels, opt);

	  return;
	}

  // Fill values into HTML form element

  var i = 0;

  if ((element.type == 'select-multiple') || (element.type == 'select-one'))
	{ for (i = 0; i < values.length; i++)
		{ if (document.all)
			position = element.length;
		  else
			position = null;

		  var entry = document.createElement('option');
		  entry.value = values[ i ];
		  entry.text  = labels[ i ];
		  entry.selected = true;

		  element.add(entry, position);
		}
	}
  else
	{ if (! opt[ 'multi_separator' ])
		opt[ 'multi_separator' ] = '; ';

	  // XXX hack
	  if (opt[ 'multi_separator' ] == 'NEWLINE')
		opt[ 'multi_separator' ] = '\n';

	  if (! opt[ 'clean_before_apply' ])
		{ // Empty string evaluates to false, catch it here
		  if (opt[ 'clean_before_apply' ] != '')
			opt[ 'clean_before_apply' ] = 1;
		}

	  if (opt[ 'clean_before_apply' ] == 1)
		element.value = '';

	  for (i = 0; i < values.length; i++)
		{ if (element.value != '')
			element.value = element.value + opt[ 'multi_separator' ];

		  element.value = element.value + values[ i ];
		}
	}
}


function DC_Main_AssistantCallbackHierarchy(env, values, labels, opt)
{ var i = 0;

  // newenv = env makes a reference, not a copy! Copy manually...

  var newenv = new Array;
  for (i in env) newenv[ i ] = env[ i ];

  var newopt = new Array;
  for (i in opt) newopt[ i ] = opt[ i ];

  newopt[ '_no_hierarchy_' ] = true;

  // Build values array by hierarchy level

  var newvalues = new Array;

  for (i = 0; i < values.length; i++)
	{ var tmp = values[ i ].split(opt[ 'hierarchy_separator' ]);

	  for (j = 0; j < tmp.length; j++)
		{ if (newvalues.length <= j)
			newvalues[ j ] = new Array;

		  newvalues[ j ][ newvalues[ j ].length ] = tmp[ j ];
		}
	}

  // Build labels array by hierarchy level

  var newlabels = new Array;

  for (i = 0; i < labels.length; i++)
	{ var tmp = labels[ i ].split(opt[ 'hierarchy_separator' ]);

	  for (j = 0; j < tmp.length; j++)
		{ if (newlabels.length <= j)
			newlabels[ j ] = new Array;

		  newlabels[ j ][ newlabels[ j ].length ] = tmp[ j ];
		}
	}

  // Call this function again, once for each hierarchy level

  for (i = 0; i < 4; i++)
	{ if (! newvalues[ i ]) newvalues[ i ] = new Array;
	  if (! newlabels[ i ]) newlabels[ i ] = new Array;

	  if (! opt[ 'hierarchy_field' + i ])
		break;

	  newenv[ 'field_name' ] = opt[ 'hierarchy_field' + i ];

	  DC_Main_AssistantCallback(newenv, newvalues[ i ], newlabels[ i ], newopt);
	}
}


function DC_Main_GetSelectedText()
{ txtselected = '';

  if (window.getSelection)
	txtselected = window.getSelection();
  else if(document.getSelection)
	txtselected = document.getSelection();
  else if(document.selection)
	txtselected = document.selection.createRange().text;

  return txtselected;
}


function DC_Main_RemoveSelectedOptions(form_name, myelement)
{ var f = document.forms[ form_name ];

  if (! f)
	return;

  var e = f.elements[ myelement ];

  if (! e)
	return;

  // Convenience hack: Make this work for text and textarea fields as well
  // (can be useful for read-only text fields)

  if ((e.type == 'text') || (e.type == 'textarea'))
	e.value = '';
  else if ((e.type == 'select-multiple') || (e.type == 'select-one'))
	{ len = e.length - 1;

	  for (i = len; i >= 0; i--)
		if (e.options[ i ].selected)
		  e.remove(i);
	}
}


function DC_Main_SelectAllOptions(form_name, myelements)
{ var f = document.forms[ form_name ];

  if (! f)
	return;

  // In myelements, accept form field name (string) or array of form field names

  if (typeof(myelements) == 'string')
	{ tmp = myelements;
	  myelements = new Array;
	  myelements[ 0 ] = tmp;
	}

  len = myelements.length;

  for (i = 0; i < len; i++)
	{ var e = f.elements[ myelements[ i ] ];

	  if (e)
		{ l = e.length;

		  for (j = 0; j < l; j++)
			{ e.options[ j ].selected = true;
			}
		}
	}
}


function DC_Main_FocusFirstElement(form_name)
{ cnt = document.forms[ form_name ].elements.length;

  for (i = 0; i < cnt; i++)
	// Filter out <fieldset>
	if (document.forms[ form_name ].elements[ i ].type)
	  // Filter out hidden and disabled fields
	  if ((document.forms[ form_name ].elements[ i ].type != 'hidden') && (! document.forms[ form_name ].elements[ i ].disabled))
		{ document.forms[ form_name ].elements[ i ].focus();
		  break;
		}
}


var dc_main_onformsubmit = new Array;

function DC_Main_RegisterOnFormSubmit(form_name, code)
{ len = dc_main_onformsubmit.length;

  dc_main_onformsubmit[ len ] = new Array;
  dc_main_onformsubmit[ len ][ 0 ] = form_name;
  dc_main_onformsubmit[ len ][ 1 ] = code;
}


function DC_Main_RunOnFormSubmit(form_name)
{ len = dc_main_onformsubmit.length;
  code = '';

  for (i = 0; i < len; i++)
	if (dc_main_onformsubmit[ i ][ 0 ] == form_name)
	  code = code + ' ' + dc_main_onformsubmit[ i ][ 1 ];

  if (code != '')
	{ // eval() seemed to work only once in this function, that's why we had
	  // to concatenate all statements instead of eval()ing them one by one

	  eval(code);
	}

  return true;
}


function DC_Main_OpenDossierDetails(objectid, pos, resultset, hide_header, hide_contextmenu)
{ var parts = objectid.split('~');

  var detail_href =
	DC_WWW + 'archives/' + parts[ 0 ] + '/view/' + parts[ 1 ] +
	'?pos=' + escape(pos);

  if (typeof hide_header == "undefined")
    var hide_header = true;

  if (typeof hide_contextmenu == "undefined")
    var hide_contextmenu = false;

  if (hide_header)
	detail_href = detail_href + '&hide_header=1';
  
  if (hide_contextmenu)
	detail_href = detail_href + '&hide_contextmenu=1';
    
  detail_href = detail_href + '&resultset=' + escape(resultset);

  var detail_win = window.open(detail_href, 'dc_dialog_details', 'resizable=yes,scrollbars=yes,width=900,height=600');
  detail_win.focus();
}


function DC_Main_SelectPrompt_AddOption(listbox, prompt_value, prompt_message)
{ if (listbox.value != prompt_value)
    return;

  var str = prompt(prompt_message, '');

  if (str == '')
    return;

  var new_option = document.createElement('option');

  new_option.value = str;
  new_option.text  = str;
  new_option.selected = true;

  if (document.all)
    position = listbox.length;
  else
    position = null;

  listbox.add(new_option, position);
}

// Eigene Trim()-Funktion fuer JS
function DC_Main_JStrim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

// Prueft gueltige Eingabe fuer Jahr
function DC_Main_CheckYear(obj, inf)
{
  a=document.getElementById(obj.id);

  if (DC_Main_JStrim(a.value) != '')
  {
    if (a.value == parseInt(a.value))
    { if (a.value < 1000 || a.value > 9999)
        alert(inf);
    }
    else
      alert(inf);
  }
}
