var highlightedElement;

function initWindowId() {
    $('form').each(function() {
        $(this).append('<input type="hidden" name="windowId" value="' + window.name + '"/> ');
        //$('#\\.id').append('<input type="hidden" name="windowId" value="' + window.name + '"/> ');
    });
}

function initWindowIdForForm(formId) {
    $('#' + formId).append('<input type="hidden" name="windowId" value="' + window.name + '"/> ');
}

function forceNumeric(checkObject, acceptNegative) {
    var allowNegative = (acceptNegative != null) ? acceptNegative : false;
    if (checkObject.value.length > 0) {
        var curVal = checkObject.value;
        if (checkObject.value.length >= 256) {
            checkObject.value = "" + checkObject.value.substring(0, 255);
        }
        var dots = 0;
        var newVal = "";
        var temp = checkObject.value.split('');
        for (var i in temp) {
            if(i == 0){
                if(allowNegative && temp.indexOf('-') != -1){
                    newVal += ("-");
                }
            }
            if ("0123456789".indexOf(temp[i]) != -1) {
                newVal += temp[i];
            } else if (temp[i] == ".") {
                dots += 1;
                if (dots <= 1) {
                    newVal += temp[i];
                }
            }
        }
        if (curVal != newVal) {
            highlightedElement = checkObject;
            //do not change backgroundColor, as it changes overall styling, not bg color only
            setTimeout("highlightedElement.style.color = 'black';", 500);
            checkObject.style.color = "red";
            checkObject.value = newVal;
        }
    }
}

function forceInteger(checkObject) {
    if (checkObject.value.length > 0) {
        var curVal = checkObject.value;
        if (checkObject.value.length >= 256) {
            checkObject.value = "" + checkObject.value.substring(0, 255);
        }
        var newVal = "";
        var temp = checkObject.value.split('');
        for (var i in temp) {
            if ("0123456789".indexOf(temp[i]) != -1) {
                newVal += temp[i];
            }
        }
        if (curVal != newVal) {
            highlightedElement = checkObject;
            //do not change backgroundColor, as it changes overall styling, not bg color only
            setTimeout("highlightedElement.style.color = 'black';", 500);
            checkObject.style.color = "red";
            checkObject.value = newVal;
        }
    }
}

function checkForNumeric(checkObject) {
    if (checkObject.value.length >= 256) {
        checkObject.value = "" + checkObject.value.substring(0, 255);
    }
    var temp = checkObject.value;
    replaceNonDigits(checkObject);
}

function replaceNonDigits(checkObject) {
    var oldValue = checkObject.value;
    var newValue = "";
    var temp = new Array();
    temp = checkObject.value.split('');
    for (i = 0; i < temp.length; i++) {
        if (isForNumeric(temp[i]))
            newValue += temp[i];
    }
    if (oldValue != newValue)
        checkObject.value = newValue;
}

function replaceNonFV(checkObject) {
    var oldValue = checkObject.value;
    var newValue = "";
    var temp = new Array();
    temp = checkObject.value.split('');
    for (i = 0; i < temp.length; i++) {
        if (isForFV(temp[i]))
            newValue += temp[i];
    }
    if (oldValue != newValue)
        checkObject.value = newValue;
}

function isForNumeric(char_) {
    return "0123456789.".indexOf(char_) >= 0;
}

function isForFV(char_) {
    return "FV".indexOf(char_) >= 0;
}

function processInput(checkObject) {
}
function processInputFV(checkObject) {
    if (checkObject.value.length > 1) {
        checkObject.value = checkObject.value.toUpperCase();
    }
    replaceNonFV(checkObject);
    if (checkObject.value.length > 1) {
        checkObject.value = "" + checkObject.value.substring(0, 1);
    }
}
function processInputBD(checkObject) {
    checkForNumeric(checkObject);
}

function isDateValid(dateVal) {
    var dateRegExp = /^\d{1,2}(\.)\d{1,2}\1\d{4}$/;
    if ($.trim(dateVal).length = 0) {
        return false;
    }
    if (!dateRegExp.test(dateVal)) {
        return false;
    } else {

    }
    return true;
}

function isEmailValid(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(email);
}

function updateSelectOptionsDblClickHandlers(selectOne, functionOne, selectTwo, functionTwo){
    $('#' + selectOne + ' option').unbind('dblclick');
    $('#' + selectOne + ' option').bind({
        dblclick : function(evt){
            if(typeof functionOne == 'function') {
                functionOne();
            }else{
                eval(functionOne + "()");
            }
            updateSelectOptionsDblClickHandlers(selectOne, functionOne, selectTwo, functionTwo);
        }
    });

    $('#' + selectTwo + ' option').unbind('dblclick');
    $('#' + selectTwo + ' option').bind({
        dblclick : function(evt){
            if(typeof functionTwo == 'function') {
                functionTwo();
            }else{
                eval(functionTwo + "()");
            }
            updateSelectOptionsDblClickHandlers(selectOne, functionOne, selectTwo, functionTwo);
        }
    });
}

function forceLength(checkObject, valueLength) {
    if (checkObject.value.length > valueLength) {
        checkObject.value = checkObject.value.substr(0, valueLength);
        highlightedElement = checkObject;
        //do not change backgroundColor, as it changes overall styling, not bg color only
        setTimeout("highlightedElement.style.color = 'black';", 500);
        checkObject.style.color = "red";
        $('#maxLengthNotification').show();
        $('#maxLengthNotification').fadeOut(1500);
    }
}

