//---------- Password Reset Form Validation ----------//

function validate_password()
{
	$('form#password-reset').validate(
	{
		errorClass: 'invalid',
		validClass: 'valid',
		errorElement: 'span',
		onkeyup: false,
		rules: 
		{
			email:		
			{
				email: true,
				required: true
			},
			city:	{ required: true },
			password:
			{
				required: true,
				pgs_password: true,
				minlength: 5
			},
			confirmPassword:
			{
				required: true,
				pgs_password: true,
				minlength: 5,
				equalTo: '#password'
			},
			agreeterms: { required: true }
		},
		highlight: function(element, errorClass, validClass)
		{
			$(element).parents('td').removeClass('valid').addClass('invalid');
			
			if($(element).attr('name') == 'agreeterms')
			{
				$('#agreeterms').parents('tr').find('td').removeClass('valid').addClass('invalid');
			}
		},
		unhighlight: function(element, errorClass, validClass)
		{
			$(element).parents('td').removeClass('invalid').addClass('valid');
			
			if($(element).attr('name') == 'agreeterms')
			{
				$('#agreeterms').parents('tr').find('td').removeClass('invalid').addClass('valid');
			}
		},
		errorPlacement: function(error, element) 
		{
			$('span.info').css('color', '#FF0000');
			$('span.info').html(error.html());
		}
	});
}

//---------- Registration Form Validation ----------//

function validate_registration()
{	
	$('form#registration').validate(
	{	
		errorClass: 'invalid',
		validClass: 'valid',
		errorElement: 'span',
		onkeyup: false,
		groups:
		{
			dob: "birthMonth birthDay birthYear"
		},
		rules: 
		{
            alias:
            {
                minlength: 5,
                maxlength: 25,
                pgs_check_alias:
                {
					url: "/register/check_alias",
					type: "post",
					async: true
				},
                required: true
			},
            firstName:	{ required: true },
			lastName:	{ required: true },
			birthMonth:	{ required: true },
			birthDay:	{ required: true },
			birthYear:	{ required: true },
			email:		
			{
				email: true,
				required: true
			},
			country:	{ required: true },
			address1:	{ required: true },
			houseNumber:{ required: true },
			postalCode:	
			{ 
				required: true, 
				pgs_postal_code: { locale: current_lang }
			},
			city:		{ required: true },
			phoneNumber:
			{ 
				required: true,
				pgs_phone_number: true
			},
			password:
			{
				required: true,
				pgs_password: true,
				minlength: 5
			},
			confirmPassword:
			{
				required: true,
				pgs_password: true,
				minlength: 5,
				equalTo: '#password'
			},
			pinCode:
			{
				required: true,
				minlength: 10,
				maxlength: 10
			},
			agreeterms: { required: true }
		},
		highlight: function(element, errorClass, validClass)
		{
			if($(element).attr('name') == 'agreeterms')
			{
				$('#agreeterms').parents('tr').find('td').removeClass('valid').addClass('invalid');
			}
			else if($(element).attr('name') == 'birthDay' || $(element).attr('name') == 'birthMonth' || $(element).attr('name') == 'birthYear')
			{
				$(element).removeClass('valid');
				
				if($('#birthDay').hasClass('valid') == false || $('#birthMonth').hasClass('valid') == false || $('#birthYear').hasClass('valid') == false)
				{
					$(element).parents('td').removeClass('valid').addClass('invalid');
				}
			}
			else if($(element).attr('name') == 'address1' || $(element).attr('name') == 'houseNumber')
			{
				$(element).removeClass('valid');
				
				if($('#address1').hasClass('valid') == false || $('#houseNumber').hasClass('valid') == false)
				{
					$(element).parents('td').removeClass('valid').addClass('invalid');
				}
			}
			else
			{
				$(element).parents('td').removeClass('valid').addClass('invalid');
			}
		},
		unhighlight: function(element, errorClass, validClass)
		{
			if($(element).attr('name') == 'agreeterms')
			{
				$('#agreeterms').parents('tr').find('td').removeClass('invalid').addClass('valid');
			}
			else if($(element).attr('name') == 'birthDay' || $(element).attr('name') == 'birthMonth' || $(element).attr('name') == 'birthYear')
			{
				$(element).addClass('valid');
				
				if($('#birthDay').hasClass('valid') && $('#birthMonth').hasClass('valid') && $('#birthYear').hasClass('valid'))
				{
					$(element).parents('td').removeClass('invalid').addClass('valid');
				}
			}
			else if($(element).attr('name') == 'address1' || $(element).attr('name') == 'houseNumber')
			{
				$(element).addClass('valid');
				
				if($('#address1').hasClass('valid') && $('#houseNumber').hasClass('valid'))
				{
					$(element).parents('td').removeClass('invalid').addClass('valid');
				}
			}
			else
			{
				$(element).parents('td').removeClass('invalid').addClass('valid');
			}
		},
		errorPlacement: function(error, element) 
		{
			$('span.info').css('color', '#FF0000');
			$('span.info').html(error.html());
		}
	});
	
	$('#registration input').bind('focusin', function()
	{
		$(this).siblings('div.tool-tip').fadeIn('slow');
	});
	
	$('#registration input').bind('focusout', function()
	{
		$(this).siblings('div.tool-tip').fadeOut('slow');
	});
}

//---------- Pin Code Validation ----------//

function validate_pin()
{
	$('form#update-pin').validate(
	{
		errorClass: 'invalid',
		validClass: 'valid',
		errorElement: 'span',
		onkeyup: false,
		rules: 
		{
			pinCode:
			{
				required: true,
				minlength: 10,
				maxlength: 10
			}
		},
		errorPlacement: function(error, element) 
		{
			$('span.info').css('color', '#FF0000');
			$('span.info').html(error.html());
		}
	});
}

//---------- Process Reg Step One ----------//

function process_reg_step_one()
{
	if($('form#registration').validate().form())
	{
		$('div.modal div#reg-view-wrapper').hide('blind', function()
		{
			$('div.modal div#reg-loader').fadeIn('fast');
		
			$('form#registration').ajaxSubmit(
			{
				success: function(response)
				{
					$('div.modal div#reg-loader').fadeOut('fast', function()
					{
						$('div.modal div#reg-view-wrapper').html(response);

						$('div.modal div#reg-view-wrapper').show('blind');
					});
				}
			});	
		});
	}
}

//---------- Process Reg Step Two ----------//

function process_reg_step_two(redirect)
{
    if($('form#registration').validate().form())
	{
		$('div.modal div#reg-view-wrapper').hide('blind', function()
		{
			$('div.modal div#reg-loader').fadeIn('fast');

			$('form#registration').ajaxSubmit(
			{
				dataType: 'json',
                success: function(response)
				{
					//If the registration was successful, leave the loading screen up while we make our next move.
					if(response.success == true)
					{
						//Load the view that was returned. This is for tracking only and won't actually be shown
						$('div.modal div#reg-view-wrapper').html(response.view_data);
						
						if(redirect == 'deposit')
						{
							first_deposit();
						}
						else
						{
							window.location.href = webRoot + 'games/slots';
						}
					}
					//Otherwise, show returned view with error.
					else
					{
						$('div.modal div#reg-loader').fadeOut('fast', function()
						{
							//Load the view that was returned.
							$('div.modal div#reg-view-wrapper').html(response.view_data);
						
							$('div.modal div#reg-view-wrapper').show('blind');
						});
					}
				}
			});
		});
	}
}

//------------------------------------------------------------------------

/**
 *	first_deposit
 */
 function first_deposit()
 {
	var data = ajax_page_load('members/deposit', false, true)
						
	if(data.statusText == 'OK')
	{
		//Fade out login button
		$('#already-a-member span').fadeOut('fast');
		$('#already-a-member a').fadeOut('fast');
	
		//Show deposit page
		$('div.modal div#reg-loader').fadeOut('fast', function()
		{
			$('div.modal div#reg-view-wrapper').html(data.responseText);

			$('div.modal div#reg-view-wrapper').show('blind');
		});
		
		//Change the click handler to force a redirect. This will update the display so user's know they are logged in.
		$('a.simplemodal-close').click(function()
		{
			window.location.href = window.location.href = webRoot + 'games/slots';
		});
	}
 }
