﻿function hitBreakpoint()
{
    //debugger;
}

function IsFirefoxOrChrome()
{
    return (navigator.appName == 'Netscape');
}
function IsChrome()
{
    if ( !IsFirefoxOrChrome() )
        return false;
    return (navigator.userAgent.toLowerCase().indexOf('chrome') > -1);
}
function IsFirefox()
{
    if ( !IsFirefoxOrChrome() )
        return false;
    return !IsChrome();
}
function IsIE6()
{
    var bIE6 = /MSIE (5\.5|6|7)/.test(navigator.userAgent);
    return bIE6;
}
function MoveElement(elem, parElem)
{
    var newElem = (elem != null) ? elem.cloneNode(true, true) : null;
    if (newElem != null && parElem != null)
    {
        elem.removeNode(true);
        parElem.appendChild(newElem);
    }
}
function ElementByTagName(parent, tagName, recursive)
{
    var child = null;
    for (var i=0; i<parent.children.length; i++)
    {
        child = parent.children[i];
        if ( child == null )
            break;
        if ( child.tagName == tagName )
            return child;
        child = recursive? ElementByTagName(child, tagName, recursive) : null;
        if ( child != null )
            return child;
    }
    return null;
}
function MoveHdrTable(hdrTblID, boxID, xOffset, yOffset, width, height)
{
    var div = GetFormObject(boxID, false);
    var tbl = ElementByTagName(div, 'TABLE', false);
    var row = (tbl != null && tbl.rows.length > 0) ? tbl.rows[0] : null;
    var cell = (row != null && row.cells.length >= 3) ? row.cells[1] : null;
    var hdrTbl = GetFormObject(hdrTblID, true);
    var maincell = document.getElementById("RBOXY_1-MainCell");
    if (hdrTbl != null && cell != null && maincell != null)
    {
        var coordCont = IsFirefox() ? GetElemCoord(div) : GetElemCoord(maincell);
        var coords = GetElemCoord(cell);
        coords.x -= coordCont.x;
        coords.y -= coordCont.y;
        coords.x += xOffset;
        coords.y += yOffset;
        hdrTbl.style.position = "absolute";
        hdrTbl.style.left = coords.x + 'px';
        hdrTbl.style.top = coords.y + 'px';
        if ( width > 0 )
            hdrTbl.style.width = width + 'px';
        if ( height > 0 )
            hdrTbl.style.height = height + 'px';
        hdrTbl.style.zIndex = 300;
    }
}
function GetElemCoord(cell)
{
    var coords = { x: 0, y: 0 };
    var elem = cell;
    while (elem)
    {
        coords.x += elem.offsetLeft;
        coords.y += elem.offsetTop;
        elem = elem.offsetParent;
    }
    return coords;
}
function HideImage(hdrImgID)
{
    var imgDiv = GetFormObject(hdrImgID, true);
    if ( imgDiv )
    {
        imgDiv.style.display = "none";
    }
}
function MoveImage(hdrTblID, hdrImgID, cellIdx)
{
    var imgDiv = GetFormObject(hdrImgID, true);
    var img = (imgDiv != null && imgDiv.children.length > 0) ? imgDiv.children[0] : null;
    if (imgDiv == null)
        return;
    var maincell = document.getElementById("RBOXY_1-MainCell");
    var tbl = GetFormObject(hdrTblID, true);
    var row = (tbl != null && tbl.rows.length > 0) ? tbl.rows[0] : null;
    var cell = (row == null || row.cells.length <= cellIdx) ? null : row.cells[cellIdx];
    if (maincell == null || cell == null)
        return;
    if ( IsIE6() )
    {
        var coordCont = GetElemCoord(maincell);
        var coords = GetElemCoord(cell);
        coords.x -= coordCont.x;
        coords.y -= coordCont.y;
        if ( cell.style.verticalAlign = "bottom" )
        {
            coords.y += (cell.offsetHeight - imgDiv.offsetHeight);
        }
        imgDiv.style.position = "absolute";
        imgDiv.style.left = coords.x + 'px';
        imgDiv.style.top = coords.y + 'px';
    }
    else
    {
        cell.innerHTML = imgDiv.innerHTML;
        imgDiv.style.display = "none";
    }
}
function NodeClicked(attribute)
{
    //var link = document.getElementById(id);
    if ( !LoggedIn() )
        return;
    eval(attribute);
}
function IsAnchor(obj)
{
    return (obj != null && (obj.tagName == "A" || obj.tagName == "a"));
}
function treeview_onclick(event)
{
    event = event || window.event;
    var obj = event.target || event.srcElement;
    if (obj == null || obj.tagName == null)
        return false;
    if (!IsAnchor(obj))
    {
        return true;
    }
    if ( !LoggedIn() )
    {
        event.preventDefault();
        return false;
    }
    return true;
}
function MasterOnload()
{
/*
    var tree = GetFormObject("DEMOTREE", false);
    var links = (tree != null) ? tree.getElementsByTagName("a") : null;
    for (var i = 0; i < links.length; i++)
    {
        var href = links[i].getAttribute("href");
        var href2 = "if(LoggedIn())" + href;
        //links[i].setAttribute("href", href2);
        //links[i].setAttribute("href", "javascript:NodeClicked(\"" + links[i].id + "\", \"" + links[i].getAttribute("href") + "\")");
        if ( href.indexOf("_doPostBack") != -1 )
            links[i].setAttribute("href", "javascript:NodeClicked(\"" + links[i].getAttribute("href") + "\")");
    }
*/    
    hitBreakpoint();

}
function correctPNG()
{
    MasterOnload();
    MoveHdrTable("HDRTABLE", "RBOXY_1", 0, 6, 0, 0);
    return;
}        
function LoggedIn()
{
    var edit = GetFormObject("LOGIN_EDIT", false);
    if ( edit == null )
        return false;
    if ( edit.disabled == false )
    {
        alert ('This feature is not available to unauthorized users.\nPlease enter your login name and password.');
        return false;
    }
    return true;
}
function ValidateSearchClick()
{
    var edit = GetFormObject("QUERYID", false);
    var chkBox = GetFormObject("SHOWALLCHK", false);
    if ( chkBox == null || edit== null )
        return false;
    if ( chkBox.checked == false && (edit.value == null || edit.value.length == 0) )
    {
        alert ('Please enter a query ID');
        return false;
    }
    return LoggedIn();
}
function EnableSearchCtrl(chkbox)
{
    var edit = GetFormObject("QUERYID", false);        
    if ( chkbox == null || edit== null )
        return;
    edit.disabled = chkbox.checked;
    return false;
}
function VerifyHLink(link)
{
    if ( link == null || link.className == "curpage" )
        return false;
    if ( (link.id == GetFullName ("MENU_RGST", false)) && !IsCustomer() )
        return false;
    if ( link.id != GetFullName ("MENU_DWNL", false) &&
        link.id != GetFullName ("MENU_UMNL", false) )
        return true;
    return LoggedIn();
}
function IsCustomer()
{
    var edit = GetFormObject("LOGIN_EDIT", false);
    if ( edit == null )
        return false;
    if ( (edit.value.length != 0) && (edit.value.search(':') >= 0) )
    {
        alert ('This option is only available for MPG customers');
        return false;
    }
    return true;
}
function CanRertievePassword()
{
    var edit = GetFormObject("LOGIN_EDIT", false);
    if ( edit == null )
        return false;
    if ( !IsCustomer() )
        return false;
    if ( edit.value.length == 0 )
    {
        alert ('Please enter your login name');
        return false;
    }                
    return true;                
}
function GetFullName(objName, placeholder)
{
    var fullName = "MPGMasterPage_";
    if ( placeholder )
        fullName += "MPGContentPlaceHolder_";
    fullName += objName;
    return fullName;
}
function GetFormObject(objName, placeholder)
{
    var fullName = GetFullName(objName, placeholder);
    return document.getElementById(fullName);
}
function VerifyRegistrationForm(tableID, loginForm)
{
    var bValidForm = true;
    var table = GetFormObject(tableID, true);
    if ( table == null )
        return false;
    var cellTitle = "";
    var pw = "";
    var tblRows = table.rows;
    for (var i=0; i<tblRows.length; i++)
    {
        if ( tblRows[i].cells.length < 2 )
            continue;
        var titleCell = tblRows[i].cells[0];
        if ( titleCell.className != "reqtitle" )
            continue;
        var valueCell = tblRows[i].cells[1];
        if ( valueCell.childNodes.length == 0 )
            continue;
        if ( valueCell.childNodes[0].tagName == 'SELECT' )
        {
            var nIndex = parseInt(valueCell.childNodes[0].value);
            if ( nIndex < 0 )
            {
                cellTitle = IsFirefoxOrChrome() ? titleCell.textContent : titleCell.innerText;
                bValidForm = false;
                break;
            }
        }
        if ( valueCell.childNodes[0].tagName != 'INPUT' )
            continue;
        if ( valueCell.childNodes[0].value.length == 0 )
        {
            cellTitle = IsFirefoxOrChrome() ? titleCell.textContent : titleCell.innerText;
            bValidForm = false;
            break;
        }
        else if ( loginForm == true )
        {
            if ( valueCell.childNodes[0].type == 'password' )
            {
                if ( pw.length == 0 )
                    pw = valueCell.childNodes[0].value;
                else if ( pw != valueCell.childNodes[0].value )
                {
                    alert ('The passwords you entered are not the same.');
                    return false;
                }
            }
            else if ( i == 0 )
            {
                if ( valueCell.childNodes[0].value.search(':') >= 0 ||
                    valueCell.childNodes[0].value.search(';') >= 0 )
                {
                    alert ('Colon (:) and semicolon (;) are not allowed in the login name');
                    return false;
                }
                var prefix = valueCell.childNodes[0].value.substring(0, 9);
                if ( prefix == 'MPGLOGIN_' )
                {
                    alert ('\'MPGLOGIN_\' is a reserved login prefix.\nPlease provide a different login name');
                    return false;
                }
            }
        }
    }
    if ( bValidForm == true )
        document.body.style.cursor = "wait";
    else
        alert ('\'' + cellTitle + '\' is a required field\nand must be provided.');
    return bValidForm;
}
function DisplayComboText(tableID)
{
    var table = GetFormObject(tableID, true);
    if ( table == null )
        return;
    var tblRows = table.rows;
    for (var i=0; i<tblRows.length; i++)
    {
        if ( tblRows[i].cells.length < 2 )
            break;
        if ( tblRows[i].cells[1].childNodes.length < 2 )
            continue;
        var edit = tblRows[i].cells[1].childNodes[0];
        var list = tblRows[i].cells[1].childNodes[1];
        if ( list != null && edit != null )
        {
            edit.value = list.value;
            edit.focus();
        }
    }
}
function VerifyLogin()
{
    var btn = GetFormObject("LOGINBTN", false);
    if ( btn != null )
    {
        if ( btn.value == "Logout" )
            return true;
    }
    var bResult = true;
    for (var i=0; i<2; i++)
    {
        var ID = (i > 0) ? 'PASSWORD_EDIT' : 'LOGIN_EDIT';
        var edit = GetFormObject(ID, false);
        if ( edit != null )
        {
            if ( edit.value.length == 0 )
            {
                bResult = false;
                if ( i == 1 )
                    alert('Password cannot be empty.');
                else
                    alert('Login name cannot be empty.');
                break;
            }
        }
    }
    return bResult;
}

function OnPreNodeSelected(id)
{
}

