function alltrim(str) {
    return str.replace(/^\s+|\s+$/g, '');
}
function conf(){
var sel = 0;
var conf = '';
var v = '';
	$('[name=conf[]]').each(function(index) {
		if($(this).attr('checked')){
		++sel;
		conf += v+$(this).val();
		v = ',';
		}
	});
	if(sel < 2){
		alert(ERRCONFARTICOLI);
	}else{
		var url = "/"+LANG+"/inc/confronta.php?c="+conf
		newWindow=window.open(url,'name','height=600,width=990,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,status=yes');
		if (window.focus) {newWindow.focus()}

	}
}
function checkCerca(f){
		if(!alltrim(f.k.value) && !f.c1.value){ alert(ERCERCA); return false;}
}
function del_value(obj,val){
    if(obj.value == val){ obj.value = ''; }
}

function set_value(obj,val){
    if(obj.value == ''){ obj.value = val; }
}
// home page tab
function sel_tab(id_tab,s,cmd){
	for(i=1;i<=s;i++){
		if(id_tab == i){
			$("#tab_"+i).addClass("tab_sel");
			$.ajax({
			  type: "get",	
			  url: "/inc/ajax_offerte_home.php",
			  data: ({
					 'id_tab':id_tab,
					 'cmd':cmd,
					 'lang':LANG}),
			  cache: false,
			  beforeSend: function(){ 
					$("#tab_0").html('<img src="'+TEMPLATE_URL+'images/loading_p.gif" />');
				},
			  success: function(result){ 
					$("#result").html(result);
					$("#tab_0").html('');
				}
			 });	
		}else{
			$("#tab_"+i).removeClass('tab_sel');
		}
	}
}

function addWishlist(id){
	
		$.ajax({
		  type: "get",	
		  url: "/inc/add_wishlist.php",
		  data: ({'articolo_id':id,'lang':LANG}),
		  cache: false,
		  beforeSend: function(){ 
				//$("#tab_0").html('<img src="'+TEMPLATE_URL+'images/loading_p.gif" />');
			},
		  success: function(result){ 
				$("#r"+id).append(result);
				removeBox('#wish' + id);
			}
		 });	

}

function addAvviso(id){
	
		$.ajax({
		  type: "get",	
		  url: "/inc/add_avviso.php",
		  data: ({'articolo_id':id,'lang':LANG}),
		  cache: false,
		  beforeSend: function(){ 
				//$("#tab_0").html('<img src="'+TEMPLATE_URL+'images/loading_p.gif" />');
			},
		  success: function(result){ 
				$("#avviso"+id).html(result);
				//removeBox('#avviso'+id);
			}
		 });	

}

function removeBox(obj){
	setTimeout(function(){$(obj).remove()},2000);	
}

/**************************************
    Controllo del Codice Fiscale
    Linguaggio: JavaScript
	http://www.icosaedro.it/cf-pi/index.html
***************************************/

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 e' corretta ";
    validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for( i = 0; i < 16; i++ ){
        if( validi.indexOf( cf.charAt(i) ) == -1 )
            return "Il Codice Fiscale contiene il carattere ''"+cf.charAt(i)+"'' non valido";
    }
    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 e' corretto";
    return "";
}

function ControllaPIVA(pi)
{
	if( pi == '' )  return '';
	if( pi.length != 11 )
		return "La lunghezza della partita IVA non e' corretta";
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
			return "Il numero di Partita IVA contiene il carattere ''" + pi.charAt(i) + "'' non valido";
	}
	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 "Il numero di Partita IVA non e' valido";
	return '';
}
