// This script provides functions/listeners for updating a page as a result of 
// 	category select/deselect,
// 	sector select/deselect,
// 	subsector select/deselect
//
//	update_type: 	
//			1 ... if category 	 	 selected/deselected
//			2 ... if sector   	 	 selected/deselected
//			3 ... if subsector       selected/deselected
//			4 ... if subsector_entry selected/deselected

// this method is called when a category is selected/deselected and 
// follows another onChange method category_changed(...) - search_form.thtml (jobs)
//														   

function get_sectors(elem, type, site, categories4site) {

		// if the session runs out, then no site is supplied => don't bother about 
		// sectors

		if (! site) return;

		// testing which of the selected categories are valid
		var allowed_cats = [];

		var selected_cats = $F(elem);
        var s_item;
		
	    selected_cats.each(
	        function(s_item) {

				var valid_cats = categories4site.split(',');
		        var v_item;
			    valid_cats.each(
			        function(v_item) {
						 if( s_item == v_item ) {
						 	allowed_cats.push('cat=' + s_item);
						 }
					}
				);
			}
		);

		// do nothing if none of the site related categories was selected
		if ( allowed_cats.length < 1 ) {
			$('sector_data').innerHTML = '';
			return;
		}

		var params = "update_type=1&site="  + site; 
		if ( allowed_cats.length > 0 ) params = params + '&';
		params = params + allowed_cats.join('&');

		if (type == 'posting') params = params + '&type=posting';

		new Ajax.Updater(
			'sector_data',
			'/get_sector_data.cgi', 
			{ 
				method:        'get',
				parameters: 	params,
				onComplete:	function() {
					var elem = $('sectors');
					var select_sectors = $F(elem);
					if (select_sectors.size() < 1) {
						elem.value = 1;
						get_subsectors(elem);
					}
				}
			}
		);
}

function get_sectors4checkbox( elem, type, site, dom_type, categories4site ) {

		// if the session runs out, then no site is supplied => don't bother about 
		// sectors
		if (! site) return;

		var select_cat = $F(elem);

		// to be able to reuse the code for accessing database sector data
		// we need to map the checked checkboxes to the same datastructure
		// as is used in the method get_sectors(elem) (see above)

		var valid_cats = categories4site.split(',');

		// on category unselect
		if (select_cat != null) {

			// test which selected categories can be sent through
			var selected_cats;
			if ( $(elem).name == 'category' ) {
				selected_cats = $A(document.getElementsByName('category'));
			} else if ( $(elem).name == 'categories' ) {
				selected_cats = $A(document.getElementsByName('categories'));
			}
			
	        var s_item, v_item;

			// test if we need to remove the sector menus
			var still_selected = 1;
		    selected_cats.each(
		        function(s_item) {
				    valid_cats.each(
				        function(v_item) {
							 if( s_item.value == v_item && ! s_item.checked) {
					 			still_selected = 0;
							 }
						}
					);
				}
			);

			var allowed_cat = '';
			// ids of selected categories
			//selected_cats = $F(elem).split(','); 

	    	valid_cats.each(
	        	function(v_item) {
					if( v_item == select_cat ) allowed_cat = select_cat;
				}
			);
			

			// remove the sector menus if no valid categories are selected
			if ( ! still_selected ) $('sector_data').innerHTML = '';

			// do not try to get the sectors if no valid category are selected
			if ( ! allowed_cat ) return;

			var params = "update_type=1&site="  + site + 
						 "&dom_type=" + dom_type;

			// posting pages have different display of sector data
			if (type == 'posting') params = params + "&type=posting&";
			params = params + '&cat=' + allowed_cat;

			//params = params + "&cat=" + select_cat;
			new Ajax.Updater(
				'sector_data',
				'/get_sector_data.cgi', 
				{ 
					method:        'get',
					parameters: 	params
				}
			);

		// on category select
		} else {

			// check if the selected category was deselected
			// and remove the sector data if it was

			var selected_cats;
			if ( $(elem).name == 'category' ) {
				selected_cats = $A(document.getElementsByName('category'));
			} else if ( $(elem).name == 'categories' ) {
				selected_cats = $A(document.getElementsByName('categories'));
			}
			
			//var cat     = $F('select_cat');
			var still_selected = 1;
	        var s_item;
		    selected_cats.each(
		        function(s_item) {

				var valid_cats = categories4site.split(',');
				var v_item;
			    valid_cats.each(
			        function(v_item) {
						 if( s_item.value==v_item && !s_item.checked ) {
						 	still_selected = 0;
						 }
					});

				}
			);

			if ( ! still_selected ) {
				$('sector_data').innerHTML = '';
			}
		}
		

}

	// on sector change
	
function get_subsectors(elem, type) {

		// store the selected sector ids
		var sids = $F(elem).join(',');
		$('sids').value  = sids;
		$('seids').value = '';

		// Secretarial sectors do not have subsectors => no Ajax call needed
		if ($F('select_cat') == 2) return;
		
		// populate subsectors only if one sector was selected
		if ( $F(elem).length > 1 )  {
			$('subsectors_exp').innerHTML = '' ;
			$('subsectors_pa').innerHTML = '' ;
			return; 
		}
			
		var params;

		// only one category per site that will have associated sectors
		var cat  = $F('select_cat');

		params = $('sectors').serialize(true) + 
				 '&' + $('select_cat').serialize(true) +
			     //"&site_id=" + $F('site_id') +
			     "&update_type=2";
	    //params = params + '&select_cats=' + $F('select_cats');

		new Ajax.Updater(
			'subsector_data',
			'/get_sector_data.cgi', 
			{ 
				method: 	'get',
				parameters: 	params
			}
		);

}
	// on sector change
	
function get_subsectors_checkbox(elem, sector) {

		// check if the selected sector was deselected
		// and remove the subsector data if it was
		var theList = $A(document.getElementsByName('sectors'));
		var still_selected = 1;
        var item;
		var selected_sectors = [];
	    theList.each(
	        function(item) {
				 if( sector == item.value && !item.checked ) {
				 	still_selected = 0;
				 }

				 if (item.checked) {
				 	selected_sectors.push(item.value);
				 }
			});

		if ( ! still_selected ) {
		
			$('subsector_data_' + sector).innerHTML  = '';
		}
		
		// make sure only a limited number of sectors  is selected
		// -------------------------------------
		if ( $F('select_cat') == 24 && selected_sectors.length > 2 ) {
			$(elem).checked = false;
			alert( "Only two sectors can be selected at a time." );
			return;
		}
//		if ( $F('select_cat') == 2 && selected_sectors.length > 5 ) {
//			$(elem).checked = false;
//			alert( "Only five sectors can be selected at a time." );
//			return;
//		}
		
		// store the selected sector ids
		var sids = selected_sectors.join(',');
		$('sids').value = sids;

		// Secretarial sectors do not have subsectors => no Ajax call needed
		if ($F('select_cat') == 2) return;
		
		// populate subsectors only if up to two sectors were selected
		if ( $F('select_cat') == 24 && selected_sectors.length > 2 )  {
			return; 
		}
			
		var params;

		// only one category per site that will have associated sectors
		//var cat  = $F('select_cat');

		params = $(elem).serialize(true) + 
				 '&dom_type=checkbox&' + $('select_cat').serialize(true) +
			     //"&site_id=" + $F('site_id') +
			     "&update_type=2&sector=" + sector;

		new Ajax.Updater(
			'subsector_data_' + sector,
			'/get_sector_data.cgi', 
			{ 
				method: 	'get',
				parameters: 	params
			}
		);

}

// on subsector entry change
// (update the subsector_entry_ids element that will be used for adding/updating Jobs/CVs
function get_subsector_entry_ids(elem, dom_type) {

		var select_cats, i, length, exp, pa, seids, seids_exp, seids_pa;
    	// store the selected sector ids
		if ( dom_type == 'checkbox' ) {

			var sectors = [];
			sectors = $F('sids').split(','); 

	        var sid;
			var seids = '';
		    sectors.each(
		        function(sid) {
						if ($('subsectors_exp_' + sid)) 
									seids = $F('subsectors_exp_' + sid).join(',') + ',' +
											seids;
						if ($('subsectors_pa_' + sid)) 
									seids = $F('subsectors_pa_' + sid).join(',') + ',' +
											seids;
				});
		} else {
		
			seids_exp = '';
			seids_pa  = '';
			if ( $('subsectors_exp') ) seids_exp = $F('subsectors_exp').join(',');
			if ( $('subsectors_pa') )  seids_pa  = $F('subsectors_pa').join(',');
			seids = seids_pa + ',' + seids_exp;
		}

		seids.replace(/,,/g, ',');
		seids.replace(/^,/, '');
		seids.replace(/,$/, '');
		$('seids').value = seids;

}

