$(document).ready(function(){

	// KIELIVALINTANAPIT
	
	$("#" + kieli + "_hover").css({ 'display': 'none' });	
	
	// Yhteydenoton lähetys

	$("#yhteydenottopyynto").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 rakentaminen = "";
				
				$('input:checkbox[name="Rakentaminen"]:checked').each(function() { 
					
					
					if($(this).attr("value") == "jokin_muu") {
					
						rakentaminen = rakentaminen + $("#RakentaminenOther").attr("value")  + ", ";
						
					} else {
						
						rakentaminen = rakentaminen + $(this).attr("value") + ", ";
					}

				});
				
				var teollisuus = "";
				
				$('input:checkbox[name="Teollisuus"]:checked').each(function() { 
					
					
				if($(this).attr("value") == "jokin_muu") {
					
						teollisuus = teollisuus + $("#TeollisuusOther").attr("value")  + ", ";
						
					} else {
					
						teollisuus = teollisuus + $(this).attr("value") + ", ";
						
					}

				});		

				var laivapalvelut = "";
				
				$('input:checkbox[name="Laivapalvelut"]:checked').each(function() { 
					
					
					if($(this).attr("value") == "jokin_muu") {
					
						laivapalvelut = laivapalvelut + $("#LaivapalvelutOther").attr("value")  + ", ";
						
					} else {
					
						laivapalvelut = laivapalvelut + $(this).attr("value") + ", ";
						
					}

				});
				
				rakentaminen = rakentaminen.substring(0, rakentaminen.length-2);	
				teollisuus = teollisuus.substring(0, teollisuus.length-2);	
				laivapalvelut = laivapalvelut.substring(0, laivapalvelut.length-2);						

				var palaute = 'lomake=yhteyspyynto' + '&rakentaminen='+ $.URLEncode(rakentaminen) + '&teollisuus=' + $.URLEncode(teollisuus) + '&laivapalvelut=' + $.URLEncode(laivapalvelut) + '&nimi=' + $.URLEncode($("#nimi").val())  
				+ '&yritys=' + $.URLEncode($("#yritys").val()) + '&osoite=' + $.URLEncode($("#osoite").val())  + '&postinro=' + $.URLEncode($("#postinro").val()) + '&postitoimipaikka=' + $.URLEncode($("#postitoimipaikka").val()) 
				+ '&puhnum=' + $.URLEncode($("#puhnum").val()) + '&sposti=' + $.URLEncode($("#sposti").val()) + '&jasentunnus=' + $("#jasentunnus").val();
	
				$.ajax({  
					  type: "GET",  
					  url: osoite + "index.php",  
					  data: palaute,  
					  success: function() {			
				
							$("#yhteydenottopyynto").animate({
								
								opacity: 0
								
							}, 500, function () {

									if(kieli == "en") {
										$("#yhteydenottopyynto").replaceWith("<h2 style='text-align: center; margin: 25px;'> Thank you for your interest! </h2>");
									} else if(kieli == "se") {
										$("#yhteydenottopyynto").replaceWith("<h2 style='text-align: center; margin: 25px;'> Tack för ditt intresse! </h2>");
									} else {
										$("#yhteydenottopyynto").replaceWith("<h2 style='text-align: center; margin: 25px;'> Kiitos yhteydenotosta! </h2>");
									}
	
							});
							
					  }
				
				});

			}
	 
	});	
	
	// 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($("#ajokorttiluokka").val() == "") {
				
					$("#ajokorttiluokka").css({ background: vari });
					virhe++;
					
				} else {
					
					$("#ajokorttiluokka").css({ background: "#ffffff" });
					
				}
				
				$("#ajokorttiluokka").focus(function() { $(this).css({ background: "#ffffff" }); });
				
			// ---------------------------------------------------------------			
			
			if(virhe == 0) {
	
				var toimi = "";
				
				$('input:checkbox[name="toimi"]:checked').each(function() { 
	
					toimi = toimi + $(this).attr("value") + ", ";

				});
				
				toimi = toimi.substring(0, toimi.length-2);						

				var rekry = 'lomake=rekrytointi' + '&toimi='+ $.URLEncode(toimi) + '&ajokorttiluokka=' + $.URLEncode($("#ajokorttiluokka").val()) + '&nimi=' + $.URLEncode($("#nimi").val())  
				+ '&osoite=' + $.URLEncode($("#osoite").val())  + '&postinro=' + $.URLEncode($("#postinro").val()) + '&postitoimipaikka=' + $.URLEncode($("#postitoimipaikka").val()) + '&puhnum=' + $.URLEncode($("#puhnum").val()) 
				+ '&syntymaaika=' + $.URLEncode($("#syntymaaika").val())  + '&tyokokemus=' + $.URLEncode($("#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");
		
	});
	
	
	
	// ARVONTALOMAKE
	
	$("input:radio").click(function() {
	
		if($(this).val() == "kylla" && $(this).attr("name") == "kysymys_kaksi") {
	
			oletko_kayttanyt = "kylla";
	
		} else if ($(this).val() == "ei" && $(this).attr("name") == "kysymys_kaksi") {
	
			oletko_kayttanyt = "ei";	
	
		}
	
	});	
	
	
	$("#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(virhe == 0) {
			
				var tyoala = "";
				
				$('input:checkbox[name="tyoala"]:checked').each(function() { 
	
					if($(this).attr("value") == "jokin_muu") {
					
						tyoala = tyoala + $("#kisaOther").attr("value") + ", ";
						
					} else {
					
						tyoala = tyoala + $(this).attr("value") + ", ";
						
					}					
	
				});	
				
				tyoala = tyoala.substring(0, tyoala.length-2);					
				
				
				
				if(oletko_kayttanyt == "kylla") {
				
					var vastaus_kaksi = "Kyllä";
					
				} else {
				
					var vastaus_kaksi = "Ei";
					
				}
				
				
				
				if($('input:checkbox[name=lisaatietoa]:checked').val() == "kylla") {
					
					lisaatietoa = "Kyllä";
					
				} else {
				
					lisaatietoa = "Ei";
					
				}											
	
				arvontavastaus = {
				
					lomake: "arvonta",
					tyoala: tyoala,
					vastaus_kaksi: vastaus_kaksi,
					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>");
							}	
					
					
					});
					
				}
				
				});
			
			}
			
		
	});	        	
	
});
