﻿// JScript File

//--Browser object for other functions to check
function browser()
{
    var ua, s, i;
    this.isIE = false;
    this.isNS = false;
    this.version = null;
    
    ua = navigator.userAgent;
    s = "MSIE";
    if ((i = ua.indexOf(s)) >= 0)
    {
        this.isIE = true;
        this.version = parseFloat(ua.substr(i + s.length));
        g_IE = true;
        
        return;
    }
    s = "Netscape";
    if ((i = ua.indexOf(s)) >= 0)
    {
        this.isNS = true;
        this.version = parseFloat(ua.substr(i + s.length));
        
        return;
    }
    s = "Gecko";
    if ((i = ua.indexOf(s)) >= 0)
    {
        this.isNS = true;
        this.version = 6.1;
        
        return;
    }
}

var g_IE = false;
var g_browser = new browser();
var g_coordSep = "|";

function pickTool(strTool, toolTxtBoxID, coordsTxtBoxID, evt)
{
    evt = getEvent(evt);
    var toolTxtBox = document.getElementById(toolTxtBoxID);
    toolTxtBox.value = strTool;
    clearCoords(coordsTxtBoxID);
    
    cancelEvent(evt);
}

function getEleX(ele, evt)
{
    evt = getEvent(evt);
    var parEle = ele;
    var x = evt.clientX;
    if (g_IE)
        x += document.documentElement.scrollLeft + document.body.scrollLeft;
    else
        x += window.scrollX;
    var isAbs = false;
    while (parEle)
    {
        if (parEle.style != null &&
            parEle.style.position.toLowerCase() == "absolute" &&
            parEle.style.left != "")
        {
            x -= parseInt(parEle.style.left, 10);
            if (g_IE)
            {
                if (parEle.scrollLeft)
                    x += parEle.scrollLeft;
            }
            else
            {
                if (parEle.scrollX)
                    x += parEle.scrollX;
            }
            isAbs = true;
        }
        parEle = parEle.parentNode;
    }
    if (!isAbs)
        throw new Error(
            "Element must be contained in an absolutely positioned context.");
    
    return x >= 2 ? x - 2 : 0;
}

function getEleY(ele, evt)
{
    evt = getEvent(evt);
    var parEle = ele;
    var y = evt.clientY;
    if (g_IE)
        y += document.documentElement.scrollTop + document.body.scrollTop;
    else
        y += window.scrollY;
    var isAbs = false;
    while (parEle)
    {
        if (parEle.style != null &&
            parEle.style.position.toLowerCase() == "absolute" &&
            parEle.style.top != "")
        {
            y -= parseInt(parEle.style.top, 10);
            if (g_IE)
            {
                if (parEle.scrollTop)
                    y += parEle.scrollTop;
            }
            else
            {
                if (parEle.scrollY)
                    y += parEle.scrollY;
            }
            isAbs = true;
        }
        parEle = parEle.parentNode;
    }
    if (!isAbs)
        throw new Error(
            "Element must be contained in an absolutely positioned context.");
    
    return y >= 2 ? y - 2 : 0;
}

function addCoords(txtBoxID, evt)
{
    evt = getEvent(evt);
    var ele = getEventElement(evt);
    var coords = document.getElementById(txtBoxID);
    coords.value += getEleX(ele, evt).toString() + g_coordSep +
        getEleY(ele, evt).toString() + g_coordSep;
        
    cancelEvent(evt);
}

function clearCoords(txtBoxID)
{
    var ele = document.getElementById(txtBoxID);
    ele.value = "";
}

function confirmAction(str, evt)
{
    evt = getEvent(evt);
    if (!confirm(str))
        cancelEvent(evt);
}

function loadTextBox(ddid, tid, evt)
{
    evt = getEvent(evt);
    var dd = document.getElementById(ddid);
    var t = document.getElementById(tid);
    if (null == dd || null == t)
        return;
        
    t.value = dd.value;
    t.focus();
    t.select();
}

function isEmptyTextBox(id, evt)
{
    evt = getEvent(evt);
    var lntbox = document.getElementById(id);
    if(lntbox.value == '')
    {
        alert('Please enter some text to search on.');
        cancelEvent(evt);
    }
}

function newWin(url, winName, cancelClick, evt)
{
    evt = getEvent(evt);
    var win = window.open(url, winName, "width=800,height=600,resizable=yes", true);
    win.focus();
    if (cancelClick)
        cancelEvent(evt);
}

//--Prevents the event from going through to the server.
function cancelEvent(evt)
{
    if (g_IE)
    {
        evt.cancelBubble = true;
        evt.returnValue = false;
    }
    else
        evt.preventDefault();
}

//--Gets the event passed to a function, or creates it if IE.
function getEvent(evt)
{
    if (g_IE)
        evt = window.event;
        
    return evt;
}

//--Gets the target of an event.
function getEventElement(evt)
{
    var ele;
    if (g_IE)
        ele = evt.srcElement;
    else
        ele = evt.target;
        
    return ele;
}

function clearText(compareText, evt)
{
    evt = getEvent(evt);
    var textBox = getEventElement(evt);
    
    if (textBox.value == compareText)
        textBox.value = "";
}
   
function collExp(eleID, ht, evt)
{
    if (null == evt)
        evt = window.event;
        
    var sender = evt.srcElement;
    if (null == sender)
        sender = evt.target;
    
    var divEle = document.getElementById(eleID);
    if(divEle.style.overflow == "hidden")
    {
        divEle.style.height = ht;
        divEle.style.visibility = "visible";
        divEle.style.overflow = "visible";
    }
    else
    {
        divEle.style.height = "0px";
        divEle.style.visibility = "hidden";
        divEle.style.overflow = "hidden";
    }
}

function imgWindow(url, winName, evt)
{
    evt = getEvent(evt);
    var win = window.open(url, winName, "width=415,height=430,resizable=yes", true);
    win.focus();
}

function configWindow(url, winName, evt)
{
    evt = getEvent(evt);
    var win = window.open(url, winName,
        "width=830,height=650,resizable=yes,scrollbars=yes", true);
    win.focus();
}

function openReport(clientID, quoteNumber, evt)
{
    evt = getEvent(evt);
    var ele = document.getElementById(clientID);
    var selection = ele.selectedIndex;
    var url;
    
    if (selection == 0)
        url = "reports/CustomerProposalReport.aspx?Quote_ID=" + quoteNumber;
    if (selection == 1)
        url = "reports/QuoteDetailReport.aspx?Quote_ID=" + quoteNumber;
     if (selection == 2)
         url = "reports/QuoteSummaryReport.aspx?Quote_ID=" + quoteNumber;

    var win = window.open(url, "quoteReport", "width=800,height=600,resizable=yes", true);
    win.focus();
}

function fade(imageId)
{
    image = document.getElementById(imageId);
    if (null == image)
        return;
    
    setOpacity(image, 0);
    image.style.visibility = "visible";
    fadeIn(imageId, 0);
}

function revealRight(divId, maxWidth)
{
    var div = document.getElementById(divId);
    if (null == div)
        return;
    
    div.style.overflow = "hidden";
    setWidth(divId, 0, maxWidth);
}

function revealDown(divId, maxHeight)
{
    var div = document.getElementById(divId);
    if (null == div)
        return;
    
    div.style.overflow = "hidden";
    setHeight(divId, 0, maxHeight);
}

function revealRightDown(divId, maxWidth, maxHeight)
{
    var div = document.getElementById(divId);
    if (null == div)
        return;
    
    div.style.overflow = "hidden";
    setWidth(divId, 0, maxWidth);
    setHeight(divId, 0, maxHeight);
}

function slideRight(divId, width)
{
    var div = document.getElementById(divId);
    if (null == div)
        return;
    
    div.style.overflow = "hidden";
    setXPos(divId, -width, 0, width);
}

function slideDown(divId, height)
{
    var div = document.getElementById(divId);
    if (null == div)
        return;
    
    div.style.overflow = "hidden";
    setYPos(divId, -height, 0, height);
}

function slideRightDown(divId, width, height)
{
    var div = document.getElementById(divId);
    if (null == div)
        return;
    
    div.style.overflow = "hidden";
    setXPos(divId, -width, 0, width);
    setYPos(divId, -height, 0, height);
}

function setWidth(divId, width, maxWidth)
{
    var div = document.getElementById(divId);
    if (null == div)
        return;
    
    width += maxWidth / 30;
    if (width > maxWidth)
        width = maxWidth;
    if (width <= maxWidth)
    {
        div.style.width = width.toString() + "px";
        if (width < maxWidth)
        {
            window.setTimeout("setWidth('" + divId + "'," + width +
                "," + maxWidth + ")", 10);
        }
    }
}

function setHeight(divId, height, maxHeight)
{
    var div = document.getElementById(divId);
    if (null == div)
        return;
    
    height += maxHeight / 30;
    if (height > maxHeight)
        height = maxHeight;
    if (height <= maxHeight)
    {
        div.style.height = height.toString() + "px";
        if (height < maxHeight)
        {
            window.setTimeout("setHeight('" + divId + "'," + height +
                "," + maxHeight + ")", 10);
        }
    }
}

function setXPos(divId, pos, finishPos, width)
{
    var div = document.getElementById(divId);
    if (null == div)
        return;
    
    pos += width / 30;
    if (pos > finishPos)
        pos = finishPos;
    if (pos <= finishPos)
    {
        div.style.left = pos.toString() + "px";
        if (pos < finishPos)
        {
            window.setTimeout("setXPos('" + divId + "'," + pos +
                "," + finishPos + "," + width + ")", 10);
        }
    }
}

function setYPos(divId, pos, finishPos, height)
{
    var div = document.getElementById(divId);
    if (null == div)
        return;
    
    pos += height / 30;
    if (pos > finishPos)
        pos = finishPos;
    if (pos <= finishPos)
    {
        div.style.top = pos.toString() + "px";
        if (pos < finishPos)
        {
            window.setTimeout("setYPos('" + divId + "'," + pos +
                "," + finishPos + "," + height + ")", 10);
        }
    }
}

function setOpacity(obj, opacity)
{
    opacity = (opacity == 100) ? 99.999 : opacity;

    // IE/Win
    obj.style.filter = "alpha(opacity:" + opacity + ")";

    // Safari<1.2, Konqueror
    obj.style.KHTMLOpacity = opacity / 100;

    // Older Mozilla and Firefox
    obj.style.MozOpacity = opacity / 100;

    // Safari 1.2, newer Firefox and Mozilla, CSS3
    obj.style.opacity = opacity / 100;
}

function fadeIn(objId, opacity)
{
    if (document.getElementById)
    {
        obj = document.getElementById(objId);
        if (opacity <= 100)
        {
            setOpacity(obj, opacity);
            opacity += 2;
            window.setTimeout("fadeIn('" + objId + "'," + opacity + ")", 15);
        }
    }
}