// JavaScript Document

// general functions that *could* be used on any page
	var is_closing = true;
	window.addEvent('load',function () {
		var a = $$('a');
		for(var i = 0; i < a.length; i++)
		{
			//alert(a[i].href.indexOf('budgetnewsletters'));
			if (a[i].href.indexOf('budgetnewsletters') != -1)
			{
				//alert('NOT changing: '+a[i].href);
				a[i].addEvent('click',function() { is_closing=false; return(true); });
			}
			// else alert('NOT changing: '+a[i].href);
		}
		var f = $$('form');
		for(var i = 0; i < f.length; i++)
		{
			//alert(f[i].action);
			f[i].addEvent('submit',function() { is_closing=false; return(true); });
		}
		var popWindow = window.open('empty.php');
		popWindow.close();
	});
	window.addEvent('unload',function(e) {
		if (is_closing === true) popUpWindowOpener('/exit_survey.php', 'Exit Survey', '', '', '', 400, '', '', '', '', '', '', '', '', '', 600);
	});

	function popUpWindowOpener(url, name, ch, di, fu, he, le, lo, me, re, sc, st, ti, to, tp, wi)
	{
		var specs = '';
		
		if (ch.length > 0) specs += 'channelmode='+ch+',';
		if (di.length > 0) specs += 'directories='+di+',';
		if (fu.length > 0) specs += 'fullscreen='+fu+',';
		if (lo.length > 0) specs += 'location='+lo+',';
		if (me.length > 0) specs += 'menubar='+me+',';
		if (re.length > 0) specs += 'resizable='+re+',';
		if (sc.length > 0) specs += 'scrollbars='+sc+',';
		if (st.length > 0) specs += 'status='+st+',';
		if (ti.length > 0) specs += 'titlebar='+ti+',';
		if (to.length > 0) specs += 'toolbar='+to+',';
		
		if (parseFloat(he) < 100) he = 100;
		if (parseFloat(wi) < 100) wi = 100;
		
		if (le.length == 0)
		{
			if (window.innerWidth) le = ((window.innerWidth/2)+window.screenX)-(parseFloat(wi)/2);
			else if (document.body.clientWidth) le = ((document.body.clientWidth/2)+window.screenLeft)-(parseFloat(wi)/2);
			else le = 0;
		}
		if (tp.length == 0)
		{
			if (window.innerHeight) tp = ((window.innerHeight/2)+window.screenY)-(parseFloat(he)/2);
			else if (document.body.clientHeight) tp = ((document.body.clientHeight/2)+window.screenTop)-(parseFloat(he)/2);
			else tp = 0;
		}
		
		specs += 'height='+he+',';
		specs += 'width='+wi+',';
		specs += 'left='+le+',';
		specs += 'top='+tp;
		
		//specs = specs.substr(0,(specs.length - 1));
		
		if (name.length == '') name = 'BNL_InfoWindow';
		
		var popWindow = window.open(url, name, specs);
		//popWindow.document.close()
	}
	
	function loginClient(me,url,sid,base)
	{
		var val = me.innerHTML;
		var error = '';
		
		if (val == '[ Logout ]')
		{
			var params = 'action=logout&sess='+sid;
			var ajax = initializeAjax(url,params);
			if (ajax !== false)
			{
				try
				{
					ajax.onreadystatechange = function()
					{
						if (ajax.readyState == 4 && ajax.status == 200)
						{
							//alert(ajax.responseText);
							if (ajax.responseText != 'false')
							{
								var href = window.location.href;
								window.location = href;
								return(false);
								/*
								me.innerHTML = '[ Login ]';
								document.getElementById('login_name').innerHTML = '';
								
								var login_block = document.getElementById('login_block');
								if (login_block != null) login_block.style.display = 'block';
								
								var UL_Form = document.getElementById('UL_Form');
								if (UL_Form != null) UL_Form.style.display = 'none';
								
								var PastProj_List = document.getElementById('PastProj_List');
								if (PastProj_List != null) PastProj_List.style.display = 'none';
								
								var proof = document.getElementById('proof');
								if (proof != null) proof.style.display = 'none';
								*/
							}
							else alert('Unable to logout.');
						}
					};
					
					try { ajax.send(params); }
					catch(e) { error = 'unable to send parameters: '+e; }
				}
				catch (e) { error = 'unable to set onreadystatechange function: '+e; }
			}
			else error = 'unable to initialize ajax';
			
			if (error.length > 0) alert(error);
			
			return(false);
		}
		else
		{
			var login_url = base+'/orderstatus.php?return='+me.href;
			me.href = login_url;
			return(true);
		}
	}