function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + value +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

var NOME_COOKIE="Order";
function getCookieVal (offsetb) {
	var endstrb = document.cookie.indexOf (";", offsetb);
	if (endstrb == -1)
	endstrb = document.cookie.length;
	return unescape(document.cookie.substring(offsetb, endstrb));
}
function GetCookie (searchcookie) {
	var argb = searchcookie + "=";
	var alenb = argb.length;
	var clenb = document.cookie.length;
	var ib = 0;
	while (ib < clenb) {
		var jb = ib + alenb;
		if (document.cookie.substring(ib, jb) == argb)
		return getCookieVal (jb);
		ib = document.cookie.indexOf(" ", ib) + 1;
		if (ib == 0) break;
	}
	return null;
}
VALORE_COOKIE = GetCookie(NOME_COOKIE);

var string = "Order="+VALORE_COOKIE;

var puntatore = string.lastIndexOf("Order=#");
var lung = string.indexOf("##");

if (puntatore == -1) {
	if (LANG == 0) {
		alert ("Non è possibile comporre un ordine se il carrello è vuoto!\nAggiungere uno o più articoli al carrello prima di comporre l'ordine d'acquisto.");
		window.open('carrello.htm','_top');
	} else {
		alert ("You can\'t compose a purchase order if the shopping cart is empty!\nYou must add one or more items to the shopping cart before trying to compose the purchase order.");
		window.open('cart.htm','_top');
	}	
}

function updateInfo() {
	var VALORE_COOKIE = "ckinfo=#" + document.Mail.TITOLO.selectedIndex + "|" + document.Mail.realname.value + "|" + document.Mail.SOCIETA.value + "|" + document.Mail.email.value + "|" + document.Mail.INDIRIZZO.value + "|" + document.Mail.CAP.value + "|" + document.Mail.CITTA.value + "|" + document.Mail.PROVINCIA.selectedIndex + "|" + document.Mail.STATO.value + "|" + document.Mail.TELEFONO.value + "|" + document.Mail.CELLULARE.value + "|" + document.Mail.FAX.value + "|" + document.Mail.METODO_PAGAMENTO.selectedIndex + "|" + document.Mail.TIPO_SPEDIZIONE.selectedIndex + "|" + document.Mail.VALUTA_CONTEGGIO.selectedIndex + "|" + document.Mail.INDIRIZZO_CONSEGNA.value + "|" + document.Mail.NOTE.value + "##"; 
	var NOME_COOKIE = "ckinfo";
	pathname = location.pathname;
	myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';
	var largeExpDate = new Date ();
	largeExpDate.setTime(largeExpDate.getTime()+(365 * 24 * 3600 * 1000));
	SetCookie(NOME_COOKIE,VALORE_COOKIE,largeExpDate,myDomain);
}

function updateAmount(form) {
	var Prezzoup=0;
	var Qt=0;
	var Sub=0;
	var SubTotale=0;
	for (i=1; i<=cont; i++) {
		Prezzoup=Math.round(eval("form.Prezzo"+i+".value")*100);
		Qt=eval("form.Quant"+i+".value");
		if (isNaN(Qt) || (Qt<0) || (Qt=="")) {Qt=1;
		                eval("form.Quant"+i+".value=1");}
		if (Qt>999) {Qt=999;
					   eval("form.Quant"+i+".value=999");}
		Sub=parseInt(Math.round(Prezzoup*(parseInt(Qt))));
		SubTotale+=Sub;
		eval("form.Totale"+i+".value=parseMoney(Sub,CENTS,true)");
	}

	var spediz=form.TIPO_SPEDIZIONE.selectedIndex+1;
	Prezzoup=spese[spediz];

	form.SPESE_SPEDIZIONE_EURO.value=parseMoney(Prezzoup,CENTS,true);
	form.SPESE_SPEDIZIONE_USD.value=parseMoney(Math.round(Prezzoup*cambio/100),CENTS,true);
	ImpOrdine=SubTotale;
	SubTotale+=Prezzoup;
	form.TOTALE_ORDINE_EURO.value=parseMoney(SubTotale,CENTS,true);
	form.TOTALE_ORDINE_USD.value=parseMoney(Math.round(SubTotale*cambio/100),CENTS,true);
}

function errore(campo) {
   if (LANG == 0) {
   	alert("ATTENZIONE!\nImpossibile inviare l'ordine perchè il campo «"+campo+"»\nnon è stato correttamente valorizzato! Compilare attentamente\ntutti i campi obbligatori, quindi ritentare l'invio dell'ordine.\nI campi obbligatori sono contrassegnati in grassetto.");
   } else {
      	alert("WARNING!\nThe field «"+campo+"» is not correctly filled out, so the\norder can't be sent! Please fill out carefully all the\nmandatory fields, then try again to send the order.\nAll the bold fields are mandatory.");
   }
}


function inviaOrdine() {

	var err="";
	var flag=0;
	
	document.Mail.NOME_COGNOME.value = document.Mail.realname.value;
	var title_text = document.Mail.TITOLO.selectedIndex;
	var name_text = document.Mail.NOME_COGNOME.value;
	var indemail = document.Mail.email.value;
	var addr_text = document.Mail.INDIRIZZO.value;
	var zip_text = document.Mail.CAP.value;
	var city_text = document.Mail.CITTA.value;
	var country_text = document.Mail.STATO.value;
	var alt_address_text = document.Mail.INDIRIZZO_CONSEGNA.value;

	if (title_text == 0 || title_text == null || title_text == "" || title_text == "null") {
		if (LANG == 0) { err = "Titolo personale"; } else { err = "Title"; }
		flag=1;
	}

	if ((name_text == "" || name_text == null || name_text == "null") && (flag==0)) {
		if (LANG == 0) { err = "Nome e cognome"; } else { err = "Name and surname"; }
		flag=1;	
	}

	if ((indemail == "" || indemail == null || indemail == "null" || indemail.lastIndexOf("@") == -1 || indemail.lastIndexOf(".") == -1) && (flag==0)) {
		if (LANG == 0) { err = "Indirizzo e-mail"; } else { err = "E-mail address"; }
		flag=1;
	}
	
	if ((addr_text == "" || addr_text == null || addr_text == "null") && (flag==0)) {
		if (LANG == 0) { err = "Indirizzo Civico"; } else { err = "Civic address"; }
		flag=1;	
	}
	
	if ((zip_text == "" || zip_text == null || zip_text == "null") && (flag==0)) {
		if (LANG == 0) { err = "C.A.P."; } else { err = "ZIP code"; }
		flag=1;	
	}

	if ((city_text == "" || city_text == null || city_text == "null") && (flag==0)) {
		if (LANG == 0) { err = "Città"; } else { err = "City"; }
		flag=1;	
	}
	
	if ((country_text == "" || country_text == null || country_text == "null") && (flag==0)) {
		if (LANG == 0) { err = "Stato"; } else { err = "Country"; }
		flag=1;	
	}

	if (err != "" && flag!=0) {
		errore(err);
	}
	
	var pagam=document.Mail.METODO_PAGAMENTO.selectedIndex+1;
	var spediz=document.Mail.TIPO_SPEDIZIONE.selectedIndex+1;

	warn_flag=0;

	if (spediz=="1" || spediz=="2" || spediz=="3" || spediz=="4") {
		if (cat_flag == 1 && att_flag == 1) {
			if (LANG==0) { warn = "L\'ordine contiene cataloghi e attrezzatura numismatica"; } else { warn = "The order contains both catalogs and numismatic supplies"; }
			warn_flag=1;
		}	
		if (cat_flag == 1 && att_flag == 0) {
			if (LANG==0) { warn = "L\'ordine prevede la spedizione di cataloghi ufficiali"; } else { warn = "The order foresees the mailing of numismatic catalogs"; }
			warn_flag=1;
		}	
		if (cat_flag == 0 && att_flag == 1) {
			if (LANG==0) { warn = "L\'ordine prevede la spedizione di attrezzatura numismatica"; } else { warn = "The order foresees the mailing of numismatic supplies"; }
			warn_flag=1;
		}	
		if (warn_flag==1) {
			if (LANG==0) { alert ("ATTENZIONE!\n"+warn+"\nperciò è necessario selezionare l'imballaggio con \'PACCO\'\n(es. \'Pacco ordinario\' o \'Pacco in contrassegno\')."); } else { alert ("WARNING!\n"+warn+"\nso you must select a packaging with \'PARCEL\'\n(e.g. \'Ordinary parcel\' or \'Cash on delivery parcel\')."); }
			flag=1;
		}
	} else {
		if (cat_flag == 0 && att_flag == 0) {
			if (LANG==0) { alert ("ATTENZIONE!\nL\'ordine non contiene nè cataloghi nè attrezzatura numismatica\nperciò è consigliabile selezionare un imballaggio con \'BUSTA\'\n(es. \'Busta in contrassegno\' o \'Busta raccomandata\')."); } else { alert ("WARNING!\nThe order does not contain neither catalogs nor numismatic supplies\nso you\'d better select a packaging with \'ENVELOPE\'\n(e.g. \'Cash on delivery envelope\' o \'Registered mail envelope\')."); }
			flag=1;
		}
	}

	if (flag == 0) {
		var check_string = new Array("Italy","ITALY","Italia","ITALIA");
		var check_items = 4;
		var check_flagX = 0;
		for (i=0;i<check_items;i++) {
			if (country_text.indexOf(check_string[i])!=-1) { check_flagX = 1; }
			if (alt_address_text.indexOf(check_string[i])!=-1) { check_flagX = 1; }
		}
		// ******* Verifica se la spedizione è congruente con lo stato di appartenenza *******
		if ((LANG != 0) && (spediz!="3") && (spediz!="12") && (spediz!="13") && (check_flagX==0)) {
			alert("WARNING!\nYou\'ve selected the delivery method:\n«"+spedizione[spediz]+"».\nThis delivery method is only for ITALY!\nPlease select another delivery method\nand an appropriate payment method."); 
			flag=1;
		}
		// ******* Verifica la congruenza fra spedizioni in contrassegno e metodi di pagamento ********
		if (((pagam=="2" || pagam=="3" || pagam=="4" || pagam=="5") && (spediz=="1" || spediz=="5" || spediz=="6" || spediz=="10" || spediz=="11")) || ((pagam=="1") && (spediz=="2" || spediz=="3" || spediz=="4" || spediz=="7" || spediz=="8" || spediz=="9" || spediz=="12" || spediz=="13" || spediz=="14" || spediz=="15"))) {
			if (LANG==0) { alert("ATTENZIONE!\nNon è consentito selezionare il metodo di pagamento: «"+pagamento[pagam]+"»\ncon la modalità di spedizione: «"+spedizione[spediz]+"».\nVerificare attentamente la congruenza fra metodo di pagamento e modalità di spedizione."); } else { alert("WARNING!\nYou can\'t select the payment method: «"+pagamento[pagam]+"»\nwith the delivery method: «"+spedizione[spediz]+"».\nPlease verify carefully the congruence between payment method and delivery method."); }
			flag=1;
		}
		// ******* Verifica accettazione delle clausole sulla tutela della privacy ********
		if (document.Mail.PRIVACY_CHECK.checked != true || document.Mail.CONDITIONS_CHECK.checked != true) {
			if (LANG==0) { alert("ATTENZIONE!\nNon è consentito l'invio del modulo d'ordine senza l'esplicita accettazione\ndelle clausole relative alla tutela della privacy e alle condizioni commerciali.\nConfermare entrambe le clausole spuntando le relative caselle per accettazione."); } else { alert("WARNING!\nYou can\'t send the purchase order without having confirmed both\nthe acceptance clauses of privacy statement and trading conditions.\nPlease confirm both the acceptance clauses on checking the related\nfields before sending the order."); }
			flag=1;
		}
	}

	if (flag == 0) {
		
		if (LANG==0) { var choice = confirm("Desidera stampare il modulo d\'ordine prima\ndi procedere con l\'invio dell\'ordine?\n\nPremere «OK» per stampare prima dell\'invio.\nPremere «Annulla» per inviare direttamente l\'ordine."); } else { var choice = confirm("Do you want to print the purchase order form before\nsending it?\n\nClick on «OK» to print the order before the mailing.\nClick on «Cancel» to directly send the order."); }
		
		if (choice == true) {
			window.print();
		}
		
		var x = navigator;
		var now = new Date()
		var user_info = "";
		
		user_info = "Tipo di processore: "+x.cpuClass+" | Piattaforma di sistema: "+x.platform+" | Lingua: "+x.systemLanguage+" | Online: "+x.onLine+" | Data di sistema: "+now+" | Risoluzione video: "+screen.width+"x"+screen.height+" pixels | Zona visualizzabile: "+screen.availWidth+"x"+screen.availHeight+" pixels | Profondità colore: "+screen.colorDepth+" bit | Browser: "+x.appName+" | Versione: "+x.appVersion+" | Agente: "+x.userAgent+" | Lingua browser: "+x.browserLanguage+" | Cookies-enabled: "+x.cookieEnabled+" | Proveniente da: ["+document.referrer+"]."
		
		document.Mail.CODICE_ORDINE.value = ordercode;
		document.Mail.INFO_UTENTE.value = user_info;
		
		updateInfo();

		document.Mail.submit();
	}
}

