$(function() {		
		function updateTips(t) {
			tips.text(t).effect("highlight",{},2500);
		}

		function checkLength(o,n,min,max) {			
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips(n + " must be between "+min+" and "+max+" characters.");
				return false;
			} else {
				return true;
			}

		}

		function checkLength1(o,n,min,max) {			
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Please enter your registered email address.");
				return false;
			} else {
				return true;
			}
		}
		

		function checkRegexp(o,regexp,n) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips("Invaid input : "+n);
				return false;
			} else {
				return true;
			}
		}

	////////////////////////////////////// Login Box /////////////////////////////////////////	
		var username = $("#username"),
			password = $("#password"),
			allFields = $([]).add(username).add(password),
			tips = $("#validateTips");


		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 'auto',
			modal: true,
			buttons: {
				'Login': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					/*bValid = bValid && checkLength(name,"username",3,16);*/
					bValid = bValid && checkLength(username,"username",4,24);
					bValid = bValid && checkLength(password,"password",6,24);

					//bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Username may consist of a-z, 0-9, underscores, begin with a letter.");
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/username_address_validation/

					/*bValid = bValid && checkRegexp(username,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. test@urecruitme.co.uk");*/

					//bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Password field only allow : a-z 0-9");
					


					if (bValid) {
						
							/*$('#users tbody').append('<tr>' +
							'<td>' + name.val() + '</td>' + 
							'<td>' + username.val() + '</td>' + 
							'<td>' + password.val() + '</td>' +
							'</tr>'); */
						//	alert(username.val());
						//	alert(password.val());

						var url;
						url = "check_login.php?username="+username.val()+"&password="+password.val()
						window.location.href=url;	
						
						$(this).dialog('close');  // To close the window
						
					}
				}/*,
				Cancel: function() {
					$(this).dialog('close');
				}*/
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		
		$('#create-user').click(function() {
			$('#dialog').dialog('open');
		})	
		////////////////////////////////////// End of  Login Box /////////////////////////////////////////				


		////////////////////////////////////// Forgot Password///////////////////////////////////////////////				
		var ForgotEmail = $("#ForgotEmail"),
			allFields = $([]).add(ForgotEmail),
			tips = $("#validateForgot");

			function Send_Ajax_data(o,data)
			{			
				// Send the ajax request.
				 $.ajax({
				   type: "POST",
				   url: "check_email.php?ForgotEmail="+data,
				   data: ({dataXml : data}),
				   dataType: "html",
				   success: function(msg){					
					if(msg=='false')	{
						$("#validateForgot").html("");
						$("#validateForgot").html("<span class='ui-error'>We don't have this email address. Please try again.</span>");					 
						 return false;
					}else{
						$("#validateForgot").html("");
						$("#validateForgot").html("<span class='block'>An email has been sent to "+ForgotEmail.val()+ ". \nYou should receive it shortly.\n If you don't see it please check your spam folder.\n It might have accidentally been flagged as spam.</span>");
						$(".block").animate( { backgroundColor: 'pink' }, 1000)
					     .animate( { backgroundColor: '#3366FF' }, 2000);	
						
						setTimeout("$('#forgot-passwd-dialog').dialog('close');",10000)
						return true;
					 }
				   }
				 });
			}

			$("#forgot-passwd-dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 'auto',
			modal: true,
			buttons: {
				'Send': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');
					
					bValid = bValid && checkLength1(ForgotEmail,"Email address",4,80);
					bValid = bValid && checkRegexp(ForgotEmail,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. test@example.com");
					
					if (bValid) {						
						Send_Ajax_data(ForgotEmail,ForgotEmail.val());							
						return false;
					}					
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		$('#forgot-password').click(function() {
			$('#dialog').dialog('close');
			$('#forgot-passwd-dialog').dialog('open');
		})	
		//////////////////////////////////End of Forgot Password///////////////////////////////////////////	

	});



/// This is for text box click on all pages
$(function() {
		$('input[type=text]').each(function() {
			var defaultValue = $(this).val();
			$(this).focus(function() {
				if($(this).val() == defaultValue) {
					$(this).val('');
				}
			});
			$(this).blur(function() {
				if($.trim($(this).val()) == '') {
					$(this).val(defaultValue);
				}
			});
		});
		$('#footer #brands a').attr('target', '_blank');
		$('#footer #credits a').attr('target', '_blank');
	});
