﻿function collapse(obj) {
    var expandDiv = document.getElementById("expand");
    var navMenu = document.getElementById("navigationMenu");
    //var topBar = document.getElementById("handleRight");
    //var bottomBar = document.getElementById("handleRightBtm");
    var slidingDiv = document.getElementById("slidingDiv");
    //var curveImg = document.getElementById("curveImg");
    obj.style.display = 'none';
    expandDiv.style.display = 'block';
    navMenu.style.display = 'none';
    //topBar.style.display = 'none';
    //bottomBar.style.display = 'none';
    //curveImg.style.display = 'none';
    slidingDiv.className = 'collapsed';
}

function expand(obj) {
    var collapseDiv = document.getElementById("collapse");
    var navMenu = document.getElementById("navigationMenu");
    //var topBar = document.getElementById("handleRight");
    //var bottomBar = document.getElementById("handleRightBtm");
    var slidingDiv = document.getElementById("slidingDiv");
    //var curveImg = document.getElementById("curveImg");
    obj.style.display = 'none';
    collapseDiv.style.display = 'block';
    navMenu.style.display = '';
    //topBar.style.display = '';
    //bottomBar.style.display = '';
    //curveImg.style.display = '';
    slidingDiv.className = '';
}

function collapseExpand(obj) {
    var contentDiv;
    var divs = obj.parentNode.parentNode.childNodes;
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].className && divs[i].className.indexOf("panelContents") != -1) {
            contentDiv = divs[i];
        }
    }
    if (obj.className == "collapseContent") {
        obj.className = 'expandContent';
        contentDiv.style.display = 'none';
        contentDiv.parentNode.style.borderBottom = '0px';
    } else {
        obj.className = 'collapseContent';
        contentDiv.style.display = '';
        contentDiv.parentNode.style.borderBottom = '1px solid #747677';
    }

}
function collapseExpandMenu(obj, menuId) {
    var menuItems = document.getElementById(menuId);
    if (obj.className.indexOf('expandMenu') != -1) {
        obj.className = 'menuItem collapseMenu';
        menuItems.style.display = '';
    } else {
        obj.className = 'menuItem expandMenu';
        menuItems.style.display = 'none';
    }
}
function divPosition(x, y) {
    this.X = x;
    this.Y = y;

    this.Add = function(val) {
        var newPos = new divPosition(this.X, this.Y);
        if (val != null) {
            if (!isNaN(val.X))
                newPos.X += val.X;
            if (!isNaN(val.Y))
                newPos.Y += val.Y
        }
        return newPos;
    }

    this.Subtract = function(val) {
        var newPos = new divPosition(this.X, this.Y);
        if (val != null) {
            if (!isNaN(val.X))
                newPos.X -= val.X;
            if (!isNaN(val.Y))
                newPos.Y -= val.Y
        }
        return newPos;
    }

    this.Min = function(val) {
        var newPos = new divPosition(this.X, this.Y)
        if (val == null)
            return newPos;

        if (!isNaN(val.X) && this.X > val.X)
            newPos.X = val.X;
        if (!isNaN(val.Y) && this.Y > val.Y)
            newPos.Y = val.Y;

        return newPos;
    }

    this.Max = function(val) {

        var newPos = new divPosition(this.X, this.Y)
        if (val == null)
            return newPos;

        if (!isNaN(val.X) && this.X < val.X)
            newPos.X = val.X;
        if (!isNaN(val.Y) && this.Y < val.Y)
            newPos.Y = val.Y;

        return newPos;
    }

    this.Bound = function(lower, upper) {
        var newPos = this.Max(lower);
        return newPos.Min(upper);
    }

    this.Check = function() {
        var newPos = new divPosition(this.X, this.Y);
        if (isNaN(newPos.X))
            newPos.X = 0;
        if (isNaN(newPos.Y))
            newPos.Y = 0;
        return newPos;
    }

    this.Apply = function(element) {
        if (typeof (element) == "string")
            element = document.getElementById(element);
        if (element == null)
            return;
        if (!isNaN(this.X))
            element.style.left = this.X + 'px';
        if (!isNaN(this.Y))
            element.style.top = this.Y + 'px';
    }
}

function hookEvent(element, eventName, callback) {
    if (typeof (element) == "string")
        element = document.getElementById(element);
    if (element == null)
        return;
    if (element.addEventListener) {
        element.addEventListener(eventName, callback, false);
    }
    else if (element.attachEvent)
        element.attachEvent("on" + eventName, callback);
}

function unhookEvent(element, eventName, callback) {
    if (typeof (element) == "string")
        element = document.getElementById(element);
    if (element == null)
        return;
    if (element.removeEventListener)
        element.removeEventListener(eventName, callback, false);
    else if (element.detachEvent)
        element.detachEvent("on" + eventName, callback);
}

function cancelDivEvent(e) {
    e = e ? e : window.event;
    if (e.stopPropagation)
        e.stopPropagation();
    if (e.preventDefault)
        e.preventDefault();
    e.cancelBubble = true;
    e.cancel = true;
    e.returnValue = false;
    return false;
}

function getMousePos(eventObj) {
    eventObj = eventObj ? eventObj : window.event;
    var pos;
    if (isNaN(eventObj.layerX))
        pos = new divPosition(eventObj.offsetX, eventObj.offsetY);
    else
        pos = new divPosition(eventObj.layerX, eventObj.layerY);
    return correctOffset(pos, pointerOffset, true);
}

function getDivEventTarget(e) {
    e = e ? e : window.event;
    return e.target ? e.target : e.srcElement;
}

function absoluteCursorPostion(eventObj) {
    eventObj = eventObj ? eventObj : window.event;

    if (isNaN(window.scrollX))
        return new divPosition(eventObj.clientX + document.documentElement.scrollLeft + document.body.scrollLeft,
      eventObj.clientY + document.documentElement.scrollTop + document.body.scrollTop);
    else
        return new divPosition(eventObj.clientX + window.scrollX, eventObj.clientY + window.scrollY);
}

function dragObject(element, attachElement, lowerBound, upperBound, startCallback, moveCallback, endCallback, attachLater) {
    if (typeof (element) == "string")
        element = document.getElementById(element);
    if (element == null)
        return;

    if (lowerBound != null && upperBound != null) {
        var temp = lowerBound.Min(upperBound);
        upperBound = lowerBound.Max(upperBound);
        lowerBound = temp;
    }

    var cursorStartPos = null;
    var elementStartPos = null;
    var dragging = false;
    var listening = false;
    var disposed = false;

    function divDragStart(eventObj) {
        if (dragging || !listening || disposed) return;
        dragging = true;

        if (startCallback != null)
            startCallback(eventObj, element);

        cursorStartPos = absoluteCursorPostion(eventObj);

        elementStartPos = new divPosition(parseInt(element.style.left), parseInt(element.style.top));

        elementStartPos = elementStartPos.Check();

        hookEvent(document, "mousemove", dragGo);
        hookEvent(document, "mouseup", dragStopHook);

        return cancelDivEvent(eventObj);
    }

    function dragGo(eventObj) {
        if (!dragging || disposed) return;

        var newPos = absoluteCursorPostion(eventObj);
        newPos = newPos.Add(elementStartPos).Subtract(cursorStartPos);
        newPos = newPos.Bound(lowerBound, upperBound)
        newPos.Apply(element);
        if (moveCallback != null)
            moveCallback(newPos, element);

        return cancelDivEvent(eventObj);
    }

    function dragStopHook(eventObj) {
        dragStop();
        return cancelDivEvent(eventObj);
    }

    function dragStop() {
        if (!dragging || disposed) return;
        unhookEvent(document, "mousemove", dragGo);
        unhookEvent(document, "mouseup", dragStopHook);
        cursorStartPos = null;
        elementStartPos = null;
        if (endCallback != null)
            endCallback(element);
        dragging = false;
    }

    this.Dispose = function() {
        if (disposed) return;
        this.StopListening(true);
        element = null;
        attachElement = null
        lowerBound = null;
        upperBound = null;
        startCallback = null;
        moveCallback = null
        endCallback = null;
        disposed = true;
    }

    this.StartListening = function() {
        if (listening || disposed) return;
        listening = true;
        hookEvent(attachElement, "mousedown", divDragStart);
    }

    this.StopListening = function(stopCurrentDragging) {
        if (!listening || disposed) return;
        unhookEvent(attachElement, "mousedown", divDragStart);
        listening = false;

        if (stopCurrentDragging && dragging)
            dragStop();
    }

    this.IsDragging = function() { return dragging; }
    this.IsListening = function() { return listening; }
    this.IsDisposed = function() { return disposed; }

    if (typeof (attachElement) == "string")
        attachElement = document.getElementById(attachElement);
    if (attachElement == null)
        attachElement = element;

    if (!attachLater)
        this.StartListening();
}


var otherDiv1, otherDiv2, otherDiv3;

var flagchkLoad = false;

function getElemsForResize() {

    flagchkLoad = true;
    otherDiv1 = document.getElementById('overflowDiv');
    otherDiv2 = document.getElementById('navigationMenu');
    otherDiv3 = document.getElementById('leftNav-scroll-up');
}

var flagResize = false;
function setSlidingBarPosition(initLeft) {
    var ileft = document.getElementById("handleRight");
    ileft.style.left = initLeft + "px";
    var ileftBtm = document.getElementById("handleRightBtm");
    ileftBtm.style.left = initLeft + "px";


}
var chkDivFlag = false;
function resizeStartTopDiv() {
    resizeStartPosition();
}
function resizeStartBotDiv() {
    chkDivFlag = true;
    resizeStartPosition();
}

function getWindowHeight() {
    var myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myHeight = window.innerHeight;
    }
    else {
        if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
            //IE 6+ in 'standards compliant mode'
            myHeight = document.documentElement.clientHeight;
        }
        else {
            if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
                //IE 4 compatible
                myHeight = document.body.clientHeight;
            }
        }
    }
    return myHeight;
}

function resizeStartPosition() {
    flagResize = true;
    var x = document.getElementById("handleRight");
    var x2 = document.getElementById("handleRightBtm");
    var maxHeight = getWindowHeight();
    x.style.height = (maxHeight + 220) + "px";
    x.style.top = "0px";

    x2.style.height = (maxHeight + 220) + "px";
    x2.style.top = "0px";

}
function resizeEndPosition() {
    if (flagResize == true) {
        var divx = document.getElementById("handleRight");
        var divx2 = document.getElementById("handleRightBtm");
        divx.style.height = "18px";
        divx.style.top = "232px";

        divx2.style.height = "18px";
        divx2.style.top = "275px";

        var newWidth;
        if (!chkDivFlag) {
            newWidth = divx.style.left;
        }
        else if (chkDivFlag) {
            newWidth = divx2.style.left;
        }
        chkDivFlag = false;

        newWidth = parseInt(newWidth.substring(0, (newWidth.length - 2)));
        ;
        divx.style.left = newWidth + "px";
        divx2.style.left = newWidth + "px";

        if (!flagchkLoad) {
            getElemsForResize();
        }

        if (otherDiv1) { otherDiv1.style.width = newWidth + "px"; }
        if (otherDiv2) { otherDiv2.style.width = newWidth + "px"; }
        if (otherDiv3) { otherDiv3.style.width = newWidth + "px"; }

        flagResize = false;

    }

}

document.onmouseup = resizeEndPosition;	
	
/**
* gets the key number
*/
function getKeyNum(e) {

    var keyNum;

    if (window.event) {// IE	
        keyNum = e.keyCode;

    } else if (e.which) { // Netscape/Firefox/Opera	
        keyNum = e.which;
    }
    return keyNum;
}

/**
* 	Phone number masking
*/
function keyPressOnPhone(e,pCtrlClientId) {
  
    var k;
    //var e = window.event.srcElement
    var h = document.getElementById(pCtrlClientId)
    
    if ("which" in e) {
        k = e.which
    }

    keynum = getKeyNum(e);
    h.maxLength = 12;

    if (keynum == 8) {

    } else if (h.value.length == 3) {
         h.value = h.value + '-';
    } else if (h.value.length == 7) {
         h.value = h.value + '-';
    } else if (h.value.length == 12) {
    }else if (k == 0) {
    } ;
}

function ValidateFields(pbtn,pchk,pchk2)
{
    var lbtn = document.getElementById(pbtn);
    var lchk = document.getElementById(pchk);
    var lchk2 = document.getElementById(pchk2);
    var lckhValid=false;
    var lck2Valid=false;
    
    if (lchk.checked==true) 
    {
        Page_ClientValidate('123');
        lckhValid = Page_IsValid
    }
    if (lchk2.checked==true) 
    {
        Page_ClientValidate('234');
        lckh2Valid = Page_IsValid
    }
    
    
     if (lckhValid==true && lckh2Valid==true )
     {
        document.forms[0].submit;
     }
     else
     {
        window.event.cancelBubble=true;
        return false;
     }

}
function TriggerFullPostBack()
{
    Page_ClientValidate();
    if (Page_IsValid==true)
    {
        __doPostBack('btnNext','');
    }
    
}


function ValidateName()
{
    var name=document.getElementById('txtname').value
    if(name !="")
        return true;
    else
        return false;
}
function ValidateEmail()
{
    var email=document.getElementById('txtemail').value
//    var reg=\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
//    if(reg.test(email))
//        return true;
//    else
//        return false;
}
function ValidatePhone()
{
    var phone=document.getElementById('txtphone').value
    if(phone.search("\d{3}-\d{3}-\d{4}"))
        return true;
    else
        return false;
}
function ValidateInput(hfname,hfphone,hfemail)
{
    alert('in')
    var lbl=document.getElementById("lblfiled")
    lbl.innerHTML="";
//    if(ValidateName())
//    {
        if(ValidatePhone())
        {
            alert('pok')
            if(ValidateEmail())
            {
                alert('eok')
                hfname=document.getElementById('txtname').value
                hfemail.value= document.getElementById('txtemail').value
                hfphone=document.getElementById('txtphone').value
                lbl.style.display="none"
                return true;
            }
            else
            {
                lbl.style.display=""
                lbl.innerHTML="Email Not Valid <br />"
            }
        }
        else 
        {
            lbl.style.display=""
            lbl.innerHTML="Phone No Not Valid  <br />"
        }
        
//    }
//    else 
//    {
//        lbl.innerHTML="Name Required <br />"
//    }
    return false;
}


// this function is responsible for expanding and collapsing subGrid (Endorsement)
function expandcollapse(obj,row)
{
    var div = document.getElementById(obj);
    
    if (div.style.display == "none")
    {
          div.style.display = "block";
    }
    else
    {
        div.style.display = "none";
    }
} 

// this function is responsible for expanding and collapsing subGrid (Endorsement)
function expandcollapseAdditionalFee(obj,row)
{    
    var div = document.getElementById(obj);
//    var img = document.getElementById('img' + obj);
    
    if (div.style.display == "none")
    {
        div.style.display = "block";
//        if (row == 'alt')
//        {
//            img.src = "minus.gif";
//        }
//        else
//        {
//            img.src = "minus.gif";
//        }
//        img.alt = "Close to view other Customers";
    }
    else
    {        
        div.style.display = "none";
//        if (row == 'alt')
//        {
//            img.src = "plus.gif";
//        }
//        else
//        {
//            img.src = "plus.gif";
//        }
//        img.alt = "Expand to show Orders";
    }
}


window.onscroll = showcenterdiv

function showcenterdiv() {

    var divProgressBackgroundFilter = document.getElementById('progressBackgroundFilter');
    var divProcessMessage = document.getElementById('processMessage');
    var scrolledX, scrolledY;

    if (divProgressBackgroundFilter) {
        if (self.pageYoffset) {
            scrolledX = self.pageXoffset;
            scrolledY = self.pageYoffset;
        } else if (document.documentElement && document.documentElement.scrollTop) {
            scrolledX = document.documentElement.scrollLeft;
            scrolledY = document.documentElement.scrollTop;
        } else if (document.body) {
            scrolledX = document.body.scrollLeft;
            scrolledY = document.body.scrollTop;
        }
        // Next, determine the coordinates of the center of browser's window
        var centerX, centerY;
        if (self.innerHeight) {
            centerX = self.innerWidth;
            centerY = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) {
            centerX = document.documentElement.clientWidth;
            centerY = document.documentElement.clientHeight;
        } else if (document.body) {
            centerX = document.body.clientWidth;
            centerY = document.body.clientHeight;
        }

        var leftoffset = scrolledX;
        var topoffset = scrolledY;

        var StyledivProgressBackgroundFilter = divProgressBackgroundFilter.style;
        StyledivProgressBackgroundFilter.top = topoffset + 'px';
        StyledivProgressBackgroundFilter.left = leftoffset + 'px';

        var styledivProcessMessage = divProcessMessage.style;
        styledivProcessMessage.top = (topoffset + (self.screen.availHeight * .35)) + 'px';
        styledivProcessMessage.left = (leftoffset + (self.screen.availWidth * .40)) + 'px';
    }
}

function OpenReportPdf(pFileName) {
    var strUrl = '';
    strUrl = pFileName;

    var windowWidth = screen.availWidth;
    var windowHeight = screen.availHeight - 20;

    var blnModalWindow = true;
    var x = 0;
    var y = 0;
    var strWindowName = 'wndModal';
    if (screen.availWidth) {
        x = 10;
        y = 10;
    }

    var windowFeatures = 'left=' + x + ', screenX=' + x + ', screenY=' + y + ', top=' + y + ', maximized=no, menubar=no, toolbar=no, directories=no, status=no, resizable=yes, scrollbars=yes, Height=' + windowHeight + ', Width=' + windowWidth;
    var childWindow = window.open(strUrl, strWindowName, windowFeatures);

    if (blnModalWindow == true) {
        window.onfocus = function() { if (childWindow.closed == false) { childWindow.focus(); }; };
    }
}

var TREEVIEW_ID = "trvUserRights"; //the ID of the TreeView control
var btnSave_ID = "btnSave"; //the ID of the TreeView control

//the constants used by GetNodeIndex()
var LINK = 0;
var CHECKBOX = 1;

function setControlID(pTreeControlID, pbtnControlID) {
    //   debugger
    TREEVIEW_ID = pTreeControlID;
    btnSave_ID = pbtnControlID;
}
//checkbox click event handler
function checkBox_Click(eventElement) {
    // debugger; 
    ToggleChildCheckBoxes(eventElement.target);
    ToggleParentCheckBox(eventElement.target);
}

function Set_Focus(eventElement) {
    //debugger; 
    document.getElementById(btnSave_ID).focus();
}

//returns the index of the clicked link or the checkbox
function GetNodeIndex(elementId, elementType) {
    //debugger; 
    var nodeIndex;
    if (elementType == LINK) {
        nodeIndex = elementId.substring((TREEVIEW_ID + "t").length);
    }
    else if (elementType == CHECKBOX) {
        nodeIndex = elementId.substring((TREEVIEW_ID + "n").length, elementId.indexOf("CheckBox"));
    }
    return nodeIndex;
}

//checks or unchecks the nested checkboxes
function ToggleChildCheckBoxes(checkBox) {
    //debugger; 
    var postfix = "n";
    var childContainerId = TREEVIEW_ID + postfix + GetNodeIndex(checkBox.id, CHECKBOX) + "Nodes";
    var childContainer = document.getElementById(childContainerId);
    if (childContainer) {
        var childCheckBoxes = childContainer.getElementsByTagName("input");
        for (var i = 0; i < childCheckBoxes.length; i++) {
            childCheckBoxes[i].checked = checkBox.checked;
        }
    }
}

//unchecks the parent checkboxes if the current one is unchecked
function ToggleParentCheckBox(checkBox) {
    //debugger; 
    if (checkBox.checked == false) {
        var parentContainer = GetParentNodeById(checkBox, TREEVIEW_ID);
        if (parentContainer) {
            var parentCheckBoxId = parentContainer.id.substring(0, parentContainer.id.search("Nodes")) + "CheckBox";
            if ($get(parentCheckBoxId) && $get(parentCheckBoxId).type == "checkbox") {
                $get(parentCheckBoxId).checked = false;
                ToggleParentCheckBox($get(parentCheckBoxId));
            }
        }
    }
    else {
        var parentContainer = GetParentNodeById(checkBox, TREEVIEW_ID);
        var unCheckMain = 0;
        if (parentContainer) {
            var childCheckBoxes = parentContainer.getElementsByTagName("input");
            for (var i = 0; i < childCheckBoxes.length; i++) {
                if (!childCheckBoxes[i].checked) {
                    unCheckMain = 1;
                    break;
                }
            }
            if (parentContainer.id.search("Nodes") > 0) {
                var parentCheckBoxId = parentContainer.id.substring(0, parentContainer.id.search("Nodes")) + "CheckBox";
                if (unCheckMain == 1) {
                    document.getElementById(parentCheckBoxId).checked = false;
                    //document.getElementById(parentCheckBoxId).style.backgroundColor = "green";
                }
                else {
                    document.getElementById(parentCheckBoxId).checked = true;
                    //document.getElementById(parentCheckBoxId).style.backgroundColor = "white";
                }

            }
        }
    }
}

//returns the ID of the parent container if the current checkbox is unchecked
function GetParentNodeById(element, id) {
    //debugger; 
    var parent = element.parentNode;
    if (parent == null) {
        return false;
    }
    if (parent.id.search(id) == -1) {
        return GetParentNodeById(parent, id);
    }
    else {
        return parent;
    }
}
