var dc = 0;
var loadcount = 1;
var intvl = 0;
var intvlcount = 0;
var metrics = new Array();
var loaded = 0;
var windowloaded = false;
function dodebug(instr)
{
	var d = document.getElementById('debugbox');
	if (d)
	{
		d.style.height='100px';
			d.style.width='600px';
		d.style.visibility='';
		d.value = dc +':'+instr+'\n'+d.value ;
		dc = dc + 1;
	}
}

function getFlash(id)
{
		try 
		{
			movie = window.document.getElementById(id);
			if (movie)
				movie.SetVariable('','');
		}
		catch (ex)
		{
			try {
				movie = window.document.getElementById('e'+id);
			} catch (e) { return false }
		}
		dodebug('movie '+id+' '+movie);
		if (movie) 
			return movie;
		else return false;
}
function setField(fid,inputnum,f_value)
{//filter out extra character in ie
	{	
		f_name = 'field_'+inputnum;
		f_value = f_value.replace(/\u000D/g,'');
		movie = getFlash(fid);
		
		if (movie)
		{
			if (f_value == '') f_value = ' ';
			dodebug('1');
			movie.SetVariable('_root.'+f_name,f_value);
			movie.SetVariable('_root.label.'+f_name+'.text',f_value);
						dodebug('2');
			movie.SetVariable('_root.label.'+f_name+'.dirty',true);
						dodebug('3');
			movie.SetVariable('_root.dirty',true);
						dodebug('4');
		}
	}
}
function setInput(fid,inputnum)
{
	id = 'InputBox'+inputnum;
	var inputbox = window.document.getElementById(id);
	dodebug('inputbox '+inputbox);
	if (inputbox) 
		setField(fid,inputnum,inputbox.value);
	else dodebug('input '+f_name+' not found!!!');
}

function setupmovie(flashid)
{
	var movie = getFlash(flashid);
	var count = 1;
	var newmetric = false;
	var newvalue = '';
	movie.SetVariable('Stage.scaleMode','noBorder');
	movie.SetVariable('Stage.align','T');
}
function preloadlabel(flashid)
{
	dodebug('preloadlabel');
	intvl = setInterval("loadlabel('"+flashid+"',false);",100);
	setTimeout("loadlabel('"+flashid+"',true);",1500);
}
function loadlabel(flashid,repeat)
{
	dodebug('loadlabel repeat:'+repeat+ ' '+loaded);
	if ((intvlcount > 5) || (loaded))
	{
		clearInterval(intvl);
	}
	if (!loaded)
	{
		intvlcount = intvlcount + 1;
		var count = 0;
		var movie = getFlash(flashid);
		dodebug(movie);
		if (movie)
		{	dodebug('movie found' +movie);
				var moviecontainer = window.document.getElementById('container'+flashid);
				
				//moviecontainer.style.visibility='';
				var tolen = (1000*loadcount);
				if (repeat)
				{
					var to = window.setTimeout("dodebug('timeout "+tolen+"');loadlabel('"+flashid+"');",tolen);
					loadcount = loadcount + 1;
				}
				try
				{	
					{
						dodebug('setInput start');
						count = 1;
						while (window.document.getElementById('InputBox'+count))
						{
							dodebug('setInput '+count);
							setInput(flashid,count);
							count = count + 1;
						}
						clearTimeout(to);
						clearInterval(intvl); 
						setupmovie(flashid,count-1);
						loaded++;
					}
				} catch (e) {dodebug('error'+e)}
		}
	}
}

function setuplabelmovie(flashid)
{
	dodebug('wl'+windowloaded);
	if (windowloaded)
	{
		dodebug('setuplabelmovie: already loaded, running loadlabel');
		loadlabel(flashid,false);
	}
	else 
	{
		window.setTimeout('setuplabelmovie("'+flashid+'")',100);
	}
	/*
	else
	{
		var func = "loadlabel('"+flashid+"',false);";
		dodebug(func);
		try{window.attachEvent('onload',function() { eval(func);})}catch(e) {}
		func = "preloadlabel('"+flashid+"',false);";
		try{window.addEventListener('load',function() { eval(func);},false);}catch (e){}
	}*/
}
function setwl()
{	windowloaded = true;
}
windowloaded = false;
//try {window.attachEvent('onload',setwl)} catch (e) {}
//try {window.addEventListener('load',setwl,false);}	catch (e){}
//if (!window.attachEvent) 
window.onload = setwl;// function() {windowloaded = true};
