﻿//This method will switch the plus to a minus and vice versa on the client side
function ChangePlusMinusText(PlusMinusCellID) {
    try {

        var PlusMinusCellObj = document.all(PlusMinusCellID);
        if (PlusMinusCellObj != null) {
            if (PlusMinusCellObj.innerHTML == "<A>[+]</A>") {
                PlusMinusCellObj.innerHTML = "<A>[-]</A>";

            }
            else {
                PlusMinusCellObj.innerHTML = "<A>[+]</A>";
            }
        }
    }
    catch (e) {
        alert("Error in ChangePlusMinusText Method: " + e);
    }
}

//After a postback occurs, this method will re-expand old data.
function ShowExpandedDivInfo(TextBoxIDOfExpandedDiv, DataGridID) {
    try {
        var DIVTB = document.all(TextBoxIDOfExpandedDiv);
        if (DIVTB != null) {
            var data = DIVTB.value;
            if (data != null && data.length > 0 && data != ",") {
                var SplitData = data.split(",");

                //Temp variables
                var SplitDataOfArrayItem = "";
                var DivIdentifier = "";
                var PlusMinusCellIdentifier = "";

                for (var i = 0; i < SplitData.length; i++) {
                    //Format of textbox data
                    //NameOfDIV + "@" + PlusMinusCellID
                    SplitDataOfArrayItem = SplitData[i].split("@");
                    //Get the ID of the DIV section
                    DivIdentifier = SplitDataOfArrayItem[0];
                    //Get the ID of the Cell with the Plus or Minus showing collapsed or expanded state.
                    PlusMinusCellIdentifier = SplitDataOfArrayItem[1];

                    if (DivIdentifier != null && DivIdentifier.length > 0) {
                        //Expand DIV Section
                        ShowPanel(DivIdentifier);
                    }

                    if (PlusMinusCellIdentifier != null && PlusMinusCellIdentifier.length > 0) {
                        var PlusMinusCellObj = document.all(PlusMinusCellIdentifier);
                        if (PlusMinusCellObj != null) {
                            //We found the cell to expand
                            PlusMinusCellObj.innerHTML = "<A>[-]</A>";
                        }
                        else {
                            //We DID NOT FIND the cell to expand
                            //May Have Been Lost After Postback.

                            //Invoke Method to get id of cell using partial data which will always
                            //be unique...
                            var NewPlusMinusCellObjID = getItem(PlusMinusCellIdentifier, DataGridID);

                            if (NewPlusMinusCellObjID != null) {
                                var NewCellObject = document.all(NewPlusMinusCellObjID);
                                if (NewCellObject != null) {
                                    var rows = NewCellObject.getElementsByTagName("td");
                                    if (rows != null && rows.length >= 0) {
                                        //the initial row has to be set to expanded....
                                        try {
                                            rows[0].innerHTML = "<A>[-]</A>";
                                        }
                                        catch (e) {
                                            //alert("Error in setting row to expanded in ShowExpandedDivInfo Method.  Error 101: " + e);
                                        }

                                    } //end of rows if statement
                                } //end of NewCellObject != null
                            } //end of NewPlusMinusCellObjID != null
                        } //end of PlusMinusCellObj != null if else
                    } //end of PlusMinusCellIdentifier != null
                } //end of for loop
            }
        }
    }
    catch (e) {
        alert("Error in ShowExpandedDivInfo Method: " + e);
    }
}

//This will keep the data in the hidden textbox field.
function SetExpandedDIVInfo(PlusMinusCellID, HiddenTextBoxToHoldDivInfo, DIVInfoID) {
    try {
        var IsExpanded = false;
        var HTBObj = document.all(HiddenTextBoxToHoldDivInfo);
        var HLObj = document.all(PlusMinusCellID);

        if (HLObj != null && HTBObj != null) {

            if (HLObj.innerHTML == "<A>[+]</A>") {
                IsExpanded = false;
            }
            else {
                IsExpanded = true;
            }

            //What is in the hidden DIV Textbox field?
            var ExpandedData = HTBObj.value;

            if (ExpandedData == null || ExpandedData == ",") {
                ExpandedData = "";
            }

            //===============================================================
            if (ExpandedData.length < 1 && IsExpanded == true) {
                //No Previous Expanded Data.  
                //Add new Expanded Field.
                ExpandedData = DIVInfoID + "@" + PlusMinusCellID;
                HTBObj.value = ExpandedData;
            }
            else if (ExpandedData.length < 1 && IsExpanded == false) {
                //No Previous Expanded Data.  
                //Clean up old Expanded Field. 
                //ExpandedData is empty, so no work is needed.
            }

            else if (ExpandedData.length > 0 && ExpandedData.indexOf(DIVInfoID) == -1 && IsExpanded == true) {
                //Expanded data has data from before.
                //No existing record exists for this field. 
                //We can add new Expanded field. 
                //We can use a comma as a delimeter.
                ExpandedData = ExpandedData + "," + DIVInfoID + "@" + PlusMinusCellID;
                ExpandedData = ExpandedData.replace(",,", ",");
                HTBObj.value = ExpandedData;
            }
            else if (ExpandedData.indexOf(DIVInfoID) > -1 && IsExpanded == true) {
                //Expanded data has data from before. 
                //Existing record exists for this field. 
                //We do not need to perform any updates.
            }
            else if (ExpandedData.indexOf(DIVInfoID) > -1 && IsExpanded == false) {
                //Expanded data has data from before.
                //Existing record exists for this field
                //We remove it as it is not expanded any longer.
                ExpandedData = ExpandedData.replace(DIVInfoID + "@" + PlusMinusCellID, "");

                //Make sure we don't have a double delimeter
                ExpandedData = ExpandedData.replace(",,", ",");
                HTBObj.value = ExpandedData;
            }
            else if (ExpandedData.indexOf(DIVInfoID) == -1 && IsExpanded == false) {
                //Expanded data has data from before.
                //Existing record does not exists for this field
                //No work is needed.
            }

            //Recheck we don't have any garbage data.
            //Added because deleting items, causes a comma
            //to stay sometimes.
            if (HTBObj != null && HTBObj.value == ",") {
                HTBObj.value = "";
            }
            //===============================================================
        }
    }
    catch (e) {
        alert("Error in SetExpandedDIVInfo Method: " + e);
    }
}

//This method will hide the panel.
function HidePanel(Panel) {
    try {
        var ChosenPanel = document.all(Panel);
        if (ChosenPanel != null) {
            document.all(Panel).style.display = "none";
        }
    }
    catch (e) {
        alert("Error in HidePanel Method: " + e);
    }
}

//This method will show the panel.	
function ShowPanel(Panel) {
    try {
        var ChosenPanel = document.all(Panel);
        if (ChosenPanel != null) {
            document.all(Panel).style.display = "block";
        }
    }
    catch (e) {
        alert("Error in ShowPanel Method: " + e);
    }
}

//This method will hide and show the panel.	
function HideShowPanel(Panel) {
    try {
        var ChosenPanel = document.all(Panel);
        if (ChosenPanel != null) {
            var currentdisplay = document.all(Panel).style.display;
            if (currentdisplay != "block") {
                document.all(Panel).style.display = "block";
            }
            else {
                document.all(Panel).style.display = "none";
            }
        }
    }
    catch (e) {
        alert("Error in HideShowPanel Method: " + e);
    }
}

//This method will get the object using partial id match.
function getItem(IDSearchCriteria, DataGridID) {
    try {
        if (IDSearchCriteria == null || IDSearchCriteria.length <= 0) {
            return null;
        }
        else if (DataGridID == null || DataGridID.length <= 0) {
            return null;
        }

        if (document.getElementsByTagName) {
            var table = document.getElementById(DataGridID);
            if (table != null) {
                var rows = table.getElementsByTagName("tr");
                for (var i = 0; i < rows.length; i++) {
                    var Identity = rows[i].id;

                    if (Identity != null && Identity.length > 0) {

                        var cellid = IDSearchCriteria.substring(IDSearchCriteria.lastIndexOf("_") + 1);
                        if (Identity.match(cellid)) {
                            //alert("Found: " + Identity);
                            return Identity;
                        }

                    }
                }
            }
            return null;
        }
        else {
            return null;
        }
    }
    catch (e) {
        alert("Error in getItem Method: " + e);
        return null;
    }
}

//For Back Button
function Back() {
    history.go(-1);
    return false;
}

function GridViewHelperClass() {
    function GridViewInit(idGrid, ExtraWidth, ExtraHeight) {
        var oTBODY = FindTBODY(idGrid);

        for (var i = 1; ; i++) {
            try {
                if (oTBODY.childNodes[i].getAttribute("isadd") == null)
                    continue;
                // content looks like:
                // <input type="button" value="Edit" onclick="javascript:__doPostBack('GridView1','Edit$0')" />&nbsp;<input type="button" value="Delete" onclick="javascript:__doPostBack('GridView1','Delete$0')" />
                // replace Edit with Add, remove Delete
                if (oTBODY.childNodes[i].childNodes[0].childNodes[0].type == "text")
                    oTBODY.childNodes[i].childNodes[0].childNodes[0].value = "Add";
                else
                    oTBODY.childNodes[i].childNodes[0].childNodes[0].src = "Images/Add.gif";

                var txt = oTBODY.childNodes[i].childNodes[0].innerHTML;
                var j = txt.indexOf("&nbsp;");
                oTBODY.childNodes[i].childNodes[0].innerHTML = txt.slice(0, j);
            }
            catch (e) {
                break;
            }
        }
        // put delete confirmations in, skip the header row
        for (var i = 1; ; i++) {
            try {
                var ctl = oTBODY.childNodes[i].childNodes[0].childNodes[2];
                if (oTBODY.childNodes[i].getAttribute("isadd") == "1")
                    continue;
                /*window.alert(i+": "+ctl.outerHTML);
                window.alert(i+": "+ctl.onclick);
                window.alert(i+": "+ctl.tagName);*/

                if (ctl.tagName == "INPUT") {
                    var onc = ctl.onclick.toString();
                    // window.alert(onc);	// uncomment this to see what the onclick actually contains
                    // if(onc.indexOf("Delete$") == -1)
                    //	continue;	// don't want to add confirm to "update cancel"
                    var j = onc.indexOf("__do");
                    var k = onc.indexOf(")", j) + 1;
                    onc = "if(confirm('Are you sure') == false) return(false); " + onc.slice(j, k);
                    ctl.onclick = onc;
                    ctl.outerHTML = ctl.outerHTML; 	// if you don't do this then the onclick will not work. it is probably related to how the onclick is actually defined (see window.alert above)
                    // window.alert(ctl.outerHTML);
                }

            }
            catch (e) {
                break;
            }
        }
        InitDrillDown(idGrid);
        ResizeMe(idGrid, ExtraWidth, ExtraHeight);
    }
    this.Init = GridViewInit;

    function ResizeMe(idGrid, ExtraWidth, ExtraHeight) {
        if (window.frameElement != null) {
            if (window.frameElement.tagName == "IFRAME") {
                // we are in an iframe

                // set the width to be really wide so there is no column wrapping
                window.frameElement.width = 10000;

                // now calculate the width required and set the frame width to it (plus a fudge factor)
                // window.alert("before width: "+GridViewHelper.CalcWidth(document.all.GridView1)+" "+ExtraWidth);
                window.frameElement.width = GridViewHelper.CalcWidth(document.all.GridView1) + ExtraWidth;

                // set the frame height to height of the generated document.
                // window.alert("height: "+document.body.scrollHeight);
                window.frameElement.height = document.body.scrollHeight + ExtraHeight;
                return;
            }
        }
        // get the container around the grid
        var Parent = GridView1.offsetParent;
        // make the parent really wide so that no columns will wrap
        Parent.style.width = "10000px";
        // calcuate the real width
        var RealWidth = GridViewHelper.CalcWidth(document.all.GridView1) + 100;
        // set the parent width back to nothing	
        Parent.style.width = "";
        //set the grid to the size it needs to be
        GridView1.width = "" + RealWidth;
    }

    // change the onclick function for the select buttons
    function InitDrillDown(idGrid) {
        var oTBODY = FindTBODY(idGrid);
        for (var i = 0; ; i++) {
            try {
                var ctl = oTBODY.childNodes[i].childNodes[0];
                var selectctl = ctl.childNodes[ctl.childNodes.length - 1]
                if (selectctl.tagName == "INPUT") {
                    var onc = selectctl.onclick.toString();
                    //window.alert(onc);	// uncomment this to see what the onclick actually contains
                    if (onc.indexOf("Select$") == -1)
                        continue; // probably an Add row line
                    onc = "return(GridViewHelper.DrillDownOrUp(this));"
                    selectctl.onclick = onc;
                    selectctl.outerHTML = selectctl.outerHTML; 	// if you don't do this then the onclick will not work. it is probably related to how the onclick is actually defined (see window.alert above)
                }
            }
            catch (e) {
                break;
            }
        }
    }

    function GetParentObject(o, tagName) {
        srcElem = o;
        //crawl up to find the table
        while (srcElem.tagName != tagName)
            srcElem = srcElem.parentElement;
        return (srcElem);
    }
    function RowObjectToIndex(oTR) {
        if (oTR == null)
            return (-1);

        var oTABLE = GetParentObject(oTR, "TABLE");
        // find the row index of our row
        var i;
        for (i = 0; i < oTABLE.rows.length; i++) {
            if (oTABLE.rows[i] == oTR) {
                return (i);
            }
        }
    }
    function DrillDownOrUpX(This) {
        var oRow = GetParentObject(This, "TR");
        // window.alert("oRow: "+oRow.outerHTML);
        var RowIndex = RowObjectToIndex(oRow)
        var oTable = GetParentObject(This, "TABLE");
        // window.alert("in drill: open='"+oRow.open+"' hascontent='"+oRow.hascontent+"'");
        var oPlusMinus = oRow.firstChild.childNodes[0];
        if (oRow.open == "1") {
            var DetailsRow = oTable.rows[RowIndex + 1];
            DetailsRow.style.display = "none";
            oRow.open = "0";
            var Gif = oPlusMinus.src;
            var iii = Gif.lastIndexOf("/");
            Gif = Gif.slice(0, iii) + "/Plus.gif";
            oPlusMinus.src = Gif;
            return (false);
        }
        if (oRow.hascontent == "1") {
            var DetailsRow = oTable.rows[RowIndex + 1];
            DetailsRow.style.display = "block";
            oRow.open = "1";
            var Gif = oPlusMinus.src;
            var iii = Gif.lastIndexOf("/");
            Gif = Gif.slice(0, iii) + "/Minus.gif";
            oPlusMinus.src = Gif;
            return (false);
        }

        var ColumnCount = oRow.cells.length;
        // need to add the row
        var NewRow = oTable.insertRow(RowIndex + 1);
        var NewCell = NewRow.insertCell(0);
        NewCell.setAttribute("colSpan", ColumnCount.toString());

        var CellContent =
			"<table cellpadding='0' cellspacing='0'>" +
			"<tr><td><iframe src='" + oRow.href + "' frameborder='0' width='100%' height='200'></iframe></td></tr>" +
			"</table>";
        // window.alert(CellContent);
        // window.prompt("", oRow.href);
        NewCell.innerHTML = CellContent;
        // window.alert("NewRow: "+NewRow.outerHTML);
        oRow.open = "1";
        oRow.hascontent = "1";
        var Gif = oPlusMinus.src;
        var iii = Gif.lastIndexOf("/");
        Gif = Gif.slice(0, iii) + "/Minus.gif";
        oPlusMinus.src = Gif;
        // window.alert("oRow: "+oRow.outerHTML);
        return (false);
    }
    this.DrillDownOrUp = DrillDownOrUpX;
    function FindTBODY(idGrid) {
        if (idGrid.firstChild.tagName == "TBODY")
            return (idGrid.firstChild);
        // there is a caption, so go down one more level
        return (idGrid.firstChild.nextSibling);
    }

    function CalcWidth(idGrid) {
        var oTBODY = FindTBODY(idGrid);
        var oTR = oTBODY.firstChild; // get the first row object
        var oLastCell = oTR.cells[oTR.cells.length - 1];

        var kb = 0;
        var r = oLastCell;
        while (r) {
            kb += r["offsetLeft"];
            r = r.offsetParent
        }
        kb += oLastCell.offsetWidth;
        return kb;
    }
    this.CalcWidth = CalcWidth;


    function AddToolTips(idGrid, ToolTips) {
        var oTBODY = FindTBODY(idGrid);
        var oTR = oTBODY.firstChild; // get the first row object which contains the column titles
        if (ToolTips.length > oTR.children.length)
            ToolTips.length = oTR.children.length;
        for (var i = 0; i < ToolTips.length; i++) {
            var oChild = oTR.children[i];
            // window.alert("OOO: "+oChild.outerHTML);
            oChild.title = ToolTips[i];
        }
    }
    this.AddToolTips = AddToolTips;
}


var GridViewHelper = new GridViewHelperClass();



