/**
 * GENERAL
 */
GENERAL = {};

/**
 * Constants
 */
GENERAL.constants = {};
GENERAL.constants.REGEXP_IS_DECIMAL_AMOUNT = /(^[0-9]\d{0,2}[\,\.]\d{1,3}$)|(^[1-9]\d{0,2}$)/;
GENERAL.constants.REGEXP_IS_AMOUNT = /^[1-9]{1,1}[0-9]{0,2}$/;
GENERAL.constants.REGEXP_IS_COUNTRY_CODE = /^[1-9]{1,1}[0-9]{0,4}$/;
GENERAL.constants.REGEXP_IS_EMAIL = /^([^<>@]+)@([^<>@]+)([.])(\w{2,10})$/;
GENERAL.constants.REGEXP_IS_HTML = /[<>\^]/;
GENERAL.constants.REGEXP_IS_URL = /(http|ftp|https):\/\/[\w-_]+(\.[\w-_]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?/;
GENERAL.constants.REGEXP_IS_NUMBERS = /^\d+$/;
GENERAL.constants.REGEXP_IS_PASSWORD = /^[a-zA-Z0-9]+$/;

/**
 * Validators
 */
GENERAL.validators = {};
GENERAL.validators.isEqual = function(s1, s2) {
    return (s1 == s2);
}
GENERAL.validators.isValidDateInterval = function(toDateStr, fromDateStr) {

    var toDateArr = toDateStr.split("-");
    var fromDateArr = fromDateStr.split("-");
    var todayDate = new Date();

    var toDD = parseInt(toDateArr[0], 10);
    var toMM = parseInt(toDateArr[1], 10) - 1;
    var toYY = parseInt(toDateArr[2]);

    var fromDD = parseInt(fromDateArr[0], 10);
    var fromMM = parseInt(fromDateArr[1], 10) - 1;
    var fromYY = parseInt(fromDateArr[2]);

    var toDate = new Date(toYY, toMM, toDD, 0, 0, 0, 0);
    var fromDate = new Date(fromYY, fromMM, fromDD, 0, 0, 0, 0);
    var todayDate = new Date();

    todayDate.setHours(0, 0, 0, 0);

    if (((toDate.getTime() - fromDate.getTime()) < 36806400000) && (todayDate - fromDate < 36806400000)) {
        if (fromDate.getTime() > (todayDate.getTime() - (90 * 24 * 60 * 60 * 1000))) {
            return (toDate.getTime() <= todayDate.getTime() && fromDate.getTime() < toDate.getTime()) ? true : false;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

/**
 * Form and error handling
 */
GENERAL.curErrorDivElm = null;
GENERAL.curErrorTextDivElm = null;
GENERAL.resetError = function() {
    if (DynInterface.initialized && !DynInterface.loaded) return;
    if (GENERAL.curErrorDivElm != null) {
        GENERAL.curErrorDivElm.style.display = 'none';
    }
    if (GENERAL.curErrorElm != null) {
        GENERAL.curErrorElm.obj.style.backgroundColor = 'transparent';
    }
}
GENERAL.errorHandler = function() {
    GENERAL.resetError();
    GENERAL.curErrorElm = this.errorElement;
    GENERAL.curErrorDivElm = document.getElementById(this.errorElement.form.name + '_error_DIV');
    GENERAL.curErrorTextDivElm = document.getElementById(this.errorElement.form.name + '_error_text_DIV');
    if (GENERAL.curErrorDivElm != null) {
        if (GENERAL.curErrorTextDivElm != null) {
            GENERAL.curErrorTextDivElm.innerHTML = this.errorElement.errorMessage;
        }
        GENERAL.curErrorDivElm.style.display = 'block';
    }
    GENERAL.curErrorElm.obj.style.backgroundColor = '#E3E8EE';
}
GENERAL.showErrorMessage = function(errorDivId, errorTextDivId, errorMessage) {
    GENERAL.resetError();
    GENERAL.curErrorDivElm = document.getElementById(errorDivId);
    GENERAL.curErrorTextDivElm = document.getElementById(errorTextDivId);
    if (GENERAL.curErrorDivElm != null) {
        if (GENERAL.curErrorTextDivElm != null) {
            GENERAL.curErrorTextDivElm.innerHTML = errorMessage;
        }
        GENERAL.curErrorDivElm.style.display = 'block';
    }
}
GENERAL.enableFormElement = function(element) {
    element.getObject().disabled = false;
    element.getObject().style.backgroundColor = '#FFFFFF';
    element.getObject().style.color = '#000000';
}
GENERAL.disableFormElement = function(element) {
    element.getObject().disabled = true;
    element.getObject().style.backgroundColor = '#EFEFEF';
    element.getObject().style.color = '#AAAAAA';
}


/**
 * Table paging
 */
GENERAL.setPagingNumber = function() {
    var curPaymentNumber = document.getElementById(DynLayerPager.current.id + "_aPaymentNumber" + DynLayerPager.current.getPreviousNumber());
    if (curPaymentNumber != null) {
        curPaymentNumber.className = 'paging';
    }
    var newPaymentNumber = document.getElementById(DynLayerPager.current.id + "_aPaymentNumber" + DynLayerPager.current.getCurrentNumber());
    if (newPaymentNumber) {
        newPaymentNumber.className = 'paging-selected';
    }
}

/**
 * Country / State selector handler
 */
GENERAL.countryStateSelector = {};
GENERAL.countryStateSelector.usElement = null;
GENERAL.countryStateSelector.canadaElement = null;
GENERAL.countryStateSelector.onchange = function() {
    var usDiv = document.getElementById('us_states_div');
    var canadaDiv = document.getElementById('canada_states_div');
    var usElement = GENERAL.countryStateSelector.usElement;
    var canadaElement = GENERAL.countryStateSelector.canadaElement;
    if (this.options[this.selectedIndex].text == 'United States') {
        usDiv.style.display = 'block';
        usElement.enable();
    } else {
        usDiv.style.display = 'none';
        usElement.disable();
    }
    if (this.options[this.selectedIndex].text == 'Canada') {
        canadaDiv.style.display = 'block';
        canadaElement.enable();
    } else {
        canadaDiv.style.display = 'none';
        canadaElement.disable();
    }
}

/**
 * Initialize radio buttons
 */
GENERAL.initRadioButtons = function(form) {
    for (var i = 0; i < form.elements.length; i++) {
        if (form.elements[i].type.toLowerCase() == 'radio' && form.elements[i].checked) {
            if (form.elements[i].onclick && form.elements[i].onclick != null) {
                form.elements[i].onclick();
            }
        }
    }
}
