//
// AWW: Does this really need to be obfuscated ?
//

function initParm() {
    bInit = false;
    ddlMa = gEId('make'); ddlMo = gEId('model'); ddlVT = gEId('type');
    ddlZR = gEId('zipRadius');
    var pL = gEId('priceL'); var pR = gEId('priceR');
    var mL = gEId('mileageL'); var mR = gEId('mileageR');
    var yL = gEId('yearL'); var yR = gEId('yearR');
    var payL = gEId('paymentL');var payR = gEId('paymentR');
    if (ddlMa && ddlMo && ddlVT) {
        strNU = gEId('hdnNewUsed').value;
        //Fill makes
        lDdl(ddlMa, arrMakes, 0);
        ddlMa_oc(); ddlMo_oc();
        if (ddlMo.length == 0) { lDdl(ddlMo, arrEmpty, 0); disMo(true); }
        if (pL) pL.selectedIndex = 0;
        if (pR) pR.selectedIndex = pR.length - 1;
        if (mL) mL.selectedIndex = 0;
        if (mR) mR.selectedIndex = mR.length - 1;
        if (yL) yL.selectedIndex = 0;
        if (yR) yR.selectedIndex = yR.length - 1;
        if (payL) payL.selectedIndex = 0;
        if (payR) payR.selectedIndex = payR.length - 1;

        selMake = gEIdN('selMake');
        selModel = gEIdN('selModel');
        selType = gEIdN('selType');
        selPriceR = gEIdN('selPriceR');
        selPriceL = gEIdN('selPriceL');
        hdnMake = gEIdN('hdnMake');
        hdnModel = gEIdN('hdnModel');
        hdnType = gEIdN('hdnType');

        if (selMake != null) {
            sDdlv(ddlMa, selMake);
            ddlMa_oc();
        }
        else if (hdnMake != null) {
            sDdlt(ddlMa, hdnMake);
            ddlMa_oc();
        }
        if (selType != null) {
            sDdlv(ddlVT, selType);
            ddlVT_oc();
        }
        else if (hdnType != null) {
            sDdlt(ddlVT, hdnType);
            ddlVT_oc();
        }
        if (selModel != null) {
            sDdlv(ddlMo, selModel);
        }
        else if (hdnModel != null) {
            sDdlt(ddlMo, hdnModel);
        }
        if (selPriceR != null) {
            sDdlv(pR, selPriceR);
        }
        if (selPriceL != null) {
            sDdlv(pL, selPriceL);
        }
    }
    if (ddlZR) {
        selRadius = gEIdN('selRadius');
        if (selRadius != null) {
            sDdlv(ddlZR, selRadius);
        }
    }
    
    if (pL && pR) ls_oc(pL.id, pR.id, p, false);
    if (mL && mR) ls_oc(mL.id, mR.id, m, false);
    if (yL && yR) ls_oc(yL.id, yR.id, y, true);
    if (payL && payR) ls_oc(payL.id, payR.id, pArray, false);
    bInit = true;
}

//Drop down left-right coordination
//l=Left Dropdown id
//r=Right Dropdown id
//ra=first character used to evaulate against a specific array
//y->true=adjust for last item = max
function ls_oc(l, r, ra, y) {
    var l = gEId(l); var r = gEId(r);
    var lSi; var cur; var j = 0;
    lSi = si(l) * 2;
    if (si(r) != null && si(r) != -1) cur = sv(r);
    r.options.length = 0;
    for (i = lSi; i < ra.length; i += 2) { aDdl(r, j, ra[i + 1], ra[i]); j++; }
    if(!y)aDdl(r, j, 'No Max', 0);
    if (cur) { for (i = 0; i < r.options.length; i++) { if (r.options[i].value == cur) r.selectedIndex = i; } };
    if (!cur || sv(l) > cur) r.selectedIndex = r.length - 1;
}

//Specialized helper function to load into dropdown and compair bitmask
function lDdl(s, a, b) {
    var cur = null; var j = 0;
    if (si(s) != null && si(s) != -1) cur = st(s);
    s.options.length = 0;
    aDdl(s, 0, 'Show me all', '#0');
    for (i = 0; i < a.length; i += 2) {
        var p = a[i].substr(0, 1);
        if (p == '#' || p == 'b' || strNU == 'b' || p == strNU)
        { if (b & prs(a[i]) || prs(a[i]) == 0 || b == 0) { aDdl(s, j + 1, a[i + 1], a[i]); j++; } }
    }
    if (cur) for (i = 0; i < s.options.length; i++) if (s.options[i].text == cur) s.selectedIndex = i;
}
///////////////////////////////////////////////////////////////////
//General Helper functions
//Get element
function gEId(s) { return document.getElementById(s); }
//Get element if it exists, otherwise return null
function gEIdN(s) {
    rv = null;
    if (gEId(s)) {
        rv = gEId(s).value;
    }
    return rv;
}

//Get selected index
function si(s) { return s.selectedIndex; }
//Get selected text
function st(s) { return s.options[si(s)].text; }
//Get selected value
function sv(s) { return s.options[si(s)].value; }

//Add a new option to a dropdown list
function aDdl(s, i, t, v) { s.options[i == null ? s.options.length : i] = new Option(t, v); }
//load the select
function ls(s, a, j) { for (i = 0; i < a.length; i += 2) { aDdl(s, j, a[i + 1], a[i]); j++; } }
//set selected option by text
function sDdlt(d, t) {
    for (i = 0; i < d.length; i++) {
        if (d.options[i].text == t) {
            d.selectedIndex = i;
        }
    }
}

//set selected option by value
function sDdlv(d, v) {
    for (i = 0; i < d.length; i++) {
        if (d.options[i].value == v) {
            d.selectedIndex = i;
        }
    }
}

///////////////////////////////////////////////////////////////////
//Specialized helper functions

//Check string for make abbreviation
function hasMake(v, m) { for (l = 0; l < v.length; l += 2) if (v.substr(l, 2) == m) return true; return false; }
//Disable Model and set it to all
function disMo(b) { if (b) ddlMo.selectedIndex = 0; ddlMo.disabled = b; }
//Parse value
function prs(h) { var i = h.indexOf('#'); return parseInt(h.substr(i + 1)); }

//Loop array and add bitmask to get all bits from the array
function calcMask(a) {
    var mask = 0;
    for (i = 0; i < a.length; i += 2) {
        var p = a[i].substr(0, 1);
        if (p == '#' || p == 'd' || p == 'b' || strNU == 'b' || p == strNU)
            mask = (mask | prs(a[i]));
    }
    return mask;
}

// On Make Change
function ddlMa_oc() {
    var s = sv(ddlMa);
    if (s.substr(0, 1) != '#') {
        lDdl(ddlVT, arrTypes, calcMask(arrG[s.substr(3) - 1]));
        ddlVT_oc();
    }
    else { lDdl(ddlVT, arrTypes, 0); disMo(true); }
    //saving the last	
    /*
    if(bInit){gEId('last_make').value=s.substr(1,2);}
    if(sv(ddlNU)=='n'||gEId('last_state').value!='')Loc_click();
    else loadMkt(ddlMkt,arrMark);	
    */
}
// On Vehicle Type Change 
function ddlVT_oc() {
    var t = sv(ddlVT); var m = sv(ddlMa);
    if (m.substr(0, 1) != '#') lDdl(ddlMo, arrG[m.substr(3) - 1], prs(t));
    disMo(m.substr(0, 1) == '#' || m.charAt(0) == 'd');
    //saving the last
    //if(bInit){gEId('last_type').value=getcode(t);}
}
// On Model Change
function ddlMo_oc() {
    var s;
    if (si(ddlMo) != -1) s = sv(ddlMo); else return;
    var b = prs(s);
    if (si(ddlMo) == 0) {
        var ms = sv(ddlMa);
        if (ms.substr(0, 1) != '#') b = calcMask(arrG[ms.substr(3) - 1]);
    }
    //saving the last
    //if(bInit){gEId('last_model').value=getcode(s);}
    lDdl(ddlVT, arrTypes, b);
}

