/***********************************************************
**                 Needed datastruct                      **
************************************************************
**                                                        **
** An array struct like:                                  **
** countyMunicipal = new Array(                           **
**     new Array(                                         **
**         'county table id',                             **
**         'county name / text',                          **
**         new Array(                                     **
**             new Array(                                 **
**                 'municipal table id',                  **
**                 'municipal name'                       **
**             ), new Array(                              **
**             ..                                         **
**             )                                          **
**         )                                              **
**     ), new Array (                                     **
**     ..                                                 **
**     )                                                  **
** );                                                     **
**                                                        **
** Another array stryct like:                             **
** countyMunicipalConfig = new Array(                     **
**     'name of the form',                                **
**     'name for the county select',                      **
**     'name for the municipal select'                    **
** );                                                     **
**                                                        **
************************************************************

************************************************************
**           Function constructMunicipal                  **
************************************************************
**                                                        **
** Input:                                                 **
**     form name,                                         **
**     county select name,                                **
**     municipal select name                              **
**                                                        **
** Output:                                                **
**     returns nothing, changes the content in the        **
**     municipal select.                                  **
**                                                        **
***********************************************************/

function constructMunicipal(cid, mid) {
	var f = countyMunicipalConfig[0];
	var cs = countyMunicipalConfig[1];
	var ms = countyMunicipalConfig[2];
	var cslength = 0;

	eval('cslength = document.' + f + '.' + cs + '.options.length;');

	if ( cid <= 0 || !cid ) {
		var csid = 0;
		var csids = 0;
		eval('csids = document.' + f + '.' + cs + '.selectedIndex;');

		if ( csids > 0 )
			eval('csid = document.' + f + '.' + cs + '.options[csids].value;');

		cid = csid;
	}

	if ( parseInt(cid) <= 0 || parseInt(cslength) <= 1 )
		return;

	eval('document.' + f + '.' + ms + '.options.length = 0;');

	for ( var i = 0 ; i < countyMunicipal.length ; i++ ) {
		if ( parseInt(countyMunicipal[i][0]) == parseInt(cid) ) {
			for ( var j = 0 ; j < countyMunicipal[i][2].length ; j++ )
				eval('document.' + f + '.' + ms + '.options[document.' + f + '.' + ms + '.options.length] = new Option("' + countyMunicipal[i][2][j][1] + '", ' + countyMunicipal[i][2][j][0] + ( countyMunicipal[i][2][j][0] == mid ? ', true' : '' ) + ');');
		}
	}
}

function constructCounty(id) {
	var f = countyMunicipalConfig[0];
	var cs = countyMunicipalConfig[1];
	var ms = countyMunicipalConfig[2];

	if ( countyMunicipal.length <= 0 )
		return;

	eval('document.' + f + '.' + cs + '.options.length = 0;');

	if ( parseInt(id) > 0 )
		eval('document.' + f + '.' + ms + '.options.length = 0;');

	for ( var i = 0 ; i < countyMunicipal.length ; i++ )
		eval('document.' + f + '.' + cs + '.options[document.' + f + '.' + cs + '.options.length] = new Option("' + countyMunicipal[i][1] + '", ' + countyMunicipal[i][0] + ( countyMunicipal[i][0] == id ? ', true' : '' ) + ');');

	if ( parseInt(id) <= 0 || !id ) {
		eval('document.' + f + '.' + cs + '.options[0].selected = true;');
		eval('id = document.' + f + '.' + cs + '.selectedIndex;');
	}

	return id;
}
