document.write('<div style="z-index: 3000;" style="hidden" class="jqmWindow jqmID1" id="dialog">&nbsp;</div>');
document.write('<div style="hidden" id="script">&nbsp;</div>');

var tourName;
var moreTour = false;

window.onload = function() {
    
    $("#dialog").jqm();
	$("#event_all").checked = false;
	$("#checkView").checked = false;
        
        $('#gyomronevezesek').click(function () {
		$("#dialog").html($("#gyomronevezesekDiv").html());
		$("#dialog").jqmShow();

		return false;
	});
        $('#eselyesek').click(function () {
		$("#dialog").html($("#eselyesekDIV").html());
		$("#dialog").jqmShow();

		return false;
	});

	$('a.lightbox').lightBox();
	$("#event_all").click(function() {
		if (this.checked == false) {
			$("#races .event").each( function() {
				this.checked = false;
			});
		}
		if (this.checked == true) {
			$("#races .event").each( function() {
				this.checked = true;
			});
		}

	});
	start();
	$("#registrationMore").click(function() {
		var False = 0;
		var check = 0;
		$("#races .event").each( function(i) {
			check ++
			if (this.checked == false) {
				False ++
			}
			if (this.checked == true) {
				if ($(this).attr("name") == "event_esztergom") {
					tourName = "Esztergom"
				}
				if ($(this).attr("name") == "event_tihany") {
					tourName = "Tihany"
				}
				if ($(this).attr("name") == "event_gyomro") {
					tourName = "Gyömrő"
				}
				if ($(this).attr("name") == "event_budapest") {
					tourName = "Budapest"
				}
			}

			if ($('#event_tihany').attr('checked')) {
				$('#distancediv_tihany').show();
				$('#replyDistanceTihanyPar').show();
				$('#distanceSelector').show();
			} else {
				$('#distancediv_tihany').hide();
				$('#replyDistanceTihanyPar').hide();
				$('#distanceSelector').hide();
			}
			/*if ($('#event_budapest').attr('checked')) {
				$('#distancediv_budapest').show();
				$('#replyDistanceBudapestPar').show();
			} else {
				$('#distancediv_budapest').hide();
				$('#replyDistanceBudapestPar').hide();
			}*/

		});
		True = check - False;

		if (False == check) {
			alert('Legalább egy versenyt meg kell jelölnöd!')
		}
		if (True == 1) {
			hidden();
			registrationStep1();
		}
		if (True > 1 ) {
			hidden();
			registrationStep1();
			moreTour = true;
		}
		registrationStep2();
	});
	$('.ajaxForm').submit(function () {
		var target = this.target == false ? "dialog" : this.target;
		ajaxPostUpdate (this.id, this.action + (this.action.indexOf("?") == -1 ? "?" : "&") + "ajax=true", target, true);


		return false;
	});

	$('.ajaxLink').click(function () {
		var target = this.rel == false ? "dialog" : this.rel;
		ajaxGetUpdate (target, this.href + (this.href.indexOf("?") == -1 ? "?" : "&") + "ajax=true", true);

		return false;
	});
	$('form').submit(function(){
		$('input[type=submit]', this).attr('disabled', 'disabled');
	});
	$(".exp").each(function() {
		$(".exp-head", this).click(function(){
			$(this).next().slideToggle("fast");
			this.blur(); // most browsers
			this.hideFocus = false; // ie
			this.style.outline = null; // mozilla
		});
	});
	$(".exp-head").each(function() {
		$(this).next().slideToggle("fast");

		$(this).mouseover(function() {
			$("h2",this).css({
				"cursor":"pointer",
				"color":"#ff0000"
			});
		});
		$(this).mouseout(function() {
			$("h2",this).css({
				"cursor":"normal",
				"color":"#003399"
			});
		});
	});
	$("#startForm").find('input[type|="submit"]').show();
	$("#startForm").submit(function() {
		$("#startForm").find('input[type|="submit"]').css("visibility", "hidden");
            
		var myAjax = $.ajax({
			type: "GET",
			url: "/banking/ajax/start?txid=" + this.title,
			error: function (msg) {
				if(msg.statusText == "TXEXISTS") {
					$('.main').html('<h1>Elévült link</h1><br /><p>A link már elévült. amennyiben új tranzakciót szeretne indítani, kérjük regisztráljon újból.</p>');
				} else {
					$('#debugInput').val(msg.responseText);
					$('#debugForm').submit();
				}
			},
			success: function(msg){
				$("#startForm").unbind("submit");
				$("#startForm").submit();
			}
		});

		return false;
	});
	$(".reload").click(function() {
		document.location.href = "/banking/eredmeny.html";

		return false;
	});
	$("#registrationMore3").click(function() {
		checkAge();
		var agentfill=0
		var agentinput=0
		var agentOK = false
		$("#agent input").each(function() {
			agentinput ++
			if (this.value.length) {
				agentfill++
			}
		});

		$("#agentOK").each( function() {
			if (this.checked == true ) {
				agentOK = true
			}
		});

		if ( agentfill < agentinput || agentOK == false ) {
			alert('Minden mező kitöltése kötelező!')
		}else{
			var agentYear = $("#agentbornyear").attr("value").length;
			if (agentYear < 4) {
				alert('Az évszámot nem helyesen adtad meg!');
			}else{
				checkAge2()
			}
		}
	});

	$("#checkView").click(function(){
		if (this.checked == true ) {
			$("#checkDiv").slideDown("fast");
		}
		if (this.checked == false ) {
			$("#checkDiv").slideUp("fast");
		}
	});
}
function hidden() {
	$(".hidden").css("display", "none");
}
function start() {
	hidden();
	$("#require").css("display", "none");
	$("#races").css("display", "block");
	$("#registrationMore2").css("display", "block");
}
function back() {
	hidden();
	$("#tour").css("display", "block");
	$("#registrationMore2").css("display", "block");
	if ( Agent == true){
		$("#agent").css("display", "block");
		$("#registrationMore2").css("display", "none");

	}
}
function back2() {
	hidden();
	$("#declarationMore").css("display", "block");
	$("#decNext").css("display", "block");
}
var Agent = false;
function registrationStep1() {
	$("#tour").css("display", "block");

	if ( Agent == true){
		$("#agent").css("display", "block");
		$("#registrationMore2").css("display", "none");

	}
}
function registrationStep2() {

	$("#registrationMore2").click(function() {
		var fill=0
		var input=0
		$("#datas input").each(function() {
			input ++
			if (this.value.length) {
				fill++
			}
		});
		if ( fill < input) {
			alert('Minden mező kitöltése kötelező!')
		}else{
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			var address = $('#email').val();
			if(reg.test(address) == false) {
				alert('Az e-mail cím formátuma nem megfelelő');
			} else {
				var Year = $("#bornyear").attr("value").length;
				if (Year < 4) {
					alert('Az évszámot nem helyesen adtad meg!');
				}else{
					checkAge()
				}
			}
		}
	});
}
function registrationStep3() {
	hidden();
	if (moreTour == false) {
		$("#declarationOne").css("display", "block");
		$("#advert1").checked = true;
		$("#advert2").checked = false;
	}else{
		$("#declarationMore").css("display", "block");
		$("#advert1").checked = false;
		$("#advert2").checked = true;
	}
	$("#decNext").css("display", "block");
	$(".tourname").html( tourName );
}
var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode;
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}

	function containsElement(arr, ele) {
		var found = false, index = 0;
		while(!found && index < arr.length)
			if(arr[index] == ele)
				found = true;
			else
				index++;
		return found;
	}

	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input)index = i;
			else i++;
		return index;
	}
	return true;
}
function checkAge()
{
	/* the minumum age you want to allow in */
	var min_age = 18;

	/* change "age_form" to whatever your form has for a name="..." */
	var year = parseInt(document.forms["age_form"]["bornyear"].value);
	var month = parseInt(document.forms["age_form"]["bornmonth"].value) - 1;
	var day = parseInt(document.forms["age_form"]["bornday"].value);

	var theirDate = new Date((year + min_age), month, day);
	var today = new Date;

	if ( (today.getTime() - theirDate.getTime()) < 0) {
		//alert("Túl fiatal vagy!");
		$("#agent").css("display", "block");
		$("#registrationMore2").css("display", "none");

		Agent = true
	}
	else {
		//alert("Elég idős vagy!");
		$("#registrationMore2").css("display", "block");
		registrationStep3();
		Agent = false
	}
}

function checkAge2()
{
	/* the minumum age you want to allow in */
	var min_age = 18;

	/* change "age_form" to whatever your form has for a name="..." */
	var year = parseInt(document.forms["age_form"]["agentbornyear"].value);
	var month = parseInt(document.forms["age_form"]["agentbornmonth"].value) - 1;
	var day = parseInt(document.forms["age_form"]["agentbornday"].value);

	var theirDate = new Date((year + min_age), month, day);
	var today = new Date;

	if ( (today.getTime() - theirDate.getTime()) < 0) {
		//alert("Túl fiatal vagy!");
		alert("A törvényes képviselő nem lehet 18 évnél fiatalabb!")
	}
	else {
		//alert("Elég idős vagy!");
		registrationStep3();

	}
}
function registrationStep4() {
	if ($('#validationOne').attr('checked') || $('#validationMore').attr('checked')) {
		var tihany = false;
		var budapest = false;
		$("#replyEvent").html('');

		hidden();
		$("#alldatas").css("display", "block");
		$("#submitDiv").css("display", "block");
		
		if ($('#event_tihany').attr('checked')) {
			tihany = true;
			$("#replyEvent").html($("#replyEvent").html() + "<br />Tihany");
		}
		if ($('#event_budapest').attr('checked')) {
			budapest = true;
			$("#replyEvent").html($("#replyEvent").html() + "<br />Budapest");
		}

		if (tihany && budapest) {
			$("#replyPrice").html((4000 + bp).toString());
			$('#priceInput').val((4000 + bp).toString());
		} else if (tihany && !budapest) {
			$("#replyPrice").html((4000).toString());
			$('#priceInput').val((4000).toString());
		} else if (!tihany && budapest) {
			var message = "<ul><li><font color='#434343' size='1'  style='font-size: 12px; text-transform: none;' face='Arial, Helvetica, Verdana, Tahoma'>azok számára, akik más 2011. évi Erste Tour eseményre már neveztek 2011. július 10-ig 2.000 Ft, azaz kettőezer forint, 2011. július 11-től 3.000 Ft azaz háromezer forint.</font></li><li><font color='#434343' size='1'  style='font-size: 12px; text-transform: none;' face='Arial, Helvetica, Verdana, Tahoma'>A nevezési díj mértéke csak a budapesti versenyre történő, illetve helyszíni nevezés esetén 4.000 Ft, azaz négyezer forint.</font></li></ul>";
			$("#replyPrice").html(message);
			$('#priceInput').val(message);
		}

		$("#replyName").html($("#lastname").val() + " " + $("#firstname").val());
		$("#replyBorn").html($("#bornyear").val() + "-" + $("#bornmonth").val() + "-" + $("#bornday").val());
		$("#replyBornPlace").html($("#bornplace").val());
		$("#replyEmail").html($("#email").val());
		$("#replyPhone").html($("#phone").val());
		$("#replyMother").html($("#mother").val());
		$("#replyAddress").html($("#postcode").val() + " " + $("#city").val() + ", " + $("#address").val());
		$("#replyShirt").html($("#shirt").val());
		$("#replyTeam").html($("#team").val());
		$("#replyDistanceTihany").html($("#distance_tihany").val());
		//$("#replyDistanceBudapest").html($("#distance_budapest").val());
		if ( Agent == true){
			$("#allagent").css("display", "block");
			$("#replyAgentName").html($("#agentlastname").val() + " " + $("#agentfirstname").val());
			$("#replyAgentMother").html($("#agentmother").val());
			$("#replyAgentAddress").html($("#agentpostcode").val() + " " + $("#agentcity").val() + ", " + $("#agentaddress").val());
			$("#replyAgentBornPlace").html($("#agentbornaddress").val());
			$("#replyAgentBorn").html($("#agentbornyear").val() + "-" + $("#agentbornmonth").val() + "-" + $("#agentbornday").val());
		}
	} else {
		alert('A nyilatkozatot el kell fogadni.');
	}
}

	
