//Function to expand or collapse twisties based on user clicking twisities
function toggleSection(el) { 
	var element = document.getElementById(el + "Content");
	var image = document.images[el + "Arrow"];
	var text = document.getElementById(el + "Text");

	//determine if the twisty is expanded or not
	if (element.style.display == "none") {
		//expand
		element.style.display = "block";
		image.src = image.src.replace('twisty_up','twisty_down');
		text.innerHTML="Hide this section";	
	}
	else { //collapse
		element.style.display = "none";
		image.src = image.src.replace('twisty_down','twisty_up');
		text.innerHTML="Show this section";	
	}
}

//Function to expand or collapse twisties based on user clicking twisities
function toggleDay(el) { 
	var element = document.getElementById(el + "Content");
	var image = document.images[el + "Arrow"];
	var text = document.getElementById(el + "Text");

	//determine if the twisty is expanded or not
	if (element.style.display == "none") {
		//expand
		element.style.display = "block";
		image.src = image.src.replace('twisty_up','twisty_down');
		text.innerHTML="Hide Day";	
	}
	else { //collapse
		element.style.display = "none";
		image.src = image.src.replace('twisty_down','twisty_up');
		text.innerHTML="Show Day";	
	}
}

//Function to hide or show sections of a page using the <div> tag to designate the section and onLoad and onClick to trigger action.
function togglePanel(elemId, show)  {
	var elem = document.getElementById(elemId);
	if (elem) {
		if (show==true) {
			elem.style.display = "block";
		} else {
			elem.style.display = "none";
		}
	}
	return true;	
}

//Function to Switch between Basic and Advanced Search
function ToggleSearch(el) { 

	if (document.getElementById(el)) {
		document.getElementById(el).style.display = "none";
	}

	if (el.substring(0,3)=='adv') {
		if (document.getElementById('bas' + el.substring(3,el.length))) {
			document.getElementById('bas' + el.substring(3,el.length)).style.display = "";
		}
	} else {
		if (document.getElementById('adv' + el.substring(3,el.length))) {
			document.getElementById('adv' + el.substring(3,el.length)).style.display = "";
		}
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
//shen's functions start from here
//SelectAllSymbols(),CreateAlerts(),ChartThumbnail(),DeleteSymbols()
//////////////////////////////////////////////////////////////////////////////////////////////////////
function PopupWindow(theURL,winName,w,h) { //v2.0
     var winl = (screen.width - w) / 2;
     var wint = (screen.height - h) / 2;
     var winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable=0'

  	 window.open(theURL,winName,winprops);
}
function ToggleSelectAll(n) {
    
    var i,el,e2;
	var nn = '';
	if (arguments[0] != null) {
		nn = n
	}
	
	if ((document.getElementById("datagrid" + nn))) {
		e2 = document.getElementById("datagrid" + nn);
	}
	var text;
	if (document.getElementById("btnSelectAll"+nn)) {
		//var btnSrc = document.getElementById("btnSelectAll"+nn).src
        var btnSelectAll = document.getElementById("btnSelectAll"+nn).value;
        //alert("here:"+btnSelectAll);
		//if (btnSrc.indexOf('btn_selectall') != -1) {
        if (btnSelectAll.indexOf('Select') != -1) {
            for (i=0; i<e2.elements.length; i++) {
				el=e2.elements[i];
				if (el.type == 'checkbox' && el.id.substring(0,8) == 'cbSymbol') {
					el.checked = true;
				}
			}
			//btnSrc=btnSrc.replace('btn_selectall','btn_clearall');
            text = "Clear All";
        } else {
			for (i=0; i<e2.elements.length; i++) {
				el=e2.elements[i];
				if (el.type == 'checkbox' && el.id.substring(0,8) == 'cbSymbol') {
					el.checked = false;
				}
			}
			//btnSrc=btnSrc.replace('btn_clearall','btn_selectall');
            text = "Select All";
        }
		SetButtonText("btnSelectAll" + nn, text)
	}
	SetDataGridButtonStatus(n);
}

function ToggleShowHide(elname) {

	if (document.getElementById(elname)) {
		var btnSrc = document.getElementById("btnShow"+elname).src
		if (btnSrc.indexOf('btn_hide') != -1) {
			document.getElementById(elname).style.display = 'none';
			btnSrc=btnSrc.replace('btn_hide','btn_show');
		} else {
			document.getElementById(elname).style.display = 'block';
			btnSrc=btnSrc.replace('btn_show','btn_hide');
		}
		SetButtonText("btnShow"+elname, btnSrc)
	}
}

function ToggleBasicDetail(elname) {

	if (document.getElementById(elname)) {
		var btnSrc = document.getElementById("btnShow"+elname).src
		if (btnSrc.indexOf('btn_basicview') != -1) {
			document.getElementById(elname).style.display = 'none';
			document.getElementById("BasicView").style.display = 'block';
			btnSrc=btnSrc.replace('btn_basicview','btn_detailedview');
		} else {
			document.getElementById(elname).style.display = 'block';
			document.getElementById("BasicView").style.display = 'none';
			btnSrc=btnSrc.replace('btn_detailedview','btn_basicview');
		}
		SetButtonText("btnShow"+elname, btnSrc)
	}
}

// this is a big piece of init code to set initial status (enable of disable) of 
// certain buttons such as Delete Symbols, Create Alerts, and Chart Thumbnail
// and add event handler to relate checkboxes etc.
function seinit() {
	for (var i=0;i<10;i++) {
		k=i;
		if (i==0) k='';
		ToggleShowHide('Desc'+ k);
		ToggleShowHide('Indi' + k);
	}
	SetDataGridButtonStatus();
	AddEventHandlers();
	CheckFindButtonStatus();
	AddRowEventToSearchTable();	
}

function mpinit() {
	AddEventHandlers();
	DisableSaveButton();
	for (var i=0;i<10;i++) {
		k=i;
		SetDataGridButtonStatus(k);
		AddEventHandlers(k);
		DisableSaveButton(k);
	}
	DisableViewButton();
	DisableSideViewButton();
	SetDataGridButtonStatus();
}

function anainit() {
	AddSymbolAEventHandlers();
	txtSymbolAChanges();
	SetReportCbo();
}

function SetDataGridButtonStatus(n) {
	var oform;
	var nn = '';
	if (arguments[0] != null) {
		nn = n
	}

	// added by request of V.Beznosov, the following:
	// line 196: if (!document.getElementById("form errors")){
	// line 302: }
	// C.Soo 01/10/2006
//	if (!document.getElementById("form errors")){
		if ((document.getElementById("datagrid" + nn))) {
			oform = document.getElementById("datagrid" + nn);
			var bNoneSelected = true;
			var nSelectCounter = 0;
			for (var i=0; i<oform.elements.length; i++) {
				var el=oform.elements[i];
				if (el.type == 'checkbox' && el.id.substring(0,8) == 'cbSymbol') {
					if (el.checked) {
						bNoneSelected = false;
						nSelectCounter++
					}
				}
			}
			if (document.getElementById("btnSelectAll" + nn)) {
				if (bNoneSelected == true) {
					//var btnSrc = document.getElementById("btnSelectAll"+nn).src
					//btnSrc=btnSrc.replace('btn_clearall','btn_selectall');
                    text = "Select All";
                    SetButtonText("btnSelectAll" + nn, text)
				} else {
					//var btnSrc = document.getElementById("btnSelectAll"+nn).src
					//btnSrc=btnSrc.replace('btn_selectall','btn_clearall');
                     text = "Clear All";
                    SetButtonText("btnSelectAll" + nn, text)
				}
			}
		}
        
        // set btnDeleteSymbols
		if (document.getElementById("btnDeleteSymbols" + nn)) {
            if (bNoneSelected) {
					SetButtonStatus("btnDeleteSymbols" + nn,false);
			} else {
                SetButtonStatus("btnDeleteSymbols" + nn,true);
			}
		}

		// set btnCreateAlerts
		if (document.getElementById("btnCreateAlerts" + nn)) {
			if (bNoneSelected) {
				SetButtonStatus("btnCreateAlerts" + nn,false);
			} else {
				SetButtonStatus("btnCreateAlerts" + nn,true);
			}
		}
        
        // set btnDeleteAlerts
        if (document.getElementById("btnDeleteAlerts" + nn)) {
			if (bNoneSelected) {
				SetButtonStatus("btnDeleteAlerts" + nn,false);
			} else {
				SetButtonStatus("btnDeleteAlerts" + nn,true);
			}
		}        

        // set btnChartThumbnail
		if (document.getElementById("btnChartThumbnail" + nn)) {
			if (bNoneSelected) {
				SetButtonStatus("btnChartThumbnail" + nn,false);
			} else {
				SetButtonStatus("btnChartThumbnail" + nn,true);
			}
		}

		// set btnCreateAlerts
		if (document.getElementById("btnEditAlerts" + nn)) {
			if (bNoneSelected) {
				SetButtonStatus("btnEditAlerts" + nn,false);
			} else {
				SetButtonStatus("btnEditAlerts" + nn,true);
			}
		}

		// set btnCopyP
		if (document.getElementById("btnCopyP" + nn)) {
			if (bNoneSelected) {
				SetButtonStatus("btnCopyP" + nn,false);
			} else {
				SetButtonStatus("btnCopyP" + nn,true);
			}
		}

		// set btnCopyW
		if (document.getElementById("btnCopyW" + nn)) {
			if (bNoneSelected) {
				SetButtonStatus("btnCopyW" + nn,false);
			} else {
				SetButtonStatus("btnCopyW" + nn,true);
			}
		}

		// set btnSnapshot
		if (document.getElementById("btnSnapshot" + nn)) {
			if (bNoneSelected) {
				SetButtonStatus("btnSnapshot" + nn,false);
			} else {
				if (nSelectCounter == 1) {
					SetButtonStatus("btnSnapshot" + nn,true);
				} else {
					SetButtonStatus("btnSnapshot" + nn,false);
				}
			}
		}

		// set btnSaveSearch
		//if (document.getElementById("btnSaveSearch")) {
			//if (bNoneSelected) {
				//SetButtonStatus("btnSaveSearch",false);
			//} else {
			//	SetButtonStatus("btnSaveSearch",true);
		//	}
		//}

		// set btnPProReset
		if (document.getElementById("btnPProReset" + nn)) {
			if (bNoneSelected) {
				SetButtonStatus("btnPProReset" + nn,false);
			} else {
				SetButtonStatus("btnPProReset" + nn,true);
			}
		}
	//}
}

function AddSymbolAEventHandlers() {
	// for disabling btnCopyA
	if (document.getElementById("txtSymbolA")) {
		document.getElementById("txtSymbolA").onchange = txtSymbolAChanges;
		document.getElementById("txtSymbolA").onkeyup = txtSymbolAChanges;
	}
	
	if (document.getElementById("cboCSIList")) {
		document.getElementById("cboCSIList").onchange = cboCSIChanges;
	}
	
	//for page "Analyze_TechCharts_Static.html"
	//for page "Analyze_TechCharts_Static.html"
	var inputs = document.getElementsByTagName("INPUT"); 
	for (var i = 0; i < inputs.length; i++) { 
		if (inputs[i].type == 'text') {
			if (inputs[i].id.substring(0,11) == 'chartSymbol') {
					inputs[i].onchange = txtSymbolAChanges; 
					inputs[i].onkeyup = txtSymbolAChanges; 
			}
		}
	} 
	
	if (document.getElementById("txtCustomChart")) {
		document.getElementById("txtCustomChart").onchange = txtSymbolAChanges;
		document.getElementById("txtCustomChart").onkeyup = txtSymbolAChanges;
	}
	
	txtSymbolAChanges();
}

function txtSymbolAChanges() {
	if (document.getElementById("txtSymbolA")) {
		var el = document.getElementById("txtSymbolA");
		var currentValue = el.value;
		if (currentValue == "") {
			SetButtonStatus('btnAnalyzeA',false);
		} else {
			SetButtonStatus('btnAnalyzeA',true);
		}
		SetButtonStatus('btnCopyA',false);
	}
	
	if (document.getElementById("btnViewChartSymbol")) {
		var bEmpty = true;
		for (var i=0; i<11; i++) {
			if (document.getElementById("chartSymbol" + i)) {
				if (document.getElementById("chartSymbol" + i).value != "") {
					bEmpty = false;
				}
			}
		}
		if (bEmpty) {
			SetButtonStatus('btnViewChartSymbol',false);
		} else {
			SetButtonStatus('btnViewChartSymbol',true);
		}
	}
	
	if (document.getElementById("txtCustomChart")) {
		var el = document.getElementById("txtCustomChart");
		var currentValue = el.value;
		if (currentValue == "") {
			SetButtonStatus('btnCreateCustomChart',false);
		} else {
			SetButtonStatus('btnCreateCustomChart',true);
		}
	}

}

function cboCSIChanges() {
	var el = document.getElementById("cboCSIList");
	var currentIndex = el.selectedIndex;
	var currentValue = el.options[currentIndex].value;
	if (currentValue == "") {
		SetButtonStatus('btnCopyA',false);
	} else {
		SetButtonStatus('btnCopyA',true);
	}
}

function SetReportCbo() {
	if (document.getElementById("cboASector") 
			&& document.getElementById("cboAIndustry")
			&& document.getElementById("cboAReport")) {
		
		var el = document.getElementById("cboASector");
		var e2 = document.getElementById("cboAIndustry");
		var e3 = document.getElementById("cboAReport");
		
		var currentIndex = el.selectedIndex;
		var currentValue = el.options[currentIndex].value;
		if (currentValue == ";") {
			e2.disabled = true;
			e3.disabled = true;
		} else {
			e2.disabled = false;
			e3.disabled = false;
		}
	}
}

function AddEventHandlers(n) {
	var nn = '';
	if (arguments[0] != null) {
		nn = n
	}
	if ((document.getElementById("datagrid" + nn))) {
		var e2 = document.getElementById("datagrid" + nn);
		for (var i=0; i<e2.elements.length; i++) {
			var el=e2.elements[i];
			if (el.type == 'checkbox' && el.id.substring(0,8) == 'cbSymbol') {
				el.onclick = function() {SetDataGridButtonStatus(nn)};
			}
			if (el.type == 'checkbox' && el.id.substring(0,8) != 'cbSymbol') {
				el.onclick = function() {EnableBtnSave(nn)};
			}
			if (el.type == 'text') {
				el.onchange = function() {EnableBtnSave(nn)};
			}
		}
		//add event handler to other controls
	}
	//add event handler to list control
	if (document.getElementById("cboPList")) {
		document.getElementById("cboPList").onchange = CheckViewButtonStatus;
	}

	if (document.getElementById("cboWList")) {
		document.getElementById("cboWList").onchange = CheckViewButtonStatus;
	}

	// for home default page
	if (document.getElementById("cboSdWList")) {
		document.getElementById("cboSdWList").onchange = CheckSideWListButtonStatus;
		document.getElementById("cboPMMList").onchange = CheckSideWListButtonStatus;
		document.getElementById("cboWMMList").onchange = CheckSideWListButtonStatus;
	}

	// for Find Button on Power ProSearch
	if (document.getElementById("txtPProKWD")) {
		document.getElementById("txtPProKWD").onchange = CheckFindButtonStatus;
		document.getElementById("txtPProKWD").onkeyup = CheckFindButtonStatus;
	}

}

function EnableBtnSave(n) {
	var nn = '';
	if (arguments[0] != null) {
		nn = n
	}
	// set btnSave
	if (document.getElementById("btnSave" + nn)) {
		SetButtonStatus("btnSave" + nn,true);
	}
}

function DisableSaveButton(n) {
	var nn = '';
	if (arguments[0] != null) {
		nn = n
	}
	// set btnSave
	if (document.getElementById("btnSave" + nn)) {
		SetButtonStatus("btnSave" + nn,false);
	}
}

function CheckViewButtonStatus() {
	// set cboPList
	if (document.getElementById("cboPList")) {
		SetButtonStatus("btnViewPList",true);
	}
	// set cboWList
	if (document.getElementById("cboWList")) {
		SetButtonStatus("btnViewWList",true);
	}
}

function CheckSideWListButtonStatus() {
	// set buttons, for home page only
	if (document.getElementById("btnAnalyzerW")) {
		var currentIndex = document.getElementById("cboSdWList").selectedIndex;
		if (currentIndex == 0) {
			SetButtonStatus("btnViewSdWList",false);
			SetButtonStatus("btnEditW",false);
			SetButtonStatus("btnDeleteW",false);
			SetButtonStatus("btnAnalyzerW",false);
			SetButtonStatus("btnIndicatorW",false);
		} else {
			SetButtonStatus("btnViewSdWList",true);
			SetButtonStatus("btnEditW",true);
			SetButtonStatus("btnDeleteW",true);
			SetButtonStatus("btnAnalyzerW",true);
			SetButtonStatus("btnIndicatorW",true);
		}
		
		// set cboPMMList
		if (document.getElementById("cboPMMList")) {
			currentIndex = document.getElementById("cboPMMList").selectedIndex;
			if (currentIndex == 0) {
				SetButtonStatus("btnViewPMList",false);
			} else {
				SetButtonStatus("btnViewPMList",true);
			}
		}
		// set cboWMMList
		if (document.getElementById("cboWMMList")) {
			currentIndex = document.getElementById("cboWMMList").selectedIndex;
			if (currentIndex == 0) {
				SetButtonStatus("btnViewWMList",false);
			} else {
				SetButtonStatus("btnViewWMList",true);
			}
		}

	}
}

function DisableViewButton() {
	// set cboPWList
	if (document.getElementById("cboPList")) {
		SetButtonStatus("btnViewPList",false);
	}
	// set cboWList
	if (document.getElementById("cboWList")) {
		SetButtonStatus("btnViewWList",false);
	}
}

function CheckFindButtonStatus() {
	// set btnPProFind
	if (document.getElementById("btnPProFind")) {
		if (document.getElementById("txtPProKWD").value.length != 0) {
			SetButtonStatus("btnPProFind",true);
		} else {
			SetButtonStatus("btnPProFind",false);
		}
	}
}

function DisableSideViewButton() {
	CheckSideWListButtonStatus();
}

function ShowHideGroupDD(n) {
	var currentIndex;
	var nn = '';
	if (arguments[0] != null) {
		nn = n
	}
	if (document.getElementById("rowIndGroup"+nn)) {
		currentIndex = document.getElementById("cboIndSector"+nn).selectedIndex;
		if (currentIndex == 0) {
			document.getElementById("rowIndGroup"+nn).style.display = 'none';
		} else {
			document.getElementById("rowIndGroup"+nn).style.display = '';
		}
	}
}
//to chnage industry group dropdown content based on sector selection
var industryGrps=new Array();

industryGrps[1]="Vehicles/auto parts-equipment,Vehicles/auto rental-service,Vehicles/manufacturers,Vehicles/mobile-modular homes,Vehicles/motor homes,Vehicles/trucks-trailers".split(',');
industryGrps[2]="Beverages,Beverages/beer & ale,Beverages/bottlers,Beverages/distillers,Beverages/soft drink syrup,Beverages/wines and liquors".split(',');

function PopulateIndGroup(val,n) {
	var currentIndex;
	var nn = '';
	if (arguments[0] != null) {
		nn = n
	}
	var el = document.getElementById("indGroup"+nn);
	if (el) {
		el.options.length=0;
		var grp = industryGrps[val];
		el.options.length=grp.length;
		for (i=0;i<grp.length;i++){
			el.options[i].text=grp[i];
		}
	}
}

function ResetIISs(n) {
	var el;
	for (i=0;i<20;i++){
		if(document.getElementById("iis"+i)) {
			if (i != n) {
				document.getElementById("iis"+i).selectedIndex = 0;
			}
		}
	}
}

function SetButtonStatus(btnID, mode) {
	var el=document.getElementById(btnID);
    //alert("el:"+el);
    if (el) {
		var bStatus = IsDisabled(btnID);
        //alert("bStatus:"+bStatus);
        if (mode) {	//enable button
			if (bStatus) {
				//el.src = el.src.replace('_grey','');
                //alert("el.disabled:"+el.disabled);
                el.disabled = false;
			}
		} else {//disable button
			if (!bStatus) {
				//el.src = el.src.replace('.gif','_grey.gif');
				el.disabled = true;
			}
		}
	}
}

function IsDisabled(btnID) {
	var el=document.getElementById(btnID);
	if (el) {
		if (el.disabled) {
			return true;
		} else {
			return false;
		}
	}
}

function SetButtonText(btnID, text) {
	if (document.getElementById(btnID)) {
		//document.getElementById(btnID).src = btnImg;
        document.getElementById(btnID).value = text;
    }
}


//select the radio button within the selected row
//assume only one radio per row in one cloumn. Can be easily extented to muticolumns
function SelectRow(event){  
	//el = el.parentElement;
	el=this.childNodes;
	for (i=0;i<el.length;i++){
		if(el.item(i).nodeName != '#text') {
			el=el.item(i);
		}
	}
	el=el.childNodes;
	for (i=0;i<el.length;i++){
		if(el.item(i).nodeName == 'INPUT' && el.item(i).type=='radio') {
			el.item(i).checked = true;
			//more functions can be added to the onClick event of the radio button
			//so they can be excuted by the following code.
			//var myAtt = el.item(i).getAttribute('onclick');
			if (el.item(i).getAttribute('onclick') != null) {
				el.item(i).onclick();
			}
		}
	}
}

function AddRowEventToSearchTable() {
	var tbls = document.getElementsByTagName('TABLE');
	if (tbls) {
		for(var i=0; i<tbls.length; i++){
			if (tbls[i].id.substring(0,9)=='searchtbl') {
      			var tbl = tbls[i];
				var rows = tbl.getElementsByTagName('TR');
			    for(var j=0; j<rows.length; j++){
					rows[j].onclick=SelectRow;
					rows[j].onmouseover = rowmouseover;
					rows[j].onmouseout  = rowmouseout;
				}
			}
		}
	}
}

function rowmouseover(event) {
  this.style.backgroundColor='#F6EF9F';
}

function rowmouseout(event) {
  this.style.backgroundColor='';
}

function ShowHideIndicatorColumns(do_show) {

    var stl;
    if (do_show) {
		stl = '';
	} else {
        stl = 'none';
	}

	var tbl  = document.getElementById('SRTable0');
	
	if( tbl != null )
	{
	    var rows = tbl.getElementsByTagName('tr');
		var cels = rows[0].getElementsByTagName('td');
		var row;
		var col;
		
		for (col=9; col<cels.length;col++) {
			cels[col].style.display=stl;
		}
		
	    for (row=1; row<rows.length;row++) {
	      cels = rows[row].getElementsByTagName('td');
		  for (col=13; col<cels.length;col++) {
			cels[col].style.display=stl;
		  }
	    }
	 }
}

function DisplayIndList(SecID,index,indArray) {
	var eleID = SecID + index;
	var TempArray = new Array();
	var tbl = document.getElementById(SecID + 'IndList');
  	var lastRow = tbl.rows.length;
	while (lastRow > 1) {
		tbl.deleteRow(lastRow - 1);
		lastRow = tbl.rows.length;
	}
	var tbody = tbl.getElementsByTagName("TBODY")[0];
	TempArray = indArray[index][1];
	for (var j=0; j<TempArray.length; j++) {
	    var row = document.createElement("TR");
		if (j == 0) {
		    row.setAttribute('className', 'tableRowSelected'); //for IE
		    row.setAttribute('class', 'tableRowSelected');
		} else {
		    row.setAttribute('className', 'tableRow'); //for IE
		    row.setAttribute('class', 'tableRow');
		}
		row.onmouseover = rowmouseover;
		row.onmouseout  = rowmouseout;
		var td1 = document.createElement("TD");
		var ael = document.createElement('a');
		ael.appendChild(document.createTextNode(TempArray[j][1]));
		ael.setAttribute('href',"javascript:DisplayIndDesc('" + SecID + "','" + eleID + "_" + j + "',"+ SecID + "Section)" );
		td1.appendChild(ael);
		
		var td2 = document.createElement("TD");
		td2.appendChild(document.createTextNode(TempArray[j][2]));
		row.appendChild(td1);
		row.appendChild(td2);
		tbody.appendChild(row);
		//alert(tbody.innerHTML);
	}
}
function DisplayIndDesc(SecID, descID, indArray) {
	var i,j;
	var sSec;
	sSec = descID.split('_');
	for (i=0; i<indArray.length; i++) {
		if (indArray[i][0] == sSec[0]) {
			j=i;
			break;
		}
	}
	
	var sDesc = document.getElementById(SecID + "IndDesc");
	var TempArray = new Array();
	TempArray = indArray[j][1];
	for (i=0; i<TempArray.length; i++) {
		if (TempArray[i][0] == descID) {
			sDesc.innerHTML = TempArray[i][3];
			break;
		}
	}
}

// This generic function will dynamically set the innerHTML of the given
// element id with the text in the given array and index
function setSectionText(section, index, array) {
	var element = document.getElementById(section);
	if(element != null)
		element.innerHTML = array[index];
}

function SetCopyButtonStatus() {
	
}

// This function will dynamically set the width of dropdown menus
// Cindy Soo
// http://www.hedgerwow.com/360/dhtml/ui_select_with_fixed_width/demo.php
function foo(px,py,pw,ph,baseElement,fid)
{
		var win = document.getElementById(this.fid);
}


function dropdown_menu_hack(el)
{
	if(el.runtimeStyle.behavior.toLowerCase()=="none"){return;}
	el.runtimeStyle.behavior="none";

	var ie5 = (document.namespaces==null);
	el.ondblclick = function(e)
	{
		window.event.returnValue=false;
		return false;
	}
	
	if(window.createPopup==null)
	{
		
		var fid = "dropdown_menu_hack_" + Date.parse(new Date());
	
		window.createPopup = function()
		{
			if(window.createPopup.frameWindow==null)
			{
					el.insertAdjacentHTML("AfterEnd","<iframe   id='"+fid+"' name='"+fid+"' src='about:blank'  frameborder='1' scrolling='no'></></iframe>");
					var f = document.frames[fid];
					f.document.open();
					f.document.write("<html><body></body></html>");
					f.document.close();
					f.fid = fid; 
					

					var fwin = document.getElementById(fid);
					fwin.style.cssText="position:absolute;top:0;left:0;display:none;z-index:99999;";
				
					
					f.show = function(px,py,pw,ph,baseElement)
					{							
						py = py + baseElement.getBoundingClientRect().top + Math.max( document.body.scrollTop, document.documentElement.scrollTop) ;
						px = px + baseElement.getBoundingClientRect().left + Math.max( document.body.scrollLeft, document.documentElement.scrollLeft) ;
						fwin.style.width = pw + "px";
						fwin.style.height = ph + "px";						
						fwin.style.posLeft =px ;
						fwin.style.posTop = py ;		
						fwin.style.display="block";						
					}

					
					f_hide = function(e)
					{ 
						if(window.event && window.event.srcElement	&& window.event.srcElement.tagName && window.event.srcElement.tagName.toLowerCase()=="select"){return true;}
						fwin.style.display="none";
					} 
					f.hide = f_hide;
					document.attachEvent("onclick",f_hide);		
					document.attachEvent("onkeydown",f_hide);		
					
			}
			return f;
		}
	}

	function showMenu()
	{
		
		function selectMenu(obj)
			{				
				var o = document.createElement("option");
				o.value = obj.value;
				o.innerHTML = obj.innerHTML;			
				while(el.options.length>0){el.options[0].removeNode(true);}
				el.appendChild(o);
				el.title =  o.innerHTML; 
				el.contentIndex = obj.selectedIndex  ;
				el.menu.hide(); 				
			}		
		
		
		el.menu.show(0 , el.offsetHeight , 10,  10, el); 
		var mb = el.menu.document.body;
		
		mb.style.cssText ="border:solid 1px black;margin:0;padding:0;overflow-y:auto;overflow-x:auto;background:white;text-aligbn:center;font-family:Verdana;font-size:11px;";
		var t = el.contentHTML;
		t = t.replace(/<select/gi,'<ul');
		t = t.replace(/<option/gi,'<li');
		t = t.replace(/<\/option/gi,'</li');
		t = t.replace(/<\/select/gi,'</ul');
		mb.innerHTML = t;	
	
		
		el.select = mb.all.tags("ul")[0];
		el.select.style.cssText="list-style:none;margin:0;padding:0;";
		mb.options = el.select.getElementsByTagName("li");
		
		for(var i=0;i<mb.options.length;i++)
		{
			mb.options[i].selectedIndex = i;
			mb.options[i].style.cssText = "list-style:none;margin:0;padding:1px 2px;width/**/:100%;cursor:hand;cursor:pointer;white-space:nowrap;"
			mb.options[i].title =mb.options[i].innerHTML;
			mb.options[i].innerHTML ="<nobr>" + mb.options[i].innerHTML + "</nobr>";
			mb.options[i].onmouseover = function()
				{
					if( mb.options.selected ){mb.options.selected.style.background="white";mb.options.selected.style.color="black";}
					mb.options.selected = this;
					this.style.background="#999999";this.style.color="white";
				}
			
			mb.options[i].onmouseout = function(){this.style.background="white";this.style.color="black";}
			mb.options[i].onmousedown = function(){selectMenu(this);	}
			mb.options[i].onkeydown = function(){selectMenu(this);	}
				

			if(i == el.contentIndex)
			{
				mb.options[i].style.background="#999999";
				mb.options[i].style.color="white";	
				mb.options.selected = mb.options[i];
			}
		}
	
		
		var mw = Math.max(   ( el.select.offsetWidth + 22 ), el.offsetWidth + 22  );
			 mw = Math.max(  mw, ( mb.scrollWidth+22) );
		var mh =  mb.options.length * 15  + 8 ; 
			 
		var mx = (ie5)?-3:0;
		var my = el.offsetHeight -2;
		var docH =   document.documentElement.offsetHeight ;
		var bottomH = docH  - el.getBoundingClientRect().bottom ; 

		mh = Math.min(mh, Math.max(( docH - el.getBoundingClientRect().top - 50),100)		);
		
		if(( bottomH < mh) )
		{
			
			mh = Math.max( (bottomH - 12),10);
			if( mh <100 ) 
			{
				my = -100 ;

			}
			mh = Math.max(mh,100);			
		}

		
		self.focus(); 
		
		el.menu.show( mx , my ,  mw, mh , el); 
		sync=null;
		if(mb.options.selected)
		{
			mb.scrollTop = mb.options.selected.offsetTop;
		}
	

		
		
		window.onresize = function(){el.menu.hide()};		
	}

	function switchMenu()
	{
		if(event.keyCode)
		{
			if(event.keyCode==40){ el.contentIndex++ ;}
			else if(event.keyCode==38){ el.contentIndex--; }
		}
		else if(event.wheelDelta )
		{
			if (event.wheelDelta >= 120)
			el.contentIndex++ ;
			else if (event.wheelDelta <= -120)
			el.contentIndex-- ;
		}else{return true;}




		if( el.contentIndex > (el.contentOptions.length-1) ){ el.contentIndex =0;}
		else if (el.contentIndex<0){el.contentIndex = el.contentOptions.length-1 ;}

		var o = document.createElement("option");
			 o.value = el.contentOptions[el.contentIndex].value;
			 o.innerHTML = el.contentOptions[el.contentIndex].text;
			 while(el.options.length>0){el.options[0].removeNode(true);}
			 el.appendChild(o);
			 el.title =  o.innerHTML; 
	}
	
	if(dropdown_menu_hack.menu ==null)
	{
		dropdown_menu_hack.menu =  window.createPopup();
		document.attachEvent("onkeydown",dropdown_menu_hack.menu.hide);
	}
	el.menu = dropdown_menu_hack.menu ;
	el.contentOptions = new Array();
	el.contentIndex = el.selectedIndex;
	el.contentHTML = el.outerHTML;

	for(var i=0;i<el.options.length;i++)
	{	
		el.contentOptions [el.contentOptions.length] = 
		{
			"value": el.options[i].value,
			"text": el.options[i].innerHTML
		}

		if(!el.options[i].selected){el.options[i].removeNode(true);i--;};
	}

	
	el.onkeydown = switchMenu;
	el.onclick = showMenu;
	el.onmousewheel= switchMenu;

}

//--------------------------------------------------------------------------------------
//the following block is special for the Alert page with only one form and many sections
//--------------------------------------------------------------------------------------
//initialization of the Alert page
function mpalertinit() {
	AddEventHandlersAlert();
	DisableSaveButton();
	for (var i=0;i<10;i++) {
		k=i;
		SetDataGridButtonStatusAlert(k);
		AddEventHandlersAlert(k);
		DisableSaveButton(k);
	}
	DisableViewButton();
	SetDataGridButtonStatusAlert();
}
//function to set select all and delete buttons
function SetDataGridButtonStatusAlert(n) {
	var nn = '';
	if (arguments[0] != null) {
		nn = n
	}

	if (!document.getElementById("form errors")){
		if ((document.getElementById("datagrid" + nn))) {
			var tbl = document.getElementById("datagrid" + nn);
			var bNoneSelected = true;
			var nSelectCounter = 0;
			
			var rows = tbl.getElementsByTagName('tr');
			for (var row=0;row<rows.length;row++) {
				var cels = rows[row].getElementsByTagName('td');
				for (var col=0;col<cels.length;col++) {
					var el = cels[col].childNodes;
					for (var i=0;i<el.length;i++) {
						if (el.item(i).type == 'checkbox' && el.item(i).id.substring(0,8) == 'cbSymbol') {
							if (el.item(i).checked) {
								bNoneSelected = false;
								nSelectCounter++
							}
						}
					}
				}
			}

			if (document.getElementById("btnSelectAll" + nn)) {
				if (bNoneSelected == true) {
					var btnSrc = document.getElementById("btnSelectAll"+nn).src
					btnSrc=btnSrc.replace('btn_clearall','btn_selectall');
					SetButtonText("btnSelectAll" + nn, btnSrc)
				} else {
					var btnSrc = document.getElementById("btnSelectAll"+nn).src
					btnSrc=btnSrc.replace('btn_selectall','btn_clearall');
					SetButtonText("btnSelectAll" + nn, btnSrc)
				}
			}
		}
		// set btnDeleteSymbols
		if (document.getElementById("btnDeleteSymbols" + nn)) {
			if (bNoneSelected) {
				SetButtonStatus("btnDeleteSymbols" + nn,false);
			} else {
				SetButtonStatus("btnDeleteSymbols" + nn,true);
			}
		}
	}
}
//function to toggle the select buttons
function ToggleSelectAllAlert(n) {
	var nn = '';
	if (arguments[0] != null) {
		nn = n
	}
	if (document.getElementById("btnSelectAll"+nn)) {
		var btnSrc = document.getElementById("btnSelectAll"+nn).src
	
		if (btnSrc.indexOf('btn_selectall') != -1) {
			checkboxSet("datagrid" + nn,true);
			btnSrc=btnSrc.replace('btn_selectall','btn_clearall');
		} else {
			checkboxSet("datagrid" + nn,false);
			btnSrc=btnSrc.replace('btn_clearall','btn_selectall');
		}
		SetButtonText("btnSelectAll" + nn, btnSrc)
	}
	SetDataGridButtonStatusAlert(n);
}
// function to set first column checkbox. 
function checkboxSet(tblID, bTF) {
	if (!document.getElementById) return;

	var tbl  = document.getElementById(tblID);
	
	if( tbl != null ) {
	    var rows = tbl.getElementsByTagName('tr');
		for (var row=0;row<rows.length;row++) {
			var cels = rows[row].getElementsByTagName('td');
			for (var col=0;col<cels.length;col++) {
				var el = cels[col].childNodes;
				//alert(el.length);
				for (var i=0;i<el.length;i++) {
					if (el.item(i).type == 'checkbox' && el.item(i).id.substring(0,8) == 'cbSymbol') {
						el.item(i).checked = bTF;
					}
				}
			}
		}
	}
}
//function to add event handlers
function AddEventHandlersAlert(n) {
	var nn = '';
	if (arguments[0] != null) {
		nn = n
	}
	
	if ((document.getElementById("datagrid" + nn))) {
		var tbl = document.getElementById("datagrid" + nn);
		var rows = tbl.getElementsByTagName('tr');
		for (var row=0;row<rows.length;row++) {
			var cels = rows[row].getElementsByTagName('td');
			for (var col=0;col<cels.length;col++) {
				var el = cels[col].childNodes;
				for (var i=0;i<el.length;i++) {
					if (el.item(i).type == 'checkbox' && el.item(i).id.substring(0,8) == 'cbSymbol') {
						el.item(i).onclick = function() {SetDataGridButtonStatusAlert(nn)};
					}
					if (el.item(i).type == 'checkbox' && el.item(i).id.substring(0,8) != 'cbSymbol') {
						el.item(i).onclick = function() {EnableBtnSave(nn)};
					}
					if (el.item(i).type == 'text') {
						el.item(i).onchange = function() {EnableBtnSave(nn)};
					}
				}
			}
		}
	}
}
//--------------------------------------------------------------------------------------
// End special script for the Alert page
//--------------------------------------------------------------------------------------


function changePortfolio(form) {
    form.submit();
    return false;
}