/**
* DropDownCheckList.js
* 
* Contains the scripts of the DropDownCheckList control.
*/
function initDropDownCheckList(ddclId, textsId, valuesId, options) {
	try {
		var separator = ', ';
		var textValueSeparator = '|';
		var maximumDropHeight = 200;
		var firstChecksAll = false;
		var width = 150;
		var debug = false;
		if (options.separator != null) {
			separator = options.separator;
		}
		if (options.textValueSeparator != null) {
			textValueSeparator = options.textValueSeparator;
		}
		if (options.maximumDropHeight != null) {
			maximumDropHeight = parseInt(options.maximumDropHeight);
			if (isNaN(maximumDropHeight) || maximumDropHeight < 1) {
				maximumDropHeight = 200;
			}
		}
		if (options.firstItemChecksAll != null && options.firstItemChecksAll) {
			firstChecksAll = true;
		}
		if (options.width != null) {
			width = parseInt(options.width);
			if (isNaN(width) || width < 1) {
				width = 150;
			}
		}
		if (options.debug != null && options.debug) {
			debug = true;
		}

		$('#' + ddclId).dropdownchecklist('destroy');
		var val = $('#' + valuesId).val();
		if (typeof (val) !== 'undefined') {
			$('#' + ddclId).val(val.split(textValueSeparator));
		} else {
			$('#' + ddclId).val(null);
		}
		$('#' + ddclId).dropdownchecklist({
			maxDropHeight: maximumDropHeight,
			firstItemChecksAll: firstChecksAll,
			width: width,
			textFormatFunction: function(options) {
				var firstOption = $(options[0]);
				var selectedOptionObjects = options.filter(':selected');
				var firstChecksAllActivated = false;
				if (this.firstItemChecksAll && firstOption != null && firstOption.attr('selected')) {
					firstChecksAllActivated = true;
				}
				var selectedOptionTexts = new Array();
				var selectedOptionValues = new Array();
				// Extract selected texts and values (excludes first option if 'firstItemChecksAll' option is activated).
				for (var i = firstChecksAllActivated ? 1 : 0; i < selectedOptionObjects.length; i++) {
					selectedOptionTexts.push(selectedOptionObjects[i].text);
					selectedOptionValues.push(selectedOptionObjects[i].value);
				}
				var currentTexts = new Array();
				var currentValues = new Array();
				// Retrieve previously selected texts and values.
				var texts = $('#' + textsId).val().split(textValueSeparator);
				var values = $('#' + valuesId).val().split(textValueSeparator);
				// Add existing previous selected texts and values.
				for (var j = 0; j < values.length; j++) {
					if ($.inArray(values[j], selectedOptionValues) > -1) {
						currentTexts.push(texts[j]);
						currentValues.push(values[j]);
					}
				}
				// Add new selected texts and values.
				for (var k = 0; k < selectedOptionValues.length; k++) {
					if ($.inArray(selectedOptionValues[k], currentValues) == -1) {
						currentTexts.push(selectedOptionTexts[k]);
						currentValues.push(selectedOptionValues[k]);
					}
				}
				// Save selected texts and values for postback events.
				$('#' + textsId).val(currentTexts.join(textValueSeparator));
				$('#' + valuesId).val(currentValues.join(textValueSeparator));
				return currentTexts.join(separator);
			}
		});
	} catch (e) {
		if (debug) {
			var message = 'DropDownCheckList initialization error: ' + e.message;
			if (typeof (console) != 'undefined') {
				console.error(message);
			} else {
				alert(message);
			}
		}
	}
}

