/* -------------------------------------------------------------------------- */
/*   This file contains javascript functions used by the datatypes            */
/*   (com.softdes.tec.datatype.Datatype java subclasses).                     */
/* -------------------------------------------------------------------------- */

/* -------------------------------------------------------------------------- */
/*   Verifies if the value of an input matches some regular expression.       */
/*                                                                            */
/*   Params:                                                                  */
/*     pField  - input to be validated                                        */
/*     pRegexp - regular expression to apply                                  */
/*     pFormat - format to show to user, when it doesn't match                */
/*                                                                            */
/*   Returns:                                                                 */
/*     true, if the value matches the expression; false, otherwise            */
/* -------------------------------------------------------------------------- */
function checkRegExp(pField, pRegexp, pFormat) {

    if (pField.value == null || pField.value.length == 0) {
        return true;
    }

    re = new RegExp(pRegexp);
    ar = re.exec(pField.value);

    if (ar == null || ar[0] != pField.value) {

        msg = (pFormat != "") ?
            "O valor não está no formato " + pFormat + ". Por favor, corrija-o!" :
            "O valor não é válido. Por favor, corrija-o!";

        alert(msg);
        pField.focus();
        pField.select();
        return false;

    } else {
        return true;
    }
}

/* -------------------------------------------------------------------------- */
/*   Formats a number with custom thousand separator, decimal separator and   */
/*   number of decimal places.                                                */
/*                                                                            */
/*   Params:                                                                  */
/*     field     - input to be formatted                                      */
/*     thSep     - thousand separator character; if null, no separator is put */
/*     decSep    - decimal separator character; if null, no separator is put  */
/*     decPlaces - number of decimal places; makes no sense if decSep is null */
/*	   maxSize   - not used! but NumberDataType passes such arg, and it's     */
/*				   easier for me to correct it here.						  */
/*     signed    - if true, '-' and '+' are not ignored                       */
/*                                                                            */
/*   Returns:                                                                 */
/*     true, if the caracter typed is valid; false, otherwise                 */
/* -------------------------------------------------------------------------- */
function numericFormat(field, thSep, decSep, e, decPlaces, maxSize, signed) {

	if (field.readonly) {
		return true;
	}
    
    var numbers = '0123456789';
    var signals = '+-';

    // Get the code and the matching key.
    var whichCode = window.Event ? e.which : e.keyCode;
    var key = String.fromCharCode(whichCode);
    var signal = '';

    // Check if the Enter was keypressed.
    if (whichCode == 13) {
        return true;

    // Check if it's not a valid key (not a number).
    } else if (numbers.indexOf(key) == -1 && signals.indexOf(key) == -1) {
        return false;
    }

    // Gets the signal to append, if any.
    if (signals.indexOf(key) != -1) {
        if (!signed) {
            return false;
        } else {
            signal = key;
            key = (field.value == '') ? '0' : '';
        }
    } else if (field.value.length > 0 && signals.indexOf(field.value.charAt(0)) != -1) {
        signal = field.value.charAt(0);
        field.value = field.value.substr(1, field.value.length - 1);
    }

    // Process the new number.
    var result = '', i = 0;

    // Get only the numeric digits.
    for (i = 0; i < field.value.length; i++) {
        if (numbers.indexOf(field.value.charAt(i)) != -1) {
            result += field.value.charAt(i);
        }
    }

    // Put off the left zeros if the number accepts decimal places.
    if (decPlaces > 0) {
        for (i = 0; i < result.length && result.charAt(i) == '0'; i++);
        result = result.substr(i, result.length);
    }

    // Check for available space.
    if (result.length < field.maxLength || key == '') {

        // Append the new caracter.
        result += key;

        // Copy the formatted decimal part (from right to left), if any.
        i = result.length;
        field.value = '';

        if (decSep != null && decPlaces > 0) {
            for (; i <= decPlaces; i++) {
                result = '0' + result;
            }

            i = result.length - decPlaces;
            field.value = decSep + result.substr(i, result.length);
        }

        // Copy the formatted integer part (from right to left).
        if (thSep == null) {
            field.value = result.substr(0, i) + field.value;
        } else {
            var count = 1;
            for (i -= 1; i >= 0; i--) {
                field.value = result.charAt(i) + field.value;
                if (count == 3 && i > 0) {
                    field.value = thSep + field.value;
                    count = 1;
                } else {
                    count++;
                }
            }
        }
    }

    // Appends the signal, if any.
    if (signal != '') {
        field.value = signal + field.value;
    }

    return false;
}

/* -------------------------------------------------------------------------- */
/*   Fills a date with '/' while it's being typed.                            */
/*                                                                            */
/*   Params:                                                                  */
/*     field     - input to be filled.                                        */
/* -------------------------------------------------------------------------- */
function autoFillDate(field) {
	
    var whichCode = window.Event ? event.which : event.keyCode;
    var key = String.fromCharCode(whichCode);
	
	if ((field.value.length == 2 || field.value.length == 5) && key != '/')
		field.value = field.value + '/';
	
}

/* -------------------------------------------------------------------------- */
/*   Completes a date that has a 2-digit year with a 4-digit year.            */
/*	                                                                          */
/*	 If the 2-digit year is greater then 50, it will be considerated of the   */
/*	 20th century. Otherwise, it will be a 21th century year.                 */
/*   																		  */
/*   CAUTION: This function is made to receive an already validated date.     */
/*                                                                            */
/*   Params:                                                                  */
/*     field     - input to be completed.                                     */
/* -------------------------------------------------------------------------- */
function completeYear(field) {
	
	if (field.value.length == 8){
		var ano = field.value.slice(6);
		
		if (ano < 50)
			ano = '20' + ano;
		else
			ano = '19' + ano;
			
		field.value = field.value.slice(0,6) + ano;
	}
}