var arOutputKeys = new Array();
var arOutputValues = new Array();
var bGetValues = false;
var bTypeAhead = false;
var bLocalTypeAhead = false;

function processGetPost() {
	var myajax=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
		
	if (myajax.readyState == 4) { //if request of file completed
		if (myajax.status==200 || window.location.href.indexOf("http")==-1) { //if request was successful or running script locally
			if (myfiletype=="txt") {
				handleSearchSuggest(myajax.responseText);
			}
			else {
				alert(myajax.responseXML)
			}
		}
	}
}

function handleSearchSuggest(sStrIn) {
	eval(sStrIn);
	if (bGetValues) {
		oTextbox.provider.names = arOutputValues;
		oTextbox.provider.requestSuggestions(oTextbox, bLocalTypeAhead);
	}
}

function getPostParameters(sValue){
	var poststr = "sSearch=" + encodeURI(sValue)
	return poststr
}
	
function ForenameSuggestions() {
	this.names = [];
}

ForenameSuggestions.prototype.requestSuggestions = function(oAutoSuggestControl /*:AutoSuggestControl*/,
                                                          bTypeAhead /*:boolean*/) {
    var aSuggestions = [];
    var sTextboxValue = oAutoSuggestControl.textbox.value;

    if (sTextboxValue.length > 0) {

        //search for matching names

        for (var i = 0; i < this.names.length; i++) {
            if (this.names[i].toLowerCase().indexOf(sTextboxValue.toLowerCase()) == 0) {
                aSuggestions.push(this.names[i]);

            }
        }
    }
    //provide suggestions to the control
    oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);
};

ForenameSuggestions.prototype.populateSuggestions = function(oAutoSuggestControl /*:AutoSuggestControl*/,
																	bAsynch /*:boolean*/) {
    oTextbox = oAutoSuggestControl;
    bGetValues = true;

    var sInput = oAutoSuggestControl.textbox;

    if (sInput.value.length < 2) {
        bGetValues = false;
    }

    if (bGetValues == true) {

        //window.status = bAsynch
        var sValue = sInput.value
        if (sValue.indexOf(' - ') > 0) {
            sValue = sValue.substr(0, sValue.indexOf(' - '));
        }
        ajaxpack.getAjaxRequest('/Suggest.ashx?WCI=SuggestForenames&member_key=0&', getPostParameters(sValue), processGetPost, 'txt', null, bAsynch);
    }
}

	
function SurnameSuggestions() {
	this.names = [];
}

SurnameSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/,
                                                          bTypeAhead /*:boolean*/) {
    var aSuggestions = [];
    var sTextboxValue = oAutoSuggestControl.textbox.value;
	
    if (sTextboxValue.length > 0){
    
        //search for matching names

        for (var i=0; i < this.names.length; i++) { 
            if (this.names[i].toLowerCase().indexOf(sTextboxValue.toLowerCase()) == 0) { 
                aSuggestions.push(this.names[i]);
				
            }
		}
    }

    //provide suggestions to the control
    oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);
};

SurnameSuggestions.prototype.populateSuggestions = function(oAutoSuggestControl /*:AutoSuggestControl*/,
																	bAsynch /*:boolean*/) {
    oTextbox = oAutoSuggestControl;
    bGetValues = true;

    var sInput = oAutoSuggestControl.textbox;

    if (sInput.value.length < 2) {
        bGetValues = false;
    }

    if (bGetValues == true) {

        //window.status = bAsynch
        var sValue = sInput.value
        if (sValue.indexOf(' - ') > 0) {
            sValue = sValue.substr(0, sValue.indexOf(' - '));
        }
        ajaxpack.getAjaxRequest('/Suggest.ashx?WCI=SuggestSurnames&member_key=0&', getPostParameters(sValue), processGetPost, 'txt', null, bAsynch);
    }
}