function onChangeFromState() {
    
    var stateList = document.getElementById('from_state');
    var stateCode = stateList.value;
    if (stateCode != "") {
        var i = 0;
        var optionList = document.getElementById('from_loc');
        /* clear the options except the 1st option*/
        for (i=optionList.length-1; i>0; i--) {
            optionList.options[i] = null;
        }
    	
	    /* call to AJAX */
	    busschedule.SS_FilterArea(stateCode, onChangeFromState_CallBack);
    }
	return false;
}

function onChangeFromState_CallBack(response){

    var ds = response.value;
    var i = 0;
    if (response.error != null){
        alert(response.error);
        return;
    }
    var areaList = document.getElementById('from_loc');
    if ((ds.Tables != null) && (ds.Tables[0].Rows.length > 0)) {
        for (i=0; i<ds.Tables[0].Rows.length; i++) {
            areaList.options[areaList.length] = new Option(ds.Tables[0].Rows[i]["area_name"], ds.Tables[0].Rows[i]["area_code"]);
        }
    } else {
        areaList.options[areaList.length] = new Option('Error loading areas', '');
    }
}

function onChangeToState() {
    
    var stateList = document.getElementById('to_state');
    var stateCode = stateList.value;
    if (stateCode != "") {
        var i = 0;
        var optionList = document.getElementById('to_loc');
        /* clear the options except the 1st option*/
        for (i=optionList.length-1; i>0; i--) {
            optionList.options[i] = null;
        }
    	
	    /* call to AJAX */
	    busschedule.SS_FilterArea(stateCode, onChangeToState_CallBack);
    }
	return false;
}

function onChangeToState_CallBack(response){

    var ds = response.value;
    var i = 0;
    if (response.error != null){
        alert(response.error);
        return;
    }
    var areaList = document.getElementById('to_loc');
    if ((ds.Tables != null) && (ds.Tables[0].Rows.length > 0)) {
        for (i=0; i<ds.Tables[0].Rows.length; i++) {
            areaList.options[areaList.length] = new Option(ds.Tables[0].Rows[i]["area_name"], ds.Tables[0].Rows[i]["area_code"]);
        }
    } else {
        areaList.options[areaList.length] = new Option('Error loading areas', '');
    }
}
