function getObject(id)
{
	if (document.getElementById) {
		// this is the way the standards work
		return document.getElementById(id);
	} else if (document.all) {
		// this is the way old msie versions work
		return document.all[id];
	} else if (document.layers)	{
		// this is the way nn4 works
		return document.layers[id];
	}
}

// recupera la label si un campo a partire dall'id
function GetLabelForId(id)
{
     var labels = document.getElementsByTagName('label');
     for (var i = 0; i < labels.length; i++)
     if (labels[i].htmlFor == id)
         return labels[i];
     return null;
}

// controlla se un campo è vuoto
function Empty(szStr) 
{
    var fEsi = true;
    var ii = 0;
    var ch = "";
    for (ii = 0; ii < szStr.length && fEsi; ii++)
    {
        ch = szStr.charAt (ii);
        if (ch != " ")
            fEsi = false;
    }
    return fEsi;
}

// controlla formato della data gg/mm/aaaa
function FrmtDate(camp) 
{
    var MESI = "312831303130313130313031";
    var fEsi = true;
    var fSep = false;
    var iLen = 0; var ii = 0; var iSep1 = 0; var iSep2 = 0; var iGG = 0;
    var iMM = 0; var iGGMM = 0; var iAnno = 0; var iCntSep = 0; var iCntCif = 0;
    var iLenAnno = 0; 
    var ch = "";
    var szGG = ""; var szMM = ""; var szAnno = "";
    var szVal = camp.value;
    var szNam = camp.name;
    if (Empty(szVal)) 
        return fEsi;
    iLen = szVal.length;
    if (iLen < 6)
        fEsi = false;
    else
    {
        for (ii = 0; ii < iLen && fEsi == true; ii++)
        {
            ch = szVal.charAt(ii);
            if (ch < "0" || ch > "9")
            {
                if (ch != " " && ch != "/" && ch != "-" && ch != ".") 
                    fEsi = false;
                else
                {
                    if ((ii == 0) || (ii >= (iLen - 2)) ||
                        ((iCntSep == 1) && (ii == iSep1 + 1))) 
                        fEsi = false;
                    else
                    {
                        iCntSep++;
                        if (iCntSep == 1)
                        {
                            fSep = true; 
                            iSep1 = ii;
                        }
                        else if (iCntSep == 2)
                            iSep2 = ii;
                        else
                            fEsi = false; 
                    }
                }
            }
            else
            {
                iCntCif++;
                if (iCntCif > 8)
                    fEsi = false;
            } 
        }
        if (iCntSep == 1)
            fEsi = false;
        if (fEsi)
        {
            if (!fSep)
                iSep1 = 2;
            szGG = szVal.substring(0, iSep1);
            //iGG = eval(szGG); 
            iGG = parseInt(szGG,10); 
            if (szGG.length == 1)
                szGG = "0" + szGG;
            if (!fSep)
            {
                iSep1 = 1;
                iSep2 = 4;
            }
            szMM = szVal.substring(iSep1+1, iSep2);
            if (!fSep)
                iSep2 = 3;
            iMM = eval(szMM);
            if (iMM < 1 || iMM > 12)
                fEsi = false;
            else
            {
                if (szMM.length == 1)
                    szMM = "0" + szMM;
                szAnno = szVal.substring(iSep2+1, iLen);
                iLenAnno = szAnno.length;
                if (iLenAnno % 2 != 0) 
                    fEsi = false;
                else
                {
                    iAnno = parseInt(szAnno, 10);
                    if (iLenAnno == 2)
                    {
                        if (iAnno < 26) 
                            iAnno += 2000;
                        else
                            iAnno += 1900;
                        szAnno = eval("" + iAnno);
                    }
                    iGGMM = eval( MESI.substring((iMM-1)*2, ((iMM-1)*2)+2));
                    if (iMM == 2)
                        if ((iAnno % 4 == 0) && (iAnno % 100 != 0 || iAnno % 400 == 0))
                            iGGMM++;
                    if (iGG > iGGMM)
                        fEsi = false;
                    else
                        szVal = szGG + "/" + szMM + "/" + szAnno;
                }
            }
        }
    }
    return fEsi;
}

//Controllo campi numerici
function isvalidnumber(field) { 
var valid = "0123456789,."
var ok = true;
var temp;
for (var i=0; i<field.value.length; i++) {
	temp = "" + field.value.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok = false; 
	}
return ok;
}

// evidenziazione dei campi con problemi di validazione
function getKoColor () { return '#FFFFBB' }
function getOkColor () { return '#FFFFFF' }
function highlite_field(field) {
	field.style.backgroundColor = getKoColor();
}
function reset_field(field) {
	field.style.backgroundColor = getOkColor();
}

// valida l'editing dei dati del socio
function check_socio(aForm) {
	var result = true;
	
	if ( (Empty(aForm.elements['associato.cap'].value)) || (!isvalidnumber(aForm.elements['associato.cap'])) || (aForm.elements['associato.cap'].value.length<5) ) { highlite_field(aForm.elements['associato.cap']) ; result = false } else {reset_field(aForm.elements['associato.cap'])} ;
	if ( (Empty(aForm.elements['associato.presidenteCap'].value)) || (!isvalidnumber(aForm.elements['associato.presidenteCap'])) || (aForm.elements['associato.presidenteCap'].value.length<5) ) { highlite_field(aForm.elements['associato.presidenteCap']) ; result = false } else {reset_field(aForm.elements['associato.presidenteCap'])} ;
	if ( (Empty(aForm.elements['associato.annoFondazione'].value)) || (!isvalidnumber(aForm.elements['associato.annoFondazione'])) || (aForm.elements['associato.annoFondazione'].value.length<4) ) { highlite_field(aForm.elements['associato.annoFondazione']) ; result = false } else {reset_field(aForm.elements['associato.annoFondazione'])} ;
	if ( (Empty(aForm.elements['associato.numeroAssociati'].value)) || (!isvalidnumber(aForm.elements['associato.numeroAssociati'])) ) { highlite_field(aForm.elements['associato.numeroAssociati']) ; result = false } else {reset_field(aForm.elements['associato.numeroAssociati'])} ;

	if (result == false) {
		msg1 = 'Alcuni campi non sono compilati correttamente.\n\n';
		msg3 = 'Prego verificare';
		msg = msg1 + msg3;
 		alert(msg);
	};
	return result;
}

// valida l'editing dei dati di una sede
function check_sede(aForm) {
	var result = true;
	
	if ( (Empty(aForm.elements['sede.coordCap'].value)) || (!isvalidnumber(aForm.elements['sede.coordCap'])) || (aForm.elements['sede.coordCap'].value.length<5) ) { highlite_field(aForm.elements['sede.coordCap']) ; result = false } else {reset_field(aForm.elements['sede.coordCap'])} ;

	if (result == false) {
		msg1 = 'Alcuni campi non sono compilati correttamente.\n\n';
		msg3 = 'Prego verificare';
		msg = msg1 + msg3;
 		alert(msg);
	};
	return result;
}