$(document).ready( function() {
    $('#company-name').focus( function() {
        if ($(this).val() == 'Yrityksesi nimi*') {
            $(this).val('');
        }
    });
    $('#company-name').blur( function() {
        if ($(this).val() == '') {
            $(this).val('Yrityksesi nimi*');
        }
    });
    $('#company-address').focus( function() {
        if ($(this).val() == 'Yrityksesi käyntiosoite*') {
            $(this).val('');
        }
    });
    $('#company-address').blur( function() {
        if ($(this).val() == '') {
            $(this).val('Yrityksesi käyntiosoite*');
        }
    });
    $('#company-city').focus( function() {
        if ($(this).val() == 'Yrityksesi toimipaikka*') {
            $(this).val('');
        }
    });
    $('#company-city').blur( function() {
        if ($(this).val() == '') {
            $(this).val('Yrityksesi toimipaikka*');
        }
    });
    $('#company-ynumber').focus( function() {
        if ($(this).val() == 'Y-tunnus*') {
            $(this).val('');
        }
    });
    $('#company-ynumber').blur( function() {
        if ($(this).val() == '') {
            $(this).val('Y-tunnus*');
        }
    });
    $('#contact-firstname').focus( function() {
        if ($(this).val() == 'Yhteyshenkilön etunimi*') {
            $(this).val('');
        }
    });
    $('#contact-firstname').blur( function() {
        if ($(this).val() == '') {
            $(this).val('Yhteyshenkilön etunimi*');
        }
    });
    $('#contact-lastname').focus( function() {
        if ($(this).val() == 'Yhteyshenkilön sukunimi*') {
            $(this).val('');
        }
    });
    $('#contact-lastname').blur( function() {
        if ($(this).val() == '') {
            $(this).val('Yhteyshenkilön sukunimi*');
        }
    });
    $('#contact-email').focus( function() {
        if ($(this).val() == 'Yhteyshenkilön sähköposti*') {
            $(this).val('');
        }
    });
    $('#contact-email').blur( function() {
        if ($(this).val() == '') {
            $(this).val('Yhteyshenkilön sähköposti*');
        }
    });
    $('#contact-phone').focus( function() {
        if ($(this).val() == 'Yhteyshenkilön puhelinnumero') {
            $(this).val('');
        }
    });
    $('#contact-phone').blur( function() {
        if ($(this).val() == '') {
            $(this).val('Yhteyshenkilön puhelinnumero');
        }
    });

    $('#frm-tabletv input[type=image]').click( function(e) {
        e.preventDefault();

        if ($('#contact-checkbox:checked').val() !== null) {
            var contactcheckbox = 'yes';
        } else {
            var contactcheckbox = 'no';
        }

        var organisationname = $('#organisation-name').val();
        var companyname      = $('#company-name').val();
        var companyaddress   = $('#company-address').val();
        var companycity      = $('#company-city').val();
        var companyynumber   = $('#company-ynumber').val();
        var contactfirstname = $('#contact-firstname').val();
        var contactlastname  = $('#contact-lastname').val();
        var contactemail     = $('#contact-email').val();
        var contactphone     = $('#contact-phone').val();
        var error            = false;

        if (organisationname == '') {
            $('#organisation-name').css('backgroundColor', '#d66');
            error = true;
        }
        if (companyname == '' || companyname == 'Yrityksesi nimi*') {
            $('#company-name').css('backgroundColor', '#d66');
            error = true;
        }
        if (companyaddress == '' || companyaddress == 'Yrityksesi käyntiosoite*') {
            $('#company-address').css('backgroundColor', '#d66');
            error = true;
        }
        if (companycity == '' || companycity == 'Yrityksesi toimipaikka*') {
            $('#company-city').css('backgroundColor', '#d66');
            error = true;
        }
        if (companyynumber == '' || companyynumber == 'Y-tunnus*') {
            $('#company-ynumber').css('backgroundColor', '#d66');
            error = true;
        }
        if (contactfirstname == '' || contactfirstname == 'Yhteyshenkilön etunimi*') {
            $('#contact-firstname').css('backgroundColor', '#d66');
            error = true;
        }
        if (contactlastname == '' || contactlastname == 'Yhteyshenkilön sukunimi*') {
            $('#contact-lastname').css('backgroundColor', '#d66');
            error = true;
        }
        if (contactemail == '' || contactemail == 'Yhteyshenkilön sähköposti*') {
            $('#contact-email').css('backgroundColor', '#d66');
            error = true;
        }

        if (!error) {
            $.post('/yrittajille/edut-kisa.php', {
                organisationname: organisationname,
                companyname: companyname,
                companyaddress: companyaddress,
                companycity: companycity,
                companyynumber: companyynumber,
                contactfirstname: contactfirstname,
                contactlastname: contactlastname,
                contactemail: contactemail,
                contactphone: contactphone,
                contactcheckbox: contactcheckbox
            });

            $('#competition-tabletv').hide();
            $('#competition-tabletv-thankyou').show();
						$('.edut').css('height', '225px')
            return true;
        } else {
            return false;
        }
    });
		$('a#contact-competition').click(function() {
			$('#competition-tabletv').show();
			$('.edut').css('height', '325px')
		});
});
