function pop_conditions(relative_ancor) {

	url = "salesconditions.html" ;
	if (typeof relative_ancor != "undefined") {
		url += "#"+relative_ancor ;
	}

	wref = window.open (url, "salescond", "height=600,width=580,scrollbars=1") ;
	wref.focus() ;
	
}


function startSendOrder() {
	
	var proceed = true ;
	
	while (proceed) {
	
		// verifica quantitą
		if (!(parseInt(document.forms["main_form"].elements["qta_0"].value,10) + parseInt(document.forms["main_form"].elements["qta_1"].value,10) >= 1)) {
	
			alert ("ordering nothing ?") ;
			proceed = false ;
			break ;
		
		} 
		
		// verifica codice fiscale utenti IT
		if ( parseInt(document.forms["main_form"].elements["regmode"].value,10)==1 && ! (chk_cf(document.forms["main_form"].elements["fat_cfit"].value) || chk_piva(document.forms["main_form"].elements["fat_cfit"].value))) {
		
			alert ("verificare il codice fiscale ?") ;
			proceed = false ;
			break ;
		
		} 

		// verifica Partita IVA inserita utenti IT
		if ( parseInt(document.forms["main_form"].elements["regmode"].value,10)==1 && document.forms["main_form"].elements["fat_ivait"].value.length > 0 && ! chk_piva(document.forms["main_form"].elements["fat_ivait"].value) ) {
			
			alert ("verificare la partita iva ?")  ;
			proceed = false ;
			break ;
			
		} 
		
		// verifica VAT number Utenti EU 
		if ( parseInt(document.forms["main_form"].elements["regmode"].value,10)==2 && ! document.forms["main_form"].elements["fat_ivaeu"].value.length > 0 ) {
			
			alert ("check your EU VAT number ?")  ;
			proceed = false ;
			break ;
			
		}

		// campi obbligatori fatturazione 
		if ( document.forms["main_form"].elements["fat_nome"].value.length == 0  || document.forms["main_form"].elements["fat_indirizzo"].value.length == 0 || document.forms["main_form"].elements["fat_citta"].value.length == 0 || document.forms["main_form"].elements["fat_provincia"].value.length == 0 || document.forms["main_form"].elements["fat_nazione"].value.length == 0  || document.forms["main_form"].elements["fat_cap"].value.length == 0 || document.forms["main_form"].elements["fat_telefono"].value.length == 0 || document.forms["main_form"].elements["fat_email"].value.length == 0 ) {
		
			alert ("check required fields in invoice details ?")  ;
			proceed = false ;
			break ;
		
		}
		
		// email 
		if (!chk_mail (document.forms["main_form"].elements["fat_email"].value)) {
		
			alert ("check your mail ?")  ;
			proceed = false ;
			break ;
		
		}
		
		// campi obbligatori spedizione 
		if (( document.forms["main_form"].elements["gift"].checked || ! document.forms["main_form"].elements["multiconsegna"].checked) && ( document.forms["main_form"].elements["inv_nome"].value.length == 0  || document.forms["main_form"].elements["inv_indirizzo"].value.length == 0 || document.forms["main_form"].elements["inv_citta"].value.length == 0 || document.forms["main_form"].elements["inv_provincia"].value.length == 0 || document.forms["main_form"].elements["inv_nazione"].value.length == 0  || document.forms["main_form"].elements["inv_cap"].value.length == 0 )) {
		
			alert ("check required fields in shipping details ?")  ;
			proceed = false ;
			break ;
		
		}
	
		
		// void loop
		break ;
	
	}
	
	if (proceed) {
	
		document.getElementById("finalbutton").style.display = "none" ;
		document.forms["main_form"].action = "saveorder.asp" ;
		document.forms["main_form"].submit() ;
		
	}

}

function chk_mail (email) {

	var retval = (email.indexOf("@")>0 && email.indexOf("@") < email.length-3 && email.lastIndexOf(".", email.indexOf("@")) && email.lastIndexOf(".") < email.length-2 ) ? true : false ;
	return retval ;

}

function chk_cf(cf) {
        cf = cf.toUpperCase() ;
		
		while (cf.indexOf(" ")>=0) {
		
			cf = cf.replace (" ", "") ;
		} 
		
        var cfok = false ;
		
        if( cf.length == 16 ) {
                cfok = true ;
                var validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                for( var i = 0; i < 16; i++ ){
                        if( validi.indexOf( cf.charAt(i) ) == -1 ) {
                                cfok = false;
                                break ;
                        }
                }
                if (cfok) {
                        var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                        var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
                        var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                        var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
                        var 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) ) cfok = false;
                }
        }
        return cfok ;
}

function chk_piva(piva) {

        var pivaok = false;
        if(piva.length == 11) {
			pivaok = true;
			validi = "0123456789";
			for(var  i = 0; i < 11; i++ ){
				if( validi.indexOf( piva.charAt(i) ) == -1 ) {
						pivaok = false;
						break ;
				}
			}
			if (pivaok) {
				var s = 0;
				var fix = '0';
				for( i = 0; i <= 9; i += 2 ) {
					s += piva.charCodeAt(i) - fix.charCodeAt(0);
				}
				for( i = 1; i <= 9; i += 2 ) {
					var c = 2*(piva.charCodeAt(i) - fix.charCodeAt(0) );
					if( c > 9 )  c -= 9 ;
					s += c;
				}
				if( ( 10 - s%10 )%10 != piva.charCodeAt(10) - fix.charCodeAt(0) ) pivaok = false ;
			}
        }
        return pivaok ;
}


function reCalc() {

	calcSubTotals(parseInt(top.document.forms["main_form"].elements["regmode"].value,10)) ;
	cartChange(2) ;
	// applyPagamento(parseInt(top.document.forms["main_form"].elements["paymode"].value, 10)) ;
	
}

function chkQt (evt, i) {

	var tempQtVal = parseInt(document.forms["main_form"].elements["qta_"+i].value, 10) ;
	if (isNaN(tempQtVal)) tempQtVal = 0;
	if (tempQtVal != refQtVal) {
		
		var ordmax = parseInt(document.forms["main_form"].elements["maxord_"+i].value, 10) ;
		if (tempQtVal > ordmax) {
		
			document.forms["main_form"].elements["qta_"+i].value = ordmax ;
			alert ("Your request exceed maximum amount able to be ordered") ;
					
		}
		reCalc() ;
		refQtVal = tempQtVal ;
	} 
}

function setQt(i) {

	refQtVal = parseInt(document.forms["main_form"].elements["qta_"+i].value, 10) ;
	if (isNaN(refQtVal)) refQtVal = 0;
}

function setPagamento(nr) {

	if (parseInt(top.document.forms["main_form"].elements["paymode"].value, 10) != nr) {
	

		applyPagamento(nr) ;
		top.document.forms["main_form"].elements["paymode"].value = nr ;
	}
}

function applyPagamento(nr) {

		var temptotale = parseFloat(top.document.forms["main_form"].elements["temptotale"].value) ;
		if (temptotale > 0) {
			var ssped = (nr==1) ? parseFloat(top.document.forms["main_form"].elements["spesesped"].value) : parseFloat(top.document.forms["main_form"].elements["spesesped"].value)+parseFloat(top.document.forms["main_form"].elements["nrumerosped"].value)*parseFloat(top.document.forms["main_form"].elements["contrassegno"].value) ;
			var totale = temptotale + ssped ;
		} else {
			var totale = 0 ;
			var ssped = 0 ;
		}
		
		top.document.forms["main_form"].elements["totalerefe"].value = totale ;
		
		top.document.getElementById("spesped").innerHTML = "Euro "+clientFormatNumber(ssped,2) ;
		top.document.getElementById("totalOrder").innerHTML = "Euro "+clientFormatNumber(totale,2) ;
	

}

function setRegime(nr) {

	
	if (parseInt(document.forms["main_form"].elements["regmode"].value, 10) != nr) {

		var oldzona = parseInt(document.forms["main_form"].elements["zona"].value, 10) ;
		var newzona = oldzona ;
		
		switch (nr) {
		
			case 1:
			
				newzona = 1 ;
				
				document.getElementById("block_it").style.display = "inline" ;
				document.getElementById("block_eu").style.display = "none" ;
				document.forms["main_form"].elements["fat_nazione"].value = "Italia" ;
				document.forms["main_form"].elements["inv_nazione"].value = "Italia" ;
				
			break ;
			
			case 2:
				newzona = 2 ;
				document.getElementById("block_it").style.display = "none" ;
				document.getElementById("block_eu").style.display = "inline" ;
				unsetItalia() ;
				 
			break ;
			
			case 3:
				newzona = 2 ;
				document.getElementById("block_it").style.display = "none" ;
				document.getElementById("block_eu").style.display = "none" ;
				unsetItalia() ;
				
			break ;
			
			
			case 4:
				newzona = 3 ;
				document.getElementById("block_it").style.display = "none" ;
				document.getElementById("block_eu").style.display = "none" ;
				unsetItalia() ;
				
			break ;
		}
		
		document.forms["main_form"].elements["regmode"].value = nr ;
		calcSubTotals( nr ) ;
		
		if (newzona != oldzona) {
		
			// ricalcolo le spese di spedizione per il cambio zona 
			
			document.forms["main_form"].elements["zona"].value = newzona ;
			cartChange(1) ;
			
		} else {
		
			// Forzo la chiamata della funzione che applica le spese di spedizione gią calcolate
			applyPagamento(parseInt(top.document.forms["main_form"].elements["paymode"].value, 10)) ;
			
		
		}
		
	}
}


function unsetItalia() {
	if (document.forms["main_form"].elements["fat_nazione"].value == "Italia") document.forms["main_form"].elements["fat_nazione"].value = "" ;
	if (document.forms["main_form"].elements["inv_nazione"].value == "Italia") document.forms["main_form"].elements["inv_nazione"].value = "" ;

}

function cartChange(mode, stringvar) {

	var zona = document.forms["main_form"].elements["zona"].value ;
	document.getElementById("spesped").innerHTML = "loading ..." ;
	document.getElementById("totalOrder").innerHTML = "loading ..." ;
	document.getElementById("riepilogo").innerHTML = "loading ..." ;
	
	var n1 = parseInt(document.forms["main_form"].elements["qta_0"].value,10) ;
	var n2 = parseInt(document.forms["main_form"].elements["qta_1"].value,10) ;
	
	if (isNaN (n1)) n1 = 0 ;
	if (isNaN (n2)) n2 = 0 ;
	
	var p1 = document.forms["main_form"].elements["peso_0"].value ;
	var p2 = document.forms["main_form"].elements["peso_1"].value ;
	
	if (isNaN (p1)) p1 = 0 ;
	if (isNaN (p2)) p2 = 0 ;
	
	url = "cartchange.asp?mode="+mode+"&n1="+n1+"&n2="+n2+"&p1="+p1+"&p2="+p2+"&zn="+zona ;
	if (typeof stringvar != "undefined") {
		url += stringvar ;
	}
  	
	if (document.all) document.frames["ifrbase"].location.href = url ;
	else if (document.getElementById && ! document.all) document.getElementById("ifrbase").contentDocument.location.href = url ;

}

function calcSubTotals( regime ) {

	// Applicazione del regime fiscale 
	switch (regime) {
	
		case 1:
		case 3:
			
			var totale = 0 ;
			
			for (var j=0 ; j<=1 ; j++) {
			
				var prezzonetto = parseFloat (top.document.forms["main_form"].elements["prezzonetto_"+j].value) ;
				var prezzolordo = parseFloat (top.document.forms["main_form"].elements["prezzolordo_"+j].value) ;
				var qta = parseInt(top.document.forms["main_form"].elements["qta_"+j].value, 10) ;
				var iva = parseFloat (top.document.forms["main_form"].elements["iva_"+j].value) ;
			
				if ( qta > 0) {
				
					var subtotale = qta * prezzolordo ;
					totale += subtotale ;
					top.document.getElementById("subTotal_"+j).innerHTML = "Euro " + clientFormatNumber(subtotale, 2) ;
					
				} else {
				
					top.document.getElementById("subTotal_"+j).innerHTML = "&nbsp;" ;
					
				}
				
				top.document.getElementById("price_"+j).innerHTML = clientFormatNumber(prezzonetto,2) ;
				top.document.getElementById("vat_"+j).innerHTML = clientFormatNumber(iva ,2) ;
				
			}
		
			top.document.forms["main_form"].elements["temptotale"].value = clientFormatNumber(totale, 2) ;
			
		break ;
		
		case 2:
		case 4:	
			var totale = 0 ; 
			for (var j=0; j<=1; j++) {
			
				var prezzonetto = parseFloat(top.document.forms["main_form"].elements["prezzonetto_"+j].value) ;
				var qta = parseInt(top.document.forms["main_form"].elements["qta_"+j].value, 10) ;
						
				if ( qta > 0 ) {
									
					var subtotale = qta * prezzonetto ;
					totale += subtotale ;
					top.document.getElementById("subTotal_"+j).innerHTML = "Euro " + clientFormatNumber(subtotale, 2) ;
					
				} else {
				
					top.document.getElementById("subTotal_"+j).innerHTML = "&nbsp;" ;
					
				}
				
				top.document.getElementById("price_"+j).innerHTML = clientFormatNumber(prezzonetto, 2) ; 
				top.document.getElementById("vat_"+j).innerHTML = " - " ;
				
			}
		
			top.document.forms["main_form"].elements["temptotale"].value = clientFormatNumber(totale, 2) ;
		
		
		break ;
		
		default:
			alert ("regime fiscale non codificato") ;
		break ;
	}

}

function setMultiConsegna() {

	if (document.forms["main_form"].elements["multiconsegna"].checked) {
	
		document.getElementById("block_inv").style.display = "none" ;
		document.forms["main_form"].elements["gift"].checked = false ;
	
	} else {
	
		document.getElementById("block_inv").style.display = "inline" ;
		
	}

}

function checkGift() {

	if (document.forms["main_form"].elements["gift"].checked) {
	
		document.getElementById("block_inv").style.display = "inline" ;
		document.forms["main_form"].elements["multiconsegna"].checked = false ;
		
	} 

}

function pCodeEnter() {

	document.getElementById("xpcode").innerHTML = "<a class='amenu' href='javascript:pCodeChk()'><b>verify my code !</b></a>" ;
	
	
}

function pCodeExit() {

	var newval = String( document.forms["main_form"].elements["pcodenter"].value ) ;
	var oldval = String( document.forms["main_form"].elements["pcode"].value ) ;
	if (oldval == newval) { 

		document.getElementById("xpcode").innerHTML = "&nbsp;" ;
		
	} else {
	
		pCodeChk() ;
	
	}
}

function pCodeChk() {

	cartChange(3, "&pcode="+String( document.forms["main_form"].elements["pcodenter"].value )) ;


}


function clientFormatNumber(expr, decimals ) {

    var str = "" + Math.round( eval(expr) * Math.pow(10,decimals))
	while (str.length <= decimals) { str = "0" + str } 
    var decpoint = str.length - decimals; 
    var result = str.substring(0,decpoint);
	if (decimals) result += "." + str.substring(decpoint,str.length);
	return result;

}

