var win = null; // Must declare for closeDep to work when win doesn't exist
var clockID = 0;

// *****************************************************
// Functions to save position on page across postbacks
// *****************************************************
function SmartScroller_GetCoords()
   {
      var scrollX, scrollY;
      
      if (document.all)
      {
         if (!document.documentElement.scrollLeft)
            scrollX = document.body.scrollLeft;
         else
            scrollX = document.documentElement.scrollLeft;
               
         if (!document.documentElement.scrollTop)
            scrollY = document.body.scrollTop;
         else
            scrollY = document.documentElement.scrollTop;
      }   
      else
      {
         scrollX = window.pageXOffset;
         scrollY = window.pageYOffset;
      }
     objx= document.getElementById('xCoord');
     objy= document.getElementById('yCoord');
     
      objx.value = scrollX;
      objy.value = scrollY;
   }
   
   function SmartScroller_Scroll()
   {
	  objx= document.getElementById('xCoord');
	  objy= document.getElementById('yCoord');
      var x = objx.value;
      var y = objy.value;
      window.scrollTo(x, y);
   }

// *****************************************************
// Functions to disable non-menu navigation
// *****************************************************

function disableNonmenuNav()
		{
		javascript:window.history.forward(1);
		var nav = window.Event ? true : false; 
		if (nav) { 
		window.captureEvents(Event.KEYDOWN); 
		window.onkeydown = NetscapeEventHandler_KeyDown; 
		} else { 
		document.onkeydown = MicrosoftEventHandler_KeyDown; 
		} 
		}

		function NetscapeEventHandler_KeyDown(e) { 
		if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit') { return false; } 
		return true; 
		} 

		function MicrosoftEventHandler_KeyDown() { 
		if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit') 
		return false; 
		return true; 
		}
// *****************************************************

function launch()
	{
		objPage = document.getElementById('pageString');	
		objPage.value = 'Settings';
		__doPostBack('lbPage','');
	}

function NewObj(url) 
{
	 objPage = document.getElementById('pageString');
	 objPage.value = url;
	 __doPostBack('lbPage','');			  
}

function ConfirmDelete(x,msg1,msg2, act) {
		   if (x == -1)
		   alert(msg1);
		   else
		   {
		   if (confirm(msg2)) 
		   {      
			  setAction(act);
			  __doPostBack('lbPage','');		
		   } //end confirmed clause
		  } //end else clause
	}
	
function ConfirmDeleteAction(x) {
	if (confirm('Are you sure?')) 
		   {      
			  setAction(x);
			  __doPostBack('lbPage','');		
		   }
	}
	
function AreYouSure(arg) {
	objSure = document.getElementById('areyousureValue');
	if (confirm('Are you sure?')) 
	{objSure.value = arg;}
	}
	
function genNewStrat(uname) {
	val = document.getElementById(uname);
	val2= document.getElementById('newName');
	y = val.value;
	val2.value = y;
	if (y==null) 
		x=0;
	else
		x = y.length ;
	if (x<3)
		{
		var msg1 = '';
		msg1 += 'Please enter a unique descriptive name ';
		msg1 += '\nfor the new strategy that is between ';
		msg1 += '\n3 and 40 characters in length.';
		alert(msg1);
		}
	else
	{setAction(999); //signal create new Strategy
	__doPostBack('','');		
	}
}

function genNewDS(uname) {  // called by clicking submit button in NewDS.ascx
	val = document.getElementById(uname); //uname is the uniqueID of tbName
	val2= document.getElementById('newName'); //in default.aspx
	y = val.value;
	val2.value = y;
	if (y==null) 
		x=0;
	else
		x = y.length ;
	if (x<3)
		{
		var msg1 = '';
		msg1 += 'Please enter a unique descriptive name ';
		msg1 += '\nfor the new dataset that is between ';
		msg1 += '\n3 and 40 characters in length.';
		alert(msg1);
		}
	else
	{setAction(999); //signal create new DS
	__doPostBack('','');		
	}
}

function genNewProj(uname) {
	val = document.getElementById(uname);
	val2= document.getElementById('newName');
	y = val.value;
	val2.value = y;
	if (y==null) 
		x=0;
	else
		x = y.length ;
	if (x<3)
		{
		var msg1 = '';
		msg1 += 'Please enter a unique descriptive name ';
		msg1 += '\nfor the new project that is between ';
		msg1 += '\n3 and 40 characters in length.';
		alert(msg1);
		}
	else
	{setAction(999); //signal create new project
	__doPostBack('','');		
	}
}

function genNewScen(uname) {
	val = document.getElementById(uname);
	val2= document.getElementById('newName');
	y = val.value;
	val2.value = y;
	if (y==null) 
		x=0;
	else
		x = y.length ;
	if (x<3)
		{
		var msg1 = '';
		msg1 += 'Please enter a unique descriptive name ';
		msg1 += '\nfor the new scenario that is between ';
		msg1 += '\n3 and 40 characters in length.';
		alert(msg1);
		}
	else
	{setAction(999); //signal create new scenario
	__doPostBack('','');		
	}
}

function genNewRes(uname) {
	val = document.getElementById(uname);
	val2= document.getElementById('newName');
	y = val.value;
	val2.value = y;
	if (y==null) 
		x=0;
	else
		x = y.length ;
	if (x<3)
		{
		var msg1 = '';
		msg1 += 'Please enter a unique descriptive name ';
		msg1 += '\nfor the new results set that is between ';
		msg1 += '\n3 and 40 characters in length.';
		alert(msg1);
		}
	else
	{setAction(999); //signal create new scenario
	__doPostBack('','');		
	}
}
//TM: --------------------
function genReports() {
    var falseCount = 0;
    var prfx = 'dch1__ctl0_';
    var cb = [];
    cb[0] = document.getElementById(prfx + 'cbSettings').checked;
    cb[1] = document.getElementById(prfx + 'cbStratSegs1').checked;
    cb[2] = document.getElementById(prfx + 'cbStratSegs2').checked;
    cb[3] = document.getElementById(prfx + 'cbStratSegs3').checked;
    cb[4] = document.getElementById(prfx + 'cbStratSegs4').checked;
    cb[5] = document.getElementById(prfx + 'cbStratISIC1').checked;
    cb[6] = document.getElementById(prfx + 'cbStratISIC2').checked;
    cb[7] = document.getElementById(prfx + 'cbProjSegs1').checked;
    cb[8] = document.getElementById(prfx + 'cbProjSegs2').checked;
    cb[9] = document.getElementById(prfx + 'cbProjSegs3').checked;
    cb[10] = document.getElementById(prfx + 'cbProjSegs4').checked;
    cb[11] = document.getElementById(prfx + 'cbProjSegs5').checked;
    cb[12] = document.getElementById(prfx + 'cbProjSegs6').checked;
    cb[13] = document.getElementById(prfx + 'cbProjISIC1').checked;
    cb[14] = document.getElementById(prfx + 'cbProjISIC2').checked;
    cb[15] = document.getElementById(prfx + 'cbProjISIC3').checked;
    cb[16] = document.getElementById(prfx + 'cbParams1').checked;
    cb[17] = document.getElementById(prfx + 'cbParams2').checked;
    cb[18] = document.getElementById(prfx + 'cbScen').checked;
    cb[19] = document.getElementById(prfx + 'cbRes1').checked;
    cb[20] = document.getElementById(prfx + 'cbRes2').checked;
    cb[21] = document.getElementById(prfx + 'cbRes3').checked;
    cb[22] = document.getElementById(prfx + 'cbRes4').checked;
    cb[23] = document.getElementById(prfx + 'cbRes5').checked;

    for (i=0; i < cb.length; i++) {
        if (cb[i] == true) {
            switch(i)
            {
            case 0:
                popUpPDF3(1,1);
                break;
            case 1:
                popUpPDF3(10,2);
                break;
            case 2:
                popUpPDF3(11,3);
                break;
            case 3:
                popUpPDF3(12,4);
                break;
            case 4:
                popUpPDF3(13,5);
                break;
            case 5:
                popUpPDF3(20,6);
                break;
            case 6:
                popUpPDF3(21,7);
                break;
            case 7:
                popUpPDF3(30,8);
                break;
            case 8:
                popUpPDF3(31,9);
                break;
            case 9:
                popUpPDF3(32,10);
                break;
            case 10:
                popUpPDF3(33,11);
                break;
            case 11:
                popUpPDF3(34,12);
                break;
            case 12:
                popUpPDF3(35,13);
                break;
            case 13:
                popUpPDF3(40,14);
                break;
            case 14:
                popUpPDF3(41,15);
                break;
            case 15:
                popUpPDF3(42,16);
                break;
            case 16:
                popUpPDF3(50,17);
                break;
            case 17:
                popUpPDF3(51,18);
                break;
            case 18:
                popUpPDF3(60,19);
                break;
            case 19:
                popUpPDF3(70,20);
                break;
            case 20:
                popUpPDF3(71,21);                
                break;
            case 21:
                popUpPDF3(72,22);                
                break;
            case 22:
                popUpPDF3(73,23);                
                break;
            case 23:
                popUpPDF3(74,24);
                break;
            }
        }
        else {
            falseCount = falseCount + 1;
        }
    }
    if (falseCount == 24) {
        alert('Please select at least one report to generate');
    } 
}

function selectAll() {
    var count = document.forms[0].elements.length;
    for (i=0; i<count; i++) {
        if (document.forms[0].elements[i].type == 'checkbox') {
            if (document.forms[0].elements[i].checked == false) {
                document.forms[0].elements[i].checked = true;
            }
        }
    }
}
function deselectAll() {
    var count = document.forms[0].elements.length;
    for (i=0; i<count; i++) {
        if (document.forms[0].elements[i].type == 'checkbox') {
            if (document.forms[0].elements[i].checked == true) {
                document.forms[0].elements[i].checked = false;
            }
        }
    }
}
//----------------------------------


function popupScen(URL) {
	winvars='toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=850,height=480,left = 60,top = 84, dependent=yes';
	win = window.open(URL, 'scenWindow', winvars); // a window object
	win.focus();
}

function popUpUserHelp() {
	winvars='toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=980,height=620,left = 5,top = 5, dependent=yes';
	win = window.open('help/UsersManual/default.htm', 'helpWindow', winvars); // a window object
	win.focus();
}

function popUpExercise1() {
	winvars='toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=980,height=620,left = 5,top = 5, dependent=yes';
	win = window.open('help/Exercise1/default.htm', 'exercise1Window', winvars); // a window object
	win.focus();
}

function popUpExercise2() {
	winvars='toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=980,height=620,left = 5,top = 5, dependent=yes';
	win = window.open('help/Exercise2/default.htm', 'exercise2Window', winvars); // a window object
	win.focus();
}

function popUpExercise3() {
	winvars='toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=980,height=620,left = 5,top = 5, dependent=yes';
	win = window.open('help/Exercise3/default.htm', 'exercise3Window', winvars); // a window object
	win.focus();
}

function popUpExercise4() {
	winvars='toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=980,height=620,left = 5,top = 5, dependent=yes';
	win = window.open('help/Exercise4/default.htm', 'exercise4Window', winvars); // a window object
	win.focus();
}

function popUpAbout() {
	winvars='toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=710,height=200,left=100,top = 150, dependent=yes';
	win = window.open('about.htm', 'aboutWindow', winvars); // a window object
	win.focus();
}

function popUpTplateLinks() {
	winvars='toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=730,height=450,left=100,top = 150, dependent=yes';
	win = window.open('templateLinks.htm', 'tlinksWindow', winvars); // a window object
	win.focus();
}

function popupRes(URL) {
	winvars='toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=520,left = 60,top = 84, dependent=yes';
	win = window.open(URL, 'resWindow', winvars); // a window object
	win.focus();
}

function popUpAdmin(URL) {
	winvars='toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=980,height=620,left = 5,top = 5, dependent=yes';
	win = window.open(URL, 'adminWindow', winvars); // a window object
	win.focus();
}

function popUpTrouble() {
	winvars='toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=930,height=620,left = 20,top = 20, dependent=yes';
	win = window.open('Trouble.aspx', 'troubleWindow', winvars); // a window object
	win.focus();
}

function popUpNotice1() {
	winvars='toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=930,height=620,left = 20,top = 20, dependent=yes';
	win = window.open('FHWApp.htm', 'pp1Window', winvars); // a window object
	win.focus();
}

function popUpNotice1a() {
	winvars='toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=930,height=620,left = 20,top = 20, dependent=yes';
	win = window.open('../FHWApp.htm', 'pp1Window', winvars); // a window object
	win.focus();
}

function popUpNotice1b() {
	winvars='toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=930,height=620,left = 20,top = 20, dependent=yes';
	win = window.open('../BCApp.htm', 'pp2Window', winvars); // a window object
	win.focus();
}

function popUpNotice1c() {
	winvars='toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=930,height=620,left = 20,top = 20, dependent=yes';
	win = window.open('../TOU.htm', 'pp3Window', winvars); // a window object
	win.focus();
}

function popUpNotice2() {
	winvars='toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=930,height=620,left = 20,top = 20, dependent=yes';
	win = window.open('BCApp.htm', 'pp2Window', winvars); // a window object
	win.focus();
}

function popUpHelpDir() {
	winvars='toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=930,height=620,left = 20,top = 20, dependent=yes';
	win = window.open('help', 'helpWindow', winvars); // a window object
	win.focus();
}
	
function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=750,height=570,left = 112,top = 84');");
}

function popUpPDF(qval) {
	winvars='titlebar=0,toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=750,height=520,left = 112,top = 84, dependent=yes';
	eval("win = window.open('pdfView.aspx?v=" + qval + "','pdfWindow', winvars);");	
	// win = window.open('pdfView.aspx?v=1', 'pdfWindow', winvars); // a window object
	win.focus();
}

function popUpPDF2(url) {
	winvars='titlebar=0,toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=750,height=520,left = 112,top = 84, dependent=yes';
	eval("win = window.open('pdfView.aspx?u=" + url + "','pdfWindow', winvars);");	
	// win = window.open('pdfView.aspx?v=1', 'pdfWindow', winvars); // a window object
	win.focus();
}
//TM: ----------------------------
function popUpPDF3(qval,num) {
	winvars='titlebar=0,toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=750,height=520,left = 112,top = 84, dependent=yes';
	eval("win = window.open('pdfView.aspx?v=" + qval + "','pdfWindow" + num + "', winvars);");	
	// win = window.open('pdfView.aspx?v=1', 'pdfWindow', winvars); // a window object
	win.focus();
}
//--------------------------------

function CancelSim() {
	 objPage = document.getElementById('pageString');
	 objPage.value = 'Simul';
	 __doPostBack('lbPage','');			  
}


function InitSim() {
	var s = document.getElementById('divSim');
	s.className = 'hideDiv';
    var td = document.getElementById("tdCancel");
    var d = td.getElementsByTagName("div").item(0);
	td.className = 'MainTDx0';
	d.className = 'showDiv';	
	StartClock();
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function KillClock() {
   if(clockID) {
      clearTimeout(clockID);
      obj = document.getElementById('lbTime');
      obj.value="";
	  clockID  = 0;
   }
}


function initClock() {
var clockID = 0;
var zDate = new Date();
var h = zDate.getHours();
var min = zDate.getMinutes();
var sec = zDate.getSeconds();
}

function UpdateClock() {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }
   var tDate = new Date();
   var clh = tDate.getHours();
   var clm = tDate.getMinutes();
   var cls = tDate.getSeconds();
   var sinc = 0
   var minc = 0
   var msinc = 0
   var hinc = 0
   var hminc = 0
   var tsec = ""
   if (cls>=sec) {
		sinc = -sec;
	}
		else   {
		sinc = 60-sec;
		msinc = 1;
	}
	
	if (clm>=min) {
		minc = -min - msinc;
	}
		else   {
		minc = 60 - min - msinc;
		hminc = 1;
	}
	
	if (clh>=h) {
		hinc = -h - hminc;
	}
		else   {
		hinc = 60 - h - hminc;
	}
		
	if  ((cls + sinc)<10) {
		tsec = "0" + (cls + sinc);
	}	
		else    {
		tsec = (cls + sinc);
	}

			objx = document.getElementById('lbTime');			   
			objx.value = "SIMULATION IN PROGRESS.  TIME ELAPSED: " 
                                   + (clm + minc) + ":" 
                                   + tsec ;
   
   clockID = setTimeout("UpdateClock()", 1000);
	}
	
function StartClock() {
		zDate = new Date();
		h = zDate.getHours();
		min = zDate.getMinutes();
		sec = zDate.getSeconds();
		//document.simView.lbTime.value = "";
		clockID = setTimeout("UpdateClock()", 500);
}

function enableControls(arg0,arg1,arg2,arg3,arg4,arg5,arg6,arg7) {
//Enable the first control (i.e., go button) and disable the rest
        obj = document.getElementById(arg0);
	    obj.disabled = false;
	    turnOff(arg1);
	    turnOff(arg2);
	    turnOff(arg3);
	    turnOff(arg4);
	    turnOff(arg5);
	    turnOff(arg6);
	    turnOff(arg7);	    	    
	    }

function turnOff(arg) 
{
    if (arg !== ''){objx = document.getElementById(arg); objx.disabled = true;}
}


function execDelete(argv,arg0,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11,arg12,arg13,arg14,arg15,arg16,arg17,arg18,arg19,arg20) 
        {
//if at least one checkbox is checked, run AreYouSure
        var arg = new Array()
        arg[0] = arg0;   arg[1] = arg1;   arg[2] = arg2;   arg[3] = arg3;   arg[4] = arg4;
        arg[5] = arg5;   arg[6] = arg6;   arg[7] = arg7;   arg[8] = arg8;   arg[9] = arg9;
        arg[10] = arg10; arg[11] = arg11; arg[12] = arg12; arg[13] = arg13; arg[14] = arg14;
        arg[15] = arg15; arg[16] = arg16; arg[17] = arg17; arg[18] = arg18; arg[19] = arg19;
        arg[20] = arg20;
                
        if (IsChecked(arg) == true)
        {        
        AreYouSure(argv);
	    }
	    else	    
	    alert('Nothing was checked.');    
	    }
	    
function IsChecked(arg) 
{
    var bval = false;
    var i;
    for (i in arg)
        {
            if (arg[i] == '') {return false;}
            objx = document.getElementById(arg[i]);            
            if (objx.checked == true){return true;}
        }
    return false;  
}
	    
function sleep(naptime){
     naptime = naptime * 1000;
     var sleeping = true;
     var now = new Date();
     var alarm;
     var startingMSeconds = now.getTime();
     //alert("starting nap at timestamp: " + startingMSeconds + "\nWill sleep for: " + naptime + " ms");
     while(sleeping){
        alarm = new Date();
        alarmMSeconds = alarm.getTime();
        if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; }
       }        
       //alert("Wakeup!");
}

