/**
 * General utility functions for forms.
 */
OOMBLE.util.FormUtils = function() {
    
    return {
        
        openUrl: function(url) {
            window.open(url);  
        },
        
        showElement: function(elementId, show) {
            var element = document.getElementById(elementId);
            if (element == null)
                return;
            var style = element.style;
            if (style == null)
                return;
            if (show)
                style.display = "block";
            else
                style.display = "none";
        },
        
        setFocus: function(elementId) {
            var element = document.getElementById(elementId);
            if (element == null)
                return;
            element.focus();
        },
        
        setText: function(elementId, text) {
            var element = document.getElementById(elementId);
            if (element == null)
                return;
            element.innerHTML = text;
        },
        
        setButtonText: function(elementId, text) {
            var element = document.getElementById(elementId);
            if (element == null)
                return;
            element.value = text;
        },
        
        clearError: function(textId, imageId) {
            OOMBLE.util.FormUtils.showElement(textId, false);
            OOMBLE.util.FormUtils.showElement(imageId, false);
        },
        
        setError: function(textId, imageId, errorMessage) {
            OOMBLE.util.FormUtils.setText(textId, errorMessage);
            OOMBLE.util.FormUtils.showElement(textId, true);
            OOMBLE.util.FormUtils.showElement(imageId, true);
        },
        
        styleElement: function(parentId, elementId, className) {
            var element = document.getElementById(elementId);
//            var element = OOMBLE.util.WebUtils.getElementById(parentId, elementId);
            if (element == null)
                return;
            element.className = className;
        },
        
        showText: function(isVisible, elementId, textMessage) {
            if (isVisible)
                OOMBLE.util.FormUtils.setText(elementId, textMessage);
            OOMBLE.util.FormUtils.showElement(elementId, isVisible);
        },
        
        isValidLength: function(string, minSize, maxSize) {
            if (string.length < minSize || string.length > maxSize)
                return false;
            else
                return true;
        },

        isValidSelectionWithError: function(selection, textId, imageId, errorMessage) {
            var isValid = false;
            for (var i = 0; i < selection.length; i++) {
                if (selection[i].checked) {
                    isValid = true;
                    break;
                }
            }
            if (!isValid)
                OOMBLE.util.FormUtils.setError(textId, imageId, errorMessage);
            return isValid;
        },

        isValidEmailAddress: function(email) {
            if (email == null)
                return false;
            var index = email.indexOf("@");
            if (index == -1)
                return false;
            index = email.indexOf(" ");
            if (index != -1)
                return false;
            return true;
        },
        
        isValidNumber: function(number, size) {
            if (number == null)
                return false;
            if (number.length != size)
                return false;
            for (var i = 0; i < number.length; i++) {
                var c = number.charAt(i);
                var a = c.charCodeAt(0);
                if (a < 48 || a > 57)
                    return false;
            }
            return true;
        },
        
        isValidPasswordLength: function(password) {
            return OOMBLE.util.FormUtils.isValidLength(password, 3, 32);
        },
            
        isValidPassword: function(password1, password2) {
            if (password1.length != password2.length)
                return false;
            else if (!OOMBLE.util.FormUtils.isValidPasswordLength(password1))
                return false;
            else if (password1 != password2)
                return false;
            var index = password1.indexOf(" ");
            if (index != -1)
                return false;
            return true;
        },

        isValidIndex: function(index) {
            return (index < 0) ? false : true;
        },
        
        isValidIndexWithError: function(index, textId, imageId, errorMessage) {
            var isValid = OOMBLE.util.FormUtils.isValidIndex(index);
            if (!isValid)
                OOMBLE.util.FormUtils.setError(textId, imageId, errorMessage);
            return isValid;
        },
            
        // Validate password.
        isValidPasswordsWithError: function(password1, password2, textId, imageId, errorMessage) {
            var isValid = OOMBLE.util.FormUtils.isValidPassword(password1, password2);
            if (!isValid)
                OOMBLE.util.FormUtils.setError(textId, imageId, errorMessage);
            return isValid;
        },
        
        isValidPasswordWithError: function(password, textId, imageId, errorMessage) {
            var isValid = OOMBLE.util.FormUtils.isValidLength(password, 3, 32);
            if (!isValid)
                OOMBLE.util.FormUtils.setError(textId, imageId, errorMessage);
            return isValid;
        },

        isValidPhoneNumber: function(num1, num2, num3) {
            var isValid = true;
            if (!OOMBLE.util.FormUtils.isValidNumber(num1, 3))
                isValid = false;
            if (!OOMBLE.util.FormUtils.isValidNumber(num2, 3))
                isValid = false;
            if (!OOMBLE.util.FormUtils.isValidNumber(num3, 4))
                isValid = false;
            return isValid;
        },
        
        // Validate phone number.
        isValidPhoneNumberWithError: function(num1, num2, num3, textId, imageId, errorMessage) {
            var isValid = OOMBLE.util.FormUtils.isValidPhoneNumber(num1, num2, num3);
            if (!isValid)
                OOMBLE.util.FormUtils.setError(textId, imageId, errorMessage);
            return isValid;
        },
        
        // Validatie user name
        isValidUserNameWithError: function(userName, textId, imageId, errorMessage) {
            var isValid = OOMBLE.util.FormUtils.isValidLength(userName, 1, 32);
            if (!isValid)
                OOMBLE.util.FormUtils.setError(textId, imageId, errorMessage);
            return isValid;
        },
        
        // Validate first name.
        isValidFirstName: function(firstName) {
            return OOMBLE.util.FormUtils.isValidLength(firstName, 1, 32);
        },

        // Validate last name.
        isValidLastName: function(lastName) {
            return OOMBLE.util.FormUtils.isValidLength(lastName, 1, 32);
        },

        // Validate first name.
        isValidFirstNameWithError: function(firstName, textId, imageId, errorMessage) {
            var isValid = OOMBLE.util.FormUtils.isValidFirstName(firstName);
            if (!isValid)
                OOMBLE.util.FormUtils.setError(textId, imageId, errorMessage);
            return isValid;
        },
        
        // Validate last name.
        isValidLastNameWithError: function(lastName, textId, imageId, errorMessage) {
            var isValid = OOMBLE.util.FormUtils.isValidLastName(lastName);
            if (!isValid)
                OOMBLE.util.FormUtils.setError(textId, imageId, errorMessage);
            return isValid;
        },
        
        // Validate email address.
        isValidEmailAddressWithError: function(email, textId, imageId, errorMessage) {   
            var isValidLen = OOMBLE.util.FormUtils.isValidLength(email, 1, 64);
            var isValidAddress = OOMBLE.util.FormUtils.isValidEmailAddress(email);
            var isValid = isValidLen && isValidAddress;
            if (!isValid)
                OOMBLE.util.FormUtils.setError(textId, imageId, errorMessage);
            return isValid;
        }
    };
}();
