function writeConsole(string)
{
	if (document.getElementById("console"))
		document.getElementById("console").innerHTML = document.getElementById("console").innerHTML+string;

	return;
}
function FindDT(model,modelrun,hoursout)
{
	var modelnum;
	var dt;

	modelnum=FindModel(model,modelrun);
	if (hoursout>=hdfvars[modelnum].changehour)
		dt=hdfvars[modelnum].changedt;
	else
		dt=hdfvars[modelnum].dt
return dt;
}
//---------------------------------------------------------------GETS USER INPUTTED MODEL REQUEST
function GetModelInfo()
{
	var model;
	var level;
	var domain;
	var time=gTime;
	var modelrun;
	gNumColumns=1;
//------------------------------------------------READING VALUES FROM SELECT---------------------------
gnumHours=17;
	model=document.getElementById("model").value;
	level=document.getElementById("level").value;
	domain=document.getElementById("domain").value;
	modelrun=document.getElementById("modelrun").value;
//-----------------------------------------------------------CHECKING WHETHER TO FIND NEW START AND END TIME------------------
//	if (document.getElementById("keep_hours").checked){
//		}
//	else{		
//gstarttime=0;
			gNumColumns=gnumHours;
			if (i%parseInt(FindDT(model,modelrun,i)))
			{}
			else
			{
				gNumColumns++
			}
//		}
//-------------------------------------------------FINDING GMAXTIME-------------------------------------
	var loopcounter=0;
	var endtime=gStartTime;
	for (var i=gStartTime; loopcounter<(gnumHours); i++)
		{
			if (i%parseInt(FindDT(model,modelrun,i)))
			{
			}
			else
			{
				endtime=endtime+FindDT(model,modelrun,i)
				loopcounter++
			}
		}
		gMaxTime=gStartTime+gnumHours;
		gMaxTime=endtime;
		gMaxTime=parseInt(gMaxTime);
//------------------------------------------------FOUND GMAXTIME-------------------------------------------
	AddNewLine(model,level,domain,time,modelrun);
	return ;
}
//Finds gMaxTime from a given start time----______incidentally, gives the hour difference between start and end if you pass it 0--------
function FindgMaxTime(model,starttime,modelrun)
{
	var starttime;
	var loopcounter=0;
	var endtime=starttime;
	for (var i=starttime; loopcounter<(gnumHours); i++)
	{
		if (i%parseInt(FindDT(model,modelrun,i)))
		{}
		else
		{
			endtime=endtime+FindDT(model,modelrun,i)
			loopcounter++
		}
	}
return endtime;
}

//-----------------------------------------------------------gets key input----------------------------------------
function register(e)
{
	if (!e) e = window.event;
	var keyInfo = String.fromCharCode(e.keyCode);
	keyInfo =  String.fromCharCode(e['keyCode']) ;
	DoNewKeyEvent(keyInfo);
}

function DoNewKeyEvent(deckeypressed)
{
	var badalign=0;
	var time=gmaptime;
	var upincrement;
	var downincrement;
	var modeltimenum;
	var modelnum=gmodelnum;
	var length=maplist.length;
	length=length-1;

	//-------------------------------------finds the increments to add or subtract based on the current time-----------------------
	if (time >= maplist[modelnum].changehour)
		upincrement=parseInt(maplist[modelnum].changedt)
	else
		upincrement=parseInt(maplist[modelnum].dt)
	if (time <= maplist[modelnum].changehour)
		downincrement=parseInt(maplist[modelnum].dt)
	else 
		downincrement=parseInt(maplist[modelnum].changedt)
	//--------------------------------------------------------------------------now we know the correct increment-----------------
	switch (deckeypressed)
		{
		case 'L':
				
			if (time < maplist[modelnum].maxt)
				{
				time=parseInt(time)+upincrement;
				}
			if (time>=maplist[modelnum].VThash.hoursout[globalendtime])
				{
				NewScrollLinks(modelnum,time,"up");
				}		
		break; 	
		case 'J':
			if (time > 0)
				{
				time=parseInt(time)-downincrement
				}
			if (time <= validtimetohoursout(firstvalidtime,modelnum))
				{
				NewScrollLinks(modelnum,maplist[modelnum].validtime[time],"down");
				}	

		break; 	
		case 'I':

			if (modelnum>0)
			{
				modelnum--
			if (maplist[(modelnum+1)].validtime[time]!=maplist[modelnum].validtime[time])
				{
				var timediff=maplist[modelnum+1].validtime[time]-maplist[modelnum].validtime[time];
				var oldtime=time;
				time=time+timediff	
				if (maplist[(modelnum)].validhourlytimes[time]!=1)
				//	if (maplist[(modelnum+1)].validtime[oldtime]!=maplist[modelnum].validtime[time])
					{
					time=time-timediff	
					badalign=1
					}
				}
			}
		break; 	
        case 'D':

            if ((time+24) <= maplist[modelnum].maxt)
                {
                time=parseInt(time)+24;
                }
            if (time>=maplist[modelnum].VThash.hoursout[globalendtime])
                {
                NewScrollLinks(modelnum,time,"up");
                }
        break;
        case 'S':
            if ((time-24) >= 0)
                {
                time=parseInt(time)-24
                }
            if (time <= validtimetohoursout(firstvalidtime,modelnum))
                {
                NewScrollLinks(modelnum,maplist[modelnum].validtime[time],"down");
                }

        break;


		case 'K':

			if (modelnum<length)
			{
				modelnum++
				if (maplist[(modelnum-1)].validtime[time]!=maplist[modelnum].validtime[time])
				{
					var timediff=maplist[modelnum-1].validtime[time]-maplist[modelnum].validtime[time];
					var oldtime=time;
					time=time+timediff
					//	if (maplist[(modelnum-1)].validtime[oldtime]!=maplist[modelnum].validtime[time])
					if (maplist[(modelnum)].validhourlytimes[time]!=1)
					{
						time=time-timediff
						badalign=1
					}
	
				}
			}
		break; 	
		}
if (maplist[modelnum].validhourlytimes[time]==1)
	{
		NewChangeImage(modelnum,time);
		DrawLinksTable(17);
	}

}//--end of donewkeyevent





function SetMapLoaded()
{
	document.MapLoaded = 1;
	document.theMap.src=tester.src;
	return false;
}

function PreChangeImage(modelnum,time)
{
	if (gMouseOverFlag==0)
		{
		globalcount++;
		gMouseOverFlag=1;
		NewChangeImage(modelnum,time);
		DrawLinksTable(17);
		setTimeout("gMouseOverFlag=0",10)
		}
	return false;
}


function NewChangeImage(modelnum,time) 
{
var theinitialtime = new Date(maplist[modelnum].validtime[0]*60*60*1000);
var theinitialstring= maplist[modelnum].model +" initialized at " +theinitialtime.getHours()+ "z on "+(theinitialtime.getMonth()+1)+"/" +theinitialtime.getDate()+"  "
document.getElementById("init").innerHTML=theinitialstring;
gmodelnum=modelnum;
gmapmodel=maplist[modelnum].model;
gmapdomain=maplist[modelnum].domain;
gmaplevel=maplist[modelnum].level;
gmaptime=time;
gmapmodelrun=maplist[modelnum].modelrun;
modelrun=maplist[modelnum].modelrun


if (ImageArray[modelnum][time]==null || ImageArray[modelnum][time].linestring != maplist[modelnum].model+"_"+maplist[modelnum].domain+"_"+maplist[modelnum].level+"_"+maplist[modelnum].modelrun)
	{
	if (modelrun == 0)
		url = '/data/models/'+maplist[modelnum].model+'/'+maplist[modelnum].domain+'/'+maplist[modelnum].model+'_'+maplist[modelnum].domain+'_'+maplist[modelnum].level+'_'+time+'.gif?t='+gMODELCACHESTRING;
	else if (modelrun == 1)
		url = '/data/models/'+maplist[modelnum].model+'.'+maplist[modelnum].modelrun+'/'+maplist[modelnum].domain+'/'+maplist[modelnum].model+'_'+maplist[modelnum].domain+'_'+maplist[modelnum].level+'_'+time+'.gif?t='+gMODELCACHESTRING;
	else if (modelrun == 2)
		url = '/data/models/'+maplist[modelnum].model+'_TMP/'+maplist[modelnum].domain+'/'+maplist[modelnum].model+'_'+maplist[modelnum].domain+'_'+maplist[modelnum].level+'_'+time+'.gif?t='+gMODELCACHESTRING;


	ImageArray[modelnum][time]=new Image();
	ImageArray[modelnum][time].src=url
	ImageArray[modelnum][time].linestring=maplist[modelnum].model+"_"+maplist[modelnum].domain+"_"+maplist[modelnum].level+"_"+maplist[modelnum].modelrun;
	}
	document.theMap.src=ImageArray[modelnum][time].src;
//var ms=maplist[modelnum].validtime[time]*3600*1000;
//var thevalidtime =new Date(ms);
//var offset = thevalidtime.getTimezoneOffset();
//var localvalidtime=new Date(ms-(offset*60*1000));
//document.getElementById("time").innerHTML=localvalidtime;
	fillTime(modelnum, time);

if (document.getElementById("message1").innerHTML != maptypevars[maplist[modelnum].level].desc)
	{
	document.getElementById("message1").innerHTML=maptypevars[maplist[modelnum].level].desc;
	}
	return false;
}

function fillTime(modelnum, time){
	var ms=maplist[modelnum].validtime[time]*3600*1000;
	var thevalidtime =new Date(ms);
	var offset = thevalidtime.getTimezoneOffset();
	var localvalidtime=new Date(ms-(offset*60*1000));
	document.getElementById("time").innerHTML=localvalidtime;
}



function DoMapLoad(modelnum,time) 
{
	var url;
if (ImageArray[modelnum][time]==null)
    {
	    if (maplist[modelnum].modelrun == 0)
	    url = '/data/models/'+maplist[modelnum].model+'/'+maplist[modelnum].domain+'/'+maplist[modelnum].model+'_'+maplist[modelnum].domain+'_'+maplist[modelnum].level+'_'+time+'.gif?t='+gMODELCACHESTRING;
	    else
	        url = '/data/models/'+maplist[modelnum].model+'.'+maplist[modelnum].modelrun+'/'+maplist[modelnum].domain+'/'+maplist[modelnum].model+'_'+maplist[modelnum].domain+'_'+maplist[modelnum].level+'_'+time+'.gif?t='+gMODELCACHESTRING;
	ImageArray[modelnum][time]=new Image();
    ImageArray[modelnum][time].src=url
    }
    tester.onload=SetMapLoaded;
	tester.onerror=SetMapLoaded;
    tester.src=ImageArray[modelnum][time].src;


	return false;
}


function WrapAnimate(modelnum,time)
{
	gStopSpan="<a href=\"\" onClick=\"Slower(); return false\">&dArr <span onclick=\"StopAnimate()>\"</a><a href=\"\" onClick=\"StopAnimate(); return false\">Stop</a></span><a href=\"\" onClick=\"Faster(); return false\"> &uArr</a>";
	gAnimateStrNoClick="<a href=\"\" onClick=\"Slower(); return false\">Slower</a><span onclick=\"StopAnimate();\"><a href=\"\" onClick=\"StopAnimate(); return false\">Stop</a></span><a href=\"\" onClick=\"Faster(); return false\">Faster</a>";
	Animating=1;
	document.getElementById("stopspan").innerHTML=gStopSpan;
	OuterAnimate(modelnum,time);
}

function OuterAnimate(modelnum,time)
{
var localdelay=gDelay;
	if (document.MapLoaded==1)
	{
		document.MapLoaded = 0;
		time = Animate(modelnum,time);
		if (time>=validtimetohoursout(globalendtime-12,modelnum))
			InsideScrollLinks(modelnum,time,"up");
		if (maplist[modelnum].validtime[time]<firstvalidtime)
			firstvalidtime=maplist[modelnum].validtime[time];
		DrawLinksTable(17);

	}
if (time>maplist[modelnum].changehour)
	{
		localdelay=maplist[modelnum].changedt/maplist[modelnum].dt;
		localdelay=localdelay*gDelay;
	}
	timeout_str = "OuterAnimate(\""+modelnum+"\",\""+time+"\")";	
    timeout_id = setTimeout(timeout_str,localdelay);

}


function Faster()
{
if (gDelay>20)
	gDelay=gDelay-20;
return false;
}



function Slower()
{
	gDelay=gDelay+20;
	return false;
}

function Animate(modelnum,time)
{
	var timout_str;

	if (time >= maplist[modelnum].maxt)
		time=0;
	if (time >= maplist[modelnum].changehour)
		time=parseInt(time)+parseInt(maplist[modelnum].changedt);
	else
		time=parseInt(time)+parseInt(maplist[modelnum].dt);
	gmaptime=time
	gmodelnum=modelnum
	DoMapLoad(modelnum,time);
	fillTime(modelnum, time);

	return time;
}



function RemoveLine(modelnum)
{
	ImageArray[modelnum]=new Array();
	maplist.splice(modelnum,1)
	MakeValidMapArray();
	DrawLinksTable(17);
return false;
}
function AddNewLine(model,level,domain,time,modelrun)
{
	var length=maplist.length;
	var modeltimenum=FindModelTimeNum(model,modelrun);
	var modelnum=FindModel(model,modelrun);
	var DT;
	var tablestarttime;

	ImageArray[length]=new Array;   //image array will be filled in with images in the form ImageArray[row number][hour from start]
	
	gRowNum=1;	
	gNumRows++;
	if (document.getElementById("start_over").checked)
	{
		maplist=new Array();
		ImageArray=new Array();
		ImageArray[0]=new Array;   //image array will be filled in with images in the form ImageArray[row number][hour from start]
		length=maplist.length;
	}
	maplist[length]=new MapNode();
	maplist[length].model=model;
	maplist[length].level=level;
//	if (model=="NAM")
//	maplist[length].domain="US";
//	else
	
	maplist[length].domain=domain;
	maplist[length].modelrun=modelrun;
	maplist[length].hours=modeltimevars[modeltimenum].hours;          
	maplist[length].maxt=hdfvars[modelnum].maxt;
	maplist[length].dt=hdfvars[modelnum].dt;
	maplist[length].changehour=hdfvars[modelnum].changehour;
	maplist[length].changedt=hdfvars[modelnum].changedt;
//-------validhourlytimes tells you whether the model is valid for a given time eg. validhourlytimes[4]=0 means 4 hours out is not valid---------
	maplist[length].validhourlytimes=new Array();
//-----------------------------hoursout is the time a model is valid for for a given column eg. validtime 2=3 means the second column is 3---------------
	maplist[length].hoursout=new Array();
	maplist[length].validtime=new Array();	
	maplist[length].VThash=new Object();	
	maplist[length].VThash.VT=new Array();	
	maplist[length].VThash.hoursout=new Array();
	maplist[length].VThash.periodnumber=new Array();
	maplist[length].starthour=modeltimevars[modeltimenum].hour;          


	zerotime=parseInt(FindOldestStartModel());
	if (length==0)
		{
			firstvalidtime=zerotime+gStartTime;
		}

//	if (document.getElementById("keep_hours").checked)
//		{
//			firstvalidtime=zerotime+gStartTime;
//		}


	for (var i=0; i<=hdfvars[modelnum].maxt; i++)
		{
		DT=FindDT(model,modelrun,i)	
		if (i%DT)
			{
			maplist[length].validhourlytimes[i]=0;
			}
		else
			{
			maplist[length].validhourlytimes[i]=1;
			}
		}
//--------------------------------------fills out the columns for the model----------------------------------
var count=0;
	for (var i=0; i<= maplist[length].maxt; i++)
		{
		maplist[length].hoursout[i]=i;		
				count++	
	lastcol=count;
		}
//-------------------------------------------does the math to find differences in times---------------------
//----this is filled with the time difference data for the models-------------------------------------------
var diffarr=new Array();

	for (var i=0; i<=length; i++)
		{
		diffarr[i]=zerotime-parseInt(maplist[i].hours);
		}
	
//-------------------------this finds the max separation--max----------------
var max=0;
	for (var j=0; j<=length; j++)
		{
		if (diffarr[j] > max)
			max=diffarr[j];
		}

//--------------------------this finds the offset for each model
var offset=new Array();
	for (var j=0; j<=length; j++)
		{
		offset[j]=max-diffarr[j];	
		}
//-----------------------------------------finding the time the model period is valid for in hrs after 1970--------------------------
for (var i=0; i<=length; i++)
    {
    for (var j=0; j<=lastcol; j++)
        {
            if (maplist[i].hoursout[j]!="x")
            maplist[i].validtime[j]=maplist[i].hours+maplist[i].hoursout[j];
        }
    }
var maxoffset=0;
for (var i=0; i<=length; i++) 
{
if (offset[i]>maxoffset)
	maxoffset=offset[i];
}

var columncount=new Array();
var lasttime=new Array();
var numcols=new Array();
var NewEndHour;
var NewEndTimeModel;
maxTDs=0;
NewEndTime=0;


for (var i=0; i<=length; i++)
    {	
	columncount[i]=1
    for (var j=0; j<=maplist[i].maxt && columncount[i]<=gnumHours; j++)
        {
			if (maplist[i].validhourlytimes[j]==1)
				{
					columncount[i]++
					
				}
			numcols[j]=j;
			if (numcols[j]>maxTDs)
				maxTDs=numcols[j];
			if (lasttime[j]>NewEndTime)
					NewEndTimeModel=i;
					NewEndHour=j;
        }
    }
// validtime
//-----------------VThash.VT-----
for (var i=0; i<=length; i++)
	{
	for (var j=0; j<=maplist[i].maxt; j++)
		{
			if (maplist[i].validhourlytimes[j]==1)
				maplist[i].VThash.VT[(maplist[i].validtime[j])]=maplist[i].validtime[j];
				maplist[i].VThash.hoursout[(maplist[i].validtime[j])]=maplist[i].hoursout[j];
		}
	}
	MakeValidMapArray();
DrawLinksTable(maxTDs+maxoffset);
	return;
}

function MakeValidMapArray()
{
fulltablearray=new Array();

var length=maplist.length;
var k=0;


fulltablearray[k]=0;
for (var j=zerotime; j<=(zerotime+450); j++)
    {
    for (var i=0; i<length; i++)
        {
        if (maplist[i].validhourlytimes[maplist[i].VThash.hoursout[j]]==1 && fulltablearray[k-1]!=j){
            fulltablearray[k]=j;
			k++
            }
        }
    }
return;
}


function FindOldestStartModel()
{
	var lasttime=maplist[0].hours
	for (i=0; i<maplist.length; i++)
		{
		if (maplist[i].hours<lasttime)
			lasttime=maplist[i].hours;
		}
return lasttime;
}

function MapNode()
{
	mapnode = new Object();
	mapnode.model="NAM";
	mapnode.domain="US";
	mapnode.level="SURPRE";
	mapnode.modelrun="0"
	mapnode.hours=modeltimevars[FindModelTimeNum("NAM",0)].hours;
	return mapnode
}	




//---------------------------maxTDs doesn't do anything probably----------------
function DrawLinksTable(maxTDs) //-------uses the global variable firstvalidtime---- should have been passed around instead of global--low priority fix.
{
var changefirstvalidtime=0
var length=maplist.length
var rows=maplist.length;
var cols=firstvalidtime+maxTDs;
var tablehtml="<center><table cellspacing=\"0\" cellpadding=\"0\" id=\"drawlinkstable\">";
var newendtime;
var flag=0;
var columns=0;

var tablearray=new Array();


for (var j=firstvalidtime; columns<17; j++)
	{
	tablearray[j]=0;
	for (var i=0; i<length; i++)
		{
		if (maplist[i].validhourlytimes[maplist[i].VThash.hoursout[j]]==1){
				tablearray[j]=1;
			}
		if (maplist[i].validhourlytimes[maplist[i].VThash.hoursout[j]]==1 && flag!=1)
			{
				flag=1;
				columns++;
				newendtime=j;
			}
		if (j>(firstvalidtime+400))
			{
				columns=18;
				changefirstvalidtime=1
			}
		}
		flag=0;
	}
if (changefirstvalidtime!=1)
	{

		var count=0;
		for (var i=1; i<=length; i++)
	        {
				if (Animating !=1){
				gAnimateStr="<a href=\"\" onClick=\"WrapAnimate("+(i-1)+","+gmaptime+"); return false\">Animate</a>";
			//	gAnimateStr="";
				}
				else
				{
				gAnimateStr="";
				}
					tablehtml=tablehtml+"<tr><td name=\""+i+"model\" id=\""+i+"model\" class=\"model\"><span style=\"font-weight: bold;\"></span> "+maplist[i-1].model+"</td><td width=\"50\" class=\"level\">"+maplist[i-1].level+"</td> <td name=\""+i+"minus\" id=\""+i+"minus\" class=\"direction\"><a href=\"\" onClick=\"NewScrollLinks("+(i-1)+","+firstvalidtime+",'down');return false\">&laquo;</a></td>"
	        for (var j=firstvalidtime; j<=newendtime; j++)
	            {
					count++	
					if (tablearray[j]==1)// && maplist[(i-1)].validhourlytimes[validtimetohoursout(j,(i-1))]==1)
					{
						if (maplist[(i-1)].VThash.hoursout[j]<=(maplist[(i-1)].maxt))
							{
								if (maplist[(i-1)].validhourlytimes[validtimetohoursout(j,(i-1))]==1)
									{
										globalendtime=j;
									if (gmodelnum==(i-1) && gmaptime==validtimetohoursout(j,(i-1)))
										{
											tablehtml=tablehtml+"<td width=\"27\" class=\"hourOff\"  align=\"center\"  name=\""+i+"x"+j+"\""+" id=\""+i+"x"+j+"\"><a href='' onMouseOver=\"PreChangeImage("+(i-1)+","+maplist[(i-1)].VThash.hoursout[j]+")\"; onClick='DoNothing(); return false'>"+maplist[(i-1)].VThash.hoursout[j]+"</a></td>";
										}
									else
											tablehtml=tablehtml+"<td width=\"27\" class=\"hourOn\"  align=\"center\"  name=\""+i+"x"+j+"\""+" id=\""+i+"x"+j+"\"><a href='' onMouseOver=\"PreChangeImage("+(i-1)+","+maplist[(i-1)].VThash.hoursout[j]+")\"; onClick='DoNothing(); return false'>"+maplist[(i-1)].VThash.hoursout[j]+"</a></td>";
									}
								else
							tablehtml=tablehtml+"<td width=\"27\" class=\"hourNo\"  align=\"center\"  name=\"empty\" id=\"empty\">&nbsp;</td></div>";
							}
                    else
                        tablehtml=tablehtml+"<td width=\"27\" class=\"hourNo\"  align=\"center\"  name=\"empty\" id=\"empty\">&nbsp;</td></div>";

					}
						
	            }
			if (rows!=1)
				{
//	        	    tablehtml=tablehtml+"<td name=\""+i+"plus\" id=\""+i+"plus\" class=\"direction\"><a href=\"\" onClick=\"PreScrollLinks("+(i-1)+","+firstvalidtime+",'up');return false\">&raquo;</a></td><td id=\""+(i-1)+"Animate\" class=\"control\">"+gAnimateStr+"</td><td class=\"remove\" style=\"background-color: #F5F5F5;\">&nbsp;<a href=\"\" onClick=\"RemoveLine("+(i-1)+"); return false\" style=\"font-weight: normal;\">Remove</a>&nbsp;</td></tr>"
	        	    tablehtml=tablehtml+"<td name=\""+i+"plus\" id=\""+i+"plus\" class=\"direction\"><a href=\"\" onClick=\"PreScrollLinks("+(i-1)+","+firstvalidtime+",'up');return false\">&raquo;</a></td><td id=\""+(i-1)+"Animate\" class=\"control\">"+gAnimateStr+"</td><td class=\"remove\" style=\"background-color: #F5F5F5;\">&nbsp;<a href=\"\" onClick=\"RemoveLine("+(i-1)+"); return false\" style=\"font-weight: normal;\">Remove</a>&nbsp;</td></tr>"
				}
			else
				{
	   	         tablehtml=tablehtml+"<td name=\""+i+"plus\" id=\""+i+"plus\" class=\"direction\"><a href=\"\" onClick=\"PreScrollLinks("+(i-1)+","+firstvalidtime+",'up');return false\">&raquo;</a></td><td id=\""+(i-1)+"Animate\" class=\"control\">"+gAnimateStr+"</td></tr>"
				}
	        }
	tablehtml=tablehtml+"</center></table>"
	    document.getElementById("hours").innerHTML=tablehtml;
	
	}
else
	{
	var totaltdlength=fulltablearray.length;
	firstvalidtime=fulltablearray[totaltdlength-17]
//	alert(validtimetohoursout(firstvalidtime));
	DrawLinksTable(17);	
	}

return;
}

function FillTable()
{
alert("fill table shouldn't be called, kill the call")
}

function DoNothing()
{
return false;
}



function DelModel()
{
	for (i=0; i < gNumRows; i++)
		{
			if (i != mod2del)
				{
				}
		}	
}
function Check()
{
	DrawMapSelects();
	return;
}
function ParseModel()
{
			var modelcount;
			modelcount=gNumRows;
			IndMod = new Array();
			IndMod=fullhourstr.split(">+</a></table");
			IndMod[modelcount]=IndMod[modelcount-1]+">+</a></table>";
			return IndMod;
}

function FindModel(model,modelrun)
{
			var modelnum;
			modelnum=0;
            for (i=0; hdfvars[i].model!==model && hdfvars[i].modelrun!=modelrun; i++)
                {
//					alert("model="+model);
//					alert("hdfvars[i].model=  "+hdfvars[i].model);
//					alert(i + "i---gCSNUM" +gCSNUM);
                    modelnum=i+1;
                    if (i>=gCSNUM){
						alert("breaking in FindModel");
                    break;
					}
                }
return modelnum;
}


function FindModelTimeNum(model,modelrun)
{
	var modeltimenum=0;
	for (var i=0; i<modeltimevars.length; i++)
		{
			if (model==modeltimevars[i].name)
			{
			if (modelrun==modeltimevars[i].modelrun)
				modeltimenum=i;
			}
		}
return modeltimenum;
}
				



//----------------this function finds the last time that would be in the table given a start time and number of periods------------
//------starttime should be in hoursout---------
//---warning this will go past the end of the table if you give it a start time that allows that.----
function FindLastTableTime(numperiods,modelnum,starttime)
{	
var count=0;
var endtime=starttime;
	for (var i=starttime; count<numperiods; i++)
		{
		if (maplist[modelnum].validhourlytimes[i]==1 && i<maplist[modelnum].changehour)
			{
			count++
			endtime=endtime+maplist[modelnum].dt
			}

		if (maplist[modelnum].validhourlytimes[i]==1 && i>=maplist[modelnum].changehour)
			{
			count++
			endtime=endtime+maplist[modelnum].changedt
			}
		}
return endtime;
}



function validtimetohoursout(validtime,modelnum)
{
	for (var i=0; i<=maplist[modelnum].maxt; i++)
		{
		if (maplist[modelnum].validtime[i]==validtime)
			{
				var hournum=i;
				i=maplist[modelnum].maxt;
			}
		}
return hournum;			
}



function PreScrollLinks(modelnum,time,upordown)
{
		{
	        NewScrollLinks(modelnum,time,upordown)
		}
return;
}


//---time should be coming in in validtime & is supposed to be firstvalidtime
function NewScrollLinks(modelnum,time,upordown)
{
var ISLtime;
ISLtime=InsideScrollLinks(modelnum,time,upordown);
NewChangeImage(modelnum,ISLtime);
DrawLinksTable(maxTDs);
}



function InsideScrollLinks(modelnum,time,upordown)
{
var errorstring="+";
var timechange=0;
//------dt is the dt for vhanging the map
var dt;
//----DT is the dt for changing the Links
var DT;
time=validtimetohoursout(time,modelnum);
endtime=validtimetohoursout(globalendtime,modelnum)
dt=FindDT(maplist[modelnum].model,maplist[modelnum].modelrun,time)	
DT=FindDT(maplist[modelnum].model,maplist[modelnum].modelrun,gmaptime)	
if (upordown=="up")
	{
	if (endtime<=maplist[modelnum].maxt)
		{
//			if (time>=maplist[modelnum].maxt)
		firstvalidtime=firstvalidtime+DT;
		timechange=timechange+DT;
		
		}
	}

if (upordown=="down")
		{
		if (time>0)
		{
			firstvalidtime=firstvalidtime-dt;
			timechange=timechange-DT;
		}
	}
var ISLtime=parseInt(gmaptime)+parseInt(timechange);
if (ISLtime <= maplist[gmodelnum].maxt)
	{
		return ISLtime;
	}
else 
	{
		return gmaptime;
	}
//alert(ISLtime+"--"+time);
}




function ScrollDown(model,level,domain,time,modelrun)
{
	ScrollLinks(model,level,domain,time,modelrun,"down")
	return;
}

function ScrollUp(model,level,domain,time,modelrun)
{
	 ScrollLinks(model,level,domain,time,modelrun,"up")
	 return;
}

function ScrollLinks(model,level,domain,time,modelrun,upOrDown)
{
	var modelnum;
	var uplastincrement;
	var downlastincrement;
	var	upstartincrement;
	var downstartincrement;
	var starttimeholder;
	var maxtimeholder;

		modelnum=FindModel(model,modelrun);
		if (gMaxTime > hdfvars[modelnum].changehour){
			uplastincrement=hdfvars[modelnum].changedt;
			downlastincrement=hdfvars[modelnum].changedt;
		}
		else if (gMaxTime == hdfvars[modelnum].changehour){
			uplastincrement=hdfvars[modelnum].changedt;
            downlastincrement=hdfvars[modelnum].dt;
		}
		else{
			uplastincrement=hdfvars[modelnum].dt;
            downlastincrement=hdfvars[modelnum].dt;
		}

		
		if (gStartTime > hdfvars[modelnum].changehour){
            upstartincrement=hdfvars[modelnum].changedt;
            downstartincrement=hdfvars[modelnum].changedt;
		}
        else if (gStartTime == hdfvars[modelnum].changehour){
            upstartincrement=hdfvars[modelnum].changedt;
            downstartincrement=hdfvars[modelnum].dt;
		}
        else{
            upstartincrement=hdfvars[modelnum].dt;
            downstartincrement=hdfvars[modelnum].dt;
		}
		maxtimeholder=gMaxTime;
		starttimeholder=gStartTime;
		if (upOrDown == "up"  && gMaxTime !== hdfvars[modelnum].maxt)
		{
			gMaxTime=gMaxTime+uplastincrement;
			gStartTime=gStartTime+upstartincrement;		
			time=gMaxTime;
		}

		if (upOrDown == "down" && gStartTime !== 0)
		{
		gMaxTime=gMaxTime-downlastincrement;
		gStartTime=gStartTime-downstartincrement;		
		time=gStartTime;
		}
       		ThisFrame(model,level,domain,time,modelrun,gRowNum);
	return;
}
//this function redraws the map to the time passed to it
function ThisFrame(model,level,domain,time,modelrun,rownum)
{
		
			gmodel=model;
			glevel=level;
			gdomain=domain;
			gtime=time;
			gmodelrun=modelrun;	
			
			gRowNum=rownum;
	ChangeImage(model,domain,level,time,modelrun);

}




function StopAnimate()
{
gStopStr="";
document.getElementById("stopspan").innerHTML=gStopStr;
Animating=0;
DrawLinksTable(17);
	gstop=1;
//	document.MapLoaded = 1;
	clearTimeout(timeout_id);
	return;
}

function getMouseX(e)
{
	var mousex = 0;

	themap = document.divcontainer;

	mapx = themap.offsetLeft;

	if (e)
	{
		mousex = e.pageX - mapx;
	}
	else
	{
		if (event.srcElement.name=="theMap")
			mousex = event.offsetX;
	}

	return mousex;
}

function getMouseY(e)
{
	var mousey = 0;

	themap = document.divcontainer;

	mapy = themap.offsetTop;

	if (e)
	{
		mousey = e.pageY - mapy;
	}
	else
	{
		if (event.srcElement.name=="theMap")
			mousey = event.offsetY;
	}

	return mousey;
}

function getNumDays()
{
	var numdays;
	var numdaysf;

	var starthour = maplist[gmodelnum].starthour;

	numdaysf = (maplist[gmodelnum].maxt+starthour+1) / (24);
	numdays = parseInt(numdaysf);
	if (numdaysf > numdays)
		numdays++;

	return numdays;
}

function drawUI(e)
{
	var mapx,mapy;
	var mousex, mousey;
	var divmapo;
	var jg;

	jg = document.jg;
	themap = document.divcontainer;
	mousex = getMouseX(e);
	mousey = getMouseY(e);

	var found = 0;
	var i=0;
	var numdays;
	var leftx;
	var boxwidth;
	var boxheight;
	numdays = getNumDays();
	leftx = gLeftX;
	boxwidth = gCalWidth/(numdays);
	boxheight=22;

	jg.clear();

	for (i=0; i<numdays;i++)
	{
		var boxminx;
		var boxmaxx;
		var boxminy;
		var boxmaxy;

		boxminy = 2;
		boxmaxy = boxminy+boxheight;
		boxminx = i*boxwidth + leftx;
		boxmaxx = boxminx + boxwidth;

		if (mousex > boxminx && mousex < boxmaxx && mousey > boxminy && mousey < boxmaxy)
		{
			jg.setStroke(3);
			jg.setColor("#0000ff"); // blue
			jg.drawRect(boxminx+1, boxminy, boxwidth, boxheight);
			jg.paint();
			found=1;
		}
	}

}

function FindfirstvalidtimeForclickUI(time)
{
//---if hrsfromCH is positive time is after changehour
var hrsfromCH=  time -  maplist[gmodelnum].changehour;
var DTpers
var CDTpers
if (hrsfromCH >=0)
	{
	if ((time + 17*maplist[gmodelnum].changedt) <= maplist[gmodelnum].maxt)
		{
		firstvalidtime=time
		}
	else
		{
		time=time-maplist[gmodelnum].changedt
		FindfirstvalidtimeForclickUI(time)
		}
	}
if (hrsfromCH < 0)
	{
	DTpers=(-hrsfromCH/maplist[gmodelnum].dt);
	CDTpers=17-DTpers;
	if (CDTpers*maplist[gmodelnum].changedt > maplist[gmodelnum].maxt)
		{
		time=time-maplist[gmodelnum].dt;
		}	
	}
return maplist[gmodelnum].validtime[time];
}

function clickUI(e)
{
	var divmapo;
	var mousex;
	var mousey;
	themap = document.divmapo;
	mousex = getMouseX(e);
	mousey = getMouseY(e);
	var found = 0;
	var i=0;
	var numdays;
	var leftx;
	var boxwidth;
	var boxheight;
	var periodsperday;

	numdays = getNumDays();
	periodsperday = 24 / initialdt;

	leftx = gLeftX;
	boxwidth = gCalWidth/(numdays);
	boxheight=22;

	for (i=0; i<numdays;i++)
	{
		var boxminx;
		var boxmaxx;
		var boxminy;
		var boxmaxy;

		boxminy = 2;
		boxmaxy = boxminy+boxheight;
		boxminx = i*boxwidth + leftx;
		boxmaxx = boxminx + boxwidth;

		if (mousex > boxminx && mousex < boxmaxx && mousey > boxminy && mousey < boxmaxy)
		{
			var thetime;

			thetime = i*periodsperday*maplist[gmodelnum].dt;
			if (thetime > maplist[gmodelnum].maxt)
				thetime = maplist[gmodelnum].maxt
			else if (thetime < 0)
				thetime = 0;
				
			NewChangeImage(gmodelnum,thetime);
firstvalidtime=FindfirstvalidtimeForclickUI(thetime);
if (firstvalidtime<0)
	firstvalidtime=0;

DrawLinksTable(17);
		}
	}

return;
	
}

//--------------if you want the global modelnumber pass modelnum 0------------------
function FindStart(time,modelnum)
{
var starttime;
//----------checks to see if a modelnum was passed--------------
	if (modelnum==0)									//------
		modelnum=gmodelnum;								//------
//--------------------------------------------------------------
	if (time<maplist[modelnum].changehour)
	{
	starttime=(time-maplist[modelnum].dt*3)
	}
return starttime;
}

//------------------------------turns the Debug console on or off--------------need to activate the button to make this active
function consoleonoff()
{
	if (document.getElementById("outerconsole").innerHTML!="")
		document.getElementById("outerconsole").innerHTML="";
	else
		document.getElementById("outerconsole").innerHTML="<textarea id=\"console\" cols=\"80\" rows=\"10\">"
}


function ModelChangeSelect()
{

var length=hdfvars.length
var domainlen;
var selectelement = document.getElementById("domain");
var selectmaps = document.getElementById("level");
var selectmodel = document.getElementById("model").value;
var localmodelnum=0;
var mapslen=0;
var timelen=0;
//alert(selectmodel);
for (var i=0; i<length; i++){
	if (hdfvars[i].model==selectmodel){
			domainlen=hdfvars[i].domainabbr.length;
			var modelnum=i;
			i=length+1;
		}
	}
	selectelement.options.length = 0;
	for (var j=0; j<domainlen; j++){
		selectelement.options[j] = new Option(hdfvars[modelnum].domainlong[j], hdfvars[modelnum].domainabbr[j]);
	}
	mapslen=hdfvars[modelnum].maps.length;
	selectmaps.options.length = 0;
	for (var j=0; j<mapslen; j++){
		selectmaps.options[j] = new Option(hdfvars[modelnum].mapsabbr[j], hdfvars[modelnum].maps[j]);
	}
	drawModelrunSelect(selectmodel);
}

function drawModelrunSelect(model){
	var selectrun = document.getElementById("modelrun");
	var	timelen=modeltimevars.length;
	var thetitle="";
	var optioncount=0;
	
	selectrun.options.length = 0;
	for (var j=0; j<timelen; j++){
		if (modeltimevars[j].name==model && !isNaN(modeltimevars[j].hour)){
            if (modeltimevars[j].modelrun==2){
                thetitle=modeltimevars[j].hour+"z (Unfinished)";
			}
			else {
				thetitle=modeltimevars[j].hour+"z";
            }
			selectrun.options[optioncount] = new Option(thetitle, modeltimevars[j].modelrun )	
			optioncount++;
		}
	}
}

function DrawMapSelects()
{
	var modellength=hdfvars.length;
	var levellength=maptypevarssel.length;
//	var levellength=maptypevarssel.length;
	var domainlength;
	
	var domainlist=new Array();
	var modexistflag=0;
	var localmodelnum=0;
	

//	alert(initialmodel+"====initialmodel");	
	for (var i=0; i<modellength; i++)
	{
		var selectelement = document.getElementById("model");
		selectelement.options[selectelement.options.length] = new Option(hdfvars[i].model, hdfvars[i].model);
			if (initialmodel==hdfvars[i].model)
			{
				selectelement.selectedIndex=(selectelement.options.length-1);
				var selectedmodel=hdfvars[i].model;
				localmodelnum=i;
				
			}
	}



	for (var i=0; i<modellength; i++)
	{
		domainlength=hdfvars[i].domainabbr.length
		for (var j=0; j<domainlength; j++)
		{
			for (var k=0; k<domainlist.length; k++)	
			{
				if (hdfvars[i].domainabbr[j]==domainlist[k])
				{
					modexistflag=1;
					//---model exists already move on
				}
			}
			if (modexistflag!=1)
			{
				var domsel = document.getElementById("domain");

				domsel.options[domsel.options.length] = new Option(hdfvars[i].domainlong[j], hdfvars[i].domainabbr[j]);
				if (initialdomain==hdfvars[i].domainabbr[j])
				{
					domsel.selectedIndex=(domsel.options.length-1);
				}
			}
		}
	}
	var levellength=hdfvars[localmodelnum].maps.length;
//	alert(levellength);
	for (var i=0; i<levellength; i++)
	{
		var levsel = document.getElementById("level");

		levsel.options[levsel.options.length] = new Option(hdfvars[localmodelnum].mapsabbr[i], hdfvars[localmodelnum].maps[i]);
//		levsel.options[levsel.options.length] = new Option(maptypevarssel[i].abbr, maptypevarssel[i].name);
	}
	if (selectedmodel != "GFS"){ ModelChangeSelect(); }
}
	var jg = new jsGraphics("divmapui");
	var divmapo;
	var divmapui;
	var divcontainer;
	var gDelay = 333;
	var timeout_id;
	var tester=new Image;
	var gLeftX;
	var gCalWidth;
	var IndMod;
	gLeftX=396;
	gCalWidth=364;
	document.MapLoaded = 1;
	divmapo = document.getElementById("divmap");
	divmapui = document.getElementById("divmapui");
	divcontainer = document.getElementById("divcontainer");
	document.jg = jg;
	document.divmapo = divmapo;

	document.divmapui = divmapui;
	document.divcontainer = divcontainer;
	window.onload = function() { 
		DrawMapSelects();
		AddNewLine(initialmodel,"SURPRE",initialdomain,0,0);
		NewChangeImage(0,12)	
		document.onkeydown = register;	
		var x = document.forms[0].elements[0];	
		x.onkeydown = register;	
		document.onmousemove = drawUI;
		document.onmouseup = clickUI;
	}
