function mmInitialize(parent, make, model, make_caption, model_caption, bnew)
{
    mmInitializeEx(parent, make, model, make_caption, model_caption, bnew, false);
}


function mmInitializeEx(parent, make, model, make_caption, model_caption, bnew, buse_short_name)

{
    clearList(parent.mmMakes);
    
    addElement(parent.mmMakes, make_caption, 0);
    
    if (bnew == 'true')
	{
        for (var i = 0; i < mmMkN.length; i++)
        {
            if (mmMkN[i])
			{
                addElement(parent.mmMakes, mmMkN[i], mmMkN[i]);
            }
        }
    }
    else
	{
        for (var i = 0; i < mmMkU.length; i++)
            if (mmMkU[i])
            {
                addElement(parent.mmMakes, mmMkU[i], mmMkU[i]);
			}
    }

    if (make)
    {
        setDefaultByText(parent.mmMakes, make);
        
        if (model_caption != "none")
            mmChangeMakeEx(parent, bnew, model_caption, buse_short_name);
        
        if (model)
            setDefaultByText(parent.mmModels, model);
	}
    else
	{
        parent.mmMakes.selectedIndex = 0;
        mmChangeMakeEx(parent, bnew, model_caption, buse_short_name);
    }
}


function mmChangeMake(parent, bnew, model_caption)
{
    mmChangeMakeEx(parent, bnew, model_caption, false);
}

/**
 *  AWong. Sample code from http://developer.irt.org/script/822.htm
 */
function replace(string,text,by) {
//  Replaces text with by in string
     var strLength = string.length, txtLength = text.length;
     if ((strLength == 0) || (txtLength == 0)) return string;

     var i = string.indexOf(text);
     if ((!i) && (text != string.substring(0,txtLength))) return string;
     if (i == -1) return string;

     var newstr = string.substring(0,i) + by;

     if (i+txtLength < strLength)
         newstr += replace(string.substring(i+txtLength,strLength),text,by);

     return newstr;
 }

function mmRedirect(parent)
{
    //alert("mmModels.value=" + parent.mmModels.options[parent.mmModels.selectedIndex].value);
    //alert("Link=" + location.protocol + "//" + location.host + "/solver/" + parent.mmModels.options[parent.mmModels.selectedIndex].value);   
    //alert(replace(parent.mmModels.options[parent.mmModels.selectedIndex].value,' ',''));
    newLocation = replace(parent.mmModels.options[parent.mmModels.selectedIndex].value,' ','').toLowerCase() + ".html";
    
    //Redirect to the solver page
    location.href = location.protocol + "//" + location.host + "/solver/" + newLocation;   
}

function mmChangeMakeEx(parent, bnew, model_caption, buse_short_name)
{
    if (bnew == 'true')
        var ModelList = mmMdN[parent.mmMakes.options[parent.mmMakes.selectedIndex].value];
    else
        var ModelList = mmMdU[parent.mmMakes.options[parent.mmMakes.selectedIndex].value];

    clearList(parent.mmModels);
    addElement(parent.mmModels, model_caption, 0);
    if(ModelList)
    {
        var rgModels = ModelList.split(',');
        for (var i = 0; i < rgModels.length; i++)
        {
            var sName, sValue;
            // rgModels[i] may be a singular value of ModelAscii, or a "#" delimited string of ModelID and ModelAscii
			if (rgModels[i].indexOf('#') != -1)
			{
				var rgModel = rgModels[i].split('#');
				sName = rgModel[1];
				sValue = rgModel[0];
			}
			else
				sName = sValue = rgModels[i];
            if (rgModels[i])
            {
                //alert("sName=" + sName);
                //alert("sValue=" + sValue);
                addElement(parent.mmModels, (buse_short_name == true ? MakeShortName(sName) : sName), sValue);
            }
        }
        parent.mmModels.disabled = false;
    }
    else
        parent.mmModels.disabled = true;

    parent.mmModels.selectedIndex = 0;
} 

// In order for model names to fit within the 130px-wide LeftNav control we need to modify
// some of the model names so that they fit.  Any name "tweaks" here must go through 
// editorial...
function MakeShortName(sName)
{
    switch(sName)
    {
        case "3-Series Sport Wagon":
            return("3-Series Sprt Wgn");
        case "5-Series Sport Wagon":
            return("5-Series Sprt Wgn");
        case "Passat Wagon (2001.5)":
            return("Passat Wgn (01.5)");
        case "Caprice Classic Wagon":
            return("Caprice Clssic Wgn");
        case "Power Ram 50 Sport Cab":
            return("Pwr Ram 50 Sport");
        case "Explorer  Sport Trac":
            return("Explorer Sport Trac");
        case "LTD Crown Victoria Wagon":
            return("LTD Crown Vic Wgn");
        case "Grand Marquis Wagon":
            return("Grand Marquis Wgn");
        case "Cutlass Ciera Wagon":
            return("Cutlass Ciera Wgn");
        default:
            return(sName);    
    }
}

