function isEmpty(data) {
	for(var i=0; i<data.length; i++) {
		if(data.substring(i, i+1) != " ") return(false);
	}
	return(true);
} 

function checkEMAIL(val) {
	var email=/^[A-Za-z0-9][\w-.]+[A-Za-z0-9]@[A-Za-z0-9]([\w-.]+[A-Za-z0-9]\.)+([A-Za-z]){2,4}$/i;
	return(email.test(val));
}

function findObj(theObj, theDoc) {
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}

function calcola()
{
	var fe = document.forms[0].elements;
	var numfe = fe.length;
	var totpezzi = 0;
	var toteuro = 0;
	var totpeso = 0;
	var totale = 0;

	for(var i=0; i<numfe; i++) {
		var el = fe[i];
		var keys = el.name.split("_");
		
		if(keys[0]=="qta" && !isEmpty(el.value) && !isNaN(el.value)) {
			qta = parseInt(el.value, 10);
			prz = parseFloat(findObj('prz_'+keys[1]+'_'+keys[2]).value);
			peso = parseFloat(findObj('conf_'+keys[1]+'_'+keys[2]).value);
			
			totpezzi += qta;
			toteuro += qta*prz;
			totpeso += peso*qta;
		} 
		if(keys[0]=="qta" && isNaN(el.value)) {
			el.value = "";
		}
	}

	findObj('totale_confezioni').value = totpezzi.toFixed(0)
	var peso = totpeso/1000;
	findObj('totale_peso').value = peso.toFixed(3)
	findObj('totale_prodotti').value = toteuro.toFixed(2);

	provincia = findObj('provincia').value;
	if(isEmpty(provincia)) {
		alert('Per calcolare le spese di spedizione e completare l\'ordine \n devono essere compilate citta\' e provincia');
		
		findObj('spese_spedizione').value = '****';
		findObj('totale_ordine').value = '****';
		return false;
	} else {
		var kpr = provincia.split('|');
		var idx = kpr[1];
		if(kpr[0].toUpperCase()=='FE' && findObj('citta').value.toUpperCase()=='FERRARA') {idx = 1;}
		
		var xxx = 'tcosto_'+idx;
		var yyy = 'tlimite_'+idx;
		var spesatrasporto = findObj(xxx).value*1;
		var pesominimo = findObj(yyy).value*1;
		
		findObj('spese_spedizione').value = spesatrasporto.toFixed(2);
		var totale = toteuro*1+spesatrasporto*1;
		findObj('totale_ordine').value = totale.toFixed(2);
		
		if(pesominimo*1000 > totpeso) {
			alert('La quantita\' minima ('+pesominimo.toFixed(2)+' kg) non e\' stata raggiunta.');
			return false;
		}
	}
	
	return true;
}

function verifica()
{

	if(!calcola()) return false;

    var aaa = new Array('nome','cognome','citta','email', 'provincia','indirizzo','cap');
    
	errors = '';
    for(var i=0; i<aaa.length; i++){
		if( isEmpty(findObj(aaa[i]).value) ){
			errors += aaa[i];
			errors += ' - ';
        }
	}
	if(!isEmpty(errors)) {
		alert('Inserire: '+ errors);
		return false;
	}

    if( !checkEMAIL(findObj('email').value) ){
      alert('Indirizzo email non valido, prego verificare');
      findObj('email').focus();
      return false;
    }
    
    if( !findObj("privacyyes").checked) {
    	alert("E' necessario approvare la clausola sulla privacy per provedere con la richiesta.");
    	return false;
    }

	return true;
}


