// Preload Images
img1 = new Image(16, 16);  
img1.src="images/spinner.gif";

img2 = new Image(220, 19);  
img2.src="images/ajax-loader.gif";

// When DOM is ready
$(document).ready(function() {

	// Launch MODAL BOX if the Login Link is clicked
	$("#login_link").click(function() {
	
		$('#login_form').modal({
			onShow: function (dialog) {
				$("#tmp-img").html($("#b2b-img").html());
				$("#gal-img").html($("#b2b-img").html());
				$("h1.msg-title").html(titles.b2b.t1);
				$("h2.msg-title2").html(titles.b2b.t2);
			},
			onClose: function (dialog) {
				$("#tmp-img").html($("#no-vis").html());
				$("#gal-img").html($("#tmp-img").html());
				
				$("h1.msg-title").html(titles.def.t1);
				$("h2.msg-title2").html(titles.def.t2);
				
				dialog.container.slideUp('normal', function () {
					dialog.overlay.fadeOut('normal', function () {
					$.modal.close(); // must call this!
					});
				});
			}
		});
	});

	// When the form is submitted
	$("#status > form").submit(function() {

		// Hide 'Submit' Button
		$('#submit').hide();

		// Show Gif Spinning Rotator
		$('#ajax_loading').show();

		// 'this' refers to the current submitted form  
		var str = $(this).serialize();

		// -- Start AJAX Call --

		$.ajax({
			type: "POST",
			url: "Mediator/login_ajax.asp",
			// Send the login info to this page
			data: str,
			success: function(msg) {

				$("#status").ajaxComplete(function(event, request, settings) {

					// Show 'Submit' Button
					$('#submit').show();

					// Hide Gif Spinning Rotator
					$('#ajax_loading').hide();

					if (msg == 'OK') // LOGIN OK?
					{
						var login_response = '<div id="logged_in">' 
											+ '<div style="width: 350px; float: left; margin-left: 70px;">' 
											+ '<div style="width: 40px; float: left;">' 
											+ '<img style="margin: 10px 0px 10px 0px;" align="absmiddle" src="images/ajax-loader.gif">' 
											+ '</div>' + '<div style="margin: 10px 0px 0px 10px; float: right; width: 300px;">' 
											+ "You are successfully logged in! <br /> Please wait while you're redirected...</div></div>";

						$('a.modalCloseImg').hide();

						$('#simplemodal-container').css("width", "500px");
						$('#simplemodal-container').css("height", "120px");

						//$(this).html(login_response); // Refers to 'status'
						$(this).html($("#logged_in").html()); 
						// After 3 seconds redirect the 
						setTimeout('go_to_private_page()', 3000);
					} else if(msg == 'OKa'){
						var login_response = '<div id="logged_in">' 
											+ '<div style="width: 350px; float: left; margin-left: 70px;">' 
											+ '<div style="width: 40px; float: left;">' 
											+ '<img style="margin: 10px 0px 10px 0px;" align="absmiddle" src="images/ajax-loader.gif">' 
											+ '</div>' + '<div style="margin: 10px 0px 0px 10px; float: right; width: 300px;">' 
											+ "You are successfully logged in! <br /> Please wait while you're redirected...</div></div>";

						$('a.modalCloseImg').hide();

						$('#simplemodal-container').css("width", "500px");
						$('#simplemodal-container').css("height", "120px");

						//$(this).html(login_response); // Refers to 'status'
						$(this).html($("#logged_in").html()); 
						// After 3 seconds redirect the 
						setTimeout('go_to_private_page2()', 3000);
					}else // ERROR?
					{
						var login_response = msg;
						$('#login_response').html(login_response);
					}

				});

			}

		});

		// -- End AJAX Call --

		return false;

	}); // end submit event

});

function go_to_private_page()
{
window.location = 'lease_calc_b2b.asp?pid=24'; // Members Area
}
function go_to_private_page2()
{
window.location = 'Mediator/search_offers.asp'; // Members Area
}
