	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
	Number.prototype.roundTo = roundTo; 
	
	function EnableFields(product){
		
		switch (parseInt(product))
		{
			case 1:	// RETAIL
				for (i=0; i<=document.frmProduct.retail.length -1; i++){
					if(i!=2) document.frmProduct.retail[i].disabled = false;
				}
				document.frmProduct.retail_qta_license.disabled = false;
				document.frmProduct.retail_qta_maintenance.disabled = false;
				DisableFields(2)
				break;	

			case 2:	// EDUCATIONAL
				for (i=0; i<=document.frmProduct.educational.length -1; i++){
					if(i!=2) document.frmProduct.educational[i].disabled = false;
				}
				document.frmProduct.educational_qta_license.disabled = false;
				document.frmProduct.educational_qta_maintenance.disabled = false;
				DisableFields(1)
				break;
							
			default:
		}
	}
	
	
	function DisableFields(product){
		
		switch (parseInt(product))
		{
			case 1:	// RETAIL
				for (i=0; i<=document.frmProduct.retail.length -1; i++){
					document.frmProduct.retail[i].disabled = true;
					document.frmProduct.retail[i].checked = false;
				}
				
				document.frmProduct.retail_qta_license.value = "0";
				document.frmProduct.retail_qta_add_license.selectedIndex=0;
				document.frmProduct.retail_qta_maintenance.value = "0";
				document.frmProduct.retail_qta_upgrade.selectedIndex=0
				document.frmProduct.vat.value = "0";
				document.frmProduct.totalamount.value = "0";
				document.frmProduct.retail_totalamount.value = "0";
				
				document.frmProduct.retail_qta_license.disabled = true;
				document.frmProduct.retail_qta_add_license.disabled = true;
				document.frmProduct.retail_qta_maintenance.disabled = true;
				document.frmProduct.retail_qta_upgrade.disabled = true;
				
				// empty cart
				document.getElementById("CustomFields").innerHTML = ""; 
				break;	

			case 2:	// EDUCATIONAL
				for (i=0; i<=document.frmProduct.educational.length -1; i++){
					document.frmProduct.educational[i].disabled = true;
					document.frmProduct.educational[i].checked = false;
				}
				
				document.frmProduct.educational_qta_license.value = "0";
				document.frmProduct.educational_qta_add_license.selectedIndex=0;
				document.frmProduct.educational_qta_maintenance.value = "0";
				document.frmProduct.educational_qta_upgrade.selectedIndex=0
				document.frmProduct.vat.value = "0";
				document.frmProduct.totalamount.value = "0";
				document.frmProduct.educational_totalamount.value = "0";
				
				document.frmProduct.educational_qta_license.disabled = true;
				document.frmProduct.educational_qta_add_license.disabled = true;
				document.frmProduct.educational_qta_maintenance.disabled = true;
				document.frmProduct.educational_qta_upgrade.disabled = true;

				// empty cart
				document.getElementById("CustomFields").innerHTML = ""; 
				break;	
							
			default:
		}
	}
	
	
	function DoPayment(lc)
	{
		try{
			var custom;
			var shipping;
			var custom_msg = "";
			
			var iTotalAmount = 0;
			
			var msg_partita_iva  = "Inserire la partita iva.";
			var msg_consegna     = "Selezionare la modalit\u00E0 di consegna del prodotto.";
			var msg_invio_ordine = "Procedere con l'invio dell'ordine ?";
			var msg_no_product   = "Nessun prodotto selezionato.";
			var msg_servizio_non_disponibile = "Servizio momentaneamente non disponibile.";
			
			if(lc!="IT"){
				msg_partita_iva  = "Please insert a VAT position.";
				msg_consegna     = "Please select delivery mode.";
				msg_invio_ordine = "Proceed with the dispatch of order ?";
				msg_no_product   = "No products selected.";
				msg_servizio_non_disponibile = "Service not available.";	
			}
			
			// check if cart is not empty
			if(document.frmProduct.product[0].checked){
				iTotalAmount = parseFloat(document.frmProduct.retail_totalamount.value);
			}else if(document.frmProduct.product[1].checked){
				iTotalAmount = parseFloat(document.frmProduct.educational_totalamount.value);
			}
			
			if(iTotalAmount==0){
				alert(msg_no_product);
				return;
			} 	
			
			// check invoice information
			if(lc!="IT"){ // outside Italy
				if (document.frmProduct.cee.checked) {
					custom_msg += document.frmProduct.cee.value + "|";
				}
			}else{ // Italy
				if(document.frmProduct.txt_codfiscale.value.trim().length == 0){
					alert("Il CODICE FISCALE \u00E8 obbligatotorio.");
					document.frmProduct.txt_codfiscale.focus();
					return;
				}else{
					custom_msg += "Codice Fiscale: " + document.frmProduct.txt_codfiscale.value.toUpperCase() + "|";
				}
			}
			
			if (document.frmProduct.piva.checked) {
				if(document.frmProduct.txt_piva.value.trim().length == 0){
					alert(msg_partita_iva);
					document.frmProduct.txt_piva.focus();
					return;
				}else{
					custom_msg += document.frmProduct.piva.value + document.frmProduct.txt_piva.value.toUpperCase() + "|";
				}
			}
			
			// shipping tax (if first item is selected)
			if(document.frmProduct.preferences[0].checked){
				shipping = "<input type=\"hidden\" name=\"shipping_1\" value=\"" + document.paypal.paypal_shipping_tax.value + "\">";
			}
			
			// delivery preferences (description of delivery mode)
			var _selected = false;
			for (i=0; i<=document.frmProduct.preferences.length -1; i++){
				if (document.frmProduct.preferences[i].checked) {
					custom_msg += document.frmProduct.preferences[i].value + "|";
					_selected = true;
				}
			}
			
			// no delivery mode was selected
			if(!_selected){
				alert(msg_consegna);
				return;
			}
				
			// build custom field with message for the seller
			custom = "<input type=\"hidden\" name=\"custom\" value=\"" + custom_msg + "\">";;

			// add shipping info and custom message
			document.getElementById("CustomFields").innerHTML += shipping;
			document.getElementById("CustomFields").innerHTML += custom; 
			
			
			// set VAT value
			document.paypal.tax_cart.value = document.frmProduct.vat.value;
			
						
			// send order
			if(confirm(msg_invio_ordine)){	
				document.paypal.submit();
			}
			
		}catch(e){
			alert(msg_servizio_non_disponibile);	
		}
	}
	
	
	function IsNumeric(sText)
	{
		var ValidChars = "0123456789";
		var IsNumber = true;
		var Char;
		 
		for (i = 0; i < sText.length && IsNumber == true; i++) 
		{ 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) 
			{
				IsNumber = false;
			}
		}
	 
		return IsNumber;
	}

	
	function roundTo(decimalpositions)
	{
	    var i = this * Math.pow(10,decimalpositions);
	    i = Math.round(i);
	    return i / Math.pow(10,decimalpositions);
	}
		
		
	function CalcolaIVA(prezzo, aliquota) {
		var iva = 0;
		var prezzo_con_iva = 0;
		
	    if (aliquota == 0) return;
	    
	    aliquota = parseFloat(aliquota);
	    prezzo = parseFloat(prezzo);
	    
	    if (/^\d+(\.\d{1,2})?$/.test(prezzo)) {
	        prezzo_con_iva = (Math.round(prezzo * (1 + aliquota) * 100)) / 100;
	        iva = prezzo_con_iva - prezzo;
	    }

	    //return prezzo_con_iva;
	    return iva.roundTo(2);
	}
	
	function AddRemoveVAT(){
		var iTotalAmount = 0;
		
		if(document.frmProduct.product[0].checked){
			iTotalAmount = parseFloat(document.frmProduct.retail_totalamount.value);
		}else{
			iTotalAmount = parseFloat(document.frmProduct.educational_totalamount.value);
		}
		
		if (document.frmProduct.cee.checked || document.frmProduct.piva.checked){
			document.frmProduct.vat.value = parseFloat(0);	
			document.frmProduct.totalamount.value = iTotalAmount.roundTo(2);	
		}else{
			var iva = CalcolaIVA(iTotalAmount, document.paypal.paypal_sales_tax.value);
			document.frmProduct.vat.value = iva;
			
			iTotalAmount += iva;
			document.frmProduct.totalamount.value = iTotalAmount.roundTo(2);
		}
	}