var old_num = 1;
var old_faq_num = 0;
var my_tool = null;
var tb_check = new Array();
var tb_check_num = 2;
var tb_serial = '{08DBC529-29F7-4A3C-AD2E-A6B7822F5242}';
var base_var = 1;

var GoSearchParams = { 'it': {
		sa: 'Cerca',
		client: 'pub-9091551696182533',
		forid: '1',
		ie: 'UTF-8',
		oe: 'UTF-8',
		cof: 'GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0099FF;LC:0099FF;T:000000;GFNT:0099FF;GIMP:0099FF;FORID:11',
		hl:'it',
		channel: '0398950985'
	},
	'www' : {
		sa: 'Cerca',
		client: 'pub-9091551696182533',
		forid: '1',
		ie: 'UTF-8',
		oe: 'UTF-8',
		cof: 'GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0099FF;LC:0099FF;T:000000;GFNT:0099FF;GIMP:0099FF;FORID:11',
		hl:'us',
		channel: '8546867149'
	}
}


function ToolBarInit(tool)
{
	my_tool = tool;
	
	// verifica della toolbar Peeplo
	if (my_tool)
	{
		var clsid = '';
		if (navigator.userAgent.indexOf("Firefox") > -1)		// per Firefox
			clsid = my_tool.guid;
		else
			clsid = my_tool.clsid;
			
		if (clsid != tb_serial)
			my_tool = null;
	}
}

function set_toolbar()
{
	if (my_tool)
	{
		document.getElementById('TB_download_on').style.display = 'none';
		document.getElementById('TB_download_off').style.display = 'block';
		document.getElementById('TB_download_disable').style.display = 'none';
		
		if (document.getElementById('check_0_div'))
			document.getElementById('check_0_div').style.display = 'none';
			
		if (document.getElementById('browser_div'))
			document.getElementById('browser_div').style.display = 'none';
		
		for (i = 1; i <= tb_check_num; i++)
		{
			tb_check[i] = get_tb_var(i);
			if (document.getElementById('obj_check_' + i))
			{
				if (tb_check[i] == '1')
					document.getElementById('obj_check_' + i).checked = true;
				else if (tb_check[i] == '0')
					document.getElementById('obj_check_' + i).checked = false;
			}
		}
		
		setCookie('toolbar', my_tool.ToolbarID);
	}
	else if ((navigator.userAgent.indexOf("Firefox/") <= -1) && (navigator.userAgent.indexOf("MSIE") <= -1))
	{
		document.getElementById('TB_download_on').style.display = 'none';
		document.getElementById('TB_download_off').style.display = 'none';
		document.getElementById('TB_download_disable').style.display = 'block';
		if (document.getElementById('browser_div'))
			document.getElementById('browser_div').style.display = 'none';
		document.getElementById('browser_div_2').style.display = 'block';
	}
	else
	{
		document.getElementById('TB_download_on').style.display = 'block';
		document.getElementById('TB_download_off').style.display = 'none';
		document.getElementById('TB_download_disable').style.display = 'none';
	}
}

function show_div(num) {
	document.getElementById('TB_BOX_nav_' + old_num).style.display = 'none';
	document.getElementById('TB_BOX_nav_' + num).style.display = 'block';
	
	document.getElementById('TB_LI_nav_' + old_num).className = '';
	document.getElementById('TB_LI_nav_' + num).className = 'current';
	
	old_num = num;
	return false;
}

function show_faq(num) {
	if (old_faq_num > 0)
		document.getElementById('faq_' + old_faq_num).style.display = 'none';
	document.getElementById('faq_' + num).style.display = 'block';
	
	old_faq_num = num;
	return false;
}

function install_tb_complete (name, result) {
	if (result != 0 && result != 999) alert("An error occured: " + result);
	else alert("Restart the browser to finish installation");
}

function Install_tb(type)
{
	var tb_type = '';
	
	if (type > 3)			// per la toolbar di 4YOU
	{
		tb_type = '&tb=4you';
		type -= 3;
	}
	
	if (type == 1)			// per Internet Explorer
	{
		document.location = "https://resources.peeplo.com/toolbar/?cmd=download&b=1" + tb_type;
		return false;
	}
	else if (type == 2)		// per Firefox
	{
		if (!document.getElementById('check_0').checked)
		{
			alert(document.getElementById('check_0_txt').value);
			return;
		}
		var xpi = new Object();
		if (navigator.userAgent.indexOf("Firefox/3") > -1)		// per Firefox 3.x
		{
			xpi["Peeplo_Toolbar"] = "https://resources.peeplo.com/toolbar/?cmd=download&b=2" + tb_type;
		}
		InstallTrigger.install(xpi, install_tb_complete);
	}
	else if (type == 3)			// versione per FF scaricata da Internet Explorer
	{
		if (!document.getElementById('check_0').checked)
		{
			alert(document.getElementById('check_0_txt').value);
			return;
		}
		document.location = "https://resources.peeplo.com/toolbar/?cmd=download&b=3" + tb_type;
		return false;
	}
}

function get_tb_var(num)
{
	if (navigator.userAgent.indexOf("Firefox") > -1)		// per Firefox
		return my_tool.Var[base_var + num];
	else
		return my_tool.Var(base_var + num);
}

function set_tb_var(num, value)
{
	if (navigator.userAgent.indexOf("Firefox") > -1)		// per Firefox
		my_tool.Var[base_var + num] = value;
	else
		my_tool.Var(base_var + num) = value;
}

function set_check(num)
{
	check_obj = document.getElementById('obj_check_' + num);
	if (my_tool && check_obj)
	{
		if (check_obj.checked)
			set_tb_var(num, '1');
		else
			set_tb_var(num, '0');
		my_tool.Reload();
	}
}

function set_all_checks()
{
	for (i = 1; i <= tb_check_num; i++)
	{
		if (document.getElementById('obj_check_' + i))
		{
			if (document.getElementById('obj_check_' + i).checked)
				set_tb_var(i, '1');
			else
				set_tb_var(i, '0');
		}
	}
}

function set_div()
{
	var set_div = parseUrl();
	if ((set_div != undefined) && set_div['original_params'] &&  set_div['original_params']['d'])
	{
		show_div(set_div['original_params']['d']);
	}
}


function searchIt4you(forced, tld)
{
	if(!forced && document.searchf.elements['c'] && document.searchf.elements['c'].value!='1')
	{
		document.searchf.elements['rs_display_name'].value = '';
		return false;
	}
    var chars = document.searchf.elements['rs_display_name'].value;
    var tot = chars.length;
    if ( !chars )
	{
        document.searchf.elements['rs_display_name'].focus();
        return false;
    }
    if ( tot < 2 )
	{
        document.searchf.elements['rs_display_name'].focus();
        return false;
    }
    
	if ((tld != 'it') && (tld != 'www'))
		tld = 'www';
    var loc = tld + ".4you.4dh.com";
	
	if(document.searchf.sect.value)
		loc += '/'+document.searchf.sect.value;
	if(	document.searchf.sect.value != 'ringtones' && 
		document.searchf.sect.value != 'ov' && 
		document.searchf.sect.value != 'go')
		loc += "/find/";
	else
		loc+="/";
		
	chars=cleanWord(chars);
	loc+=chars+"/";
	var adq=chars;
	
	var url = 'http://'+loc+'?q3='+adq;
	if(document.searchf.sect.value == 'go') //google
	{
		url+='&q='+chars;
		if(GoSearchParams[tld])
		{
			if( self.GOOGLE_PUB && GOOGLE_PUB)
				GoSearchParams[tld]['client'] = GOOGLE_PUB; 
			for(var k in GoSearchParams[tld])
				url+='&'+k+'='+encodeUrl(GoSearchParams[tld][k]);
		}
	}
	document.location.href = url;
	
    return false;
}


function cleanWord(word)
{
	var w="";
	for(var i = 0; i < word.length; i++)
		w += word.substr(i,1).replace("/"," ");
	return encodeUrl(w);	
}
