/*****
 * JavaScript form validation using DOM Level 2 Events Setup
 *
 * Workaround for IE7 "getElementById()" unknown run-time errors with DOM 
 * Element manipulation.
 * 
 * Author: Gabriele Ghirimoldi @ OpenSourceFuture ORG
 * 
 *************************************************************************/

var msg = "Campo obbligatorio.";
var msg_mail = "Questo indirizzo e-mail non \u00E8 valido.";	
var msg_att = "I formati permessi sono .pdf, .xls, .xlsx";

validCliente = false;
validIndirizzo = false;
validName = false;
validRif = false;
validTel = false;
validFax = false;
validEmail = false;
validText = false;
validAtt = true;

if(window.addEventListener)
	{
	window.addEventListener("load",setupEventsForm,false);
	} else if (window.attachEvent){
		window.attachEvent("onload",setupEventsForm);
	} else {window.onload = setupEventsForm;
}

function setupEventsForm(evnt) {

	document.inviorichiesta.cliente.onblur = checkRequiredCliente;
	document.inviorichiesta.indirizzo.onblur = checkRequiredIndirizzo;
	document.inviorichiesta.name_required.onblur = checkRequiredName;
	document.inviorichiesta.riferimento.onblur = checkRequiredRif;
	document.inviorichiesta.telefono.onblur = checkRequiredTel;
	document.inviorichiesta.fax.onblur = checkRequiredFax;
	document.inviorichiesta.email.onblur = checkRequiredEmail;
	document.inviorichiesta.textarea.onblur = checkRequiredText;
	document.inviorichiesta.fileupload_file.onblur = checkRequiredAtt;
	
	if(document.inviorichiesta.addEventListener) {
		document.inviorichiesta.addEventListener("submit", validateForm, false);
	} else if(document.inviorichiesta.attachEvent) {
		document.inviorichiesta.attachEvent("onsubmit", validateForm);
	}
	else {
		document.inviorichiesta.onsubmit = validateForm; 
	}
}

function checkRequiredAtt(evnt) {

	evnt = evnt ? evnt : window.event;
	var target = evnt.target ? evnt.target : evnt.srcElement;
	var file = target.value;
	if(file == "" || file == null) {
		validAtt=true;
	}
	else {
		var okArray = new Array();
		extArray = new Array(".pdf", ".xls", ".xlsx");
		while (file.indexOf("/") != -1)
		file = file.slice(file.indexOf("/") + 1);
		ext = file.slice(file.indexOf(".")).toLowerCase();
		n=0;
		for (i in extArray) {
			if (extArray[i] == ext) {
				okArray[n] = ext;
				n++;
			}
		}
		if (okArray.length==1) { validAtt=true;}
		else validAtt=false;
	}
} 

function checkRequiredCliente(evnt) {
	
	evnt = evnt ? evnt : window.event;
	var target = evnt.target ? evnt.target : evnt.srcElement;
	var txtInput = target.value;
	if(txtInput == "" || txtInput == null) {
	validCliente = false;
	} else validCliente = true;
}

function checkRequiredIndirizzo(evnt) {
	
	evnt = evnt ? evnt : window.event;
	var target = evnt.target ? evnt.target : evnt.srcElement;
	var txtInput = target.value;
	if(txtInput == "" || txtInput == null) {
	validIndirizzo = false;
	} else validIndirizzo = true;
}

function checkRequiredName(evnt) {
	
	evnt = evnt ? evnt : window.event;
	var target = evnt.target ? evnt.target : evnt.srcElement;
	var txtInput = target.value;
	if(txtInput == "" || txtInput == null) {
	validName = false;
	} else validName = true;
}

function checkRequiredRif(evnt) {
	
	evnt = evnt ? evnt : window.event;
	var target = evnt.target ? evnt.target : evnt.srcElement;
	var txtInput = target.value;
	if(txtInput == "" || txtInput == null) {
	validRif = false;
	} else validRif = true;
}

function checkRequiredTel(evnt) {
	
	evnt = evnt ? evnt : window.event;
	var target = evnt.target ? evnt.target : evnt.srcElement;
	var txtInput = target.value;
	if(txtInput == "" || txtInput == null) {
	validTel = false;
	} else validTel = true;
}

function checkRequiredEmail(evnt) {
	
	evnt = evnt ? evnt : window.event;
	var target = evnt.target ? evnt.target : evnt.srcElement;
	var txtInput = target.value;
	var rgEx = /^.+@[^\.].*\.[a-z]{2,}$/;
	var OK = rgEx.exec(target.value);
	if(!OK || txtInput == "" || txtInput == null) {
	validEmail = false;
	} else validEmail = true;
}

function checkRequiredFax(evnt) {
	
	evnt = evnt ? evnt : window.event;
	var target = evnt.target ? evnt.target : evnt.srcElement;
	var txtInput = target.value;
	if(txtInput == "" || txtInput == null) {
	validFax = false;
	} else validFax = true;
}

function checkRequiredText(evnt) {
	
	evnt = evnt ? evnt : window.event;
	var target = evnt.target ? evnt.target : evnt.srcElement;
	var txtInput = target.value;
	if(txtInput == "" || txtInput == null) {
	validText = false;
	} else validText = true;
}

function validateForm(evnt) {
	
		var valid = new Array(validCliente,validIndirizzo,validName,validRif,validTel,validFax,
		validEmail,validText,validAtt);
		
		var control = new Array('validCliente','validIndirizzo','validName','validRif','validTel','validFax',
		'validEmail','validText','validAtt');
		
		var ok = new Array();
		i=0;
		for(index in valid) {
			if(valid[index]==true) {
				ok[i] = true;
				i++;
			} 
		}
	if(ok.length==control.length);
	else {	
	
		for(index in control) {	
		var value = document.createElement("td");
			switch (control[index]) {
			case 'validCliente':
				var td1 = document.getElementById("cliente");
				var td2 = document.getElementById("cliente1");				
				if(valid[index] == false) {
					value.innerHTML = msg;
					value.setAttribute("id","cliente1");
					td1.replaceChild(value,td2);				
				} else {
					value.innerHTML = "";
					value.setAttribute("id","cliente1");
					td1.replaceChild(value,td2);			
				}
				break;	
			case 'validIndirizzo':
				var td1 = document.getElementById("Indirizzo");
				var td2 = document.getElementById("Indirizzo1");
				if(valid[index] == false) {
					value.innerHTML = msg;
					value.setAttribute("id","Indirizzo1");
					td1.replaceChild(value,td2);				
				} else {
					value.innerHTML = "";
					value.setAttribute("id","Indirizzo1");
					td1.replaceChild(value,td2);			
				}
				break;
			case 'validName':
				var td1 = document.getElementById("Name");
				var td2 = document.getElementById("Name1");
				if(valid[index] == false) {
					value.innerHTML = msg;
					value.setAttribute("id","Name1");
					td1.replaceChild(value,td2);				
				} else {
					value.innerHTML = "";
					value.setAttribute("id","Name1");
					td1.replaceChild(value,td2);			
				}
				break;
			case 'validRif':
				var td1 = document.getElementById("Rif");
				var td2 = document.getElementById("Rif1");
				if(valid[index] == false) {
					value.innerHTML = msg;
					value.setAttribute("id","Rif1");
					td1.replaceChild(value,td2);				
				} else {
					value.innerHTML = "";
					value.setAttribute("id","Rif1");
					td1.replaceChild(value,td2);			
				}
				break;
			case 'validTel':
				var td1 = document.getElementById("Tel");
				var td2 = document.getElementById("Tel1");
				if(valid[index] == false) {
					value.innerHTML = msg;
					value.setAttribute("id","Tel1");
					td1.replaceChild(value,td2);				
				} else {
					value.innerHTML = "";
					value.setAttribute("id","Tel1");
					td1.replaceChild(value,td2);			
				}
				break;
			case 'validFax':
				var td1 = document.getElementById("Fax");
				var td2 = document.getElementById("Fax1");
				if(valid[index] == false) {
					value.innerHTML = msg;
					value.setAttribute("id","Fax1");
					td1.replaceChild(value,td2);				
				} else {
					value.innerHTML = "";
					value.setAttribute("id","Fax1");
					td1.replaceChild(value,td2);			
				}
				break;
			case 'validEmail':
				var td1 = document.getElementById("Email");
				var td2 = document.getElementById("Email1");
				if(valid[index] == false) {
					value.innerHTML = msg_mail;
					value.setAttribute("id","Email1");
					td1.replaceChild(value,td2);				
				} else {
					value.innerHTML = "";
					value.setAttribute("id","Email1");
					td1.replaceChild(value,td2);			
				}
				break;
			case 'validText':
				var td1 = document.getElementById("Text");
				var td2 = document.getElementById("Text1");
				if(valid[index] == false) {
					value.innerHTML = msg;
					value.setAttribute("id","Text1");
					td1.replaceChild(value,td2);				
				} else {
					value.innerHTML = "";
					value.setAttribute("id","Text1");
					td1.replaceChild(value,td2);			
				}
				break;
			case 'validAtt':
				var td1 = document.getElementById("Att");
				var td2 = document.getElementById("Att1");
				if(valid[index] == false) {
					value.innerHTML = msg_att;
					value.setAttribute("id","Att1");
					td1.replaceChild(value,td2);				
				} else {
					value.innerHTML = "";
					value.setAttribute("id","Att1");
					td1.replaceChild(value,td2);			
				}
				break;	
			}
		}	
		evnt = evnt ? evnt : window.event;
		if(evnt.preventDefault) {
			evnt.preventDefault();}
		else if(evnt.cancelBubble!=null) {
			evnt.cancelBubble=true;}
		return false;
	}
}

