$(document).ready(function() {
    var l = $('#box-top .left').height();

    if (l < 200) {
        $('#box-top .left').css('height', 'auto');
    }

    var asiakkuus, asia, yhaihe, paihe, yrityskoko, syrpalauteaihe, yhteydenotto;

    $('#yhteydenottoform input[type=text]').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
    $('#yhteydenottoform textarea').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
    $('#yhteydenottoform select').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });

    function hideall() {
        $('.valinta-asia').hide();

        
        $('.yritys-tiedot').hide();

        $('.yhteydenotto-aihe').hide();
        $('.yhteydenotto-asia').hide();
        $('.palaute-tiedot').hide();
        $('.palaute-aihe').hide();
        $('.palaute-tapa').hide();
        $('.yhteydenotto-tiedot').hide();
        $('.yhteydenotto-tapa').hide();
        $('.palaute-tiedot-yritys').hide();
        $('#yhteydenotto_error').hide();

        $('.sopnumero').hide();
        $('.yh-yhnumero').hide();
        $('.yh-puhelin').hide();

        $('.viesti-info').hide();
        $('.viesti-info-palaute').hide();

        $('.yhteydenotto-tapa-puhelin').hide();

        $('.submit').hide();
    }
	
	function validateEmail(email){
		var emailRegEx = /^([a-zA-Z0-9])(([a-zA-Z0-9])*([\._\+-])*([a-zA-Z0-9]))*@(([a-zA-Z0-9\-])+(\.))+([a-zA-Z]{2,4})+$/;
		return  email.search(emailRegEx)!=-1;
	}
    
    // Recursive hide procedures, to handle the show / hide better
    function hideAihe() {
        $('#yh-palaute-aihe option:first').attr('selected', 'selected');
        $('#syr-palaute-aihe option:first').attr('selected', 'selected');
        $('#palaute-aihe option:first').attr('selected', 'selected');
        $('#aihe option:first').attr('selected', 'selected');
        $('#yh-yli10-yhteydenotto-aihe option:first').attr('selected', 'selected');
        
        $(".yh-yli10-yhteydenotto-aihe").hide();
        $('.palaute-aihe').hide();
        $('.yh-yr-palaute-aihe').hide();
        $('.yhteydenotto-aihe').hide();
        // also the questions below it
        $('.yhteydenotto-asia-mv').hide();
        $('.yhteydenotto-asia-lk').hide();
        
        // hides also the contact fields, since they cannot be shown if the aihe is hidden
        hideContactFields();
    }
    
    function hideContactFields() {
        $(".palaute-tiedot").hide();
        $(".yhteydenotto-tiedot").hide();
        
        // empty the values ?
        $(".palaute-tiedot input").each(function() { $(this).val(""); });
        $(".yhteydenotto-tiedot").each(function() { $(this).val(""); });
        
        // hides also the questions
        hideContactQuestion();
    }
    
    function hideContactQuestion() {
        $(".yhteydenotto-tapa").hide();
        $('.yhteydenotto-tapa-puhelin').hide();
        $(".palaute-tapa").hide();
    }
    
    function isAsiaChecked() {
        if($('.asiakkuus:checked').val() == 'henkiloasiakas') {
            $('.asiakkuus:checked').attr('checked', '');
            return true;

            hideall();
            asiakkuus = 'henkiloasiakas';
            
            
            $('.yritys-tiedot').hide();
            $('.valinta-asia').show();
            
            if ($('.asia:checked').val() == 'yhteydenotto') {
                asia = 'yhteydenotto';
                
                $('.palaute-tiedot').hide();
                $('.palaute-aihe').hide();
                $('.viesti-info').show();
                $('.viesti-info-palaute').hide();
                $('.yh-yr-palaute-aihe').hide();
                $('.yhteydenotto-aihe').show();
                $('.yhteydenotto-tiedot').show();
                $('.yhteydenotto-tapa').show();
                $('.submit').show();
            }
        }
    }
    
    $('input.asiakkuus').click( function() {
        asiakkuus = $(this).val();
        $('.hint').hide();

        if (asiakkuus == 'yritysasiakas') {

            hideall();

            $('.yhteydenotto-aihe').show();
        } else if (asiakkuus == 'henkiloasiakas') {
            window.location="yksityisille.php";
            return true;
            hideall();

            
            $('.yritys-tiedot').hide();
            $('.valinta-asia').show();
        }
        $('input.asia').attr('checked', '');
        $('input.yhdeydenotto').attr('checked', '');
        hideContactFields();
    });


    $('#aihe').change( function() {
        yhaihe = $(this).val();
		$('input.mv-asia').attr('checked', '');
		$('.yhteydenotto-tapa').hide();
		$('.yhteydenotto-tiedot').hide();
		$('.yhteydenotto-asia').hide();
		$('.submit').hide();
		$('.right').hide();

        if (yhaihe == 'matkaviestinta_ja_mobiililaajakaista'|| yhaihe == 'laajakaista_ja_lankapuhelin' || yhaihe == 'muut') {
			$('.yhteydenotto-asia').show();
        }
		else if(yhaihe == 'asennus'){
			$('.yhteydenotto-tapa').show();
            $('.yhteydenotto-tiedot').show();
			$('.right').show();
			$('.submit').show();
        }
		else if(yhaihe == 'raportointipalvelu'){
         	$('.right').show();            
			$('.submit').show();  
			$('.yhteydenotto-tiedot').show();
        }
		

    });
	
	 $('input.mv-asia').click( function() {
		 asia = $(this).val();
		 if(asia=='tilaus'){window.location="http://www.oma.elisa.fi/yrityksille";}
		 else{
         $('.yhteydenotto-tiedot').show();
		 $('.submit').show();
		 $('.right').show();
		 }
		 return true;
    });
	



    $('#yhteydenottoform').submit( function() {
        var error;

        if (asiakkuus == 'henkiloasiakas') {
            
            if($('.yhteydenotto:checked').val() == null){
                error = true;
                $('#yhteydenotto_error').show();
            } else {
                $('#yhteydenotto_error').hide();            
            }
        
            if(asia == 'palaute' && $('.yhteydenotto:checked').val() == 'email'){
                if ($('#aihe').val() == '') {
                    error = true;
                    $('#aihe').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('#aihe').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }

                if ($('#viesti').val() == '') {
                    error = true;
                    $('#viesti').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('#viesti').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }

                if ($('#aihe').val() == 'matkaviestinta') {
                    if ($('.yhteydenotto-tiedot #puhelin').val() == '') {
                        error = true;
                        $('.yhteydenotto-tiedot #puhelin').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                    } else {
                        $('.yhteydenotto-tiedot #puhelin').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                    }

                    if ($('input[name=mv-asia]:checked').val() == undefined) {
                        error = true;
                        $('.yhteydenotto-asia-mv').css({ color:"#e99" });
                    } else {
                        $('.yhteydenotto-asia-mv').css({ color:"#fff" });
                    }

                } else if ($('#aihe').val() == 'laajakaista') {
                    if ($('.yhteydenotto-tiedot #yh_tai_sopimusnumero').val() == '') {
                        error = true;
                        $('.yhteydenotto-tiedot #yh_tai_sopimusnumero').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                    } else {
                        $('.yhteydenotto-tiedot #yh_tai_sopimusnumero').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                    }

                    if ($('input[name=lk-asia]:checked').val() == undefined) {
                        error = true;
                        $('.yhteydenotto-asia-lk').css({ color:"#e99" });
                    } else {
                        $('.yhteydenotto-asia-lk').css({ color:"#fff" });
                    }
                } else if ($('#aihe').val() == 'lankapuhelin') {
                    if ($('.yhteydenotto-tiedot #yh_tai_sopimusnumero').val() == '') {
                        error = true;
                        $('.yhteydenotto-tiedot #yh_tai_sopimusnumero').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                    } else {
                        $('.yhteydenotto-tiedot #yh_tai_sopimusnumero').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                    }

                    if ($('.yhteydenotto-tiedot #puhelin').val() == '') {
                        error = true;
                        $('.yhteydenotto-tiedot #puhelin').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                    } else {
                        $('.yhteydenotto-tiedot #puhelin').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                    }
                } else if ($('#aihe').val() == 'tv') {
                    if ($('.yhteydenotto-tiedot #yh_tai_sopimusnumero').val() == '') {
                        error = true;
                        $('.yhteydenotto-tiedot #yh_tai_sopimusnumero').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                    } else {
                        $('.yhteydenotto-tiedot #yh_tai_sopimusnumero').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                    }
                }

                if ($('.yhteydenotto-tiedot #etunimi').val() == '') {
                    error = true;
                    $('.yhteydenotto-tiedot #etunimi').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('.yhteydenotto-tiedot #etunimi').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }

                if ($('.yhteydenotto-tiedot #sukunimi').val() == '') {
                    error = true;
                    $('.yhteydenotto-tiedot #sukunimi').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('.yhteydenotto-tiedot #sukunimi').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }

                if ($('.yhteydenotto-tiedot #katuosoite').val() == '') {
                    error = true;
                    $('.yhteydenotto-tiedot #katuosoite').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('.yhteydenotto-tiedot #katuosoite').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }

                if ($('.yhteydenotto-tiedot #postinumero').val() == '') {
                    error = true;
                    $('.yhteydenotto-tiedot #postinumero').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('.yhteydenotto-tiedot #postinumero').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }

                if ($('.yhteydenotto-tiedot #postitoimipaikka').val() == '') {
                    error = true;
                    $('.yhteydenotto-tiedot #postitoimipaikka').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('.yhteydenotto-tiedot #postitoimipaikka').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }

                if ($('.yhteydenotto:checked').val() == 'email' && $('.yhteydenotto-tiedot #email').val() == '') {
                    error = true;
                    $('.yhteydenotto-tiedot #email').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('.yhteydenotto-tiedot #email').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }
            } else if (asia == 'yhteydenotto'){
                if ($('#aihe').val() == '') {
                    error = true;
                    $('#aihe').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('#aihe').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }

                if ($('#viesti').val() == '') {
                    error = true;
                    $('#viesti').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('#viesti').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }

                if ($('#aihe').val() == 'matkaviestinta') {
                    if ($('.yhteydenotto-tiedot #puhelin').val() == '') {
                        error = true;
                        $('.yhteydenotto-tiedot #puhelin').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                    } else {
                        $('.yhteydenotto-tiedot #puhelin').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                    }

                    if ($('input[name=mv-asia]:checked').val() == undefined) {
                        error = true;
                        $('.yhteydenotto-asia-mv').css({ color:"#e99" });
                    } else {
                        $('.yhteydenotto-asia-mv').css({ color:"#fff" });
                    }

                } else if ($('#aihe').val() == 'laajakaista') {
                    if ($('.yhteydenotto-tiedot #yh_tai_sopimusnumero').val() == '') {
                        error = true;
                        $('.yhteydenotto-tiedot #yh_tai_sopimusnumero').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                    } else {
                        $('.yhteydenotto-tiedot #yh_tai_sopimusnumero').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                    }

                    if ($('input[name=lk-asia]:checked').val() == undefined) {
                        error = true;
                        $('.yhteydenotto-asia-lk').css({ color:"#e99" });
                    } else {
                        $('.yhteydenotto-asia-lk').css({ color:"#fff" });
                    }
                } else if ($('#aihe').val() == 'lankapuhelin') {
                    if ($('.yhteydenotto-tiedot #yh_tai_sopimusnumero').val() == '') {
                        error = true;
                        $('.yhteydenotto-tiedot #yh_tai_sopimusnumero').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                    } else {
                        $('.yhteydenotto-tiedot #yh_tai_sopimusnumero').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                    }

                    if ($('.yhteydenotto-tiedot #puhelin').val() == '') {
                        error = true;
                        $('.yhteydenotto-tiedot #puhelin').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                    } else {
                        $('.yhteydenotto-tiedot #puhelin').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                    }
                } else if ($('#aihe').val() == 'tv') {
                    if ($('.yhteydenotto-tiedot #yh_tai_sopimusnumero').val() == '') {
                        error = true;
                        $('.yhteydenotto-tiedot #yh_tai_sopimusnumero').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                    } else {
                        $('.yhteydenotto-tiedot #yh_tai_sopimusnumero').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                    }
                }

                if ($('.yhteydenotto-tiedot #etunimi').val() == '') {
                    error = true;
                    $('.yhteydenotto-tiedot #etunimi').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('.yhteydenotto-tiedot #etunimi').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }

                if ($('.yhteydenotto-tiedot #sukunimi').val() == '') {
                    error = true;
                    $('.yhteydenotto-tiedot #sukunimi').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('.yhteydenotto-tiedot #sukunimi').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }

                if ($('.yhteydenotto-tiedot #katuosoite').val() == '') {
                    error = true;
                    $('.yhteydenotto-tiedot #katuosoite').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('.yhteydenotto-tiedot #katuosoite').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }

                if ($('.yhteydenotto-tiedot #postinumero').val() == '') {
                    error = true;
                    $('.yhteydenotto-tiedot #postinumero').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('.yhteydenotto-tiedot #postinumero').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }

                if ($('.yhteydenotto-tiedot #postitoimipaikka').val() == '') {
                    error = true;
                    $('.yhteydenotto-tiedot #postitoimipaikka').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('.yhteydenotto-tiedot #postitoimipaikka').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }

                if ($('.yhteydenotto:checked').val() == 'email' && $('.yhteydenotto-tiedot #email').val() == '') {
                    error = true;
                    $('.yhteydenotto-tiedot #email').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('.yhteydenotto-tiedot #email').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }
            }
        } else if (asiakkuus == 'yritysasiakas') {
            
                if ($('#aihe').val() == '') {
                    error = true;
                    $('#aihe').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('#aihe').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }
							
                if ($('#ytunnus').val() == '') {
                    error = true;
                    $('#ytunnus').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('#ytunnus').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }
              
                if ($('#viesti').val() == '') {
                    error = true;
                    $('#viesti').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('#viesti').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }
				
				
                if ($('#yritys').val() == '') {
                    error = true;
                    $('#yritys').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('#yritys').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }
				
                if ($('#yh').val() == '') {
                    error = true;
                    $('#yh').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('#yh').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }
				
                if ($('#ypuhelin').val() == '') {
                    error = true;
                    $('#ypuhelin').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('#ypuhelin').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }
				
                if (!validateEmail($('#email').val())) {//$('#email').val() == '') {
                    error = true;
                    $('#email').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('#email').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }
				
                if ($('#katuosoite').val() == '') {
                    error = true;
                    $('#katuosoite').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('#katuosoite').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }
				
                if ($('#postinumero').val() == '') {
                    error = true;
                    $('#postinumero').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('#postinumero').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }
				
                if ($('#postitoimipaikka').val() == '') {
                    error = true;
                    $('#postitoimipaikka').css({ backgroundColor:"#e99", border:"1px solid #f00" });
                } else {
                    $('#postitoimipaikka').css({ backgroundColor:"#ecf4f9", border:"1px solid #42789a" });
                }

            
        }

        if (error == true) {
            $('.errortext').html('<p>Tarkista vielä punaisella merkityt pakolliset kentät.</p>');
            return false;
        } else {
            return true;
        }
    });

    hideall();
    isAsiaChecked();
});
