// <m name="Message Handler"
//    author="Edwin Rijvordt (erijvordt@consultdata.nl)"
//    version="1.0"
//    type="component"
//    language="JavaScript">  Client-side Javascript class for a generic message handler.
//    An single instance of this class exists, which is used by
//    constraint checking functions (or other functions) to
//    generate messages, mostly error messages.
//
//    <p/>
//    By default an instance is installed that should be suitable
//    for most uses, but a derived class can be installed for
//    generating more specific messages, for example messages in
//    other languages.
//
//    <p/>
//    Clients can call all of the class's members to generate
//    messages, by using the single instance of the class like this:
//
//    <pre>
//        var msg = MsgHandler.emptyItem("name");
//        var msg = MsgHandler.noOptionSelected("category");
//    </pre>
//
//    If you want to override the default messages, define a subclass
//    of the MsgHandler, and override one or more of the message-
//    generating functions, and call the following to install it:
//    <pre>
//        MsgHandler.setHandler(new YourMsgHandler());
//    </pre>
// </m>

// User constructor.
function MsgHandler() {
    this.MsgHandler_constructor = MsgHandler_constructor;
    this.MsgHandler_constructor();
}

// Real constructor.
function MsgHandler_constructor() {
    // Attribute properties.
    this.window = null;

    // Method properties.
    this.setHandler = MsgHandler_setHandler;
    this.setWindow = MsgHandler_setWindow;
    this.showMessage = MsgHandler_showMessage;

    // Default implementations.
    this.emptyItem = MsgHandler_emptyItem;
    this.notEnoughCharacters = MsgHandler_notEnoughCharacters;
    this.tooManyCharacters = MsgHandler_tooManyCharacters;
    this.invalidNumber = MsgHandler_invalidNumber;
    this.invalidPositiveNumber = MsgHandler_invalidPositiveNumber;
    this.invalidFloat = MsgHandler_invalidFloat;
    this.invalidPositiveFloat = MsgHandler_invalidPositiveFloat;
    this.invalidFixed144 = MsgHandler_invalidFixed144;
    this.invalidPositiveFixed144 = MsgHandler_invalidPositiveFixed144;
    this.numberTooSmall = MsgHandler_numberTooSmall;
    this.numberTooLarge = MsgHandler_numberTooLarge;
    this.invalidAlphabetic = MsgHandler_invalidAlphabetic;
    this.invalidAlphaPunct = MsgHandler_invalidAlphaPunct;
    this.invalidAlphaNumeric = MsgHandler_invalidAlphaNumeric;
    this.invalidAlphaNumericExtended = MsgHandler_invalidAlphaNumericExtended;
    this.invalidEmailAddress = MsgHandler_invalidEmailAddress;
    this.invalidMoneyAmount = MsgHandler_invalidMoneyAmount;
    this.invalidPositiveMoneyAmount = MsgHandler_invalidPositiveMoneyAmount;
    this.invalidPhoneNumber = MsgHandler_invalidPhoneNumber;
    this.noRadioButtonSelected = MsgHandler_noRadioButtonSelected;
    this.noOptionSelected = MsgHandler_noOptionSelected;
    this.incompleteItem = MsgHandler_incompleteItem;
    this.invalidMonthDay = MsgHandler_invalidMonthDay;
    this.invalidLeapYear = MsgHandler_invalidLeapYear;
    this.dateNotBefore = MsgHandler_dateNotBefore;
    this.invalidFormat = MsgHandler_invalidFormat;
}

function MsgHandler_setHandler(newHandler) {
    MsgHandler = newHandler;
}

function MsgHandler_setWindow(newWindow) {
    this.window = newWindow;
}

function MsgHandler_showMessage(msg) {
    if (this.window == null) {
		alert(msg);
    } else {
        this.window.alert(msg);
    }
}

function MsgHandler_emptyItem(itemName) {
	return "Required field \"" + itemName + "\" was not entered.";
}

function MsgHandler_notEnoughCharacters(itemName, minLength) {
	return "Not enough characters entered for field \"" + itemName + "\" (at least " + minLength + " must be entered).";
}

function MsgHandler_tooManyCharacters(itemName, maxLength) {
	return "Too many characters entered for field \"" + itemName + "\" (at most " + maxLength + " can be entered).";
}

function MsgHandler_invalidNumber(itemName) {
    return "The field \"" + itemName + "\" may only contain digits and a minus-sign.";
}

function MsgHandler_invalidPositiveNumber(itemName) {
    return "The field \"" + itemName + "\" may only contain digits.";
}

function MsgHandler_invalidFloat(itemName) {
    return "The field \"" + itemName + "\" does not contain a valid decimal value (like -2.1 or 3.5).";
}

function MsgHandler_invalidPositiveFloat(itemName) {
    return "The field \"" + itemName + "\" does not contain a valid positive decimal value (like 2.1 or 3.5).";
}

function MsgHandler_invalidFixed144(itemName) {
    return "The field \"" + itemName + "\" does not contain a valid fixed.14.4 value (like -2.1 or 32.1234).";
}

function MsgHandler_invalidPositiveFixed144(itemName) {
    return "The field \"" + itemName + "\" does not contain a valid positive fixed.14.4 value (like 2.1 or 32.1234).";
}

function MsgHandler_numberTooSmall(itemName, minValue) {
	return "The value entered in field \"" + itemName + "\" is too small (must be at least " + minValue + ").";
}

function MsgHandler_numberTooLarge(itemName, maxValue) {
	return "The value entered in field \"" + itemName + "\" is too large (can be at most " + maxValue + ").";
}

function MsgHandler_invalidAlphabetic(itemName) {
	return "The field \"" + itemName + "\" may only contain alphabetic characters.";
}

function MsgHandler_invalidAlphaPunct(itemName) {
	return "The field \"" + itemName + "\" may only contain alphabetic characters and punctuation characters (<space>, ', `, \", -, . or ,).";
}

function MsgHandler_invalidAlphaNumeric(itemName) {
	return "The field \"" + itemName + "\" may only contain alphanumeric charakters and underscores.";
}

function MsgHandler_invalidAlphaNumericExtended(itemName) {
	return "The field \"" + itemName + "\" may only contain alphanumeric charakters, spaces, underscores, \'@\', \-\ and \'.\'.";
}

function MsgHandler_invalidEmailAddress(itemName) {
    return "The field \"" + itemName + "\" does not contain a proper email address.";
}

function MsgHandler_invalidMoneyAmount(itemName) {
    return "The field \"" + itemName + "\" does not contain a valid money amount (valid examples are: -13.95 or +13,95 or 14).";
}

function MsgHandler_invalidPositiveMoneyAmount(itemName) {
    return "The field \"" + itemName + "\" does not contain a valid money amount (valid examples are: 13.95 or 14).";
}

function MsgHandler_invalidPhoneNumber(itemName) {
    return "The field \"" + itemName + "\" does not contain a valid phone number (valid characters are digits, parentheses, minus and plus signs).";
}

function MsgHandler_noRadioButtonSelected(itemName) {
	return "No item in the group \"" + itemName + "\" was selected.";
}

function MsgHandler_noOptionSelected(itemName) {
	return "No item from the list \"" + itemName + "\" was selected.";
}

function MsgHandler_incompleteItem(itemName) {
	return "The item \"" + itemName + "\" has not been filled in completely.";
}

function MsgHandler_invalidMonthDay(itemName, dayGiven, dayAllowed, month) {
	return "The field \"" + itemName + "\" contains an invalid date: the month " + monthToString(month, 1) + " only has " + dayAllowed + " days.";
}

function MsgHandler_invalidLeapYear(itemName, year) {
	return "The field \"" + itemName + "\" contains an invalid date: the year " + year + " is not a leap year.";
}

function MsgHandler_dateNotBefore(itemName, date1, date2) {
    return "The date in field \"" + itemName + "\" does not lie before " + formatDate(date2, DATEFMT_TEXT, 1) + ".";
}

function MsgHandler_invalidFormat(itemName, format, isRE) {
    if (isRE) {
		return "The field \"" + itemName + "\" does not have the proper format '" + format + "'.";
    } else {
		return "The field \"" + itemName + "\" does not have the proper format for a " + format + ".";
    }
}

// Create the default global MsgHandler instance.
var MsgHandler = new MsgHandler();


