//============================================================================================================================================
//
//
//============================================================================================================================================


//============================================================================================================================================
// Costanti utili

var val_string_not_null       = 1;
var val_string_not_only_space = 2;
var val_long                  = 3;
var val_date                  = 5;
var val_date_null	      = 6;


//============================================================================================================================================
// Ritorna true se value è nullo
function is_null(value){
    if (value.length == 0) return true;
    return false;
}

//============================================================================================================================================
// Ritorna true se value è nullo o contiene solo spazi
function is_only_space(value){
    if (is_null(value)) return true;
    for (var i = 0; i < value.length; i++){
        if (value.charCodeAt(i)!= 32) return false;
    }
    return true;
}

//============================================================================================================================================
// Ritorna true se value è long
function is_long(value){
    if (is_null(value)) return false;
    for (var i = 0; i < value.length; i++){
        switch (value.charCodeAt(i)){
        //   '0'      '1'      '2'      '3'      '4'      '5'      '6'      '7'      '8'      '9'
        case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57:
            break;
        default:
	    return false
        }
    }
    return true;
}

//============================================================================================================================================
// Ritorna true se value è una data valida
function is_date(value){
    var d = 0; var s_d = new String(""); var s_d2 = new String("");
    var m = 0; var s_m = new String(""); var s_m2 = new String("");
    var y = 0; var s_y = new String(""); var s_y2 = new String("");

    var i;
    var j;

    if (is_null(value)) return false;

    j = 0;
    for (i = 0; i < value.length; i++){
        if (value.charAt(i) == "/"){
            j++;
        }
        else{
            if (j == 0) s_d = s_d + value.charAt(i);
            if (j == 1) s_m = s_m + value.charAt(i);
            if (j == 2) s_y = s_y + value.charAt(i);
        }
    }

    if (!is_long(s_d)) return false;
    if (!is_long(s_m)) return false;
    if (!is_long(s_y)) return false;

    j = 0;
    for (i = 0; i < s_d.length; i++){
        if (j == 0 && s_d.charAt(i) != "0") j = 1;
        if (j == 1) s_d2 = s_d2 + s_d.charAt(i);
    }
    if (s_d2.length == 0) s_d2 = "0";
    j = 0;
    for (i = 0; i < s_m.length; i++){
        if (j == 0 && s_m.charAt(i) != "0") j = 1;
        if (j == 1) s_m2 = s_m2 + s_m.charAt(i);
    }
    if (s_m2.length == 0) s_m2 = "0";
    j = 0;
    for (i = 0; i < s_y.length; i++){
        if (j == 0 && s_y.charAt(i) != "0") j = 1;
        if (j == 1) s_y2 = s_y2 + s_y.charAt(i);
    }
    if (s_y2.length == 0) s_y2 = "0";



    d = parseInt(s_d2);
    m = parseInt(s_m2);
    y = parseInt(s_y2);

    if (y < 1900 || y > 9999) return false;
    if (m < 1 || m > 12) return false;
    if (d < 1) return false;

    switch (m){
    case 1:  // Gennaio
    case 3:  // Marzo
    case 5:  // Maggio
    case 7:  // Luglio
    case 8:  // Agosto
    case 10: // Ottobre
    case 12: // Dicembre
        if (d > 31) return false;
        break;
    case 4:  // Aprile
    case 6:  // Giugno
    case 9:  // Settembre
    case 11: // Novembre
        if (d > 30) return false;
        break;

    case 2:  // Febbraio 2000 29, 2001 28, 2002 28, 2003 28, 2004 29
        if ((y % 4) == 0){
            if (d > 29) return false;
        }
        else{
            if (d > 28) return false;
        }
        break;
    }
    return true;
}


//============================================================================================================================================
function is_valid(object, val_type, field_name){
    var i;
    var obj;
    var value;

    obj = object;

    if (obj == undefined) return true;

    value = obj.value;

    switch (val_type) {
    case val_string_not_null:
        if (is_null(value))         {alert("'" + field_name + "' non può essere vuoto!");           return false;}
	break;

    case val_string_not_only_space:
        if (is_null(value))         {alert("'" + field_name + "' non può essere vuoto!");           return false;}
        if (is_only_space(value))   {alert("'" + field_name + "' non contiene caratteri validi!");  return false;}
        break;

    case val_long:
        if (is_null(value))         {alert("'" + field_name + "' non può essere vuoto!");           return false;}
        if (!is_long(value))        {alert("'" + field_name + "' non è un numero valido!");	    return false;}


    case 4: // val_long_null
        if (is_null(value))         return true;
        if (!is_long(value))        {alert("'" + field_name + "' non è un numero valido!");	    return false;}
        break;

    case val_date: 
        if (is_null(value))         {alert("'" + field_name + "' non può essere vuota!");           return false;}
        if (!is_date(value))        {alert("'" + field_name + "' non è una data valida!");          return false;}
        break;

    case val_date_null:
        if (is_null(value))         return true;
        if (!is_date(value))        {alert("'" + field_name + "' non è una data valida!");          return false;}
        break;

    case 7: // val_time
        if (is_null(value))         {alert("'" + field_name + "' non può essere vuota!");           return false;}
        if (!is_time(value))        {alert("'" + field_name + "' non è un orario valido!");         return false;}
        break;

    case 8: // val_time_null
        if (is_null(value))         return true;
        if (!is_time(value))        {alert("'" + field_name + "' non è un orario valido!");	    return false;}
        break;

    default:
        alert("(is_valid) Unknown val_type = " + val_type);
    }

    return true;
}
