ontextMenu = null;
var ID_ADD	= 1001;
var ID_MODIFY = 1002;
var ID_DELETE = 1003;
var envNameList = null;
var envIdList = null;
var bReadyToUse = false;
var scriptEngine;

var OnClickCB = null;
var OnBeforeMenuCB = null;
var cbList1BeforeLabelEdit = null;
var cbList1AfterLabelEdit = null;
var cbList1ColumnClick = null;
var cbList1KeyDown = null;
var cbList1MouseDown = null;
var cbList1DisplayContextMenu = null;

function window_onload()
{
	var wbTemp = window.external;
	wb = wbTemp;
	ds = wbTemp.AppletList.Applet("DSApplet").App;
	obj = ds.Tree.GetObject(ds.Tree.FirstActiveObjectID);
	stringTable = wbTemp.AppletList.WBApplet("DSApplet").StringTable;
	scriptEngine = wb.ScriptEngine.Engine;
	
    if(ds.GUIState && ds.GUIState.GUIDriver)
    {
	    ds.GUIState.GUIDriver.MenuListener.AddListener(3, this);
    }

	processStateCB = ds.OnProcessingStateFinished;
	processStateCB.AddCOM(this, "Processing_State_FinishedDelegate");
	lvChangeCB = ds.Script.lv.OnChange;
	lvChangeCB.AddCOM(this, "lv_ChangeDelegate");
	
	OnClickCB = CommandContainer1.OnClickDelegate;
    OnClickCB.AddCOM(this, "CommandContainer1_onclickDelegate");
    
    OnBeforeMenuCB = CommandContainer1.OnBeforeMenuDelegate;
    OnBeforeMenuCB.AddCOM(this, "CommandContainer1_onbeforemenuDelegate");
    
    cbList1BeforeLabelEdit = List1.OnBeforeLabelEdit;
    cbList1BeforeLabelEdit.AddCOM(this, "List1_BeforeLabelEditDelegate");
    
    cbList1AfterLabelEdit = List1.OnAfterLabelEdit;
    cbList1AfterLabelEdit.AddCOM(this, "List1_AfterLabelEditDelegate");

    cbList1ColumnClick = List1.OnColumnClick;
    cbList1ColumnClick.AddCOM(this, "List1_ColumnClickDelegate");

    cbList1KeyDown = List1.OnKeyDown;
    cbList1KeyDown.AddCOM(this, "List1_KeyDownDelegate");

    cbList1MouseDown = List1.OnMouseDown;
    cbList1MouseDown.AddCOM(this, "List1_MouseDownDelegate");

    cbList1DisplayContextMenu = List1.OnDisplayContextMenu;
    cbList1DisplayContextMenu.AddCOM(this, "List1_DisplayContextMenuDelegate");

	setTimeout("setList()", 250);
}

function ResizeList(bInvalidate)
{
	if(bInvalidate)
	{
		wndHelper.SetRedraw(List1.hWnd,false);
		wndHelper.SetRedraw(List1.hWndColumnHeader,false);
	}

	var countGrid = List1.ListItems.Count;
	if(countGrid < initialCountsPerPageList1)
	{
		if(countGrid > 0)
		{
			var height = List1.ListItems.Item(1).Height;
			List1.style.height = (countGrid + 1.4 ) * height + columnHeaderHeightList1;
		}
		else
			List1.style.height = 2 * columnHeaderHeightList1;
	}
	else
	{
		if(List1.style.posHeight != initialPixelHeightList1)
			List1.style.height = initialPixelHeightList1;
	}

	if(bInvalidate)
	{
		wndHelper.SetRedraw(List1.hWnd,true);
		wndHelper.SetRedraw(List1.hWndColumnHeader,true);

		wndHelper.Invalidate(List1.hWnd,true);
		wndHelper.Invalidate(List1.hWndColumnHeader,true);
	}
}

function setList()
{
    initCommandContainer();

	setListProps();
	setVars();

	wndHelper.SetRedraw(List1.hWndColumnHeader,false);
	wndHelper.SetRedraw(List1.hWnd,false);

	//clear any old data from the list
	List1.ColumnHeaders.Clear();

	setColumnHeaders();

	List1.ListItems.Clear();

	var rectTemp = wndHelper.GetWindowRect(List1.hWndColumnHeader);
	columnHeaderHeightList1 =  rectTemp.Height;

	addListItems(false);

	var count = List1.ColumnHeaders.Count;
	for(var i=1; i<=count; i++)
	{
		sortArr[List1.ColumnHeaders(i).Index] = descending;
	}
	List1.Font.Name = stringTable.String("ID_VSFlexGridTahoma");
	List1.Font.Size = 8.25;

	List1.AutoSizeColumns(0);

	ResizeList(false);

	wndHelper.SetRedraw(List1.hWndColumnHeader,true);
	wndHelper.SetRedraw(List1.hWnd,true);

	wndHelper.Invalidate(List1.hWnd,true);
	wndHelper.Invalidate(List1.hWndColumnHeader,true);


	pageDone = true;
	bReadyToUse = true;

	ds.Script.CheckIfWorksheetInReport();
}

function setVars()
{
	initialCountsPerPageList1 = List1.GetCountPerPage();
	initialPixelHeightList1 = .93 * document.body.offsetHeight;
	pageName.innerText = obj.Name;
	instructionText.innerText = stringTable.String("ID_VariableManagerScenarioText");
	sortArr		= new Array();
	wndHelper	= scriptEngine.CreateActiveXObject( scriptEngine.GenWBProgId("WBControls.WBWndHelper") );
	rect		= scriptEngine.CreateActiveXObject( scriptEngine.GenWBProgId("WBControls.WBRect") );
	envNameList = new Array();
	envIdList	= new Array();

	var idx = 0;
	var envList = ds.Tree.FirstActiveBranch.Model.Environments;
	var count = envList.Count;
	for(var i = 0; i < count; i++)
	{
		var envId = envList(i+1).ID;
		if(!IsEnvironmentValid(envId)) continue;

		envNameList[idx] = envList(i+1).Name;
		envIdList[idx++] = envId;
	}
}

function setListProps()
{
	List1.BorderStyle	= 1;	//no border
	List1.Appearance	= 0;	//flat appearance
	List1.GridLines		= true;	//show gridlines in list
	List1.BackColor		= wb.GetWindowColor();
	List1.Font.Name		= stringTable.String("ID_VSFlexGridTahoma");
	List1.Font.Size		= 8.25;
	List1.Checkboxes	= true;
	List1.LabelEdit     = 1;
	List1.View			= 3;
	List1.GridLines		= true;
	List1.FullRowSelect = true;
	List1.MultiSelect	= false;
	List1.HideSelection = true;
}

function addListItems(bValidate)
{
	if(bValidate)
	{
		wndHelper.SetRedraw(List1.hWnd,false);
	}

	var index = 0;
	for(var i=0; i<obj.NumLoadCases; i++)
	{
		var subindex = 0;
		var item = List1.ListItems.Add(++index, "", "", null, null);
		item.Checked = obj.LoadCaseInUse( i );
		item.ListSubItems.Add(++subindex, "a"+subindex, obj.Coefficient( i ), null, null);

		var id = obj.EnvironmentId(i);
		var bIsEnvValid = false;
		if(IsEnvironmentValid(id)) bIsEnvValid = true;
		var nameText = "";
		for(var j=0; j<envIdList.length; j++)
		{
			if( envIdList[j] == id )
				nameText = envNameList[j];
		}

		var subItem = item.ListSubItems.Add(++subindex, "a"+subindex, nameText, null, null);
		subItem.Tag = id;

		if( 0 == id || !bIsEnvValid )
			subItem.BkgndColor = color(255, 255, 0);
		else
			subItem.BkgndColor = color(255, 255, 255);

		var time = 0;
        var timeStr = "";
        time = obj.Time( i );
        if (time == 0)
            timeStr = stringTable.string("ID_EndTime");
        else
            timeStr = time;
        item.ListSubItems.Add(++subindex, "a"+subindex, timeStr, null, null);

		if(time < 0)
			subItem.BkgndColor = color(255, 255, 0);
		else
			subItem.BkgndColor = color(255, 255, 255);
	}
	if(bValidate)
	{
		wndHelper.SetRedraw(List1.hWnd,true);
		wndHelper.Invalidate(List1.hWnd,true);
	}
}

function setColumnHeaders()
{
	var pageSize = List1.scrollWidth;
	var initialSize = pageSize/4;
	var index = 0;

	//List1.ColumnHeaders.Add(index, key, text, width, align, icon)
	List1.ColumnHeaders.Add( ++index, "Active", "",	initialSize, 2, null ); //do not translate
	List1.ColumnHeaders.Add( ++index, stringTable.String("ID_Coefficient"), stringTable.String("ID_Coefficient"),	initialSize, 2, null );
	List1.ColumnHeaders.Add( ++index, stringTable.String("ID_EnvironmentName"), stringTable.String("ID_EnvironmentName"),	initialSize, 2, null );
    List1.ColumnHeaders.Add( ++index, stringTable.String("ID_Time"), stringTable.String("ID_Time"), initialSize, 2, null );
	List1.ColumnHeaders.Add( ++index, "", "", initialSize, 2, null );
}

function List1_ColumnClickDelegate(sender, args)
{
    List1_ColumnClick(args.ColumnHeader);
}

function List1_ColumnClick(currentCol)
{
 /*
	if(List1.ListItems.Count > 1)  //can't sort unless there is more than one item
	{
		//the mode, freq, min and max col should sort numerically, the others alphabetically
		if( currentCol.Key == stringTable.String("ID_Coefficient") )
		{
			//use numeric sorting
			sortVal = sortArr[currentCol.Index];
			List1.SortItems(currentCol.Index, sortVal, byNumber);
			if(sortArr[currentCol.Index] == ascending)
				sortArr[currentCol.Index] = descending;
			else
				sortArr[currentCol.Index] = ascending;
		}
		else
		{
			//use alphabetic sorting
			sortVal = sortArr[currentCol.Index];
			List1.SortItems(currentCol.Index, sortVal, byText);
			if(sortArr[currentCol.Index] == ascending)
				sortArr[currentCol.Index] = descending;
			else
				sortArr[currentCol.Index] = ascending;
		}
	}
 */
}

function Processing_State_FinishedDelegate(sender, args)
{
	Processing_State_Finished(args.Type);
}

function Processing_State_Finished(processCode)
{
	if(processCode == 2)
	{
		//after the solve has completed, update the information in the grid
		setList();
	}
}

function refreshPage()
{
	setList();
}

function lv_ChangeDelegate(sender, args)
{
    lv_Change();
}

function lv_Change()
{
	if(modeCount != obj.NumberRequested)
	{
		//update the list if the user changes the number of modes to find
		setList();
		modeCount = obj.NumberRequested;
	}
}

function window_onunload()
{
	obj = null;

	if(ds && ds.GUIState && ds.GUIState.GUIDriver)
        ds.GUIState.GUIDriver.MenuListener.RemoveListener(3);

	if(lvChangeCB)
	{
		lvChangeCB.RemoveCOM(this, "lv_ChangeDelegate");
		lvChangeCB = null;
	}
	if(processStateCB)
	{
	    processStateCB.RemoveCOM(this, "Processing_State_FinishedDelegate");
		processStateCB = null;
	}

	if (OnClickCB)
	{
	    OnClickCB.RemoveCOM(this, "CommandContainer1_onclickDelegate");
	    OnClickCB = null;
	}
	
	if (OnBeforeMenuCB)
	{
	    OnBeforeMenuCB.RemoveCOM(this, "CommandContainer1_onbeforemenuDelegate");
	    OnBeforeMenuCB = null;
	}
	
	if (cbList1BeforeLabelEdit)
	{
	    cbList1BeforeLabelEdit.RemoveCOM(this, "List1_BeforeLabelEditDelegate");
	    cbList1BeforeLabelEdit = null;
	}
	
	if (cbList1AfterLabelEdit)
	{
	    cbList1AfterLabelEdit.RemoveCOM(this, "List1_AfterLabelEditDelegate");
	    cbList1AfterLabelEdit = null;
	}

	if (cbList1ColumnClick)
	{
	    cbList1ColumnClick.RemoveCOM(this, "List1_ColumnClickDelegate");
	    cbList1ColumnClick = null;
	}

	if (cbList1KeyDown)
	{
	    cbList1KeyDown.RemoveCOM(this, "List1_KeyDownDelegate");
	    cbList1KeyDown = null;
	}

	if (cbList1MouseDown)
	{
	    cbList1MouseDown.RemoveCOM(this, "List1_MouseDownDelegate");
	    cbList1MouseDown = null;
	}

	if (cbList1DisplayContextMenu)
	{
	    cbList1DisplayContextMenu.RemoveCOM(this, "List1_DisplayContextMenuDelegate");
	    cbList1DisplayContextMenu = null;
	}
}

function exportDataToFile( fileName )
{
	var fileText = createTextForFile();
	ds.Script.WriteTextInFile(fileName, fileText);
}

function createTextForFile()
{
	var fileText = "";

	var count = List1.ColumnHeaders.Count;

	//write the column titles to the file
	for(var i=1; i<=count; i++)
	{
		fileText += List1.ColumnHeaders.Item(i).Text;
		fileText += "\t";
	}
	fileText += "\n";

	var itemCount = List1.ListItems.Count;
	for(i=1; i<=itemCount; i++)
	{
		fileText += List1.ListItems.Item(i).Text;
		fileText += "\t";

		var subItemCount = List1.ListItems.Item(i).ListSubItems.Count;
		for(var j=1; j<=subItemCount; j++)
		{
			fileText += List1.ListItems.Item(i).ListSubItems(j).Text;
			fileText += "\t";
		}

		fileText += "\n";
	}

	return fileText;
}

function List1_DisplayContextMenuDelegate(sender, args)
{
    List1_DisplayContextMenu(args.X, args.Y);
}

function List1_DisplayContextMenu(x,y)
{
	if(contextMenu != null)
	{
		rect.Left = x;
		rect.Top = y;
		rect.Width = 1;
		rect.Height = 1;

		var rectConvert = wndHelper.ScreenToClient(List1.hWnd,rect);
		XDuringCM = rectConvert.Left;
		YDuringCM = rectConvert.Top;

		contextMenu.TrackpopupMenu(x,y);
	}
}

function initCommandContainer()
{
	CommandContainer1.Init();
	CommandContainer1.RestoreSettings();
	var mbar = CommandContainer1.MenuBars.Add("MenuBar1");
	var menu1 = mbar.Menus.Add("Menu1");

	//For the menu item type
	var MT_STRING = 1;
	var MT_SEPARATOR = 2;

	menu1.MenuItems.AddMenuItem(MT_STRING, stringTable.string("ID_Add"), ID_ADD);
	menu1.MenuItems.AddMenuItem(MT_STRING, stringTable.string("ID_Modify"), ID_MODIFY);
	menu1.MenuItems.AddMenuItem(MT_STRING, stringTable.string("ID_Delete"), ID_DELETE);

	contextMenu = menu1;
}

function CommandContainer1_onclickDelegate(sender, args)
{
    CommandContainer1_onclick(args.EventName, args.EventID);
}

function CommandContainer1_onclick( bsName,menuID )
{
	if(ID_ADD == menuID)
	{
		document.AddNewRow();
	}
	else if(ID_MODIFY == menuID)
	{
		MakeEditable();
	}
	else if(ID_DELETE == menuID)
	{
		document.DeleteCurrentRow();
	}
}

function MakeEditable()
{
	var selItem = List1.SelectedItem;
	if(selItem == null)
		return;

	var listSubItem = List1.SubItemHitTest(XDuringCM,YDuringCM);
	if(listSubItem == null)
		return;

	if(listSubItem.Index == 1 || listSubItem.Index == 2 || listSubItem.Index == 3)
	{
		listSubItem.StartLabelEdit();
		return;
	}
}

function document.DeleteCurrentRow()
{
	if(List1.SelectedItem != null)
	{
		var index = List1.SelectedItem.Index - 1; //subtract 1 to account for the column headers
		//List1.ListItems.Remove(index);
		obj.DeleteLoadCase(index);
		List1.ListItems.Clear();
		addListItems(true);
		ResizeList(true);
	}
}

function document.AddNewRow()
{
	obj.AddLoadCase();
	var numCases = obj.NumLoadCases;
	var objIndex = numCases-1;

	var lstItem = List1.ListItems.Add(null,null,"");
	lstItem.Checked = obj.LoadCaseInUse( objIndex );

	var listSubItem = null;
	var subindex = 0;

	listSubItem = lstItem.ListSubItems.Add(++subindex, "a"+subindex, obj.Coefficient( objIndex ), null, null);
	listSubItem = lstItem.ListSubItems.Add(++subindex, "a"+subindex, "", null, null);
	if( 0 == obj.EnvironmentId( objIndex ) )
		listSubItem.BkgndColor = color(255, 255, 0);
	else
		listSubItem.BkgndColor = color(255, 255, 255);

	listSubItem = lstItem.ListSubItems.Add(++subindex, "a"+subindex, obj.Time( objIndex ), null, null);

	ResizeList(true);
}

function color(red, green, blue)
{
	return red + (256*green) + (65536*blue);
}

function CommandContainer1_onbeforemenuDelegate(sender, args)
{
    CommandContainer1_onbeforemenu(sender);
}

function CommandContainer1_onbeforemenu( menu )
{
	if(null == List1.SelectedItem)
	{
		//disable Modify and Delete
		menu.MenuItems(1).Enable = true;
		menu.MenuItems(2).Enable = false;
		menu.MenuItems(3).Enable = false;
	}
	else
	{
		menu.MenuItems(1).Enable = true;
		menu.MenuItems(2).Enable = true;
		menu.MenuItems(3).Enable = true;
	}
}

function List1_BeforeLabelEditDelegate(sender, args)
{
    List1_BeforeLabelEdit(args.ListItem, args.ListSubItem, args.Helper);
}

function List1_BeforeLabelEdit(lstItem, lstSubItem, lvHelper)
{
    if(lstItem == null || lstSubItem == null)
        return;

	var colTextString = List1.ColumnHeaders.Item(lstSubItem.Index+1).Text;
	if(colTextString == stringTable.String("ID_EnvironmentName"))
	{
		var WT_STATIC	= 2;
		var WT_EDITBOX	= 2;
		var WT_COMBOBOX	= 3;

	    lvHelper.InplaceControlType = WT_COMBOBOX;
	    lvHelper.ComboBox.ResetContent();

	    for( var i = 0; i < envNameList.length; i++ )
	    {
		    lvHelper.ComboBox.AddString(envNameList[i]);
	    }

	    if( envNameList.length > 0 )
	    {
		    var index = lvHelper.ComboBox.FindStringExact(0, lstSubItem.Text);
		    if(-1 == index)
		    {
			    lvHelper.ComboBox.Selected = 0;
		    }
		    else
			    lvHelper.ComboBox.Selected = index;
	    }
	}
}

function IsEnvironmentItem(lstItem,lstSubItem)
{
	if(lstItem != null)
	{
		if(lstSubItem != null)
		{
			var Index = lstSubItem.Index;

			if( List1.ColumnHeaders.Item(Index+1).Text == stringTable.String("ID_EnvironmentName"))
			{
				return true;
			}
		}
	}
	return false;
}

function IsEnvironmentValid(envId)
{
    if( !envId ) return false;
    
	var envObj = ds.Tree.GetObject(envId);
	if( !envObj ) return false;
	if( (envObj.AnswerSet == null) || (typeof(envObj.AnswerSet) == "undefined") )
		return false;
	
	var ansSetObj = ds.Tree.GetObject(envObj.AnswerSet.ID);
	if( (ansSetObj == null) || (typeof(ansSetObj) == "undefined") )
		return false;

	var physicsType = envObj.PhysicsType;
	var analysisType = envObj.AnalysisType;
	if( !(scriptEngine.id_PT_Mechanical == physicsType && scriptEngine.id_SimTypeStatic == analysisType ) )
		return false;
		
	return true;
}

function IsCoefficientItem(lstItem,lstSubItem)
{
	if(lstItem != null)
	{
		if(lstSubItem != null)
		{
			var Index = lstSubItem.Index;
			if( List1.ColumnHeaders.Item(Index+1).Text == stringTable.String("ID_Coefficient"))
			{
				return true;
			}
		}
	}
	return false;
}

function IsCheckBoxItem(lstItem,lstSubItem)
{
	if(lstItem != null)
	{
		if(lstSubItem != null)
		{
			var Index = lstSubItem.Index;
			if( List1.ColumnHeaders.Item(Index+1).Text == stringTable.String("ID_Coefficient"))
			{
				return true;
			}
		}
	}
	return false;
}

function IsTimeItem(lstItem,lstSubItem)
{
	if(lstItem != null)
	{
		if(lstSubItem != null)
		{
			var Index = lstSubItem.Index;
			if( List1.ColumnHeaders.Item(Index+1).Text == stringTable.String("ID_Time"))
			{
				return true;
			}
		}
	}
	return false;
}

function List1_MouseDownDelegate(sender, args)
{
    List1_MouseDown(args.Button, args.Modifiers, args.X, args.Y);
}

function List1_MouseDown(Button,Shift,x,y)
{
	var vbLeftButton = 1;

	if(vbLeftButton != Button)
	{
		return;
	}

	ModifyRow(x, y, true);
}

function ModifyRow(x, y, editDefault)
{
	var listItem = List1.HitTest(x,y);

	if(null == listItem)
		return;

	var objIndex = listItem.Index - 1;
	obj.LoadCaseInUse(objIndex) =  listItem.Checked;

	var bEditAlreadyStarted = false;
	var row = listItem.Index;

	var listSubItem = List1.SubItemHitTest(x,y);
	if(null == listSubItem)
		return;

	var col = listSubItem.Index;
	var colCount = listItem.ListSubItems.Count;

	if (col)
	{
		bEditAlreadyStarted = true;
		wndHelper.SetFocus(List1.hWnd);
		listSubItem.StartLabelEdit();
	}

	if(false == bEditAlreadyStarted && true == editDefault)
	{
		wndHelper.SetFocus(List1.hWnd);
		listItem.ListSubItems(colCount).StartLabelEdit();
	}
}

function List1_AfterLabelEditDelegate(sender, args)
{
    List1_AfterLabelEdit(args.ListItem, args.ListSubItem, args.Helper);
}

function List1_AfterLabelEdit(lstItem, lstSubItem, lvHelper)
{
	var WT_EDITBOX	= 2;
	var WT_COMBOBOX	= 3;
	var index, value, cur_col, cur_row, param;

	cur_col = lstSubItem.Index;
	objIndex = lstItem.Index - 1;
	no_col = List1.ColumnHeaders.Count;

	var bEnvironment = IsEnvironmentItem(lstItem, lstSubItem);
	var bCoeff = IsCoefficientItem(lstItem, lstSubItem);
	var bTime = IsTimeItem(lstItem, lstSubItem);

	obj.LoadCaseInUse(objIndex) =  lstItem.Checked;

	if(bEnvironment)
	{
		var selected = lvHelper.ComboBox.Selected;
		obj.EnvironmentId(objIndex) = envIdList[selected];

		if( 0 == obj.EnvironmentId( objIndex ) )
			lstSubItem.BkgndColor = color(255, 255, 0);
		else
			lstSubItem.BkgndColor = color(255, 255, 255);
			
	}
	else if(bCoeff)
	{
		try
		{
			value = ds.Script.ValidateNumberForCurrentLocale(lvHelper.LabelEditString);
			if( value == "failed" )
            {	
			    lvHelper.CancelLabelEdit = true;
			    return;
		    }
		    lvHelper.LabelEditString = value;
		}
		catch(e)
		{	
			lvHelper.CancelLabelEdit = true;
			return;
		}

		if (value > 1.0e30 || value < -1.0e30)
		{
			lvHelper.CancelLabelEdit = true;
			return;
		}
		else
			obj.Coefficient(objIndex) = value;
	}
	else if(bTime)
	{
		try
		{
			value = ds.Script.ValidateNumberForCurrentLocale(lvHelper.LabelEditString);
			if( value == "failed" )
            {	
			    lvHelper.CancelLabelEdit = true;
			    return;
		    }
		}
		catch(e)
		{	
			lvHelper.CancelLabelEdit = true;
			return;
		}

		// Check the time against the total end time
        var index = 0;
        for (var i = 1; i <= List1.ColumnHeaders.Count; i++)
        {
            if (List1.ColumnHeaders.Item(i).Text == stringTable.String("ID_EnvironmentName"))
                index = i;
        }

		// the envID is stored as the tag on the subitem. (needed because string compare won't handle dups.)
        var envID = lstItem.ListSubItems.Item(index-1).Tag;

	    var envObj = ds.Tree.GetObject(envID);
	    if( envObj != null )
	    {
			var totalEndTime = 0;
			try
			{
				totalEndTime = envObj.AnalysisSettings.GetAnalysisEndTime();
			}
			catch(e)
			{
				lvHelper.CancelLabelEdit = true;
				return;
			}

			if (value > totalEndTime || value < 0.0)
			{
				lvHelper.CancelLabelEdit = true;
				return;
			}

			obj.Time(objIndex) = value;
            if (value == 0)
            {
                lvHelper.LabelEditString = stringTable.string("ID_EndTime");
            }
		}
	}
	
	refreshPage();
}

function document.SetValueForList1(row,col,val)
{
	if(1 == col)
	{
		return "first column cannot be changed";
	}

	var lstItem = List1.ListItems(row);
	var lstSubItem = lstItem.ListSubItems(col-1);

	var index, value, tempVal, cur_col, cur_row, param;

	cur_col = lstSubItem.Index;
	objIndex = lstItem.Index - 1;

	no_col = List1.ColumnHeaders.Count;

	var bEnvironment = IsEnvironmentItem(lstItem, lstSubItem);
	var bCoeff = IsCoefficientItem(lstItem, lstSubItem);
	var bTime = IsTimeItem(lstItem, lstSubItem);

	if(bEnvironment)
	{
		var name = val;
		var selected = GetEnviromentIndexForName(name);
		if(-1 == selected)
		{
			return "not a valid environment name"
		}
		lstSubItem.Text = name;
		obj.EnvironmentId(objIndex) = envIdList[selected];

		if( 0 == obj.EnvironmentId( objIndex ) )
			lstSubItem.BkgndColor = color(255, 255, 0);
		else
			lstSubItem.BkgndColor = color(255, 255, 255);
	}
	else if(bCoeff)
	{
		value = val;
		try
		{
			tempVal = wb.Locale.CurrentLocaleString2Double(value);
		}
		catch(e)
		{
			tempVal = value;
		}
		if (isNaN(tempVal))
		{
			return "not a valid cofficient"
		}
		else
		{
			if (value > 1.0e30 || value < -1.0e30)
			{
				return "not a valid cofficient";
			}
			else
			{
				lstSubItem.Text = value;
				obj.Coefficient(objIndex) = value;
			}
		}
	}
	else if(bTime)
	{
		value = val;
		try
		{
			tempVal = wb.Locale.CurrentLocaleString2Double(value);
		}
		catch(e)
		{
			tempVal = value;
		}
		if (isNaN(tempVal))
		{
			return "not a valid time"
		}
		else
		{
			if (value > 1.0e30 || value < 0)
			{
				return "not a valid time";
			}
			else
			{
				lstSubItem.Text = value;
				obj.Time(objIndex) = value;
			}
		}
	}

	return "";
}

function document.ChooseScenarioInList1(row,bCheck)
{
	var lstItem = List1.ListItems(row);
	var objIndex = lstItem.Index - 1;
	lstItem.Checked = bCheck;
	obj.LoadCaseInUse(objIndex) =  lstItem.Checked;
}

function GetEnviromentIndexForName(name)
{
	var length = envNameList.length;
	var index = 0;
	for( index = 0; index < length; index++ )
	{
		if(name == envNameList[index])
		{
			return index;
		}
	}

	return -1;
}


function document.IsControlLoaded()
{
	return bReadyToUse;
}


function List1_KeyDownDelegate(sender, args)
{
    List1_KeyDown(args.KeyCode, args.Shift);
}

function List1_KeyDown(KeyCode, Shift)
{
	if (KeyCode == 46 )			// ascii for "delete" key
		document.DeleteCurrentRow();
}

//-->
</SCRIPT>
</HEAD>
<BODY LANGUAGE=javascript onload="return window_onload()" onunload="return window_onunload()">
	<DIV class="gradient">
		<H1 id=pageName>&nbsp;</H1>
	</DIV>
<br id="linebreak">
<P id = instructionText>&nbsp;</P>
<center>
<OBJECT id=CommandContainer1 classid=clsid:9524298E-9FA4-46A7-9974-B9BD790DB4A5 width = 1 height = 1 VIEWASTEXT></OBJECT>
<OBJECT id=List1 style="LEFT: 3px; WIDTH: 97%; TOP: 34px; HEIGHT: 93%"
classid=clsid:8FD1FC60-73C7-4847-8199-1AFD5724F190 width="97%" height="93%" VIEWASTEXT></OBJECT>
</center>
</BODY>
</HTML>

