function RedrawRegion(array) {
    try {
        var row;
        var orginalName;
        ClearListBox(regionDropDownList);
        ClearListBox(countryDropDownList);
        ClearListBox(makeDropDownList);
        RegionMaps(regionDropDownList, mapDiv);

        for (row = 0; row < array.length; ++row) {
            addOption(regionDropDownList, array[row][0], array[row][1]);
        }
        regionDropDownList.selectedIndex = -1;
        GoGreen(regionSelectorGo, false);
    }
    catch (err) {
        // do nothing
    }
}


function RedrawCountry(array, productClassId, regionId) {
    try {
        var row;
        var orginalName;
        ClearListBox(countryDropDownList);
        ClearListBox(makeDropDownList);
        RegionMaps(regionDropDownList, mapDiv);


        for (row = 0; row < array.length; ++row) {
            if (array[row][0] == productClassId && array[row][2] == regionId) {
                if (orginalName != array[row][5]) {
                    addOption(countryDropDownList, array[row][5], array[row][4]);
                }
                orginalName = array[row][5];
            }
        }
        countryDropDownList.selectedIndex = -1;
        GoGreen(regionSelectorGo, false);
    }
    catch (err) {

        // do nothing
    }
}

function RedrawMake(array, productClassId, regionId, countryId) {
    try {
        //alert("debug: " + isThisMake + " | " + hasPrefixInUrl);
        var row;
        ClearListBox(makeDropDownList);
        GoGreen(regionSelectorGo, false);

        for (row = 0; row < array.length; ++row) {
            if (array[row][0] == productClassId && array[row][2] == regionId && array[row][4] == countryId) {
                if (hasPrefixInUrl == "True") {
                    if (isThisMake == array[row][7]) {
                        addOption(makeDropDownList, array[row][7], array[row][6]);
                        //hasPrefixInUrl = "False";
                        break;
                    }
                }
                else if (hasPrefixInUrl == "False") {
                    addOption(makeDropDownList, array[row][7], array[row][6]);
                }
            }
        }
        if (makeDropDownList.length == 1) {
            makeDropDownList.selectedIndex = 0;
            GoGreen(regionSelectorGo, true);
        }
    }
    catch (err) {
        // do nothing
    }
}
function GoGreen(item, goGreen) {
    if (regionSelectorGo == null) {
        return;
    }

    if (goGreen == true) {
        regionSelectorGo.style.background = 'url(commonimages/gfxGoGreen.gif) no-repeat';
        regionSelectorGo.disabled = false;
    }
    else {
        item.style.background = 'url(commonimages/gfxGoGreenDisabled.gif) no-repeat';
        item.disabled = true;
    }
}
function RegionMaps(regionDropDownList, div) {

    if (div == null) {
        return;
    }

    if (regionDropDownList.selectedIndex >= 0) {
        regionName = "Region" + regionDropDownList.options[regionDropDownList.selectedIndex].value;
        regionAlt = regionDropDownList.options[regionDropDownList.selectedIndex].text;
        gRegionIs = regionName;
        regionNameNew = regionName;
        regionNameNew = regionNameNew.replace(" ", "");
        regionNameNew = regionNameNew.replace("&", "");
        regionNameNew = regionNameNew.replace("/", "");
        regionNameNew = regionNameNew.replace(" ", "");
        regionNameNew = regionNameNew.replace(" ", "");
        //div.style.backgroundImage = "/CommonImages/LexusCar.jpg";
        div.innerHTML = '<img src=\"CommonImages/Maps/' + regionNameNew + '.jpg\" alt=\"' + regionAlt + '\" />';
    }
}
function ClearListBox(selectbox) {
    if (selectbox.options) {
        var num = selectbox.options.length;
        var i = 0;
        while (num > i) {
            selectbox.options[0] = null;
            i++;
        }
    }
}
function addOption(selectbox, text, value) {
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    //selectbox.options.add(optn);
    selectbox.options[selectbox.length] = optn;

}
function andGo(navigationName, rememberCheckBoxId) {

    if (regionDropDownList.selectedIndex == -1 || countryDropDownList.selectedIndex == -1 || makeDropDownList.selectedIndex == -1) {
        alert("Please select Country and Manufacturer!");
    }
    else {
        try {
            var element = document.getElementById(rememberCheckBoxId);
            if (element != null) {
                createCookie("RememberArea", element.checked, 1);
            }
            createCookie("RegionId", regionDropDownList.value, 1);
            createCookie("CountryId", countryDropDownList.value, 1);
            createCookie("MakeId", makeDropDownList.value, 1);
        } catch (e) { }

        areaId = regionDropDownList.value + "X" + countryDropDownList.value + "X" + makeDropDownList.value;

        //alert("go to " + areaId);
        document.location = "Default.aspx?Page=" + navigationName + "&area=" + areaId;
        //alert("Default.aspx?Page=" + navigationName + "&area=" + areaId);
    }
}

function Init() {

    RedrawRegion(rsaRegion);

    cookieChecker();
}
function PageLoad() {

    try {
        try {
            RegionId = readCookie("RegionId");
            CountryId = readCookie("CountryId");
            MakeId = readCookie("MakeId");

        } catch (e) { 

	}
   
	if (RegionId == null){
		RegionId = 4;
	}

	
        
        if (RegionId) {
            selectFromCookie(regionDropDownList, RegionId);
            RegionMaps(regionDropDownList, mapDiv);
            if (regionDropDownList.options.length > 0) {
                if (regionDropDownList.selectedIndex >= 0) {
                    RedrawCountry(rsa, productClassId, regionDropDownList.options[regionDropDownList.selectedIndex].value);                  
                }
            }
        }

	if(countryDropDownList.options.length == 0){
		RegionId = 4;
            selectFromCookie(regionDropDownList, RegionId);
            RegionMaps(regionDropDownList, mapDiv);
            if (regionDropDownList.options.length > 0) {
                if (regionDropDownList.selectedIndex >= 0) {
                    RedrawCountry(rsa, productClassId, regionDropDownList.options[regionDropDownList.selectedIndex].value);                  
                }
            }


	}

        if (CountryId) {
            selectFromCookie(countryDropDownList, CountryId);
            if (countryDropDownList.options.length > 0) {
                if (countryDropDownList.selectedIndex >= 0) {
                    RedrawMake(rsa, productClassId, regionDropDownList.options[regionDropDownList.selectedIndex].value, countryDropDownList.options[countryDropDownList.selectedIndex].value);
                }
            }
        }
        else {
           // ClearListBox(countryDropDownList);
        }

        if (MakeId) {
            selectFromCookie(makeDropDownList, MakeId);
            GoGreen(regionSelectorGo, true);
        }
        else {
          //  ClearListBox(makeDropDownList);
        }

        try {
            RememberArea = readCookie("RememberArea");
        } catch (e) {
            RememberArea = false;
        }

        rememberCheckBox.checked = RememberArea.toString().toLowerCase() == 'true';       
    }
    catch (err) {
        // do nothing
    }
}
function selectFromCookie(obj, id) {
    for (i = 0; i < obj.length; i++) {
        if (obj.options[i].value == id) {
            obj.options[i].selected = true;
        }
    }
}