﻿
/* 241008:T004162 - PNS: Gets the position of the object based on ID passed and returns the object*/
function getElementPosition(elemID) {
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 &&
			typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return { left: offsetLeft, top: offsetTop };
}

/* 241008:T004162 - PNS: Shows the passed popup message for the passed object */
function ShowPopUpMessage(obj, popUpMsg) {

    // Gets the coordinates for the passed object and returns the object
    var ctrl = getElementPosition(obj.id);
    var posx = 0;
    var posy = 0;

    // Gets the mouse coordinates
    posx = ctrl.left;
    posy = ctrl.top;

    // Checks if the popup div exists. If not then creates a new div
    var div = document.getElementById('popup');

    if (!div) {
        div = document.createElement('div');
        div.setAttribute('id', 'popup');
        div.className = 'popupdiv';
    }

    // Checks if the inner popup text div exists. If not then creates a new inner div
    var innerDiv = document.getElementById('popupText');
    if (!innerDiv) {
        innerDiv = document.createElement('div');
        innerDiv.setAttribute('id', 'popupText');
        //			innerDiv.className='innerpopupdiv';
    }
    innerDiv.innerHTML = popUpMsg;

    // Adds the inner div with the popup message to the main div
    div.appendChild(innerDiv);
    document.getElementsByTagName('body')[0].appendChild(div);

    // NOTE: This is the width of the div as set in the style sheet. 
    var divWidth = document.getElementById('popup').offsetWidth;
    var divHeight = document.getElementById('popup').offsetHeight;

    // move pop-up DIV element to the desired position i.e if the position of the 
    // popup and size of the popup is greater than screen width then opens popup on left side
    // otherwise opens popup on the right side
    if (posx != null && posy != null && (posx + 15 + divWidth) < screen.width) {
        div.style.top = posy + 15 + 'px';
        div.style.left = posx + 15 + 'px';
    }
    else {
        div.style.top = posy + 15 + 'px';
        div.style.left = (posx - 15 - divWidth) + 'px';
    }

    // move pop-up DIV element to the desired position i.e if the position of the 
    // popup and height of the popup is greater than screen height then opens popup on top side
    // otherwise opens popup on the right side

    if (posx != null && posy != null && (posy + 175 + divHeight - document.documentElement.scrollTop) < screen.height) {
        div.style.top = posy + 15 + 'px';
        div.style.left = posx + 15 + 'px';
    }
    else {
        div.style.top = (posy + 15 - divHeight) + 'px';
        div.style.left = posx + 15 + 'px';
    }
}

/* 241008:T004162 - PNS: Hides the popup message */
function HidePopUpMessage() {
    var div = document.getElementById('popup');
    var innerDiv = document.getElementById('popupText');
    if (div == null && innerDiv == null) {
        return;
    }

    // remove innerdiv first and then parent div
    if (innerDiv != null) {
        innerDiv.parentNode.removeChild(innerDiv);
    }

    if (div != null) {
        div.parentNode.removeChild(div);
    }
    return;
}

/*
* 121208:T004216-TRA
* Toggle Display of an object through its display property
*/
function ShowHideObject(id) {
    var object = window.document.getElementById(id);

    if (object.style.display == "none")
        object.style.display = "inline";
    else
        object.style.display = "none";

    return;
}

/*
* 240909:T004431-TRA
* New function to diplay a modal message box (hiding the background with divHideScreen)
*/
function ShowHideModalObject(divHideScreen, divMessageBox) {
    ShowHideObject(divHideScreen); // show the screen that will hide all other elements
    ShowHideObject(divMessageBox); // show the message box
    return;
}

function SetObjectValue(elem1, orderId) {
    var e1 = document.getElementById(elem1);

    if (e1 != null && orderId != null)
        e1.value = orderId;

}

/*
* 01102009:T4940-CFL
* Cancel Beneficiary payment transfer - don't run processing animation unless reason is filled in
*/
function validateDesc(dvRunningID, txtReason, dvAllID) {
    if (document.getElementById(txtReason) != null && document.getElementById(txtReason).value.length > 0) {
        Running(dvRunningID, dvAllID);
    }
    else {
        return;
    }
}

function setDivPleaseWaitPosition(divName1) {
    var dvLoading = $("#" + divName1);

    if (dvLoading != null)
        dvLoading.css("top", ($(document.documentElement).scrollTop() + 200) + "px");
}

function setDivPositionValue(dvName1, dvName2, ctrlID, ctrlValue)
{
    var containerDiv = $("#" + dvName1);
    var scrollingDiv = $("#" + dvName2);
    var ctrlHidden = $("#" + ctrlID);

//    alert(ctrlHidden);
//    alert(ctrlID);
//    alert(ctrlValue);
//    alert(containerDiv);

    if (ctrlHidden != null)
    {
        ctrlHidden.val(ctrlValue);
    }

    if (containerDiv != null)
    {
        containerDiv.css("display", "block");
        containerDiv.height($("body").outerHeight());
    }

    if (scrollingDiv != null)
    {
        scrollingDiv.css("display", "block");
//        scrollingDiv.offset({ top: $(document.documentElement).scrollTop() + 200 });
        if (window.event != null)
        {
            scrollingDiv.offset({ top: 200, left: 200 });
        }
        else
        {
            scrollingDiv.offset({ top: $(document.documentElement).scrollTop() + 200, left: 200 });
        }
 //       scrollingDiv.animate({ "top": ($(document.documentElement).scrollTop() + 200) + "px" }, "fast");
    }
}

function setDivPosition(dvName1, dvName2){

    var containerDiv = $("#" + dvName1);
    var scrollingDiv = $("#" + dvName2);

    if (containerDiv != null) {

        containerDiv.css("display", "block");        
        containerDiv.height($("body").outerHeight());
    }

    if (scrollingDiv != null) {
        scrollingDiv.css("display", "block");
        scrollingDiv.animate({ "top": ($(document.documentElement).scrollTop() + 200) + "px" }, "fast");
    }
}

function validateselection(selectedNode, selectedNodeValue) {

    if (selectedNodeValue != "")
        return true;
    else {
        var label = document.getElementById(selectedNode);
        if (label != null)
            label.innerHTML = "<span class='validator_style'>Please select a menu to rename.</span>";
        return false;
    }
}

/* Tree View updating children or parent on Node check*/
function UpdateAllChildren(nodes, checked) {
    var i;
    for (i = 0; i < nodes.length; i++) {
        if (checked) {
            nodes[i].Check();
        }
        else {
            nodes[i].UnCheck();
        }
        if (nodes[i].Nodes.length > 0) {
            UpdateAllChildren(nodes[i].Nodes, checked);
        }
    }
}

function UpdateParent(node, checked) {
    var i;
    var bAnyChildChecked = false;
    if (node.Nodes != null) {
        for (i = 0; i < node.Nodes.length; i++) {
            if (node.Nodes[i].Checked) {
                node.Check();
                bAnyChildChecked = true;
                break;
            }
        }
    }

    if (node.Level == 0 && !bAnyChildChecked)
        node.UnCheck();

    if (node.Parent != null) {
        UpdateParent(node.Parent, node.Checked);
    }
}

function AfterCheck(node) {
    if (node.Nodes != null)
        UpdateAllChildren(node.Nodes, node.Checked);
    if (node.Parent != null) {
        UpdateParent(node.Parent, node.Checked);
    }
}
/* OVER - Tree View updating children or parent on Node check*/

/* Triggering page post back using javascript */
function postBackByObject(e) {
    var ObjEvent = e;
    var o;

    if (!e) // IE does not pass event to method
        ObjEvent = window.event; // IE way of detecting that event occured

    if (ObjEvent) {
        if (ObjEvent.srcElement) {
            o = ObjEvent.srcElement; // IE
        }
        else {
            o = ObjEvent.target;  // Firefox
        }

        if (o.tagName == "INPUT" && o.type == "checkbox") {
            __doPostBack("", "");
        }
    }
}

/*Search functionalities */

function getSearchResult() {
    var searchStr = document.getElementById("txtSearch").value;
    var sReturnURL = location.pathname;

    var proxy = new PoplarGrove.WebServices.ISearchService();

    proxy.GetSearchResult(
            searchStr,
            sReturnURL,
            onMethodCompleted,
            null,
            null);
}

//function getSearchResult()
//{
//
//    var searchStr = document.getElementById("txtSearch").value;
//    var sReturnURL = location.pathname;
//    PoplarGrove.WebServices.SearchService.GetSearchResult(searchStr, sReturnURL, onMethodCompleted);
//}

function onFailed(result) {
    alert(result);
}

function onMethodCompleted(results)
{
    var dvResult = document.getElementById("dvResult");
    var dvClose = document.createElement("div");
    alert(results);

    dvClose.setAttribute("text-align", "right");
    dvClose.setAttribute("border-width", "solid 1px black");
    dvClose.innerHTML = "<a class='divCloseStyle' onclick='CloseDiv();'>Close</a>"

    dvResult.innerHTML = results;
    dvResult.appendChild(dvClose);
    dvResult.style.display = 'block';
}

function showUser(userId, sURL)
{
    location.href = sURL + '?user=' + userId;
}

function setSearchString(hdnControl)
{
    var searchStr = document.getElementById("txtSearch").value;
    document.getElementById(hdnControl).value = searchStr;
}

function CloseDiv()
{
    var dvResult = document.getElementById("dvResult");
    dvResult.style.display = 'none';
}

function clearText(ctrlID, ctrlValue)
{
    var txtBox = document.getElementById(ctrlID);
    if (txtBox != null && txtBox.value.toLowerCase().trim() == ctrlValue)
        txtBox.value = '';
}

function setText(ctrlID, ctrlValue)
{
    var txtBox = document.getElementById(ctrlID);
    if (txtBox != null && txtBox.value.length==0)
    {
        switch (ctrlValue)
        {
            case "email":
                txtBox.value = "Email";
                break;
            case "password":
                txtBox.value = "Password";
                break;
            default:
                break;
        }
    }
}

function DisplayCalendar(dvFromCal, dvToCal, displayCalendar)
{
    var cal1 = document.getElementById(dvFromCal);
    var cal2 = document.getElementById(dvToCal);

    if (cal1 != null && cal2 != null && displayCalendar == 'FromCalendar')
    {
        cal1.style.display = 'block';
        cal2.style.display = 'none';
    }
    if (cal2 != null && cal2 != null && displayCalendar == 'ToCalendar')
    {
        cal1.style.display = 'none';
        cal2.style.display = 'block'
    }
}

function hidediv(dvCal)
{
    var dvCalendar = document.getElementById(dvCal);

    if (dvCalendar != null)
        dvCalendar.style.display = 'none';
}


