
/*********************************************************************
'***    Function: adds autocomplete options to city field
'***
'***    Parameters: 
'***        fieldId [str]* - jQuery selector for city field
'***        cityData [obj]* - data which will be sent to remote script ( var CityData = { City: getCityFieldLastTermValue }; )
'***        cityListItems [obj]* - map for autocomplete list items with events ( var cityMap = {label: item.City + ", " + item.State, value: item.City + "," + item.State} )
'***        onSelect [func] - use custom function when item will be selected
'***
'***	Returns: autocomplete list depending on entered value
'***
'***    Remarks:
'***        usually used for input[type="text"] with autocomplete
'***        relies on getAutoCompleteOptions
'***        jQuery must be initialized already
'***
'***	Use example:
'***		addCityAutoComplete(CityFieldId, CityData, getListItemsForCity, onSelectCity);
'***
'***    Created by: sergeyh
'***    Changed by: 
'***    Last change: 11/18/2010
'*********************************************************************/
function addCityAutoComplete(fieldId, cityData, cityListItems, onSelect){
    var minLength = 3; // min chars to show autocomplete
    fieldId = (fieldId == null || fieldId == "") ? "#city" : fieldId;
    var url = "/Components/ZipCodeCityLookup.asp"; // url for getting data
    
    var options = getAutoCompleteOptions(url, cityData, cityListItems, minLength, onSelect);
    jQuery(fieldId).autocomplete(options);
}

/*********************************************************************
'***    Function: adds autocomplete options to zipCode field
'***
'***    Parameters: 
'***        fieldId [str]* - jQuery selector for zipCode field
'***        zipData [obj]* - data which will be sent to remote script ( var ZipData = { ZipCode: getZipFieldLastTermValue }; )
'***        zipCodeListItems [obj]* - map for autocomplete list items with events ( var zipMap = { label: item.City + ", " + item.State, value: item.City + "," + item.State } )
'***        onSelect [func] - use custom function when item will be selected
'***
'***	Returns: autocomplete list depending on entered value
'***
'***    Remarks:
'***        usually used for input[type="text"] with autocomplete
'***        relies on getAutoCompleteOptions
'***        jQuery must be initialized already
'***
'***	Use example:
'***		addZipAutoComplete(ZipCodeFieldId, ZipData, getListItemsForZipCode, onSelectZipCode);
'***
'***    Created by: sergeyh
'***    Changed by: 
'***    Last change: 11/18/2010
'*********************************************************************/
function addZipAutoComplete(fieldId, zipData, zipCodeListItems, onSelect){
    var minLength = 3; // min chars to show autocomplete
    fieldId = (fieldId == null || fieldId == "") ? "#zip1" : fieldId;
    var url = "/Components/ZipCodeCityLookup.asp"; // url for getting data
    
    var options = getAutoCompleteOptions(url, zipData, zipCodeListItems, minLength, onSelect);
    jQuery(fieldId).autocomplete(options);
}





/*********************************************************************
'***    Function: gets state field data
'***
'***    Parameters: 
'***        countryFieldId [str]* - jQuery selector for country field (this field must be single <select>)
'***        stateFieldCntId [str]* - jQuery selector for state field content (where ajax request result will be placed <li id="stateFieldCntId"></li>)
'***        stateFieldId [str]* - jQuery selector for state field (this field must be single <select>)
'***        selectedValue [str] - init state field with selected value
'***        onCompleteFunc [function] - use custom function on completed ajax request
'***        fnSetupAutoComplete [function] - additional customization for fields etc when function initialized
'***
'***	Returns: html
'***
'***    Remarks:
'***        used only after jQuery initialized
'***        relies on getCountryIDSelectedValue, getJQAjaxOptions
'***
'***	Use example:
'***		showStateFieldForAutoComplete(CountryFieldId, StateFieldCntId, StateFieldId, "<%=strJobLocations_StateCode%>", null, setupAutoCompleteForCountry);
'***
'***    Created by: sergeyh
'***    Changed by: 
'***    Last change: 11/22/2010
'*********************************************************************/
function showStateFieldForAutoComplete(countryFieldId, stateFieldCntId, stateFieldId, selectedValue, onCompleteFunc, fnSetupAutoComplete){
    // set default field ids
    countryFieldId = (countryFieldId == null || countryFieldId == "") ? "#cn" : countryFieldId
    stateFieldCntId = (stateFieldCntId == null || stateFieldCntId == "") ? "#StateFieldCnt" : stateFieldCntId;
    selectedValue = (selectedValue == null) ? "" : selectedValue;
    stateFieldId = (stateFieldId == null || stateFieldId == "") ? "#NewStateCode" : stateFieldId;
    
    var StateFieldId_DOM = stateFieldId.substr(1, stateFieldId.length);
    
    var countryId = getCountryIDSelectedValue(countryFieldId);
    
    // init events when country changed
    if(typeof(fnSetupAutoComplete) == "function"){
        fnSetupAutoComplete();
    }
    
    //build the url with necessary params
    var strUrl = "/Components/GetStateListBoxForCountry.asp";
    var strQS = "CountryID=" + countryId + "&StateValue=" + selectedValue + "&Name=" + StateFieldId_DOM;
    
    // use standard function if custom is not defined
    if(typeof(onCompleteFunc) != "function"){
        onCompleteFunc = function(XMLHttpRequest, textStatus){
	        var StateLabel = "<label for='" + StateFieldId_DOM + "'>State/Province</label>";
	        var StateHtml = jQuery(stateFieldCntId).html();
	        // add label to result
	        jQuery(stateFieldCntId).html(StateLabel + StateHtml);
	    }
    }
    
    //get state field html via ajax
    var options = getJQAjaxOptions(stateFieldCntId, strUrl, strQS, null, onCompleteFunc);
    jQuery.ajax(options);
}

/*********************************************************************
'***    Function: gets multi select state field data
'***
'***    Parameters: 
'***        countryFieldId [str]* - jQuery selector for country field (this field must be single <select>)
'***        stateFieldCntId [str]* - jQuery selector for state field content (where ajax request result will be placed <li id="stateFieldCntId"></li>)
'***        stateFieldId [str]* - jQuery selector for state field (this field must be single <select>)
'***        selectedValue [str] - init state field with selected value
'***        onCompleteFunc [function] - use custom function on completed ajax request
'***        fnSetupAutoComplete [function] - additional customization for fields etc when function initialized
'***
'***	Returns: html
'***
'***    Remarks:
'***        used only after jQuery initialized
'***        relies on getCountryIDSelectedValue, getJQAjaxOptions
'***        remember to redifine setupAutoCompleteForCountry as it will use standard state field id (e.g. function setupAutoCompleteForCountryLocal() { setupAutoCompleteForCountry(CountryFieldId, ZipCodeFieldId, CityFieldId, StateFieldId); }
'***
'***	Use example:
'***		showMultiSelectStateFieldForAutoComplete(CountryFieldId, StateFieldCntId, StateFieldId, "<%=strJobLocations_StateCode%>", null, setupAutoCompleteForCountry);
'***
'***    Created by: sergeyh
'***    Changed by: 
'***    Last change: 11/19/2010
'*********************************************************************/
function showMultiSelectStateFieldForAutoComplete(countryFieldId, stateFieldCntId, stateFieldId, selectedValue, onCompleteFunc, fnSetupAutoComplete){
    // set default field ids
    countryFieldId = (countryFieldId == null || countryFieldId == "") ? "#cn" : countryFieldId
    stateFieldCntId = (stateFieldCntId == null || stateFieldCntId == "") ? "#StateFieldCnt" : stateFieldCntId;
    selectedValue = (selectedValue == null) ? "" : selectedValue;
    stateFieldId = (stateFieldId == null || stateFieldId == "") ? "#st" : stateFieldId;
    
    var countryId = getCountryIDSelectedValue(countryFieldId);
    
    // init events when country changed
    if(typeof(fnSetupAutoComplete) == "function"){
        fnSetupAutoComplete();
    }
    
    //build the url with necessary params
    var strUrl = "/Components/GetStateMultiSelectListBoxForCountry.asp";
    var strQS = "CountryID=" + countryId + "&Sel=" + selectedValue;
    
    //get state field html via ajax
    var options = getJQAjaxOptions(stateFieldCntId, strUrl, strQS, null, onCompleteFunc);
    jQuery.ajax(options);
}


/*********************************************************************
'***    Function: inits autocomplete options for zipCode field
'***
'***    Parameters: 
'***        CountryFieldId [str]* - jQuery selector for country field (this field must be single <select>)
'***        ZipCodeFieldId [str]* - jQuery selector for zipCode field
'***        CityFieldId [str]* - jQuery selector for city field
'***        StateFieldId [str]* - jQuery selector for state field (this field must be single <select>)
'***
'***	Returns: object
'***
'***    Remarks:
'***        usually used for input[type="text"]
'***        jQuery must be initialized already
'***        relies on getLastTermValue, removeZipCodeOnBlurEvent
'***
'***	Use example:
'***		initZipCodeAutoComplete(CountryFieldId, ZipCodeFieldId, CityFieldId, StateFieldId);
'***
'***    Created by: sergeyh
'***    Changed by: 
'***    Last change: 11/19/2010
'*********************************************************************/
function initZipCodeAutoComplete(CountryFieldId, ZipCodeFieldId, CityFieldId, StateFieldId){
    CountryFieldId = (CountryFieldId == null || CountryFieldId == "") ? "#cn" : CountryFieldId;
    ZipCodeFieldId = (ZipCodeFieldId == null || ZipCodeFieldId == "") ? "#zip1" : ZipCodeFieldId;
    CityFieldId = (CityFieldId == null || CityFieldId == "") ? "#city" : CityFieldId;
    StateFieldId = (StateFieldId == null || StateFieldId == "") ? "#NewStateCode" : StateFieldId;
    
    // ****************** zip code autocomplete
    // last entered value
    var getZipFieldLastTermValue = function(){
        return getLastTermValue(ZipCodeFieldId);
    }
    // remote ajax data
    var ZipData = { ZipCode: getZipFieldLastTermValue };
    // map for the autocomplete items
    var getListItemsForZipCode = function (item){
        lastTerm = getZipFieldLastTermValue();
        
        var zipMap = {
		    label: item.City + ", " + item.State,
	        value: item.City + "," + item.State
        };
        
        if(lastTerm.length >= 4){
            zipMap = {
		        label: item.City + ", " + item.State + ", " + item.ZipCode,
		        value: item.City + "," + item.State + "," + item.ZipCode
            };
        }
        
        return zipMap;
    }
    // add selection function
    var onSelectZipCode = function(inputValue, itemValue){
        var arrValues = itemValue.split(",");
        
        var strCity = arrValues[0];
        var strState = arrValues[1];
        var strZipCode = (arrValues.length == 3) ? arrValues[2] : getZipFieldLastTermValue();
        
        jQuery(ZipCodeFieldId).val(strZipCode);
        // add city
        jQuery(CityFieldId).val(strCity);
        // select state
        jQuery("select" + StateFieldId + " [value='" + strState + "']").attr("selected", "selected");
        // remove blur to avoid changes again
        removeZipCodeOnBlurEvent(CountryFieldId, ZipCodeFieldId, CityFieldId, StateFieldId);
    }
    
    // init autocomplete
    addZipAutoComplete(ZipCodeFieldId, ZipData, getListItemsForZipCode, onSelectZipCode);
}

/*********************************************************************
'***    Function: inits autocomplete options for city field
'***
'***    Parameters: 
'***        CityFieldId [str]* - jQuery selector for city field
'***        StateFieldId [str]* - jQuery selector for state field (this field must be single <select>)
'***
'***	Returns: object
'***
'***    Remarks:
'***        usually used for input[type="text"]
'***        jQuery must be initialized already
'***        relies on getLastTermValue
'***
'***	Use example:
'***		initZipCodeAutoComplete(ZipCodeFieldId, CityFieldId, StateFieldId);
'***
'***    Created by: sergeyh
'***    Changed by: 
'***    Last change: 11/18/2010
'*********************************************************************/
function initCityAutoComplete(CityFieldId, StateFieldId){
    CityFieldId = (CityFieldId == null || CityFieldId == "") ? "#city" : CityFieldId;
    StateFieldId = (StateFieldId == null || StateFieldId == "") ? "#NewStateCode" : StateFieldId;
    
    // ****************** city autocomplete
    // last entered value
    var getCityFieldLastTermValue = function(){
        return getLastTermValue(CityFieldId);
    }
    // remote ajax data
    var CityData = { City: getCityFieldLastTermValue };
    
    // map for the autocomplete items
    var getListItemsForCity = function (item){
        var cityMap = {
            label: item.City + ", " + item.State,
            value: item.City + "," + item.State
        }
        return cityMap;
    }
    // add selection function
    var onSelectCity = function(inputValue, itemValue){
        var arrValues = itemValue.split(",");
        
        var strCity = arrValues[0];
        var strState = arrValues[1];
        
        jQuery(CityFieldId).val(strCity);
        // remove selections if states listbox is multiselect
        jQuery("select" + StateFieldId + " option:selected").each(function(){
            jQuery(this).attr("selected", "");
        });
        // select state
        jQuery("select" + StateFieldId + " [value='" + strState + "']").attr("selected", "selected");
    }
    
    // init autocomplete
    addCityAutoComplete(CityFieldId, CityData, getListItemsForCity, onSelectCity);
}



/*********************************************************************
'***    Function: inits fields events for autocomplete
'***
'***    Parameters: 
'***        CountryFieldId [str]* - jQuery selector for country field (this field must be single <select>)
'***        ZipCodeFieldId [str]* - jQuery selector for zipCode field
'***        CityFieldId [str]* - jQuery selector for city field
'***        StateFieldId [str]* - jQuery selector for state field (this field must be single <select>)
'***
'***	Returns: void
'***
'***    Remarks:
'***        used only after jQuery initialized
'***        relies on getCountryIDSelectedValue, allowAutoCompleteForCountry, removeZipCodeOnBlurEvent, initZipCodeAutoComplete, initCityAutoComplete
'***
'***	Use example:
'***		setupAutoCompleteForCountry();
'***
'***    Created by: sergeyh
'***    Changed by: 
'***    Last change: 11/19/2010
'*********************************************************************/
function setupAutoCompleteForCountry(CountryFieldId, ZipCodeFieldId, CityFieldId, StateFieldId) {
    CountryFieldId = (CountryFieldId == null || CountryFieldId == "") ? "#cn" : CountryFieldId;
    ZipCodeFieldId = (ZipCodeFieldId == null || ZipCodeFieldId == "") ? "#zip1" : ZipCodeFieldId;
    CityFieldId = (CityFieldId == null || CityFieldId == "") ? "#city" : CityFieldId;
    StateFieldId = (StateFieldId == null || StateFieldId == "") ? "#NewStateCode" : StateFieldId;
    
    var countryId = getCountryIDSelectedValue(CountryFieldId);
    var blnValidCountryForAutoComplete = allowAutoCompleteForCountry(countryId);
    
    // do not allow trips if selected country is not Canada or USA
    if(!blnValidCountryForAutoComplete){
        // remove autocomplete
        jQuery(ZipCodeFieldId).autocomplete("destroy");
        jQuery(CityFieldId).autocomplete("destroy");
        // remove zip code autofill data
        removeZipCodeOnBlurEvent(CountryFieldId, ZipCodeFieldId, CityFieldId, StateFieldId);
        
        return;
    }
    else {
        // add autocomplete
        initZipCodeAutoComplete(CountryFieldId, ZipCodeFieldId, CityFieldId, StateFieldId);
        initCityAutoComplete(CityFieldId, StateFieldId);
        // add zip code autofill data
        addZipCodeOnBlurEvent(ZipCodeFieldId, CityFieldId, StateFieldId);
        
        return;
    }
}











/*********************************************************************
'***    Function: populates fields data (city and state) if full (and valid) zipCode entered when field loses focus
'***
'***    Parameters: 
'***        CityFieldId [str]* - jQuery selector for city field
'***        StateFieldId [str]* - jQuery selector for state field (this field must be single <select>)
'***
'***	Returns: string
'***
'***    Remarks:
'***        usually used for input[type="text"]
'***        jQuery must be initialized already
'***        relies on getLastTermValue, parsePostalCode (SearchJobs.js), jsonResponseHasError (ValidationLib.js)
'***
'***	Use example:
'***		initZipCodeBlurEvent(ZipCodeFieldId, CityFieldId, StateFieldId);
'***
'***    Created by: sergeyh
'***    Changed by: 
'***    Last change: 11/18/2010
'*********************************************************************/
function initZipCodeBlurEvent(ZipCodeFieldId, CityFieldId, StateFieldId){
    ZipCodeFieldId = (ZipCodeFieldId == null || ZipCodeFieldId == "") ? "#zip1" : ZipCodeFieldId;
    CityFieldId = (CityFieldId == null || CityFieldId == "") ? "#city" : CityFieldId;
    StateFieldId = (StateFieldId == null || StateFieldId == "") ? "#NewStateCode" : StateFieldId;
    
    var lastTerm = getLastTermValue(ZipCodeFieldId);
    var url = "/Components/ZipCodeCityLookup.asp"; // url for getting data
    
    // do not get data if not min 5 chars entered
    if(lastTerm.length < 5){
        return;
    }
    
    if(parsePostalCode(lastTerm) == ""){
        // invalid postal code
        return;
    }
    
    // remote ajax data
    var ZipData = { ZipCode: lastTerm };
    
    var populateFields = function(jsonData){
        //if an error response was returned
        if(jsonResponseHasError(jsonData)){
            return;
        }
        
        //get json object records array
        var arrJSON = jsonData.Response.Records;
        // do not populate fields if number of records more than 3
        if(arrJSON.length > 3 || arrJSON.length == 0){
            return;
        }
        
        var strCity = arrJSON[0].City;
        var strState = arrJSON[0].State;
        // populate city
        jQuery(CityFieldId).val(strCity);
        // select state
        jQuery("select" + StateFieldId + " option[value='" + strState + "']").attr("selected", "selected");
        // remove autocomplete options, cause user already moved cursor and focus (do not overlay other fields)
        jQuery(ZipCodeFieldId).autocomplete("close");
    }
    
    jQuery.getJSON(url, ZipData, populateFields);
}

/*********************************************************************
'***    Function: removes change event from zipCode field and adds blur event.
'***            After initZipCodeBlurEvent was called - autocomplete list will be closed.
'***
'***    Parameters: 
'***        ZipCodeFieldId [str]* - jQuery selector for zipCode field
'***        CityFieldId [str]* - jQuery selector for city field
'***        StateFieldId [str]* - jQuery selector for state field (this field must be single <select>)
'***
'***	Returns: void
'***
'***    Remarks:
'***        used only after jQuery initialized
'***        relies on initZipCodeBlurEvent
'***
'***	Use example:
'***		addZipCodeOnBlurEvent(ZipCodeFieldId, CityFieldId, StateFieldId);
'***
'***    Created by: sergeyh
'***    Changed by: 
'***    Last change: 11/18/2010
'*********************************************************************/
function addZipCodeOnBlurEvent(ZipCodeFieldId, CityFieldId, StateFieldId){
    ZipCodeFieldId = (ZipCodeFieldId == null || ZipCodeFieldId == "") ? "#zip1" : ZipCodeFieldId;
    CityFieldId = (CityFieldId == null || CityFieldId == "") ? "#city" : CityFieldId;
    StateFieldId = (StateFieldId == null || StateFieldId == "") ? "#NewStateCode" : StateFieldId;
    
    jQuery(ZipCodeFieldId).unbind("change");
    jQuery(ZipCodeFieldId).bind("blur", function() {
        initZipCodeBlurEvent(ZipCodeFieldId, CityFieldId, StateFieldId);
        // remove autocomplete options, cause user already moved cursor and focus (do not overlay other fields)
        setTimeout(function () {
            jQuery(ZipCodeFieldId).autocomplete("close");
        }, 500);
    });
}

/*********************************************************************
'***    Function: removes blur event from zipCode field depending on selected country and adds change event for future use autocomplete
'***
'***    Parameters: 
'***        CountryFieldId [str]* - jQuery selector for country field (this field must be single <select>)
'***        ZipCodeFieldId [str]* - jQuery selector for zipCode field
'***        CityFieldId [str]* - jQuery selector for city field
'***        StateFieldId [str]* - jQuery selector for state field (this field must be single <select>)
'***
'***	Returns: void
'***
'***    Remarks:
'***        used only after jQuery initialized
'***        relies on getCountryIDSelectedValue, allowAutoCompleteForCountry, initZipCodeBlurEvent
'***
'***	Use example:
'***		removeZipCodeOnBlurEvent(CountryFieldId, ZipCodeFieldId, CityFieldId, StateFieldId);
'***
'***    Created by: sergeyh
'***    Changed by: 
'***    Last change: 11/18/2010
'*********************************************************************/
function removeZipCodeOnBlurEvent(CountryFieldId, ZipCodeFieldId, CityFieldId, StateFieldId){
    CountryFieldId = (CountryFieldId == null || CountryFieldId == "") ? "#cn" : CountryFieldId;
    ZipCodeFieldId = (ZipCodeFieldId == null || ZipCodeFieldId == "") ? "#zip1" : ZipCodeFieldId;
    CityFieldId = (CityFieldId == null || CityFieldId == "") ? "#city" : CityFieldId;
    StateFieldId = (StateFieldId == null || StateFieldId == "") ? "#NewStateCode" : StateFieldId;
    
    jQuery(ZipCodeFieldId).unbind("blur");
    
    var countryId = getCountryIDSelectedValue(CountryFieldId);
    var blnValidCountryForAutoComplete = allowAutoCompleteForCountry(countryId);
    if(blnValidCountryForAutoComplete){
        // if content changed - add autocomplete again
        // do not add change event if different country is selected
        jQuery(ZipCodeFieldId).bind("change", function() {
            initZipCodeBlurEvent(ZipCodeFieldId, CityFieldId, StateFieldId);
        });
    }
    else {
        jQuery(ZipCodeFieldId).unbind("change");
    }
    
}






/*********************************************************************
'***    Function: returns options for autocomplete list
'***
'***    Parameters: 
'***        url [str]* - path to remote script
'***        fieldData [obj]* - data which will be sent to remote script ( var ZipData = { ZipCode: getZipFieldLastTermValue }; )
'***        fieldMap [obj]* - map for autocomplete list items with events ( var zipMap = { label: item.City + ", " + item.State, value: item.City + "," + item.State } )
'***        minLength [int] - minimum length of entered value when data will be get from remote script
'***        onSelect [func] - use custom function when item will be selected
'***
'***	Returns: object
'***
'***    Remarks:
'***        usually used for input[type="text"] with autocomplete
'***        jQuery must be initialized already
'***        relies on jsonResponseHasError (ValidationLib.js)
'***
'***	Use example:
'***		var options = getAutoCompleteOptions(url, zipData, zipCodeListItems, minLength, onSelect);
'***
'***    Created by: sergeyh
'***    Changed by: 
'***    Last change: 11/18/2010
'*********************************************************************/
function getAutoCompleteOptions(url, fieldData, fieldMap, minLength, onSelect, onSearch) {
    return {
        minLength: minLength,
        source: function(request, response) {
            var showAutoComplete = function(jsonData) {
                //if an error response was returned
                if (jsonData == null || jsonResponseHasError(jsonData)) {
                    return;
                }

                //get json object records array
                var arrJSON = jsonData.Response.Records;

                response(jQuery.map(arrJSON, fieldMap));
            }
            // get data
            jQuery.getJSON(url, fieldData, showAutoComplete);
        },
        search: function(event, ui) {
            //if argument is not a function, set it
            if (typeof (onSearch) != "function") {
                onSearch = function(inputValue) {
                    return (inputValue != "");
                }
            }
            
            var blnResult = onSearch(this.value);
            
            return blnResult;
        },
        focus: function() {
            return false; // prevent value inserted on focus
        },
        select: function(event, ui) {
            if (typeof (onSelect) == "function") {
                onSelect(this.value, ui.item.value);
            }
            else {
                this.value = ui.item.value;
            }
            return false;
        }
    }
}

/*********************************************************************
'***    Function: returns options for jQuery ajax request
'***
'***    Parameters: 
'***        fieldId [str]* - jQuery selector for result field
'***        strUrl [str]* - path to remote script
'***        strQS [str]* - query string which will be sent to remote script
'***        onSuccess [func] - use custom function when query data will be get
'***        onComplete [func] - use custom function when query data will be set
'***
'***	Returns: object
'***
'***    Remarks: none
'***
'***	Use example:
'***		var options = getJQAjaxOptions(stateFieldCntId, strUrl, strQS, null, onCompleteFunc);
'***
'***    Created by: sergeyh
'***    Changed by: 
'***    Last change: 11/18/2010
'*********************************************************************/
function getJQAjaxOptions(fieldId, strUrl, strQS, onSuccess, onComplete){
    if(fieldId == null || fieldId == "")
        return;
    if(typeof(onSuccess) != "function"){
        onSuccess = function(data, textStatus, XMLHttpRequest){
            jQuery(fieldId).html(data);
        }
    }
    if(typeof(onComplete) != "function"){
        onComplete = function(XMLHttpRequest, textStatus){
            return;
        }
    }
    return {
        type: "POST",
        url: strUrl,
        data: strQS,
        dataType: "html",
        success: onSuccess,
        complete: onComplete,
        error: function(XMLHttpRequest, textStatus, errorThrown) {
             jQuery(fieldId).text("unable to load this field...");
        }
    }
}

/*********************************************************************
'***    Function: validates countryId against countries for which autocmplete allowed
'***
'***    Parameters: 
'***        countryId [int]* - country id
'***
'***	Returns: boolean
'***
'***    Remarks: none
'***
'***	Use example:
'***		var blnValidCountryForAutoComplete = allowAutoCompleteForCountry(countryId);
'***
'***    Created by: sergeyh
'***    Changed by: 
'***    Last change: 11/18/2010
'*********************************************************************/
function allowAutoCompleteForCountry(countryId){
    countryId = parseInt(countryId);
    
    var countryId_USA = 1;
    var countryId_CANADA = 2;
    
    if(countryId == countryId_USA || countryId == countryId_CANADA){
        return true;
    }
    
    return false;
}

/*********************************************************************
'***    Function: returns selected country id
'***
'***    Parameters: 
'***        CountryFieldId [str]* - jQuery selector for country field (this field must be single <select>)
'***
'***	Returns: integer
'***
'***    Remarks:
'***        used only after jQuery initialized
'***
'***	Use example:
'***		var countryId = getCountryIDSelectedValue(countryFieldId);
'***
'***    Created by: sergeyh
'***    Changed by: 
'***    Last change: 11/18/2010
'*********************************************************************/
function getCountryIDSelectedValue(CountryFieldId){
    CountryFieldId = (CountryFieldId == null || CountryFieldId == "") ? "#cn" : CountryFieldId;
    var countryId = jQuery("select" + CountryFieldId + " option:selected").val();
    
    countryId = parseInt(countryId);
    if(isNaN(countryId) || countryId < 0)
        countryId = 1; // default United States
    
    return countryId;
}

// get last entered value from obect
function getLastTermValue(fieldId){
    return jQuery(fieldId).val();
}
