$().ready(function(){
    if ($('#members')) {
        $('#members form').submit(function(e) {
            e.preventDefault();
            if($('#members input[name=amount]:checked').length != 0) {
                this.submit();
            } else {
                alert('Please select a package before continuing. Thanks.');
            }
        });
    }
    if ($('.cancel')) {
        $('.cancel').click(function(e) {
            e.preventDefault();
            window.location = $(this).attr('rel');
        });
    }
    if($('#sf_guard_user_is_corp')) {
        $('#sf_guard_user_is_corp').attr('checked', false);
    }
    if($('#sf_guard_user_ata_cert')) {
        $('#sf_guard_user_ata_cert').attr('checked', false);
    }
    if($('#sf_guard_user_pair')) {
        $('#sf_guard_user_pair').closest('form').submit(function(e) {
            if(!$('#sf_guard_user_is_corp').checked && invalidPairs($('#sf_guard_user_pair').val())) {
                e.preventDefault();
                alert('You can have a maximum of 5 language pairs.');
            }
        });
    }
if($('form .delete')) {
        $('form .delete').click(function(e) {
            var name = $(this).closest('p').siblings('input[type=file]').attr('name');
            $(this).closest('p').siblings('input[type=hidden]').attr('name', name).val('');
            $(this).closest('p').siblings('input[type=file]').remove();
        });
    }
});

function invalidPairs(pair) {
    var chars = pair.split();
    var charsLength = chars.length;
    var sanStr = '';
    for (i=0;i<charsLength;i++) {
        sanStr += chars[i].replace(' ', '');
    }
    var pairs = sanStr.split(',');
    if(pairs.length > 5) {
        if(pairs[5] != '') {
            return true;
        }
    }
    return false;
}
