/*
* 2007-2015 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <contact@prestashop.com>
*  @copyright  2007-2015 PrestaShop SA
*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/
$(document).ready(function(){
	if(typeof fbpsc != 'undefined')
		fbpsc.getConnectors('Login2022', 'Login2022');

	if($('.guest-form').hasClass('create')){
		commande_newcustomer();
	}

	//$(document).on('submit', '#new_account_form', function(e){
		//e.preventDefault();
		//submitFunction();
	//});
	//$(document).on('submit', '#create-account_form', function(e){
	//	e.preventDefault();
	//	submitFunction();
	//});
	$('.is_customer_param').hide();
	$('.commande_guest').click(function(e){
		e.preventDefault();
		$('.partie_gauche_auth .login-form').stop().fadeTo('fast',0,function(){
			var title_livraison=$('#new_account_form h1.title_livraison').attr('data-commander-en-invite');
			$('#new_account_form .title_livraison').html(title_livraison);
			$('.partie_gauche_auth .login-form').hide();
			$('.mot_de_passe.form-group').hide();
			$('#new_account_form').stop().show().fadeTo('fast',1);
			$('html,body').stop().animate({scrollTop:0}, 400);
			$('#submitAccount').attr('name','submitGuestAccount2022');
			$('#display_guest_checkout').attr('value','1'); 
			$('#email_create').attr('value','0'); 
			$('#is_new_customer').attr('value','0');  
			$('.commande_guest').hide();
			
		});
	});
	$('#commande_newcustomer').click(function(e){
		e.preventDefault();
		commande_newcustomer();
	});
	$('#new_account_form .text_retour').click(function(e){
		e.preventDefault(); 
		$('#new_account_form').stop().fadeTo('fast',0,function(){
			$('#new_account_form').hide();
			$('.partie_gauche_auth .login-form').show().stop().fadeTo('fast',1);
			$('html,body').stop().animate({scrollTop:0}, 400);
			$('.commande_guest').show();
		});
	});
});

function commande_newcustomer(){
	var title_livraison=$('#new_account_form h1.title_livraison').attr('data-creer-compte');
	$('#new_account_form .title_livraison').html(title_livraison);
	$('.mot_de_passe.form-group').show();
	$('.partie_gauche_auth .login-form').stop().fadeTo('fast',0,function(){
		$('#submitAccount').attr('name','submitAccount2022');
		$('#display_guest_checkout').attr('value','0');
		$('#email_create').attr('value','1');
		$('#is_new_customer').attr('value','1');

		$('.partie_gauche_auth .login-form').hide();
		$('#new_account_form').stop().show().fadeTo('fast',1);
		$('html,body').stop().animate({scrollTop:0}, 400);
		$('.commande_guest').hide();
	});
}

function submitFunction()
{
	$('#create_account_error').html('').hide();
	
	$.ajax({
		type: 'POST',
		url: baseUri + '?rand=' + new Date().getTime(),
		async: true,
		cache: false,
		dataType : "json",
		headers: { "cache-control": "no-cache" },
		data:
		{
			controller: 'authentication',
			SubmitCreate: 1,
			ajax: true,
			email_create: $('#create_email_2022').val(),
			back: $('input[name=back]').val(),
			token: token
		},
		success: function(jsonData)
		{
			if (jsonData.hasError)
			{
				var errors = '';
				for(error in jsonData.errors)
					//IE6 bug fix
					if(error != 'indexOf')
						errors += '<li>' + jsonData.errors[error] + '</li>';
				$('#create_account_error').html('<ol>' + errors + '</ol>').show();
			}
			else
			{
				// adding a div to display a transition
				var my_parrain="";
				if($('#my_parrain').length==1){				
						var my_parrain=$('#my_parrain').val();
				}
				console.log('test'+my_parrain);
				$('#center_column').html('<div id="noSlide">' + $('#center_column').html() + '</div>');
				$('#noSlide').fadeOut('slow', function()
				{
					$('#noSlide').html(jsonData.page);
					$(this).fadeIn('slow', function()
					{
						if (typeof bindUniform !=='undefined')
							bindUniform();
						if (typeof bindStateInputAndUpdate !=='undefined')
							bindStateInputAndUpdate();
						document.location = '#account-creation';
						if(my_parrain!=""){	 
							if($('#boixsponsoring').val()=="")
								$('#boixsponsoring').attr('value',my_parrain);
						}
						
					});
				});
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			error = "TECHNICAL ERROR: unable to load form.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus;
			if (!!$.prototype.fancybox)
			{
				$.fancybox.open([
				{
					type: 'inline',
					autoScale: true,
					minHeight: 30,
					content: "<p class='fancybox-error'>" + error + '</p>'
				}],
				{
					padding: 0
				});
			}
			else
				alert(error);
		}
	});
}


function authValidateEmail(email) { 
	 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	 return re.test(email);
}

function authNextStep() {

	 $('.alert.alert-danger.step1').css('display','none');

	if(authValidateEmail(document.getElementById('email').value)){
		$('.step2').css('display','block');
		$('.step1').css('display','none');
		return true;
	}else{		
		setTimeout(function() {
			$('.step1').css('display','block');
			return false;
		}, 200);		
	}
}

function authViewPasswd() {
	var x = $("input[name*='passwd']");
	if (x.attr('type') === "password") {
		x.attr('type','text');
		$('#login_form .icone-eye').addClass('active');
	} else {
		x.attr('type','password');
		$('#login_form .icone-eye').removeClass('active');
	}
}