// close and open popup
var theWin

function popup(theURL) {

	if (theWin == null || theWin.closed) {
		theWin = window.open(theURL, "theWin", "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=525,height=650,top=50,left=200");
	} else {
		theWin.close()
		theWin = window.open(theURL, "theWin", "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=525,height=650,top=50,left=200");
	}
}

// hack to deal with image rollover flicker in IE
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch (e) { }

function removeSpecialCharacters(value, formField) {
	value = value.replace(/[^a-zA-Z 0-9 _]+/g, '');
	formField.value = value;
}

function UltraWebGridRow_Over(gridName, Id, objectType) {
	//debugger;
	if (objectType == 0) {
		var cell = igtbl_getElementById(Id);
		cell.style.cursor = 'hand'
        ;
		try {
			var Row = igtbl_getRowById(Id);
			var RowIndex = Row.getIndex();
			setRowColor(Row, "in", RowIndex);
		} catch (e) {

		}
	}
}

function UltraWebGridRow_Out(gridName, Id, objectType) {
	try {
		//debugger;
		var Row = igtbl_getRowById(Id);
		//var RowIndex = Row.getIndex();
		// Note from TIM: Changed to check the row style instead of using row index
		// since index can be out of synch when using ajax behavior of the grid (virtual scrolling).
		if (Row.FirstRow.className.search(/TrgIgGridRowAlternateStyle/) != -1) {
			setRowColor(Row, "out", 1);
		}
		else {
			setRowColor(Row, "out", 2);
		}

	} catch (e) {

	}
}

function setRowColor(Row, Direction, RowIndex) {
	var sColor = "#FFFFE6"
	if (Direction == "out") {
		if (RowIndex % 2 == 0) {
			sColor = "#F5F5F5";
		} else {
			sColor = "#dfdfdf";
		}
	}

	var cells = Row.getCellElements();
	for (var i = 0; i < cells.length; i++) {
		cells[i].style.backgroundColor = sColor;
	}
}

// These scripts are from Danko at Infragistics supposedly to solve the
// problem of a virtual scrolling hierarchical grid (can't view child elements outside view port).
var array = new Array();
var rCount = 0;

function uwgAfterRowCollapsedHandler(gridName, e) {
	for (var k = 0; k < rCount; k++) {
		if (array[k] == e) {
			array.remove(k);
			rCount--;
			break;
		}
	}
}
function uwgAfterRowExpandedHandler(gridName, e) {
	array[rCount] = e;
	rCount++;
}
function uwgAfterXmlHttpResponseProcessed(gridName, e) {
	var gn = document.getElementById(gridName);

	for (var k = 0; k < rCount; k++) {
		var row = document.getElementById(array[k]);
		//Expand row 
	}
}
// End of code from Danko

function uwgXmlInitializeLayout(gn) {
	//   debugger
	if (document.getElementsByName(gn)[0].id != null) {
		var gridId = document.getElementsByName(gn)[0].id;
		var grid = igtbl_getGridById(gridId);

		var tbl = grid.Element;
		var gridHeightPx = 0;
		var rowCount = grid.Rows.length;
		if (rowCount < grid.PageSize) {
			grid.MainGrid.style.height = "100%"
			var gridHeight = grid.Element.clientHeight;
			gridHeight = gridHeight == 0 ? 28 + rowCount * 32 : gridHeight;
			gridHeightPx = gridHeight; // + \"px\"
			grid.Element.style.height = gridHeightPx;
		}
		else {
			var headerCell = tbl.childNodes[1].firstChild;
			var gridHeight = headerCell.clientHeight;   // header row
			gridHeight = gridHeight == 0 ? 28 : gridHeight;
			for (i = 0; i <= grid.PageSize; i++) {
				var tableCell = tbl.childNodes[2].childNodes[i];
				if (tableCell != null) {
					var rowClientHeight = tableCell.clientHeight;
					rowClientHeight = rowClientHeight == 0 ? 32 : rowClientHeight;
					gridHeight = gridHeight + rowClientHeight;    // table row
				}
			}
			gridHeightPx = gridHeight; // + \"px\"
		}
		grid.DivElement.style.height = gridHeightPx;
		if (grid.XmlLoadOnDemandType != 0) {
			grid.DivElement.style.top = "-" + gridHeightPx;
			grid._scrElem.style.height = gridHeightPx;
			grid._scrElem.parentNode.style.height = gridHeightPx;
		}
		grid._tdContainer.style.height = gridHeightPx; // < _mc element>
		grid.MainGrid.firstChild.firstChild.style.height = gridHeightPx;  // < _mr element>
		grid.MainGrid.firstChild.style.height = gridHeightPx;   // < _mtb element>
		grid.MainGrid.style.height = gridHeightPx;
	}
}

// This doesn't seem to work.
//		function getEstHeight(obj) {
//		  var objStyle = obj.style;
//		  var visibility = objStyle.visibility;
//		  var position = objStyle.position; 
//		  var display = objStyle.display;        
//		  objStyle.visibility = \"visible\";        
//		  objStyle.position = \"absolute\";        
//		  objStyle.display = \"block\";        
//		  height = obj.clientHeight;        
//		  objStyle.visibility = visibility;        
//		  objStyle.position = position;        
//		  objStyle.display = display;        
//		  return parseInt(height);
//		}


/* 
functions below are used by generic fields
*/
var specifyFieldsArray = new Array();
function specifyField(controlId, controlType, index, specifyDivId) {
    this.controlId = controlId;
    this.controlType = controlType;
    this.index = index;
    this.specifyDivId = specifyDivId;
}

function addSpecifyField(controlId, controlType, index, specifyDivId) {
    specifyFieldsArray.push(new specifyField(controlId, controlType, index, specifyDivId))
}

function manageSpecifyFields() {
    try {
        for (i = 0; i < specifyFieldsArray.length; i++) {
            var speficyField = specifyFieldsArray[i];
            var specifyDiv = document.getElementById(speficyField.specifyDivId);
            var selectedFormField = document.getElementById(speficyField.controlId);
            hide(specifyDiv);
            
            switch(speficyField.controlType) {
                case "select-one":
                    if (speficyField.index == selectedFormField.selectedIndex) {
                        var controlBounds = Sys.UI.DomElement.getBounds(selectedFormField);
                        Sys.UI.DomElement.setLocation(specifyDiv, controlBounds.x + controlBounds.width + 5, controlBounds.y + 1);
                        show(specifyDiv);
                    }
                    break;
                case "checkbox":
                    var inputArray = selectedFormField.getElementsByTagName("input");
                    var labelArray = selectedFormField.getElementsByTagName("label");
                    if (inputArray[speficyField.index].checked) {
                        var labelBounds = Sys.UI.DomElement.getBounds(labelArray[speficyField.index]);
                        Sys.UI.DomElement.setLocation(specifyDiv, labelBounds.x + labelBounds.width + 5, labelBounds.y + 1);
                        show(specifyDiv);
                    }
                    break;
                case "radio":
                    var inputArray = selectedFormField.getElementsByTagName("input");
                    var labelArray = selectedFormField.getElementsByTagName("label");
                    if (inputArray[speficyField.index].checked) {
                        var labelBounds = Sys.UI.DomElement.getBounds(labelArray[speficyField.index]);
                        Sys.UI.DomElement.setLocation(specifyDiv, labelBounds.x + labelBounds.width + 5, labelBounds.y + 1);
                        show(specifyDiv);
                    }
                    break;
                default:
                    
            }

        }
    } catch (exc) {

    }
}

function specify(selectedFormField, specifyDivId, selectedIndex) {
    try {
        var specifyDiv
        if (specifyDivId != "") {
            specifyDiv = $get(specifyDivId);
        }
        
        switch (selectedFormField.type.toLowerCase()) {
            case "select-one":
                var selectedId = selectedFormField.id;
                var selectedIndex = selectedFormField.selectedIndex;

                for (i = 0; i < specifyFieldsArray.length; i++) {
                    var specifyDiv = $get(specifyFieldsArray[i].specifyDivId);
                    if (specifyFieldsArray[i].controlId == selectedId) {
                        if (specifyFieldsArray[i].index == selectedIndex) {
                            var controlBounds = Sys.UI.DomElement.getBounds(selectedFormField);
                            Sys.UI.DomElement.setLocation(specifyDiv, controlBounds.x + controlBounds.width + 5, controlBounds.y + 1);
                            show(specifyDiv);
                        } else {
                            hide(specifyDiv);
                        }
                    }
                }

                break;
            case "checkbox":
                if (selectedFormField.checked) {
                    var labelArray = selectedFormField.parentNode.getElementsByTagName("label");
                    var labelBounds = Sys.UI.DomElement.getBounds(labelArray[selectedIndex]);
                    Sys.UI.DomElement.setLocation(specifyDiv, labelBounds.x + labelBounds.width + 5, labelBounds.y + 1);
                    show(specifyDiv);
                } else {
                    hide(specifyDiv);
                }
                break;
            case "radio":
                selectedControlId = selectedFormField.parentNode.id;
                
                for (i = 0; i < specifyFieldsArray.length; i++) {
                    if (specifyFieldsArray[i].controlId == selectedControlId) {
                        if (specifyFieldsArray[i].index != selectedIndex) {
                            hide($get(specifyFieldsArray[i].specifyDivId));
                        }
                    }
                }

                if (specifyDivId != "") {
                    var labelArray = selectedFormField.parentNode.getElementsByTagName("label");
                    var labelBounds = Sys.UI.DomElement.getBounds(labelArray[selectedIndex]);
                    Sys.UI.DomElement.setLocation(specifyDiv, labelBounds.x + labelBounds.width + 5, labelBounds.y + 1);
                    show(specifyDiv);
                }

                break;
            default:

        }
    } catch (exc) {

    }
}