function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function isEmpty(data) {
	for(var i=0; i<data.length; i++) {
		if(data.substring(i, i+1) != " ") return(false);
	}
	return(true);
} 

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 aprichiudi(objid, expanded) {
 	if(expanded!='table') expanded='block';
 
 	var stato = document.getElementById(objid).style.display;
 	if(stato=='none') document.getElementById(objid).style.display = expanded;
 	else			  document.getElementById(objid).style.display = 'none';
}

function calcola_old() {
	var fe = document.forms[0].elements;
	var numfe = fe.length;
	var totpezzi = 0;
	var toteuro = 0;

	for(var i=0; i<numfe; i++) {
		var el = fe[i];
		var keys = el.name.split("_");
		
		if(keys[0]=="q" && !isEmpty(el.value) && !isNaN(el.value)) {
			var id_prezzo = 'p';
			for(var j=1; j<keys.length; j++) {
				id_prezzo += '_';
				id_prezzo += keys[j];
			}
			
			qta = parseInt(el.value, 10);
			prz = parseFloat(findObj(id_prezzo).value);
			
			totpezzi += qta;
			toteuro += qta*prz;
		} 
		if(keys[0]=="q" && isNaN(el.value)) {
			el.value = "";
		}
	}

	findObj("tot_pezzi").value = totpezzi;
	findObj("tot_euro").value = toteuro;
	return true;
}

function calcola() {
	var fe = document.forms[0].elements;
	var numfe = fe.length;
	var totpezzi = 0;
	var toteuro = 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('prezzo_'+keys[1]).value);
			findObj("tot_"+keys[1]).value = qta*prz;
			
			totpezzi += qta;
			toteuro += qta*prz;
		} 
		if(keys[0]=="qta" && isNaN(el.value)) {
			el.value = "";
		}
	}

	findObj("tot_pezzi").value = totpezzi;
	findObj("tot_euro").value = toteuro;
	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 verifica() {

	calcola();	

	if( isEmpty(findObj('tot_pezzi').value) ||  isEmpty(findObj('tot_euro').value) ||
			findObj('tot_pezzi').value==0 || findObj('tot_euro').value==0 ) {
		alert('Inserire almeno un articolo');
        return false;
    }
	

    var aaa = new Array('nome','cognome','indirizzo','citta','cap','provincia','telefono','email');
    
	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;
    }

	frm = findObj('formlist');
	frm.action = "formmail.php";
	frm.submit();
	return true;
}
