// FUNZIONI PER FORM

function checkForm(nomeForm){
	var oForms = document.getElementsByTagName("form");
	
	for(var i = 0; i < oForms.length; i++){
		if(oForms[i].getAttribute("name") == nomeForm){
			var oForm = oForms[i];
		}
	}
	
	var aInput = oForm.getElementsByTagName("input");
	var aSelect = oForm.getElementsByTagName("select");
	//da completare con i radio, checkbox
	

	for(var i = 0; i < aInput.length; i++){
		if((aInput[i].getAttribute("obbligo") == 1) && (aInput[i].value == '')){
			alert(aInput[i].getAttribute("alert"));
			aInput[i].focus();
			return false;
		}
		else{
			if((aInput[i].getAttribute("http") == 1) && (aInput[i].value.indexOf("http://") == -1)){
				aInput[i].value = 'http://' + aInput[i].value;
			}
		}
	}

	for(var i = 0; i < aSelect.length; i++){
		if((aSelect[i].getAttribute("obbligo") == 1) && (aSelect[i].options[aSelect[i].selectedIndex].value == '')){
			alert(aSelect[i].getAttribute("alert"));
			aSelect[i].focus();
			return false;
		}
	}
	
	return true;
}

//SET DATA

function setData(sForm, controllo){
	var aInput = sForm.getElementsByTagName("input");
	
	var dData = new Date();
	var giornoNum = dData.getDate();
	if(giornoNum.length == 1){giornoNum = "0" + giornoNum;}
	var meseNum = dData.getMonth() + 1;
	if(meseNum.length == 1){meseNum = "0" + meseNum;}
	var annoNum = dData.getFullYear();
	
	//alert(giornoNum + " " + meseNum + " " + annoNum);
	
	for(var i = 0; i != aInput.length; i++){
		if(aInput[i].getAttribute("name").indexOf("giorno") != -1){
			aInput[i].value = giornoNum;
		}
		if(aInput[i].getAttribute("name").indexOf("mese") != -1){
			aInput[i].value = meseNum;
		}
		if(aInput[i].getAttribute("name").indexOf("anno") != -1){
			aInput[i].value = annoNum;
		}
	}
	if(controllo != -1){
		return checkForm(sForm.getAttribute("name"));
	}
	else{
		return true;
	}
}

//CONTATORE CARATTERI

function contatoreCaratteri(spazioInput, spazioContatore, limite){
	var numCaratteri = spazioInput.value.length;
	var innerSpazioContatore = document.getElementById(spazioContatore); 
	if(numCaratteri <= limite){
		innerSpazioContatore.innerHTML = limite - (numCaratteri);
	}
	else{
		spazioInput.value = spazioInput.value.substr(0, limite);
		alert("Impossibile inserire piu' di " + limite + " caratteri");
	}
	
}
