﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />


function initDateControl(aleid) { 
    $(function()
        { $("[aleID='"+aleid+"']").datepicker(
                {
                    changeMonth: true,
                    changeYear: true, 
                    showOn: 'button', 
                    buttonImage: 'images/calendar.gif', 
                    buttonImageOnly: true, 
                    constrainInput: true
           });
          $("[aleID='"+aleid+"']").datepicker('option',$.datepicker.regional['it']);
    });
}

function initSimpleDateControls() {
    $(function() {
        $("[aleSimpleData]").blur(function() {
            var d;
            d = new Date(this.value);
            this.value = d.toLocaleDateString();
        });
    });
}

function initAleData() {
    $(function() {
        /*
        $("[aleData]").datepicker(
        {
        clearText: "dd/mm/yyyy"
        , changeMonth: true
        , changeYear: true
        });
        */

        //$("[aleData]").datepicker('option', $.datepicker.regional['it']);

        $(".aleData").blur(function() {
            var d;
            /*
            d = new Date(this.value);
            if (d == "NaN") {
            this.style.backgroundColor = "red";
            this.focus();
                
            } else {
            this.style.backgroundColor = "white";   
            }
            */

            try {
                d = $.datepicker.parseDate('dd/mm/yy', this.value);
                //this.value = $datePicker('dd/mm/yyyy', d);
                if (d < (new Date("1/1/1900")) || d > (new Date("6/6/2079"))) {
                    this.style.backgroundColor = "red";
                    this.focus();
                } else {
                    this.style.backgroundColor = "white";
                }
            }
            catch (e) {
                this.style.backgroundColor = "red";
                this.focus();
            }
        });

        $(".aleData").keypress(function() {
            if (event.keyCode < 47 || event.keyCode > 57) {
                event.keyCode = 0;
            }
            else {
                if (event.keyCode == 47) {
                    if (this.value.length == 1) {
                        this.value = "0" + this.value;
                    } else if (this.value.length == 2) {

                    } else if (this.value.length == 4) {
                        this.value = this.value.substr(0, this.value.length - 1) + "0" + this.value.charAt(this.value.length - 1);
                    } else if (this.value.length == 5) {

                    } else {
                        event.keyCode = 0;
                    }
                }
                else {
                    if (this.value.length == 2) {
                        this.value += "/";
                    }
                    else if (this.value.length == 5) {
                        this.value += "/";
                    }
                    else if (this.value.length > 9) {
                        var selText = (document.all) ? document.selection.createRange().text : document.getSelection();
                        if (selText.length == 0) {
                            // make sure the field doesn't exceed the maximum length
                            event.keyCode = 0;
                        }
                    }
                }
            }
        });
    });
}

$(function() {
    initSimpleDateControls();
    initAleData();
});

