
   function populateTerritory()
   {
    var Service = document.getElementById("ddService");
    Territory = document.getElementById("ddRegion");
    if(Service.options[Service.selectedIndex].value != 0)
    //Check if the selectedItem is not "--Select--"
    {
     return SendRequest(Service.options[Service.selectedIndex].value);
    }
    else
    {
    clearSelect(Territory);//Clear the Territory dropdown
//    status.innerText = "";//Blank the status text label
    }
   }
   
   function InitializeRequest()
   {
    try
    {
     request = new ActiveXObject("Microsoft.XMLHTTP");
     //Try creating an XMLHTTP Object
    }
    catch(Ex)
    {
     try
     {
      //First failure, try again creating an XMLHTTP Object
      request = new ActiveXObject("Microsoft.XMLHTTP");
     }
     catch(Ex)
     {
        //Else assign null to request

    request = null;
     }
    }
    if(!request&&typeof XMLHttpRequest != 'undefined')
    {
     request = new XMLHttpRequest();
    }
   }
  
   function SendRequest(ID)
   {
   //status.innerText = "Loading.....";//Set the status to "Loading....."
    InitializeRequest();//Call InitializeRequest to set request object
    
    //Create the url to send the request to
    var url = "ServiceRegionAjaxServer.aspx?ID="+ID;

    //Delegate ProcessRequest to onreadystatechange property so 
    //it gets called for every change in readyState value
    request.onreadystatechange = ProcessRequest;

    request.open("GET", url, true);//Open a GET request to the URL
    request.send(null);//Send the request with a null body.
   }
   
   function ProcessRequest()
   {
    if(request.readyState == 4)
    //If the readyState is in the "Ready" state
    
    {
     if(request.status == 200)
     //If the returned status code was 200. 
     //Everything was OK.
     
     {
      if(request.responseText != "")
      //If responseText is not blank
      {
       //Call the populateList fucntion
       
       populateList(request.responseText);
       //Set the status to "Territories Loaded"
//       status.innerText = "Territories Loaded";
      }
      else
      {
       //Set the status to "None Found"
       //status.innerText = "None Found";
       //Call clearSelect function
       Territory = document.getElementById("ddRegion");
       clearSelect(Territory);
      }
     }
    }
    return true;//return
   }


function populateList(response)
   {
    //Create the XMLDOM object
    var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
    //Load the responseText into the XMLDOM document
    xmlDoc.loadXML(response);
alert(response.value);
    var opt;
    //Create the ServiceTerritories element
    var TerritoriesElem = 
      xmlDoc.getElementsByTagName("ServiceTerritories"); 

    //Create the TPI_Region element
    var TerritoryElem = 
      TerritoriesElem[0].getElementsByTagName("TPI_Supplier_Offering");

	document.getElementById("ddRegion");
    clearSelect(Territory);
    //Clear the dropdown before filling it with new values


    if(TerritoriesElem.length > 0)
    //If there are one or more TERRITORIES nodes
    {
    for (var i = 0; i < TerritoryElem.length; i++)
    //Loop through the XML TERRITORIES nodes
    {
     //Create a TextNode
     var textNode = document.createTextNode(
           TerritoryElem[i].getAttribute("Region"));
     //Call appendToSelect to append the text 
     //elements to the Territory dropdown
     appendToSelect(Territory, 
       TerritoryElem[i].getAttribute("RegionID"), textNode);
    }
    }
   }
   
   function appendToSelect(select, value, content)
   {
    var opt;
    //Create an Element of type option
    opt = document.createElement("option");
    //Set the option's value
    opt.value = value;
        
    //Attach the text content to the option
    opt.appendChild(content);
    //Append the option to the referenced [Territory] select box
    select.appendChild(opt);
    
   }
   
    function clearSelect(select)
   {
    //Set the select box's length to 1 
    //so only "--Select--" is available in the selection on calling this function.
    select.options.length = 1;
    //You may want to write your own clearSelect logic
   }
   
   function isFirstPicked(ddList)
	{
	alert("here");
	if (document.formname.ddList.selectedIndex == 0)
	{
	return false;
	}
	}
	
	function setSelectedIndex()
	{
		//ddRegion2 = document.getElementById("ddRegion2");
		//alert(ddRegion2.selectedIndex);
		//ddRegion2.selectedIndex = ddRegion2.selectedIndex;
		//alert(ddRegion2.selectedIndex);
	}
	

    function MyApp(sender)
    {
		var lbMatch = false;
        var loDDL2 = document.tpi.ddRegion;
        for(var i=0; i< loDDL2.length-1; i++)
        {
            lbMatch = sender.value==loDDL2.options[i].value;        
            lsSelected = lbMatch ? "<=SELECTED" : "";
            if(lbMatch)
                loDDL2.selectedIndex = sender.selectedIndex;
            
        }
    }

	function redirectURL()
	{
	var ddRegion = document.tpi.ddRegion;
	var ddService = document.tpi.ddService;
	var svalue = ddRegion.selectedIndex;
	
	if (svalue == 3) {
		svalue = 2;
	}
	else if (svalue == 1) {
		svalue = 3;
	}
	else if (svalue == 2) {
		svalue = 1;
	}
	var URL = 'serviceproviderresults.aspx?R=' + svalue + '&O=' + ddService.selectedIndex;
	//alert(URL);
	//window.navigate(URL);
	location.href=URL;
	
	}
