	$(function() {
		
		$("#submit").click(function() {
			validateForm();
		});
		
		var serial = $("#serial"),
			tips = $("#loginText");

		function updateTips(t,v) {
			tips.text(t).effect('highlight',{},1500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				return false;
			} else {
				return true;
			}
		}

		$("#submit").click(function() {
			validateForm();
		});


		$("#serial").keydown(function(e){
			if (e.keyCode == 13) {
				validateForm();
			}
		});

		$(document).ready(function() {
			formState("#serial","d");
			formState("#submit","d");
			formState("#serialText","d");
			updateTips("");
			$('#status').html('<img src="images/ajax-loader.gif" width="200" height="19" alt="Loading..."><br>&nbsp;&nbsp;Checking for an active Session...');
					jQuery.ajax({
					type: "GET",
					url: "support.php?checkSession=true",
					success: function(data){checkSession(data);}
					});
		});
		
		function checkSession(d) {
		var bValid = true;
		if (d == "false") {
			//return form to normal state
			formState("#serial","e");
			formState("#submit","e");
			formState("#serialText","e");
			$('#status').html('');
			updateTips("Please enter serial number to access download area");
		} else {
			updateTips("");
			formState("#serial","d");
			formState("#submit","d");
			formState("#serialText","d");
				updateTips("");
				$('#status').html('<img src="images/ajax-loader.gif" width="200" height="19" alt="Loading..."><br>&nbsp;&nbsp;Restoring Previous Session...');
					// allow the user to see the tip
					//fetch the result
					jQuery.ajax({
							    type: "GET",
						        url: "support.php?login=true",
						        data: {serial: d},
						        success: function(data){rVal(data);}
							    });
						
				}
		}
			
		function formState(o,a) {
			if (a == "d") {
				$(o).fadeTo("slow", 0);
				$(o).attr("disabled", true);
			} else {
				$(o).fadeTo("slow", 1);
				$(o).attr("disabled", false);
			}
		}
		

		
		function rVal(d) {
			if (d == "false") {
				$('#status').html('');
				updateTips("The serial provided is not recognized, please try again");
				formState("#serial","e");
				formState("#submit","e");
				formState("#serialText","e");
				$('#loginForm').effect("shake", { times:2 }, 200);

			} else {
				$('#status').html('<img src="images/ajax-loader.gif" width="200" height="19" alt="Loading..."><br>&nbsp;&nbsp;Loading Download Portal...');
				updateTips("");
				$("#loginForm").slideUp("slow");
				$("#rContainer").html(d);
				$("#rContainer").slideDown("slow");	
				formState("#serial","e");
				formState("#submit","e");
				formState("#serialText","e");
			}
		}
		
		function validateForm() {
			var bValid = true;
					formState("#serial","d");
					formState("#submit","d");
					formState("#serialText","d");
					bValid = bValid && checkLength(serial,"Serial Number",8,20);

					if (bValid) {		
						updateTips("");
						
						$('#status').html('<img src="images/ajax-loader.gif" width="200" height="19" alt="Loading..."><br>&nbsp;&nbsp;Validating Request...');
						// allow the user to see the tip
						//fetch the result
						 jQuery.ajax({
								   type: "GET",
								   url: "support.php?login=true",
								   data: {serial: $("#serial").val()},
								   success: function(data){rVal(data);}
								 });
					} else {
						updateTips("Invalid serial format, please try again!");
						formState("#serial","e");
						formState("#submit","e");
						formState("#serialText","e");
						$('#loginForm').effect("shake", { times:2 }, 200);
					}
		}
	});