javascript function for set periodicity in the week as in google calendar
<p>I am trying to write a generic javascript subroutine to set the periodicity in the week:</p>

<pre><code>var items = {"0": "none", "1":"Daily", "2": "Every weekday (Monday to Friday)", "3": "Weekly"};
</code></pre>

<p>I wrote the following module, but I would like to improve it, making it more generic and readable if it is possible.</p>

<p>So my questions are:<br>
1) How can I ameliorate this piece of code?<br>
2) How can make a callback in order to make something like this: </p>

<pre><code>var callback = selectDay(idValue);​
createSelect(items, idValue, callback);
</code></pre>

<hr>

<pre><code>var createSelect = function (items, idValue) {
    var selElem = document.createElement("select");
    $.each(items, function (key, value) {
        var ov = document.createElement("option");
        ov.value = key;
        ov.appendChild(document.createTextNode(value));
        selElem.appendChild(ov);
    });
    $(idValue).prepend(selElem);
};
</code></pre>

<hr>

<pre><code>var selectDay = function (idValue, element) {
    var element = $(idValue);
    var childElements = idValue + '_';
    element.find('select').change(function () {
        if($(this).val() === '0') {
            element.find('label').hide();
            $(this).closest('div').find('input').attr("checked", false);
        }
        if($(this).val() === '1') {
            element.find('label').hide();
            $(this).closest('div').find('input').attr("checked", true);
        }
        if($(this).val() === '2') {
            element.find('label').show();
            for (var i = 0; i &lt; 5; i += 1) {
                $(childElements + i).attr("checked", true);
            }
            for (var i = 5; i &lt; 7; i += 1) {
                $(childElements + i).attr("checked", false);
            }
        }
        if($(this).val() === '3') {
            element.find('label').show();
            $(this).closest('div').find('input').attr("checked", false);
            $(childElements + (new Date()).getDay()).attr("checked", true);
        }
    });
};
</code></pre>

<hr>

<pre><code>var items = {"0": "none", "1":"Daily", "2": "Every weekday (Monday to Friday)", "3": "Weekly"};
var idValue = '#contest_data_updatePeriodicity_days';
createSelect(items, idValue);
selectDay(idValue);​
</code></pre>

<p>Here is my demo <a href="http://jsfiddle.net/D2RLR/2634/" rel="nofollow">http://jsfiddle.net/D2RLR/2634/</a></p>
