function genericSubmit()
{
	var container = '#ajaxHolder';
	var form      = $(container + ' form');
	var action    = form.attr('action');
	var vars      = form.serialize();
	
	$.post(action, vars,  
		function(content) {
			$(container).html(content);
		}
	);
}

function registrationStepSubmit() 
{
	genericSubmit();
}


function changeSpinner(increment) 
{
	var min = $('#spinnerMin').val();
	var max = $('#spinnerMax').val();
	var oldValue = Number($('#nr').val()); 
	if ((oldValue + increment) >= min && (oldValue + increment) <= max) {
		$('#nr').val(oldValue + increment);
	}
	
	spinnerDoneCallback();
}


function swichBebe(nrBebe) 
{
	$('.bebe').hide();
	$('#bebe'+nrBebe).fadeIn();
	$('.list2 li').removeClass('on');
	$('#tabBaby'+nrBebe).addClass('on');
}


function initDropdown(identifier) 
{
	var clsName  = identifier + '__drop';
	var clicked;

	$("." + clsName + " dt a").click(function() {
		$("." + clsName + " dd ul").toggle();
	});

	bindClickOnDropdown(identifier);

	$(document).bind('click', function(e) {
		clicked = $(e.target);
		if (!clicked.parents().hasClass(clsName)) {
			$("." + clsName + " dd ul").hide();
		};
    });
}

function disableDropdowns()
{
	$('.dropdown').each(function(){
		   $(this).find('dt a').addClass('disabled');
		   $(this).find('dt a').unbind('click');
	});
}

function enableDropdowns()
{
	$('.dropdown').each(function(){
		var a = $(this).find('dt a');
		if(a.hasClass('disabled')) {
		   a.removeClass('disabled');
		   var class_name = this.className.split(' ');
		   identifier = class_name[1].replace('__drop', '');
		   initDropdown(identifier);
		}
	});
}


function bindClickOnDropdown(identifier)
{
	var clsName  = identifier + '__drop';
	var resultId = identifier + '__val';
	var text, selValue;
	
	$("." + clsName + " dd ul li a").click(function() {
		text = $(this).html();
		$("." + clsName + " dt a span").html(text);
		$("." + clsName + " dd ul").hide();
		selValue = $("." + clsName).find("dt a span.value").html();
		$("#" + resultId).val(selValue).trigger('change');
	});
}


function changeRegFoodType(identFoodType, identBrand, identProduct) 
{
	var typeValue = $("#" + identFoodType + '__val').val();
	var itemId, itemValueId, itemTitleId, url, content;
	var brandVal, valueFound;
	
	//hide brand
	$('#' + identBrand +'__element').hide();
	$('#' + identBrand +'__title').hide();
	
	//hide product
	$('#' + identProduct +'__element').hide();
	$('#' + identProduct +'__title').hide();
	
	if (typeValue.length > 0) {
		itemId      = '#' + identBrand + '__element';
		itemValueId = '#' + identBrand + '__val';
		itemTitleId = '#' + identBrand + '__title';
		
		url = $('#url_json_brands').val() + '/type/' + typeValue;
		$.getJSON(url, function(data){
			content    = '';
			brandVal   = $(itemValueId).val();
			valueFound = false;
			var jsonResultCount = data.length;
			
			if (jsonResultCount > 0) {
				$.each(data, function() {
					if (brandVal == this.id) {
						valueFound = true;
						$("." + identBrand + "__drop dt a span").html(this.name);
					}
					content += '<li><a href="javascript:void(0);">'
						+ this.name 
						+ '<span class="value">' + this.id + '</span>'
						+ '</a></li>';
				});
			}
			$('.' + identBrand +'__drop ul').html(content);
			bindClickOnDropdown(identBrand);
			
			if (!valueFound) {
				$("." + identBrand + "__drop dt a span").html($('#trans_SELECT_FROM_LIST').val());
				$(itemValueId).val('');
			}
			
			//if there are brands found...
			if (jsonResultCount > 0) {
				$(itemId).show(); //show drop down
				$(itemTitleId).show(); //show title
				$(itemValueId).trigger('change'); //trigger dropdown on change
			}
		});
	}
}


function changeRegBrand(identFoodType, identBrand, identProduct) 
{
	var typeValue  = $("#" + identFoodType + '__val').val();
	var brandValue = $("#" + identBrand + '__val').val();
	
	//hide product
	$('#' + identProduct +'__element').hide();
	$('#' + identProduct +'__title').hide();
	
	if (brandValue.length > 0) {
		itemId      = '#' + identProduct + '__element';
		itemValueId = '#' + identProduct + '__val';
		itemTitleId = '#' + identProduct + '__title';

		url = $('#url_json_products').val() + '/brandId/' + brandValue;
		$.getJSON(url, function(data){
			content    = '';
			productVal   = $(itemValueId).val();
			valueFound = false;
			$.each(data, function() {
				if (productVal == this.id) {
					valueFound = true;
					$("." + identProduct + "__drop dt a span").html(this.name);
				}
				content += '<li><a href="javascript:void(0);">'
					+ this.name 
					+ '<span class="value">' + this.id + '</span>'
					+ '</a></li>';
			});
			$('.' + identProduct +'__drop ul').html(content);
			bindClickOnDropdown(identProduct);
						
			if (!valueFound) {
				$("." + identProduct + "__drop dt a span").html($('#trans_SELECT_FROM_LIST').val());
				$(itemValueId).val('');
			}
		});		
		
		$(itemId).show(); //show drop down
		$(itemTitleId).show().html($('#trans_what_' + typeValue).val()); // change title
		//$(itemValueId).trigger('change'); //trigger dropdown on change
	}
}


function changeTntCategory()
{
	var categValue    = $('#category__val').val();
	var subcategValue = $('#subcategory__val').val();
	
	if (categValue.length > 0) {
		url = $('#url_json_tntsubcategory_by_category').val() + '/id/' +  categValue;
		$.getJSON(url, function(data){
			content      = '';
			valueFound = false;
			$.each(data, function() {
				if (subcategValue == this.id) {
					valueFound = true;
					$(".subcategory__drop dt a span").html(this.name);
				}
				content += '<li><a href="javascript:void(0);">'
					+ this.name 
					+ '<span class="value">' + this.id + '</span>'
					+ '</a></li>';
			});
			$('.subcategory__drop ul').html(content);
			bindClickOnDropdown('subcategory');
						
			if (!valueFound) {
				$(".subcategory__drop dt a span").html($('#trans_SELECT_FROM_LIST').val());
				$('#subcategory__val').val('');
			}			
		});
	}
}


function changeTntSubcategory()
{
	var categValue    = $('#category__val').val();
	var subcategValue = $('#subcategory__val').val();
	
	document.location.href = $('#url_self').val() + '/theme/' + categValue + '/soustheme/' + subcategValue;
}


function changeSwfDepartment(code)
{
	var ajaxUrl = $('#url_ajax_region').val();

	if (arguments.length == 1) {
		document.location.hash = '#region-' + code;
	} else {
		code = document.location.hash;
	}
	
	$('#emergencyNumberHolder').load(ajaxUrl, { 'filter': code});
}


function numbersPopup(rowId)
{
	$('#emergencyPopup').hide();
	
	$('#emergencyPopup p.title').html($('#emnum_'+rowId+' #name').html());
	$('#emergencyPopup p.no').html($('#emnum_'+rowId+' #phone').html());
	$('#emergencyPopup p.desc').html($('#emnum_'+rowId+' #other').html());
	
	$('#emergencyPopup').fadeIn();
}


function openEmergencyNumberPopup()
{
	var url = $('#url_print_popup').val();
	var popwin = window.open(url,'Modilac','height=700,width=800,scrollbars=1,resizable=1,'
				+'status=0,toolbar=0,location=0,menubar=0,directories=0');
	if (window.focus) {popwin.focus(); };
}


function openProductStoresPopup()
{
	var url = $('#url_print_popup').val();
	var popwin = window.open(url,'Modilac','height=700,width=800,scrollbars=1,resizable=1,'
				+'status=0,toolbar=0,location=0,menubar=0,directories=0');
	if (window.focus) {popwin.focus(); };
}


function changeSwfProductDepartment(code)
{
	$('#departmentCode').val(code);
	selectProductInList();
	loadProductStoreAjax();
}


function changeImgProduct(id)
{
	$('#productId').val(id);
	selectProductInList();
	loadProductStoreAjax();
}


function selectProductInList()
{
	$('#jcHorizontalProducts a').removeClass('on');
	$('#prod_' + $('#productId').val()).addClass('on');
}


function initHiddenDeptProdFields()
{
	var productMatch    = document.location.hash.match(/produit\/([^\/]+)/);
	var departmentMatch = document.location.hash.match(/departement\/([^\/]+)/);
	
	if (productMatch) {
		$('#productId').val(productMatch[1]);
	}
	
	if (departmentMatch) {
		$('#departmentCode').val(departmentMatch[1])
	}
	
	selectProductInList();
}


function loadProductStoreAjax()
{
	var ajaxUrl        = $('#url_ajax_stores').val();
	var productId      = $('#productId').val();
	var departmentCode = $('#departmentCode').val()

	if (productId != 0 && departmentCode != 0) {
		document.location.hash = '/produit/' + productId + '/departement/' +  departmentCode;
	} else {
		document.location.hash = '';
	}
	
	$('#ajaxHolder').load(ajaxUrl, { 
		'productId':      productId, 
		'departmentCode': departmentCode
	});
}

function viewText(nrText) {
	$('.text').hide();
	$('#text'+nrText).fadeIn();

	$('.links a').removeClass('on');
	$('#link'+nrText).addClass('on');
}


function subBlockShow(blockId, tabId)
{
	//title
	$('.subBlocks .subBlock_of_' + tabId).removeClass('on');
	$('.subBlocks #block_title_' + blockId).addClass('on');
	
	//content
	$('.subBlocksContent .subBlock_content_of_' + tabId).hide();
	$('.subBlocksContent #block_' + blockId).fadeIn();
}


function dueDate()
{
	var dd  = $('#fday').val();
	var mm  = $('#fmonth').val();
	var yy  = $('#fyear').val();
	var url = $('#url_json_due_date').val();
	var text = '';

	$.getJSON(url, {dd :dd, mm: mm, yy: yy}, function(data){
		if (data.valid) {
			
			text = '<span class="fffTitle">' 
				+ $('#trans_due_date').val() 
				+ ' : </span> '
				+ ' <span class="fffDate">'
				+ $('#trans_the_m').val()
				+ ' ' + data.date
				+ '</span>';
			
			$('#fffResult').html(text);
			$('#fday').removeClass('genericError');
			$('#fmonth').removeClass('genericError');
			$('#fyear').removeClass('genericError');
			$('#nextLink').show();
		} else {
			$('#fday').addClass('genericError');
			$('#fmonth').addClass('genericError');
			$('#fyear').addClass('genericError');
		}
	});
		
}

function getImc()
{
	var weight = $('#weight').val();
	var height = $('#height').val();
	var url    = $('#url_json_imc').val();
	var text   = '';
	
	$.getJSON(url, {weight: weight, height: height}, function(data){
		if (data.valid) {
			
			text = '<span class="fffTitle">' 
				+ $('#trans_imc').val() 
				+ ' : </span> '
				+ ' <span class="fffDate">'
				+ ' ' + data.imc
				+ '</span>';
			
			$('#fffResult').html(text);
			$('#weight').removeClass('genericError');
			$('#height').removeClass('genericError');
		} else {
			$('#weight').addClass('genericError');
			$('#height').addClass('genericError');
		}
	});	
}

//do not use, only purpose is to fix a broken swf
function popup(notUsed, width, height)
{
	var popwin = window.open('/res/trousseau-maternite/index.html','Modilac','height='+height+',width='+width+',scrollbars=1,resizable=1,'
			+'status=0,toolbar=0,location=0,menubar=0,directories=0');
	if (window.focus) {popwin.focus(); };
}
