$(window).load(function() {
	$('.focus').focus();
	
	$.datepicker.setDefaults({
		dateFormat: 'dd/mm/yy',
		firstDay: 1,
		dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
		monthNames: ['Janvier','F&eacute;vrier','Mars','Avril','Mai','Juin','Juillet','Ao&ucirc;t','Septembre','Octobre','Novembre','D&eacute;cembre']
	});
	$('#dateStartFormated').datepicker({
		onSelect: function(dateText) {
			var dd = dateText.substring(0,2);
			var mm = dateText.substring(3,5);
			var yy = dateText.substring(6,10);
			var dateIso = yy + '-' + mm + '-' + dd; 

			$('#dateStart').val(dateIso);
			$('#dateStopFormated').val(dateText);
			$('#dateStop').val(dateIso);
		}
	});
	$('#dateStopFormated').datepicker({
		onSelect: function(dateText) {
			var dd = dateText.substring(0,2);
			var mm = dateText.substring(3,5);
			var yy = dateText.substring(6,10);
			var dateIso = yy + '-' + mm + '-' + dd; 

			$('#dateStop').val(dateIso);
		}
	});
});

function redirect(page) {
	window.location.replace(page);
}

function valid(elements) {
	if(elements.length > 0) {
		for (var i=0; i < elements.length; i++) {
			if ($(elements[i]).val() == '') {
				$(elements[i]).addClass('error');
				
				var result = false;
			}
			else {
				$(elements[i]).removeClass('error');
				
				if (result != false) {
					var result = true;
				}
			}
		}
		
		if (!result) {
			$('#error').html('Veuillez compl&eacute;ter les champs entour&eacute;s.');
			$('#error').slideDown(function (){
				$('#error').effect('pulsate', { times: 1 });
			});
		}
	}
	else {
		var result = true;
	}
	
	return result;
}

function ajaxRequest(fields, url, formId, theData, responseUrl) {
	if (valid(fields)) {
		if (formId == '') {
			var data = theData;
		}
		else {
			var data = $(formId).serialize();		
		}
		$('#error').slideUp();
		$('#loader').slideDown(function () {
    		$.post(
    			url,
    			data,
    			function(msg){
    				if (msg == 'ok') {
			    		redirect(responseUrl);
		    		}
		    		else {
	    				$('#loader').slideUp();
	    				if (msg != '') {
		    				showError(msg);
	    				}
	    				if (theData == 'clearForm') {
		    				clearForm(formId);
	    				}
	    			}
    			}
    		);
    	});
	}
}

function ajaxUpdater(elementId, theUrl, theData) {
	$('#loader').slideDown(function () {
		$(elementId).load(theUrl, theData, function(){
			$('#loader').slideUp();
		});
	});
}

function clearForm(form) {
  // iterate over all of the inputs for the form
  // element that was passed in
  $(':input', form).each(function() {
    var type = this.type;
    var tag = this.tagName.toLowerCase(); // normalize case
    // it's ok to reset the value attr of text inputs,
    // password inputs, and textareas
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = "";
    // checkboxes and radios need to have their checked state cleared
    // but should *not* have their 'value' changed
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    // select elements need to have their 'selectedIndex' property set to -1
    // (this works for both single and multiple select elements)
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};

function showError(theError) {
	$('#error').html(theError);
	$('#error').slideDown(function (){
		$('#error').effect('pulsate', { times: 1 });
	});
}

function getDatePicker(id) {
	$(id).datepicker('show');
}

function delImage() {
	$('#thumbnails').html('').hide();
	$('#img').attr('value', '');
	$('#delete').hide();
}