﻿$(document).ready(function() {
	// Show hidden elements
	$("ul#newsscroll").show();
	$("#banner").show();
	$("#sponsorlogos").show();
	$("body").css("visibility", "visible");

	// Navigation submenus
	$("#navlist li").hover(function() {
		$(this).find("ul:first").stop(true, true).delay(110).slideDown(300);
	}, function() {
		$(this).find("ul:first").stop(true, true).delay(110).slideUp(300);
	});


	// Start Ticker
	$("ul#newsscroll").liScroll({travelocity: 0.05});


	// News Accordian
	$("h1.accordian").click(function() {
		$(this).next("div.accordiantext").stop(true, true).animate({ opacity: "toggle", height: "toggle" }, 500);
	});


	// Cycle through banners
	$("#banner").cycle({
		timeout: 4000,
		speed:   600
	});


	// Cycle through sponsor logos
	$("#sponsorlogos").cycle({
		timeout: 4000,
		speed:   600
	});


	// Tooltips
	var mx, my, t;
	
	function updateTTPos(ele) {
	    ele.css('top', my + 10 );
        ele.css('left', mx + 20 );	
	}
	$(".helplink").hover(function(e) {
		updateTTPos($(this).find("p"));
		$(this).find("p").stop(true, true).delay(110).fadeIn(300);
	}, function() {
		$(this).find("p").stop(true, true).delay(110).fadeOut(300);
	});
	$(".helplink").mousemove(function(e) {
		mx = e.pageX;
		my = e.pageY;
		updateTTPos($(this).find("p"));
	});
	
	// Validate exhibitor login
	$("#manuallogin").submit(function() {
		if ($("#exhiblogin").val().length < 1)
		{
			alert ("You must enter your user name before logging in.");
			return false;
		}

		if ($("#exhibpw").val().length < 1)
		{
			alert ("You must enter your password before logging in.");
			return false;
		}

		$.post("exhibitor_login.asp", 
			$("#manuallogin").serialize(),
			function(data) {
				if (data != "0") {
					$("#manuallogin").fadeOut(100);
					$("#exhibitorloggedin").fadeIn(300);
					t = data.split("|");
					$("#exhibName").html(t[0]);
					if (t[1] === "2012") {
						$("#bk").hide();
						$("#em").show();
					} else {
						$("#em").hide();
						$("#bk").show();
					}
				} else {
					alert ("The login details are incorrect, please try again.");
				}
			});
		return false;
	});

	$("#frmmanuallogin").submit(function() {
		if ($("#emlogin").val().length < 1)
		{
			alert ("You must enter your user name before logging in.");
			return false;
		}

		if ($("#empw").val().length < 1)
		{
			alert ("You must enter your password before logging in.");
			return false;
		}

		$.post("exhibitor_login2.asp", 
			$("#frmmanuallogin").serialize(),
			function(data) {
				if (data != "0") {
					window.location = "exhibitor_manual.asp";
				} else {
					alert ("The login details are incorrect, please try again.");
				}
			});
		return false;
	});	

	$("#manuallogout").submit(function() {
		$.post("exhibitor_logout.asp", function() {
			$("#exhibitorloggedin").hide();
			$("#manuallogin").show();
		});
		window.location.href = "index.asp";
		return false;
	});	

	// Populate booking form if currently logged in
	if ($("#exhibid").val() > 0)
	{
		$.post("get_exhibitor.asp", 
		{ id: $("#exhibid").val() },
		function(data)
		{
			if (data == "rnf") { return false; }
			jQuery.map(data.previous_exhibitor_data, function(i, j) 
			{
				$("#" + j).val(i);
			});
		}, 
		"json");
	}
	
	
	// Validate booking form
	$("#booking_form").submit(function() {

		if ($("#trading_name").val().length == 0)
		{
			alert ("You must enter your Trading Name before submitting the form.");
			$("#trading_name").focus();
			return false;
		}

		if ($("#azindex").val().length == 0)
		{
			alert ("You must enter the A-Z index letter you would like to be listed under before submitting the form.");
			$("#azindex").focus();
			return false;
		}
		
		if ($("#c_address").val().length == 0)
		{
			alert ("You must enter your Correspondence Address before submitting the form.");
			$("#c_address").focus();
			return false;
		}
		
		if ($("#c_telephone").val().length == 0)
		{
			alert ("You must enter your Correspondence Telephone before submitting the form.");
			$("#c_telephone").focus();
			return false;
		}
		
		if ($("#c_email").val().length == 0)
		{
			alert ("You must enter your Correspondence Email before submitting the form.");
			$("#c_email").focus();
			return false;
		}

		if ($("#c_email").val().indexOf("@") == -1 || $("#c_email").val().lastIndexOf(".") == -1)
		{
			alert ("The entered email address does not appear to be valid. Please correct it before submitting the form.")
			$("#c_email").focus();
			return false;
		}
		
		if (!$("input[name='same_space']:checked").val())
		{
			alert ("You must indicate whether you wish to keep the same space before submitting the form.");
			$("#same_space_yes").focus();
			return false;
		}
		
		if (!$("input[name='stand_type']:checked").val())
		{
			alert ("You must choose the Stand Type you would like before submitting the form.");
			$("#stand_type_space").focus();
			return false;
		}

		if ($("#min_area").val().length == 0)
		{
			alert ("You must choose the Minimum Area you would like before submitting the form.");
			$("#min_area").focus();
			return false;
		}
		
		if (isNaN($("#min_area").val()))
		{
			alert ("The Minimum Area must contain numbers only.")
			$("#min_area").focus();
			return false;
		}
		
		if ($("#max_area").val().length > 0 && isNaN($("#max_area").val()))
		{
			alert ("The Maximum Area area must contain numbers only.")
			$("#max_area").focus();
			return false;
		}
		
		if (!$("#digital_signature:checked").val())
		{
			alert ("You must sign the form before submitting it.");
			$("#digital_signature").focus();
			return false;
		}
	});

});

function popup(url) {
	mywin = window.open(url,
	'windowname2', 
	'width=630, \
	height=600, \
	directories=no, \
	location=no, \
	menubar=no, \
	resizable=no, \
	scrollbars=1, \
	status=no, \
	toolbar=no');
}
