/**
 * Librairie javascript commune a tout l'ecog
 *
 * @author			Julie Tetard <julie@2jstudio.com> 
 * @version		E-cog 3.0
 * @package			public
 * @subpackage		param
 * @category		js
 * @internal		last modified
 */



/**
 * Attache Evenements
 */
$(document).ready(function () {
	
	var lang = $('meta[name=language]').attr('content');
	
	if ($('#detail-photos a').length > 0) {
		$('#detail-photos a').lightBox({
			imageBtnClose: 'param/pix/lightbox/btn-close.gif',
			imageBtnPrev: 'param/pix/lightbox/btn-prev.gif',
			imageBtnNext: 'param/pix/lightbox/btn-next.gif',
			imageLoading: 'param/pix/lightbox/loading.gif',
			containerResizeSpeed:800,
			overlayBgColor: '#471F1F'
		});
		
	}
	
	$('#cart-form select').change(
		function () {
			// On recalcule le total de la ligne
			var unitPrice = cleanNum($(this).parent().prev().text());
			var qtyPrice = unitPrice * $(this).val();
			$(this).parent().next().text(formatNum(qtyPrice.toFixed(2))); 
			// On recalcule le total final
			var total = 0;
			$('#cart-form table tr').find('td:last').each(
				function () {					
					var unitPrice = cleanNum($(this).text());
					total = total + parseFloat(unitPrice);					
				}				
			);	
			
			$('.cTotal').text(formatNum(total.toFixed(2)));
		}
	);
	
	$('#billing-toggle').mouseup(function() {
		if ($(this).attr('checked') === true) {
			$('#billing-information').show();
		} else {
			$('#billing-information').hide();
		}
	});
	
	if ($('#shipping').length > 0) {
		$('#next,#previous').click(
			function() {
				clicked = $(this).attr('id');
			}
		);
		$('#cart-form').submit(
			function() {
				if (clicked == 'next') {
					var wrong = false;
					var all_empty = false;
					// Shipping
					$('#shipping .inputText,#shipping .inputTextarea').each(function(){
						if ($(this).attr('id') != 'shipping-state' && $(this).val() == '') {
							$(this).addClass('e_warn');
							if (wrong === false) {
								$(this).focus();
							}
							wrong = true;
							all_empty = true;
						}
						else {
							$(this).removeClass('e_warn');
						}
					});
					if (!validation($('#shipping-email').val())) {
						$('#shipping-email').addClass('e_warn');
						if (wrong === false) {
							$('#shipping-email').focus();
						}
						wrong = true;
						if (lang == 'fr') {
							alert('Le format de votre email de livraison n\'est pas valide.');
						} else {
							alert('Please enter a valid shipping email.');
						}
					} else {
						$('#shipping-email').removeClass('e_warn');
					}
					if ($('#shipping-email').val() != $('#shipping-email2').val()) {
						$('#shipping-email2').addClass('e_warn');
						if (wrong === false) {
							$('#shipping-email2').focus();
						}
						wrong = true;
						if (lang == 'fr') {
							alert('Vous avez confirmé un email de livraison différent du premier.');
						} else {
							alert('The two shipping emails are different.');
						}
						
					} else {
						$('#shipping-email2').removeClass('e_warn');
					}
					// Billing
					if ($('#billing-toggle').not(':checked').length > 0) {
						$('#billing .inputText,#billing .inputTextarea').each(function(){
							if ($(this).attr('id') != 'billing-state' && $(this).val() == '') {
								$(this).addClass('e_warn');
								if (wrong === false) {
									$(this).focus();
								}
								wrong = true;
								all_empty = true;
							}
							else {
								$(this).removeClass('e_warn');
							}
						});						
						if (!validation($('#billing-email').val())) {
							$(this).addClass('e_warn');
							if (wrong === false) {
								$(this).focus();
							}
							wrong = true;
							if (lang == 'fr') {
								alert('Le format de votre email de facturation n\'est pas valide.');
							} else {
								alert('Please enter a valid billing email.');
							}
						}
						else {
							$(this).removeClass('e_warn');
						}
						if ($('#billing-email').val() != $('#billing-email2').val()) {
							$('#billing-email2').addClass('e_warn');
							if (wrong === false) {
								$('#billing-email2').focus();
							}
							wrong = true;
							if (lang == 'fr') {
								alert('Vous avez confirmé un email de facturation différent du premier.');
							} else {
								alert('The two billing emails are different.');
							}
						} else {
							$('#billing-email2').removeClass('e_warn');
						}
					}
					if (all_empty === true) {
						if (lang == 'fr') {
							alert('Tous les champs en rouge sont obligatoires.');
						} else {
							alert('Highlighted fields required.');
						}
					}						
					// If false
					if (wrong === true) {
						return false;
					} else {
						return true;
					}
				}				
			}
		);
	}
	
	if ($('#check-terms').length > 0) {
		$('#next,#previous,#no-country').click(
			function() {
				clicked = $(this).attr('id');
			}
		);
		$('#cart-form').submit(
			function(){
				if ($('#check-terms').not(':checked').length > 0) {
					if (clicked == 'next') {
						if (lang == 'fr') {
							alert('Vous devez accepter les conditions de vente d\'Oggi pour valider votre panier.');
						} else {
							alert('You must accept Oggi Terms & Conditions to proceed to checkout.');
						}						
						return false;
					}
				}
			} 
		);
	}
});

function cleanNum(number) {
	var allowedH = ['0','1','2','3','4','5','6','7','8','9','.'];
	var newnumber = '';
	for (i = 0 ; i < number.length ; i++) {
		for (j = 0; j < allowedH.length; j++) {
			if (number.substr(i, 1) == allowedH[j]) {
				newnumber += number.substr(i, 1);
				break;
			}
		}
	}
	return newnumber;
}

function formatNum(number) {
	var numberH = number.toString().split('.');
	var newnumber = numberH[0]; 
	var newnewnumber = '';
	for (i = 1 ; i <= newnumber.length ; i++) {
		var j = newnumber.length - i;
		newnewnumber = (i / 3 == Math.round(i / 3) ? ' ' : '') + newnumber.substr(j, 1) + newnewnumber;
	}
	return newnewnumber + number.substr(number.length - 3) + ' \u20AC';
}

function validation(email)
{
	var verif = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
 	 if (verif.exec(email) == null)	{
		return false;
	} else	{
		return true;
	}
} 
