//<![CDATA[

// Main function to get suggestions from database
function suggestMe(event){
	if(!validateSuggestMe())
		return false;

	var formCatalogStateSelect = new GetObject('formCatalogStateSelect');
	var formCatalogState = new GetObject('formCatalogState');
	if(formCatalogStateSelect.obj != null && formCatalogState.obj != null){
		formCatalogState.obj.value = formCatalogStateSelect.obj.options[formCatalogStateSelect.obj.selectedIndex].value;
	}

	var parameters = new Array('formCatalogSuggest', 'formCatalogState');
	ajaxRequestMethodExtended('catalog', 'suggestMe', parameters, suggestMeResponse, null, null, 'pobieranie...', 0);
}

// response on database results
function suggestMeResponse(){
	var response_saute = ajaxResponseMethod(null);
	var message = '';
	var suggestBoxText = '';
	var lookFor = '';
	var first_value = '';
	var positionFirst = -1;
	
	// get what we are looging for
	var suggestField = new GetObject('formCatalogSuggest');
	if(suggestField.obj != null)
		lookFor = suggestField.obj.value;
	else
		return;
	
	if(typeof(response_saute)=="undefined"){
		return;
	}
//	var messageBox = new GetObject('formSuggestBody');
//	messageBox.obj.innerHTML = response;
//	return;
	
	// if nothing to show
	if(response_saute == '-1'){
		suggestBoxText = '';
	}else{
		// split response into rows
		var response_records = response_saute.split('$$$');
		// how many records
		var response_length = response_records.length;
		
		// if empty
		if(response_length == 0){
			return;
		}
		
		// suggest box text
		suggestBoxText = '';
		
//		suggestBoxText += '<div class="suggestBoxBg">Podpowiedzi</div>';
		
		for(var i = 0; i < response_length; i++){
			if(response_records[i]){
				// split rows into fileds
				var record_fields = response_records[i].split('###');
				// get number of fields
				var record_length = record_fields.length;
				// empty record?
				if(record_length == 0){
					return;
				}
				// there must be 2 fields [`id` and `nazwa`]
				if(record_length > 2){
					return;
				}
				
				// look for in `nazwa`
				positionFirst = record_fields[1].search(eval('/'+lookFor+'/ig'));
				
				// values before exposing found letters
				first_value = record_fields[1];
				
				if( positionFirst >= 0 ){
					// expose in first value
					first_value = first_value.replace(eval('/'+lookFor+'/i'), '<b>'+lookFor+'</b>');
				}
				
				// add fields to suggest box
				suggestBoxText += '<a class="suggestRecord" href="javascript: addSuggestion(\'' + record_fields[1] + '\');" >' + first_value +'</a>';
				
				//
				//
				// @TODO:
				// + przewijanie zawartości (sprawdzić czy funkcjonalne)
				//
				//
			}
		}
		// Zamykanie okienka
		suggestBoxText += '<div class="suggestRecordPlain"><a href="javascript: hideSuggestionBox();" onclick="hideSuggestionBox();">zamknij</a></div>';
	}
		
		var suggestBox = new GetObject('formCatalogSuggestBody');
		if(suggestBox.obj != null){
			if(suggestBoxText == ''){
				suggestBox.obj.style.display = 'none';
				suggestBox.obj.style.height = '0';
			}else{
				suggestBox.obj.style.display = 'block';
//				if(response_length > 5){
//					suggestBox.obj.style.height = (5*16+14)+'px';
//					suggestBox.obj.style.overflow = 'hidden';
//				}else{
					suggestBox.obj.style.height = (response_length*16+14)+'px';
//				}
				var formCatalogStateSelectFrame = new GetObject('formCatalogStateSelectFrame');
				if(formCatalogStateSelectFrame.obj != null){
					formCatalogStateSelectFrame.styl.width = '156px';
					formCatalogStateSelectFrame.styl.height = '25px';
				}
			}
			suggestBox.obj.innerHTML = suggestBoxText;
		}
	
}

// add choosen suggestion to input field
function addSuggestion(suggestion){
	var suggestField = new GetObject('formCatalogSuggest');
	if(suggestField.obj != null)
		suggestField.obj.value = suggestion;
}

// hide suggestion box when clicked
function hideSuggestionBox(){
	var suggestBox = new GetObject('formCatalogSuggestBody');
	if(suggestBox.obj != null){
		suggestBox.obj.style.display = 'none';
		suggestBox.obj.style.height = '0';
		var formCatalogStateSelectFrame = new GetObject('formCatalogStateSelectFrame');
		if(formCatalogStateSelectFrame.obj != null){
			formCatalogStateSelectFrame.styl.width = '0';
			formCatalogStateSelectFrame.styl.height = '0';
		}
	}
}

// change first letter to capital
function firstCap(that) {
   return (that.substr(0, 1).toUpperCase() + that.substr(1));
}

// check if everything is ok with our form
function validateSuggestMe(){
	var suggestField = new GetObject('formCatalogSuggest');
	if(suggestField.obj != null){
		// cut value if exeeds 150 chars
		if(suggestField.obj.value.length > 150)
			suggestField.obj.value = suggestField.obj.value.substr(0,150);
		
		// min value
		if(suggestField.obj.value.length<1){
			// if length i less than 1 char - hide
			hideSuggestionBox();
			return false;
		}
			
		return true;
	}
	// if there is aproblem with gettng formSuggest field object - hide
	hideSuggestionBox();
	return false;
}
//]]>
