/**
 * Removes all options from a list, and optionally adds a default set of options to the list. It
 * tries to set the selected option to the previous value or to a certain value if specified.
 *
 * The function returns the value of the list option which was selected before.
 *
 * selectId:	The if of the HTML <select ...> element to manipulate.
 * defaultOptions:	The array of objects used to add the default select options. Each object in
 *		the array has to contain a value and a text property to fill in the HTML <option>
 *		element's value and text, respectively.
 * 		e.g.
 * 		var optionArray = [ { value:'-1', text:'Please choose' } ];
 * defaultValue:	The default value to set after resetting the select list.
 */
function resetList(selectId, defaultOptions, defaultValue) {
	var selectedValue = dwr.util.getValue(selectId);
	dwr.util.removeAllOptions(selectId);
	
	if (defaultOptions) {
		dwr.util.addOptions(selectId, defaultOptions, "value", "text");
		
		if (defaultValue) {
			dwr.util.setValue(selectId, defaultValue);
		} else {
			dwr.util.setValue(selectId, selectedValue);
		}
	}
	
	return selectedValue;
}

function enable() {
	for (var i = 0; i < arguments.length; i++) {
		var e = document.getElementById(arguments[i]);
		if (e != null) {
			e.removeAttribute('disabled');
		} else {
			// radio buttons
			e = document.getElementsByName(arguments[i]);
			for (var j = 0; j < e.length; j++) {
				e[j].removeAttribute('disabled');
			}
		}
	}
}

function disable() {
	for (var i = 0; i < arguments.length; i++) {
		var e = document.getElementById(arguments[i]);
		if (e != null) {
			e.setAttribute('disabled', 'disabled');
		} else {
			// radio buttons
			e = document.getElementsByName(arguments[i]);
			for (var j = 0; j < e.length; j++) {
				e[j].setAttribute('disabled', 'disabled');
			}
		}
	}
}

function show() {
	for (var i = 0; i < arguments.length; i++) {
		var e = document.getElementById(arguments[i]);
		e.style.display = '';
	}
}

function hide() {
	for (var i = 0; i < arguments.length; i++) {
		var e = document.getElementById(arguments[i]);
		e.style.display = 'none';
	}
}

/**
 * Returns the caret position in a text input field.
 */
function getCaretPos(textInput) {
	if (document.selection) {
		// IE
		// To get cursor position, get empty selection range
		var range = document.selection.createRange();
		
		// Move selection start to 0 position
		range.moveStart('character', -textInput.value.length);
		
		// The caret position is selection length
		return range.text.length;
	} else if (textInput.selectionStart || textInput.selectionStart == '0') {
		// Gecko
		return textInput.selectionStart;
	} else {
		// we don't know
		return -1;
	}
}

/**
 * Allows entering only a date in a text input. The date format is yyyy.MM.dd.
 *
 * The function has to be added to an input element as the keypress event handler via Javascript.
 * Failing to do so will prevent the event handling mechanism to work. 
 *
 * document.getElementById('xxx').onkeypress = dateMask;
 */
function dateMask(e) {
	// enter in IE
	if (!e && event.keyCode == 13) return true;

	var code = e ? e.charCode : event.keyCode;
	// non-printable character
	if (code == 0) return true;

	var v = this.value;
	if (v.length == 10) return false;

	var c = String.fromCharCode(code);
	// allow 19yy.mm.dd - 20yy.mm.dd only
	switch (getCaretPos(this)) {
		// year
		case 0:
			if (c != '1' && c != '2') return false;
			break;
		case 1:
			if (v.charAt(0) == '1' && c != '9') return false;
			if (v.charAt(0) == '2' && c != '0') return false;
			break;
		case 2:
		case 3:
			if (c < '0' || c > '9') return false;
			break;
		case 4:
			if (c != '.') return false;
			break;
		// month
		case 5:
			if (c != '0' && c != '1') return false;
			break;
		case 6:
			if (v.charAt(5) == '0' && (c < '1' || c > '9')) return false;
			if (v.charAt(5) == '1' && (c < '0' || c > '2')) return false;
			break;
		case 7:
			if (c != '.') return false;
			break;
		// day
		case 8:
			if (c < '0' || c > '3') return false;
			break;
		case 9:
			if (v.charAt(8) == '3' && (c < '0' || c > '1')) return false;
			if (c < '0' || c > '9') return false;
			break;
		default:
			return false;
			break;
	}
	
	return true;
}
