﻿//switch passengers
$(document).ready(function(){
	$("#passengers2, #passengers3, #passengers4, #passengers5").click(function () { 
		$("#classvehicle2").attr("disabled", "disabled");
		
		var mers = $("#classvehicle2").attr("checked");
			if(mers) $("#classvehicle1").attr("checked", "checked");
    });
	
	$("#passengers1").click(function () { 
		$("#classvehicle2").attr("disabled", "");
    });
});

//switch location from
$(document).ready(function(){
	$("#from1, #from2, #from3, #from4").click(function () {

			$("#fromlocation")
			.attr("id", "fromlocation_temp")
			.attr("name", "fromlocation_temp")
			.after("<select class=\"inselect\" id=\"fromlocation\" name=\"fromlocation\"></select>");
			
			var flag = document.getElementById("fromlocation_text");
			
			if(!flag){
				var tempdata = $("#fromlocation_temp").val();
				
				$("#fromlocation")
				.after("<input type=\"hidden\" id=\"fromlocation_text\" />");
				$("#fromlocation_text").val(tempdata);
			}
			
			$("#fromlocation_input, #fromlocation_container, #fromlocation_temp").remove();

			var id = $(this).val();																
			var req = new JSHttpRequest();
			
			req.onreadystatechange = function(){
				if (req.readyState == 4) {
					$("#fromlocation")
					.html(req.responseJS.row);
					//.selectbox();
				}
			}
			
			req.caching = true;
			req.open('GET', '/m/u_loadlocations.php', true);
			req.send({ type: id });
    });
	
	$("#from5").click(function(){
		$("#fromlocation")
		.attr("id", "fromlocation_hide")
		.attr("name", "fromlocation_hide")
		.hide();
		
		$("#fromlocation_input, #fromlocation_container").remove();
		
		var tempdata = $("#fromlocation_text").val();
		
		$("#fromlocation_hide")
		.after("<textarea class=\"txtarea\" id=\"fromlocation\" name=\"fromlocation\" rows=\"5\" cols=\"15\"></textarea>");
		
		$("#fromlocation").val(tempdata);
		
		$("#fromlocation_text").remove();
		$("#fromlocation_hide").remove();
	});
	
	$("#from3, #from4, #from5").click(function () {
		$("#numbreisfrom").attr("disabled", "disabled");
	});
	
	$("#from1, #from2").click(function () {
		$("#numbreisfrom").attr("disabled", "");
	});
});

//switch location to
$(document).ready(function(){
	$("#to1, #to2, #to3, #to4").click(function () {
			
			$("#tolocation")
			.attr("id", "tolocation_temp")
			.attr("name", "tolocation_temp")
			.after("<select class=\"inselect\" id=\"tolocation\" name=\"tolocation\"></select>");
			
			var flag = document.getElementById("tolocation_text");
			
			if(!flag){
				var tempdata = $("#tolocation_temp").val();
				
				$("#tolocation")
				.after("<input type=\"hidden\" id=\"tolocation_text\" />");
				$("#tolocation_text").val(tempdata);
			}
			
			$("#tolocation_input, #tolocation_container, #tolocation_temp").remove();
														
			var id = $(this).val();																
			var req = new JSHttpRequest();
			
			req.onreadystatechange = function(){
				if (req.readyState == 4) {
					$("#tolocation")
					.html(req.responseJS.row);
					//.selectbox();
				}
			}
						
			req.caching = true;
			req.open('GET', '/m/u_loadlocations.php', true);
			req.send({ type: id });
    });
	
	$("#to5").click(function() {
		$("#tolocation")
		.attr("id", "tolocation_hide")
		.attr("name", "tolocation_hide")
		.hide();
		
		$("#tolocation_input, #tolocation_container").remove();
		
		var tempdata = $("#tolocation_text").val();
		
		$("#tolocation_hide")
		.after("<textarea class=\"txtarea\" id=\"tolocation\" name=\"tolocation\" rows=\"5\" cols=\"15\"></textarea>");
		
		$("#tolocation").val(tempdata);
		
		$("#tolocation_text").remove();
		$("#tolocation_hide").remove();
	});
	
	$("#to3, #to4, #to5").click(function () {
		$("#numbreisto").attr("disabled", "disabled");
	});
	
	$("#to1, #to2").click(function () {
		$("#numbreisto").attr("disabled", "");
	});
});

//get transfer sum 
$(document).ready(function(){
	$(".transfers #reset").click(function () {
		FormWork.gettransfersum();
		return false;
    });
});

//confirm transfer form
$(document).ready(function(){
	$(".transfers #fsubmit").click(function () {
		FormWork.gettransfersum(1);
		return false;
    });
});

//get excursion sum
$(document).ready(function(){
	$(".excursion #reset").click(function () {
		FormWork.getexcursionsum();
		return false;
    });
});

//confirm excursion form
$(document).ready(function(){
	$(".excursion #fsubmit").click(function () {
		FormWork.getexcursionsum(1);
		return false;
    });
});

//confirm event form
$(document).ready(function(){
	$(".event #fsubmit").click(function () {
		FormWork.confirmeventform();
		return false;
    });
});

//confirm night form
$(document).ready(function(){
	$(".night #fsubmit").click(function () {
		FormWork.confirmnightform();
		return false;
    });
});


var FormWork = new Object({
	getexcursionsum: function(subm) {
		var queryString = $('#webform').formSerialize();
		var rand = Math.floor(Math.random()*(10000-1+1))+1;
		
		$(".fieldsubmit").addClass("sloading");
		
		var req = new JSHttpRequest();
			
		req.onreadystatechange = function(){
			if (req.readyState == 4){
				
				var sum = req.responseJS.sum;
				var mess = req.responseJS.mess;
				
				$(".fieldsubmit").removeClass("sloading");
				
				$(".transfersum")
				.slideUp("normal", function(){
					
					$(".mess").removeClass("sum");
					
					if(sum){
						$(".grd").show();
						$(".mess").text(sum + "€").addClass("sum");
						$(".transfersum").slideDown("normal");
						$("#sum").val(sum);
					} else {
						$(".grd").hide();
						$(".mess").html(mess);
						$(".transfersum").slideDown("normal");
						$("#sum").val("?");
					}
					
					$.scrollTo(".transfersum", "slow");
					
					if(subm) FormWork.confirmexcursionform();
				});
				
			}
		}
					
		req.caching = true;
		req.open('GET', '/m/u_getexcursionsum.php', true);
		req.send({ query: queryString, r: rand });
		
		return true;
	},
	
	confirmexcursionform: function() {
		var queryString = $('#webform').formSerialize();
		var rand = Math.floor(Math.random()*(10000-1+1))+1;
		var container;
		
		$(".fieldsubmit").addClass("sloading");
		
		var req = new JSHttpRequest();
			
		req.onreadystatechange = function(){
			if (req.readyState == 4) {
				if(req.responseJS.row == "correct"){
					document.getElementById("webform").submit();
				} else {
					var field = document.getElementById(req.responseJS.field);
					var parent = $(field).parent();
					
					$(".fieldsubmit").removeClass("sloading");
						
					$("<div>")
					.addClass("inputalert")
					.css("display", "none")
					.appendTo(parent)
					.fadeIn("normal")
					.html(req.responseJS.row);
						
					$(field).addClass("confirm");
						
					$(".inputalert")
					.click(function(){
					   $(this).fadeOut("fast", function(){
							$(this).remove();
						});
					   return false;
					});
					
					$('input')
					.focus(function(){
						$('input').removeClass("confirm");
						
						$(".inputalert").fadeOut("fast", function(){
							$(this).remove();
						});
						return false;
					});
				}
			}
		}
					
		req.caching = true;
		req.open('GET', '/m/u_confirmexcursionform.php', true);
		req.send({ query: queryString, r: rand });
	},
	
	gettransfersum: function(subm) {
		var queryString = $('#webform').formSerialize();
		var rand = Math.floor(Math.random()*(10000-1+1))+1;
		
		$(".fieldsubmit").addClass("sloading");
		
		var req = new JSHttpRequest();
			
		req.onreadystatechange = function(){
			if (req.readyState == 4){
				
				var sum = req.responseJS.sum;
				var mess = req.responseJS.mess;
				
				$(".fieldsubmit").removeClass("sloading");
				
				$(".transfersum")
				.slideUp("normal", function(){
					
					$(".mess").removeClass("sum");
					
					if(sum){
						$(".grd").show();
						$(".mess").text(sum + "€").addClass("sum");
						$(".transfersum").slideDown("normal");
						$("#sum").val(sum);
					} else {
						$(".grd").hide();
						$(".mess").html(mess);
						$(".transfersum").slideDown("normal");
						$("#sum").val("?");
					}
					
					$.scrollTo(".transfersum", "slow");	
					if(subm) FormWork.confirmtransferform();
				});
				
			}
		}
					
		req.caching = true;
		req.open('GET', '/m/u_gettransfersum.php', true);
		req.send({ query: queryString, r: rand });
	},
	
	confirmtransferform: function() {
		var queryString = $('#webform').formSerialize();
		var rand = Math.floor(Math.random()*(10000-1+1))+1;
		var container;
		
		$(".fieldsubmit").addClass("sloading");
		
		var req = new JSHttpRequest();
			
		req.onreadystatechange = function(){
			if (req.readyState == 4) {
				if(req.responseJS.row == "correct"){
					document.getElementById("webform").submit();
				} else {
					var field = document.getElementById(req.responseJS.field);
					var parent = $(field).parent();
					
					$(".fieldsubmit").removeClass("sloading");
					
					/*
					switch (req.responseJS.field){
						case "date":
							container = 0;
						break;
						
						case "name":
						case "phone":
						case "email":
							container = 3;
						break;
							
						default:
							container = 0;
						break;
					}
					*/
					//$(".newwebform").accordion("activate", container);
					$.scrollTo(field, "slow");
						
					$("<div>")
					.addClass("inputalert")
					.css("display", "none")
					.appendTo(parent)
					.fadeIn("normal")
					.html(req.responseJS.row);
						
					$(field).addClass("confirm");
						
					$(".inputalert")
					.click(function(){
					   $(this).fadeOut("fast", function(){
							$(this).remove();
						});
					   return false;
					});
					
					$('input')
					.focus(function(){
						$('input').removeClass("confirm");
						
						$(".inputalert").fadeOut("fast", function(){
							$(this).remove();
						});
						return false;
					});
				}
			}
		}
					
		req.caching = true;
		req.open('GET', '/m/u_confirmtransferform.php', true);
		req.send({ query: queryString, r: rand });
	},
	
	confirmeventform: function() {
		
		var queryString = $('#webform').formSerialize();
		var rand = Math.floor(Math.random()*(10000-1+1))+1;
		var container;
		
		$(".fieldsubmit").addClass("sloading");
		
		var req = new JSHttpRequest();
			
		req.onreadystatechange = function(){
			if (req.readyState == 4) {
				if(req.responseJS.row == "correct"){
					document.getElementById("webform").submit();
				} else {
					var field = document.getElementById(req.responseJS.field);
					var parent = $(field).parent();
					
					$(".fieldsubmit").removeClass("sloading");
						
					$("<div>")
					.addClass("inputalert")
					.css("display", "none")
					.appendTo(parent)
					.fadeIn("normal")
					.html(req.responseJS.row);
						
					$(field).addClass("confirm");
						
					$(".inputalert")
					.click(function(){
					   $(this).fadeOut("fast", function(){
							$(this).remove();
						});
					   return false;
					});
					
					$('input')
					.focus(function(){
						$('input').removeClass("confirm");
						
						$(".inputalert").fadeOut("fast", function(){
							$(this).remove();
						});
						return false;
					});
				}
			}
		}
					
		req.caching = true;
		req.open('GET', '/m/u_confirmeventform.php', true);
		req.send({ query: queryString, r: rand });
	},
	
	confirmnightform: function() {
		var queryString = $('#webform').formSerialize();
		var rand = Math.floor(Math.random()*(10000-1+1))+1;
		var container;
		
		$(".fieldsubmit").addClass("sloading");
		
		var req = new JSHttpRequest();
			
		req.onreadystatechange = function(){
			if (req.readyState == 4) {
				if(req.responseJS.row == "correct"){
					document.getElementById("webform").submit();
				} else {
					var field = document.getElementById(req.responseJS.field);
					var parent = $(field).parent();
					
					$(".fieldsubmit").removeClass("sloading");
						
					$("<div>")
					.addClass("inputalert")
					.css("display", "none")
					.appendTo(parent)
					.fadeIn("normal")
					.html(req.responseJS.row);
						
					$(field).addClass("confirm");
						
					$(".inputalert")
					.click(function(){
					   $(this).fadeOut("fast", function(){
							$(this).remove();
						});
					   return false;
					});
					
					$('input')
					.focus(function(){
						$('input').removeClass("confirm");
						
						$(".inputalert").fadeOut("fast", function(){
							$(this).remove();
						});
						return false;
					});
				}
			}
		}
					
		req.caching = true;
		req.open('GET', '/m/u_confirmnightform.php', true);
		req.send({ query: queryString, r: rand });
	}
});

//form select replacement
/*
$(document).ready(function(){
	if($(".inselect").length > 0){
		$(".inselect").selectbox();
	}
	
	if($("select.time_hours, select.time_minute").length > 0){
		$("select.time_hours, select.time_minute").selectbox({
			inputClass: "time"
		});
	}
});
*/

$(document).ready(function(){
	if($(".header_container .image_container").length > 0){
		$(".header_container .image_container img").load( function(){
			$(".header_container .image_container").cycle({ 
				fx:    'fade', 
				speed:  3000
			});
		});
	}
	
	//run offers
	if($("#offers-1").length > 0){
		$(".service_offers .image_container img").load( function(){
			$(".service_offers .image_container").cycle({
				fx: 'fade',
				speed: 4500
			});
		});
	}
	
	$("div.text_container").ifixpng();
});
