﻿// File JScript
function glnud_onKeyDown (ctrl, event)
{
    var numeric = GetAttribute (ctrl, "numeric");
    var md = GetAttribute (ctrl.parentNode, "modifyDocument");
    
    if (numeric == "false")
        return;

    if (event.keyCode.toString () == "38" && 
        parseFloat (ctrl.value, 10) < parseFloat (GetAttribute (ctrl, "maxValue"), 10))
    {
        if (md == null || md == "true")
            docModified = true;
        ctrl.value ++;
    }
    else if (event.keyCode.toString () == "40" && 
        parseFloat (ctrl.value, 10) > parseFloat (GetAttribute (ctrl, "minValue"), 10))
    {
        if (md == null || md == "true")
            docModified = true;
        ctrl.value --;
    }
   
    if (ctrl.value.indexOf (" ") != -1)
       ctrl.value = ctrl.value.replace (/ /, "");
       
    if (event.keyCode.toString () == "32" && GetAttribute (ctrl, "oldValue") == "")
    {
        SetAttribute (ctrl, "oldValue", ctrl.value);
    }
}

function glnud_onKeyUp (ctrl, event)
{
    var numeric = GetAttribute (ctrl, "numeric");
    
    if (numeric == "false")
        return;

    var md = GetAttribute (ctrl.parentNode, "modifyDocument");
    var endValue = "";
    var firstChunkIsEq = true;

    if (ctrl.value.indexOf (" ") != -1)
        ctrl.value = ctrl.value.replace (/ /, "");
       
    if (ctrl.value != "" && Number (ctrl.value).toString () == Number.NaN.toString ())
        ctrl.value = GetAttribute (ctrl, "oldValue");
    else if (ctrl.value.length > 0)
    {
        var maxL = parseFloat (GetAttribute (ctrl, "maxL"), 10);
        var oldValue = GetAttribute (ctrl, "oldValue");
        
        if (ctrl.value.length > maxL)
        {
            for (var i = 0; i < maxL; i ++)
            {
                endValue += ctrl.value [i];
                if (ctrl.value [i] != oldValue [i])
                    firstChunkIsEq = false;
            }

            if (GetAttribute (ctrl, "wrapAround") == "true")
            {
                if (firstChunkIsEq)
                {
                    ctrl.value = ctrl.value [maxL];
                }
                else
                    ctrl.value = endValue;
                
                SetAttribute (ctrl, "oldValue", ctrl.value);
                SetAttribute (ctrl.parentNode, "value", ctrl.value);

                // Ora facciamo il seguente controllo nel caso il numericUpDown sia parte del
                // controllo glCalendar
                CalendarCheckValue (ctrl);
                
                if (md == null || md == "true")
                    docModified = true;
            }
//            else
//                ctrl.value = GetAttribute (ctrl, "oldValue");
            else
                ctrl.value = endValue;
                
            return;
        }
        else if (event.keyCode.toString () != "32")
        {
            SetAttribute (ctrl, "oldValue", ctrl.value);
            SetAttribute (ctrl.parentNode, "value", ctrl.value);
            
            CalendarCheckValue (ctrl);

            if (md == null || md == "true")
                docModified = true;
       }
    }
    else if (ctrl.value == "")
    {
        SetAttribute (ctrl, "oldValue", ctrl.value);
        SetAttribute (ctrl.parentNode, "value", ctrl.value);
        
        CalendarCheckValue (ctrl);

        if (md == null || md == "true")
            docModified = true;
    }
        
    if (event.keyCode.toString () == "32" ||  // barra spazio
        (ctrl.value.length >= GetAttribute (ctrl, "maxL") && GetAttribute (ctrl, "wrapAround") == "false" &&
         event.keyCode.toString () != "38" && event.keyCode.toString () != "40" && event.keyCode.toString () != "9"))
    {
        ctrl.value = GetAttribute (ctrl, "oldValue");
        var nextCtrl = GetAttribute (ctrl, "nextControl");
        if (document.getElementById(nextCtrl) != null)
        {
            document.getElementById(nextCtrl).focus ();
            document.getElementById(nextCtrl).select ();
        }
    }
}

//function SetCalendarStuff (ctrl)
//{
//    if (ctrl.parentNode.parentNode != null)
//    {
//        var dd = GetAttribute (ctrl.parentNode.parentNode, "dd");
//        var MM = GetAttribute (ctrl.parentNode.parentNode, "MM");
//        var yyyy = GetAttribute (ctrl.parentNode.parentNode, "yyyy");
//        
//        if (GetAttribute (ctrl, "dd") != null)
//        {
//            SetAttribute (ctrl.parentNode.parentNode, "dd", ctrl.value);
//            dd = ctrl.value;
//        }
//        else if (GetAttribute (ctrl, "MM") != null)
//        {
//            SetAttribute (ctrl.parentNode.parentNode, "MM", ctrl.value);
//            MM = ctrl.value;
//        }
//        else if (GetAttribute (ctrl, "yyyy") != null)
//        {
//            SetAttribute (ctrl.parentNode.parentNode, "yyyy", ctrl.value);
//            yyyy = ctrl.value;
//        }
//        
//        SetAttribute (ctrl.parentNode.parentNode, "value", dd + "/" + MM + "/" + yyyy);
//    }
//}

function glnud_onDeactivate (ctrl)
{
    CheckNudValue (ctrl);
}

function glnud_onBlur (ctrl)
{
    CheckNudValue (ctrl);
}

function glnud_onFocus (ctrl)
{
    ctrl.select ();
}

function CheckNudValue (ctrl)
{
    var numeric = GetAttribute (ctrl, "numeric");
    
    if (numeric == "false")
    {
        var fieldName = GetAttribute (ctrl, "fieldName");
        if (parseFloat (GetAttribute (ctrl, "minValue"), 10) > ctrl.value.length || 
            parseFloat (GetAttribute (ctrl, "maxValue"), 10) < ctrl.value.length)
        {
            dataError = true;
            alert ("Numero di caratteri non valido per il campo <" + fieldName + ">");
        }
    }
    else
    {   
        if (ctrl.value != "" && Number (ctrl.value).toString () == Number.NaN.toString ())
            ctrl.value = GetAttribute (ctrl, "oldValue");
        else if (ctrl.value != "" && parseFloat (ctrl.value, 10) >= parseFloat (GetAttribute (ctrl, "minValue"), 10) &&
                 parseFloat (ctrl.value, 10) <= parseFloat (GetAttribute (ctrl, "maxValue"), 10))
            SetAttribute (ctrl, "oldValue", ctrl.value);
        else if (ctrl.value != "")
        {
            if (parseFloat (ctrl.value, 10) < parseFloat (GetAttribute (ctrl, "minValue"), 10))
                ctrl.value = parseFloat (GetAttribute (ctrl, "minValue"), 10)
            else
            {
                ctrl.value = parseFloat (GetAttribute (ctrl, "maxValue"), 10)
            }
                
            SetAttribute (ctrl, "oldValue", ctrl.value);
        }
        
        CallCustomFunction (ctrl, "CustomCheckNudValue");
    }
}

