﻿Sys.Application.add_init(AppInit);

var displayExitPopup=true; //used for the popup on the special financing application into page

function AppInit(sender) {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(End);
    //Init();
//    if (Sys.WebForms != undefined) 
//    {
//        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(InitializeRequest);
//        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);
//    }
}

function End(sender, args) { }

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_initializeRequest(InitializeRequest);
    prm.add_endRequest(EndRequest);
 
    function InitializeRequest(sender, args) {
        //if (args.get_postBackElement().id != GetClientId("bestValueVehicleUpdateTimer"))
        //{
            document.body.style.cursor = 'wait';

            //if (args.get_postBackElement().id != GetClientId("addVehicleVinUpdatePanel") && args.get_postBackElement().id != GetClientId("addVehicleBasicOptionsStoryUpdatePanel") && args.get_postBackElement().id != GetClientId("addVehicleSiteOptionsUpdatePanel") && args.get_postBackElement().id != GetClientId("addVehicleOtherFinancialUpdatePanel") && args.get_postBackElement().id != GetClientId("sellVehicleVinUpdatePanel") && args.get_postBackElement().id != GetClientId("sellVehicleInfoUpdatePanel") && args.get_postBackElement().id != GetClientId("editVehicleVinUpdatePanel") && args.get_postBackElement().id != GetClientId("editVehicleBasicUpdatePanel") && args.get_postBackElement().id != GetClientId("editVehicleFeaturesAndStoryUpdatePanel") && args.get_postBackElement().id != GetClientId("editVehiclePhotosUpdatePanel") && args.get_postBackElement().id != GetClientId("editVehicleSiteOptionsUpdatePanel") && args.get_postBackElement().id != GetClientId("editVehicleOtherFinancialUpdatePanel") && args.get_postBackElement().id != GetClientId("changeVehiclePriceSortUpdatePanel") && args.get_postBackElement().id != GetClientId("changeVehiclePriceMainUpdatePanel"))
            if ((args.get_request().get_url().indexOf("VehicleInventory.aspx") == -1) && (args.get_request().get_url().indexOf("Customers.aspx") == -1) && (args.get_request().get_url().indexOf("Stats.aspx") == -1) && (args.get_request().get_url().indexOf("IHFAPage3.aspx") == -1) && (args.get_request().get_url().indexOf("IHFAPage2.aspx") == -1) && (args.get_request().get_url().indexOf("IHFAPage1.aspx") == -1) && (args.get_request().get_url().indexOf("IHFAPage4.aspx") == -1) && (args.get_request().get_url().indexOf("IHFAReviewPage.aspx") == -1) && (args.get_request().get_url().indexOf("Customer.aspx") == -1) && (args.get_postBackElement().id != GetClientId("searchMakeDropDownList")) && (args.get_postBackElement().id != GetClientId("searchButton")) && (args.get_postBackElement().id != GetClientId("displayMostRecentButton")) && (args.get_postBackElement().id != GetClientId("selectedMakeModelYearSortRadio")) && (args.get_postBackElement().id != GetClientId("selectedMakeModelPriceSortRadio")) && (args.get_request().get_url().indexOf("BuyHerePayHereForm.aspx") == -1) && (args.get_postBackElement().id != GetClientId("bestValuePreviousVehicleButton")) && (args.get_postBackElement().id != GetClientId("bestValueNextVehicleButton"))) {
                window.scrollTo(0, 163);
            }
            else 
            {
                if (args.get_postBackElement().id == GetClientId("VehicleInventoryTree")) 
                {
                    window.scrollTo(0, 0);
                }
            }
            
            if (args.get_postBackElement().id == GetClientId("CustomerTree")) 
            {
                var showCustomerOverallPanel = getObjById(GetClientId("showCustomerOverallPanel"));
                var showCustomerLoadingPanel = getObjById(GetClientId("loadingShowCustomerPanel"));

                showCustomerOverallPanel.style.display = 'none';
                showCustomerLoadingPanel.style.display = 'inline';                
            }
            
            //var mainPageLoadingPanel = getObjById('ctl00_mainContent_loadingTransPanel');
            //GetClientId("txtTest"));
            if ((args.get_postBackElement().id != GetClientId("bestValuePreviousVehicleButton")) && (args.get_postBackElement().id != GetClientId("bestValueNextVehicleButton")) && (args.get_postBackElement().id != GetClientId("selectedVehicleShareModalSendEmailButton")) && (args.get_postBackElement().id != GetClientId("globalEmailShareModalSendEmailButton")))
            {
                var mainPageLoadingPanel = getObjById(GetClientId("loadingTransPanel"));
                //var mainPageLoadingImg = getObjById('ctl00_mainContent_loadingTransPanelLoadingProgress');
                var mainPageLoadingImg = getObjById(GetClientId("loadingTransPanelLoadingProgress"));
                if (mainPageLoadingPanel != null)
                {
                    mainPageLoadingPanel.style.display = 'block';
                }
                if (mainPageLoadingImg != null)
                {
                    mainPageLoadingImg.style.display = 'inline';
                }
            }            
            //getObjById('ctl00_mainContent_loadingTransPanel').style.display = 'block';
            //getObjById('ctl00_mainContent_loadngTransPanelLoadingProgress').style.display = 'inline';
            //alert(document.getElementById("<%=loadngTransPanelLoadingProgress.ClientID%>"));
            //alert(getObjById('<%= loadngTransPanelLoadingProgress.ClientID %>'));
    //        $get('addVehicleTabPanel').style.cursor = 'wait';
            //ctl00_mainContent_bestValueVehicleUpdateTimer
        //}        
    }
    
    function GetClientId(dummy)
    {
        return null;
    }
 
    function EndRequest(sender, args) {
        document.body.style.cursor = 'auto';
        
        //var mainPageLoadingPanel = getObjById('ctl00_mainContent_loadingTransPanel');
        //var mainPageLoadingImg = getObjById('ctl00_mainContent_loadingTransPanelLoadingProgress');
        var mainPageLoadingPanel = getObjById(GetClientId("loadingTransPanel"));
        var mainPageLoadingImg = getObjById(GetClientId("loadingTransPanelLoadingProgress"));
        if (mainPageLoadingPanel != null)
        {
            mainPageLoadingPanel.style.display = 'none';
        }
        if (mainPageLoadingImg != null)
        {
            mainPageLoadingImg.style.display = 'none';
        }
        
        // Get a reference to the iframe.
        var adFrame = document.getElementById('AdFrame');         
        // Force it to refresh, by setting its location to
        //  whatever it currently is.
        if (adFrame != null)
        {
            adFrame.src = adFrame.src;
        }

        
        //getObjById('ctl00_mainContent_loadingTransPanel').style.display = 'none';
        //getObjById('ctl00_mainContent_loadngTransPanelLoadingProgress').style.display = 'none';
        //$get('loadingTransPanel').style.display = 'none';
        //$get('loadngTransPanelLoadingProgress').style.display = 'none';
//        $get('addVehicleTabPanel').style.cursor = 'default';
    }
    
    function getObjById( id )
    { 
        if (document.getElementById) 
            var returnVar = document.getElementById(id); 
        else if (document.all) 
            var returnVar = document.all[id]; 
        else if (document.layers) 
            var returnVar = document.layers[id]; 
        return returnVar; 
    }

    function cursor_wait()
    {
        document.body.style.cursor = 'wait';
    }
    
//    function resetDotNetScrollPosition()
//    {
//        var scrollX = document.getElementById('__SCROLLPOSITIONX');
//        var scrollY = document.getElementById('__SCROLLPOSITIONY');

//        if(scrollX != null && scrollY != null)
//        {
//            scrollX.value = 0;
//            scrollY.value = 0;
//        }
    //    }

//function Init() {
//    var form = document.getElementById("aspnetForm");
//    if (form.addEventListener) {    // Firefox, Opera and Safari
//        form.addEventListener("focus", OnFocusInForm, true);
//        form.addEventListener("blur", OnFocusOutForm, true);
//    }
//}


String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function checkValidInputAndAlertOnBlur(inputBox, regEx, errorLabelRequired, errorLabelInvalid, watermarkText, inputIsOptional, boxIsMasked, maskCharacter) {
    var boxInput = inputBox.value.trim();

    if (boxIsMasked) {
        boxInput = inputBox.value.replace(new RegExp(maskCharacter, 'g'), '').trim();
    }
    if (boxInput.match(regEx) && boxInput != watermarkText) {
        inputBox.className = "onValidStyle";
        errorLabelRequired.style.display = "none";
        errorLabelInvalid.style.display = "none";
    }
    else {
        if (!inputIsOptional && (boxInput == "" || boxInput == watermarkText)) {
            errorLabelRequired.style.display = "inline";
            errorLabelInvalid.style.display = "none";
            inputBox.className = "onInvalidStyle";
        }
        else if (inputIsOptional && (boxInput == "" || boxInput == watermarkText)) {
            inputBox.className = "onEmptyStyle";
            errorLabelRequired.style.display = "none";
            errorLabelInvalid.style.display = "none";
        }
        else {
            errorLabelInvalid.style.display = "inline";
            errorLabelRequired.style.display = "none";
            inputBox.className = "onInvalidStyle";
        }
    }
}

function displayWatermarkOnBlur(inputBox, waterMarkText, boxIsMasked, maskCharacter) {
    var boxInput = inputBox.value.trim();

    if (boxIsMasked) {
        boxInput = inputBox.value.replace(new RegExp(maskCharacter, 'g'), '').trim();
    }

    if (boxInput == "" || boxInput == waterMarkText) {
        inputBox.value = waterMarkText;
        inputBox.style.color = "#666666";
        inputBox.style['fontStyle'] = "italic";
    }
}

function clearWatermarkAndSetStyleOnFocus(inputBox, waterMarkText, boxIsMasked, maskCharacter) {
    var boxInput = inputBox.value.trim();

    if (boxIsMasked) {
        boxInput = inputBox.value.replace(new RegExp(maskCharacter, 'g'), '').trim();
    }

    if (boxInput == waterMarkText) {
        inputBox.value = "";
    }
    inputBox.style.color = "black";
    inputBox.style['fontStyle'] = "normal";
}
function ChangeColor(tableRow, highLight) {
    if (highLight) {
    //<%= Url.Content ("~/Scripts/jquery-1.2.6.js") %>
        //tableRow.style.backgroundImage = 'url(Images/gradient-e6eeff-80px.png)';
        //tableRow.style.backgroundImage = 'url(/SteadfastIHF/Images/gradient-e6eeff-80px.png)';
        tableRow.style.backgroundImage = 'url(https://www.baycitymotors.net/Images/gradient-e6eeff-80px.png)';
        tableRow.style.cursor = 'pointer';
    }
    else {
        tableRow.style.backgroundColor = 'white';
        tableRow.style.backgroundImage = 'none';
    }
}

function ChangeVehicleStatNotClickedRowColorRed(tableRow, highLight) {
    if (highLight) {
        tableRow.style.backgroundImage = 'url(https://www.baycitymotors.net/Images/gradient-cddfed-40px.png)';
        tableRow.style.cursor = 'pointer';
    }
    else {
        tableRow.style.backgroundImage = 'url(https://www.baycitymotors.net/Images/gradient-ff724c-40px.png)';        
    }
}

function ChangeVehicleStatNotClickedRowColorYellow(tableRow, highLight) {
    if (highLight) {
        tableRow.style.backgroundImage = 'url(https://www.baycitymotors.net/Images/gradient-cddfed-40px.png)';
        tableRow.style.cursor = 'pointer';
    }
    else {
        tableRow.style.backgroundImage = 'url(https://www.baycitymotors.net/Images/gradient-ecde04-40px.png)';        
    }
}

function ChangeColorCustomerPage(tableRow, highLight) {
    if (highLight) {
    //<%= Url.Content ("~/Scripts/jquery-1.2.6.js") %>
        //tableRow.style.backgroundImage = 'url(Images/gradient-e6eeff-80px.png)';
        //tableRow.style.backgroundImage = 'url(/SteadfastIHF/Images/gradient-e6eeff-80px.png)';
        tableRow.style.backgroundImage = 'url(https://www.baycitymotors.net/Images/gradient-98aac0-efefef-98aac0-80px.png)';
        tableRow.style.cursor = 'pointer';
    }
    else {
        tableRow.style.backgroundColor = '#efefef';
        tableRow.style.backgroundImage = 'none';
    }
}

function ChangeColorLarge(tableRow, highLight) {
    if (highLight) {
        tableRow.style.backgroundImage = 'url(Images/gradient-e6eeff-248px.png)';
        tableRow.style.cursor = 'pointer';
    }
    else {
        tableRow.style.backgroundColor = 'white';
        tableRow.style.backgroundImage = 'none';
    }
}

function DoClick(theButton) {
    theButton.click();
    //            document.location.href = theUrl;
}

function OpenWindow(theHyperLink) {
    var thisLink = theHyperLink.getAttribute("href");
    window.open(thisLink);
}

function ScrollToElement(theElement) {
    var selectedPosX = 0;
    var selectedPosY = 0;

    while (theElement != null) {
        selectedPosX += theElement.offsetLeft;
        selectedPosY += theElement.offsetTop;
        theElement = theElement.offsetParent;
    }

    window.scrollTo(selectedPosX, selectedPosY);

}

function showProgressBar(theProgressBar) {

}
        
function GetURLParameter(name)
{  
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
    var regexS = "[\\?&]"+name+"=([^&#]*)";  
    var regex = new RegExp( regexS );  
    var results = regex.exec( window.location.href );  
    if (results == null)    
        return "";  
    else    
        return results[1].replace(/%20/g," ");
}
