jQuery(document).ready(function() {
    var moneyFields = $('input.money');
    if (moneyFields) {
        moneyFields.blur(checkMoney);
        moneyFields.keydown(validateMoney);
    }
});


function clearFormElements(ele) {
    $jQuery(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $jQuery(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
}

function applyErrorHighlight(id) {
    if (id.indexOf('#') != -1) {
        $(id).addClass('errHilite');
    } else {
        // it's the name of a form field not an element id
        $(':input*[name="' + id + '"]').addClass('errHilite');
    }

}

function submitForm(formName, validate) {
    if (validate) {
        if (validateForm(document[formName],false,true,false,true,8)) {
            document[formName].submit();
        }
    }
}

function dumpObj(obj, name, indent, depth) {
    if (depth > 10) {
        return indent + name + ": <Maximum Depth Reached>\n";
    }
    if (typeof obj == "object") {
        var child = null;
        var output = indent + name + "\n";
        indent += "\t";
        for (var item in obj)
        {
            try {
                child = obj[item];
            } catch (e) {
                child = "<Unable to Evaluate>";
            }
            if (typeof child == "object") {
                output += dumpObj(child, item, indent, depth + 1);
            } else {
                output += indent + item + ": " + child + "\n";
            }
        }
        return output;
    } else {
        return obj;
    }
}

function forgotPassword(username) {
    if (! username) {
        username = prompt('Please enter your Username');
    }
    if (username) {
        jQuery.getJSON('forgotPassword?callback=?',
                       {username: username},
                       function(data) {
                            if (data.found) {
                                alert('Instructions for logging into your account have been sent to the email address on file.');
                            } else {
                                alert('No such user was found in our database.');
                            }
                       });
    }
}

function forgotUsername() {
    var email = prompt('Enter your email address');
    if (email) {
        jQuery.getJSON('findUsername?callback=?',
                       {email: email},
                       function(data) {
                            if (data.found) {
                                alert('Your username has been sent to ' + email);
                            } else {
                                alert('The email address ' + email + ' was not found in our database.');
                            }
                       });
    }
}


var floatPattern = /[0-9\.]/g;

function checkMoney() {
    var validated = '';
    var chars = $(this).val().split('');
    for (var i = 0; i < chars.length; i++) {
        if (chars[i].match(floatPattern)) {
            validated = validated + chars[i];
        }
    }
    $(this).val(validated);
}

function validateMoney(event) {
    if (! event) {
        event = window.event;
    }
    var code = event.keyCode || event.which;
    var amount = $(this).val();
    var decimalPlaces = 0;
    var containsDecimal = amount.indexOf('.') != -1;
    if (containsDecimal) {
        decimalPlaces = amount.length - amount.indexOf('.');
    }
    if ((code > 47 && code < 58 && ! event.shiftKey && decimalPlaces < 3)
            // only allow one decimal
        || (event.keyCode === 190 && ! containsDecimal)
        || event.keyCode === 8 // allow backspace
        || event.keyCode === 9 // allow tab
        || event.keyCode === 35 // allow home
        || event.keyCode === 36 // allow end
        || event.keyCode === 37 // allow left arrow
        || event.keyCode === 39 // allow right arrow
        || event.keyCode === 27) // allow escape
    {
        return true;
    } else {
        event.returnValue = false;
        if (event.preventDefault) {
            event.preventDefault();
        }
        return false;
    }
}


function toggleGrid() {
    var toggle = document.getElementById('toggleGrid');
    var container;
    if(toggle.innerHTML == 'Hide Grid') {
        toggle.innerHTML = 'Show Grid';
        ripClass('');
    }
    else if(toggle.innerHTML == 'Show Grid') {
        toggle.innerHTML = 'Hide Grid';
        ripClass(' showgrid');
    }
}

function ripClass(c) {
    if(document.getElementsByClassName) {
        container = document.getElementsByClassName('container');
        for(var i=0; i < container.length; i++) {
            container[i].className = 'container' + c;
        }
    }
    else {
        container = document.getElementsByTagName('div');
        for(var i=0; i < container.length; i++) {
            if(container[i].className == 'container') {
                container[i].className = 'container' + c;
            }
            else if(container[i].className == 'container showgrid') {
                container[i].className = 'container';
            }
        }
    }
}

