$(document).ready(function() {
	$("a#donate").bind("click", function() {
		$("#donate_form").submit()
	});

	
	$("a.single_3").fancybox({
		'overlayShow'			: false,
		'zoomSpeedIn'			: 700,
		'zoomSpeedOut'			: 900,
		'easingIn'				: 'easeOutBack',
		'easingOut'				: 'easeInBack'
	});
	
	$("a.group").fancybox({
		'hideOnContentClick': false
	});
	

$("#various5").fancybox({
		'width'				: '70%',
		'height'			: '78%',
        'autoScale'     	: true,
        'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});

	

});



$(document).ready(function(){ 
	
	$("#dialog").dialog({ autoOpen: false,modal:true });

function checkLength(o) {

	if ( o.val().length < 1 ) {
	o.addClass('ui-state-error');
	return false;
	} else {
	return true;
	}
}

function trimspace(s)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}

function isInteger(s)
{ 
	var i;
	var ss = s.val();
	var ssa = trimspace(ss);
	
	for (i = 0; i < ssa.length; i++)
	{
		var c = ssa.charAt(i);
		
		if (((c < "0") || (c > "9") )) 
		{
		s.addClass('ui-state-error');
		$(".validateTipsreg").text('Invalid Phone Number. Only Digits Allowed').addClass('ui-state-highlight');
		setTimeout(function() {
			$(".validateTipsreg").removeClass('ui-state-highlight', 4000);
			$(".validateTipsreg").text('');
			}, 4000);		
		return false;
		}
	}
	return true;
}

$("#reghere").click( function() 
{
		$("#dialog-reg").dialog('open'); 
});

$("#firstname").keyup( function() 
{
		$("#firstname").removeClass('ui-state-error');
});

$("#lastname").keyup( function() 
{
		$("#lastname").removeClass('ui-state-error');
});

$("#phone").keyup( function() 
{
		$("#phone").removeClass('ui-state-error');
});

$("#dialog-reg").dialog({
			autoOpen: false,
			height: 500,
			width: 350,
			modal: true,
			buttons: {
				'Register': function() {
					var bValid = true;
					$("#firstname").removeClass('ui-state-error');
					$("#lastname").removeClass('ui-state-error');
					$("#phone").removeClass('ui-state-error');
					bValid = bValid && checkLength($("#firstname"));
					bValid = bValid && checkLength($("#lastname"));
					bValid = bValid && checkLength($("#phone")) && isInteger($("#phone"));
					if (bValid) {
							

							
	 	$.ajax({
          type: 'post',
          url: 'http://www.fitnessequipment-services.co.uk/loginpages/email.php',
		  data: 'firstname='+$("#firstname").val()+'&lastname='+$("#lastname").val()+'&company='+$("#company").val()+'&phone='+$("#phone").val()+'&comments='+$("#comments").val(),      
          success: function(results) 
		  {
			if (trim(results)=="T")
			{
				
				pageTracker._trackPageview('/register/http://www.fitnessequipment-services.co.uk');
				
				$(".validateTipsreg").text('Thank you for registering your interest. We will get in touch with you soon.').addClass('ui-state-highlight');
				setTimeout(function() {
					$(".validateTipsreg").removeClass('ui-state-highlight', 4000);
					$(".validateTipsreg").text('');
				}, 4000);
				setTimeout(function() {
				$("#dialog-reg").dialog('close');				
				},6000);
				
			}
			
          }
      }); // end ajax 
							
							
							
							
					}
				}
			},
			close: function() {
				$("#firstname").val('').removeClass('ui-state-error');
				$("#lastname").val('').removeClass('ui-state-error');
				$("#company").val('').removeClass('ui-state-error');
				$("#phone").val('').removeClass('ui-state-error');
				$("#comments").val('').removeClass('ui-state-error');
			}
		});


		
/*
* Forgot Password Functions Here
*/



$("#showforgot").click( function() 
{
		$("#dialog-form").dialog('open');
});

$("#user_name").keyup( function() 
{
		$("#user_name").removeClass('ui-state-error');
});

$("#dialog-form").dialog({
			autoOpen: false,
			height: 250,
			width: 350,
			modal: true,
			buttons: {
				'Get Password': function() {
					var bValid = true;
					$("#user_name").removeClass('ui-state-error');
					bValid = bValid && checkLength($("#user_name"));
					if (bValid) {
							

							
	 	$.ajax({
          type: 'post',
          url: 'loginpages/forpassnew.php',
		  data: 'username='+$("#user_name").val(),      
          success: function(results) 
		  {
			if (trim(results)=="ER")
			{
				$(".validateTips").text('Invalid Username').addClass('ui-state-highlight');
				setTimeout(function() {
					$(".validateTips").removeClass('ui-state-highlight', 4000);
					$(".validateTips").text('');
				}, 4000);
			}
			else if(trim(results)=="T")
			{
				$(".validateTips").text('Your Password Has Been Emailed To The Email-Id Associated With Your Account.').addClass('ui-state-highlight');
				setTimeout(function() {
					$(".validateTips").removeClass('ui-state-highlight', 4000);
					$(".validateTips").text('');
					$(this).dialog('close');
				}, 4000);
			}
			else if(trim(results)=="IN")
			{
				$(".validateTips").text('Your Account Is Not Active. Please Contact Fitness World On  +44 (0) 117 9022022.').addClass('ui-state-highlight');
				setTimeout(function() {
					$(".validateTips").removeClass('ui-state-highlight', 4000);
					$(".validateTips").text('');
				}, 4000);
				
				setTimeout(function() {
				$("#dialog-form").dialog('close');				
				},6000);				
			}			
          }
      }); // end ajax 
							
							
							
							
					}
				}
			},
			close: function() {
				$("#user_name").val('').removeClass('ui-state-error');
			}
		});


/*
* Login Functions Here
*/
		
		
	$("#username_err").hide();
	$("#password_err").hide();
	$("#username").keyup( function() 
	{
		//$("#username_err").hide();
		$("#username").removeClass('ui-state-error');
	});
	$("#password").keyup( function() 
	{
		//$("#password_err").hide();
		$("#password").removeClass('ui-state-error');
	});	
	$("#loginbutton").click( function() 
	{ 
		$("#dialog").html('');
		var _u = $("#username").val();
		var _p = $("#password").val();
		var job_id = $("#job_id").val();
		var err=0;
		if (_u=="")
		{
		//$("#username_err").show();
		$("#username").addClass('ui-state-error');
		err=1;
		}
		if (_p=="")
		{
		//$("#password_err").show();
		$("#password").addClass('ui-state-error');
		err=1;
		}
		
		
		if (err==0)
		{
		 $.ajax({
          type: 'post',
          url: 'loginpages/logon.php',
		  data: 'username='+_u+'&password='+_p,      
          success: function(results) 
		  {
		   
			if (trim(results)=="E")
			{
				$("#dialog").html('Invalid Login');
				$("#dialog").dialog('open');
			}
			else if (trim(results)=="LOGGED")
			{
				$("#dialog").html('User Already Logged In.');
				$("#dialog").dialog('open');
			}
			else
			{
			$.blockUI({ message: '<h1>Please Wait</h1>'}); 
			
			document.location.href='feslogon.php';
			}
          }
      }); // end ajax 
	  
	  }
	
	})
	
})


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

