//
// Constant values
//
var utl_EMPTY_STRING = '';
var utl_UNDEFINED_TYPE = 'undefined';
var utl_STRING_TYPE = 'string';
var utl_BOOLEAN_TYPE = 'boolean';
var utl_NUMBER_TYPE = 'number';
var utl_FUNCTION_TYPE = 'function';
var utl_OBJECT_TYPE = 'object';
var reDecimal = /^([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/;
var reInteger = /^\d+$/
var defaultEmptyOK = false;
//
// Gets the default value if null value was passed to the function,
// otherwise returns the passed value
// @param o: the value that will be checked
// @param d: the default value
//
function utl_getDefIfNull(o, d) {
    return (o == null || (typeof o == utl_UNDEFINED_TYPE)) ? d : o;
}
//
// Checks if the passed value is string or not
// @param x: the value to be tested
//
function utl_isString(x) {
    return _utl_test_type(x, utl_STRING_TYPE);
}
//
// Checks if the passed value is a boolean or not
// @param x: the value to be tested
//
function utl_isBoolean(x) {
    return _utl_test_type(x, utl_BOOLEAN_TYPE);
}
//
// Checks if the passed value is a function or not
// @param x: the value to be tested
//
function utl_isNumber(x) {
    return _utl_test_type(x, utl_NUMBER_TYPE);
}
//
// Checks if the passed value is a function or not
// @param x: the value to be tested
//
function utl_isFunction(x) {
    return _utl_test_type(x, utl_FUNCTION_TYPE);
}
//
// Checks if the passed value is an object or not
// @param x: the value to be tested
//
function utl_isObject(x) {
    return _utl_test_type(x, utl_OBJECT_TYPE);
}
//
// Tests the value type is the required type
// @param x: the value to be tested
// @param reqType: the required type
//
function _utl_test_type(x, reqType) {
    if(x != null) {
        return typeof x == reqType;
    }
    
    return false;
}

//
// Gets the value for a value binding expression
// @param val: the value that will be evaluated
//
var _utl_REG_VALUE_EXP = /^\s*#JSE\{(.)+\}$/;
var _utl_EXP_LEN = 6;

function utl_getExpValue(val) {
    val = utl_getDefIfNull(val, null);
    if(utl_isString(val) && val.length > _utl_EXP_LEN) {
        var matches = _utl_REG_VALUE_EXP.match(val);
        if(!(matches == null || matches.length == 0)) {
            val = utl_getDefIfNull(eval(matches[1]), null);
        }
    }

    return val;
}

function utl_getAsString(val) {
    if(val == null) {
        return null;
    }

    var valType = typeof val;

    if(valType == utl_UNDEFINED_TYPE) {
        return null;
    }

    if(valType == utl_STRING_TYPE) {
        return val;
    }

    return val.toString();
}

function utl_getAsBoolean(val) {
    if(val == null) {
        return null;
    }

    var valType = typeof val;

    if(valType == utl_UNDEFINED_TYPE) {
        return null;
    }

    if(valType == utl_BOOLEAN_TYPE) {
        return val;
    }

    if(valType != utl_STRING_TYPE) {
        val = val.toString();
    }

    val = utl_trim(val).toLowerCase();

    var bVal;
    if(val == "true" || val == "yes") {
        bVal = true;
    } else if(val == "false" || val == "no") {
        bVal = false;
    } else {
        bVal = parseInt(val, 10);
        if(isNaN(bVal)) {
            bVal = null;
        } else {
            bVal = (bVal != 0);
        }
    }

    return bVal;
}

//
// Get as integer value
// @param val: the value that well be converted to integer
//
function utl_getAsInteger(val) {
    if(val == null) {
        return null;
    }

    var valType = typeof val;

    if(valType == utl_UNDEFINED_TYPE) {
        return null;
    }

    var iVal;
    if(valType == utl_NUMBER_TYPE) {
        iVal = val;
    } else {
        if(valType != utl_STRING_TYPE) {
            val = '' + val;
        }

        iVal = parseInt(utl_trim(val), 10);
    }

    if(isNaN(iVal)) {
        iVal = null;
    }

    return iVal;
}
//
// Get as float value
// @param val: the value that well be converted to integer
//
function utl_getAsFloat(val) {
    if(val == null) {
        return null;
    }

    var valType = typeof val;

    if(valType == utl_UNDEFINED_TYPE) {
        return null;
    }

    var fVal;
    if(valType == utl_NUMBER_TYPE) {
        fVal = val;
    } else {

        if(val != utl_STRING_TYPE) {
            val = '' + val;
        }

        fVal = parseFloat(utl_trim(val));
    }

    if(isNaN(fVal)) {
        fVal = null;
    }

    return fVal;
}

//
// Crossbrowser get element by id
// @param fldID: the id of the field to retrive it
//
function utl_getField(fldID) {

    var field = null;
    if(document.all) {
        field = utl_getDefIfNull(document.all[fldID], null);
    } else {
        field = utl_getDefIfNull(document.getElementById(fldID), null);
        if(field == null) {
            field = utl_getDefIfNull(document.getElementsByName(fldID), null);
        }
    }

    return field;
}

//
// Gets the field's value according to the field's type
// @param field: the field to get the value from it
// @param canReturnArray: use this if you expect that the returned value maybe an array
//
var utl_FIELD_TYPE_HIDDEN = 'hidden';
var utl_FIELD_TYPE_TEXT = 'text';
var utl_FIELD_TYPE_PASSWORD = 'password';
var utl_FIELD_TYPE_TEXTAREA = 'textarea';
var utl_FIELD_TYPE_FILE = 'file';
var utl_FIELD_TYPE_RADIO = 'radio';
var utl_FIELD_TYPE_CHECKBOX = 'checkbox';
var utl_FIELD_TYPE_SELECTONE = 'select-one';
var utl_FIELD_TYPE_SELECTMULTIPLE = 'select-multiple';

function utl_getFieldValue(field, canReturnArray) {
    var val = null;

    field = utl_getDefIfNull(field, null);
    if(utl_isString(field)) {
        field = utl_getField(field);
    }

    if(field == null) {
        return null;
    }

    var fType = utl_getDefIfNull(field.type, null);
    canReturnArray = utl_getDefIfNull(utl_getAsBoolean(canReturnArray), true);

    if(fType == null) {
        if(field.length) {
            var len = field.length;
            var elm = null;
            if(len > 0) {
                fType = utl_getDefIfNull(field[0].type, null);                            
                if(fType == utl_FIELD_TYPE_RADIO) {
                    for(var i = 0; i != len; ++i) {
                        elm = field[i];
                        if(elm.checked) {
                            val = elm.value;
                            break;
                        }
                    }
                } else if(canReturnArray && fType == utl_FIELD_TYPE_CHECKBOX) {
                    var arr = new Array();
                    for(var i = 0; i != len; ++i) {
                        elm = field[i];
                        if(elm.checked) {
                            arr.push(elm.value);
                        }
                    }

                    if(arr.length > 0) {
                        val = arr;
                    }
                    arr = null;
                }
            }
        }
    } else if(fType == utl_FIELD_TYPE_HIDDEN ||
            fType == utl_FIELD_TYPE_TEXT ||
            fType == utl_FIELD_TYPE_PASSWORD ||
            fType == utl_FIELD_TYPE_TEXTAREA ||
            fType == utl_FIELD_TYPE_FILE) {
        val = field.value;
    } else if(fType == utl_FIELD_TYPE_RADIO || fType == utl_FIELD_TYPE_CHECKBOX) {
        if (field.checked) {
            val = field.value;
        }
    } else if(fType == utl_FIELD_TYPE_SELECTONE) {
        var si = field.selectedIndex;
        if(si >= 0) {
            val = field.options[si].value;
        }
    } else if(canReturnArray && fType == utl_FIELD_TYPE_SELECTMULTIPLE) {
        var options = field.options;
        var len = options.length;
        var arr = new Array();
        var opt = null;

        for(var i = 0; i != len; ++i) {
            opt = options[i];
            if(opt.selected) {
                arr.push(opt.value);
            }
        }

        if(arr.length > 0) {
            val = arr;
        }
        arr = null;
    } else {
        val = field.value;
    }

    if(typeof val == utl_UNDEFINED_TYPE) {
        val = null;
    }

    return val;
}

//
// String termination functions
// @param s: string that will be terminated
//
var _utl_REG_EXP_LTRIM = /^\s*/;
function utl_ltrim(s) {
    return s.replace(_utl_REG_EXP_LTRIM, utl_EMPTY_STRING);
}
var _utl_REG_EXP_RTRIM = /\s*$/;
function utl_rtrim(s) {
    return s.replace(_utl_REG_EXP_RTRIM, utl_EMPTY_STRING);
}
function utl_trim(s) {
    return s.replace(_utl_REG_EXP_LTRIM, utl_EMPTY_STRING).replace(_utl_REG_EXP_RTRIM, utl_EMPTY_STRING);
}
var utl_REG_EXP_ADJST = /(\r)*/;
function utl_adjust(s) {
    return s.replace(utl_REG_EXP_ADJST, utl_EMPTY_STRING);
}
function utl_getAdjustedLength(s) {
    return utl_adjust(s).length;
}

//
// This function is called in the mainlayout page to show any disabled element
// as dimmed, because they are not appear dimmed in firefox
// @param elmType: the element type
// @param cssStyle: the css style name
//
function utl_displayDimmed(elmType, cssStyle) {
    for(var f = 0; f < document.forms.length; ++f) {
        var frm = document.forms[f];
        for(var e = 0; e < frm.elements.length; ++e) {
            var elm = frm.elements[e];
            if(elm.type == elmType && elm.disabled) {
                elm.className = cssStyle;
            }
        }
    }
}

//
// Disable/Enable field and sets its style
// @param fldID: the disabled html field
// @param fldDisabled: the disabled or enabled
// @param cssStyle: the css style name
//
function utl_disableField(fldID, fldDisabled, cssStyle) {
    var field = utl_getField(fldID);
    if(field != null && (typeof field.disabled == utl_BOOLEAN_TYPE)) {
         field.disabled = fldDisabled;
         field.className = cssStyle;
    }
}

//
// resets the html for a field
// @param fld: the id of the field
//
function utl_resetFieldHTML(fldID) {
    var field = utl_getField(fldID);
    if(field != null && (typeof field.innerHTML == utl_STRING_TYPE)) {
        field.innerHTML = '';
    }
}

//
// checks if a string contains only ascii characheters only or not !!
// @param str: the string that you want to check it.
//
function utl_isAsciiChars(str) {
    for(var i = 0; i != str.length; ++i) {
        if(str.charCodeAt(i) > 255) {
            return false;
        }
    }
    
    return true;
}

//
//methods for check box (select/unselect all)
//
function utl_selectAll(selectAllCheckBoxName_p,rowCheckBoxName_p,rowsCount_p){
    var isChecked=utl_getField(selectAllCheckBoxName_p).checked;
    var anyEnabled = false;
    for (i=0;i<rowsCount_p;i++){
        var checkedField=utl_getField(rowCheckBoxName_p+'['+i+']');
        if(!checkedField.disabled){
            checkedField.checked=isChecked;
            anyEnabled = true;
        }
    }
    if(!anyEnabled){
        utl_getField(selectAllCheckBoxName_p).checked = anyEnabled;
    }
}

function utl_updateSelectAll(selectAllCheckBoxName_p,rowCheckBoxName_p,rowsCount_p){         
     var allSelectedFlag=true;
        for (i=0;i<rowsCount_p;i++){
            var checkedField=utl_getField(rowCheckBoxName_p+'['+i+']');        
            if(!checkedField.checked && !checkedField.disabled){
                allSelectedFlag=false;
                break;
                }
        }
     utl_getField(selectAllCheckBoxName_p).checked=allSelectedFlag;
}

function utl_enableDecimalOnly(field) {
    if (!isDecimal(field.value)) {
        var fieldVal = field.value;
	var dotIndex= fieldVal.indexOf(".");
	if( dotIndex != -1 ){
		var firstSection = field.value.substring(0, dotIndex);
		if(firstSection==""){
			firstSection = "0";
		}
		var secondSection = fieldVal.substring(dotIndex+1);
		firstSection=firstSection.replace(/[^\d]/g,"");
		secondSection=secondSection.replace(/[^\d]/g,"");
		field.value = firstSection +"."+secondSection;
	}else{
        	field.value = fieldVal.replace(/[^\d]/g,"");		
	}
    }
}

function isDecimal (value)
{
    if (isEmpty(value)) 
       if (isDecimal.arguments.length == 1) return defaultEmptyOK;
       else return (isDecimal.arguments[1] == true);
    return reDecimal.test(value)
}

function isInteger (value)
{
    if (isEmpty(value)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);
    return reInteger.test(value)
}

function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

function utl_decimalOnly(e,field,decimal) {
    var charCode = (e.which) ? e.which : e.keyCode;    
    keychar = String.fromCharCode(charCode);
    if (decimal && (keychar == ".") && field.value.indexOf(".") == -1) {
      return true;
    }
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

function utl_disableCharacters(field) {
    if (!isInteger(field.value)) {
        field.value = field.value.replace(/[^\d]/g,"");
    }
}

function utl_clearCombo(comboId){
    var mycombo = document.getElementById(comboId);
    mycombo.options.length = 0;
}

function utl_clearAndFillCombo(src,dest,addBlankItem){
        var combo_src = document.getElementById(src);
        var combo_dest = document.getElementById(dest);
        
        var selectedDestVal = combo_dest.value;
        utl_clearCombo(dest);
        if(addBlankItem){
            var blankChoice = document.createElement('option');
            blankChoice.value ="";
            blankChoice.selected="selected";        
            combo_dest.appendChild(blankChoice);
        }
        for(i=0;i< combo_src.options.length ;i++){
            var choice = document.createElement('option');
            choice.value =   combo_src.options[i].value;          
            if(choice.value == selectedDestVal){
            choice.selected = true;
            }
            choice.appendChild(document.createTextNode(combo_src.options[i].text));
            combo_dest.appendChild(choice);
        }
}

