var DECIMAL_SEPARATOR = "\\,";
var THOUSAND_SEPARATOR = "\\.";
var DATE_SEPARATOR = "\\.";

function getForm() {
	return document.forms[0];
}


// -------------------------------------------------------------------------
// ----- Number Fields -----------------------------------------------------
// -------------------------------------------------------------------------
var oldNumberFieldsValues = new Array();

function checkNumberField(field, allowFloat, allowNegative) {
	var txt = getForm().elements[field].value;
	var search = eval("/" + THOUSAND_SEPARATOR + "/");
	if ((!_isValidNumberFormat(field, allowFloat, allowNegative)) && (txt.length > 0)) {
		getForm().elements[field].value = oldNumberFieldsValues[field];
	}
}

function storeNumberField(field, allowFloat, allowNegative) {
	var txt = getForm().elements[field].value;
	if (_isValidNumberFormat(field, allowFloat, allowNegative)) {
		oldNumberFieldsValues[field] = txt;
	}
}

function formatNumberField(field) {
	var result = getForm().elements[field].value;
	if (result.length > 0) {
		var search;
		var first;
		var second;

		// Remove thousand separators
		search = eval("/" + THOUSAND_SEPARATOR + "/g");
		result = result.replace(search, "");

		// Remove starting zero
		result = result.replace(/^0*/, "");

		// Don't remove all zeros
		if (result.length == 0) {
			result = "0";
		}

		// Set thousand separators
		search = eval("/(\\d)(\\d\\d\\d($|" + DECIMAL_SEPARATOR + "))/");
		if(search.test(result)) {
			first = RegExp.$1;
			second = RegExp.$2;
			result = result.replace(search, first + THOUSAND_SEPARATOR.replace(/\\/, "") + second);
		}

		search = eval("/(\\d)(\\d\\d\\d" + THOUSAND_SEPARATOR + ")/");
		while(search.test(result)) {
			first = RegExp.$1;
			second = RegExp.$2;
			result = result.replace(search, first +  THOUSAND_SEPARATOR.replace(/\\/, "") + second);
		}

		getForm().elements[field].value = result;
	}
}

function _isValidNumberFormat(field, allowFloat, allowNegative) {
	var txt = getForm().elements[field].value;
	var search;

	// Minus
	if ((!allowNegative) && (txt.indexOf("-") != -1)){
		return false;
	}

	// Minus sign must be the first sign
	if (txt.lastIndexOf("-") > 0) {
		return false;
	}

	// String has to consist of figures, minus sign, decimal separators
	// and thousand separators only
	search = eval("/[^\\d\\-" + DECIMAL_SEPARATOR + THOUSAND_SEPARATOR + "]/");
	if (search.test(txt)) {
		return false;
	}

	// At most one decimal separators
	search = eval("/" + DECIMAL_SEPARATOR + ".*" + DECIMAL_SEPARATOR + "/");
	if (search.test(txt)) {
		return false;
	}

	// String mustn't contain two successive thousand separators
	search = eval("/" + THOUSAND_SEPARATOR + "{2}$/");
	if (search.test(txt)) {
		return false;
	}

	// String mustn't contain a thousand separators after a decimal separator
	search = eval("/" + DECIMAL_SEPARATOR + ".*" + THOUSAND_SEPARATOR + "$/");
	if (search.test(txt)) {
		return false;
	}

	// String mustn't contain a decimal separator if allowFloat is false
	if (!allowFloat) {
		search = eval("/" + DECIMAL_SEPARATOR + "/");
		if (search.test(txt)) {
			return false;
		}
	}

	// Enter without thousand seperators
	// Remove thousand separators
	search = eval("/" + THOUSAND_SEPARATOR + "/g");
	txt = txt.replace(search, "");
	// Set thousand separators
	search = eval("/^([^" + DECIMAL_SEPARATOR + "]*\\d)(\\d\\d\\d($|" + DECIMAL_SEPARATOR + "))/");
	if(search.test(txt)) {
		first = RegExp.$1;
		second = RegExp.$2;
		txt = txt.replace(search, first + THOUSAND_SEPARATOR.replace(/\\/, "") + second);
	}

	search = eval("/(\\d)(\\d\\d\\d" + THOUSAND_SEPARATOR + ")/");
	while(search.test(txt)) {
		first = RegExp.$1;
		second = RegExp.$2;
		txt = txt.replace(search, first +  THOUSAND_SEPARATOR.replace(/\\/, "") + second);
	}
	
	if (getForm().elements[field].value != txt) {
		getForm().elements[field].value = txt;
	}

	return true;
}


// -------------------------------------------------------------------------
// ----- Currency-Field ----------------------------------------------------
// -------------------------------------------------------------------------
function checkCurrencyField(field, allowFloat, allowNegative) {
	checkNumberField(field, allowFloat, allowNegative);
}

function storeCurrencyField(field, allowFloat, allowNegative) {
	storeNumberField(field, allowFloat, allowNegative);
}

function formatCurrencyField(field, currencyCode, decimalPlaces) {
	formatNumberField(field);
	var txt = getForm().elements[field].value;
	if (txt.length == 0) {
		return;
	}
	if (decimalPlaces < 0) {
		var afterComma = 0;
		if (txt.indexOf(DECIMAL_SEPARATOR.replace(/\\/, "")) != -1) {
			afterComma = txt.length - txt.indexOf(DECIMAL_SEPARATOR.replace(/\\/, "")) - 1;
		} else {
			txt += DECIMAL_SEPARATOR.replace(/\\/, "");
		}
		for (i=0; i<-decimalPlaces-afterComma; i++) {
			txt += "0";
		}
	}
	if (currencyCode != "") {
		getForm().elements[field].value = txt + " " + currencyCode;
	}
}

function deformatCurrencyField(field, currencyCode, allowNegative) {
	var txt = getForm().elements[field].value;
	var search = eval("/ " + currencyCode + "/");
	getForm().elements[field].value = txt.replace(search, "");
}


// -------------------------------------------------------------------------
// ----- Date-Field --------------------------------------------------------
// -------------------------------------------------------------------------
var oldValues = new Array();

function checkDateField(field) {
	var txt = field.value;
	if ((!_isValidDateFormat(field)) && (txt.length > 0)) {
		field.value = oldValues[field.name];
	}
}


function storeDateField(field) {
	var txt = field.value;
	if (_isValidDateFormat(field)) {
		oldValues[field.name] = txt;
	}
}


function formatDateField(field) {
	var result = field.value;

	if (result.length > 0) {
		// Only one figure
		var search = eval("/^(\\d\\d?)" + DATE_SEPARATOR + "?$/");
		if(search.test(result)) {
			first = RegExp.$1;
         	year = (new Date()).getFullYear();
			month = (new Date()).getMonth() + 1;
			result = _createDateString(first, month, year);
		}

		// Day
		var search = eval("/^(\\d" + DATE_SEPARATOR + ")/");
		search.exec(result);
		first = RegExp.$1;
		result =
			result.replace(
				eval(
					"/^\\d" + DATE_SEPARATOR + "/"
				),
				"0" + first
			);

		// Month
		search = eval("/^(\\d\\d" + DATE_SEPARATOR + ")(\\d(" + DATE_SEPARATOR + "|$))/");
		search.exec(result);
		first = RegExp.$1;
		second = RegExp.$2;
		result =
			result.replace(
				eval(
					"/^\\d\\d" + DATE_SEPARATOR + "\\d(" + DATE_SEPARATOR + "|$)/"
				),
				first + "0" + second
			);

		// Year
		// No year
		search = eval("/^(\\d\\d" + DATE_SEPARATOR + "\\d\\d)" + DATE_SEPARATOR + "?$/");
		search.exec(result);
		first = RegExp.$1;
                year = (new Date()).getFullYear();
		result =
			result.replace(
				eval(
					"/^\\d\\d" + DATE_SEPARATOR + "\\d\\d" + DATE_SEPARATOR + "?$/"
				),
				first + DATE_SEPARATOR.replace(/\\/, "") + year
			);

		// One digits
		search = eval("/^(\\d\\d" + DATE_SEPARATOR + "\\d\\d" + DATE_SEPARATOR + ")(\\d)$/");
		search.exec(result);
		first = RegExp.$1;
		second = RegExp.$2;
		result =
			result.replace(
				eval(
					"/^\\d\\d" + DATE_SEPARATOR + "\\d\\d" + DATE_SEPARATOR + "\\d$/"
				),
				first + 0 + second
			);

		// three digits
		search = eval("/^(\\d\\d" + DATE_SEPARATOR + "\\d\\d" + DATE_SEPARATOR + ")(\\d\\d\\d)$/");
		search.exec(result);
		first = RegExp.$1;
		second = RegExp.$2;
		result =
			result.replace(
				eval(
					"/^\\d\\d" + DATE_SEPARATOR + "\\d\\d" + DATE_SEPARATOR + "\\d\\d\\d$/"
				),
				first + 0 + second
			);

		// Add century
		search = eval("/^(\\d\\d" + DATE_SEPARATOR + "\\d\\d" + DATE_SEPARATOR + ")(\\d\\d)$/");
		search.exec(result);
		first = RegExp.$1;
		second = RegExp.$2;
		var century = "20";
		if (second > 50) {
			century = "19";
		}
		result =
			result.replace(
				eval(
					"/^\\d\\d" + DATE_SEPARATOR + "\\d\\d" + DATE_SEPARATOR + "\\d\\d$/"
				),
				first + century + second
			);

		field.value = result;

		if (!_isValidDate(field)) {
			alert("Date is not valid!");  // TRANSLATE
			field.focus();
		}
	}
}


function _isValidDateFormat(field) {
	var txt = field.value;
	var search;
	var first;
	var second;

	// Digits and separators only
	search = eval("/[^\\d" + DATE_SEPARATOR + "]/");
	if (search.test(txt)) {
		return false;
	}

	// ---- SEPERATORS ----

	// At most two separators
	search = eval("/" + DATE_SEPARATOR + ".*" + DATE_SEPARATOR + ".*" + DATE_SEPARATOR + "/");
	if (search.test(txt)) {
		return false;
	}

	// String mustn't contain two successive separators
	search = eval("/" + DATE_SEPARATOR + "{2}$/");
	if (search.test(txt)) {
		return false;
	}

	// String mustn't start with a separator
	search = eval("/^" + DATE_SEPARATOR + "$/");
	if (search.test(txt)) {
		return false;
	}

	// ---- DAY ----

	// Day mustn't be 0
	search = eval("/^0" + DATE_SEPARATOR + "$/");
	if (search.test(txt)) {
		return false;
	}

	// Day mustn't be 00
	search = /^00/;
	if (search.test(txt)) {
		return false;
	}

	// Day mustn't be a number higher than 31
	search = eval("/^3[^01" + DATE_SEPARATOR + "]/");
	if (search.test(txt)) {
		return false;
	}
	search = /^[456789]\d/;
	if (search.test(txt)) {
		return false;
	}

	// Enter without seperators
	search = /^(\d\d)(\d)/;
	if(search.test(txt)) {
		first = RegExp.$1;
		second = RegExp.$2;
		txt =
			txt.replace(
				/^\d\d\d/,
				first + DATE_SEPARATOR.replace(/\\/, "") + second
			);
		field.value = txt;
	}

	// ---- MONTH ----

	// Month mustn't be 0
	search = eval("/^\\d\\d?" + DATE_SEPARATOR + "0" + DATE_SEPARATOR + "$/");
	if (search.test(txt)) {
		return false;
	}

	// Month mustn't be 00
	search = eval("/^\\d\\d?" + DATE_SEPARATOR + "00/");
	if (search.test(txt)) {
		return false;
	}

	// Month mustn't be with a number higher than 12
	search = eval("/^\\d\\d?" + DATE_SEPARATOR + "1[^012" + DATE_SEPARATOR + "]/");
	if (search.test(txt)) {
		return false;
	}
	search = eval("/^\\d\\d?" + DATE_SEPARATOR + "[23456789]\\d/");
	if (search.test(txt)) {
		return false;
	}

	// Enter without seperators
	search = /^(\d\d.\d\d)(\d)/;
	if(search.test(txt)) {
		first = RegExp.$1;
		second = RegExp.$2;
		txt =
			txt.replace(
				/^\d\d.\d\d\d/,
				first + DATE_SEPARATOR.replace(/\\/, "") + second
			);
		field.value = txt;
	}

	// ---- YEAR ----

	// Year has to consist of at most four digits
	search = eval("/^\\d\\d?" + DATE_SEPARATOR + "\\d\\d?" + DATE_SEPARATOR + "\\d{5}/");
	if (search.test(txt)) {
		return false;
	}

	// ---- OK ----

	// Everything's alright
	return true;
}


function _isValidDate(field) {
	var txt = field.value;
	var day = txt.substring(0, 2);
	var month = txt.substring(3, 5);
	var year = txt.substring(6, 10);

	// Day
	var daysInMonth = new Array (31, _getDaysInFebruary(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if ((day > daysInMonth[month-1]) || (day < 1)) {
		return false;
	}

	// Month
	if ((month < 1) || (month > 12)) {
		return false;
	}

	return true;
}


function _getDaysInFebruary(year) {
	if (
		(year % 4 == 0)  &&
		(
			(year % 100 != 0)  ||
			(year % 400 == 0)
		)
	) {
		return 29;
	}
	return 28;
}


function _createDateString(day, month, year) {
	return "" +
		day + DATE_SEPARATOR.replace(/\\/, "") +
		month + DATE_SEPARATOR.replace(/\\/, "") +
		year;
}


function _createDateStringByDate(date) {
	return "" +
		date.getDate() + DATE_SEPARATOR.replace(/\\/, "") +
		(date.getMonth()+1) + DATE_SEPARATOR.replace(/\\/, "") +
		date.getFullYear();
}


function _getDate(string) {
	var search = eval("/^(\\d\\d)" + DATE_SEPARATOR + "(\\d\\d)" + DATE_SEPARATOR + "(\\d\\d\\d\\d)$/");
	search.test(string);
	return new Date(RegExp.$3, RegExp.$2-1, RegExp.$1); 
}


// -------------------------------------------------------------------------
// ----- TextArea ----------------------------------------------------------
// -------------------------------------------------------------------------
var oldTextAreaValues = new Array();

function checkTextArea(field, max) {
	var s = getForm().elements[field].value;
	if (s.length > max) {
		getForm().elements[field].value = oldNumberFieldsValues[field];
	}
}

function storeTextArea(field, max) {
	var s = getForm().elements[field].value;
	if (s.length <= max) {
		oldNumberFieldsValues[field] = s;
	}
}



// -------------------------------------------------------------------------
// ----- Editable Combobox -------------------------------------------------
// -------------------------------------------------------------------------
function editComboChanged(field) {
	getForm().elements[field].value = getForm().elements[field + "_More"].options[getForm().elements[field + "_More"].selectedIndex].text;
	hideMore(field);
}

function showOrHideMore(field) {
	if (document.getElementById(field + "_Div").style.display == "block") {
		hideMore(field);
	} else {
		showMore(field);
	}
}

function showMore(field) {
	document.getElementById(field + "_Div").style.display="block";
	document.getElementById(field + "_More").focus();
}

function hideMore(field) {
	document.getElementById(field + "_Div").style.display="none";
}
