$(document).ready(function(){
	
	// ETUSIVUN KUVAKARUSEELI
	
	slideSwitch();
	
	// KIELIVALINTANAPIT
	
	$("#" + kieli + "_hover").css({ 'display': 'none' });

	// PALAUTELOMAKE
	
	$("#palaute").submit(function(event) {
			
			event.preventDefault();
			
			virhe = 0;
			
			vari = "#fce1e0";
			
			if($("#viesti").val() == "") {
			
				$("#viesti").css({ background: vari });
				virhe++;
				
			} else {
				
				$("#viesti").css({ background: "#ffffff" });
				
			}
			
			$("#viesti").focus(function() { $(this).css({ background: "#ffffff" }); });
			
			if(virhe == 0) {
				
				var palaute = {
					
					lomake: "palaute",
					viesti: $("#viesti").val(),
					nimi: $("#nimi").val(),
					osoite: $("#osoite").val(),
					postinro: $("#postinro").val(),
					postitoimipaikka: $("#postitoimipaikka").val(),
					puhnum: $("#puhnum").val(),
					sposti: $("#sposti").val(),
					jasentunnus: $("#jasentunnus").val()
					
				};
		
					$.ajax({  
						  type: "POST",  
						  url: osoite + "index.php",  
						  data: palaute,  
						  success: function() {			
					
								$("#palaute").animate({
									
									opacity: 0
									
								}, 500, function () {
										
									if(kieli == "en") {
										$("#palaute").replaceWith("<h2 style='text-align: center; margin: 25px;'> Thank you for you feedback! </h2>");
									} else if(kieli == "se") {
										$("#palaute").replaceWith("<h2 style='text-align: center; margin: 25px;'> Tack för din feedback! </h2>");
									} else {
										$("#palaute").replaceWith("<h2 style='text-align: center; margin: 25px;'> Kiitos palautteesta! </h2>");
									}
										
								
								});
								
						  }
					
					});				
	
			}
			
	});
	
	$("#rekrytointilomake").submit(function(event) {
			
			event.preventDefault();
			
			virhe = 0;
			
			vari = "#fce1e0";
			
			
			
			if($("#nimi").val() == "") {
			
				$("#nimi").css({ background: vari });
				virhe++;
				
			} else {
				
				$("#nimi").css({ background: "#ffffff" });
				
			}
			
			$("#nimi").focus(function() { $(this).css({ background: "#ffffff" }); });
			
			// ---------------------------------------------------------------
			 
			if($("#osoite").val() == "") {
				
					$("#osoite").css({ background: vari });
					virhe++;
					
				} else {
					
					$("#osoite").css({ background: "#ffffff" });
					
				}
				
				$("#osoite").focus(function() { $(this).css({ background: "#ffffff" }); });
				
			// ---------------------------------------------------------------
				
			if($("#postinro").val() == "") {
				
					$("#postinro").css({ background: vari });
					virhe++;
					
				} else {
					
					$("#postinro").css({ background: "#ffffff" });
					
				}
				
				$("#postinro").focus(function() { $(this).css({ background: "#ffffff" }); });
				
			// ---------------------------------------------------------------
			
			if($("#postitoimipaikka").val() == "") {
				
					$("#postitoimipaikka").css({ background: vari });
					virhe++;
					
				} else {
					
					$("#postitoimipaikka").css({ background: "#ffffff" });
					
				}
				
				$("#postitoimipaikka").focus(function() { $(this).css({ background: "#ffffff" }); });
				
			// ---------------------------------------------------------------		
	
			if($("#puhnum").val() == "") {
				
					$("#puhnum").css({ background: vari });
					virhe++;
					
				} else {
					
					$("#puhnum").css({ background: "#ffffff" });
					
				}
				
				$("#puhnum").focus(function() { $(this).css({ background: "#ffffff" }); });
				
			// ---------------------------------------------------------------
			
			if(virhe == 0) {				

				var rekry = {
					
					lomake: "rekrytointi",
					toimi: $("#toimi").val(),
					patevyys: $("#patevyys").val(),
					nimi: $("#nimi").val(),
					osoite: $("#osoite").val(),
					postinro: $("#postinro").val(),
					postitoimipaikka: $("#postitoimipaikka").val(),
					sposti: $("#sahkoposti").val(),
					puhnum: $("#puhnum").val(),
					syntymaaika: $("#syntymaaika").val(),
					tyokokemus: $("#tyokokemus").val(),
					jasentunnus: $("#jasentunnus").val()
					
				};
	
				$.ajax({  
					  type: "POST",  
					  url: osoite + "index.php",  
					  data: rekry,  
					  success: function() {			
				
							$("#rekrytointilomake").animate({
								
								opacity: 0
								
							}, 500, function () {
									
								if(kieli == "en") {
									$("#rekrytointilomake").replaceWith("<h2 style='text-align: center; margin: 25px;'> Thank you for your job application!</h2>");
								} else if(kieli == "se") {
									$("#rekrytointilomake").replaceWith("<h2 style='text-align: center; margin: 25px;'> Tack för din arbetsansökan! </h2>");
								} else {
									$("#rekrytointilomake").replaceWith("<h2 style='text-align: center; margin: 25px;'> Kiitos työhakemuksestasi </h2>");
								}

							
							});
							
					  }
				
				});

			}
	 
	});	
	
	// Kielilinkit

	$("#fi_hover").hover(function() {
								
		$(this).attr("src",osoite + "kuvat/fi_hover.png");
		
	}, function() {
				
		$(this).attr("src",osoite + "kuvat/fi.png");
		
	});
	
	$("#se_hover").hover(function() {
								
		$(this).attr("src",osoite + "kuvat/se_hover.png");
		
	}, function() {
				
		$(this).attr("src",osoite + "kuvat/se.png");
		
	});
	
	$("#en_hover").hover(function() {
								
		$(this).attr("src",osoite + "kuvat/en_hover.png");
		
	}, function() {
				
		$(this).attr("src",osoite + "kuvat/en.png");
		
	});
	
	
	// ARVONTA
	
	$("#arvontalomake").live('submit', function(event) {
		
		event.preventDefault();

		virhe = 0;
			
			vari = "#fce1e0";
			
			if($("#nimi").val() == "") {
			
				$("#nimi").css({ background: vari });
				virhe++;
				
			} else {
				
				$("#nimi").css({ background: "#ffffff" });
				
			}
			
			$("#nimi").focus(function() { $(this).css({ background: "#ffffff" }); });
			
			// ---------------------------------------------------------------
	

			if($("#osoite").val() == "") {
				
					$("#osoite").css({ background: vari });
					virhe++;
					
				} else {
					
					$("#osoite").css({ background: "#ffffff" });
					
				}
				
				$("#osoite").focus(function() { $(this).css({ background: "#ffffff" }); });
				
			// ---------------------------------------------------------------
			
			if($("#postinro").val() == "") {
				
					$("#postinro").css({ background: vari });
					virhe++;
					
				} else {
					
					$("#postinro").css({ background: "#ffffff" });
					
				}
				
				$("#postinro").focus(function() { $(this).css({ background: "#ffffff" }); });
				
			// ---------------------------------------------------------------
			
			if($("#postitoimipaikka").val() == "") {
				
					$("#postitoimipaikka").css({ background: vari });
					virhe++;
					
				} else {
					
					$("#postitoimipaikka").css({ background: "#ffffff" });
					
				}
				
				$("#postitoimipaikka").focus(function() { $(this).css({ background: "#ffffff" }); });
				
			// ---------------------------------------------------------------	
			
			if($("#puhnum").val() == "") {
				
					$("#puhnum").css({ background: vari });
					virhe++;
					
				} else {
					
					$("#puhnum").css({ background: "#ffffff" });
					
				}
				
				$("#puhnum").focus(function() { $(this).css({ background: "#ffffff" }); });
				
			// ---------------------------------------------------------------							

			if($("#maa").val() == "") {
				
					$("#maa").css({ background: vari });
					virhe++;
					
				} else {
					
					$("#maa").css({ background: "#ffffff" });
					
				}
				
				$("#maa").focus(function() { $(this).css({ background: "#ffffff" }); });
				
			// ---------------------------------------------------------------	

			if($("#tyotehtava").val() == "") {
				
					$("#tyotehtava").css({ background: vari });
					virhe++;
					
				} else {
					
					$("#tyotehtava").css({ background: "#ffffff" });
					
				}
				
				$("#tyotehtava").focus(function() { $(this).css({ background: "#ffffff" }); });
				
			// ---------------------------------------------------------------				
			
			if(virhe == 0) {
			
				var tyoala = "";
				
				$('input:checkbox[name="tyoala"]:checked').each(function() { 
					
					tyoala = tyoala + $(this).attr("value") + ", ";
	
				});		
				
				tyoala = tyoala.substring(0, tyoala.length-2);	
				
				
				if($('input:checkbox[name=lisaatietoa]:checked').val() == "kylla") {
					
					lisaatietoa = "Kyllä";
					
				} else {
				
					lisaatietoa = "Ei";
					
				}						
	
				arvontavastaus = {
				
					lomake: "arvonta",
					tyoala: tyoala,
					tyotehtava: $("#tyotehtava").val(),
					nimi: $("#nimi").val(),
					tehtavanimike: $("#tehtavanimike").val(),
					yritys: $("#yritys").val(),
					sposti: $("#sposti").val(),
					puhnum: $("#puhnum").val(),
					osoite: $("#osoite").val(),
					postinro: $("#postinro").val(),
					postitoimipaikka: $("#postitoimipaikka").val(),
					maa: $("#maa").val(),
					lisatieto: lisaatietoa,					
					jasentunnus: $("#jasentunnus").val()
	
				}
				
				$.ajax({  
				type: "POST",  
				url: osoite + "index.php",  
				data: arvontavastaus,  
				success: function() {			
				
					$("#arvontalomake").animate({
						
						opacity: 0
						
					}, 500, function () {
							
							if(kieli == "en") {
								$("#arvontalomake").replaceWith("<h2 style='text-align: center; margin: 25px;'> Thank you and good luck! </h2>");
							} else if(kieli == "se") {
								$("#arvontalomake").replaceWith("<h2 style='text-align: center; margin: 25px;'> Tack och lycka till! </h2>");
							} else {
								$("#arvontalomake").replaceWith("<h2 style='text-align: center; margin: 25px;'> Kiitos osallistumisestasi ja onnea arvontaan! </h2>");
							}	
					
					
					});
					
				}
				
				});
			
			}
			
		
	});	        
        	
	
	$("#tulostin").live('click', function() {
		
		var x = 0;
		
		$("#tekstialue h2").each(function(index, element) {
            
			if(x == 2) {
			
				$(this).css({ 'page-break-before': 'always' });
				
			}
			
			x++;
			
        });
		
		window.print();
		
		
	});
	
});
