if(typeof(String.prototype.trim) === "undefined")
{
    String.prototype.trim = function() 
    {
        return String(this).replace(/^\s+|\s+$/g, '');
    };
}

function hide_my_label() {
    $(this).prev().hide();
}

function show_my_label() {
    $(this).val($(this).val().trim());
    if (!$(this).val()) {
        $(this).prev().show();
    }
}

function has_something(selectstr) {
    return Boolean($(selectstr).val().trim());
}

function check_contact_form() {
    var problem = '';

    if (!(has_something('#contactform input[name=phone]')
          || has_something('#contactform input[name=email]')
          || has_something('#contactform input[name=street]')))
        problem = 'Please provide a way for us to contact you';

    if (!has_something('#contactform textarea[name=description]'))
        problem = 'Please add a description of your project or question';

    if (!has_something('#contactform input[name=last_name]'))
        problem = 'Please enter your name';

    if (!has_something('#contactform input[name=first_name]'))
        problem = 'Please enter your name';

    if (problem) {
        $('#contactform').submit(function() { return false; });
        $('#contactform div.hint').html(problem);
        $('#contactform button').addClass('showable').addClass('gray');
    } else {
        $('#contactform').unbind();
        $('#contactform button').removeClass('showable').removeClass('gray');
    }
}

function show_contact_hint() {
    if ($(this).hasClass('showable'))
        $('#contactform button div').show();
}

function hide_contact_hint() {
    $('#contactform button div').hide();
}

$(document).ready(function(){
    check_contact_form();
    hide_contact_hint();
    $('#contactform input').focus(hide_my_label);
    $('#contactform input').change(check_contact_form);
    $('#contactform input').keyup(check_contact_form);
    $('#contactform input').blur(show_my_label);
    $('#contactform textarea').focus(hide_my_label);
    $('#contactform textarea').change(check_contact_form);
    $('#contactform textarea').keyup(check_contact_form);
    $('#contactform textarea').blur(show_my_label);
    $('#contactform button').mouseover(show_contact_hint);
    $('#contactform button').mouseout(hide_contact_hint);
});

