function fGetInput(sName) {
	try {
		for (var i = 0; i < document.forms.length; i++) {
			var fr = document.forms[i];
			for (var j = 0; j < fr.length; j++) {
				var inp = fr[j];
				if (inp.name.indexOf(sName) > -1) return inp;
			}
		}
	} catch (er) {
		alert(er.message);
	}
}


function fFindInput() {
	try {
		for (var i = 0; i < document.forms.length; i++) {
			var fr = document.forms[i];
			for (var j = 0; j < fr.length; j++) {
				var inp = fr[j];
				var isGood = true;
				for (var argI = 0; argI < arguments.length; argI++) {
					isGood = (inp.name.indexOf(arguments[argI]) > -1) && isGood;
				}
				if (isGood) return inp;
			}
		}
	} catch (er) {
		alert(er.message);
	}
}

function GetURL(URL,OnSuccess,OnError) {

	if (!window.XMLHttpRequest) {
		window.XMLHttpRequest = function() {
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	var xr = new XMLHttpRequest();
	xr.open("GET",URL, true);
	xr.onreadystatechange = function() {
		if (xr.readyState == 4) {
			if (xr.status == 200) { //Successful Request

				if (OnSuccess) {
					OnSuccess(xr.responseText);
					return;
				}
			} else {
				OnError(xr);
			}
		}
	}
	xr.send(null);
}

function DelayExec(code,delay,sid) {
	try {
		if (!sid) sid = "Intv";
		if (window[sid]) {
			clearTimeout(window[sid]);
		}
		window[sid] = setTimeout(code, delay);
	} catch (ex) {
		alert(ex.message);
	}
}

function CancelDelayExec(sid) {
	if (!sid) sid = "Intv";
	if (window[sid]) {
		clearTimeout(window[sid]);
	}
}

function AddPageLoad(fn) {
	if (!fn) return;
	if (!window.PageLoads) {
		window.PageLoads = new Array();
	}
	window.PageLoads.push(fn);
	if (!window.pageLoad) {
		window.pageLoad = function(sender, args) {
			for (var i = 0; i < window.PageLoads.length; i++) {
				var fn = window.PageLoads[i];
				fn(sender, args);
			}
		}
	}
}

function Describe(obj, txt, tabs, depth) {
	if (!txt) {
		txt = "";
	}
	if (depth == undefined) depth = 4;
	if (depth < 1) return "";
	if (!tabs) tabs = "";
	txt += tabs + obj + "\n";
	for (var s in obj) {
		txt += Describe(obj[s], s + ": ", tabs + "\t", depth-1);
	}
	return txt;
}

function GetKey(evt) {  
   alert(window.event)?event.keyCode:evt.which;

}


function PrintDiv(divId) {
    try {

        var divToPrint = document.getElementById(divId);
        if (!divToPrint) throw new Error("Div to print not found: " + divId);
        var newPrintDiv = divToPrint.cloneNode(true);

        var docWidth = $(document.body).width();
        var docHeight = $(document.body).height();

        var div = document.createElement("whitePritDiv");
        div.style.backgroundColor = "#FFFFFF";
        div.style.position = "absolute";
        div.style.top = "0px";
        div.style.left = "0px";
        div.style.width = docWidth + "px";
        div.style.height = docHeight + "px";
        div.style.zIndex = getNextHighestZindex();
        div.onclick = function() {
            this.innerHTML = "";
            this.style.visibility = 'hidden';
        }

        div.appendChild(newPrintDiv);

        document.body.appendChild(div);

        if (window.print) {
            window.print();
        } else {
            alert("Press cmd+p or use print from the file menu");
        }

    } catch (ex) {
        alert(ex.message);
    }
}

function getNextHighestZindex(obj) {

    var highestIndex = 0;
    var currentIndex = 0;
    var elArray = Array();

    if (obj) { elArray = obj.getElementsByTagName('*'); } else { elArray = document.getElementsByTagName('*'); }

    for (var i = 0; i < elArray.length; i++) {

        if (elArray[i].currentStyle) {
            currentIndex = parseFloat(elArray[i].currentStyle['zIndex']);
        } else if (window.getComputedStyle) {
            currentIndex = parseFloat(document.defaultView.getComputedStyle(elArray[i], null).getPropertyValue('z-index'));
        }
        if (!isNaN(currentIndex) && currentIndex > highestIndex) { highestIndex = currentIndex; }
    }

    return (highestIndex + 1);

}

function OpenMaxBox(url, options) {
    if (!options) options = new Object();
    options.width = $(window).width() + "px";
    options.height = $(window).height() + "px";
    options.closeOnClick = options.closeOnClick == null ? false : options.closeOnClick;
    OpenLightBox(url, options);
}

function OpenLightBox(url, options) {
    try {

        if (!options) {
            options = new Object();
        }
        var defaultOptions = { closeOnClick: false, width: "80%", height: "80%" };
        for (var s in defaultOptions) {
            if (!options[s]) {
                options[s] = defaultOptions[s];
            }
        }
    
        // add background color
        if (!window.lightBox) {
            window.lightBox = new Object();

            // add back
            var maxWidth = $(document.body).width();
            var maxHeight = $(document.body).height();
            //alert(maxWidth + "x" + maxHeight);
            window.lightBox.back = document.createElement("div");
            //window.lightBox.back.style.width = $(document.body).width() + "px";
            window.lightBox.back.style.width = "100%";
            window.lightBox.back.style.height = ($(document.body).height() > $(window).height() ? $(document.body).height() : $(window).height()) + "px";
            window.lightBox.back.style.backgroundColor = "black";
            window.lightBox.back.style.position = "absolute";
            window.lightBox.back.style.top = "0px";
            window.lightBox.back.style.left = "0px";
            window.lightBox.back.style.opacity = "0.7";
            window.lightBox.back.style.filter = "alpha(opacity=70)";
            document.body.appendChild(window.lightBox.back);

            window.lightBox.box = document.createElement("div");
            window.lightBox.box.id = "lightBoxBox";
            window.lightBox.box.style.position = "absolute";
            document.body.appendChild(window.lightBox.box);
            window.lightBox.box.innerHTML = "<iframe id='lightBoxFrame' src='/LightBox/Loading.html' width='100%' height='100%' frameborder='0' allowtransparency='true' />";

            window.scrollTo(0, 0);

        }

        window.lightBox.box.style.width = options.width;
        window.lightBox.box.style.height = options.height;
        window.lightBox.box.style.backgroundColor = "white";
        window.lightBox.box.style.top = ((($(window).height() - $(window.lightBox.box).height()) * 0.5) + $(window).scrollTop()) + "px";
        window.lightBox.box.style.left = (($(window).width() - $(window.lightBox.box).width()) * 0.5) + "px";
                
        window.lightBox.back.style.visibility = "visible";
        window.lightBox.box.style.visibility = "visible";

        var fr = document.getElementById("lightBoxFrame");
        fr.src = url;

        if (options.closeOnClick) {
            window.lightBox.back.onclick = function() {
                CloseLightBox();
            };
        } else {
            window.lightBox.back.onclick = null;
        }

    } catch (ex) {
        alert(ex.message);
    }
}

function CloseLightBox() {
    if (window.lightBox) {
        window.lightBox.back.style.visibility = "hidden";
        window.lightBox.box.style.visibility = "hidden";
        var frame = document.getElementById("lightBoxFrame");
        if (frame) frame.src = "/LightBox/Loading.html";
    }
}

function TreeNodeCheckAll(obj) {
    
    var treeNodeFound = false;
    var checkedState;
    if (obj.tagName == "INPUT" && obj.type == "checkbox") {
        var treeNode = obj;
        checkedState = treeNode.checked;
        do {
            obj = obj.parentElement;
        } while (obj.tagName != "TABLE")
        var parentTreeLevel = obj.rows[0].cells.length;
        var parentTreeNode = obj.rows[0].cells[0];
        var tables = obj.parentElement.getElementsByTagName("TABLE");
        var numTables = tables.length
        if (numTables >= 1) {
            for (i = 0; i < numTables; i++) {
                if (tables[i] == obj) {
                    treeNodeFound = true;
                    i++;
                    if (i == numTables) {
                        return;
                    }
                }
                if (treeNodeFound == true) {
                    var childTreeLevel = tables[i].rows[0].cells.length;
                    if (childTreeLevel > parentTreeLevel) {
                        var cell = tables[i].rows[0].cells[childTreeLevel - 1];
                        var inputs = cell.getElementsByTagName("INPUT");
                        inputs[0].checked = checkedState;
                    }
                    else {
                        return;
                    }
                }
            }
        }
    }
}
