
$(document).ready(function(){
//---------- START DOCUMENT READY -------------//   
	
		$('#bf_forgotten').click(function()
		{
			showForgotten();
		});

		$('#bf_registration').click(function()
		{
			
			showRegistration();
		});

	
		
				
		$('.bf_form_send').click(function(e)
		{	
		e.preventDefault();
				
		var form = $(this).parent().parent().parent();
			if ( valid(form) )
			{
				
				$.post("ajax.php", { func: $(this).attr('name'), data: $(form).formSerialize() },
				  function(data)
				  {				  	
					action(data);
									 
				  },"json");
			}
			
			else
			{
			
			}
			
			
			});



	// dla (Forgotten) ponownej zmiany numeru
	$('#password_form input[name=number]').change(function()
	{

		$('#forgotten_form input[name=number]').val($('#password_form input[name=number]').val());	
	});
		
//---------- STOP DOCUMENT READY -------------//  	 
 });



function valid(form)
{

	var valid = true;
	//alert($(form).parent().parent().attr('id'));
	if ( $(form).parent().attr('id') == 'profil_form' )
	{
		//alert('in');
		if ( ! valCompare( $(form).find('input[name=password]'),$(form).find('input[name=password2]') ) ) valid = false;		
		if ( ! valEmail($(form).find('input[name=email]') ) ) valid = false;
		
 		

		return valid;
		
	}		
	

	
	
	$(form).find('input').not('[type=submit]').each(function()
	{
		
		if ($(this).attr('name') == 'email' || $(this).attr('name') == 'email2' )
		{			
			if ( ! valEmail($(this)) ) valid = false;
		
		}
		else
		{	
			if ( ! val($(this)) ) valid = false;
			
		}
		if ($(this).attr('name') == 'check' )
		{			
			if ( ! valCheck($(this)) ) valid = false;
		
		}
	
	});

	if ( $(form).parent().attr('id') == 'registration_form' )
	{
		

		if ( ! valCompare2( $(form).find('input[name=password]'),$(form).find('input[name=password2]') ) ) valid = false;
		if ( ! valCompareEmail( $(form).find('input[name=email]'),$(form).find('input[name=email2]') ) ) valid = false;


	
	}

//alert('valid: '+valid);
	return valid;
}

function action(data)
{

	if ( data['info']  )
	{
		$('.bf_info_back').html(data['info']);
	}
	if ( data['action'] )
	{
		
		switch( data['action'] )
		{
		case 'showSimple': showSimple(); break;
		case 'showPassword': showPassword(); break;
		case 'showRegistration': showRegistration(); break;
		case 'showForgotten': showForgotten(); break;
		case 'showProfil': showProfil(); break;
		case 'redirect': redirect(data['redirect']); break;
		}
	}

}

function redirect(red)
{
	window.location.href = red;
}

function showSimple()
{
	$('.bf_form').css('display','none');
	$('#simple_form').css('display','block');
}

function showProfil()
{
	$('.bf_form').css('display','none');
	$('#profil_form').css('display','block');
}

function showPassword()
{	
	$('#password_form input[name=number]').val($('#simple_form input[name=number]').val());
	$('.bf_form').css('display','none');
	$('#password_form').css('display','block');
}


function showForgotten()
{

	//$('.bf_form').css('display','none');
	//$('#forgotten_form input[name=number]').val($('#password_form input[name=number]').val());
	$('#forgotten_form').css('display','block');
}
function showRegistration()
{	
	$('#registration_form input[name=number]').val($('#simple_form input[name=number]').val());
	$('.bf_form').css('display','none');
	$('#registration_form').css('display','block');
}

 var liveSite =''; 


 function val(th)
 {
 	
 	if ( $(th).val() != '' )
 		{
 		 $(th).css('background','transparent');
 	 	return true;
 	 	}
 	else
 		{ 		
 		 $(th).css('background','#f8b5ad');
 		return false;
 		} 	
 		
 }

 function valCompare(th,th2)
 {
	
	if (   $(th).val() != $(th2).val()   )

	{
		$(th).css('background','#f8b5ad');
		$(th2).css('background','#f8b5ad');
		return false;
	}
	else
 	{
		$(th).css('background','transparent');
		$(th2).css('background','transparent');
		return true;
	}
 }

function valCompare2(th,th2)
 {
	
	if (  ( $(th).val() != $(th2).val() ) || (  $(th).val() == '') || ( $(th2).val() == '' ) )
	{
		$(th).css('background','#f8b5ad');
		$(th2).css('background','#f8b5ad');
		return false;
	}
	else
 	{
		$(th).css('background','transparent');
		$(th2).css('background','transparent');
		return true;
	}
 }


function valCompareEmail(th,th2)
 {
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	str =  $(th).val();
	str2 = $(th2).val();

	
	if (   ( $(th).val() != $(th2).val() ) || !str.match(emailRegEx) || !str.match(emailRegEx) )
	{
		$(th).css('background','#f8b5ad');
		$(th2).css('background','#f8b5ad');
		return false;
	}
	else 
 	{
		$(th).css('background','transparent');
		$(th2).css('background','transparent');
		return true;
	}
 }

 
 function valCheck(th)
 {
 	
 	if ( $(th).attr('checked') )
 		{
 		 $(th).parent().css('background','transparent');
 	 	return true;
 	 	}
 	else
 		{ 		
 		 $(th).parent().css('background','#f8b5ad');
 		return false;
 		} 	
 		
 }

function valEmail(th)
{
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	str =  $(th).val();
	if(str.match(emailRegEx))
	{
 		 $(th).css('background','transparent');
 	 	return true;
 	 	}
	else
	{	
		$(th).css('background','#f8b5ad');
		return false;
	}
}

function showShadowbox(url,w,h)
{
  var tmp=new Object();
  tmp.player='iframe';
  tmp.content=url;
  if(w!='default' && w!='') tmp.width=w;
  if(h!='default' && h!='') tmp.height=h;

  Shadowbox.open(tmp);
}

