function serverDate () {
    if (arguments.length > 0) {
        // set from string
        serverDate.date = new Date();
        serverDate.date.setTime(Date.parse(arguments[0]));
    }
    return serverDate.date;
}

function setBudgetOptions () {
    var active = $("#block3-unit").val();
    var hide = [];
    var show = [];
    var show_every = false;
    var budget_disabled = true;

    switch (active) {
      case 'hour':
        show = ['option-hour'];
        hide = ['option-surface'];
        show_every = true;
        break;
      case 'surface':
        show = ['option-surface'];
        hide = ['option-hour'];
        break;
      default:
        budget_disabled = false;
        hide = ['option-hour', 'option-surface'];
    }
    for (var i=0; i < hide.length; i++)
      $('#' + hide[i]).hide();
    for (var i=0; i < show.length; i++)
      $('#' + show[i]).show();

    if (show_every) {
      $('#block3-id_frequency').show();
      $('label[for="block3-id_frequency"]').show();
    }
    else {
      $('#block3-id_frequency').hide();
      $('label[for="block3-id_frequency"]').hide();
    }
    $('#block3-budget').attr('disabled', budget_disabled);
}

function floatFromNumber (number) {
    if (null == number)
	return null;

    if ('undefined' != decseparator && decseparator != '.') {
	if(number.indexOf('.') > -1 && number.indexOf(decseparator) > -1) {
	    // both are used, so maybe . as thousand-separator, so it is safer to remove
	    number = number.split('.').join('');
	}
	number = number.split(decseparator).join('.'); // replace decseparator by .
    }
    return parseFloat(number);
}

function numberFromFloat (val) {
    var number = val.toString();
    if ('undefined' != decseparator && decseparator != '.') {
	number = number.split('.').join(decseparator); // replace . by decseparator
    }
    return number;
}

function recalcBudget () {
    var active = $("#block3-unit").val();
    var id_unit_price = null;
    var id_volume = null;

    switch (active) {
      case 'hour':
        id_volume = 'block3-volume-hour';
        id_unit_price = 'block3-unit_price-hour';
        break;
      case 'surface':
        id_volume = 'block3-volume-surface';
        id_unit_price = 'block3-unit_price-surface';
        break;
    }

    var volume = null;
    var unit_price = null;
    if (null != id_volume)
        volume = jQuery.trim($('#' + id_volume).val());
    if (null != id_unit_price)
        unit_price = jQuery.trim($('#' + id_unit_price).val());

    if (null != volume && null != unit_price && '' != volume && '' != unit_price) {
        volume = floatFromNumber(volume);
        unit_price = floatFromNumber(unit_price);
        if (!isNaN(volume) && !isNaN(unit_price))
            $('#block3-budget').val(numberFromFloat(volume * unit_price));
    }

}

function setDurationOptions () {
    var active = $("#block4-id_duration").val();
    if ("-1" == active) {
        $('#block4-offer_range').show();
    }
    else {
        $('#block4-offer_range').hide();
        var now = serverDate();
        var then = new Date();
        then.setTime(now.getTime() + parseInt(active) * 1000);
        $('#block4-offer_start').val(formatDateTime(now));
        $('#block4-offer_end').val(formatDateTime(then));
    }
}

function formatDateTime (date) {
    var formatTwoDigits = function(value) {
			return (value < 10 ? '0' : '') + value;
	};

    var hour = formatTwoDigits(date.getHours());
    var min = formatTwoDigits(date.getMinutes());
    return formatTwoDigits(date.getDate()) + '.' + formatTwoDigits(date.getMonth() + 1) + '.' + date.getFullYear()
    + ' ' + hour + ':' + min;
}
