﻿/********************************************************
DECLARATIONS
********************************************************/
function CarMakeModel() {
    this.Make = "";
    this.Model = "";
    this.Year = "";
}

var CMMList = new Array();
var newCMM;

function sortSelect(selId, bAsc) {
    var lb = document.getElementById(selId);
    arrTexts = new Array();

    for (i = 0; i < lb.length; i++) {
        arrTexts[i] = lb.options[i].text;
    }
    arrTexts.sort();
    if (bAsc) {
        for (i = 0; i < lb.length; i++) {
            lb.options[i].text = arrTexts[i];
            lb.options[i].value = arrTexts[i];
        }
    }
    else {
        for (i = 0; i < lb.length; i++) {
            lb.options[lb.length - 1 - i].text = arrTexts[i];
            lb.options[lb.length - 1 - i].value = arrTexts[i];
        }
    }
}

function insertOption(theSel, newText, newValue, newIndex) {
    if (theSel.length == 0) {
        var newOpt1 = new Option(newText, newValue);
        theSel.options[0] = newOpt1;
        theSel.selectedIndex = 0;
    } else if (theSel.selectedIndex != -1) {
        var selText = new Array();
        var selValues = new Array();
        var selIsSel = new Array();
        var newCount = -1;
        var newSelected = -1;
        var i;
        for (i = 0; i < theSel.length; i++) {
            newCount++;
            if (newCount == newIndex) {
                selText[newCount] = newText;
                selValues[newCount] = newValue;
                selIsSel[newCount] = true;
                newCount++;
                newSelected = newCount;
            }
            selText[newCount] = theSel.options[i].text;
            selValues[newCount] = theSel.options[i].value;
            selIsSel[newCount] = false;
        }
        for (i = 0; i <= newCount; i++) {
            var newOpt = new Option(selText[i], selValues[i]);
            theSel.options[i] = newOpt;
            theSel.options[i].selected = selIsSel[i];
        }
    }
}


function loadYear(selYearId) {
    selYear = document.getElementById(selYearId);
    for (i = 0; i < CMMList.length; i++) {
        bFound = false;
        for (j = 0; j < selYear.options.length; j++) {
            if (CMMList[i].Year == selYear.options[j].value) {
                bFound = true;
            }
        }
        if (!bFound) {
            selYear.options[selYear.length] = new Option(CMMList[i].Year, CMMList[i].Year);
        }
    }
    sortSelect(selYearId, false);

    insertOption(selYear, 'ALL YEARS', '', 0)
}

function loadMake(selMakeId) {
    selMake = document.getElementById(selMakeId);
    for (i = 0; i < CMMList.length; i++) {
        bFound = false;
        for (j = 0; j < selMake.options.length; j++) {
            if (CMMList[i].Make == selMake.options[j].value) {
                bFound = true;
            }
        }
        if (!bFound) {
            selMake.options[selMake.length] = new Option(CMMList[i].Make, CMMList[i].Make);
        }
    }

    sortSelect(selMakeId, true);
    insertOption(selMake, 'ALL MAKES', '', 0)
}

function loadModels(selModelId, makeName) {
    selModel = document.getElementById(selModelId);
    selModel.length = 0;

    for (i = 0; i < CMMList.length; i++) {
        if (CMMList[i].Make == makeName) {
            bFound = false;
            for (j = 0; j < selModel.options.length; j++) {
                if (CMMList[i].Model == selModel.options[j].value) {
                    bFound = true;
                }
            }
            if (!bFound) {
                selModel.options[selModel.length] = new Option(CMMList[i].Model, CMMList[i].Model);
            }
        }
    }

    sortSelect(selModelId, true);
    insertOption(selModel, 'ALL MODELS', '', 0)
}

function hideImage(imageObject) {
    imageObject.style.display = "none";
}

function showNoImage(imageObject) {
    imageObject.src = "http://secure.automotiveinventory.com/Images/noimage.gif";
}

function loadThumbnail(strVin) {
    var imgUrl;
    imgUrl = "http://images.automotiveinventory.com/";
    imgUrl = imgUrl + strVin.substring(0, 4) + "/";
    imgUrl = imgUrl + strVin + "_1.jpg";

    document.write("<img class='ImageWithBorder' src='" + imgUrl + "' height='75' onerror='showNoImage(this)' border='0' />");
}

function loadCertified(iCertified, iMake, lMake, lLogo, glMake, glLogo) {
    /*
    document.write( iCertified + "<br/>" );
    document.write( iMake + "<br/>" );
    document.write( lMake + "<br/>" );
    document.write( lLogo + "<br/>" );
    document.write( glMake + "<br/>" );
    document.write( glLogo + "<br/>" );
    */

    iMake = iMake.toUpperCase();
    lMake = lMake.toUpperCase();
    glMake = glMake.toUpperCase();

    var imgURL;
    imgURL = "";

    if (iCertified == "True") {
        if (lLogo == "1") {
            imgURL = "http://secure.automotiveinventory.com/Images/Certified_Logo/" + iMake + "_small.gif";
        }
        else {
            var makeList;
            makeList = lMake;
            if (makeList.length == 0) {
                makeList = glMake;
            }

            if (makeList.indexOf(iMake) > -1) {
                imgURL = "http://secure.automotiveinventory.com/Images/Certified_Logo/" + iMake + "_small.gif";
            }
            else {
                var logoFilter;
                logoFilter = lLogo;
                if (logoFilter.length == 0) {
                    logoFilter = glLogo
                }
                //document.write( "logoFilter=" + logoFilter + "<br/>")
                if (logoFilter == "0") {
                    imgURL = "http://secure.automotiveinventory.com/Images/cert_ribbon_small.gif";
                }
                else if (logoFilter == "1") {
                    imgURL = "http://secure.automotiveinventory.com/Images/Certified_Logo/" + iMake + "_small.gif";
                }
                else if (logoFilter == "2") {
                    imgURL = "http://secure.automotiveinventory.com/Images/inspected.gif' width='74";
                }
            }
        }

        if (imgURL != "") {
            document.write("<img src='" + imgURL + "' onerror='hideImage(this); '/>");
        }
    }
}

function unhighlight(invTable) {
    invTable.className = "borderBlack";

}

function highlight(invTable) {
    invTable.className = "borderRed";

}

function sortData(sortField) {
    var cSortField = document.getElementById("SortField");
    var cSortDirection = document.getElementById("SortDirection");

    if (sortField != cSortField.value) {
        // Different Field, Set Sort ASC for field
        cSortField.value = sortField;
        cSortDirection.value = "ASC";
    }
    else {
        // Same Field, Change Sort Direction
        if (cSortDirection.value == "ASC") {
            cSortDirection.value = "DESC";
        }
        else {
            cSortDirection.value = "ASC";
        }
    }

    document.getElementById("frmSearch").submit();
}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }

    //URL Rewritten
    var myURL;
    myURL = window.location.href;
    while (myURL.indexOf("\/") != -1) {
        myURL = myURL.substring(myURL.indexOf("\/") + 1);
    }

    vars = myURL.split("-");
    for (var i = 0; i < vars.length; i = i + 2) {
        if (vars[i] == variable) {
            return vars[i + 1];
        }
    }
    return '';
}

function showDetails(PageId, DealerInventoryId, VIN, DealerLinkId, DealerGroupLinkId) {
    window.location = "PageId-" + PageId + "-PageMode-Detail-DealerInventoryId-" + DealerInventoryId + "-VIN-" + VIN + "-DealerLinkId-" + DealerLinkId + "-DealerGroupLinkId-" + DealerGroupLinkId + "-Default.aspx";
//    document.getElementById("frmSearch").action = "Detail.aspx";
//    document.getElementById("DealerInventoryId").value = DealerInventoryId;
//    document.getElementById("frmSearch").submit()
}

function buildPager(TotalCount, PageSize, HTMLElement) {
    var pgHTML = "Page: ";
    var cPageNum = getQueryVariable("PageNum");
    var cPageSize = getQueryVariable("PageSize");
    var PageTensPlace;
    var pageUrl = location.href;
    var queryStringInitializer;
    
    var sortField=getEle("SortField").value;
    var sortDirection=getEle("SortDirection").value;
    var yearStart=getEle("YearStart").value;
    var yearEnd=getEle("YearEnd").value;
    var startPrice=getEle("StartPrice").value;
    var endPrice=getEle("EndPrice").value;
    var vehicleMake=getEle("Make").value;
    var vehicleModel=getEle("Model").value;

    var cSortField = getQueryVariable("SortField");
    var cSortDirection = getQueryVariable("SortDirection");
    var cYearStart = getQueryVariable("YearStart");
    var cYearEnd = getQueryVariable("YearEnd");
    var cStartPrice = getQueryVariable("StartPrice");
    var cEndPrice = getQueryVariable("EndPrice");
    var cVehicleMake = getQueryVariable("Make");
    var cVehicleModel = getQueryVariable("Model");
    
    if (pageUrl.indexOf("?") == -1)
        queryStringInitializer = "?";
    else
        queryStringInitializer = "&";
        
    PageTensPlace = Math.floor(cPageNum / 10);

    if (cPageNum >= 10) {
        if (cPageNum == '') {
            var tempUrl = pageUrl;
            tempUrl = tempUrl + queryStringInitializer + "PageNum=" + String((PageTensPlace - 1) * 10);
            tempUrl = tempUrl + "&PageSize=" + PageSize;
            tempUrl = tempUrl + "&SortField=" + sortField;
            tempUrl = tempUrl + "&SortDirection=" + sortDirection;
            tempUrl = tempUrl + "&YearStart=" + yearStart;
            tempUrl = tempUrl + "&YearEnd=" + yearEnd;
            tempUrl = tempUrl + "&StartPrice=" + startPrice;
            tempUrl = tempUrl + "&EndPrice=" + endPrice;
            tempUrl = tempUrl + "&Make=" + vehicleMake;
            tempUrl = tempUrl + "&Model=" + vehicleModel;
            pgHTML += " &nbsp <a class='l' href='" + tempUrl + "'>&lt;</a>";
        }
        else {
            var tempUrl = pageUrl;
            tempUrl = tempUrl.replace("PageNum=" + cPageNum, "PageNum=" + String((PageTensPlace - 1) * 10));
            tempUrl = tempUrl.replace("PageSize=" + cPageSize, "PageSize=" + PageSize);
            tempUrl = tempUrl.replace("SortField=" + cSortField, "SortField=" + sortField);
            tempUrl = tempUrl.replace("SortDirection=" + cSortDirection, "SortDirection=" + sortDirection);
            tempUrl = tempUrl.replace("YearStart=" + cYearStart, "YearStart=" + yearStart);
            tempUrl = tempUrl.replace("YearEnd=" + cYearEnd, "YearEnd=" + yearEnd);
            tempUrl = tempUrl.replace("StartPrice=" + cStartPrice, "StartPrice=" + startPrice);
            tempUrl = tempUrl.replace("EndPrice=" + cEndPrice, "EndPrice=" + endPrice);
            tempUrl = tempUrl.replace("Make=" + cVehicleMake, "Make=" + vehicleMake);
            tempUrl = tempUrl.replace("Model=" + cVehicleModel, "Model=" + vehicleModel);
            pgHTML += " &nbsp <a class='l' href='" + tempUrl + "'>&lt;</a>";
        }
    }

    i = PageTensPlace * 10;
    iToPage = i + 10;
    for (i + (PageTensPlace * 10); i < iToPage; i++) {
        if (cPageNum == '') {
            if (i < (TotalCount / PageSize)) {
                if (i == 0) {
                    pgHTML += " &nbsp " + (i + 1);
                }
                else {
                    var tempUrl = pageUrl;
                    tempUrl = tempUrl + queryStringInitializer + "PageNum=" + i;
                    tempUrl = tempUrl + "&PageSize=" + PageSize;
                    tempUrl = tempUrl + "&SortField=" + sortField;
                    tempUrl = tempUrl + "&SortDirection=" + sortDirection;
                    tempUrl = tempUrl + "&YearStart=" + yearStart;
                    tempUrl = tempUrl + "&YearEnd=" + yearEnd;
                    tempUrl = tempUrl + "&StartPrice=" + startPrice;
                    tempUrl = tempUrl + "&EndPrice=" + endPrice;
                    tempUrl = tempUrl + "&Make=" + vehicleMake;
                    tempUrl = tempUrl + "&Model=" + vehicleModel;
                    pgHTML += " &nbsp <a class='l' href='" + tempUrl  + "'>" + (i + 1) + "</a>";
                }
            }
        }
        else {
            if (i < (TotalCount / PageSize)) {
                if (i == cPageNum) {
                    pgHTML += " &nbsp " + (i + 1);
                }
                else {
                    var tempUrl = pageUrl;
                    tempUrl = tempUrl.replace("PageNum=" + cPageNum, "PageNum=" + i);
                    tempUrl = tempUrl.replace("PageSize=" + cPageSize, "PageSize=" + PageSize);
                    tempUrl = tempUrl.replace("SortField=" + cSortField, "SortField=" + sortField);
                    tempUrl = tempUrl.replace("SortDirection=" + cSortDirection, "SortDirection=" + sortDirection);
                    tempUrl = tempUrl.replace("YearStart=" + cYearStart, "YearStart=" + yearStart);
                    tempUrl = tempUrl.replace("YearEnd=" + cYearEnd, "YearEnd=" + yearEnd);
                    tempUrl = tempUrl.replace("StartPrice=" + cStartPrice, "StartPrice=" + startPrice);
                    tempUrl = tempUrl.replace("EndPrice=" + cEndPrice, "EndPrice=" + endPrice);
                    tempUrl = tempUrl.replace("Make=" + cVehicleMake, "Make=" + vehicleMake);
                    tempUrl = tempUrl.replace("Model=" + cVehicleModel, "Model=" + vehicleModel);
                    pgHTML += " &nbsp <a class='l' href='" + tempUrl + "'>" + (i + 1) + "</a>";
                }
            }
        }
    }

    if (i < (TotalCount / PageSize)) {
        if (cPageNum == '') {
            var tempUrl = pageUrl;
            tempUrl = tempUrl + queryStringInitializer + "PageNum=" + String(PageTensPlace * 10 + 10);
            tempUrl = tempUrl + "&PageSize=" + PageSize;
            tempUrl = tempUrl + "&SortField=" + sortField;
            tempUrl = tempUrl + "&SortDirection=" + sortDirection;
            tempUrl = tempUrl + "&YearStart=" + yearStart;
            tempUrl = tempUrl + "&YearEnd=" + yearEnd;
            tempUrl = tempUrl + "&StartPrice=" + startPrice;
            tempUrl = tempUrl + "&EndPrice=" + endPrice;
            tempUrl = tempUrl + "&Make=" + vehicleMake;
            tempUrl = tempUrl + "&Model=" + vehicleModel;
            pgHTML += " &nbsp <a class='l' href='" + tempUrl + "'>&gt;</a>";
        }
        else {
            var tempUrl = pageUrl;
            tempUrl = tempUrl.replace("PageNum=" + cPageNum, "PageNum=" + String(PageTensPlace * 10 + 10));
            tempUrl = tempUrl.replace("PageSize=" + cPageSize, "PageSize=" + PageSize);
            tempUrl = tempUrl.replace("SortField=" + cSortField, "SortField=" + sortField);
            tempUrl = tempUrl.replace("SortDirection=" + cSortDirection, "SortDirection=" + sortDirection);
            tempUrl = tempUrl.replace("YearStart=" + cYearStart, "YearStart=" + yearStart);
            tempUrl = tempUrl.replace("YearEnd=" + cYearEnd, "YearEnd=" + yearEnd);
            tempUrl = tempUrl.replace("StartPrice=" + cStartPrice, "StartPrice=" + startPrice);
            tempUrl = tempUrl.replace("EndPrice=" + cEndPrice, "EndPrice=" + endPrice);
            tempUrl = tempUrl.replace("Make=" + cVehicleMake, "Make=" + vehicleMake);
            tempUrl = tempUrl.replace("Model=" + cVehicleModel, "Model=" + vehicleModel);
            pgHTML += " &nbsp <a class='l' href='" + tempUrl + "'>&gt;</a>";
        }
    }

    document.getElementById(HTMLElement).innerHTML = pgHTML;
}
function submitForm(formIdToSubmit) {
    document.getElementById(formIdToSubmit).submit();
}

function setYearEndValue(valueToSet) {
    document.getElementById('YearEnd').value = valueToSet
}

function dsGetInt(iValue2Get) {
    //alert( "'" + iValue2Get + "'");
    if (iValue2Get == "") {
        return 0;
    }
    else {
        return parseInt(iValue2Get);
    }
}

function CommaFormatted(amount) {
    var delimiter = ","; // replace comma if desired
    var a = amount.split('.', 2)
    var d = a[1];
    var i = parseInt(a[0]);
    if (isNaN(i)) { return ''; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while (n.length > 3) {
        var nn = n.substr(n.length - 3);
        a.unshift(nn);
        n = n.substr(0, n.length - 3);
    }
    if (n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if (d.length < 1) { amount = n; }
    else { amount = n + '.' + d; }
    amount = minus + amount;
    return amount;
}

function CurrencyFormatted(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) { i = 0.00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += '.00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}
// end of function CurrencyFormatted()

function showHistory(vehVin) {
    var hTd = document.getElementById("hist" + vehVin);
    historyHTML = hTd.innerHTML;
    //historyHTML = historyHTML.toUpperCase();
    //historyHTML = historyHTML.replace( 'TARGET', 'REMOVEDATT' );
    //historyHTML = historyHTML.replace( '<A ', '<A target="_blank" ');
    historyHTML = historyHTML.replace("INSERT_VIN_HERE", vehVin);
    historyHTML = historyHTML.replace('height="42"', 'height="38"');
    hTd.innerHTML = historyHTML;
}

function getEle(el) {
    return document.getElementById(el);
}