/********************************************************
richiede conferma su operazioni varie (cancellazione etc.)
*********************************************************/
function confirmLink(theLink, msg) {

	var is_confirmed = confirm(msg);
	if (is_confirmed) {
		theLink.href
	}

	return is_confirmed;
}

/******************************************************************
incrementa il valore quantita' di 1 
id   - id del campo di input della quantita'
*******************************************************************/
function aggiungi(id) {
   document.getElementById(id).value++;
}


/******************************************************************
decrementa il valore quantita' di 1 
id   - id del campo di input della quantita'
*******************************************************************/
function togli(id) {
   if (document.getElementById(id).value>0) {
      document.getElementById(id).value--;
   }
}

/**************************************************************************
controlla che nel carrello non venga inserita quantita' pari a zero
id  - id del campo di input della quantita'
v   - valore originale del campo
msg - messaggio di alert
***************************************************************************/
function ctrlQntZero(id,v,msg) {
   if (parseInt(document.getElementById(id).value) <=0 ) {
      alert(msg);
      document.getElementById(id).focus();
      document.getElementById(id).value=v;
      return false;
   }
}


/*********************************************************************
verifica che sia stata selezionata una modalità di pagamento e
msg - messaggio di avviso per modalità di pagamento
prMinOrdItalia - importo minimo ordine italia
prMinOrdEstero - importo minimo ordine estero
totCart - importo totale carrello
avvNoMinOrd - messaggio di avviso di non superata soglia minima ordine
avvCivico - messaggio di avviso di non specificato numero civico
**********************************************************************/
function checkTipoPagamento(msg,prMinOrdItalia,prMinOrdEstero,totCart,avvNoMinOrd,avvCivico) {

    if (document.getElementById('contrassegno').checked==false && document.getElementById('cartaDiCredito').checked==false && document.getElementById('paypal').checked==false) {
       alert(msg);
       return false;
    } else {
      // tipo pagamento spedificato
      // controlla se il mimimo ordine e' raggiunto in base allo stato
      var statoAct=document.getElementById('selectState').value;
      if (statoAct.toUpperCase()=='ITALY')
         minOrder=prMinOrdItalia;
      else
         minOrder=prMinOrdEstero;

      if (parseInt(totCart) < parseInt(minOrder)) {
	 alert(avvNoMinOrd);
	 return false;
      }

      // controlla se il numero civico e' stato specificato
      var numCivico=document.getElementById('numCivico').value;
      if (numCivico == '') {
	 alert(avvCivico);
	 return false;
      }

      return true;
    }

}

/****************************************************************************
selezione stato ITALY o OTHER
- se selezionato Italia riempie il campo con ITALY e lo setta readonly=true 
- se selezionato Other vuota il campo e vi setta il focus (readonly=false)
****************************************************************************/
function stateSelect() {
   if (document.getElementById('stateItaly').checked==true) {
      document.getElementById('stateDesc').value='';
      document.getElementById('stateDesc').readOnly=true;
    } else {
      document.getElementById('stateDesc').value='';
      document.getElementById('stateDesc').readOnly=false;
      document.getElementById('stateDesc').focus();
    }
}


/****************************************************************************
_____ NON USATA AL MOMENTO ____ CONTROLLO A LIVELLO DI FORM
verifica che il campo stato del form di conferma ordine non sia vuoto
- se vuoto visualizza un alert
****************************************************************************/
function checkState() {
   if (document.getElementById('stateDesc').value=='') {
      alert('vuoto');
      document.getElementById('stateDesc').focus();
      return false;
    } else {
      return true;
    }
}

/******************************************************************
costruisce l'indirizzo mail 
sDom - dominio
sUser - utente 
*******************************************************************/
function buildM(sDom, sUser){
  return("mail"+"to:"+sUser+"@"+sDom.replace(/%23/g,"."));
}

// Funzione chiamata prima del submit del form di registrazione/modifica profilo
//restituisce false se il form non dev'essere inviato
function checkSubmitProfilo(i18clausPrivacy,i18clausPubb)
{ if (document.getElementById('privacy')!=undefined && document.getElementById('privacy').checked!=true)
  { alert(i18clausPrivacy); 
    return false;
  }
  else
	if (document.getElementById('pubbS').checked!=true && document.getElementById('pubbN').checked!=true)
	{ alert(i18clausPubb);
	  return false;
	}
  return true
}


/***************************************************************************
setta i valori relativi alle spese di spedizione nel form se viene cambiato
lo stato di destinazione da Itali a Estero o viceversa

parametri:
prSpedItalia - prezzo spedizione italia
prSpedEstero - prezzo spedizione estero
sogliaNoCostItalia - soglia no costo spedizione italia
sogliaNoCostEstero - soglia no costo spedizione estero
prSpedRegaloItalia - prezzo spedizione regalo italia
prSpedRegaloEstero - prezzo spedizionne regalo estero
contrassItalia - prezzo contrassegno italia
contrassEstero - prezzo contrassegno estero
prMinOrdItalia - importo minimo ordine italia
prMinOrdEstero - importo minimo ordine estero
totCart - importo carrello
****************************************************************************/
function setSpeditionCost(prSpedItalia,prSpedEstero,sogliaNoCostItalia,sogliaNoCostEstero,prSpedRegaloItalia,prSpedRegaloEstero,contrassItalia,contrassEstero,prMinOrdItalia,prMinOrdEstero,totCart) {

 var statoAct=document.getElementById('selectState').value;
 // setta i costi in base allo stato: Italy o Estero
 if (statoAct.toUpperCase()=='ITALY') {
    // se Italia
    minOrder=prMinOrdItalia;
    // setta i costi di spedizione in base al totale carrello
    if (parseInt(totCart) < parseInt(sogliaNoCostItalia)) {
      document.getElementById('prSpedVis').innerHTML=prSpedItalia;
      document.getElementById('prSpedValue').value=prSpedItalia;
      document.getElementById('prRegaloVis').innerHTML=prSpedRegaloItalia;
      document.getElementById('prRegaloValue').value=prSpedRegaloItalia;
    } else {
      document.getElementById('prSpedVis').innerHTML="0";
      document.getElementById('prSpedValue').value="0";
      document.getElementById('prRegaloVis').innerHTML="0";
      document.getElementById('prRegaloValue').value="0";
    }
    // setta il costo del contrassegno
    document.getElementById('contrassVis').innerHTML=contrassItalia;
    document.getElementById('contrassValue').value=contrassItalia;
    document.getElementById('contrassField').style.display="block";

 } else {
    // se Estero
    minOrder=prMinOrdEstero;
    // setta i costi di spedizione in base al totale carrello
    if (parseInt(totCart) < parseInt(sogliaNoCostEstero)) {
      document.getElementById('prSpedVis').innerHTML=prSpedEstero;
      document.getElementById('prSpedValue').value=prSpedEstero;
      document.getElementById('prRegaloVis').innerHTML=prSpedRegaloEstero;
      document.getElementById('prRegaloValue').value=prSpedRegaloEstero;
    } else {
      document.getElementById('prSpedVis').innerHTML="0";
      document.getElementById('prSpedValue').value="0";
      document.getElementById('prRegaloVis').innerHTML="0";
      document.getElementById('prRegaloValue').value="0";
    }
    // setta il costo del contrassegno
    document.getElementById('contrassVis').innerHTML=contrassEstero;
    document.getElementById('contrassValue').value=contrassEstero;
    document.getElementById('contrassField').style.display="none";
    document.getElementById('contrassegno').checked=false;
 }

 // controlla che il carrello sia superiore al minimo d'ordine
 if (parseInt(totCart) < parseInt(minOrder)) {
    document.getElementById('contAvviso').style.display="block";
 } else {
    document.getElementById('contAvviso').style.display="none";
 }

}


/***************************************************************************
Valida l'indirizzo email inserito per l'iscrizione alla newsletter

parametri:
emailStr - indirizzo email da validare
msg - messaggio d'errore visualizzato
****************************************************************************/
function emailCheck (emailStr,msg) {

var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
	alert(msg)
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

if (user.match(userPat)==null) {
    alert(msg)
    return false
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert(msg)
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert(msg)
    return false
}

var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>4) {
   alert(msg)
   return false
}

if (len<2) {
   var errStr="This address is missing a hostname!"
   alert(msg)
   return false
}

return true;
}
