// JavaScript Document

var curId = 0;
var allowLog = true;
var allowBasket = true;
var timerBasket = null;
var freeport = 61;

function is_numeric(num)
	{
		var exp = new RegExp("^[0-9-.]*$","g");
		return exp.test(num);
	}
	
	
function number_format(num,dec,pnt,thou,curr1,curr2,n1,n2) {var x = Math.round(num * Math.pow(10,dec));if (x >= 0) n1=n2='';var y = (''+Math.abs(x)).split('');var z = y.length - dec; if (z<0) z--; for(var i = z; i < 0; i++) y.unshift('0'); if (z<0) z = 1; y.splice(z, 0, pnt); if(y[0] == pnt) y.unshift('0'); while (z > 3) {z-=3; y.splice(z,0,thou);}var r = curr1+n1+y.join('')+n2+curr2;return r;}
	

function strlen (string) {
 
    var str = string+'';
    var i = 0, chr = '', lgth = 0;
 
    var getWholeChar = function (str, i) {
        var code = str.charCodeAt(i);
        var next = '', prev = '';
        if (0xD800 <= code && code <= 0xDBFF) { 
            if (str.length <= (i+1))  {
                throw 'High surrogate without following low surrogate';
            }
            next = str.charCodeAt(i+1);
            if (0xDC00 > next || next > 0xDFFF) {
                throw 'High surrogate without following low surrogate';
            }
            return str.charAt(i)+str.charAt(i+1);
        } else if (0xDC00 <= code && code <= 0xDFFF) {
            if (i === 0) {
                throw 'Low surrogate without preceding high surrogate';
            }
            prev = str.charCodeAt(i-1);
            if (0xD800 > prev || prev > 0xDBFF) { 
                throw 'Low surrogate without preceding high surrogate';
            }
            return false; 
        }
        return str.charAt(i);
    };
 
    for (i=0, lgth=0; i < str.length; i++) {
        if ((chr = getWholeChar(str, i)) === false) {
            continue;
        } 
        lgth++;
    }
    return lgth;
}
	

function nl2br (str, is_xhtml) {

 var breakTag = '';
 breakTag = '<br />';
if (typeof is_xhtml != 'undefined' && !is_xhtml) {
 breakTag = '<br>';
}
 return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}

function trim (myString)
{
return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
}


function viewProd(id){

if(id!=curId){
curId = id;
$('.liste-produits').find('.selected').removeClass('selected');
$('#link-'+id).addClass('selected');
$('#viewProd').html('<h1><span class="loader"></span>Chargement</h1>');

$('#viewProd').load('fiche-produit.php?id='+id);

}
}


function addBasket(id){
	if(allowBasket==true){	
		allowBasket = false;
		$('#addBasket').hide();
		couleur = $('#b-couleur').val();
		taille = $('#b-taille').val();
		quantite = $('#b-quantite').val();
		$('#loadBasket').show();
		$('#actions').load('addbasket.php?id='+id+'&couleur='+encodeURIComponent(couleur)+'&taille='+encodeURIComponent(taille)+'&quantite='+encodeURIComponent(quantite),function(){
		$('#loadBasket').hide();
		timerBasket = setTimeout(function(){$('#addBasket').fadeOut(400,function(){ allowBasket=true;});},800);
		});
		

	}

}


function login(){
		if(allowLog==true){
			clearTimeout(timerBasket);
			allowLog=false;
			
			email = $('#log-email').val(); 
			passwd = $('#log-passwd').val(); 
			
			$('#action-cx').hide();
			$('.connexion').addClass('loadcx');	
			$('#form-cx').css({opacity:0.2});
			$('#action-cx').load('identify.php?email='+encodeURIComponent(email)+'&passwd='+encodeURIComponent(passwd),function(){
			$('#action-cx').show();
			checkCountry();
			$('.connexion').removeClass('loadcx');	
			$('#form-cx').css({opacity:1});
			allowLog=true;
			});
		}
}


function mainLogin(){
		if(allowLog==true){
			clearTimeout(timerBasket);
			allowLog=false;
			
			email = $('#log-email').val(); 
			passwd = $('#log-passwd').val(); 
			
			$('#action-cx').hide();
			$('.connexion').addClass('loadcx');	
			$('#form-cx').css({opacity:0.2});
			$('#action-cx').load('identify2.php?email='+encodeURIComponent(email)+'&passwd='+encodeURIComponent(passwd),function(){
			$('#action-cx').show();
			checkCountry();
			$('.connexion').removeClass('loadcx');	
			$('#form-cx').css({opacity:1});
			allowLog=true;
			});
		}
}



function logout(){	
	$('#action-cx').load('logout.php',function(){
	$('.remove').show();	
	$('#infos-persos').find('.champ2').val('');
	$('#form-pays').val('61');
	checkCountry();
	});
}

function logout2(){	
	$('#dcx').load('logout2.php');
}


function deleteBasket(id){
	$('#actions').load('deletebasket.php?id='+id);
	$('#li-'+id).slideUp(200,function(){
	$('#li-'+id).html('');
	totalBasket();	
	checkBasketContent();
	});	
}





function calculBasket(id){
if(id>0){
clearTimeout(calculBasket);
$('#load-'+id).show();	
timerbasket = setTimeout(function(){						  
$('#load-'+id).hide();								  
var quantite = parseInt($('#q_'+id).val());
var pu = parseFloat($('#pu_'+id).val());
if (is_numeric(quantite)==false){quantite = 0;}
if(quantite<0){quantite=0;}
var total = quantite * pu;
$('#tot_'+id).val(total);

$('#actions').load('updatebasket.php?id='+id+'&q='+quantite);		

$('#total_'+id).html(number_format(total,2,',',' ','','','','')+' €');
totalBasket();
},300);
}	
}

function totalBasket(){
var total_price = 0;

if(freeport!=$('#form-pays').val()){
total_price += parseFloat($('#fp_price').val());
}

$('.glob_price').each(function(){
	total_price += parseFloat($(this).val());
});

$('#glob_price').html(number_format(total_price,2,',',' ','','','','')+' €');
}


function checkCountry(){
	if(freeport==$('#form-pays').val()){
		$('.fp').hide();	
	}
		else
	{
		$('.fp').show();
	}
	
	totalBasket();
	
}



function validCommand(){

var nom = trim($('#form-nom').val());
var prenom = trim($('#form-prenom').val());
var adresse_1 = trim($('#form-adresse_1').val());
var adresse_2 = trim($('#form-adresse_2').val());
var code_postal = trim($('#form-code_postal').val());
var ville = trim($('#form-ville').val());
var pays = trim($('#form-pays').val());
var email = trim($('#form-email').val());
var telephone = trim($('#form-telephone').val());
var commentaire = trim($('#form-commentaire').val());
var passwd = trim($('#form-passwd').val());

if(is_numeric(pays)==false){pays=0;}

	if(strlen(nom)<1){
		$('#erreurCommande').html("<div>Vous devez indiquer votre nom.</div>");
	}
	else
	if(strlen(prenom)<1){
		$('#erreurCommande').html("<div>Vous devez indiquer votre prénom.</div>");
	}
	else
	if(strlen(adresse_1)<1){
		$('#erreurCommande').html("<div>Vous devez indiquer votre adresse.</div>");
	}
	else
	if(strlen(code_postal)<1){
		$('#erreurCommande').html("<div>Vous devez indiquer votre code postal.</div>");
	}
	else
	if(strlen(ville)<1){
		$('#erreurCommande').html("<div>Vous devez indiquer votre ville.</div>");
	}
	else
	if(pays<2 || pays>193){
		$('#erreurCommande').html("<div>Vous devez indiquer votre pays.</div>");
	}
	else
	if(strlen(email)<1){
		$('#erreurCommande').html("<div>Vous devez indiquer votre adresse email.</div>");
	}
	
	else
	
	{
		$('#erreurCommande').html("");
		$('#basketContent').html("<div class='loadingCommand'>Votre commande est en cours de traitement...</div>");
		$('#actions').load('insert-commande.php?nom='+encodeURIComponent(nom)+'&prenom='+encodeURIComponent(prenom)+'&adresse_1='+encodeURIComponent(adresse_1)+'&adresse_2='+encodeURIComponent(adresse_2)+'&code_postal='+encodeURIComponent(code_postal)+'&ville='+encodeURIComponent(ville)+'&pays='+encodeURIComponent(pays)+'&email='+encodeURIComponent(email)+'&telephone='+encodeURIComponent(telephone)+'&commentaire='+encodeURIComponent(commentaire)+'&passwd='+encodeURIComponent(passwd),function(){
																																																																																																			  window.location='reglement.php';
																																																																																																			  });
	}
	
	
	
}



function checkBasketContent(){
		nbArticle = $('#basketContent').find('.vignette').size();
		if(nbArticle<1){		
			$('.validBasket').hide();		$('#emptyBasket').show();	
		}
}


function payFac(id,uid){
	$('#li-'+id).html('<img src="images/load.gif" alt="Chargement">');
	$('#li-'+id).load('load-facture.php?id='+uid,function(){
	window.location='reglement.php';
	});
}

function cancelFac(id){
	$('#actions').load('cancel-facture.php?id='+id);
	$('#li-'+id).slideUp(100,function(){
	$('#li-'+id).html('');	
	$('#nbW').html($('#listecommandesWait').find('img').size());
	});
}




function updateAccount(){
var nom = trim($('#form-nom').val());
var prenom = trim($('#form-prenom').val());
var adresse_1 = trim($('#form-adresse_1').val());
var adresse_2 = trim($('#form-adresse_2').val());
var code_postal = trim($('#form-code_postal').val());
var ville = trim($('#form-ville').val());
var pays = trim($('#form-pays').val());
var telephone = trim($('#form-telephone').val());
var commentaire = trim($('#form-commentaire').val());
var passwd = trim($('#form-passwd').val());

if(is_numeric(pays)==false){pays=0;}

	if(strlen(nom)<1){
		$('#erreurAccount').html("<div>Vous devez indiquer votre nom.</div>");
	}
	else
	if(strlen(prenom)<1){
		$('#erreurAccount').html("<div>Vous devez indiquer votre prénom.</div>");
	}
	else
	if(strlen(adresse_1)<1){
		$('#erreurAccount').html("<div>Vous devez indiquer votre adresse.</div>");
	}
	else
	if(strlen(code_postal)<1){
		$('#erreurAccount').html("<div>Vous devez indiquer votre code postal.</div>");
	}
	else
	if(strlen(ville)<1){
		$('#erreurAccount').html("<div>Vous devez indiquer votre ville.</div>");
	}
	else
	if(pays<2 || pays>193){
		$('#erreurAccount').html("<div>Vous devez indiquer votre pays.</div>");
	}
	
	else
	
	{
		$('#erreurAccount').html("");
		
		$('#actions').load('update-account.php?nom='+encodeURIComponent(nom)+'&prenom='+encodeURIComponent(prenom)+'&adresse_1='+encodeURIComponent(adresse_1)+'&adresse_2='+encodeURIComponent(adresse_2)+'&code_postal='+encodeURIComponent(code_postal)+'&ville='+encodeURIComponent(ville)+'&pays='+encodeURIComponent(pays)+'&telephone='+encodeURIComponent(telephone)+'&commentaire='+encodeURIComponent(commentaire),function(){
	
	$('#erreurAccount').html("<div class='ok'>Informations mises à jour.</div>");
																																																																																																			  });
																																																																																																			
	}
	
	
	
}




function checkCode(){
	$('#load-promo').show();	
	var codepromo = trim($('#code-promo').val());
	$('#checkcode').load('checkcode.php?code='+encodeURIComponent(codepromo),function(){
	$('#load-promo').hide();
	});
}



function changePasswd(){
$('#erreurPasswd').show();
passwd1 = trim($('#form-passwd').val());
passwd2 = trim($('#form-passwd2').val());

if(strlen(passwd1)<6){
	$('#erreurPasswd').html("<div>Votre mot de passe doit comporter au moins 6 caractères.</div>");
}

else

if(passwd1!=passwd2){
	$('#erreurPasswd').html("<div>La confirmation est erronée.</div>");
}

else

{

$('#actions').load('change-pass.php?passwd='+encodeURIComponent(passwd1),function(){
$('#erreurPasswd').html("<div class='ok'>Mot de passe mis à jour.</div>");
$('#form-pass').slideUp(200);
$('#link-passwd').show();
$('#form-passwd').val('');
$('#form-passwd2').val('');
																																																																																																			  });
	

}
}