// JavaScript Document


/*---------- Home ---------*/
function redir(url){
	location.href = url;
}

function show_cons(dir){
	$('#cons div').hide();
	$('#' + dir).show();
}

function wopen(link_to,nom){
	 window.open(link_to, '', config="menubar=no, status=no, scrollbars=yes, menubar=no, width=700, height=600");	
}

function add_to_cart_promo(str){
		a = str.split('/');
		for( i=0 ; i < a.length ; i++){
				d = a[i].split('-');			
					var qte = d[1];
					$.ajax({
						type: "GET",
						url: "/ajax/cart.php",
						data: "md=add&id=" + d[0] + "&qte=" + d[1]
						});		
		}
		location.reload();	
}

function check_code_promo(value){
	if( value !== '' ){
	$('#rslt_code_p').html('');
	if( value.length > 5 ){
		$.ajax({
			type: "GET",
			url: "/ajax/divers.php",
			data: "md=check_the_code_promo&code=" + value,
			success: function(rslt){
					if( rslt == '3' ) refresh_cart();
					else $('#rslt_code_p').html('<br /><span class="colorRed">' + rslt + '</span>');
				}
			});	
	}else $('#rslt_code_p').html('<br /><span class="colorRed">Code invalide</span>');
	
	}
}

function favoris() {
if ( navigator.appName != 'Microsoft Internet Explorer' )
{ window.sidebar.addPanel("Wine Apart :: Redécouvrez le plaisir du vin !","http://www.wineapart.com",""); }
else { window.external.AddFavorite("http://www.wineapart.com","Wine Apart :: Redécouvrez le plaisir du vin !"); } }

function check_this_mrq(){
	if(document.search2.nom.value == ''){
		var value = document.search2.marque.value;
		location.href = '/M-' + value + '/';
	}
	else document.search2.submit();
}

function show_me_prod(id){
	$("#show_last_prod_app > .thumb_last").css( "border-width", '1px' );
	$("#thumb" + id).css( "border-width", '3px' );
	
	$("#fi").fadeOut("slow", function () {
	$.ajax({
		type: "GET",
		url: "/ajax/divers.php",
		data: "md=home_get&id=" + id,
		success: function(rslt){
			$('#fi').html(rslt);
			$("#fi").fadeIn("slow");
			}
		});	
	});
}

function go_pack(id){
	clearTimeout(timerr2);
	$("#num_cont .cube").css("background-color","#e1e2e4");
	$("#cube" + id).css("background-color","#917f6f");
	
	$("#window_pack").hide("slide", { direction: "left" }, 500,
		function () {
		$.ajax({
			type: "GET",
			url: "/ajax/pack.php",
			data: "id=" + id,
			success: function(rslt){
				$("#window_pack").html(rslt);
				$("#window_pack").show("slide", { direction: "right" }, 500);

			}
		});	
	});
	
	
}

/*---- Livr ---------*/
function chg_livr(){
	var name= '';
	$("#addr_livr :input").each(function(i){
		name += (this.name + '=' + this.value + '&' );
	});
	
	$.ajax({
		type: "POST",
		url: "/ajax/divers.php",
		data: "md=chg_livr&" + name,
		success: function(rslt){
			$('#livr').html(rslt);
			show_descr('chg_addr_livr');
			//window.location.reload();
			}
		});			
}
function go_to_valid_cart(prix, nom){
	$.ajax({
		type: "GET",
		url: "/ajax/divers.php",
		data: "md=go_to_valid&fdp=" + prix + "&nom=" + nom,
		success: function(){
			location.href='/Panier/Validation/';
			}
		});		
}


function chg_coord(etape){
	if(etape == 1){
		$.ajax({
			type: "GET",
			url: "/ajax/divers.php",
			data: "md=chg_coord1",
			success: function(rslt){
			$('#chg_coord').html(rslt);
			show_descr2('chg_coord');
			}
		});	
	}else if(etape == 2){
		var name= '';
		$("#form_coord :text").each(function(i){
			name += (this.name + '=' + this.value + '&' );
		});
		$("#form_coord :hidden").each(function(i){
			name += (this.name + '=' + this.value + '&' );
		});
			$.ajax({
				type: "POST",
				url: "/ajax/divers.php",
				data: "md=chg_coord2&" + name,
				success: function(rslt){
					$('#fact').html(rslt);
					show_descr('chg_coord');
					}
				});	
	}
	
}



/*---- FORMU REG ----*/
function check_mail2(value){
	if( check('mail', value) )
	$.ajax({ type: "GET", url: "/ajax/divers.php", data: "md=upd_user&value=" + value });	
}
function check(cat, value){
	if(cat == 'mail'){
		var verif_mail = 0;
		var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/;
			if(reg.exec(value)){
				$.ajax({
					type: "GET",
					url: "/ajax/divers.php",
					data: "md=check_email&email=" + value,
					success: function(rslt){
							if(rslt == '1'){ verif_mail = 1; $('#valid_email').html('<img src="/template/images/oki.png" alt="" />');  } 
							else{ $('#valid_email').html('<img src="/template/images/none.jpg" alt="" /> <span style="color:#FF0000; font-size:9px;">Cet email est dejà enregistré. Si vous avez perdu vos identifiants, cliquez ici.</span>');  }
						}
					});			
			}else{ $('#valid_email').html('<img src="/template/images/none.jpg" alt="" /> <span style="color:#FF0000; font-size:9px;">Votre email n\'est pas valide.</span>');}
		
		if(verif_mail == 1) return true;
		else return false;
	}else if(cat == 'pass'){
		if(isset(document.forms["new_client"].elements["password"].value) && isset(document.forms["new_client"].elements["passverify"].value) && document.forms["new_client"].elements["password"].value == document.forms["new_client"].elements["passverify"].value) return true;
		else return false;
	
	}	
}

function check_form(){
		var verif_form= 1;					
		
		if(!check('pass', 0)){
			$('#valid_pass').html('<img src="/template/images/none.jpg" alt="" /> <span style="color:#FF0000; font-size:9px;">Les deux mots de passe ne sont pas identiques</span>');
			verif_form = 0; }
		
		var htm_false = '<img src="/template/images/none.jpg" alt="" /> <span style="color:#FF0000; font-size:9px;">Obligatoire</span>'
		var reg=new RegExp("(chien)", "g");
		if(!isset(document.forms["new_client"].elements["nom"].value)){
			$('#valid_nom').html(htm_false);	 verif_form = 0;
			}else $('#valid_nom').html('');	
		if(!isset(document.forms["new_client"].elements["prenom"].value)){
			$('#valid_prenom').html(htm_false);	 verif_form = 0;
			}else $('#valid_prenom').html('');		
		if(!isset(document.forms["new_client"].elements["adresse"].value)){
			$('#valid_adr').html(htm_false);	 verif_form = 0; 
			}else $('#valid_adr').html('');		
		if(!isset(document.forms["new_client"].elements["cp"].value)){ 
			$('#valid_cp').html(htm_false);		 verif_form = 0; 
			}else if( document.forms["new_client"].elements["cp"].value.search(/20.+/) !== -1 ){
			$('#valid_cp').html('<img src="/template/images/none.jpg" alt="" /> <span style="color:#FF0000; font-size:9px;">Pour une livraison pour la Corse, nous contacter.</span>');		 verif_form = 0; 
			}else $('#valid_cp').html('');	
		if(!isset(document.forms["new_client"].elements["ville"].value)){ 
			$('#valid_ville').html(htm_false);	 verif_form = 0; 
			}else $('#valid_ville').html('');		
		if(!isset(document.forms["new_client"].elements["pays"].value)){
			$('#valid_pays').html(htm_false);	 verif_form = 0; 
			}else $('#valid_pays').html('');	
		if(!isset(document.forms["new_client"].elements["tel"].value)){
			$('#valid_tel').html(htm_false);	 verif_form = 0;
			}else $('#valid_tel').html('');	
		
			if(verif_form == 1) document.forms["new_client"].submit();
}

function check_form_2(){
		var verif_form= 1;					
		var htm_false = '<img src="/template/images/none.jpg" alt="" /> <span style="color:#FF0000; font-size:9px;">Obligatoire</span>'
		var reg=new RegExp("(chien)", "g");
		if(!isset(document.forms["old_client"].elements["nom"].value)){
			$('#valid_nom').html(htm_false);	 verif_form = 0;
			}else $('#valid_nom').html('');	
		if(!isset(document.forms["old_client"].elements["prenom"].value)){
			$('#valid_prenom').html(htm_false);	 verif_form = 0;
			}else $('#valid_prenom').html('');		
		if(!isset(document.forms["old_client"].elements["adresse"].value)){
			$('#valid_adr').html(htm_false);	 verif_form = 0; 
			}else $('#valid_adr').html('');		
		if(!isset(document.forms["old_client"].elements["cp"].value)){ 
			$('#valid_cp').html(htm_false);		 verif_form = 0; 
			}else if( document.forms["old_client"].elements["cp"].value.search(/20.+/) !== -1 ){
			$('#valid_cp').html('<img src="/template/images/none.jpg" alt="" /> <span style="color:#FF0000; font-size:9px;">Pour une livraison pour la Corse, nous contacter.</span>');		 verif_form = 0; 
			}else $('#valid_cp').html('');	
		if(!isset(document.forms["old_client"].elements["ville"].value)){ 
			$('#valid_ville').html(htm_false);	 verif_form = 0; 
			}else $('#valid_ville').html('');		
		if(!isset(document.forms["old_client"].elements["pays"].value)){
			$('#valid_pays').html(htm_false);	 verif_form = 0; 
			}else $('#valid_pays').html('');	
		if(!isset(document.forms["old_client"].elements["tel"].value)){
			$('#valid_tel').html(htm_false);	 verif_form = 0;
			}else $('#valid_tel').html('');	
		
			if(verif_form == 1) document.forms["old_client"].submit();
}

/*---- PROD ----*/

function show_descr(id){
	  $("#p" + id).slideToggle("slow");
}
function show_descr2(id){
	  $("#" + id).slideToggle("slow");
}

function close_shop(){
	$("#shop").hide();
	location.reload();
}

/*---- CART ----*/
function add_to_cat_fp(idprod){
	var qte = document.forms["add_to_cart"].elements["qte"].value;
	$.ajax({
   		type: "GET",
   		url: "/ajax/cart.php",
   		data: "md=add&id=" + idprod + "&qte=" + qte,
		success: function(rslt){
			if (typeof document.body.style.maxHeight == "undefined") {
 				location.reload();
			}
			$("#shop").show();
			$('#shop_prod').html(rslt);
			}
 		});		
}

function add_to_cat_fp_pack(idprod, nom){
	var qte = document.forms["add_to_cart_p"].elements["qte"].value;
	$.ajax({
   		type: "GET",
   		url: "/ajax/cart.php",
   		data: "md=add&id=" + idprod + "&qte=" + qte + "&nom=" + nom,
		success: function(rslt){
			if (typeof document.body.style.maxHeight == "undefined") {
 				location.reload();
			}
			$("#shop").show();
			$('#shop_prod').html(rslt);
			}
 		});		
}



function add_to_cat_fh(idprod){
	var qte = document.forms["add_to_cart" + idprod].elements["qte"].value;
	$.ajax({
   		type: "GET",
   		url: "/ajax/cart.php",
   		data: "md=add&id=" + idprod + "&qte=" + qte,
		success: function(rslt){
			if (typeof document.body.style.maxHeight == "undefined") {
 				location.reload();
			}
			$("#shop").show();
			$('#shop_prod').html(rslt);
			}
 		});		
}

function update_cart_qte(qte,id){
	qte = parseInt(qte);
	document.forms["form_cart"].elements["qte"].value = qte;
	
	$.ajax({
   		type: "GET",
   		url: "/ajax/cart.php",
   		data: "md=chgqte&id=" + id + "&qte=" + qte,
		success: function(rslt){
			refresh_cart();
			}
 		});		
}

function suppr_cart_prod(id,nom){
	if(confirm('Etes vous sûr de vouloir supprimer le produit ' + nom + ' ?')){
		$.ajax({
			type: "GET",
			url: "/ajax/cart.php",
			data: "md=suppr&id=" + id,
			success: function(rslt){
				refresh_cart();
				}
			});	
	}
}


function refresh_cart(){
	$.ajax({
   		type: "GET",
   		url: "/ajax/cart.php",
   		data: "md=list",
		success: function(rslt){
			$('#cart').html(rslt);
			}
 		})
	
}
/*---- END CART ----*/


  function isset(variable){
  if ( variable.length > 0 ) {
       return true;
     }
  else {
       return false;
      }
   }

	function verif_cgv(zo){
		if(zo == '0'){
			if(document.forms['cgv'].elements['CGV'].checked == true){
				/*if (typeof document.body.style.maxHeight == "undefined") {
					document.write('redirection');
				}*/
				document.location.href = "http://wineapart.com/Panier/EndB/";
			}else{
			$('#cgv_check').html('Vous devez accepter les CGV');	
			}	
		}
		else if(zo == '1'){
			if(document.forms['cgv'].elements['CGV'].checked == true){
				/*if (typeof document.body.style.maxHeight == "undefined") {
					document.write('redirection');
				}*/
				document.location.href = "http://wineapart.com/Panier/EndC/";
			}else{
			$('#cgv_check').html('Vous devez accepter les CGV');	
			}
		}
	}


		function getRotate_promo(j){
			
			//jQuery("div#stripTransmitter" + j + " a").each(function(z) {
			//jQuery(this).bind("click", function(){
				jQuery("div#stripTransmitter0 ul li").find("a").removeClass("current"); // wow!
				jQuery("div#stripTransmitter0 ul li:nth-child(" + (j+1) + ")").find("a").addClass("current"); // wow!
				var cnt = - (200*j);
				jQuery("#mygalone").find("ul").animate({ left: cnt}, 750, "easeInOutExpo");
			//return false;
			// });
			//});
				//document.write(j);
				j=j+1;
				if(j > 7) j = 0;
				timer = setTimeout("getRotate_promo(" + j + ")",5000);
			}
			
		function getRotate_pack(j){
				j=j+1;
				if(j > $('.cube').length  ) j = 1;
					$("#num_cont .cube").css("background-color","#e1e2e4");
					
					
					var idcube = $('#num_cont :nth-child(' + j + ')').attr("id");
					//document.write(idcube);
					
					$('#' + idcube).css("background-color","#917f6f");
					
					
					var idcube = new String(idcube); 
					var idc = idcube.replace("cube", ""); 

					
					$("#window_pack").hide("slide", { direction: "left" }, 500,
						function () {
						$.ajax({
							type: "GET",
							url: "/ajax/pack.php",
							data: "id=" + idc,
							success: function(rslt){
								$("#window_pack").html(rslt);
								$("#window_pack").show("slide", { direction: "right" }, 500);
				
							}
						});	
					});
				timerr2 = setTimeout("getRotate_pack(" + j + ")",7000);
			}
			
			
		function stopcount(){
			clearTimeout(timer);	
		}
			
/*----- FUNCTION -----*/
    $(window).load(function () {
		getRotate_promo(0);
		getRotate_pack(0);

    });

$(document).ready(function() 
    { 
		
	    if( $("#coordz") ){
		    $("#coordz :text").each( function(){
			$(this).change(function () {
				$.ajax({
					type: "GET",
					url: "/ajax/divers.php",
					data: "md=upd_coord&name=" + this.name + "&value=" + this.value
				});
			});
		    });
	    }
	    
	    
		$(":checkbox, :radio").css("border","0"); 
		
	
		var hright = $('#content_right').height() + 300;
		if( hright > $('#content_left').height() ){
			var browserName=navigator.appName; 	
			//document.write(browserName);
			if (browserName !== "Microsoft Internet Explorer")
			 {
				var h2 = $('#content_right').height() + 500;
				$('#content_left').css('height', h2 + 'px');	
			}else{
				var h2 = $('#content_right').height() + 500;
				$('#content_left').css('height', h2 + 'px');
			}
		}
	 /*else{
			var h2 = $('#content_left').height() + 100;
			$('#content_left').css('height', h2 + 'px');
		}*/
		
		/*if( ($("#content_right").height() + 200) > $("#content").height() ){
			var h = $("#content_right").height() + 260;	
			$("#content").css("min-height", h + "px" )
		}*/
	
		$("#num_cont :first").css("background-color","#917f6f");
	
		$("div#mygalone").slideView()
		
        $("#table_prod")
			.tablesorter({widthFixed: true, headers: { 
            0: { sorter: false }, 3: { sorter: false },	4: { sorter: false }  } })
			.tablesorterPager({container: $("#pager, #pager2")});
			
		if( document.getElementById("table_cat") && document.getElementById('table_marque') ){
			$("#spacer").after('<hr />');
			if( $("#table_cat").height() >= $("#table_marque").height() ){
				var height = $("#table_cat").height();
				height = height+20;
				$("#spacer").css("height", height);
			}else{
				var height = $("#table_marque").height();
				height = height+20;
				$("#spacer").css("height", height);
			}
		}else if( document.getElementById('table_marque') ){
			$("#spacer").after('<hr />');
			var height = $("#table_marque").height();
				height = height+20;
				$("#spacer").css("height", height);			
		}

		// Auto rotation
		
	}); 
