
function FillLocations1() {
    
    
    $("#LOCATIONS1 option").remove();
    var selectLOCATIONS1 = $("#LOCATIONS1");

    id1 = $("#COUNTRIES1 :selected").val();
    
    $.ajax({
        type: "GET",
        url: "/js/Countries.xml",
        dataType: "xml",
        success: function(xml) {

            $(xml).find('Country').each(function() {
                var countryID = $(this).find('CountryID').text();
                
                if (id1 == countryID) {


                    $(this).find('Location').each(function() {
                        
                        var locationID = $(this).find('LocationID').text();
                        var locationName = $(this).find('LocationName').text();

                        selectLOCATIONS1.append("<option value='" + locationID + "'>" + locationName + "</option>");
                    });
                }
            });
        }
    });

}

function FillLocations2() {

    $("#LOCATIONS2 option").remove();
    var selectLOCATIONS2 = $("#LOCATIONS2");

    id2 = $("#COUNTRIES2 :selected").val();

    if (id2 == '') {
    
        selectLOCATIONS2.append("<option value=''>Same as pick up</option>");
    }
    else {

        $.ajax({
            type: "GET",
            url: "/js/Countries.xml",
            dataType: "xml",
            success: function(xml) {

                $(xml).find('Country').each(function() {
                    var countryID = $(this).find('CountryID').text();

                    if (id2 == countryID) {


                        $(this).find('Location').each(function() {

                            var locationID = $(this).find('LocationID').text();
                            var locationName = $(this).find('LocationName').text();

                            selectLOCATIONS2.append("<option value='" + locationID + "'>" + locationName + "</option>");
                        });
                    }
                });
            }
        });
    }

}


$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "/js/Countries.xml",
        dataType: "xml",
        success: function(xml) {

            var selectCountries1 = $("#COUNTRIES1");
            var selectCountries2 = $("#COUNTRIES2");

            selectCountries2.append("<option value=''>Same as pick up</option>");
            
            $(xml).find('Country').each(function() {

                var countryID = $(this).find('CountryID').text();
                var countryName = $(this).find('CountryName').text();

                selectCountries1.append("<option value='" + countryID + "'>" + countryName + "</option>");
                selectCountries2.append("<option value='" + countryID + "'>" + countryName + "</option>");
            });

            
			SetValues();
        }
    });
});

function SetValues()
{
	country = QueryString("Country");
	
	if (country != "")
	{
		$("#COUNTRIES1 option[value="+ country +"]").attr("selected","true");
		$("#COUNTRIES2 option[value="+ country +"]").attr("selected","true");
	}
	
	FillLocations1();
    FillLocations2();
}

function QueryString(parameter)
{
	try
	{
		var query = window.location;
		
		url =  String(query).split('?');
		parms = url[1].split('&');
		
		
		for(i = 0; i < parms.length; i++)
		{
			
			parm = parms[i].split("=");
			
			
			if(parm[0] == parameter) 
				return parm[1]
		}
		return "";
	}
	catch(err)
	{
		return "";
	}
}