//Autore Alessandro Veneziano
function IsAlphaNumeric(TextField)
{
        var illegalChars = /[\W_]/;
        return !(illegalChars.test(TextField.value));
        //restituisce true solo per stringhe alfanumeriche
}

function ContainIllegalChar(TextField) {
    var illegalChars = /\W/;
    return (illegalChars.test(TextField.value));
}

function IsEmpty(TextField) {
   return ((TextField.value.length==0) || (TextField.value==null))
}

function Trim(TextField) {
   return TextField.value.replace(/\s+$|^\s+/g,"");
   }

function checkAll(field)
{
        if (field != null) {
                if (field[0]) {
                    for (i = 0; i < field.length; i++)
                                field[i].checked = true;
                }
                else
                        field.checked = true;
        }
}

function uncheckAll(field)
{
    if (field != null) {
                if (field[0]) {
                    for (i = 0; i < field.length; i++)
                        field[i].checked = false;
                }
                else {
                        field.checked = false;
                }
        }
}

function GetcheckCount(item) {
    var value = 0;
    if (item != null) {
            if (item[0]) {
                for (i=0;i<item.length;i++) {
                    if (item[i].checked==true) {
                       value++;
                    }
                }
            }
            else if (item.checked == true)
                    value = 1;
         }
     return value;
}

function GetCount(item) {
        if (item != null) {
                if (item[0])
                        return item.length;
                else
                        return 1;
        }
        else
            return 0;
}
/**
 * Funzione per la generazione di popup
 */
function popupper(url,x,y,name,sb,rs,st,lt,tb) {
        var win = null;
        var centerwidth=(screen.width/2)-(x/2);
        var centerheight=(screen.height/2)-(y/2);
        var scrollbarstext = 'scrollbars = no,';
        var resizabletext = 'resizable = no,';
        var statustext = 'status = no,';
        var locationtext = 'location = no,';
        var toolbartext = 'toolbar = no,';
        var features = '';
        if (sb == 1){
                scrollbarstext = 'scrollbars = yes,'
        }
        if (rs == 1){
                resizabletext = 'resizable = yes,'
        }
        if (st == 1){
                statustext = 'status = yes,'
        }
        if (lt == 1){
                locationtext = 'location = yes,'
        }
        if (tb == 1){
                toolbartext = 'toolbar = yes,'
        }
        features = scrollbarstext+resizabletext+statustext;
        features = features + locationtext+toolbartext;
        features = features + 'width = '+x+',height = '+y;
        features = features + ',top = ' + centerheight;
        features = features + ',left = ' + centerwidth;
        win = window.open(url, name, features);
}
/**
 * Funzione utilizzata per recuperare la lista dei file non che soddisfano una
 * determinata proprietà
 * restituisce un Array contenente gli indici degli elementi del vettore che soddisfano la proprietà <property>
 */
function getNotMatchingProperty(checkedfilelist,propertieslist,property,acceptnull) {
        var notmatchingarray = new Array();
        if ((propertieslist != null) && (checkedfilelist != null)) {
                if (checkedfilelist[0]) {
            for (i = 0; i < checkedfilelist.length; i++) {
                                if (checkedfilelist[i].checked) {
                                        if ((propertieslist[i].value != property) && ((propertieslist[i].value != "") || acceptnull)) {
                                                index = notmatchingarray.length;
                                                notmatchingarray[index] = i;
                                        }
                                  }
                        }
                  }
                  else {
                        if (checkedfilelist.checked) {
                                if ((propertieslist.value != property) && ((propertieslist.value != "") || acceptnull)) {
                    notmatchingarray[0] = 0;
                                  }
                          }
                }
        }
        return notmatchingarray;
}
/**
 * Funzione utilizzata per recuperare la lista dei file che soddisfano una
 * determinata proprietà
 * restituisce un Array contenente gli indici degli elementi del vettore che soddisfano la proprietà <property>
 */
function getMatchingProperty(checkedfilelist,propertieslist,property,acceptnull) {
        var matchingarray = new Array();
        if ((propertieslist != null) && (checkedfilelist != null)) {
                if (checkedfilelist[0]) {
            for (i = 0; i < checkedfilelist.length; i++) {
                                if (checkedfilelist[i].checked) {
                                        if ((propertieslist[i].value == property) && ((propertieslist[i].value != "") || acceptnull)) {
                                                index = matchingarray.length;
                                                matchingarray[index] = i;
                                        }
                                  }
                        }
                  }
                  else {
                        if (checkedfilelist.checked) {
                                if ((propertieslist.value == property) && ((propertieslist.value != "") || acceptnull)) {
                    matchingarray[0] = 0;
                                  }
                          }
                }
        }
        return matchingarray;
}

/**
 *  Restituisce una stringa contenente gli elementi del vettore vector,indicati nel vettore
 *  index separati dal carattere ch
 */
 function printVector(vector,index,ch) {
        var str = '';
        if (vector[0]) {
            for(i=0; i < index.length; i++) {
              var item = index[i];
              str = str + vector[item].value + ch;
             }
        }
        else {
              str = vector.value + ch;
        }
        return str;
 }

 // Autore Maurizio Orlando
// funzione di controllo della validità di una stringa
function findInvalidChar(strWord){
        invalidChars = " /:,;";
        for (i=0; i<invalidChars.length; i++) {
                badChar = invalidChars.charAt(i);
                if (strWord.indexOf(badChar,0) != -1) {
                        return true;
                }
        }
        return false;
}

// funzione che controlla un indirizzo email
function validateEmail(EmailField) {
        var email = "";

        email = EmailField.value;
        if (email == "") {
                return false;
        }
        if (findInvalidChar(email)) {
                return false;
        }
        atPos = email.indexOf("@",1)
        if (atPos == -1) {
                return false;
        }
        if (email.indexOf("@",atPos+1) != -1) {
                return false;
        }
        periodPos = email.indexOf(".",atPos);
        if (periodPos == -1) {
                return false;
        }
        if (periodPos+3 > email.length)        {
                return false;
        }
        offset = periodPos - atPos ;
        if (offset<2){
                return false;
        }

        return true;
}
