
/**
 * Provides suggestions for state names (USA).
 * @class
 * @scope public
 */
function RemoteStateSuggestions() {

    if (typeof XMLHttpRequest != "undefined") {
        this.http = new XMLHttpRequest();
    } else if (typeof ActiveXObject != "undefined") {
        this.http = new ActiveXObject("MSXML2.XmlHttp");
    } else {
        alert("No XMLHttpRequest object available. This functionality will not work.");
    }

}

/**
 * Request suggestions for the given autosuggest control.
 * @scope protected
 * @param oAutoSuggestControl The autosuggest control to provide suggestions for.
 */
RemoteStateSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/,
                                                          bTypeAhead /*:boolean*/) {
    var oHttp = this.http;
    var strTextboxValue = oAutoSuggestControl.textbox.value;
    var iLastComma = strTextboxValue.lastIndexOf( "," );
		var strNextTag = strTextboxValue;
		var reSeparator = /---------/;


		var oRegExp = new RegExp( "_edit" );
		var iArtifactId = oAutoSuggestControl.textbox.id.replace( oRegExp, "" );

		if ( iLastComma != -1 )
		{
			strNextTag = strTextboxValue.substring( iLastComma + 1, strTextboxValue.length );
		}
		strNextTag = strNextTag.replace( /^ /, "" );

    if ( strNextTag == "" )
    {
    	oAutoSuggestControl.hideSuggestions();
   		return;
   	}

		//if there is already a live request, cancel it
    if (oHttp.readyState != 0) {
        oHttp.abort();
    }

    //build the URL
    var sURL = "as_remote_suggestions.php?user_input=" + encodeURIComponent( strNextTag ) + "&artifact_id=" + iArtifactId;

    oHttp.open("get", sURL , true);
    oHttp.onreadystatechange = function ()
		{
        if ( oHttp.readyState == 4)
				{
          //evaluate the returned text JavaScript (an array)
						var strResponse = oHttp.responseText;
						if ( strResponse.length == 0 )
						{
							strResponse = "[]";
						}
						var aSuggestions = eval(strResponse);

						// Get the existing tags as an array
						var strExistingTags = oAutoSuggestControl.textbox.value;
						var aExistingTags = new Array();
						while ( true )
						{
							var idxComma = strExistingTags.indexOf( "," );
							if ( idxComma == -1 )
								break;

							var this_tag = strExistingTags.substring( 0, idxComma );
							strExistingTags = strExistingTags.substring( idxComma+1 );

							this_tag = this_tag.replace( /^ /, "" );
							this_tag = this_tag.replace( / $/, "" );
							aExistingTags[aExistingTags.length] = this_tag;
						}

						// check to make sure that none of the tags in the textbox are
						// being added as suggestions
						var aValidSuggestions = new Array();
						for ( var x=0; x<aSuggestions.length; x++ )
						{
							var bValid = true;
							for ( var y=0; y<aExistingTags.length; y++ )
							{
								if ( aSuggestions[ x ].indexOf( aExistingTags[ y ] ) != -1 )
								{
									bValid = false;
									break;
								}
							}
							if ( bValid )
							{
								if ( aSuggestions[ x ].search( reSeparator ) != -1 )
								{
									if ( 0 != aValidSuggestions.length && x != aSuggestions.length-1 )
									{
										aValidSuggestions[aValidSuggestions.length] = aSuggestions[ x ];
									}
								}
								else
								{
									aValidSuggestions[aValidSuggestions.length] = aSuggestions[ x ];
								}
							}
						}

            //provide only valid suggestions to the control
            oAutoSuggestControl.autosuggest(aValidSuggestions, bTypeAhead);
        }
    };
    oHttp.send(null);
};