//funzione inviaordine
function TrimJS(StrToTrim)
{
    // CONTROLLA CHE IL VALORE IN INPUT SIA DI TIPO STRING
    if (typeof StrToTrim != "string")
    {
        return StrToTrim;
    }

    // CATTURA IL PRIMO CARATTERE DELLA STRINGA PER CONTROLLARE CHE NON SIA UNO SPAZIO VUOTO
    var StrBlank = StrToTrim.substring(0, 1);

    // ELIMINA LO SPAZIO VUOTO DALLA PRIMA POSIZIONE DELLA STRINGA
    while (StrBlank == " ")
    {
        StrToTrim = StrToTrim.substring(1, StrToTrim.length);
        StrBlank = StrToTrim.substring(0, 1);
    }

    // CATTURA L'ULTIMO CARATTERE DELLA STRINGA PER CONTROLLARE CHE NON SIA UNO SPAZIO VUOTO
    StrBlank = StrToTrim.substring(StrToTrim.length - 1, StrToTrim.length);

    // ELIMINA LO SPAZIO VUOTO DALL'ULTIMA POSIZIONE DELLA STRINGA
    while (StrBlank == " ")
    {
        StrToTrim = StrToTrim.substring(0, StrToTrim.length-1);
        StrBlank = StrToTrim.substring(StrToTrim.length-1, StrToTrim.length);
    }

    // ELIMINA POTENZIALI SPAZI VUOTI MULTIPLI ALL'INIZIO ED ALLA FINE DI UNA STRINGA
    while (StrToTrim.indexOf("  ") != -1)
    {
        StrToTrim = StrToTrim.substring(0, StrToTrim.indexOf("  "));
        StrToTrim += StrToTrim.substring(StrToTrim.indexOf("  ") + 1, StrToTrim.length);
    }

    // RESTITUISCE IL VALORE FINALE SENZA SPAZI VUOTI DI CONTORNO
    return StrToTrim;
}
function len(string)
{
    if (string == null)
        return (false);
    
    return String(string).length;
}

function InviaOrdine_DEMO_3(){
	var ControlliManc= ""
	mancanti=''
	errore="0"
	anaclick=TrimJS(document.frmordine_demo.txtAna.value)
	
	if (TrimJS(document.frmordine_demo.txtNomeSCAR.value)==''){
		errore="1"
	}
	if (len(document.frmordine_demo.txtNomeSCAR.value)<3){
		errore="3"
	}
	if (TrimJS(document.frmordine_demo.txtMailSCAR.value)==''){
		errore="1"
	}
	
	var stringa = document.frmordine_demo.txtMailSCAR.value;
	 if (stringa.indexOf("@" && ".") > 0) {
	 }
	 else
	 {
		errore="2"
	 }
	
	if (errore=="2"){
		alert("Inserire un indirizzo e-mail corretto.")
	}
	else
	{
		if (errore=="1") {
			alert("Compilare i campi mancanti per procedere al download")
		}
		else{
				if (errore=="3") {
					alert("Il nome deve essere lungo almeno 3 caratteri")
				}
				else{
					
					if (confirm("All'indirizzo email da Lei specificato riceverā il link per lo scarico.\nL'indirizzo e-mail fornito č corretto?")){
					
						window.open('http://www.bagiacchi.it/Contribuenti_minimi_Avvenuta_Richiesta_2.asp','DownloadMinimi','width=600,height=370,toolbar=0, location=0 ,directories=0,status=0,menubar=0,resizable=0,scrollbars=0')
						window.open('http://www.bagiacchi.it/Download_minimi.asp?p_prov=si1&scarica_demo=1&nom='+TrimJS(document.frmordine_demo.txtNomeSCAR.value)+'&mai='+TrimJS(document.frmordine_demo.txtMailSCAR.value)+'&IdAnaClick='+anaclick,'DownloadMinimi','width=600,height=370,toolbar=0, location=0 ,directories=0,status=0,menubar=0,resizable=0,scrollbars=0')
					}

				}
		}
	}
}
function InviaOrdine_DEMO(){
	var ControlliManc= ""
	mancanti=''
	errore="0"
	anaclick=TrimJS(document.frmordine_demo.txtAna.value)
	
	if (TrimJS(document.frmordine_demo.txtNomeSCAR.value)==''){
		errore="1"
	}
	if (len(document.frmordine_demo.txtNomeSCAR.value)<3){
		errore="3"
	}
	if (TrimJS(document.frmordine_demo.txtMailSCAR.value)==''){
		errore="1"
	}
	
	var stringa = document.frmordine_demo.txtMailSCAR.value;
	 if (stringa.indexOf("@" && ".") > 0) {
	 }
	 else
	 {
		errore="2"
	 }
	
	if (errore=="2"){
		alert("Inserire un indirizzo e-mail corretto.")
	}
	else
	{
		if (errore=="1") {
			alert("Compilare i campi mancanti per procedere al download")
		}
		else{
				if (errore=="3") {
					alert("Il nome deve essere lungo almeno 3 caratteri")
				}
				else{
						window.open("http://www.bagiacchi.it/Download/Contabilitā_dei_minimi_2010_base.xls")
						window.open('http://www.bagiacchi.it/Download_minimi.asp?p_prov=si1&scarica_demo=1&nom='+TrimJS(document.frmordine_demo.txtNomeSCAR.value)+'&mai='+TrimJS(document.frmordine_demo.txtMailSCAR.value)+'&IdAnaClick='+anaclick,'DownloadMinimi','width=600,height=370,toolbar=0, location=0 ,directories=0,status=0,menubar=0,resizable=0,scrollbars=0')

				}
		}
	}
}

function InviaOrdine_Download_Minimi() {
	var ControlliManc= ""
	mancanti=''
	errore=0
/*	if ((!frmordine.chkant141.checked)){
		mancanti+='Selezione applicazione;'
	}
*/	
	if (TrimJS(document.frmordine.txtDES_DEN_UTE.value)==''){
		ControlliManc+='Ragione Sociale;\n'
	}
	if (TrimJS(document.frmordine.txtDES_PER_CON.value)==''){
		ControlliManc+='Persona da contattare;\n'
	}
	if (TrimJS(document.frmordine.txtDES_IND.value)==''){
		ControlliManc+='Via;\n'
	}
	if (TrimJS(document.frmordine.txtDES_CAP.value)==''){
		ControlliManc+='CAP;\n'
	}		
	if (TrimJS(document.frmordine.txtDES_LOC.value)==''){
		ControlliManc+='Cittā;\n'
	}
	if (TrimJS(document.frmordine.txtDES_PRV.value)==''){
		ControlliManc+='Provincia;\n'
	}
	if (TrimJS(document.frmordine.txtDES_TEL.value)==''){
		ControlliManc+='Telefono;\n'
	}else{
			var stringa = document.frmordine.txtDES_TEL.value;
	 		var PrimNum = stringa.charAt(0);
			if (PrimNum == '0') {
			}
		 	else
		 	{
			errore=1
			ControlliManc+='numeri di telefono fisso;\n'
		 	}
	}
	if (TrimJS(document.frmordine.txtDES_IND_EML.value)==''){
		ControlliManc+='Email;'
	}
	else{
		var stringa = document.frmordine.txtDES_IND_EML.value;
		 if (stringa.indexOf("@" && ".") > 0) {
		 }
		 else
		 {
			errore=1
			ControlliManc+='Indirizzo mail corretto;\n'
		 }
	}
    if (TrimJS(document.frmordine.txtDES_COD_FIS.value)=='') {
		ControlliManc+='Codice_fiscale;\n'
	}
	//if (TrimJS(document.frmordine.txtDES_PAR_IVA.value)=='') {
	//	ControlliManc+='Partita Iva;'
	//}
	
	//**************************************
    //   Controllo partita iva
	//**************************************
	var u_ok=0
	var r=0
	var strpiva=document.frmordine.txtDES_PAR_IVA.value
	
	u_ok = Math.floor(strpiva.charAt(0)) + Math.floor(strpiva.charAt(2)) + Math.floor(strpiva.charAt(4)) + Math.floor(strpiva.charAt(6)) + Math.floor(strpiva.charAt(8))
	for (var i = 1;i <= 9;i += 2) {
		r =  Math.floor(strpiva.charAt(i)) * 2
		u_ok += Math.floor(r / 10) + (r - Math.floor(r / 10) * 10)
	}
	var u_oktmp=u_ok
	u_ok = 10 - (u_ok - Math.floor(u_ok / 10) * 10)
	if (u_ok >= 10) {
		u_ok -= 10
	}

	//alert(u_ok)
	errpiva=0
	if (u_ok != Math.floor(strpiva.charAt(10)))  {
		//alert('Partita IVA errata')
		errpiva=1
	}
	if (strpiva=='07946271009') {
		//alert('Partita IVA errata')
		errpiva=2
	}
	if (strpiva.length != 11) {
		//alert('Partita IVA errata')
		errpiva=3
	}
	if (strpiva=='00000000000') {
		//alert('Partita IVA errata')
		errpiva=4
	}
	if (strpiva=='19050') {
		//alert('Partita IVA errata')
		errpiva=0
	}
	
	if (errpiva==0) 
		{
		if (mancanti!='') {
			strtmp='Campi Obbligatori Mancanti: ';
			campi = mancanti.split(';')
			i=0;
			while (i<campi.length) {
				if (i>0 && campi[i]!='') {
					strtmp=strtmp+' - ';
				}
				strtmp=strtmp+campi[i];
				
				i++;
			}
			alert(strtmp);
			errore=1
		}
		
		errore=errore+'';		
		
		}
	else
		{
		ControlliManc=ControlliManc+'Partita IVA corretta\n'
		//alert('Partita IVA errata')
		errore=1
		}
	//**************************************
    //   fine Controllo partita iva
	//**************************************
	//**************************************
    //   Controllo p.iva e cod.fisc nel campo cod.fisc
	//**************************************
	cod = document.frmordine.txtDES_COD_FIS.value;
	document.frmordine.txtDES_COD_FIS.focus();

	if( cod == '' )
		err = "hai lasciato in bianco il campo!\n";
	else if( cod.length == 16 )
		err = ControllaCF(cod);
	else if( cod.length == 11 )
		err = ControllaPIVA(cod);
	else
		err = "Il codice introdotto non č valido.";

	if( err > '' ){
		//alert("VALORE ERRATO\n\n" + err + "\nCorreggi e riprova!");
		errore=1;
		}	
	//*****************************************************************
    //   FINE Controllo partita iva e cod.fiscale nel campo cod. fiscale
	//*****************************************************************
	var mess = "Da studio Bagiacchi - Ha richiesto l'acquisto del software Contribuenti minimi "	
 	document.frmordine.txtDES_TESTO.value=mess;
 	document.frmordine.txtDES_TESTOProd.value=mess;
	
	//*****************************************************************
    //   FINE RIPORTO TESTO SULLE NOTE DELL'APPLICAZIONE DESIDERATA
	//*****************************************************************
	
	if (errore=='0') {
		// NON C'č NESSUN ERRORE
		document.frmordine.azione.value="Invia";
		document.frmordine.submit();
	}else {
		if (ControlliManc!="") {
			alert("Campi Obbligatori Mancanti. Inserire:\n\n" + ControlliManc );
		}
	}
}

//fine funzione inviaordine

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";
		//errore=1
	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";
				//errore=1
	}
	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";
			//errore=1;
	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";
			//errore=1
	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";
				//errore=1
	}
	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";
			//errore=1
	return '';	

}
