﻿var BGDivTimeoutID;
var EditQuoteMessageTriggered;
var QuoteChangeMessageShown;
var InvalidQuoteDataMessageShown;
var Back_zindex = 100;
var Mid_zindex = 500;
var Fore_zindex = 900;

//function cancelSubmitOnEnterKey(e) {
//    if (!e) e = window.event;
//    if (e.keyCode) code = e.keyCode;
//    else if (e.which) code = e.which;
//    if (code == 13) {
//        e.returnValue = false;
//        e.cancel = true;
//        return false;
//    } else {
//        return true;
//    }
//}

function MakeMeExclusive(ControlId) 
{
    //var Control = document.getElementById(ControlId);
    
    //Control.style.position = 'absolute';
    //Control.style.zIndex = Fore_zindex

   //ShowDataEditDiv();
}

function MakeMeUnexclusive(ControlId) 
{
    //var Control = document.getElementById(ControlId);
    //Control.style.position = '';
    //Control.style.zIndex = Back_zindex;

    //HideDataEditDiv();
}

function TestForEditQuoteFlag() 
{
    if (EditQuoteMessageTriggered) {
        var answer = confirm('This action will result in the creation of a new quote; select Ok to proceed. If you instead want to edit the existing quote, select Cancel, and then select Edit Quote.');
        if (!answer) {
            return false;
        }
        else {
            ResetEditQuoteMessageTrigger();
        }
    }
}

function ShowAnswerChangeInvalidatedOtherDataMessage() 
{
    if (!QuoteChangeMessageShown) 
    {
        var answer = confirm('Changes made to original data may invalidate other original data. If you continue, each selection affected by your change will be cleared, and you will have to enter the appropriate new information. Click OK to continue, or click Cancel to end this Edit Quote action and return to the Home page.');
        QuoteChangeMessageShown = true;
        if (!answer) {
            StartOver();
        }
    }
}

function ShowInvalidQuoteDataMessage() {
    if (!InvalidQuoteDataMessageShown) {
        var answer = confirm('The questions/valid responses associated with this type of transaction have changed since the original quote was created. If you continue, you will have to reenter much of the data. Click OK to continue, or click Cancel to end this Edit Quote action and return to the Home page.');
        InvalidQuoteDataMessageShown = true;
        if (!answer) {
            StartOver();
        }
    }
}

function SetEditQuoteMessageTrigger() {
    EditQuoteMessageTriggered = true;
}

function ResetEditQuoteMessageTrigger() {
    EditQuoteMessageTriggered = false;
}

function ResetQuoteDataMessageTriggers() 
{
    ResetQuoteChangeMessageTrigger();
    ResetInvalidQuoteDataMessageTrigger();
    ResetEditQuoteMessageTrigger()
}

function ResetQuoteChangeMessageTrigger() {
    QuoteChangeMessageShown = false;
}

function ResetInvalidQuoteDataMessageTrigger() {
    InvalidQuoteDataMessageShown = false;
}

function StartOver() {
    //var QueryString = window.location.search.substring(1);
    location.href = window.location.href;
} 

function FinishTestCaseEntry(RateRequestId, WasCancelled) 
{
    window.external.RateRequestEditComplete(RateRequestId);
}

function showDiv(divId) {
    var div = document.getElementById(divId);
    div.style.display = '';
}

function hideDiv(divId) {
    var div = document.getElementById(divId);
    div.style.display = 'none';
}

function showHide(divId)
{
    var div = document.getElementById(divId);
    if(div.style.display == 'none')    
    {
        div.style.display = '';
    }
    else
    {
        div.style.display = 'none';
    }
}

function Validate() {
    return Page_ClientValidate();
}

//function showHide(checkbox,divId)
//{
//    var div = document.getElementById(divId);
//    if(checkbox.checked)    
//    {
//        div.style.display = '';
//    }
//    else
//    {
//        div.style.display = 'none';
//    }
//}

//This function disables all links on a page exception those passed in as exceptions
function DisableLinksWithExceptions (Exceptions) 
{
    var input = document.getElementsByTagName("a");
    var count = input.length;

    for(var i=0;i < count;i++)
    {
        var match = false;
        
        for (var j = 0; j < Exceptions.length; j++)
        {
            if (document.getElementsByTagName("a")[i].id == Exceptions[j])
            {
                match = true;
                continue;
            }
        }
        
        if (!match)
        {
            document.getElementsByTagName("a")[i].onclick = retFalse;
            document.getElementsByTagName("a")[i].disabled = true;
            document.getElementsByTagName("a")[i].removeAttribute("href");
        }
    }

    return true;
}

function DisableInput()
{
    var input = document.getElementsByTagName("input");
    var count = input.length;

    for(var i=0;i < count;i++)
    {
        document.getElementsByTagName("input")[i].onclick = retFalse;
        document.getElementsByTagName("input")[i].disabled = true;
        //document.getElementsByTagName("input")[i].removeAttribute("href");
    }

    var input = document.getElementsByTagName("textarea");
    var count = input.length;

    for(var i=0;i < count;i++)
    {
        document.getElementsByTagName("textarea")[i].onclick = retFalse;
        document.getElementsByTagName("textarea")[i].disabled = true;
        //document.getElementsByTagName("input")[i].removeAttribute("href");
    }

    var input = document.getElementsByTagName("select");
    var count = input.length;

    for(var i=0;i < count;i++)
    {
        document.getElementsByTagName("select")[i].onclick = retFalse;
        document.getElementsByTagName("select")[i].disabled = true;
        //document.getElementsByTagName("input")[i].removeAttribute("href");
    }

    return true;
}

function SoftDisableInput(DelayTime)
{
    //add delay Time
    BGDivTimeoutID = setTimeout("ShowBgDiv()", DelayTime);
}

function ShowBgDiv() {
    var theBody = document.getElementsByTagName("BODY")[0];

    var fullHeight = getViewportHeight();
    var fullWidth = getViewportWidth();

    // Determine what's bigger, scrollHeight or fullHeight / width
    if (fullHeight > theBody.scrollHeight) {
        popHeight = fullHeight;
    } else {
        popHeight = theBody.scrollHeight;
    }

    if (fullWidth > theBody.scrollWidth) {
        popWidth = fullWidth;
    } else {
        popWidth = theBody.scrollWidth;
    }

    var backgroundDiv = document.getElementById('backgroundDiv');
    var LoadingImage = document.getElementById('pnlLoading');

    backgroundDiv.style.height = popHeight + 'px';
    backgroundDiv.style.width = popWidth + 'px';

    centerLoadingWin(LoadingImage.width, LoadingImage.height);

    // make our covering div visible
    backgroundDiv.style.display = '';
}

function HideDataEditDiv() 
{
    var backgroundDiv = document.getElementById('EditMask');
    backgroundDiv.style.zIndex = Back_zindex;
    backgroundDiv.style.display = 'none';
}

function ShowDataEditDiv() 
{
    var theBody = document.getElementsByTagName("BODY")[0];

    var fullHeight = getViewportHeight();
    var fullWidth = getViewportWidth();

    // Determine what's bigger, scrollHeight or fullHeight / width
    if (fullHeight > theBody.scrollHeight) {
        popHeight = fullHeight;
    } else {
        popHeight = theBody.scrollHeight;
    }

    if (fullWidth > theBody.scrollWidth) {
        popWidth = fullWidth;
    } else {
        popWidth = theBody.scrollWidth;
    }

    var backgroundDiv = document.getElementById('EditMask');
    
    backgroundDiv.style.height = popHeight + 'px';
    backgroundDiv.style.width = popWidth + 'px';

    backgroundDiv.style.zIndex = Mid_zindex;

    // make our covering div visible
    backgroundDiv.style.display = '';
}

function centerLoadingWin(width, height) {
    var LoadingImage = document.getElementById('pnlLoading');

    if (width == null || isNaN(width)) {
        width = LoadingImage.offsetWidth;
    }
    if (height == null) {
        height = LoadingImage.offsetHeight;
    }

    var theBody = document.getElementsByTagName("BODY")[0];
    var scTop = parseInt(getScrollTop(), 10);
    var scLeft = parseInt(theBody.scrollLeft, 10);

    var fullHeight = getViewportHeight();
    var fullWidth = getViewportWidth();

    LoadingImage.style.top = (scTop + ((fullHeight - height) / 2)) + "px";
    LoadingImage.style.left = (scLeft + ((fullWidth - width) / 2)) + "px";
}

function SoftEnableInput()
{
    clearTimeout(BGDivTimeoutID);
    document.getElementById('backgroundDiv').style.display = 'none';
}

function retFalse () 
{
    return false;
}
    
function EvaluateRequiredField(sender, args)
{
    if (args.Value != "")
    {
        args.IsValid = true;
    }
    else
    {
        args.IsValid = false;
    }
}

function Show(source, target, evt)
{
    var MinLength = (evt.type == "keydown") ? 1 : 0;

    var charCode = (evt.which) ? evt.which : event.keyCode
    
    if (charCode == 8 && source.value.length <= MinLength)
    {
        document.getElementById(target).style.display = 'none';
    }
    else
    {                
        if ((charCode == 8) || (charCode >= 48 && charCode <= 90) || (charCode >= 96 && charCode <=111) || (charCode >= 186 && charCode <= 222))
        {
            if (document.getElementById(target).style.display != 'inline')
            {
                document.getElementById(target).style.display = 'inline';
            }
        }
    }
}

function cursor_wait() {
    //$get('Main').style.cursor = 'wait';
    //document.getElementById('Main').style.cursor = 'wait';
    document.body.style.cursor = 'wait';
}

function cursor_clear() {
    //$get('Main').style.cursor = 'auto';
    //document.getElementById('Main').style.cursor = 'auto';
    document.body.style.cursor = 'auto';
}

function $()
{ 
    var elements = new Array(); 
    for (var i=0,len=arguments.length;i<len;i++) 
    { 
        var element = arguments[i]; 
        if (typeof element == 'string') 
        { 
            var matched = document.getElementById(element); 
            if (matched) 
            { 
                elements.push(matched); 
            } 
            else 
            { 
                var allels = (document.all) ? document.all : document.getElementsByTagName('*'); 
                var regexp = new RegExp('(^| )'+element+'( |$)'); 
                for (var i=0,len=allels.length;i<len;i++) 
                    if (regexp.test(allels[i].className)) elements.push(allels[i]); 
            } 
            if (!elements.length) 
                elements = document.getElementsByTagName(element); 
            if (!elements.length) 
            { 
                elements = new Array(); 
                var allels = (document.all) ? document.all : document.getElementsByTagName('*'); 
                for (var i=0,len=allels.length;i<len;i++) 
                    if (allels[i].getAttribute(element)) elements.push(allels[i]); 
            } 
            if (!elements.length) 
            { 
                var allels = (document.all) ? document.all : document.getElementsByTagName('*'); 
                for (var i=0,len=allels.length;i<len;i++) 
                    if (allels[i].attributes) 
                        for (var j=0,lenn=allels[i].attributes.length;j<lenn;j++) 
                            if (allels[i].attributes[j].specified) 
                                if (allels[i].attributes[j].nodeValue == element) elements.push(allels[i]); 
            } 
        }
        else
        { 
            elements.push(element); 
        } 
    } 
    
    if (elements.length == 1) 
    { 
        return elements[0]; 
    } 
    else
    { 
        return elements; 
    }
}

/**
* COMMON DHTML FUNCTIONS
* These are handy functions I use all the time.
*
* By Seth Banks (webmaster at subimage dot com)
* http://www.subimage.com/
*
* Up to date code can be found at http://www.subimage.com/dhtml/
*
* This code is free for you to use anywhere, just keep this comment block.
*/

/**
* X-browser event handler attachment and detachment
* TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
*
* @argument obj - the object to attach event to
* @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
* @argument fn - function to call
*/
function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    } else {
        return false;
    }
}
function removeEvent(obj, evType, fn, useCapture) {
    if (obj.removeEventListener) {
        obj.removeEventListener(evType, fn, useCapture);
        return true;
    } else if (obj.detachEvent) {
        var r = obj.detachEvent("on" + evType, fn);
        return r;
    } else {
        alert("Handler could not be removed");
    }
}

/**
* Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
*
* Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
*
* Gets the full width/height because it's different for most browsers.
*/
function getViewportHeight() {
    if (window.innerHeight != window.undefined) return window.innerHeight;
    if (document.compatMode == 'CSS1Compat') return document.documentElement.clientHeight;
    if (document.body) return document.body.clientHeight;

    return window.undefined;
}
function getViewportWidth() {
    var offset = 17;
    var width = null;
    if (window.innerWidth != window.undefined) return window.innerWidth;
    if (document.compatMode == 'CSS1Compat') return document.documentElement.clientWidth;
    if (document.body) return document.body.clientWidth;
}

/**
* Gets the real scroll top
*/
function getScrollTop() {
    if (self.pageYOffset) // all except Explorer
    {
        return self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
    {
        return document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
        return document.body.scrollTop;
    }
}
function getScrollLeft() {
    if (self.pageXOffset) // all except Explorer
    {
        return self.pageXOffset;
    }
    else if (document.documentElement && document.documentElement.scrollLeft)
    // Explorer 6 Strict
    {
        return document.documentElement.scrollLeft;
    }
    else if (document.body) // all other Explorers
    {
        return document.body.scrollLeft;
    }
}

