	var max_queue = 3;			// amount of iframes allowed to load concurrently
	var load_timeout = 6000;	// amount of time to wait for iframe before cancelling (in milliseconds)
	var i_cur = 0;
	var i_queue = 0;
	var i_failed = 0;
	var sites = null;
	var loaded = {};
	var injected = {};
	var finished = false;
	var skip_form_init = false;
	var yahoo_token = '';

	processNextIframe = function() {
		if(i_queue >= (max_queue - 1)) return;
		if(injected[i_cur] == 1) return;
		
		if(sites[i_cur]) {
			loaded[i_cur] = 0;
			injected[i_cur] = 1;
			var iframe = createIFrame(i_cur);
			iframe.setAttribute("id", 'iframe_'+ i_cur);
			iframe.setAttribute("width", '0');
			iframe.setAttribute("height", '0');
			iframe.setAttribute("style", 'position: absolute; bottom: 0px; right: 0px; width: 0px; height: 0px; display: none;');
			iframe.setAttribute("src", sites[i_cur]);
			$('gen_iframes').appendChild(iframe);
			setTimeout('checkIFrame('+ i_cur +');', load_timeout);
			
			i_cur++;
			i_queue++;
		}

		if(i_cur >= sites.length && i_queue == 0 && !finished) {
			finished	=	true;
			finishOptOut();
			return;
		}
		
		if(i_queue >= (max_queue - 1)) return;
		setTimeout('processNextIframe();', 1);
	}
	
	checkIFrame = function(idx) {
		if(loaded[idx] && loaded[idx] == 1) {
			return true;
		}
		i_failed++;
		var iframe = $('iframe_'+idx);
		if(iframe) {
			iframe.dispose();
			i_queue--;
			processNextIframe();
		}
	}
	
	iframeLoaded = function(idx) {
		i_queue--;
		loaded[idx] = 1;
		updateSpinner(i_cur);
		processNextIframe();
	}
	
	initOptOut = function() {
		if(!sites) return false;
		processNextIframe();
		handle_tokenized_optouts();
	}

	createIFrame = function(idx) {
		var iframe = $(document.createElement('iframe'));
		iframe.addEvent('load', function(e) {
			iframeLoaded(idx);
		});
		return iframe;
	}
	
	finishOptOut = function() {
		stopSpinner();
		disableCheckBoxes(0);
		if($('hide_for_ie')) $('hide_for_ie').style.display='none';
		if($('widget_box')) $('widget_box').innerHTML='<iframe class="widget" frameborder="0" src="/privacy/status_widget?hgt=584&dom=privacychoice.org"></iframe>';
	}
	
	disableCheckBoxes = function(enable) {
		var chks = $$('.optout input');
		
		var lower = $('lower_options');
		
		if(lower) {
			if (!enable) {
				lower.style.display = 'block';
			} else {			
				lower.style.display = 'none';
			}
		}
		
		
		if(chks.length > 0) chks.each(function(el) {
			if(!el.type || el.type.toLowerCase() != 'radio') {
				return;
			}
			el.disabled = enable;
		});
	}
	
	startSpinner = function() {
		if ($('new_choose_page'))
		{
			$('pc_content').style.display = 'none';
			$('new_choose_page').style.display = 'block';
			if ($('selective_content').style.display=='block') { handleSlide($('selective_content')); }
			if ($('complete_content').style.display=='block') { handleSlide($('complete_content')); }
			never_show_browser_optout_again = true;
		}
		var divv = $E('.home .optout .action');
		if(!divv) return;
		
		divv.set('html', '');
		var spin = $(document.createElement('div'));
		var span = $(document.createElement('span'));
		var pp = $(document.createElement('p'));
		spin.className = 'spinner';
		span.id = 'spinner_num';
		pp.style.textAlign = 'left';
		if(typeof(optout_msg) == 'undefined') {
			var _msg = 'Opt-out in progress. <strong>Ad blockers must be off.</strong> This may take up to a minute to complete. If the process is stuck on any number for more than 10 seconds, please <a href="http://privacychoice.uservoice.com/pages/general" target="_blank">let us know</a>. If you see confirmation buttons, click OK.';
		} else {
			var _msg = optout_msg;
		}
		pp.set('html', _msg);
		spin.appendChild(span);
		divv.appendChild(spin);
		divv.appendChild(pp);
	}
	
	updateSpinner = function(num) {
		if ($('spinner_num')) {
			$('spinner_num').set('html', num);
		}
	}
	
	stopSpinner = function() {
		var divv = $E('.home .optout .action');
		if(!divv) return;
		
		var numo = $('spinner_num');
		var total = parseInt(numo.innerHTML.replace(/[^0-9]/ig, ''));
		var sum = (i_cur - i_failed);
		total += sum;
		numo.innerHTML = addCommas(total);
		new Request({
			url: '/privacy/inc_num_opt_out/'+sum,
			method: 'get'
		}).send();
		
		divv.set('html', '');
		var h4 = $(document.createElement('h4'));
		var pp = $(document.createElement('p'));
		pp.className = 'small';
		if(typeof(complete_msg) == 'undefined') {
			
			var o1 = $('optout1');
			if (o1.checked) {
				addon_num = num_networks-sum;
			} else {
				addon_num = num_networks_concern-sum;
			}
			
			if ($('new_choose_page'))
			{
				h4.set('html', 'Opt-out process is complete.<br/>You have opted out of <strong>'+ sum +'</strong> tracking companies.<br/><br/><strong>Opt-out of '+addon_num+' more networks with the <a href="/add_on">browser add-on</a>.</strong><br/>If you do not have the add-on (Firefox only), you need to repeat this process whenever you clear your browser cookies.<br/><br/>Check out your PrivacyChoice opt-out status at any time<br/>on your <a href="/choices"><strong>Settings Page</strong></a>.');
				divv.appendChild(h4);
			}
			else
			{
				h4.set('html', 'You have opted out of <strong>'+ sum +'</strong> tracking companies.');
				if (ff_check()) {
					pp.set('html', '<a href="/add_on"><img src="/images/site/home/install_add_on.png" /></a><br/><strong>Opt-out of '+addon_num+' more networks with the <a href="/add_on">browser add-on</a>.</strong><br/>If you do not have the add-on (Firefox only), you need to repeat this process whenever you clear your browser cookies.');
				} else {
					pp.set('html', '<a href="/share"><img src="/images/site/home/tell_a_friend.png" /></a><br/><strong>Opt-out of '+addon_num+' more networks with the <a href="/add_on">browser add-on</a>.</strong><br/>If you do not have the add-on (Firefox only), you need to repeat this process whenever you clear your browser cookies.');
				}
				divv.appendChild(h4);
				divv.appendChild(pp);
			}

		} else {
			h4.set('html', complete_msg.replace('[[numoptout]]', sum));
			divv.appendChild(h4);
		}
	}
	
	selective_is_properly_chosen = function()
	{
		var o2 = $('optout2');
		var s1 = $('no_oversight');
		var s2 = $('no_anonymity');
		var s3 = $('no_deletion');
		
		if (o2.checked && (s1.checked || s2.checked || s3.checked))
		{
			return true;
		}
		return false;
	}
	
	initForm = function() {
		var o1 = $('optout1');
		var o2 = $('optout2');
		var o3 = $('optout3');
		var s1 = $('no_oversight');
		var s2 = $('no_anonymity');
		var s3 = $('no_deletion');
		
		if (o3)
		{
			if((o1.checked || selective_is_properly_chosen() || o3.checked) && !disable_form) {
				
				enableForm();
			}

		}
		else
		{
			if((o1.checked || selective_is_properly_chosen()) && !disable_form) {
				
				enableForm();
			}
		}

		
		if(o1) o1.addEvent('click', function(e) {
			enableForm();
		});
		if(o2) o2.addEvent('click', function(e) {
			if (selective_is_properly_chosen())
			{
				enableForm();
			}
			else
			{
				disableButton();
			}
		});
		if(o3) o3.addEvent('click', function(e) {
			enableForm();
		});
		
		if(s1) s1.addEvent('click', function(e) {
			if (selective_is_properly_chosen())
			{
				enableForm();
			}
			else
			{
				disableButton();
			}
		});
		if(s2) s2.addEvent('click', function(e) {
			if (selective_is_properly_chosen())
			{
				enableForm();
			}
			else
			{
				disableButton();
			}
		});
		if(s3) s3.addEvent('click', function(e) {
			if (selective_is_properly_chosen())
			{
				enableForm();
			}
			else
			{
				disableButton();
			}
		});
		
		var frm = $('get_url_form');
		if(!frm) return;
		frm.removeEvents('submit');
		frm.addEvent('submit', function(e) {
			var e = new Event(e).stop();
			frm.set('send', {
				onComplete: function(res) {
					if(res == '') {
						disableCheckBoxes(0);
						return;
					}
					if (add_on_present) {
						
						trigger_addon_event('privacychoice-reset', true);
						
						window.location.href = '/optout';
						return false;
					}
					if ($('chk_addon').checked) {
						window.location.href = '/add_on';
					} else {
						startSpinner();
						sites = res.split('|');
						setTimeout('initOptOut();', 1);
					}
				}
			}).send();
			disableCheckBoxes(1);
		});
	}
	
	addCommas = function(nStr) {
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}
	function disableForm() {
		$('optout1').disabled = true;
		$('optout2').disabled = true;
		$('optout3').disabled = true;
		$('options').style.color = 'gray';
	}
	function disableButton() {
		var button = $('button');
		var buttonDisabled = $('buttonDisabled');
		if(buttonDisabled) buttonDisabled.style.display = 'block';
		button.style.display = 'none';	
	}
	function enableForm() {
		var button = $('button');
		var buttonDisabled = $('buttonDisabled');
		if(buttonDisabled) buttonDisabled.style.display = 'none';
		button.style.display = 'block';
		
		$('optout1').disabled = false;
		$('optout2').disabled = false;
		if ($('optout3'))
		{
			$('optout3').disabled = false;
		}
		$('options').style.color = '';
		
		if($('youchoose')) $('youchoose').className='';
	}
	
	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	
	window.addEvent('domready', function() {
		// some housekeeping (JL)
		if (ff_check()) {
			if($('hide_for_ie')) $('hide_for_ie').style.display='block';
		}
		if (!skip_form_init)
		{
			if (disable_form) { disableForm(); }
			initForm();
		}
		initTips($$('.tip'), 200);

	});

	handle_yahoo_token = function(token)
	{
		yahoo_token = token;
	}
	
	handle_tokenized_optouts = function()
	{
		if (yahoo_token)
		{
			handle_yahoo_optout();
		}
	}
	
	handle_yahoo_optout = function()
	{
		$('yahoo_frame').src = 'http://info.yahoo.com/nai/optout.html?opter='+yahoo_opter+'&token='+yahoo_token;	
	}
	handle_yahoo_response = function(response)
	{
		//alert(response);
	}