
$(function() {
    
    $('.phone').mask('(999) 999-9999');
    
    $.ajax({
        type: 'post',
        url: '/ajax/upload_dir.php',
        dataType: 'json',
        success: function(data) {
            
            if (isset(data) && isset(data.upload_dir) && data.upload_dir.length > 0) {
                $('body').data('uploadDir', data.upload_dir);
                uploadDirEventCallback();
            }
            
        }
    });
    
});

function add_file() {
    
    var file_number = parseInt($('input[name="files"]').val()) + 1;
    
    $('input.file').parent().append(
        $('<input type=\'file\' class=\'file\' name=\'file_'+file_number+'\' /> <div class=\'remove\' >x</div>')
    );
    
    // $('input[name="files"]').val($('input.file').length);
    $('input[name="files"]').val(file_number);
    
    var disabled = validateRequired() ? 0 : 1;
    
    $('input.file').attr({disabled:disabled});
    
}

$.fn.remove_file = function() {
    
    var remove = $(this);
    var input = $(this).prev('input.file');
    
    remove.remove();
    input.remove();
    
    // $('input[name="files"]').val($('input.file').length);
    
}

function uploadDirEventCallback() {

    $('form#Project').submit(function(event) {
        
        event.preventDefault();
        
        $('div.status.email').hide();
        
        // Get all data from form
        
        var data = new Array();
        
        $('form#Project :input').each(function() {
            
            var tagName = $(this).tagName();
            
            if (tagName == 'input') {
                
                if ($(this).attr('name').length > 0) {
                    
                    var type = $(this).attr('type');
                    
                    if (type == 'checkbox' || type == 'radio') {
                        if ($(this).attr('checked')) {
                            data.push($(this).attr('name')+': "'+$(this).val()+'"');
                        }
                    }
                    else {
                        // type == 'text'
                        data.push($(this).attr('name')+': "'+$(this).val()+'"');
                    }
                    
                }
                
            } else { 
                // tagName == 'select' || tagName == 'textarea'
                data.push($(this).attr('name')+': "'+$(this).val()+'"');
            } 
            
        });
        
        // Get number of uploaded files
        
        if ($('input#uploadify').data('count') > 0) {
            
            data.push('uploads: "' + $('body').data('uploadDir') + '"');
            
        }
        
        // Compact data into array for post
        
        eval('data = {' + data.join(', ') + '};');
        
        // Upload project details
        
        $.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            data: data,
            dataType: 'text',
            success: function(data) {
                
                if (data == '1') {
                    
                    $('div.status.email.success').show();
                    
                    $('form#Project input.reset').click();
                    
                    validateForm();
                    
                }
                else {
                    
                    $('div.status.email.error').show();
                    
                }
                
            }
        });
        
        
    });
    
    $('form#Project input.required, form#Project textarea.required').bind('keyup keypress change blur', function() {
        
        validateForm();
        
    });
    
    $('input#uploadify').data('count', 0);
    
    $("#uploadify").uploadify({
		'auto'                : true,
        'buttonText'        : 'BROWSE',
		'cancelImg'         : 'img/uploadify.cancel.png',
		'multi'              : true,
        'onAllComplete'    : function() {
            $('.row .status.upload.success').show();
        },
        'onComplete'        : function () {
            $('input#uploadify').data('count', $('input#uploadify').data('count') + 1);
        },
        'onProgress'        : function() {
            $('.row .status.upload.success').hide();
        },
        'onSelect'          : function() {
            $('input#uploadify').uploadifySettings('folder', $('body').data('uploadDir'));
        },
		'queueID'            : 'fileQueue',
		'script'             : 'uploadify.php',
        'simUploadLimit'  : 10,
        'sizeLimit'         : 1000000000,
		'uploader'          : 'flash/uploadify.swf'
	});
    
    // Force validation
    validateForm();
    
}

function validateEmail(email) {
    
    var filter = /^([a-z0-9\_\.\-\+])+@([a-z0-9\.\-])+\.([a-z0-9])+$/i;
    
    return typeof email != 'undefined' && filter.test(email);
    
}

function validateAll() {
    
    
    
}

function validateForm() {
    
    var disabled = validateRequired() ? 0 : 1;
    
    $('input.submit').attr({disabled:disabled});
    
    // Uploadify
    if (disabled) {
        
        // Remove selected files from queue
        if ($('div#fileQueue div.cancel a').length > 0) {
            $('#uploadify').uploadifyClearQueue();
        }
        
        // Show required fields overlay
        $('div.row div.uploadifydisabled').show();
        
    } else {
        
        $('div.row div.uploadifydisabled').hide();
        
    }
    
    return true;
    
}

function validateRequired() {
    
    var first_name = $('form#Project input[name="first_name"]').val();
    var last_name = $('form#Project input[name="last_name"]').val();
    var business_name = $('form#Project input[name="business_name"]').val();
    
    var validated = true;
    
    if (typeof first_name == 'undefined' || first_name.length < 1) {
        
        // display error
        
        validated = false;
        
    }
    
    if (typeof last_name == 'undefined' || last_name.length < 1) {
    
        // display error
        
        validated = false;
        
    }
    
    if (!validateEmail($('form#Project input[name="email"]').val())) {
    
        // display error
        
        validated = false;
        
    }
    
    var description = $('form#Project textarea[name="description"]').val();
    
    if (typeof description == 'undefined' || description.length < 1) {
        
        // display error
        
        validated = false;
        
    }
    
    return validated;
    
}

