<!--

function searchHandler_init()
{	
	var oTemp;
	
	// Decode encoded strings from static data source
	// Listings
	for (var search_type in this.f)
	{
		for (var field in this.f[search_type])
		{
			if (this.f[search_type][field]['value'])
				this.f[search_type][field]['value'] = decodeURIComponent(this.f[search_type][field]['value']);

			if (isArray(d.f[search_type][field]))
			{
				this.f[search_type][field]['type'] = d.f[search_type][field]['type'];

				if (d.f[search_type][field]['subtype'])
					this.f[search_type][field]['subtype'] = d.f[search_type][field]['subtype'];

				if (d.f[search_type][field]['format'])
					this.f[search_type][field]['format'] = d.f[search_type][field]['format'];

				if (d.f[search_type][field]['default'])
					this.f[search_type][field]['default'] = decodeURIComponent(d.f[search_type][field]['default']);

				this.f[search_type][field]['listing'] = new Array();
				for (var k in d.f[search_type][field]['listing'])
				{
					if (isArray(d.f[search_type][field]['listing'][k]))
					{
						this.f[search_type][field]['listing'][k] = new Array();
						for (var k1 in d.f[search_type][field]['listing'][k])
							this.f[search_type][field]['listing'][k][k1] = decodeURIComponent(d.f[search_type][field]['listing'][k][k1]);
					}
					else
					{
						this.f[search_type][field]['listing'][k] = decodeURIComponent(d.f[search_type][field]['listing'][k]);
					}
				}
			}
		}
	}
	// Cats
	for (var cat_id in d.c)
		d.c[cat_id]['cat_name'] = decodeURIComponent(d.c[cat_id]['cat_name']);
	this.c = d.c;
	d = null;

	this.sPrefix = 'search_';
	this.bReset = true;
	this.bSubmitBlocked = false;

	this.oEvent = document.getElementById(this.sPrefix + 'evt');
	this.oData = document.getElementById(this.sPrefix + 'data');
	this.oForm = document.getElementById('fPostSearch');
	this.oCatId = document.getElementById('pob_cat_id');

	this.oSearchWaitPlace = document.getElementById('dSearchWaitPlace');
	this.oGenericSearchPlace = document.getElementById('dGenericSearchPlace');
	this.oDetailedSearchPlace = document.getElementById('dDetailedSearchPlace');
	this.oDetailedSearchType_Place = document.getElementById('dDetailedSearchType_Place');
	this.oSavedSearchesPlace = document.getElementById('dSavedSearchesPlace');
	this.oActionsPlace = document.getElementById('dActionsPlace');
	this.oSearchSimpleButtonsPlace = document.getElementById('dSearchSimpleButtonsPlace');

	oTemp = document.getElementById('dEmptyHTML'); 					this.sEmptyHTML = trim(oTemp.innerHTML);
	oTemp = document.getElementById('dGenericSearchHTML'); 			this.sGenericSearchHTML = trim(oTemp.innerHTML);
	oTemp = document.getElementById('dDetailedSearchHTML'); 		this.sDetailedSearchHTML = trim(oTemp.innerHTML);
	oTemp = document.getElementById('dDetailedSearchTypeCHTML'); 	this.sDetailedSearchTypeCHTML = trim(oTemp.innerHTML);
	oTemp = document.getElementById('dDetailedSearchTypeRHTML'); 	this.sDetailedSearchTypeRHTML = trim(oTemp.innerHTML);
	oTemp = document.getElementById('dSavedSearchesHTML'); 			this.sSavedSearchesHTML = trim(oTemp.innerHTML);
	oTemp = document.getElementById('dActionsHTML'); 				this.sActionsHTML = trim(oTemp.innerHTML);
	oTemp = document.getElementById('dSimpleButtonsHTML'); 			this.sSimpleButtonsHTML = trim(oTemp.innerHTML);
	
	this.sFG8 = this.oService.getCSSAttribute('sFG8', 'color');
	this.sFG7 = this.oService.getCSSAttribute('sFG7', 'color');
	this.oForm.onsubmit = 'return false;';
	
	this.sSearchType = null;

	// Saved searches
	this.doSaveSearch = searchHandler_doSaveSearch;
	this.doRestoreAndSearchSavedSearch = searchHandler_doRestoreAndSearchSavedSearch;
	this.doSearchSavedSearch = searchHandler_doSearchSavedSearch;
	this.doDeleteSavedSearch = searchHandler_doDeleteSavedSearch;

	this.parseFieldId = searchHandler_parseFieldId;
	this.setFieldValue = searchHandler_setFieldValue;
	this.getFieldValue = searchHandler_getFieldValue;
	this.getFieldDefault = searchHandler_getFieldDefault;
	this.getFieldType = searchHandler_getFieldType;
	this.protectNumber = searchHandler_protectNumber;
	this.unprotectNumber = searchHandler_unprotectNumber;
	
	this.setTab = searchHandler_setTab;
	this.setForm = searchHandler_setForm;

	this.setListings = searchHandler_setListings;
	this.setListing = searchHandler_setListing;
	this.unsetListing = searchHandler_unsetListing;

	this.setSubCats = searchHandler_setSubCats;
	this.setBCustomers = searchHandler_setBCustomers;
	this.setBodyTypes = searchHandler_setBodyTypes;

	this.onChange = searchHandler_onChange;
	this.submitForm = searchHandler_submitForm;
	this.setDefaults = searchHandler_setDefaults;
	this.removeDefault = searchHandler_removeDefault;
	this.restoreDefault = searchHandler_restoreDefault;

	this.clearFields = searchHandler_clearFields;
	this.clearFieldsOfType = searchHandler_clearFieldsOfType;
	this.formWillBeClearedForSearchByPostId = searchHandler_formWillBeClearedForSearchByPostId;

	this.validateForm = searchHandler_validateForm;
	this.validateField = searchHandler_validateField;
	this.validationFailedOnField = searchHandler_validationFailedOnField;
	this.enoughDataToSearch = searchHandler_enoughDataToSearch;
	this.enoughDataToSaveSearch = searchHandler_enoughDataToSaveSearch;
	this.savedSearchWithSameNameExists = searchHandler_savedSearchWithSameNameExists;

	this.changeMainCatId = searchHandler_changeMainCatId;
	this.changeSubCatId = searchHandler_changeSubCatId;
	this.detectCatId = searchHandler_detectCatId;
	this.serializeData = searchHandler_serializeData;

	this.doSearch = searchHandler_doSearch;
	this.doFilter = searchHandler_doFilter;
	this.doShowMarkedPosts = searchHandler_doShowMarkedPosts;
	
	this.oSearchWaitPlace.innerHTML = this.sEmptyHTML;
	this.setTab(this.getFieldValue('X_form_type', true));

	if (this.oSearchSimpleButtonsPlace)
		this.oSearchSimpleButtonsPlace.innerHTML = this.sSimpleButtonsHTML;
}

function searchHandler_submitForm()
{
	if (!this.bSubmitBlocked)
	{
		this.bSubmitBlocked = true;
		this.oForm.submit();
	}
}

function searchHandler_parseFieldId(sFieldId)
{
	sFieldId = sFieldId.replace(new RegExp('^' + this.sPrefix, "g"), ''); // If prefix present, remove it
	var sSearchType = sFieldId.substring(0, 1);
	var aKeys = new Array(sSearchType, sFieldId);

	return aKeys;
}

function searchHandler_setFieldValue(sField, sValue)
{
	var aKeys = this.parseFieldId(sField);
	var bFound = false;

	if (isEmpty(sValue))
		sValue = '';
		
	this.f[aKeys[0]][aKeys[1]]['value'] = sValue;

	oField = document.getElementById(this.sPrefix + sField);

	if (oField)
	{
		if (this.f[aKeys[0]][aKeys[1]]['subtype'] == 'checkbox')
		{
			if (sValue == 1)
				oField.checked = true;
			else
				oField.checked = false;
		}
		else
		{
			oField.value = sValue;
		}
	}
}

function searchHandler_getFieldValue(sField, bStored)
{
	var sValue = '';
	var aKeys = this.parseFieldId(sField)

	if (bStored)
	{
		if (this.f[aKeys[0]][aKeys[1]]['value'])
			sValue = this.f[aKeys[0]][aKeys[1]]['value'];
	}
	else
	{
		oField = document.getElementById(this.sPrefix + sField);

		if (oField)
		{
			if (this.f[aKeys[0]][aKeys[1]]['subtype'] == 'checkbox')
			{
				if (oField.checked)
					sValue = 1;
				else
					sValue = 0;
			}
			else
			{
				sValue = trim(oField.value);
			}
		}
	}

	return sValue;
}

function searchHandler_getFieldDefault(sField)
{
	var sDefault = '';
	var aKeys = this.parseFieldId(sField)

	if (this.f[aKeys[0]][aKeys[1]]['default'])
		sDefault = this.f[aKeys[0]][aKeys[1]]['default'];

	return sDefault;
}

function searchHandler_getFieldType(sField)
{
	var sType = '';
	var aKeys = this.parseFieldId(sField)

	sType = this.f[aKeys[0]][aKeys[1]]['type'];

	return sType;
}

function searchHandler_setTab(sFormType)  
{
	if (sFormType == 'generic')
	{
		if (this.oTabs.getActiveTab() != 0)
			this.oTabs.renderTabs(0);
	}
	else if (sFormType == 'detailed')
	{
		if (this.oTabs.getActiveTab() != 1)
			this.oTabs.renderTabs(1);
	}
	else if (sFormType == 'saved_searches')
	{
		if (this.oTabs.getActiveTab() != 2)
			this.oTabs.renderTabs(2);
	}

	this.setForm(sFormType);
}

function searchHandler_setForm(sNewFormType)
{
	var iMainCatId = null;
	var iSubCatId = null;
	var sNewSearchType = null;
	var sOldFormType = this.getFieldValue('X_form_type', true);
	var bFormTypeChanged = false;
	var bRefreshSubCats = false;
	var bRefreshBodyTypes = false;

	// Reset
	if (this.bReset)
	{
		bFormTypeChanged = true;
		this.setFieldValue('X_form_type', sNewFormType);

		if (sNewFormType == 'generic')
		{
			this.oGenericSearchPlace.innerHTML = this.sGenericSearchHTML;
			this.oActionsPlace.innerHTML = this.sActionsHTML;
		}
		else if (sNewFormType == 'detailed')
		{
			this.oGenericSearchPlace.innerHTML = this.sGenericSearchHTML;
			this.oDetailedSearchPlace.innerHTML = this.sDetailedSearchHTML;
			this.oActionsPlace.innerHTML = this.sActionsHTML;
		}
		else if (sNewFormType == 'saved_searches')
		{
			this.oSavedSearchesPlace.innerHTML = this.sSavedSearchesHTML;
		}
	}

	// Form type changed
	if (sNewFormType != sOldFormType)
	{
		bFormTypeChanged = true;
		this.setFieldValue('X_form_type', sNewFormType);

		if ((sOldFormType == 'generic') && (sNewFormType == 'detailed'))
		{
			this.oDetailedSearchPlace.innerHTML = this.sDetailedSearchHTML;
		}
		else if ((sOldFormType == 'generic') && (sNewFormType == 'saved_searches'))
		{
			this.oGenericSearchPlace.innerHTML = this.sEmptyHTML;
			this.oDetailedSearchPlace.innerHTML = this.sEmptyHTML;
			this.oDetailedSearchType_Place.innerHTML = this.sEmptyHTML;
			this.oActionsPlace.innerHTML = this.sEmptyHTML;
			this.oSavedSearchesPlace.innerHTML = this.sSavedSearchesHTML;
		}
		else if ((sOldFormType == 'detailed') && (sNewFormType == 'generic'))
		{
			this.oDetailedSearchPlace.innerHTML = this.sEmptyHTML;
			this.oDetailedSearchType_Place.innerHTML = this.sEmptyHTML;
		}
		else if ((sOldFormType == 'detailed') && (sNewFormType == 'saved_searches'))
		{
			this.oGenericSearchPlace.innerHTML = this.sEmptyHTML;
			this.oDetailedSearchPlace.innerHTML = this.sEmptyHTML;
			this.oDetailedSearchType_Place.innerHTML = this.sEmptyHTML;
			this.oActionsPlace.innerHTML = this.sEmptyHTML;
			this.oSavedSearchesPlace.innerHTML = this.sSavedSearchesHTML;
		}
		else if ((sOldFormType == 'saved_searches') && (sNewFormType == 'generic'))
		{
			this.oSavedSearchesPlace.innerHTML = this.sEmptyHTML;
			this.oGenericSearchPlace.innerHTML = this.sGenericSearchHTML;
			this.oActionsPlace.innerHTML = this.sActionsHTML;
		}
		else if ((sOldFormType == 'saved_searches') && (sNewFormType == 'detailed'))
		{
			this.oSavedSearchesPlace.innerHTML = this.sEmptyHTML;
			this.oGenericSearchPlace.innerHTML = this.sGenericSearchHTML;
			this.oDetailedSearchPlace.innerHTML = this.sDetailedSearchHTML;
			this.oActionsPlace.innerHTML = this.sActionsHTML;
		}
	}

	// Evaluate JavaScript
	var oScripts = this.oGenericSearchPlace.getElementsByTagName('script');
	for (var i = 0; i < oScripts.length; i++)
	{
		eval(oScripts[i].text);
	}
	oScripts = this.oDetailedSearchPlace.getElementsByTagName('script');
	for (var i = 0; i < oScripts.length; i++)
	{
		eval(oScripts[i].text);
	}

	// Cats management >>>

	if (this.bReset) // On first run, force loading preset cats
	{
		iMainCatId = this.getFieldValue('O_maincat_id', true);
		if (sNewFormType == 'detailed')
			iSubCatId = this.getFieldValue('O_subcat_id', true);
	}
	else if (bFormTypeChanged) // On form change, force no cats
	{
		iMainCatId = this.getFieldValue('O_maincat_id', false);
		if (sNewFormType == 'detailed')
			iSubCatId = '';
	}			
	else // On cat change
	{
		iMainCatId = this.getFieldValue('O_maincat_id', false);
		if (this.getFieldValue('O_maincat_id', true) != iMainCatId)
		{
			bRefreshSubCats = true;
			iSubCatId = '';
		}
		else if (sNewFormType == 'detailed')
		{
			iSubCatId = this.getFieldValue('O_subcat_id', false);
			bRefreshBodyTypes = true;
		}
	}

	this.setFieldValue('O_maincat_id', iMainCatId);
	if (sNewFormType == 'detailed')
		this.setFieldValue('O_subcat_id', iSubCatId);

	// <<< Cats management
		
	sNewSearchType = 'O';
	// Is subcat is set, it determines form type
	if (isNumeric(iSubCatId))
		sNewSearchType = this.c[this.protectNumber(iSubCatId)]['subtype'];
	// Always show realestate form when realestate maincat chosen
	if (isNumeric(iMainCatId) && (this.c[this.protectNumber(iMainCatId)]['subtype'] == 'R'))
		sNewSearchType = 'R';

	if (sNewFormType == 'generic')
	{
		if (bFormTypeChanged)
		{
			this.setDefaults('X');
			this.setListings('O');
			this.setDefaults('O');
		}
	}
	else if (sNewFormType == 'detailed')
	{
		if (bFormTypeChanged || (this.sSearchType != sNewSearchType))
		{
			this.sSearchType = sNewSearchType;
			if (sNewSearchType == 'C')
				this.oDetailedSearchType_Place.innerHTML = this.sDetailedSearchTypeCHTML;
			else if (sNewSearchType == 'R')
				this.oDetailedSearchType_Place.innerHTML = this.sDetailedSearchTypeRHTML;
			else
				this.oDetailedSearchType_Place.innerHTML = this.sEmptyHTML;
			this.setDefaults('X');
			this.setListings('O');
			this.setDefaults('O');
			if (sNewSearchType != 'O')
			{
				this.setListings(sNewSearchType);
				this.setDefaults(sNewSearchType);
			}
		}
		else if (bRefreshSubCats)
		{
			this.setSubCats();
		}
		else if (bRefreshBodyTypes && (sNewSearchType == 'C'))
		{
			this.setBodyTypes();
		}
	}

	if (this.sSearchType != sNewSearchType)
		this.sSearchType = sNewSearchType;

	this.bReset = false;
}

// Listings >>> -------------------------------------------

function searchHandler_setListings(sSearchType)
{
	var oField = null;
	
	for (var field in this.f[sSearchType])
	{
		// Custom
		if (this.f[sSearchType][field]['subtype'] == 'custom')
		{
			oField = document.getElementById(this.sPrefix + field);

			if (oField)
			{
				if (field == 'C_body_type')
					this.setBodyTypes();
				else if (field == 'O_subcat_id')
					this.setSubCats();
				else if (field == 'O_bcustomer_id')
					this.setBCustomers();
			}
		}
		// Regular
		else 
		{
			if (this.getFieldType(field) == 'dropdown')
				this.setListing(field, null, null);
		}

	}
}

function searchHandler_unsetListing(sField)
{
	var oField = document.getElementById(this.sPrefix + sField);
	
	if (oField)
		for (var i = oField.length - 1; i >= 0; i--)
			oField.options[i] = null;
}

function searchHandler_setListing(sField, sSubListing, sValue)
{
	var aKeys = this.parseFieldId(sField);
	var oField = null;
	var iIndex = 0;
	var bHasSelection = false;
	var aListing = null;
	var sDefault = null;

	// Is data present?
	if (isArray(this.f[aKeys[0]][aKeys[1]]['listing']))
	{
		if (!isNull(sSubListing) && isArray(this.f[aKeys[0]][aKeys[1]]['listing'][sSubListing]))
			aListing = this.f[aKeys[0]][aKeys[1]]['listing'][sSubListing];
		else
			aListing = this.f[aKeys[0]][aKeys[1]]['listing'];
	}

	// Data found
	if (!isNull(aListing))
		oField = document.getElementById(this.sPrefix + sField);

	// Object found and is in reset state
	if (oField && (oField.length == 0))
	{
		if (this.bReset || (this.bX24 && this.f[aKeys[0]][aKeys[1]]['static']))
			sValue = this.getFieldValue(sField, true);

		sDefault = this.getFieldDefault(sField);
		
		for (var k in aListing)
		{
			oField.options[iIndex] = new Option(aListing[k], this.unprotectNumber(k));
			if (isEmpty(sValue))
			{
				if (this.unprotectNumber(k) == sDefault)	
				{
					oField.options[iIndex].selected = true;
					bHasSelection = true;
				}
			}
			else
			{
				if (this.unprotectNumber(k) == sValue)
				{
					oField.options[iIndex].selected = true;
					bHasSelection = true;
				}
			}
			iIndex++;
		}
	
		if (!bHasSelection)
			oField.options[0].selected = true;
		
		this.onChange(oField);
	}
}

function searchHandler_setSubCats()
{
	var iMainCatId = this.getFieldValue('O_maincat_id', true);
	var iSubCatId = this.getFieldValue('O_subcat_id', true);

	this.unsetListing('O_subcat_id');

	this.f['O']['O_subcat_id']['listing'] = new Array();
	this.f['O']['O_subcat_id']['listing'][''] = this.res['subcat'];

	if (isNumeric(iMainCatId))
		for (var cat_id in this.c)
			if (this.c[cat_id]['catparent_id'] == iMainCatId)
				this.f['O']['O_subcat_id']['listing'][cat_id] = this.c[cat_id]['cat_name'];

	this.setListing('O_subcat_id', null, iSubCatId);
}

function searchHandler_setBCustomers()
{
	var iBCustomerId = null;

	if (this.bX24 && this.f['O']['O_bcustomer_id']['static'])
		iBCustomerId = this.getFieldValue('O_bcustomer_id', true);
	else
		iBCustomerId = this.getFieldValue('O_bcustomer_id', false);

	this.unsetListing('O_bcustomer_id');
	this.setListing('O_bcustomer_id', this.sSearchType, iBCustomerId);
}

function searchHandler_setBodyTypes()
{
	var iSubCatId = this.getFieldValue('O_subcat_id', true);
	var sBodyType = this.getFieldValue('C_body_type', false);

	this.unsetListing('C_body_type');

	if (isNumeric(iSubCatId))
		if (!isEmpty(this.d['cat_id_car_type'][iSubCatId]))
			this.setListing('C_body_type', this.d['cat_id_car_type'][iSubCatId], sBodyType);
}

// <<< Listings -------------------------------------------

// Clear fields >>> ---------------------------------------

function searchHandler_clearFields()
{
	this.clearFieldsOfType('X');
	this.clearFieldsOfType('O');
	if ((this.getFieldValue('X_form_type', true) == 'detailed') && (this.sSearchType != 'O'))
	{
		this.clearFieldsOfType(this.sSearchType);
		this.setForm('detailed');
	}
}

function searchHandler_clearFieldsOfType(sSearchType)
{
	var oField = null;
	for (var field in this.f[sSearchType])
		if ((this.f[sSearchType][field]['subtype'] != 'hidden') && !(this.bX24 && this.f[sSearchType][field]['static']))
			this.setFieldValue(field, '');
	this.setDefaults(sSearchType);
}

// <<< Clear fields ---------------------------------------

// Defaults >>> -------------------------------------------

function searchHandler_onChange(oField)
{
	var aKeys = null;

	if (oField)
	{
		aKeys = this.parseFieldId(oField.id)

		if
		(
			(oField.value == '')
			|| (oField.value == this.getFieldDefault(aKeys[1]))
		)
		{
			oField.style.color = this.sFG8; 
		}
		else
		{
			oField.style.color = this.sFG7; 
		}
	}
}

function searchHandler_setDefaults(sSearchType)
{
	var oField;

	for (var field in this.f[sSearchType])
	{
		oField = document.getElementById(this.sPrefix + field);
		this.restoreDefault(oField);
		this.onChange(oField);
	}
}

function searchHandler_restoreDefault(oField)
{
	var aKeys = null;
	var sValue = null;

	if (oField)
	{
		aKeys = this.parseFieldId(oField.id);
		if (this.getFieldType(aKeys[1]) != 'dropdown')
		{
			if (this.bReset)
			{
				sValue = this.getFieldValue(aKeys[1], true);
				this.setFieldValue(aKeys[1], sValue);
			} 
			if (isEmpty(this.getFieldValue(aKeys[1], false)))
			{
				oField.value = this.getFieldDefault(aKeys[1]);
				oField.style.color = this.sFG8; 
			}
		}
	}
}

function searchHandler_removeDefault(oField)
{
	var aKeys = null;

	if (oField)
	{
		aKeys = this.parseFieldId(oField.id)

		if (oField.value == this.getFieldDefault(aKeys[1]))
		{
			oField.value = '';
			oField.style.color = this.sFG7; 
		}
	}
}

// <<< Defaults -------------------------------------------

// Change maincat Id
function searchHandler_changeMainCatId()
{
	if (this.getFieldValue('X_form_type', true) == 'detailed')
		this.setForm('detailed');
}	

// Change subcat Id
function searchHandler_changeSubCatId()
{
	this.setForm('detailed');
}	

// Detect cat Id
function searchHandler_detectCatId()
{
	var sMake = null;
	var iCatId = null;
	var iMakeId = null;
	
	if ((this.sSearchType == 'C') && (this.getFieldValue('O_subcat_id', false) == 10695)) // HACK: Do detection only for cars
	{
		iMakeId = this.getFieldValue('C_make_id', false);
		if (isNumeric(iMakeId))
		{
			sMake = this.f['C']['C_make_id']['listing'][this.protectNumber(iMakeId)];
			for (var cat_id in this.c)
			{
				if (this.c[cat_id]['cat_name'] == sMake)
				{
					iCatId = this.unprotectNumber(cat_id);
					break;
				}
			}
		}
	}

	if (isEmpty(iCatId))
		if (this.getFieldValue('X_form_type', true) == 'detailed')
			iCatId = this.getFieldValue('O_subcat_id', false);

	if (isEmpty(iCatId))
		iCatId = this.getFieldValue('O_maincat_id', false);

	this.setFieldValue('X_cat_id', iCatId);

	this.oCatId.value = iCatId;
}

// Serialize data
function searchHandler_serializeData()
{
	var sData = '';
	var sValue = null;
	var sDefault = null;
	var sFormat = null;
	var iFieldsCount = 0;
	
	for (var search_type in this.f)
	{
		if ((search_type == 'X') || (search_type == 'O') || (search_type == this.sSearchType))
		{
			for (var field in this.f[search_type])
			{
				sValue = this.getFieldValue(field, false);

				if (!isEmpty(sValue))
				{
					sDefault = '';
					if (this.f[search_type][field]['default'])
						sDefault = this.f[search_type][field]['default'];
						
					if (sValue != sDefault)
					{
						sFormat = '';
						if (this.f[search_type][field]['format'])
							sFormat = this.f[search_type][field]['format'];

						if ((sFormat == 'float') || (sFormat == 'int'))
						{
							sValue = sValue.replace(/ /g, '');
							sValue = sValue.replace(/,/g, '.');
						}
						if (sFormat == 'varchar')
						{
							sValue = sValue.replace(/(<([^>]+)>)/gi, '');
						}

						sData += field + '|=>|' + encodeURIComponent(sValue) + '|,|';
						
						if (field != 'X_saved_search_name')
							iFieldsCount++;
					}
				}
			}
		}	
	}

	this.oData.value = sData;
	
	return iFieldsCount;
}

function searchHandler_protectNumber(sValue)
{
	sValue = '#' + sValue;
	return sValue;
}

function searchHandler_unprotectNumber(sValue)
{
	if (sValue.substr(0, 1) == '#')
		sValue = sValue.substr(1);
	return sValue;
}

// Validator >>> ------------------------------------------

function searchHandler_validateForm()
{
	var oField = null;

	for (var search_type in this.f)
		for (var field in this.f[search_type])
			if ((search_type == 'X') || (search_type == 'O') || (this.sSearchType == search_type))
				if (!this.validateField(field))
					return false;

	return true;
}

function searchHandler_validateField(sField)
{
	var aKeys = this.parseFieldId(sField);
	var sFormat = null;
	var sValue = null;
	var sDefault = null;
	
	// No format to validate against?
	if (!this.f[aKeys[0]][aKeys[1]]['format'])
		return true;

	// Nothing to validate?
	sValue = this.getFieldValue(sField, false);
	if (isEmpty(sValue))
		return true;

	// Default value set?
	sDefault = '';
	if (this.f[aKeys[0]][aKeys[1]]['default'])
		sDefault = trim(this.f[aKeys[0]][aKeys[1]]['default']);

	sFormat = this.f[aKeys[0]][aKeys[1]]['format'];

	if (sFormat == 'varchar')
	{
		if (sValue != sDefault)
			if (sValue.length < 2)
				return this.validationFailedOnField(sField);
	}
	else if (sFormat == 'int')
	{
		if (sValue != sDefault)
		{
			sValue = sValue.replace(/ /g, '');
			if (!isNumeric(sValue) || (Math.round(sValue) != sValue) || (sValue < -2147483648) || (sValue > 2147483647))
				return this.validationFailedOnField(sField);
		}
	}
	else if (sFormat == 'float')
	{
		if (sValue != sDefault)
		{
			sValue = sValue.replace(/ /g, '');
			sValue = sValue.replace(/,/g, '.');
			if (!isNumeric(sValue))
				return this.validationFailedOnField(sField);
		}
	}

	return true;
}

function searchHandler_validationFailedOnField(sField)
{
	oField = document.getElementById(this.sPrefix + sField);

	if (oField)
		oField.focus();

	return false;
}

function searchHandler_enoughDataToSearch(iFieldsCount)
{
	if (!this.bX24 && (iFieldsCount <= 1))
		return false;
	
	return true;
}

function searchHandler_formWillBeClearedForSearchByPostId(iFieldsCount)
{
	var sData = null;

	if
	(
		!isEmpty(this.getFieldValue('O_post_id', false))
		&& (this.getFieldValue('O_post_id', false) != this.getFieldDefault('O_post_id'))
	)
	{
		if (iFieldsCount > 2)
			return true;
	}
	
	return false;
}

// <<< Validator ------------------------------------------

function searchHandler_doSearch(bOnEnter)
{
	var aKeys = null;
	var sType = '';
	var iFieldsCount = 0;

	// Enter-submit handling
	if (bOnEnter)
	{
		if (this.oBrowser.isExplorer5)
		{
			aKeys = this.parseFieldId(event.srcElement.id)
			if (this.getFieldType(aKeys[1]) == 'dropdown')
				return false; // Cannot enter-submit from dropdown
			if (aKeys[1] == 'X_saved_search_name')
			{
				this.doSaveSearch(); // Redirect to saving search
				return true;
			}
		}
		else
		{
			//return false; // Unsupported browser
		}
	}

	if (this.validateForm())
	{
		this.oEvent.value = 'onsearch';
		this.detectCatId();
		iFieldsCount = this.serializeData(); //alert(this.oData.value); 
		if (this.enoughDataToSearch(iFieldsCount))
		{
			if (this.formWillBeClearedForSearchByPostId(iFieldsCount))
			{
				if (confirm(this.res['form_will_be_cleared_for_search_by_post_id']))
				{
					this.oCatId.value = ''; // HACK, to avoid cat lock
					this.submitForm();
				}
			}
			else
			{
				this.submitForm();
			}
		}
		else
		{
			alert(this.res['not_enough_data_to_search']);
		}
	}
	else
	{
		alert(this.res['erroneus_field']);
	}
}

// Saved searches >>> -------------------------------------

function searchHandler_enoughDataToSaveSearch(iFieldsCount)
{
	if
	(
		isEmpty(this.getFieldValue('X_saved_search_name', false))
		|| (this.getFieldValue('X_saved_search_name', false) == this.getFieldDefault('X_saved_search_name'))
	)
	{
		return false;
	}
	
	if (!isEmpty(this.getFieldValue('X_search_id', false)))
		iFieldsCount--;

	if (!this.enoughDataToSearch(iFieldsCount))
		return false;

	return true;
}

function searchHandler_savedSearchWithSameNameExists()
{
	var sName = this.getFieldValue('X_saved_search_name', false);
	
	for (var search_id in this.d['saved_searches'])
		if (sName == decodeURIComponent(this.d['saved_searches'][search_id]))
			return true;
	
	return false;
}

function searchHandler_doSaveSearch()
{
	var bOk = true;
	var sSavedSearchWithSameNameExists = this.res['saved_search_with_same_name_exists'];

	if (this.validateForm())
	{
		this.oEvent.value = 'onsavesearch';
		this.detectCatId();
		iFieldsCount = this.serializeData(); 

		if (!this.enoughDataToSaveSearch(iFieldsCount))
		{
			alert(this.res['not_enough_data_to_save_search']); bOk = false;
		}
		if (bOk && this.formWillBeClearedForSearchByPostId(iFieldsCount))
		{
			if (!confirm(this.res['form_will_be_cleared_for_search_by_post_id']))
				bOk = false;
		}
		if (bOk && this.savedSearchWithSameNameExists())
		{
			sSavedSearchWithSameNameExists = sSavedSearchWithSameNameExists.replace(/%saved_search_name%/g, this.getFieldValue('X_saved_search_name', false));
			if (!confirm(sSavedSearchWithSameNameExists))
				bOk = false;
		}
		if (bOk)
			this.submitForm();
	}
	else
	{
		alert(this.res['erroneus_field']);
	}
}

function searchHandler_doRestoreAndSearchSavedSearch(iSearchId)
{
	this.oEvent.value = 'onrestoreandsearchsavedsearch';
	this.setFieldValue('X_search_id', iSearchId);
	iFieldsCount = this.serializeData(); //alert(this.oData.value); 
	this.submitForm();
}

function searchHandler_doSearchSavedSearch(iSearchId)
{
	this.oEvent.value = 'onsearchsavedsearch';
	this.setFieldValue('X_search_id', iSearchId);
	iFieldsCount = this.serializeData(); //alert(this.oData.value); 
	this.submitForm();
}

function searchHandler_doDeleteSavedSearch(iSearchId, sName)
{
	if (confirm(this.res['sure_to_delete_saved_search'] + '\n"' + decodeURIComponent(sName) + '"?'))
	{
		this.oEvent.value = 'ondeletesavedsearch';
		this.setFieldValue('X_search_id', iSearchId);
		iFieldsCount = this.serializeData(); //alert(this.oData.value); 
		this.submitForm();
	}
}

// <<< Saved searches -------------------------------------

// Simple buttons >>> -------------------------------------

function searchHandler_doFilter(sField, sValue)
{
	var iSearchId = null;

	if (this.getFieldValue('X_form_type', true) == 'saved_searches')
	{
		iSearchId = this.getFieldValue('X_search_id', true);
		if (isNumeric(iSearchId))
		{
			if (sValue == '')
				sValue = '%null%';
			this.setFieldValue('X_' + sField, sValue);
			this.doRestoreAndSearchSavedSearch(iSearchId);
			return;
		}
	}

	this.setTab('detailed');
	oField = document.getElementById(this.sPrefix + 'O_' + sField);
	this.setFieldValue('O_' + sField, sValue);
	this.onChange(oField);
	this.doSearch(false);
}

function searchHandler_doShowMarkedPosts()
{
	if (this.getFieldValue('X_form_type', true) == 'saved_searches')
		this.setTab('detailed');

	oField = document.getElementById(this.sPrefix + 'O_marked_posts');
	if (oField)
	{
		this.clearFields();
		this.setFieldValue('O_marked_posts', 1);
		this.onChange(oField);
		this.doSearch(false);
	}
	else
	{
		alert(this.res['must_be_logged_in_to_mark_posts']);
	}
}

// <<< Simple buttons -------------------------------------

//-->

