function singleQuoteReplace(inputBox) {
	inputBox.value = inputBox.value.replace(/'/g,"`");
	return;
}
function doubleQuoteReplace(inputBox) {
	inputBox.value = inputBox.value.replace(/"/g,"`");
	return;
}

function trim(strText) { 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);
   return strText;
}

function isdata(stringa) { 
  var data = stringa; 
  if (data.length > 0) { 
    var ok = true; 
    var first = false; 
    var second = false; 
    var g = 0; 
    var m = 0; 
    var giorno; 
    var mese; 
    var anno; 
    for(i=0;i<data.length;i++) { 
      if (data.charAt(i) != '/' && (data.charAt(i) < '0') || (data.charAt(i) > '9')) ok = false; 
    } 
    if (!ok){
      return false;
    }
    for(i=0;i<data.length;i++) { 
      if (data.charAt(i) == '/' && !first) {; 
        first = true;
        g = i;
      } 
      else if (data.charAt(i) == '/' && first) { 
        second = true;
        m = i;
      } 
    } 
    if (!first || !second) ok = false; 
    if (!ok){
      return false;
    }
    giorno = data.substring(0,g); 
    mese   = data.substring(g+1,m); 
    anno   = data.substring(m+1,data.length); 
    if (anno < 1000) ok = false;     
    if (mese < 1 || mese > 12) ok = false;     
    if (giorno < 1 || giorno > 31) ok = false; 
    if (giorno > 30 && mese == 4) ok = false; 
    if (giorno > 30 && mese == 6) ok = false; 
    if (giorno > 30 && mese == 9) ok = false; 
    if (giorno > 30 && mese == 11) ok = false; 
    if (giorno > 29 && mese == 2) ok = false; 
    if (giorno == 29 && mese == 2) {
      if (anno % 4 == 0) {
        if (anno % 100 == 0) { 
          if (anno % 400 != 0) ok = false; 
        }
      }
      else ok = false;
    } 
    if (!ok){
      return false;
    }
  }
  return true;
}

function is_email(p) {
     	var reEmail = /^.+\@.+\..+$/;
	if (!reEmail.test(p)) {
          	alert ("Attenzione! L'indirizzo email inserito non è un indirizzo e-mail valido");
          	return false;
     	}
     	return true;
}

function isNumericValue(p) {
		var str = new String(p);
 		for (var i = 0; i < str.length; i++) {
			var ch = str.substring(i, i + 1);
			if(ch < "0" || "9" < ch) {
				return false;
			}
		}
		return true;
}

/*
funzione per chekkare(true) o schekkare(false) un elenco di checkbox contenuti in un form.
*/
function setCheckboxes(the_form, name_check, do_check)
{
    var elts      = (typeof(document.forms[the_form].elements[name_check]) != 'undefined')
                  ? document.forms[the_form].elements[name_check]
                  : document.forms[the_form].elements[name_check];
    var elts_cnt  = (typeof(elts.length) != 'undefined')
                  ? elts.length
                  : 0;

    if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
            elts[i].checked = do_check;
        } // end for
    } else {
        elts.checked = do_check;
    } // end if...else

    return true;
}

function refresh_page(nome_pagina) {
    	//document.forms[0].flag_reload.value = 1;
    	document.forms[0].action = nome_pagina;
    	document.forms[0].submit();
}

function refresh_page_ordini_admin(nome_pagina) {
    	//document.forms[0].flag_reload.value = 1;
    	document.form1.action = nome_pagina;
    	document.form1.submit();
}

function refresh_page_utenti_admin(nome_pagina) {
    	//document.forms[0].flag_reload.value = 1;
    	document.forms[1].action = nome_pagina;
    	document.forms[1].submit();
}

function apri_popup(f) {
		
		var w = 600;
		var h = 450;
		
		//var w = screen.Width;
		//var h = screen.Height;
		var left = (screen.availWidth / 2)  - (w / 2);
		var top = (screen.availHeight / 2)  - (h / 2);
		
		var popup = window.open(f,'','location=no,menubar=no,status=no,toolbar=no,width='+w+',height='+h+',top='+top+',left='+left);
		
		popup.focus();
}

function ControllaCF(cf)
{
    var validi, i, s, set1, set2, setpari, setdisp;
    if( cf == '' )  return '';
    cf = cf.toUpperCase();
    if( cf.length != 16 )
        return "La lunghezza del codice fiscale non è\n"
        +"corretta: il codice fiscale dovrebbe essere lungo\n"
        +"esattamente 16 caratteri.\n";
    validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for( i = 0; i < 16; i++ ){
        if( validi.indexOf( cf.charAt(i) ) == -1 )
            return "Il codice fiscale contiene un carattere non valido `" +
                cf.charAt(i) +
                "'.\nI caratteri validi sono le lettere e le cifre.\n";
    }
    set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
    s = 0;
    for( i = 1; i <= 13; i += 2 )
        s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    for( i = 0; i <= 14; i += 2 )
        s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
        return "Il codice fiscale non è corretto:\n"+
            "il codice di controllo non corrisponde.\n";
    return "";
}


function ControllaPIVA(pi)
{
	if( pi == '' )  return '';
	
	if( pi.length != 11 )
		return "La lunghezza della partita IVA non è\n" +
			"corretta: la partita IVA dovrebbe essere lunga\n" +
			"esattamente 11 caratteri.\n";
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
			return "La partita IVA contiene un carattere non valido `" +
				pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
		return "La partita IVA non è valida:\n" +
			"il codice di controllo non corrisponde.\n";
	return '';
}

function inserisci_indirizzo_spedizione(input_form) {
//funzione utilizzata nell'iscrizione degli utenti
//per ricopiare l'indirizzo domiciliare in quello per la spedizione della merce.

var i = input_form.indirizzo.value;
var ca = input_form.cap.value;
var co = input_form.comune.value;
var p = input_form.provincia.value;

	if (input_form.copia_indirizzi.checked) {
		input_form.indirizzo2.value = i;
		input_form.cap2.value = ca;
		input_form.comune2.value = co;
		input_form.provincia2.value = p;		
	} else {
		input_form.indirizzo2.value = "";
		input_form.cap2.value = "";
		input_form.comune2.value = "";
		input_form.provincia2.value = "";		
	}
	
 }
