// 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) {
	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 {
		var Row = igtbl_getRowById(Id);
		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]);
	}
}
// End of code from Danko

function uwgXmlInitializeLayout(gn) {
	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;
	}
}

/* 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(IsHorizontal) {
    try {
        var YBounds = 1;
        
        if (typeof IsHorizontal != 'undefined' && IsHorizontal) {
            YBounds = 15;
        }
    
        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 + YBounds);
                        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 + 5 + labelBounds.width + 5, labelBounds.y + YBounds);
                        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 + YBounds);
                        show(specifyDiv);
                    }
                    break;
                default:    
            }
        }
    } catch (exc) {}
}

function specify(selectedFormField, specifyDivId, selectedIndex, IsHorizontal) {
    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]);
                    if (typeof IsHorizontal != 'undefined' && IsHorizontal) {
                        YBounds = 15;
                        Sys.UI.DomElement.setLocation(specifyDiv, labelBounds.x - 16, labelBounds.y + 15);
                    } else {
                    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]);
                    if (typeof IsHorizontal != 'undefined' && IsHorizontal) {
                        YBounds = 15;
                        Sys.UI.DomElement.setLocation(specifyDiv, labelBounds.x - 16, labelBounds.y + 15);
                    } else {
                        Sys.UI.DomElement.setLocation(specifyDiv, labelBounds.x + labelBounds.width + 5, labelBounds.y + 1);
                    }
                    show(specifyDiv);
                }

                break;
            default:
        }
    } catch (exc) {}
}

/*Functions for UserCtrl.ascx*/
function UserCtrl_ShowPasswordFields(idDivPasswordLink, idDivChangePassword, idTxtPassword, idTxtRetypePassword) {
   	var divPasswordLink = $get(idDivPasswordLink);
   	var divChangePassword = $get(idDivChangePassword);
   	var txtPassword = $get(idTxtPassword); ;
   	var txtRetypePassword = $get(idTxtRetypePassword); ;

   	show(divChangePassword);
   	hide(divPasswordLink);
   	if (txtPassword != null)
   		txtPassword.text = "";
	if (txtRetypePassword != null)
   		txtRetypePassword.text = "";
}

function CustomRequiredFieldValidator(sender, args) {
	var divChangePassword = $get("divChangePassword");
	if (divChangePassword.style.display == "") {
		if (args.Value == "")
			args.IsValid = false;
		else
			args.IsValid = true;
   	}
}

function detectPopupBlocker() {
    var myTest = window.open("about:blank", "", "directories=no,height=10,width=10,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,top=0,location=no");
    if (!myTest) {
        return true;
    } else {
        myTest.close();
    }
    return false;
}

// JQuery AJAX call settings
$.ajaxSetup({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: "{}",
    dataFilter: function(data) {
        var msg;
        if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') {
            msg = JSON.parse(data);
        } else {
            msg = eval('(' + data + ')');
        }
        if (msg.hasOwnProperty('d')) {
            return msg.d;
        } else {
            return msg;
        }
    }
});
function GetDateTime() {
    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    var curr_year = d.getFullYear();
    var curr_time = d.t;
    var right_hours = d.getHours();
    var right_min = d.getMinutes();
    var ampm = "am";
    if (right_hours > 12) {right_hours = right_hours - 12;}
    if (right_min < 10) {right_min = "0" + right_min;}
    if (d.getHours() > 12) {ampm = "pm";}
    var dateStr = curr_month + "/" + curr_date + "/" + curr_year + " " + right_hours + ":" + right_min + " " + ampm;
    return dateStr;
}
function FormatTable(className) {
    $("." + className + " tr:even").addClass("alt");
    $("." + className + " tr").mouseover(function() { $(this).addClass("over"); }).mouseout(function() { $(this).removeClass("over"); });
}

//
// Use this function to toggle the enabled attribute for an Infragistics WebDateChooser field
function toggleWebDateChooserEnabled(id, enable) {
	// enable/disable the textbox and dropdown arrow button
	var superId = $("table.[id$=" + id + "]").attr("id");
	var wdcDate_ig = igdrp_getComboById(superId);
	wdcDate_ig.setEnabled(enable);

	// set the background colors
   	if (enable) {
   		$("#" + superId + " input, #" + superId + " td").css({ "background-color": "#FFF" });
   	} else {
   		$("#" + superId + " input, #" + superId + " td").css({ "background-color": "#ECECEC" });
   	}
}
//
// Use this function to toggle the enabled attribute for an Infragistics WebDatePicker field
function toggleWebDatePickerEnabled(id, enable) {
	//debugger;
	var superId = $("table.[id$=" + id + "]").attr("id");
	var datapicker = $find(superId);
	datapicker.set_enabled(enable);
}
