//Focus function
function makefocus (divid)
{
	if (divid == 'login')
    {
        setTimeout(function() {$('#lemail').focus()}, 700);
    }
   
    else if (divid == 'register')
    {
        setTimeout(function(){$('#remail').focus()}, 700);
    }
    else if (divid == 'signup')
    {
        setTimeout(function() {$('#semail').focus()}, 700);
    }
}

//Checkbox function
function remmechk()
{
 if ( $('#rememberme').is(':checked') )
 {
  $('#rememberme').val("forever");
 }
 else
 {
  $('#rememberme').val("no");
 }
}


$(document).ready(function() {  
    
//Tooltips - login
$("#login :input.title").tooltip({ 
    position: "center left", 
    relative: "true",
    offset: [55, -5], 
    effect: "fade", 
    opacity: 1, 
    predelay: 500,
    tip: '.tooltip' 
});  
$("#create :input.title").tooltip({ 
    position: "center left", 
    relative: "true",
    offset: [90, -5], 
    effect: "fade", 
    opacity: 1,
    predelay: 500, 
    tip: '.tooltip' 
});   

// $("#register").submit(function()
// {
// 	doUserReg();
// });

$("#submitregister").click(function()
{
	doUserReg();	
});

// $("#formLogin").click(function()
// {
// 	doUserLogin();
// });

$("#submitlogin").click(function()
{
	doUserLogin();	
});

/* SIGNUP FORM */
$("#formSignup").submit(function() {						 
							 
    $('#SignUpBox').removeClass().addClass('InfoBoxMenu').html('<span>Processing...</span>').fadeIn(1000);
	$.post("/form-checker?check=signup", {
		semail:$('#semail').val(),
		rand:Math.random()
	},function( data ) {
		if( data == 'done' ) {
		  	$("#SignUpBox").fadeTo (200, 0.1, function()
			{ 
			  //add message and change the class of the box and start fading
              $(this).html('<span>Thank you! We will send you an invitation code soon.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuOk').fadeTo(900,1);
              $("#semail").val("");
			});
			return;
		}
  	$("#SignUpBox").fadeTo (200, 0.1, function()
	{ 
	  //add message and change the class of the box and start fading
      $(this).html('<span>Your email address does not seem correct.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);
	});	
    });
    return false;	        
});
/* SIGNUP FORM */

});

//Registration form
function doUserReg() {
	
	if ($("#submitregister").length > 0) $("#submitregister").hide(100);
    
    $('#RegisterBox').removeClass().addClass('InfoBoxMenu').html('<span>Creating your account...</span>').fadeIn(1000);
	$.post("/form-checker?check=register", {
        remail:$('#remail').val(),
        rpassword:$('#rpassword').val(),
        rpassword2:$('#rpassword2').val(),
        phone:$('#phone').val(),
        refid:$('#refid').val(),
		param:$("#param").val(),
	rand:Math.random()
    }, function (data) {
        if (data == 'email_empty')
        {
		  	$("#RegisterBox").fadeTo (200, 0.1, function()
			{ 
			  //add message and change the class of the box and start fading
              $(this).html('<span>Please enter your email address.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);
			});		
			return;            
        }
        
        if (data == 'wrong_email')
        {
		  	$("#RegisterBox").fadeTo (200, 0.1, function()
			{ 
			  //add message and change the class of the box and start fading
			if ($("#submitregister").length > 0)
			{
				
				$("#submitregister").fadeOut(500);
				$(this).html('<span>Your email address does not seem correct.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);
				
		        setTimeout(function() {
					$("#RegisterBox").attr("style", "display:none");
					$("#submitregister").fadeIn(1000);
				}, 2000);
			}
			else
			{
				$(this).html('<span>Your email address does not seem correct.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);
			}
              
			});		
			return;            
        }
         
        if (data == 'email_exists')
        {
		  	$("#RegisterBox").fadeTo (200, 0.1, function()
			{ 
			  //add message and change the class of the box and start fading
				if ($("#submitregister").length > 0)
				{

					$("#submitregister").fadeOut(500);
					$(this).html('<span>Email address that you have entered is already registered in CallRec.me.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);

			        setTimeout(function() {
						$("#RegisterBox").attr("style", "display:none");
						$("#submitregister").fadeIn(1000);
					}, 2000);
				}
				else
				{
					$(this).html('<span>Email address that you have entered is already registered in CallRec.me.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);
				}
           
			});		
			return;            
        }
         
        if (data == 'pass_empty')
        {
		  	$("#RegisterBox").fadeTo (200, 0.1, function()
			{ 
			  //add message and change the class of the box and start fading
				if ($("#submitregister").length > 0)
				{
					$("#submitregister").fadeOut(500);
					$(this).html('<span>Please choose a password for your account.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);

			        setTimeout(function() {
						$("#RegisterBox").attr("style", "display:none");
						$("#submitregister").fadeIn(1000);
					}, 2000);
				}
				else
				{
					$(this).html('<span>Please choose a password for your account.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);
				}
				              
			});		
			return;            
        }
         
        if (data == 'pass_match')
        {
		  	$("#RegisterBox").fadeTo (200, 0.1, function()
			{ 
			  //add message and change the class of the box and start fading
				if ($("#submitregister").length > 0)
				{
					$("#submitregister").fadeOut(500);
					$(this).html('<span>Passwords do not match.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);

			        setTimeout(function() {
						$("#RegisterBox").attr("style", "display:none");
						$("#submitregister").fadeIn(1000);
					}, 2000);
				}
				else
				{
					$(this).html('<span>Passwords do not match.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);
				}

			});		
			return;            
        }
          
        if (data == 'pass_length')
        {
		  	$("#RegisterBox").fadeTo (200, 0.1, function()
			{ 
			  //add message and change the class of the box and start fading
				if ($("#submitregister").length > 0)
				{
					$("#submitregister").fadeOut(500);
	              	$(this).html('<span>Your password is too short - it should have at least 4 letters or numbers.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);

			        setTimeout(function() {
						$("#RegisterBox").attr("style", "display:none");
						$("#submitregister").fadeIn(1000);
					}, 2000);
				}
				else
				{
	              	$(this).html('<span>Your password is too short - it should have at least 4 letters or numbers.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);
				}
			});		
			return;            
        }
          
        if (data == 'wrong_pass')
        {
		  	$("#RegisterBox").fadeTo (200, 0.1, function()
			{ 
			  //add message and change the class of the box and start fading
				if ($("#submitregister").length > 0)
				{
					$("#submitregister").fadeOut(500);
	              	$(this).html('<span>Please use only letters and digits in your password.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);

			        setTimeout(function() {
						$("#RegisterBox").attr("style", "display:none");
						$("#submitregister").fadeIn(1000);
					}, 2000);
				}
				else
				{
	              	$(this).html('<span>Please use only letters and digits in your password.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);
				}
			});		
			return;            
        }
          
        if (data == 'phone_length')
        {
		  	$("#RegisterBox").fadeTo (200, 0.1, function()
			{ 
			  //add message and change the class of the box and start fading
				if ($("#submitregister").length > 0)
				{
					$("#submitregister").fadeOut(500);
	              	$(this).html('<span>Please enter the full phone number, including country and area codes.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);

			        setTimeout(function() {
						$("#RegisterBox").attr("style", "display:none");
						$("#submitregister").fadeIn(1000);
					}, 2000);
				}
				else
				{
	              	$(this).html('<span>Please enter the full phone number, including country and area codes.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);
				}
			});		
			return;            
        }
          
        if (data == 'phone_used')
        {
		  	$("#RegisterBox").fadeTo (200, 0.1, function()
			{ 
			  //add message and change the class of the box and start fading
				if ($("#submitregister").length > 0)
				{
					$("#submitregister").fadeOut(500);
	              	$(this).html('<span>This phone number is already registered in CallRec.me.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);

			        setTimeout(function() {
						$("#RegisterBox").attr("style", "display:none");
						$("#submitregister").fadeIn(1000);
					}, 2000);
				}
				else
				{
	              	$(this).html('<span>This phone number is already registered in CallRec.me.</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);
				}
			});		
			return;            
        }

        if (data == 'checkout')
        {
			var params = $("#param").val();
			document.location='/checkout?param='+params;
		   	return;           
        }

		if (data == 'pricing')
		{
			document.location = '/pricing';
			return;
		}
          
        if (data == 'done')
        {
		   document.location='/verify-user';
		   return;           
        }                                                                                                                                                       
    });
    return false;
}

//LogIn Form    
function doUserLogin() {
	
	if ($("#submitlogin").length > 0) $("#submitlogin").hide(100);
	
    $('#LogInBox').removeClass().addClass('InfoBoxMenu').html('<span>Logging in...</span>').fadeIn(1000);
	$.post("/form-checker?check=login", {
		lemail:$('#lemail').val(),
		lpassword:$('#lpassword').val(),
		rememberme:$('#rememberme').val(),
		param:$("#param").val(),
		rand:Math.random()
	},function(data) {
		if( data == 'checkout' ) {		
			var params = $("#param").val();
			
			document.location = '/checkout?param='+params;
			return;
		}
		
		if (data =='done') {			
			document.location='/';
		}
		
		else {
		
  	$("#LogInBox").fadeTo (200, 0.1, function()
	{ 
	  //add message and change the class of the box and start fading
		if ($("#submitlogin").length > 0)
		{
			$("#submitlogin").fadeOut(500);
	      	$(this).html('<span>Login failed. Maybe you have misstyped your e-mail or password?</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);

	        setTimeout(function() {
				$("#LogInBox").attr("style", "display:none");
				$("#submitlogin").fadeIn(1000);
			}, 2000);
		}
		else
		{
	      	$(this).html('<span>Login failed. Maybe you have misstyped your e-mail or password?</span>').removeClass('InfoBoxMenu').addClass('InfoBoxMenuError').fadeTo(900,1);
		}
	
	
	});	
    }

	});
    return false;	        
}
















