// JavaScript Controllo Form
function swapfinish(par,extra,obj,defimg) {
//	if (!DHTML) return;
	var sel=obj.selectedIndex;
	var pimg=document.images["PIM"+par+extra], pimgsrc;
	pimgsrc=(swapfinish.arguments[3+sel]) ?par+obj.options[sel].value :((defimg != null) ?par+defimg :"noimage");
	pimgsrc="/prodotti/images/"+pimgsrc+".gif";
	if (pimg.src.indexOf(pimgsrc) < 0) pimg.src=pimgsrc;
}

function popdisp(obj) {
	var par='status=no,toolbar=no,menubar=no,resizable=no,scrollbars=no,width=320,height=200', w;
	w=window.open(obj.href,obj.target,par);
	w.focus();
	return false;
}

function Form_Valid(theForm)
{

  if (theForm.qty.value == "")
  {
    alert("Inserire un valore per il campo \"Quantità\".");
    theForm.qty.focus();
    return (false);
  }

  if (theForm.qty.value.length < 1)
  {
    alert("Inserire almeno 1 caratteri nel campo \"Quantità\".");
    theForm.qty.focus();
    return (false);
  }

  if (theForm.qty.value.length > 2)
  {
    alert("Inserire al massimo 2 caratteri nel campo \"Quantità\".");
    theForm.qty.focus();
    return (false);
  }

  var checkOK = "0123456789-";
  var checkStr = theForm.qty.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Inserire solo cifra caratteri nel campo \"Quantità\".");
    theForm.qty.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseInt(allNum);
  if (chkVal != "" && !(prsVal >= 1 && prsVal <= 99))
  {
    alert("Inserire un valore maggiore o uguale a \"1\" e minore o uguale a \"99\" nel campo \"Quantità\".");
    theForm.qty.focus();
    return (false);
  }
  return (true);
}

function mail(user,domain,subject) {
	var compose="mailto:";
	compose+=user;
	compose+="@";
	compose+=domain;
	if (subject) compose+="?subject="+escape(subject);
	self.location.href=compose;
	return false;
}

function noenter(e) {
	var key;
	if (window.event && window.event.keyCode) key=window.event.keyCode;
	else if (e && e.which) key=e.which;
	else key=null;
	return (key != 13);
}

function customUpdate(change) {
	var form=change.form;
	var base=form.name;
	var data=self[base+'data'];
	var tax=parseFloat(form[base+'tax'].value)/100;
	
	// recupero campi
	var price=form[base+'price'];
	var price2=form[base+'price2'];
	var weight=form[base+'weight'];
	var extra=form[base+'extra'];

	var qty=parseFloat(change.value.replace(",","."));
	if (isNaN(qty)) qty=1;
	if (qty == 0) qty=0.5;
	qty=Math.ceil(qty*2)/2;
	var total_price=Math.round((data[0]+qty*data[2])*10000)/10000;
	var total_weight=Math.round((data[1]+qty*data[3])*1000)/1000;

	price.value=total_price;
	price2.value=number_format(total_price*(1+tax), 2, ',', '.');
	weight.value=total_weight;
	extra.value=change.value+data[4];
}

