// AJAX call to the weekly finance payments

/* Wrapper function for constructing a request object. Parameters:
  reqType: The HTTP request type, such as GET or POST. (in capitals)
  url: The URL of the server program.
  asynch: Whether to send the request asynchronously or not. (true or false) 
  e.g. httpRequest("GET","test.php",true);  */

function httpRequest(reqType,url,asynch){
    //Mozilla-based browsers
    if(window.XMLHttpRequest){
        request = new XMLHttpRequest(  );
    } else if (window.ActiveXObject){
        request=new ActiveXObject("Msxml2.XMLHTTP");
        if (! request){
            request=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    //the request could still be null if neither ActiveXObject
    //initialization succeeded
    if(request){
        initReq(reqType,url,asynch);
    } else {
        alert("Your browser does not permit the use of all of this application's features!");
    }
}

/* Initialize a request object that is already constructed */
function initReq(reqType,url,bool){
    /* Specify the function that will handle the HTTP response */
    request.onreadystatechange=handleResponse; 
    request.open(reqType,url,bool);
    request.send(null);
}

function handleResponse( ){
    if(request.readyState == 4){
        if(request.status == 200){
            $('city').options.length = 0;   /* remove existing options  */
            var serverresult = request.responseText; /* Grab the result as a string */
            var returnElements=serverresult.split("|")
             for ( var i=0; i< returnElements.length; i++ ){
                 valueLabelPair = returnElements[i].split("^")
                 $('city').options[i] = new Option(valueLabelPair[1], valueLabelPair[0]);
              }    
        } else {
            alert("A problem occurred with communicating between " + "the XMLHttpRequest object and the server program.");
        }
    }//end outer if
    //return serverresult;
}
function get_cities(data_path, region_id){

    var cities_url = data_path + "region2cities/?rgn=" + region_id;
//alert(cities_url);
    httpRequest("GET",cities_url,true);
}