var check_search = false;
var check_layer = false;

// variable for starting id on suggestion search
suggestion_id = -1;

/**
 * create advance search by search DOM
 * 
 * @return
 */
function createAdvanceSearch() {
	if (check_search == false) {

		// create search field
		var obj = document.createElement('input');
		obj.type = "text";
		obj.setAttribute("id", "advance_search");
		obj.setAttribute("size", 25);
		obj.setAttribute("autocomplete", "off");
		obj.className = 'blur';
		obj.setAttribute('value', 'write category');

		// input text field events
		obj.onfocus = function() {
			this.className = 'focus';
			emptyAdvancedField(this.value);
		};
		obj.onblur = function() {
			this.className = 'blur';
			emptyAdvancedField(this.value);
		};
		obj.onkeyup = function() {
			showLayer('advanced', 'advanced_layer', this.value, 'event');
			searchkeyPress('event');
		};

		// create categories suggestion layer
		var suggestions = document.createElement('div');
		suggestions.className = 'box';
		suggestions.setAttribute('id', 'advanced_layer');
		suggestions.style.width = '136px';
		suggestions.style.padding = '4px';
		// create break line
		var BreaK = document.createElement('br');

		// append child to parent
		document.getElementById("dom_search").appendChild(obj);
		document.getElementById("dom_search").appendChild(BreaK);
		document.getElementById('dom_search').appendChild(suggestions);

		// change text search
		document.getElementById("search_text").innerHTML = '<a href="#" onclick="createAdvanceSearch()" class="search_text">Normal</a>';
		check_search = true;
		check_layer = true;
	} else {
		document.getElementById("dom_search").innerHTML = "";
		document.getElementById("search_text").innerHTML = '<a href="#" onclick="createAdvanceSearch()" class="search_text">Advanced</a>';
		check_search = false;
		check_layer = false;
	}
	// auto show categroies on advanced layer
	if (document.getElementById('advanced_layer'))
		showLayer('advanced', 'advanced_layer', 'write category', 'event');
}
/**
 * output search suggestion box
 * 
 * @param type
 * @param id
 * @param key
 * @param e
 * @return
 */
function showLayer(type, id, key, e) {
	if (navigator.appName == 'Microsoft Internet Explorer'
			&& check_layer == true && type == 'search') {
	} else {
		(navigator.appName == "Netscape") ? press = e.which
				: press = event.keyCode;
		if (type == 'advanced' && (key == '' || key == 'write category')) {
			ajaxPageLoad(id, 'ajax/search.ajax.php', 'type=' + type + '&key='
					+ key);
		}
		if (press == 40 || press == 38 || press == 38 || press == 40) {
		} else {
			if (type == 'search' && (IsNumeric(key) || key == '')) {
				document.getElementById(id).style.visibility = 'hidden';
			} else {
				document.getElementById(id).style.visibility = 'visible';
				ajaxPageLoad(id, 'ajax/search.ajax.php', 'type=' + type
						+ '&key=' + key);
				document.getElementById(id).style.position = 'absolute';
			}
		}
	}
}
/**
 * all operation on mouse over for suugestion box insert suggested value in the
 * search input change class of suggestion td
 * 
 * @param id
 * @param field_id
 * @param value
 * @return
 */
function mouseOverOperation(id, field_id) {
	suggestion_id = -1;
	changeClass(id, 'selected pointer');
	document.getElementById(field_id).value = document.getElementById(id).firstChild.nodeValue;
}
/**
 * hide both search box suggestion
 * 
 * @param type
 * @return
 */
function hideBox(type) {
		suggestion_id = -1;
		//document.getElementById('advanced_layer').innerHTML = 'Loading...';
		showLayer('advanced', 'advanced_layer', document.getElementById('advance_search').value, 'event');
		document.getElementById('advanced_layer').style.visibility = 'hidden';
}
/**
 * key press function for suugestions
 * 
 * @param number
 * @param e
 * @return
 */
function searchkeyPress(e) {
	(navigator.appName == "Netscape") ? press = e.which : press = event.keyCode;
	// down key
	if (press == 40 || press == 40) {
		suggestion_id++;

		// check if suggestion id field exist or not
		if (!document.getElementById(suggestion_id)) {
		} else {

			// change class for current td field
			if (document.getElementById(suggestion_id - 1))
				changeClass(suggestion_id - 1, 'content');

			// change class and suggestion to search field
			changeClass(suggestion_id, 'selected pointer');
			document.getElementById('search_field').value = document
					.getElementById(suggestion_id).firstChild.nodeValue;
		}
	}
	// up key
	if (press == 38 || press == 38) {
		if (suggestion_id > -1) {
			suggestion_id--;
		}
		// check if suggestion id field exist or not
		if (!document.getElementById(suggestion_id)) {
		} else {

			// change class for current td field
			if (document.getElementById(suggestion_id + 1))

				changeClass(suggestion_id + 1, 'content');
			// change class and suggestion to search field
			changeClass(suggestion_id, 'selected pointer');
			document.getElementById('advance_search').value = document
					.getElementById(suggestion_id).firstChild.nodeValue;
		}
	}
}
/**
 * empty advanced search field or fill it if value is "write category"
 * 
 * @param key
 * @return
 */
function emptyAdvancedField(key) {
	if (key == '')
		document.getElementById('advance_search').value = 'write category';
	if (key == 'write category')
		document.getElementById('advance_search').value = '';
}
/**
 * empty normal search field or fill it if value is "write state/city or zip..."
 * 
 * @param key
 * @return
 */
function emptyNormalField(key) {
	if (key == '')
		document.getElementById('search_field').value = 'write state/city or zip...';
	if (key == 'write state/city or zip...')
		document.getElementById('search_field').value = '';
}
/**
 * show the suggestion layer
 * 
 * @param ajax_id
 * @param position_id
 * @param page
 * @param query
 * @param e
 * @return
 */
function suggestionLayer(ajax_id, page, query, e, key) {
	key = key.split(' ').join('');
	(navigator.appName == "Netscape") ? press = e.which : press = event.keyCode;
	if (press == 40 || press == 38) {
	} else {
		if (key == '') {
			document.getElementById(ajax_id).style.visibility = 'hidden';
			document.getElementById(ajax_id).innerHTML = 'loading...';
		} else {
			document.getElementById(ajax_id).style.visibility = 'visible';
			ajaxPageLoad(ajax_id, page, query);
			document.getElementById(ajax_id).style.position = 'absolute';
		}
	}
}
/**
 * close the suggestion layer
 * 
 * @param layer_id
 * @return
 */
function hideSuggestion(layer_id) {
	document.getElementById(layer_id).style.visibility = 'hidden';
	document.getElementById(layer_id).innerHTML = 'loading...';
}
/**
 * get the value from the suggestion layer and insert it on textfield
 * @param parent_id
 * @param value_id
 * @return
 */
function insertValue(parent_id, value_id) {
	value = document.getElementById(value_id).firstChild.nodeValue;
	//value = value.split(' ').join('');
	document.getElementById(parent_id).value = value;
}