// Orign Dest Namespace
var ODNS = new function() {
    // Private
    var serverUrl = '';
    var currDestCountry = '';
    var currDest = '';
    var companyId = '';
    var addAny = true;
    var origDestCB = function() {return};
    var rateInfoCB = function() {return};
    // Public
    return {
        _processCitiesResponse:function(originalRequest){
            eval(originalRequest.responseText);
            $('dest').innerHTML = '';
            if (result.length > 1 && addAny){
              var anyOpt = $('dest').appendChild(document.createElement('option'));
              anyOpt.text = 'Any City';
              anyOpt.value = '@any@'; 
            }
            result.each(function(city){
                var newOpt = $('dest').appendChild(document.createElement('option'));
                newOpt.text = city;
                newOpt.value = city;
            });
            try {
                origDestCB();
            } catch(e) {
            }
        },
        _processResponse: function(originalRequest){
            var destCountryIndex = -1;
            var destIndex = -1;
            //alert(originalRequest.responseText);
            eval(originalRequest.responseText);
            $('destCountry').innerHTML = '';
            if (countries.length > 1 && addAny){
              var anyOpt = $('destCountry').appendChild(document.createElement('option'));
              anyOpt.text = 'Any Country';
              anyOpt.value = '@any@'; 
            }
            for(i=0; i<countries.length; i++){
                var newOpt = $('destCountry').appendChild(document.createElement('option'));
                newOpt.text = countries[i];
                newOpt.value = countries[i];
                if (countries[i] == currDestCountry){
                    destCountryIndex = i;
                }
            }
        
            $('dest').innerHTML = '';
            if (cities.length > 1 && addAny){
              var anyOpt = $('dest').appendChild(document.createElement('option'));
              anyOpt.text = 'Any City';
              anyOpt.value = '@any@'; 
            }
            for(i=0; i<cities.length; i++){
                var newOpt = $('dest').appendChild(document.createElement('option'));
                newOpt.text = cities[i];
                newOpt.value = cities[i];
                if (cities[i] == currDest){
                    destIndex = i;
                }
            }
            if (destCountryIndex != -1){
                try {
                    var ind = destCountryIndex;
                    if (addAny){
                        ind += 1;
                    }  
                    $('destCountry').options[ind].selected = "selected";
                } catch(e){ 
                }
            }
            if (destIndex != -1){
                try {
                    var ind = destIndex;
                    if (addAny){
                        ind += 1;
                    }  
                    $('dest').options[ind].selected = "selected";
                } catch(e){ 
                }
            }
            try {
                origDestCB();
            } catch(e) {
            }
        },
        _processRateInfo: function(originalRequest){
            try {
                rateInfoCB(originalRequest);
            } catch(e) {
            }
        },
        loadByOrigin: function(){
            byFirstCountry = 0;
            if (currDestCountry == '' && !addAny){
                byFirstCountry = 1;
            }
            var pars = 'proc=getDest&companyId='+ companyId +'&origin=' + $F('origin')+ '&destCountry=' + currDestCountry + '&byFirstCountry=' + byFirstCountry;
            var myAjax = new Ajax.Request(serverUrl, { method: 'get', parameters: pars, onComplete: this._processResponse });
            
        }, 
        loadByDestCountry:function(){
            currDestCountry = $F('destCountry');
            var pars = 'proc=getDestCities&companyId='+ companyId +'&destCountry=' + $F('destCountry') + '&origin=' + $F('origin');
            var myAjax = new Ajax.Request(serverUrl, { method: 'get', parameters: pars, onComplete: this._processCitiesResponse });
        },
        loadRateInfo: function(){
            var pars = 'proc=getRateInfo&companyId='+ companyId +'&destCountry=' + $F('destCountry') +'&dest=' + $F('dest') + '&origin=' + $F('origin');
            var myAjax = new Ajax.Request(serverUrl, { method: 'get', parameters: pars, onComplete: this._processRateInfo });
        },
        setCurrDest: function(cd){
            currDest = cd;
        }, 
        setServerUrl: function(url){
            serverUrl = url;
        },
        setCompanyId: function(cid){
            companyId = cid;
        },
        setAddAny: function(aa){
            addAny = aa;
        },
        setOrigDestCallBack: function(cb){
            origDestCB = cb;
        },
        setRateInfoCallBack: function(cb){
            rateInfoCB = cb;
        }
    };
}






