$(document).ready(function() {
    // As Javascript is enabled, change the colour of the username fields
    if ($('input[name=username]').length > 0) $('input[name=username]').addClass("description");
    if ($('input[name=password]').length > 0) $('input[name=password]').addClass("description");
            
    // Clear text on field focus
    $('input[name=username]').bind("focus", function(e) {
        if ($(this).val() == 'Email') $(this).val("");
        $('input[name=username]').removeClass("description");    
    });
    $('input[name=password]').bind("focus", function(e) {
        if ($(this).val() == 'Password') $(this).val("");
        $('input[name=password]').removeClass("description");     
    });
            
    // Reinstate default text on field blur, if nothing else entered
    $('input[name=username]').bind("blur", function(e) {
        if ($(this).val() == '') {
            $(this).val("Email");
            $('input[name=username]').addClass("description");    
        }
    });
    $('input[name=password]').bind("blur", function(e) {
        if ($(this).val() == '') {
            $(this).val("Password");
            $('input[name=password]').addClass("description");     
        }
    });
    
    // Add / Amend Product Validation
    $('form.productForm').bind('submit', function(e) {
        // Check form fields
        if ($('input[name=niche]', this).val() == '' || $('input[name=title]', this).val() == '' || $('input[name=keywords]', this).val() == '' || $('input[name=researchURLs]', this).val() == '' || $('textarea[name=instructions]', this).val() == '') {
            if ($('div.error').length == 0) {
                $(this).before('<div class="error"><p>Please complete all the below fields.</p></div>');
            }
            e.preventDefault();
        }
    });
});