function validateEmail(str){
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(!str.match(emailRegEx)){
		return true;
	}else{
		return false;
	}
}
	
function addToCart(tempprod_id){
	tempprod_id=$(tempprod_id).getParent().getParent();
	var prod_id =tempprod_id.id;
	$(prod_id).injectInside('listHolder');
	$('cart_empty_message').setStyle('display','none');
	$('checkOutButton').setStyle('display','');
	var hiddenProdID = $E('.hiddenProdID',prod_id).value;
	//alert(hiddenProdID);

	quantity = $E('.hiddenAmount',prod_id).value;
	new Ajax('/ajax_calls/addToCart.asp?prod_id='+prod_id+'&quantity='+quantity+'&variation_id='+hiddenProdID, {method: 'get'}).request();
	if($defined($('prod_cart_buttons'))){
		$('prod_cart_buttons').setStyle('display','none')
	}
	getReduction();
}

function removeFromCart(tempprod_id){
	
	tempprod_id=$(tempprod_id).getParent().getParent();
	var prod_id =tempprod_id.id;
	if($('products')==null || $E('.variation_of_ID',prod_id).value>0){
		$(prod_id).remove()
	}else{
		$(prod_id).injectInside('products');
		$E('.hiddenAmount',prod_id).value = 1;
		$E('.cartAmount',prod_id).setHTML('1');
		
	}
	
	//quantity = $E('.hiddenAmount','prod_'+prod_id).value;
	new Ajax('/ajax_calls/removeFromCart.asp?prod_id='+prod_id, {method: 'get'}).request();
	getReduction();
}

var reduction=0;
var message="";
var valid=false;
var red_type="";

var getReduction= function (){
	if($("promo_code")){
	var promo_code=$("promo_code").value;
	new Ajax("/ajax_calls/testPromoCode.asp?promo_code="+promo_code, {method:'get',onComplete: function(dd){calculatCartTotal(dd);}}).request();}
}	


function calculatCartTotal(returnPromo){
	
	var amountTotal = 0;
	var tvaTotal = 0;
	var delivery_cost=0;
	reduction_variables=eval('(' + returnPromo + ')');
	//console.log(reduction_variables.valid);
	if(reduction_variables.message){
	if(reduction_variables.message!=""){
		alert(reduction_variables.message);
		
		}}
	$ES('.prodBox','cart').each(function(el){
		priceEl = $E('.hiddenPrice',el);
		priceValue = priceEl.value;
		priceValue = priceValue.replace(/,/gi, '.');
		priceValue = parseFloat(priceValue);
		
		quantityEl = $E('.hiddenAmount',el)
		quantityValue = parseInt(quantityEl.value);
		
		tvaEl = $E('.hiddenTva',el);
		tvaValue = tvaEl.value;
		tvaValue = tvaValue.replace(/,/gi, '.');
		tvaValue = parseFloat(tvaValue);
		
		if(priceValue > 0 && quantityValue > 0 ){
			//alert(amountTotal);
			prodTotal=priceValue*quantityValue;
			amountTotal = amountTotal + prodTotal;
			if( tvaValue > 0 ){
				tvaTotal = tvaTotal + prodTotal-(prodTotal/(tvaValue+1));
			}
		}
	});
	
	if(reduction_variables.valid=='True' && parseFloat(reduction_variables.reduction)>0){
		
		var reduction=parseFloat(reduction_variables.reduction)
		if(reduction_variables.red_type=='money'){
			amountTotal=amountTotal-reduction;
			$('reduction').setHTML(reduction+'&euro;');
		}
		if(reduction_variables.red_type=='percent'){
			oldAmount=amountTotal;
			reductionEuro=amountTotal*reduction/100;
			amountTotal=oldAmount-reductionEuro;
			$('reduction').setHTML(reductionEuro+'&euro; ('+reduction+'%)');
		}
		$('reddiv').setStyle('display','');
		
	}
	
	if($defined($('delivery_cost'))){
		
		if (amountTotal > parseFloat($('delivery_limit').value)){
			delivery_cost = '0'
			$('delivery_total').setHTML('(offert) 0 &euro;');
		}else{
			
			delivery_cost = $('delivery_cost').value;	
			$('delivery_total').setHTML(delivery_cost+'&euro;');
		}
		
		delivery_cost = delivery_cost.replace(/,/gi, '.');
		delivery_cost = parseFloat(delivery_cost);
		
		amountTotal = amountTotal+delivery_cost;
		tvaTotal = tvaTotal + (delivery_cost-(delivery_cost/1.21))
	}
	
	


	
	amountTotal = amountTotal.toFixed(2);
	amountTotal = amountTotal.replace(/\./gi, ',');
	$('cart_total').setHTML(amountTotal+' &euro;');
	$('cart_total_hidden').value=amountTotal;
	
	tvaTotal = tvaTotal.toFixed(2);
	tvaTotal = tvaTotal.replace(/\./gi, ',');
	if($defined($('tva_total'))){
		$('tva_total').setHTML(tvaTotal+' &euro;');
	}
}

function addProdCount(tempprod_id,max_count){
	tempprod_id=$(tempprod_id).getParent().getParent().getParent()
	var prod_id =tempprod_id.id;
	var oldValue=$E('.hiddenAmount',prod_id).value;
	if(parseInt(oldValue)<max_count){
		newValue=parseInt(oldValue)+1;
		$E('.hiddenAmount',prod_id).value = newValue;
		$E('.cartAmount',prod_id).setHTML(newValue);
		if($(prod_id).getParent().id=='listHolder'){
			new Ajax('/ajax_calls/updateToCart.asp?prod_id='+prod_id+'&quantity='+newValue, {method: 'get'}).request();
		}
		getReduction();
	}
}

function subtProdCount(tempprod_id){
	tempprod_id=$(tempprod_id).getParent().getParent().getParent()
	var prod_id =tempprod_id.id;
	var oldValue2=$E('.hiddenAmount',prod_id).value;
	if(oldValue2>0){
		newValue2=parseInt(oldValue2)-1;
		$E('.hiddenAmount',prod_id).value = newValue2;
		$E('.cartAmount',prod_id).setHTML($E('.hiddenAmount',prod_id).value);
		if($(prod_id).getParent().id=='listHolder'){
			new Ajax('/ajax_calls/updateToCart.asp?prod_id='+prod_id+'&quantity='+newValue2, {method: 'get'}).request();
		}
	}
	
	getReduction();
}
function open_sub_sub_cat(element_id){
	$ES('.sub_sub_cats').each(function(el){
		el.setStyle('display','none')
	});
	var pos=$('parentLink_'+element_id).getPosition();
	posx=parseInt(pos.x);
	$('sub_sub_cat_'+element_id).setStyle('left',posx-parseInt(posx/2));
	$('sub_sub_cat_'+element_id).setStyle('display','');
}

function send_newsletter(){
	$('newsletter_form').send({update: $('newsletter_holder')})
}