function showNavHover(imageId, image) {
	document.getElementById(imageId).src = '../pub/images/nav.ng/hover_' + image + '.jpg'
}

function hideNavHover(imageId, image) {
	document.getElementById(imageId).src = '../pub/images/nav.ng/' + image + '.jpg'
}

function showSearchOptions() {
	document.getElementById('s_per_page').style.visibility = 'visible';
	document.getElementById('s_search_type').style.visibility = 'visible';
	document.getElementById('s_options_link').style.visibility = 'hidden';	
}

function preloadImages() {
	if (document.images) {
		for (var i = 1; i <= 7; i++) {
			showHover('0' + i);
			hideHover('0' + i);
		}	
	}
}

function toList() {
	window.scrollTo(0,0);
	document.getElementById('field_name').focus();
	document.getElementById('list_info_text').style.color = 'red';
}


function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}


var http = createRequestObject(); 

function getKeywordsCheck(sourceField, checkBoxId, type) {
	return getKeywords(sourceField, document.getElementById(checkBoxId).checked, type);
}

function getKeywords(sourceField, hidden, type) {

	if (document.getElementById('autoCompletition').checked) {
		var isList = (sourceField.type == 'textarea');
		var vis;
		
		if (hidden) {
			vis = 2;
		} else {
			vis = 1;
		}	
	
		var subWord = escape(getSubWord(sourceField).replace(/^\s+|\s+$/g, ""));

		if (subWord.length >= 2) {
			var dest = '../admin/index.php?action=admin.list.keywords&source=' + sourceField.id + '&islist=' + isList + 
				'&visibility=' + vis + '&type=' + type + '&key=' + subWord;	
			
			http.open('get', dest);
			http.onreadystatechange = handleProducts; 
			http.send(null);
		} else {
			document.getElementById('keyword_list').innerHTML = '';
		}
	}
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleProducts(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		document.getElementById('keyword_list').innerHTML = response;		
	}
}

function getSubWord(sourceField) {
	var currentValue = sourceField.value;
	
	if (sourceField.type == 'textarea') {
		var lastComma = currentValue.lastIndexOf(',') +1;
		return currentValue.substr(lastComma, currentValue.length);	
	} else {
		return currentValue;
	}

}

function addKeyword(sourceFieldId, keyword) {		
	var sourceField = document.getElementById(sourceFieldId);
	var currentValue = sourceField.value;
	var newValue;

	if (sourceField.type == 'textarea') {	
		var lastComma = currentValue.lastIndexOf(',');
		if (lastComma == -1) {
			lastComma = 0;
			newValue = '';
		} else {
			newValue = currentValue.substr(0, lastComma) + ', ';
		}
		newValue = newValue + keyword + ', ';
	} else {
		newValue = keyword;
	}
	
	sourceField.value = newValue;
	sourceField.focus();
}

function createBookmarkLink() {
	var title = "FIND - Werbemitteldatenbank"; 
	var url = "http://www.werbemitteldatenbank.de/";
	
	if (window.external) { // IE Favorite
		window.external.addFavorite(url, title); 
	} else { 
		alert('Bitte setzen Sie ein Lesezeichen, um FIND schneller erreichen zu können.'); 
	}
}

